Debian Codegraph @

third_party/rust/mp4parse/tests/public.rs

raw
1 /// Check if needed fields are still public.
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
5 use mp4parse as mp4;
6
7 use crate::mp4::{ParseStrictness, Status};
8 use std::convert::TryInto;
9 use std::fs::File;
10 use std::io::{Cursor, Read, Seek};
11
12 static MINI_MP4: &str = "tests/minimal.mp4";
13 static MINI_MP4_WITH_METADATA: &str = "tests/metadata.mp4";
14 static MINI_MP4_WITH_METADATA_STD_GENRE: &str = "tests/metadata_gnre.mp4";
15
16 static AUDIO_EME_CENC_MP4: &str = "tests/bipbop-cenc-audioinit.mp4";
17 static VIDEO_EME_CENC_MP4: &str = "tests/bipbop_480wp_1001kbps-cenc-video-key1-init.mp4";
18 // The cbcs files were created via shaka-packager from Firefox's test suite's bipbop.mp4 using:
19 // packager-win.exe
20 // in=bipbop.mp4,stream=audio,init_segment=bipbop_cbcs_audio_init.mp4,segment_template=bipbop_cbcs_audio_$Number$.m4s
21 // in=bipbop.mp4,stream=video,init_segment=bipbop_cbcs_video_init.mp4,segment_template=bipbop_cbcs_video_$Number$.m4s
22 // --protection_scheme cbcs --enable_raw_key_encryption
23 // --keys label=:key_id=7e571d047e571d047e571d047e571d21:key=7e5744447e5744447e5744447e574421
24 // --iv 11223344556677889900112233445566
25 // --generate_static_mpd --mpd_output bipbop_cbcs.mpd
26 // note: only the init files are needed for these tests
27 static AUDIO_EME_CBCS_MP4: &str = "tests/bipbop_cbcs_audio_init.mp4";
28 static VIDEO_EME_CBCS_MP4: &str = "tests/bipbop_cbcs_video_init.mp4";
29 static VIDEO_AV1_MP4: &str = "tests/tiny_av1.mp4";
30 // This file contains invalid userdata in its copyright userdata. See
31 // https://bugzilla.mozilla.org/show_bug.cgi?id=1687357 for more information.
32 static VIDEO_INVALID_USERDATA: &str = "tests/invalid_userdata.mp4";
33 static IMAGE_AVIF: &str = "tests/valid.avif";
34 static IMAGE_AVIF_EXTENTS: &str = "tests/multiple-extents.avif";
35 static IMAGE_AVIF_ALPHA: &str = "tests/valid-alpha.avif";
36 static IMAGE_AVIF_ALPHA_PREMULTIPLIED: &str = "tests/1x1-black-alpha-50pct-premultiplied.avif";
37 static IMAGE_AVIF_CORRUPT: &str = "tests/corrupt/bug-1655846.avif";
38 static IMAGE_AVIF_CORRUPT_2: &str = "tests/corrupt/bug-1661347.avif";
39 static IMAGE_AVIF_IPMA_BAD_VERSION: &str = "tests/bad-ipma-version.avif";
40 static IMAGE_AVIF_IPMA_BAD_FLAGS: &str = "tests/bad-ipma-flags.avif";
41 static IMAGE_AVIF_IPMA_DUPLICATE_VERSION_AND_FLAGS: &str =
42 "tests/corrupt/ipma-duplicate-version-and-flags.avif";
43 static IMAGE_AVIF_IPMA_DUPLICATE_ITEM_ID: &str = "tests/corrupt/ipma-duplicate-item_id.avif";
44 static IMAGE_AVIF_IPMA_INVALID_PROPERTY_INDEX: &str =
45 "tests/corrupt/ipma-invalid-property-index.avif";
46 static IMAGE_AVIF_NO_HDLR: &str = "tests/corrupt/hdlr-not-first.avif";
47 static IMAGE_AVIF_HDLR_NOT_FIRST: &str = "tests/corrupt/no-hdlr.avif";
48 static IMAGE_AVIF_HDLR_NOT_PICT: &str = "tests/corrupt/hdlr-not-pict.avif";
49 static IMAGE_AVIF_HDLR_NONZERO_RESERVED: &str = "tests/hdlr-nonzero-reserved.avif";
50 static IMAGE_AVIF_HDLR_MULTIPLE_NUL: &str = "tests/invalid-avif-hdlr-name-multiple-nul.avif";
51 static IMAGE_AVIF_NO_MIF1: &str = "tests/no-mif1.avif";
52 static IMAGE_AVIF_NO_PITM: &str = "tests/corrupt/no-pitm.avif";
53 static IMAGE_AVIF_NO_PIXI: &str = "tests/corrupt/no-pixi.avif";
54 static IMAGE_AVIF_NO_AV1C: &str = "tests/corrupt/no-av1C.avif";
55 static IMAGE_AVIF_NO_ISPE: &str = "tests/corrupt/no-ispe.avif";
56 static IMAGE_AVIF_NO_ALPHA_ISPE: &str = "tests/corrupt/no-alpha-ispe.avif";
57 static IMAGE_AVIF_TRANSFORM_ORDER: &str = "tests/corrupt/invalid-transformation-order.avif";
58 static IMAGE_AVIF_TRANSFORM_BEFORE_ISPE: &str = "tests/corrupt/transformation-before-ispe.avif";
59 static IMAGE_AVIF_NO_ALPHA_AV1C: &str = "tests/corrupt/no-alpha-av1C.avif";
60 static IMAGE_AVIF_NO_ALPHA_PIXI: &str = "tests/corrupt/no-pixi-for-alpha.avif";
61 static IMAGE_AVIF_AV1C_MISSING_ESSENTIAL: &str = "tests/av1C-missing-essential.avif";
62 static IMAGE_AVIF_A1LX_MARKED_ESSENTIAL: &str = "tests/corrupt/a1lx-marked-essential.avif";
63 static IMAGE_AVIF_A1OP_MISSING_ESSENTIAL: &str = "tests/corrupt/a1op-missing-essential.avif";
64 static IMAGE_AVIF_IMIR_MISSING_ESSENTIAL: &str = "tests/imir-missing-essential.avif";
65 static IMAGE_AVIF_IROT_MISSING_ESSENTIAL: &str = "tests/irot-missing-essential.avif";
66 static IMAGE_AVIF_LSEL_MISSING_ESSENTIAL: &str = "tests/corrupt/lsel-missing-essential.avif";
67 static IMAGE_AVIF_CLAP_MISSING_ESSENTIAL: &str = "tests/clap-missing-essential.avif";
68 static IMAGE_AVIF_UNKNOWN_MDAT_SIZE: &str = "tests/unknown_mdat.avif";
69 static IMAGE_AVIF_UNKNOWN_MDAT_SIZE_IN_OVERSIZED_META: &str =
70 "tests/unknown_mdat_in_oversized_meta.avif";
71 static IMAGE_AVIF_VALID_WITH_GARBAGE_OVERREAD_AT_END: &str =
72 "tests/valid_with_garbage_overread.avif";
73 static IMAGE_AVIF_VALID_WITH_GARBAGE_BYTE_AT_END: &str = "tests/valid_with_garbage_byte.avif";
74 static IMAGE_AVIF_WIDE_BOX_SIZE_0: &str = "tests/wide_box_size_0.avif";
75 static AVIF_TEST_DIRS: &[&str] = &["tests", "av1-avif/testFiles", "link-u-avif-sample-images"];
76
77 // These files are
78 // av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_a1op.avif
79 // av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_a1lx.avif
80 // av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_lsel.avif
81 // respectively, but with https://github.com/AOMediaCodec/av1-avif/issues/174 fixed
82 static AVIF_A1OP: &str = "tests/a1op.avif";
83 static AVIF_A1LX: &str = "tests/a1lx.avif";
84 static AVIF_LSEL: &str = "tests/lsel.avif";
85
86 static AVIF_CLAP: &str = "tests/clap-basic-1_3x3-to-1x1.avif";
87 static AVIF_GRID: &str = "av1-avif/testFiles/Microsoft/Summer_in_Tomsk_720p_5x4_grid.avif";
88 static AVIF_GRID_A1LX: &str =
89 "av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_grid_a1lx.avif";
90 static AVIF_AVIS_MAJOR_NO_PITM: &str =
91 "av1-avif/testFiles/Netflix/avis/Chimera-AV1-10bit-480x270.avif";
92 /// This is av1-avif/testFiles/Netflix/avis/alpha_video.avif
93 /// but with https://github.com/AOMediaCodec/av1-avif/issues/177 fixed
94 static AVIF_AVIS_MAJOR_WITH_PITM_AND_ALPHA: &str = "tests/alpha_video_fixed.avif";
95 static AVIF_AVIS_WITH_NO_PITM_NO_ILOC: &str = "tests/avis_with_no_ptim_no_iloc.avif";
96 static AVIF_AVIS_WITH_PITM_NO_ILOC: &str = "tests/avis_with_pitm_no_iloc.avif";
97 static AVIF_AVIS_MAJOR_NO_MOOV: &str = "tests/corrupt/alpha_video_moov_is_moop.avif";
98 static AVIF_AVIS_NO_LOOP: &str = "tests/loop_none.avif";
99 static AVIF_AVIS_LOOP_FOREVER: &str = "tests/loop_forever.avif";
100 static AVIF_NO_PIXI_IMAGES: &[&str] = &[IMAGE_AVIF_NO_PIXI, IMAGE_AVIF_NO_ALPHA_PIXI];
101 static AVIF_UNSUPPORTED_IMAGES: &[&str] = &[
102 AVIF_A1LX,
103 AVIF_A1OP,
104 AVIF_CLAP,
105 IMAGE_AVIF_CLAP_MISSING_ESSENTIAL,
106 AVIF_GRID,
107 AVIF_GRID_A1LX,
108 AVIF_LSEL,
109 "av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_a1lx.avif",
110 "av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_a1op.avif",
111 "av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_a1op_lsel.avif",
112 "av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_lsel.avif",
113 "av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_grid_lsel.avif",
114 "av1-avif/testFiles/Link-U/kimono.crop.avif",
115 "av1-avif/testFiles/Link-U/kimono.mirror-vertical.rotate270.crop.avif",
116 "av1-avif/testFiles/Microsoft/Chimera_10bit_cropped_to_1920x1008.avif",
117 "av1-avif/testFiles/Microsoft/Chimera_10bit_cropped_to_1920x1008_with_HDR_metadata.avif",
118 "av1-avif/testFiles/Microsoft/Chimera_8bit_cropped_480x256.avif",
119 "av1-avif/testFiles/Xiph/abandoned_filmgrain.avif",
120 "av1-avif/testFiles/Xiph/fruits_2layer_thumbsize.avif",
121 "av1-avif/testFiles/Xiph/quebec_3layer_op2.avif",
122 "av1-avif/testFiles/Xiph/tiger_3layer_1res.avif",
123 "av1-avif/testFiles/Xiph/tiger_3layer_3res.avif",
124 "link-u-avif-sample-images/kimono.crop.avif",
125 "link-u-avif-sample-images/kimono.mirror-vertical.rotate270.crop.avif",
126 ];
127 /// See https://github.com/AOMediaCodec/av1-avif/issues/150
128 /// https://github.com/AOMediaCodec/av1-avif/issues/174
129 /// https://github.com/AOMediaCodec/av1-avif/issues/177
130 /// and https://github.com/AOMediaCodec/av1-avif/issues/178
131 // TODO: make this into a map of expected errors?
132 static AV1_AVIF_CORRUPT_IMAGES: &[&str] = &[
133 IMAGE_AVIF_UNKNOWN_MDAT_SIZE_IN_OVERSIZED_META,
134 IMAGE_AVIF_WIDE_BOX_SIZE_0,
135 "av1-avif/testFiles/Link-U/kimono.crop.avif",
136 "av1-avif/testFiles/Link-U/kimono.mirror-horizontal.avif",
137 "av1-avif/testFiles/Link-U/kimono.mirror-vertical.avif",
138 "av1-avif/testFiles/Link-U/kimono.mirror-vertical.rotate270.avif",
139 "av1-avif/testFiles/Link-U/kimono.mirror-vertical.rotate270.crop.avif",
140 "av1-avif/testFiles/Link-U/kimono.rotate90.avif",
141 "av1-avif/testFiles/Link-U/kimono.rotate270.avif",
142 "link-u-avif-sample-images/kimono.crop.avif",
143 "link-u-avif-sample-images/kimono.mirror-horizontal.avif",
144 "link-u-avif-sample-images/kimono.mirror-vertical.avif",
145 "link-u-avif-sample-images/kimono.mirror-vertical.rotate270.avif",
146 "link-u-avif-sample-images/kimono.mirror-vertical.rotate270.crop.avif",
147 "link-u-avif-sample-images/kimono.rotate90.avif",
148 "link-u-avif-sample-images/kimono.rotate270.avif",
149 "link-u-avif-sample-images/plum-blossom-large.profile0.10bpc.yuv420.alpha-full.avif",
150 "link-u-avif-sample-images/plum-blossom-large.profile0.10bpc.yuv420.alpha-full.monochrome.avif",
151 "link-u-avif-sample-images/plum-blossom-large.profile0.10bpc.yuv420.alpha-limited.avif",
152 "link-u-avif-sample-images/plum-blossom-large.profile0.10bpc.yuv420.alpha-limited.monochrome.avif",
153 "link-u-avif-sample-images/plum-blossom-large.profile0.8bpc.yuv420.alpha-full.avif",
154 "link-u-avif-sample-images/plum-blossom-large.profile0.8bpc.yuv420.alpha-full.monochrome.avif",
155 "link-u-avif-sample-images/plum-blossom-large.profile0.8bpc.yuv420.alpha-limited.avif",
156 "link-u-avif-sample-images/plum-blossom-large.profile0.8bpc.yuv420.alpha-limited.monochrome.avif",
157 "link-u-avif-sample-images/plum-blossom-large.profile1.10bpc.yuv444.alpha-full.avif",
158 "link-u-avif-sample-images/plum-blossom-large.profile1.10bpc.yuv444.alpha-limited.avif",
159 "link-u-avif-sample-images/plum-blossom-large.profile1.8bpc.yuv444.alpha-full.avif",
160 "link-u-avif-sample-images/plum-blossom-large.profile1.8bpc.yuv444.alpha-limited.avif",
161 "link-u-avif-sample-images/plum-blossom-large.profile2.10bpc.yuv422.alpha-full.avif",
162 "link-u-avif-sample-images/plum-blossom-large.profile2.10bpc.yuv422.alpha-full.monochrome.avif",
163 "link-u-avif-sample-images/plum-blossom-large.profile2.10bpc.yuv422.alpha-limited.avif",
164 "link-u-avif-sample-images/plum-blossom-large.profile2.10bpc.yuv422.alpha-limited.monochrome.avif",
165 "link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv420.alpha-full.avif",
166 "link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv420.alpha-full.monochrome.avif",
167 "link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv420.alpha-limited.avif",
168 "link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv420.alpha-limited.monochrome.avif",
169 "link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv422.alpha-full.avif",
170 "link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv422.alpha-full.monochrome.avif",
171 "link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv422.alpha-limited.avif",
172 "link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv422.alpha-limited.monochrome.avif",
173 "link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv444.alpha-full.avif",
174 "link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv444.alpha-full.monochrome.avif",
175 "link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv444.alpha-limited.avif",
176 "link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv444.alpha-limited.monochrome.avif",
177 "link-u-avif-sample-images/plum-blossom-large.profile2.8bpc.yuv422.alpha-full.avif",
178 "link-u-avif-sample-images/plum-blossom-large.profile2.8bpc.yuv422.alpha-full.monochrome.avif",
179 "link-u-avif-sample-images/plum-blossom-large.profile2.8bpc.yuv422.alpha-limited.avif",
180 "link-u-avif-sample-images/plum-blossom-large.profile2.8bpc.yuv422.alpha-limited.monochrome.avif",
181 "link-u-avif-sample-images/plum-blossom-small.profile0.10bpc.yuv420.alpha-full.avif",
182 "link-u-avif-sample-images/plum-blossom-small.profile0.10bpc.yuv420.alpha-full.monochrome.avif",
183 "link-u-avif-sample-images/plum-blossom-small.profile0.10bpc.yuv420.alpha-limited.avif",
184 "link-u-avif-sample-images/plum-blossom-small.profile0.10bpc.yuv420.alpha-limited.monochrome.avif",
185 "link-u-avif-sample-images/plum-blossom-small.profile0.8bpc.yuv420.alpha-full.avif",
186 "link-u-avif-sample-images/plum-blossom-small.profile0.8bpc.yuv420.alpha-full.monochrome.avif",
187 "link-u-avif-sample-images/plum-blossom-small.profile0.8bpc.yuv420.alpha-limited.avif",
188 "link-u-avif-sample-images/plum-blossom-small.profile0.8bpc.yuv420.alpha-limited.monochrome.avif",
189 "link-u-avif-sample-images/plum-blossom-small.profile1.10bpc.yuv444.alpha-full.avif",
190 "link-u-avif-sample-images/plum-blossom-small.profile1.10bpc.yuv444.alpha-limited.avif",
191 "link-u-avif-sample-images/plum-blossom-small.profile1.8bpc.yuv444.alpha-full.avif",
192 "link-u-avif-sample-images/plum-blossom-small.profile1.8bpc.yuv444.alpha-limited.avif",
193 "link-u-avif-sample-images/plum-blossom-small.profile2.10bpc.yuv422.alpha-full.avif",
194 "link-u-avif-sample-images/plum-blossom-small.profile2.10bpc.yuv422.alpha-full.monochrome.avif",
195 "link-u-avif-sample-images/plum-blossom-small.profile2.10bpc.yuv422.alpha-limited.avif",
196 "link-u-avif-sample-images/plum-blossom-small.profile2.10bpc.yuv422.alpha-limited.monochrome.avif",
197 "link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv420.alpha-full.avif",
198 "link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv420.alpha-full.monochrome.avif",
199 "link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv420.alpha-limited.avif",
200 "link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv420.alpha-limited.monochrome.avif",
201 "link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv422.alpha-full.avif",
202 "link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv422.alpha-full.monochrome.avif",
203 "link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv422.alpha-limited.avif",
204 "link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv422.alpha-limited.monochrome.avif",
205 "link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv444.alpha-full.avif",
206 "link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv444.alpha-full.monochrome.avif",
207 "link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv444.alpha-limited.avif",
208 "link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv444.alpha-limited.monochrome.avif",
209 "link-u-avif-sample-images/plum-blossom-small.profile2.8bpc.yuv422.alpha-full.avif",
210 "link-u-avif-sample-images/plum-blossom-small.profile2.8bpc.yuv422.alpha-full.monochrome.avif",
211 "link-u-avif-sample-images/plum-blossom-small.profile2.8bpc.yuv422.alpha-limited.avif",
212 "link-u-avif-sample-images/plum-blossom-small.profile2.8bpc.yuv422.alpha-limited.monochrome.avif",
213 ];
214 static AVIF_CORRUPT_IMAGES_DIR: &str = "tests/corrupt";
215 // The 1 frame h263 3gp file can be generated by ffmpeg with command
216 // "ffmpeg -i [input file] -f 3gp -vcodec h263 -vf scale=176x144 -frames:v 1 -an output.3gp"
217 static VIDEO_H263_3GP: &str = "tests/bbb_sunflower_QCIF_30fps_h263_noaudio_1f.3gp";
218 // The 1 frame hevc mp4 file generated by ffmpeg with command
219 // "ffmpeg -f lavfi -i color=c=white:s=640x480 -c:v libx265 -frames:v 1 -pix_fmt yuv420p hevc_white_frame.mp4"
220 static VIDEO_HEVC_MP4: &str = "tests/hevc_white_frame.mp4";
221 // The 1 frame AMR-NB 3gp file can be generated by ffmpeg with command
222 // "ffmpeg -i [input file] -f 3gp -acodec amr_nb -ar 8000 -ac 1 -frames:a 1 -vn output.3gp"
223 #[cfg(feature = "3gpp")]
224 static AUDIO_AMRNB_3GP: &str = "tests/amr_nb_1f.3gp";
225 // The 1 frame AMR-WB 3gp file can be generated by ffmpeg with command
226 // "ffmpeg -i [input file] -f 3gp -acodec amr_wb -ar 16000 -ac 1 -frames:a 1 -vn output.3gp"
227 #[cfg(feature = "3gpp")]
228 static AUDIO_AMRWB_3GP: &str = "tests/amr_wb_1f.3gp";
229 #[cfg(feature = "mp4v")]
230 // The 1 frame mp4v mp4 file can be generated by ffmpeg with command
231 // "ffmpeg -i [input file] -f mp4 -c:v mpeg4 -vf scale=176x144 -frames:v 1 -an output.mp4"
232 static VIDEO_MP4V_MP4: &str = "tests/bbb_sunflower_QCIF_30fps_mp4v_noaudio_1f.mp4";
233
234 // The 1 frame h264 mp4 file with pasp box generated by ffmpeg with command
235 // ffmpeg -f lavfi -i color=c=white:s=640x480 -c:v libx264 -frames:v 1 -pix_fmt yuv420p -vf "setsar=16/9" h264_white_frame_sar_16_9.mp4
236 static VIDEO_H264_PASP_MP4: &str = "tests/h264_white_frame_sar_16_9.mp4";
237
238 // Adapted from https://github.com/GuillaumeGomez/audio-video-metadata/blob/9dff40f565af71d5502e03a2e78ae63df95cfd40/src/metadata.rs#L53
239 #[test]
240 fn public_api() {
241 let mut fd = File::open(MINI_MP4).expect("Unknown file");
242 let mut buf = Vec::new();
243 fd.read_to_end(&mut buf).expect("File error");
244
245 let mut c = Cursor::new(&buf);
246 let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
247 assert_eq!(context.timescale, Some(mp4::MediaTimeScale(1000)));
248 for track in context.tracks {
249 match track.track_type {
250 mp4::TrackType::Video => {
251 // track part
252 assert_eq!(track.duration, Some(mp4::TrackScaledTime(512, 0)));
253 assert_eq!(track.empty_duration, Some(mp4::MediaScaledTime(0)));
254 assert_eq!(track.media_time, Some(mp4::TrackScaledTime(0, 0)));
255 assert_eq!(track.timescale, Some(mp4::TrackTimeScale(12800, 0)));
256
257 // track.tkhd part
258 let tkhd = track.tkhd.unwrap();
259 assert!(!tkhd.disabled);
260 assert_eq!(tkhd.duration, 40);
261 assert_eq!(tkhd.width, 20_971_520);
262 assert_eq!(tkhd.height, 15_728_640);
263
264 // track.stsd part
265 let stsd = track.stsd.expect("expected an stsd");
266 let v = match stsd.descriptions.first().expect("expected a SampleEntry") {
267 mp4::SampleEntry::Video(v) => v,
268 _ => panic!("expected a VideoSampleEntry"),
269 };
270 assert_eq!(v.width, 320);
271 assert_eq!(v.height, 240);
272 assert_eq!(
273 match v.codec_specific {
274 mp4::VideoCodecSpecific::AVCConfig(ref avc) => {
275 assert!(!avc.is_empty());
276 "AVC"
277 }
278 mp4::VideoCodecSpecific::VPxConfig(ref vpx) => {
279 // We don't enter in here, we just check if fields are public.
280 assert!(vpx.bit_depth > 0);
281 assert!(vpx.colour_primaries > 0);
282 assert!(vpx.chroma_subsampling > 0);
283 assert!(!vpx.codec_init.is_empty());
284 "VPx"
285 }
286 mp4::VideoCodecSpecific::ESDSConfig(ref mp4v) => {
287 assert!(!mp4v.is_empty());
288 "MP4V"
289 }
290 mp4::VideoCodecSpecific::AV1Config(ref _av1c) => {
291 "AV1"
292 }
293 mp4::VideoCodecSpecific::H263Config(ref _h263) => {
294 "H263"
295 }
296 mp4::VideoCodecSpecific::HEVCConfig(ref hevc) => {
297 assert!(!hevc.is_empty());
298 "HEVC"
299 }
300 },
301 "AVC"
302 );
303 }
304 mp4::TrackType::Audio => {
305 // track part
306 assert_eq!(track.duration, Some(mp4::TrackScaledTime(2944, 1)));
307 assert_eq!(track.empty_duration, Some(mp4::MediaScaledTime(0)));
308 assert_eq!(track.media_time, Some(mp4::TrackScaledTime(1024, 1)));
309 assert_eq!(track.timescale, Some(mp4::TrackTimeScale(48000, 1)));
310
311 // track.tkhd part
312 let tkhd = track.tkhd.unwrap();
313 assert!(!tkhd.disabled);
314 assert_eq!(tkhd.duration, 62);
315 assert_eq!(tkhd.width, 0);
316 assert_eq!(tkhd.height, 0);
317
318 // track.stsd part
319 let stsd = track.stsd.expect("expected an stsd");
320 let a = match stsd.descriptions.first().expect("expected a SampleEntry") {
321 mp4::SampleEntry::Audio(a) => a,
322 _ => panic!("expected a AudioSampleEntry"),
323 };
324 assert_eq!(
325 match a.codec_specific {
326 mp4::AudioCodecSpecific::ES_Descriptor(ref esds) => {
327 assert_eq!(esds.audio_codec, mp4::CodecType::AAC);
328 assert_eq!(esds.audio_sample_rate.unwrap(), 48000);
329 assert_eq!(esds.audio_object_type.unwrap(), 2);
330 "ES"
331 }
332 mp4::AudioCodecSpecific::FLACSpecificBox(ref flac) => {
333 // STREAMINFO block must be present and first.
334 assert!(!flac.blocks.is_empty());
335 assert_eq!(flac.blocks[0].block_type, 0);
336 assert_eq!(flac.blocks[0].data.len(), 34);
337 "FLAC"
338 }
339 mp4::AudioCodecSpecific::OpusSpecificBox(ref opus) => {
340 // We don't enter in here, we just check if fields are public.
341 assert!(opus.version > 0);
342 "Opus"
343 }
344 mp4::AudioCodecSpecific::ALACSpecificBox(ref alac) => {
345 assert!(alac.data.len() == 24 || alac.data.len() == 48);
346 "ALAC"
347 }
348 mp4::AudioCodecSpecific::MP3 => {
349 "MP3"
350 }
351 mp4::AudioCodecSpecific::LPCM => {
352 "LPCM"
353 }
354 #[cfg(feature = "3gpp")]
355 mp4::AudioCodecSpecific::AMRSpecificBox(_) => {
356 "AMR"
357 }
358 },
359 "ES"
360 );
361 assert!(a.samplesize > 0);
362 assert!(a.samplerate > 0.0);
363 }
364 _ => {}
365 }
366 }
367 }
368
369 #[test]
370 fn public_metadata() {
371 let mut fd = File::open(MINI_MP4_WITH_METADATA).expect("Unknown file");
372 let mut buf = Vec::new();
373 fd.read_to_end(&mut buf).expect("File error");
374
375 let mut c = Cursor::new(&buf);
376 let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
377 let udta = context
378 .userdata
379 .expect("didn't find udta")
380 .expect("failed to parse udta");
381 let meta = udta.meta.expect("didn't find meta");
382 assert_eq!(meta.title.unwrap(), "Title");
383 assert_eq!(meta.artist.unwrap(), "Artist");
384 assert_eq!(meta.album_artist.unwrap(), "Album Artist");
385 assert_eq!(meta.comment.unwrap(), "Comments");
386 assert_eq!(meta.year.unwrap(), "2019");
387 assert_eq!(
388 meta.genre.unwrap(),
389 mp4::Genre::CustomGenre("Custom Genre".try_into().unwrap())
390 );
391 assert_eq!(meta.encoder.unwrap(), "Lavf56.40.101");
392 assert_eq!(meta.encoded_by.unwrap(), "Encoded-by");
393 assert_eq!(meta.copyright.unwrap(), "Copyright");
394 assert_eq!(meta.track_number.unwrap(), 3);
395 assert_eq!(meta.total_tracks.unwrap(), 6);
396 assert_eq!(meta.disc_number.unwrap(), 5);
397 assert_eq!(meta.total_discs.unwrap(), 10);
398 assert_eq!(meta.beats_per_minute.unwrap(), 128);
399 assert_eq!(meta.composer.unwrap(), "Composer");
400 assert!(meta.compilation.unwrap());
401 assert!(!meta.gapless_playback.unwrap());
402 assert!(!meta.podcast.unwrap());
403 assert_eq!(meta.advisory.unwrap(), mp4::AdvisoryRating::Clean);
404 assert_eq!(meta.media_type.unwrap(), mp4::MediaType::Normal);
405 assert_eq!(meta.rating.unwrap(), "50");
406 assert_eq!(meta.grouping.unwrap(), "Grouping");
407 assert_eq!(meta.category.unwrap(), "Category");
408 assert_eq!(meta.keyword.unwrap(), "Keyword");
409 assert_eq!(meta.description.unwrap(), "Description");
410 assert_eq!(meta.lyrics.unwrap(), "Lyrics");
411 assert_eq!(meta.long_description.unwrap(), "Long Description");
412 assert_eq!(meta.tv_episode_name.unwrap(), "Episode Name");
413 assert_eq!(meta.tv_network_name.unwrap(), "Network Name");
414 assert_eq!(meta.tv_episode_number.unwrap(), 15);
415 assert_eq!(meta.tv_season.unwrap(), 10);
416 assert_eq!(meta.tv_show_name.unwrap(), "Show Name");
417 assert!(meta.hd_video.unwrap());
418 assert_eq!(meta.owner.unwrap(), "Owner");
419 assert_eq!(meta.sort_name.unwrap(), "Sort Name");
420 assert_eq!(meta.sort_album.unwrap(), "Sort Album");
421 assert_eq!(meta.sort_artist.unwrap(), "Sort Artist");
422 assert_eq!(meta.sort_album_artist.unwrap(), "Sort Album Artist");
423 assert_eq!(meta.sort_composer.unwrap(), "Sort Composer");
424
425 // Check for valid JPEG header
426 let covers = meta.cover_art.unwrap();
427 let cover = &covers[0];
428 let mut bytes = [0u8; 4];
429 bytes[0] = cover[0];
430 bytes[1] = cover[1];
431 bytes[2] = cover[2];
432 assert_eq!(u32::from_le_bytes(bytes), 0x00ff_d8ff);
433 }
434
435 #[test]
436 fn public_metadata_gnre() {
437 let mut fd = File::open(MINI_MP4_WITH_METADATA_STD_GENRE).expect("Unknown file");
438 let mut buf = Vec::new();
439 fd.read_to_end(&mut buf).expect("File error");
440
441 let mut c = Cursor::new(&buf);
442 let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
443 let udta = context
444 .userdata
445 .expect("didn't find udta")
446 .expect("failed to parse udta");
447 let meta = udta.meta.expect("didn't find meta");
448 assert_eq!(meta.title.unwrap(), "Title");
449 assert_eq!(meta.artist.unwrap(), "Artist");
450 assert_eq!(meta.album_artist.unwrap(), "Album Artist");
451 assert_eq!(meta.comment.unwrap(), "Comments");
452 assert_eq!(meta.year.unwrap(), "2019");
453 assert_eq!(meta.genre.unwrap(), mp4::Genre::StandardGenre(3));
454 assert_eq!(meta.encoder.unwrap(), "Lavf56.40.101");
455 assert_eq!(meta.encoded_by.unwrap(), "Encoded-by");
456 assert_eq!(meta.copyright.unwrap(), "Copyright");
457 assert_eq!(meta.track_number.unwrap(), 3);
458 assert_eq!(meta.total_tracks.unwrap(), 6);
459 assert_eq!(meta.disc_number.unwrap(), 5);
460 assert_eq!(meta.total_discs.unwrap(), 10);
461 assert_eq!(meta.beats_per_minute.unwrap(), 128);
462 assert_eq!(meta.composer.unwrap(), "Composer");
463 assert!(meta.compilation.unwrap());
464 assert!(!meta.gapless_playback.unwrap());
465 assert!(!meta.podcast.unwrap());
466 assert_eq!(meta.advisory.unwrap(), mp4::AdvisoryRating::Clean);
467 assert_eq!(meta.media_type.unwrap(), mp4::MediaType::Normal);
468 assert_eq!(meta.rating.unwrap(), "50");
469 assert_eq!(meta.grouping.unwrap(), "Grouping");
470 assert_eq!(meta.category.unwrap(), "Category");
471 assert_eq!(meta.keyword.unwrap(), "Keyword");
472 assert_eq!(meta.description.unwrap(), "Description");
473 assert_eq!(meta.lyrics.unwrap(), "Lyrics");
474 assert_eq!(meta.long_description.unwrap(), "Long Description");
475 assert_eq!(meta.tv_episode_name.unwrap(), "Episode Name");
476 assert_eq!(meta.tv_network_name.unwrap(), "Network Name");
477 assert_eq!(meta.tv_episode_number.unwrap(), 15);
478 assert_eq!(meta.tv_season.unwrap(), 10);
479 assert_eq!(meta.tv_show_name.unwrap(), "Show Name");
480 assert!(meta.hd_video.unwrap());
481 assert_eq!(meta.owner.unwrap(), "Owner");
482 assert_eq!(meta.sort_name.unwrap(), "Sort Name");
483 assert_eq!(meta.sort_album.unwrap(), "Sort Album");
484 assert_eq!(meta.sort_artist.unwrap(), "Sort Artist");
485 assert_eq!(meta.sort_album_artist.unwrap(), "Sort Album Artist");
486 assert_eq!(meta.sort_composer.unwrap(), "Sort Composer");
487
488 // Check for valid JPEG header
489 let covers = meta.cover_art.unwrap();
490 let cover = &covers[0];
491 let mut bytes = [0u8; 4];
492 bytes[0] = cover[0];
493 bytes[1] = cover[1];
494 bytes[2] = cover[2];
495 assert_eq!(u32::from_le_bytes(bytes), 0x00ff_d8ff);
496 }
497
498 #[test]
499 fn public_invalid_metadata() {
500 // Test that reading userdata containing invalid metadata is not fatal to parsing and that
501 // expected values are still found.
502 let mut fd = File::open(VIDEO_INVALID_USERDATA).expect("Unknown file");
503 let mut buf = Vec::new();
504 fd.read_to_end(&mut buf).expect("File error");
505
506 let mut c = Cursor::new(&buf);
507 let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
508 // Should have userdata.
509 assert!(context.userdata.is_some());
510 // But it should contain an error.
511 assert!(context.userdata.unwrap().is_err());
512 // Smoke test that other data has been parsed. Don't check everything, just make sure some
513 // values are as expected.
514 assert_eq!(context.tracks.len(), 2);
515 for track in context.tracks {
516 match track.track_type {
517 mp4::TrackType::Video => {
518 // Check some of the values in the video tkhd.
519 let tkhd = track.tkhd.unwrap();
520 assert!(!tkhd.disabled);
521 assert_eq!(tkhd.duration, 231232);
522 assert_eq!(tkhd.width, 83_886_080);
523 assert_eq!(tkhd.height, 47_185_920);
524 }
525 mp4::TrackType::Audio => {
526 // Check some of the values in the audio tkhd.
527 let tkhd = track.tkhd.unwrap();
528 assert!(!tkhd.disabled);
529 assert_eq!(tkhd.duration, 231338);
530 assert_eq!(tkhd.width, 0);
531 assert_eq!(tkhd.height, 0);
532 }
533 _ => panic!("File should not contain other tracks."),
534 }
535 }
536 }
537
538 #[test]
539 fn public_audio_tenc() {
540 let kid = vec![
541 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d,
542 0x04,
543 ];
544
545 let mut fd = File::open(AUDIO_EME_CENC_MP4).expect("Unknown file");
546 let mut buf = Vec::new();
547 fd.read_to_end(&mut buf).expect("File error");
548
549 let mut c = Cursor::new(&buf);
550 let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
551 for track in context.tracks {
552 let stsd = track.stsd.expect("expected an stsd");
553 let a = match stsd.descriptions.first().expect("expected a SampleEntry") {
554 mp4::SampleEntry::Audio(a) => a,
555 _ => panic!("expected a AudioSampleEntry"),
556 };
557 assert_eq!(a.codec_type, mp4::CodecType::EncryptedAudio);
558 match a.protection_info.iter().find(|sinf| sinf.tenc.is_some()) {
559 Some(p) => {
560 assert_eq!(p.original_format, b"mp4a");
561 if let Some(ref schm) = p.scheme_type {
562 assert_eq!(schm.scheme_type, b"cenc");
563 } else {
564 panic!("Expected scheme type info");
565 }
566 if let Some(ref tenc) = p.tenc {
567 assert!(tenc.is_encrypted > 0);
568 assert_eq!(tenc.iv_size, 16);
569 assert_eq!(tenc.kid, kid);
570 assert_eq!(tenc.crypt_byte_block_count, None);
571 assert_eq!(tenc.skip_byte_block_count, None);
572 assert_eq!(tenc.constant_iv, None);
573 } else {
574 panic!("Invalid test condition");
575 }
576 }
577 _ => {
578 panic!("Invalid test condition");
579 }
580 }
581 }
582 }
583
584 #[test]
585 fn public_video_cenc() {
586 let system_id = vec![
587 0x10, 0x77, 0xef, 0xec, 0xc0, 0xb2, 0x4d, 0x02, 0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb,
588 0x4b,
589 ];
590
591 let kid = vec![
592 0x7e, 0x57, 0x1d, 0x03, 0x7e, 0x57, 0x1d, 0x03, 0x7e, 0x57, 0x1d, 0x03, 0x7e, 0x57, 0x1d,
593 0x11,
594 ];
595
596 let pssh_box = vec![
597 0x00, 0x00, 0x00, 0x34, 0x70, 0x73, 0x73, 0x68, 0x01, 0x00, 0x00, 0x00, 0x10, 0x77, 0xef,
598 0xec, 0xc0, 0xb2, 0x4d, 0x02, 0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb, 0x4b, 0x00, 0x00,
599 0x00, 0x01, 0x7e, 0x57, 0x1d, 0x03, 0x7e, 0x57, 0x1d, 0x03, 0x7e, 0x57, 0x1d, 0x03, 0x7e,
600 0x57, 0x1d, 0x11, 0x00, 0x00, 0x00, 0x00,
601 ];
602
603 let mut fd = File::open(VIDEO_EME_CENC_MP4).expect("Unknown file");
604 let mut buf = Vec::new();
605 fd.read_to_end(&mut buf).expect("File error");
606
607 let mut c = Cursor::new(&buf);
608 let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
609 for track in context.tracks {
610 let stsd = track.stsd.expect("expected an stsd");
611 let v = match stsd.descriptions.first().expect("expected a SampleEntry") {
612 mp4::SampleEntry::Video(ref v) => v,
613 _ => panic!("expected a VideoSampleEntry"),
614 };
615 assert_eq!(v.codec_type, mp4::CodecType::EncryptedVideo);
616 match v.protection_info.iter().find(|sinf| sinf.tenc.is_some()) {
617 Some(p) => {
618 assert_eq!(p.original_format, b"avc1");
619 if let Some(ref schm) = p.scheme_type {
620 assert_eq!(schm.scheme_type, b"cenc");
621 } else {
622 panic!("Expected scheme type info");
623 }
624 if let Some(ref tenc) = p.tenc {
625 assert!(tenc.is_encrypted > 0);
626 assert_eq!(tenc.iv_size, 16);
627 assert_eq!(tenc.kid, kid);
628 assert_eq!(tenc.crypt_byte_block_count, None);
629 assert_eq!(tenc.skip_byte_block_count, None);
630 assert_eq!(tenc.constant_iv, None);
631 } else {
632 panic!("Invalid test condition");
633 }
634 }
635 _ => {
636 panic!("Invalid test condition");
637 }
638 }
639 }
640
641 for pssh in context.psshs {
642 assert_eq!(pssh.system_id, system_id);
643 for kid_id in pssh.kid {
644 assert_eq!(kid_id, kid);
645 }
646 assert!(pssh.data.is_empty());
647 assert_eq!(pssh.box_content, pssh_box);
648 }
649 }
650
651 #[test]
652 fn public_audio_cbcs() {
653 let system_id = vec![
654 0x10, 0x77, 0xef, 0xec, 0xc0, 0xb2, 0x4d, 0x02, 0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb,
655 0x4b,
656 ];
657
658 let kid = vec![
659 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d,
660 0x21,
661 ];
662
663 let default_iv = vec![
664 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55,
665 0x66,
666 ];
667
668 let pssh_box = vec![
669 0x00, 0x00, 0x00, 0x34, 0x70, 0x73, 0x73, 0x68, 0x01, 0x00, 0x00, 0x00, 0x10, 0x77, 0xef,
670 0xec, 0xc0, 0xb2, 0x4d, 0x02, 0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb, 0x4b, 0x00, 0x00,
671 0x00, 0x01, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e,
672 0x57, 0x1d, 0x21, 0x00, 0x00, 0x00, 0x00,
673 ];
674
675 let mut fd = File::open(AUDIO_EME_CBCS_MP4).expect("Unknown file");
676 let mut buf = Vec::new();
677 fd.read_to_end(&mut buf).expect("File error");
678
679 let mut c = Cursor::new(&buf);
680 let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
681 for track in context.tracks {
682 let stsd = track.stsd.expect("expected an stsd");
683 assert_eq!(stsd.descriptions.len(), 2);
684 let mut found_encrypted_sample_description = false;
685 for description in stsd.descriptions {
686 match description {
687 mp4::SampleEntry::Audio(ref a) => {
688 if let Some(p) = a.protection_info.iter().find(|sinf| sinf.tenc.is_some()) {
689 found_encrypted_sample_description = true;
690 assert_eq!(p.original_format, b"mp4a");
691 if let Some(ref schm) = p.scheme_type {
692 assert_eq!(schm.scheme_type, b"cbcs");
693 } else {
694 panic!("Expected scheme type info");
695 }
696 if let Some(ref tenc) = p.tenc {
697 assert!(tenc.is_encrypted > 0);
698 assert_eq!(tenc.iv_size, 0);
699 assert_eq!(tenc.kid, kid);
700 // Note: 0 for both crypt and skip seems odd but
701 // that's what shaka-packager produced. It appears
702 // to indicate full encryption.
703 assert_eq!(tenc.crypt_byte_block_count, Some(0));
704 assert_eq!(tenc.skip_byte_block_count, Some(0));
705 assert_eq!(tenc.constant_iv, Some(default_iv.clone().into()));
706 } else {
707 panic!("Invalid test condition");
708 }
709 }
710 }
711 _ => {
712 panic!("expected a VideoSampleEntry");
713 }
714 }
715 }
716 assert!(
717 found_encrypted_sample_description,
718 "Should have found an encrypted sample description"
719 );
720 }
721
722 for pssh in context.psshs {
723 assert_eq!(pssh.system_id, system_id);
724 for kid_id in pssh.kid {
725 assert_eq!(kid_id, kid);
726 }
727 assert!(pssh.data.is_empty());
728 assert_eq!(pssh.box_content, pssh_box);
729 }
730 }
731
732 #[test]
733 fn public_video_cbcs() {
734 let system_id = vec![
735 0x10, 0x77, 0xef, 0xec, 0xc0, 0xb2, 0x4d, 0x02, 0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb,
736 0x4b,
737 ];
738
739 let kid = vec![
740 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d,
741 0x21,
742 ];
743
744 let default_iv = vec![
745 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55,
746 0x66,
747 ];
748
749 let pssh_box = vec![
750 0x00, 0x00, 0x00, 0x34, 0x70, 0x73, 0x73, 0x68, 0x01, 0x00, 0x00, 0x00, 0x10, 0x77, 0xef,
751 0xec, 0xc0, 0xb2, 0x4d, 0x02, 0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb, 0x4b, 0x00, 0x00,
752 0x00, 0x01, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e,
753 0x57, 0x1d, 0x21, 0x00, 0x00, 0x00, 0x00,
754 ];
755
756 let mut fd = File::open(VIDEO_EME_CBCS_MP4).expect("Unknown file");
757 let mut buf = Vec::new();
758 fd.read_to_end(&mut buf).expect("File error");
759
760 let mut c = Cursor::new(&buf);
761 let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
762 for track in context.tracks {
763 let stsd = track.stsd.expect("expected an stsd");
764 assert_eq!(stsd.descriptions.len(), 2);
765 let mut found_encrypted_sample_description = false;
766 for description in stsd.descriptions {
767 match description {
768 mp4::SampleEntry::Video(ref v) => {
769 assert_eq!(v.width, 400);
770 assert_eq!(v.height, 300);
771 if let Some(p) = v.protection_info.iter().find(|sinf| sinf.tenc.is_some()) {
772 found_encrypted_sample_description = true;
773 assert_eq!(p.original_format, b"avc1");
774 if let Some(ref schm) = p.scheme_type {
775 assert_eq!(schm.scheme_type, b"cbcs");
776 } else {
777 panic!("Expected scheme type info");
778 }
779 if let Some(ref tenc) = p.tenc {
780 assert!(tenc.is_encrypted > 0);
781 assert_eq!(tenc.iv_size, 0);
782 assert_eq!(tenc.kid, kid);
783 assert_eq!(tenc.crypt_byte_block_count, Some(1));
784 assert_eq!(tenc.skip_byte_block_count, Some(9));
785 assert_eq!(tenc.constant_iv, Some(default_iv.clone().into()));
786 } else {
787 panic!("Invalid test condition");
788 }
789 }
790 }
791 _ => {
792 panic!("expected a VideoSampleEntry");
793 }
794 }
795 }
796 assert!(
797 found_encrypted_sample_description,
798 "Should have found an encrypted sample description"
799 );
800 }
801
802 for pssh in context.psshs {
803 assert_eq!(pssh.system_id, system_id);
804 for kid_id in pssh.kid {
805 assert_eq!(kid_id, kid);
806 }
807 assert!(pssh.data.is_empty());
808 assert_eq!(pssh.box_content, pssh_box);
809 }
810 }
811
812 #[test]
813 fn public_video_av1() {
814 let mut fd = File::open(VIDEO_AV1_MP4).expect("Unknown file");
815 let mut buf = Vec::new();
816 fd.read_to_end(&mut buf).expect("File error");
817
818 let mut c = Cursor::new(&buf);
819 let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
820 for track in context.tracks {
821 // track part
822 assert_eq!(track.duration, Some(mp4::TrackScaledTime(512, 0)));
823 assert_eq!(track.empty_duration, Some(mp4::MediaScaledTime(0)));
824 assert_eq!(track.media_time, Some(mp4::TrackScaledTime(0, 0)));
825 assert_eq!(track.timescale, Some(mp4::TrackTimeScale(12288, 0)));
826
827 // track.tkhd part
828 let tkhd = track.tkhd.unwrap();
829 assert!(!tkhd.disabled);
830 assert_eq!(tkhd.duration, 42);
831 assert_eq!(tkhd.width, 4_194_304);
832 assert_eq!(tkhd.height, 4_194_304);
833
834 // track.stsd part
835 let stsd = track.stsd.expect("expected an stsd");
836 let v = match stsd.descriptions.first().expect("expected a SampleEntry") {
837 mp4::SampleEntry::Video(ref v) => v,
838 _ => panic!("expected a VideoSampleEntry"),
839 };
840 assert_eq!(v.codec_type, mp4::CodecType::AV1);
841 assert_eq!(v.width, 64);
842 assert_eq!(v.height, 64);
843
844 match v.codec_specific {
845 mp4::VideoCodecSpecific::AV1Config(ref av1c) => {
846 // TODO: test av1c fields once ffmpeg is updated
847 assert_eq!(av1c.profile, 0);
848 assert_eq!(av1c.level, 0);
849 assert_eq!(av1c.tier, 0);
850 assert_eq!(av1c.bit_depth, 8);
851 assert!(!av1c.monochrome);
852 assert_eq!(av1c.chroma_subsampling_x, 1);
853 assert_eq!(av1c.chroma_subsampling_y, 1);
854 assert_eq!(av1c.chroma_sample_position, 0);
855 assert!(!av1c.initial_presentation_delay_present);
856 assert_eq!(av1c.initial_presentation_delay_minus_one, 0);
857 }
858 _ => panic!("Invalid test condition"),
859 }
860 }
861 }
862
863 #[test]
864 fn public_mp4_bug_1185230() {
865 let input = &mut File::open("tests/test_case_1185230.mp4").expect("Unknown file");
866 let context = mp4::read_mp4(input).expect("read_mp4 failed");
867 let number_video_tracks = context
868 .tracks
869 .iter()
870 .filter(|t| t.track_type == mp4::TrackType::Video)
871 .count();
872 let number_audio_tracks = context
873 .tracks
874 .iter()
875 .filter(|t| t.track_type == mp4::TrackType::Audio)
876 .count();
877 assert_eq!(number_video_tracks, 2);
878 assert_eq!(number_audio_tracks, 2);
879 }
880
881 #[test]
882 fn public_mp4_ctts_overflow() {
883 let input = &mut File::open("tests/clusterfuzz-testcase-minimized-mp4-6093954524250112")
884 .expect("Unknown file");
885 assert_eq!(Status::from(mp4::read_mp4(input)), Status::CttsBadSize);
886 }
887
888 #[test]
889 fn public_avif_primary_item() {
890 let input = &mut File::open(IMAGE_AVIF).expect("Unknown file");
891 let context = mp4::read_avif(input, ParseStrictness::Normal).expect("read_avif failed");
892 assert_eq!(
893 context.primary_item_coded_data().unwrap(),
894 [
895 0x12, 0x00, 0x0A, 0x07, 0x38, 0x00, 0x06, 0x90, 0x20, 0x20, 0x69, 0x32, 0x0C, 0x16,
896 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x79, 0x4C, 0xD2, 0x02
897 ]
898 );
899 }
900
901 #[test]
902 fn public_avif_primary_item_split_extents() {
903 let input = &mut File::open(IMAGE_AVIF_EXTENTS).expect("Unknown file");
904 let context = mp4::read_avif(input, ParseStrictness::Normal).expect("read_avif failed");
905 assert_eq!(context.primary_item_coded_data().unwrap().len(), 52);
906 }
907
908 #[test]
909 fn public_avif_alpha_item() {
910 for_strictness_result(IMAGE_AVIF_ALPHA, |_strictness, result| {
911 assert!(result.is_ok());
912 });
913 }
914
915 #[test]
916 fn public_avif_alpha_non_premultiplied() {
917 for_strictness_result(IMAGE_AVIF_ALPHA, |_strictness, result| {
918 let context = result.expect("read_avif failed");
919 assert!(context.primary_item_coded_data().is_some());
920 assert!(context.alpha_item_coded_data().is_some());
921 assert!(!context.premultiplied_alpha);
922 });
923 }
924
925 #[test]
926 fn public_avif_alpha_premultiplied() {
927 for_strictness_result(IMAGE_AVIF_ALPHA_PREMULTIPLIED, |_strictness, result| {
928 let context = result.expect("read_avif failed");
929 assert!(context.primary_item_coded_data().is_some());
930 assert!(context.alpha_item_coded_data().is_some());
931 assert!(context.premultiplied_alpha);
932 });
933 }
934
935 #[test]
936 fn public_avif_unknown_mdat() {
937 let input = &mut File::open(IMAGE_AVIF_UNKNOWN_MDAT_SIZE).expect("Unknown file");
938 let context = mp4::read_avif(input, ParseStrictness::Normal).expect("read_avif failed");
939 assert_eq!(
940 context.primary_item_coded_data().unwrap(),
941 [
942 0x12, 0x00, 0x0A, 0x07, 0x38, 0x00, 0x06, 0x90, 0x20, 0x20, 0x69, 0x32, 0x0C, 0x16,
943 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x79, 0x4C, 0xD2, 0x02
944 ]
945 );
946 }
947
948 #[test]
949 fn public_avif_unknown_mdat_in_oversized_meta() {
950 let input =
951 &mut File::open(IMAGE_AVIF_UNKNOWN_MDAT_SIZE_IN_OVERSIZED_META).expect("Unknown file");
952 assert_eq!(
953 Status::from(mp4::read_avif(input, ParseStrictness::Normal)),
954 Status::Unsupported
955 );
956 }
957
958 #[test]
959 fn public_avif_bug_1655846() {
960 let input = &mut File::open(IMAGE_AVIF_CORRUPT).expect("Unknown file");
961 assert!(mp4::read_avif(input, ParseStrictness::Normal).is_err());
962 }
963
964 #[test]
965 fn public_avif_bug_1661347() {
966 let input = &mut File::open(IMAGE_AVIF_CORRUPT_2).expect("Unknown file");
967 assert!(mp4::read_avif(input, ParseStrictness::Normal).is_err());
968 }
969
970 fn for_strictness_result(
971 path: &str,
972 check: impl Fn(ParseStrictness, mp4::Result<mp4::AvifContext>),
973 ) {
974 let input = &mut File::open(path).expect("Unknown file");
975
976 for strictness in [
977 ParseStrictness::Permissive,
978 ParseStrictness::Normal,
979 ParseStrictness::Strict,
980 ] {
981 input.rewind().expect("rewind failed");
982 check(strictness, mp4::read_avif(input, strictness));
983 }
984 }
985
986 /// Check that input generates the expected error only in strict parsing mode
987 fn assert_avif_should(path: &str, expected: Status) {
988 for_strictness_result(path, |strictness, result| {
989 if strictness == ParseStrictness::Strict {
990 assert_eq!(expected, Status::from(result));
991 } else {
992 assert!(result.is_ok());
993 }
994 })
995 }
996
997 /// Check that input generates the expected error unless in permissive parsing mode
998 fn assert_avif_shall(path: &str, expected: Status) {
999 for_strictness_result(path, |strictness, result| {
1000 if strictness == ParseStrictness::Permissive {
1001 assert!(result.is_ok());
1002 } else {
1003 assert_eq!(expected, Status::from(result));
1004 }
1005 })
1006 }
1007
1008 // Technically all transforms shall be essential, but this appears likely to change
1009 // so we only enforce it in strict parsing
1010 // See https://github.com/mozilla/mp4parse-rust/issues/284
1011
1012 #[test]
1013 fn public_avif_av1c_missing_essential() {
1014 assert_avif_should(IMAGE_AVIF_AV1C_MISSING_ESSENTIAL, Status::TxformNoEssential);
1015 }
1016
1017 #[test]
1018 fn public_avif_clap_missing_essential() {
1019 for_strictness_result(IMAGE_AVIF_CLAP_MISSING_ESSENTIAL, |strictness, result| {
1020 if strictness == ParseStrictness::Strict {
1021 assert_eq!(Status::TxformNoEssential, Status::from(result));
1022 } else {
1023 assert_unsupported_nonfatal(&result, mp4::Feature::Clap);
1024 }
1025 })
1026 }
1027
1028 #[test]
1029 fn public_avif_imir_missing_essential() {
1030 assert_avif_should(IMAGE_AVIF_IMIR_MISSING_ESSENTIAL, Status::TxformNoEssential);
1031 }
1032
1033 #[test]
1034 fn public_avif_irot_missing_essential() {
1035 assert_avif_should(IMAGE_AVIF_IROT_MISSING_ESSENTIAL, Status::TxformNoEssential);
1036 }
1037
1038 #[test]
1039 fn public_avif_ipma_bad_version() {
1040 assert_avif_should(IMAGE_AVIF_IPMA_BAD_VERSION, Status::IpmaBadVersion);
1041 }
1042
1043 #[test]
1044 fn public_avif_ipma_bad_flags() {
1045 assert_avif_should(IMAGE_AVIF_IPMA_BAD_FLAGS, Status::IpmaFlagsNonzero);
1046 }
1047
1048 #[test]
1049 fn public_avif_ipma_duplicate_version_and_flags() {
1050 assert_avif_shall(
1051 IMAGE_AVIF_IPMA_DUPLICATE_VERSION_AND_FLAGS,
1052 Status::IpmaBadQuantity,
1053 );
1054 }
1055
1056 #[test]
1057 // TODO: convert this to a `assert_avif_shall` test to cover all `ParseStrictness` modes
1058 // that would require crafting an input that validly uses multiple ipma boxes,
1059 // which is kind of annoying to make pass the "should" requirements on flags and version
1060 // as well as the "shall" requirement on duplicate version and flags
1061 fn public_avif_ipma_duplicate_item_id() {
1062 let input = &mut File::open(IMAGE_AVIF_IPMA_DUPLICATE_ITEM_ID).expect("Unknown file");
1063 assert_eq!(
1064 Status::from(mp4::read_avif(input, ParseStrictness::Permissive)),
1065 Status::IpmaDuplicateItemId
1066 )
1067 }
1068
1069 #[test]
1070 fn public_avif_ipma_invalid_property_index() {
1071 assert_avif_shall(IMAGE_AVIF_IPMA_INVALID_PROPERTY_INDEX, Status::IpmaBadIndex);
1072 }
1073
1074 #[test]
1075 fn public_avif_hdlr_first_in_meta() {
1076 assert_avif_shall(IMAGE_AVIF_NO_HDLR, Status::HdlrNotFirst);
1077 assert_avif_shall(IMAGE_AVIF_HDLR_NOT_FIRST, Status::HdlrNotFirst);
1078 }
1079
1080 #[test]
1081 fn public_avif_hdlr_is_pict() {
1082 assert_avif_shall(IMAGE_AVIF_HDLR_NOT_PICT, Status::HdlrTypeNotPict);
1083 }
1084
1085 #[test]
1086 fn public_avif_hdlr_nonzero_reserved() {
1087 // This is a "should" despite the spec indicating a (somewhat ambiguous)
1088 // requirement that this field is set to zero.
1089 // See comments in read_hdlr
1090 assert_avif_should(
1091 IMAGE_AVIF_HDLR_NONZERO_RESERVED,
1092 Status::HdlrReservedNonzero,
1093 );
1094 }
1095
1096 #[test]
1097 fn public_avif_hdlr_multiple_nul() {
1098 // This is a "should" despite the spec indicating a (somewhat ambiguous)
1099 // requirement about extra data in boxes
1100 // See comments in read_hdlr
1101 assert_avif_should(IMAGE_AVIF_HDLR_MULTIPLE_NUL, Status::Ok);
1102 }
1103
1104 #[test]
1105 fn public_avif_no_mif1() {
1106 assert_avif_should(IMAGE_AVIF_NO_MIF1, Status::MissingMif1Brand);
1107 }
1108
1109 #[test]
1110 fn public_avif_no_pitm() {
1111 assert_avif_shall(IMAGE_AVIF_NO_PITM, Status::PitmMissing);
1112 }
1113
1114 #[test]
1115 fn public_avif_pixi_present_for_displayable_images() {
1116 let pixi_test = if cfg!(feature = "missing-pixi-permitted") {
1117 assert_avif_should
1118 } else {
1119 assert_avif_shall
1120 };
1121
1122 pixi_test(IMAGE_AVIF_NO_PIXI, Status::PixiMissing);
1123 pixi_test(IMAGE_AVIF_NO_ALPHA_PIXI, Status::PixiMissing);
1124 }
1125
1126 #[test]
1127 fn public_avif_av1c_present_for_av01() {
1128 assert_avif_shall(IMAGE_AVIF_NO_AV1C, Status::Av1cMissing);
1129 assert_avif_shall(IMAGE_AVIF_NO_ALPHA_AV1C, Status::Av1cMissing);
1130 }
1131
1132 #[test]
1133 fn public_avif_ispe_present() {
1134 assert_avif_shall(IMAGE_AVIF_NO_ISPE, Status::IspeMissing);
1135 assert_avif_shall(IMAGE_AVIF_NO_ALPHA_ISPE, Status::IspeMissing);
1136 }
1137
1138 #[test]
1139 fn public_avif_transform_before_ispe() {
1140 assert_avif_shall(IMAGE_AVIF_TRANSFORM_BEFORE_ISPE, Status::TxformBeforeIspe);
1141 }
1142
1143 #[test]
1144 fn public_avif_transform_order() {
1145 assert_avif_shall(IMAGE_AVIF_TRANSFORM_ORDER, Status::TxformOrder);
1146 }
1147
1148 #[allow(clippy::uninlined_format_args)]
1149 fn assert_unsupported_nonfatal(result: &mp4::Result<mp4::AvifContext>, feature: mp4::Feature) {
1150 match result {
1151 Ok(context) => {
1152 assert!(
1153 context.unsupported_features.contains(feature),
1154 "context.unsupported_features missing expected {:?}",
1155 feature
1156 );
1157 }
1158 r => panic!(
1159 "Expected Ok with unsupported_features containing {:?}, found {:?}",
1160 feature, r
1161 ),
1162 }
1163 }
1164
1165 // Assert that across all strictness levels the given feature is tracked as
1166 // being used, but unsupported. Additionally, if the feature is essential,
1167 // assert that the primary item is not processed unless using permissive mode.
1168 // TODO: Add similar tests for alpha
1169 fn assert_unsupported(path: &str, feature: mp4::Feature, essential: bool) {
1170 for_strictness_result(path, |strictness, result| {
1171 assert_unsupported_nonfatal(&result, feature);
1172 match result {
1173 Ok(context) if essential => assert_eq!(
1174 context.primary_item_coded_data().is_some(),
1175 strictness == ParseStrictness::Permissive
1176 ),
1177 Ok(context) if !essential => assert!(context.primary_item_coded_data().is_some()),
1178 _ => panic!("Expected Ok, got {:?}", result),
1179 }
1180 });
1181 }
1182
1183 fn assert_unsupported_nonessential(path: &str, feature: mp4::Feature) {
1184 assert_unsupported(path, feature, false);
1185 }
1186
1187 fn assert_unsupported_essential(path: &str, feature: mp4::Feature) {
1188 assert_unsupported(path, feature, true);
1189 }
1190
1191 #[test]
1192 fn public_avif_a1lx() {
1193 assert_unsupported_nonessential(AVIF_A1LX, mp4::Feature::A1lx);
1194 }
1195
1196 #[test]
1197 fn public_avif_a1lx_marked_essential() {
1198 assert_avif_shall(IMAGE_AVIF_A1LX_MARKED_ESSENTIAL, Status::A1lxEssential);
1199 }
1200
1201 #[test]
1202 fn public_avif_a1op() {
1203 assert_unsupported_essential(AVIF_A1OP, mp4::Feature::A1op);
1204 }
1205
1206 #[test]
1207 fn public_avif_a1op_missing_essential() {
1208 assert_avif_shall(IMAGE_AVIF_A1OP_MISSING_ESSENTIAL, Status::A1opNoEssential);
1209 }
1210
1211 #[test]
1212 fn public_avif_lsel() {
1213 assert_unsupported_essential(AVIF_LSEL, mp4::Feature::Lsel);
1214 }
1215
1216 #[test]
1217 fn public_avif_lsel_missing_essential() {
1218 assert_avif_shall(IMAGE_AVIF_LSEL_MISSING_ESSENTIAL, Status::LselNoEssential);
1219 }
1220
1221 #[test]
1222 fn public_avif_clap() {
1223 assert_unsupported_essential(AVIF_CLAP, mp4::Feature::Clap);
1224 }
1225
1226 #[test]
1227 fn public_avif_grid() {
1228 for file in &[AVIF_GRID, AVIF_GRID_A1LX] {
1229 let input = &mut File::open(file).expect(file);
1230 assert_unsupported_nonfatal(
1231 &mp4::read_avif(input, ParseStrictness::Normal),
1232 mp4::Feature::Grid,
1233 );
1234 }
1235 }
1236
1237 #[test]
1238 fn public_avis_major_no_pitm() {
1239 let input = &mut File::open(AVIF_AVIS_MAJOR_NO_PITM).expect("Unknown file");
1240 match mp4::read_avif(input, ParseStrictness::Normal) {
1241 Ok(context) => {
1242 assert_eq!(context.major_brand, mp4::AVIS_BRAND);
1243 assert!(context.primary_item_coded_data().is_none());
1244 assert!(context.sequence.is_some());
1245 }
1246 Err(e) => panic!("Expected Ok(_), found {:?}", e),
1247 }
1248 }
1249
1250 #[test]
1251 fn public_avis_major_with_pitm_and_alpha() {
1252 let input = &mut File::open(AVIF_AVIS_MAJOR_WITH_PITM_AND_ALPHA).expect("Unknown file");
1253 match mp4::read_avif(input, ParseStrictness::Normal) {
1254 Ok(context) => {
1255 assert_eq!(context.major_brand, mp4::AVIS_BRAND);
1256 assert!(context.primary_item_coded_data().is_some());
1257 assert!(context.alpha_item_coded_data().is_some());
1258 match context.sequence {
1259 Some(sequence) => {
1260 assert!(!sequence.tracks.is_empty());
1261 assert_eq!(sequence.tracks[0].looped, None);
1262 }
1263 None => panic!("Expected sequence"),
1264 }
1265 }
1266 Err(e) => panic!("Expected Ok(_), found {:?}", e),
1267 }
1268 }
1269
1270 #[test]
1271 fn public_avif_avis_major_no_moov() {
1272 assert_avif_shall(AVIF_AVIS_MAJOR_NO_MOOV, Status::MoovMissing);
1273 }
1274
1275 #[test]
1276 fn public_avif_avis_with_no_pitm_no_iloc() {
1277 let input = &mut File::open(AVIF_AVIS_WITH_NO_PITM_NO_ILOC).expect("Unknown file");
1278 match mp4::read_avif(input, ParseStrictness::Normal) {
1279 Ok(context) => {
1280 assert_eq!(context.major_brand, mp4::AVIS_BRAND);
1281 match context.sequence {
1282 Some(sequence) => {
1283 assert!(!sequence.tracks.is_empty());
1284 assert_eq!(sequence.tracks[0].looped, Some(false));
1285 }
1286 None => panic!("Expected sequence"),
1287 }
1288 }
1289 Err(e) => panic!("Expected Ok(_), found {:?}", e),
1290 }
1291 }
1292
1293 #[test]
1294 fn public_avif_avis_with_pitm_no_iloc() {
1295 assert_avif_should(AVIF_AVIS_WITH_PITM_NO_ILOC, Status::PitmNotFound);
1296 }
1297
1298 #[test]
1299 fn public_avif_valid_with_garbage_overread_at_end() {
1300 assert_avif_should(
1301 IMAGE_AVIF_VALID_WITH_GARBAGE_OVERREAD_AT_END,
1302 Status::CheckParserStateErr,
1303 );
1304 }
1305
1306 #[test]
1307 fn public_avif_valid_with_garbage_byte_at_end() {
1308 assert_avif_should(IMAGE_AVIF_VALID_WITH_GARBAGE_BYTE_AT_END, Status::Ok);
1309 }
1310
1311 #[test]
1312 fn public_avif_bad_video_sample_entry() {
1313 let input = &mut File::open(IMAGE_AVIF_WIDE_BOX_SIZE_0).expect("Unknown file");
1314 assert_eq!(
1315 Status::from(mp4::read_avif(input, ParseStrictness::Normal)),
1316 Status::BoxBadWideSize
1317 );
1318 }
1319
1320 fn public_avis_loop_impl(path: &str, looped: bool) {
1321 let input = &mut File::open(path).expect("Unknown file");
1322 match mp4::read_avif(input, ParseStrictness::Normal) {
1323 Ok(context) => match context.sequence {
1324 Some(sequence) => {
1325 assert!(!sequence.tracks.is_empty());
1326 assert_eq!(sequence.tracks[0].looped, Some(looped));
1327 if looped {
1328 assert!(sequence.tracks[0].edited_duration.is_some());
1329 }
1330 }
1331 None => panic!(
1332 "Expected sequence in {}",
1333 AVIF_AVIS_MAJOR_WITH_PITM_AND_ALPHA
1334 ),
1335 },
1336 Err(e) => panic!("Expected Ok(_), found {:?}", e),
1337 }
1338 }
1339
1340 #[test]
1341 fn public_avif_avis_no_loop() {
1342 public_avis_loop_impl(AVIF_AVIS_NO_LOOP, false);
1343 }
1344
1345 #[test]
1346 fn public_avif_avis_loop_forever() {
1347 public_avis_loop_impl(AVIF_AVIS_LOOP_FOREVER, true);
1348 }
1349
1350 #[test]
1351 fn public_avif_read_samples() {
1352 public_avif_read_samples_impl(ParseStrictness::Normal);
1353 }
1354
1355 #[test]
1356 #[ignore] // See https://github.com/AOMediaCodec/av1-avif/issues/146
1357 fn public_avif_read_samples_strict() {
1358 public_avif_read_samples_impl(ParseStrictness::Strict);
1359 }
1360
1361 fn to_canonical_paths(strs: &[&str]) -> Vec<std::path::PathBuf> {
1362 strs.iter()
1363 .map(std::fs::canonicalize)
1364 .map(Result::unwrap)
1365 .collect()
1366 }
1367
1368 fn public_avif_read_samples_impl(strictness: ParseStrictness) {
1369 let corrupt_images = to_canonical_paths(AV1_AVIF_CORRUPT_IMAGES);
1370 let unsupported_images = to_canonical_paths(AVIF_UNSUPPORTED_IMAGES);
1371 let legal_no_pixi_images = if cfg!(feature = "missing-pixi-permitted") {
1372 to_canonical_paths(AVIF_NO_PIXI_IMAGES)
1373 } else {
1374 vec![]
1375 };
1376 for dir in AVIF_TEST_DIRS {
1377 for entry in walkdir::WalkDir::new(dir) {
1378 let entry = entry.expect("AVIF entry");
1379 let path = entry.path();
1380 let extension = path.extension().unwrap_or_default();
1381 if !path.is_file() || (extension != "avif" && extension != "avifs") {
1382 eprintln!("Skipping {path:?}");
1383 continue; // Skip directories, ReadMe.txt, etc.
1384 }
1385 let corrupt = (path.canonicalize().unwrap().parent().unwrap()
1386 == std::fs::canonicalize(AVIF_CORRUPT_IMAGES_DIR).unwrap()
1387 || corrupt_images.contains(&path.canonicalize().unwrap()))
1388 && !legal_no_pixi_images.contains(&path.canonicalize().unwrap());
1389
1390 let unsupported = unsupported_images.contains(&path.canonicalize().unwrap());
1391 println!(
1392 "parsing {}{}{:?}",
1393 if corrupt { "(corrupt) " } else { "" },
1394 if unsupported { "(unsupported) " } else { "" },
1395 path,
1396 );
1397 let input = &mut File::open(path).expect("Unknow file");
1398 match mp4::read_avif(input, strictness) {
1399 Ok(c) if unsupported || corrupt => {
1400 if unsupported {
1401 assert!(!c.unsupported_features.is_empty());
1402 } else {
1403 panic!("Expected error parsing {:?}, found:\n{:?}", path, c)
1404 }
1405 }
1406 Ok(c) => {
1407 assert!(
1408 c.unsupported_features.is_empty(),
1409 "{:?}",
1410 c.unsupported_features
1411 );
1412 eprintln!("Successfully parsed {path:?}")
1413 }
1414 Err(e) if corrupt => {
1415 eprintln!("Expected error parsing corrupt input {path:?}: {e:?}")
1416 }
1417 Err(e) => panic!("Unexpected error parsing {:?}: {:?}", path, e),
1418 }
1419 }
1420 }
1421 }
1422
1423 #[test]
1424 fn public_video_h263() {
1425 let mut fd = File::open(VIDEO_H263_3GP).expect("Unknown file");
1426 let mut buf = Vec::new();
1427 fd.read_to_end(&mut buf).expect("File error");
1428
1429 let mut c = Cursor::new(&buf);
1430 let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
1431 for track in context.tracks {
1432 let stsd = track.stsd.expect("expected an stsd");
1433 let v = match stsd.descriptions.first().expect("expected a SampleEntry") {
1434 mp4::SampleEntry::Video(ref v) => v,
1435 _ => panic!("expected a VideoSampleEntry"),
1436 };
1437 assert_eq!(v.codec_type, mp4::CodecType::H263);
1438 assert_eq!(v.width, 176);
1439 assert_eq!(v.height, 144);
1440 let _codec_specific = match v.codec_specific {
1441 mp4::VideoCodecSpecific::H263Config(_) => true,
1442 _ => {
1443 panic!("expected a H263Config",);
1444 }
1445 };
1446 }
1447 }
1448
1449 #[test]
1450 fn public_video_hevc() {
1451 let mut fd = File::open(VIDEO_HEVC_MP4).expect("Unknown file");
1452 let mut buf = Vec::new();
1453 fd.read_to_end(&mut buf).expect("File error");
1454
1455 let mut c = Cursor::new(&buf);
1456 let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
1457 for track in context.tracks {
1458 let stsd = track.stsd.expect("expected an stsd");
1459 let v = match stsd.descriptions.first().expect("expected a SampleEntry") {
1460 mp4::SampleEntry::Video(ref v) => v,
1461 _ => panic!("expected a VideoSampleEntry"),
1462 };
1463 assert_eq!(v.codec_type, mp4::CodecType::HEVC);
1464 assert_eq!(v.width, 640);
1465 assert_eq!(v.height, 480);
1466 let _codec_specific = match &v.codec_specific {
1467 mp4::VideoCodecSpecific::HEVCConfig(_) => true,
1468 _ => {
1469 panic!("expected a HEVCConfig",);
1470 }
1471 };
1472 }
1473 }
1474
1475 #[test]
1476 fn public_parse_pasp_h264() {
1477 let mut fd = File::open(VIDEO_H264_PASP_MP4).expect("Unknown file");
1478 let mut buf = Vec::new();
1479 fd.read_to_end(&mut buf).expect("File error");
1480
1481 let mut c = Cursor::new(&buf);
1482 let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
1483 for track in context.tracks {
1484 let stsd = track.stsd.expect("expected an stsd");
1485 let v = match stsd.descriptions.first().expect("expected a SampleEntry") {
1486 mp4::SampleEntry::Video(ref v) => v,
1487 _ => panic!("expected a VideoSampleEntry"),
1488 };
1489 assert_eq!(v.codec_type, mp4::CodecType::H264);
1490 assert_eq!(v.width, 640);
1491 assert_eq!(v.height, 480);
1492 assert!(
1493 v.pixel_aspect_ratio.is_some(),
1494 "pixel_aspect_ratio should exist"
1495 );
1496 assert_eq!(
1497 { v.pixel_aspect_ratio.unwrap() },
1498 16.0 / 9.0,
1499 "pixel_aspect_ratio should be 16/9"
1500 );
1501 }
1502 }
1503
1504 #[test]
1505 #[cfg(feature = "3gpp")]
1506 fn public_audio_amrnb() {
1507 let mut fd = File::open(AUDIO_AMRNB_3GP).expect("Unknown file");
1508 let mut buf = Vec::new();
1509 fd.read_to_end(&mut buf).expect("File error");
1510
1511 let mut c = Cursor::new(&buf);
1512 let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
1513 for track in context.tracks {
1514 let stsd = track.stsd.expect("expected an stsd");
1515 let a = match stsd.descriptions.first().expect("expected a SampleEntry") {
1516 mp4::SampleEntry::Audio(ref v) => v,
1517 _ => panic!("expected a AudioSampleEntry"),
1518 };
1519 assert!(a.codec_type == mp4::CodecType::AMRNB);
1520 let _codec_specific = match a.codec_specific {
1521 mp4::AudioCodecSpecific::AMRSpecificBox(_) => true,
1522 _ => {
1523 panic!("expected a AMRSpecificBox",);
1524 }
1525 };
1526 }
1527 }
1528
1529 #[test]
1530 #[cfg(feature = "3gpp")]
1531 fn public_audio_amrwb() {
1532 let mut fd = File::open(AUDIO_AMRWB_3GP).expect("Unknown file");
1533 let mut buf = Vec::new();
1534 fd.read_to_end(&mut buf).expect("File error");
1535
1536 let mut c = Cursor::new(&buf);
1537 let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
1538 for track in context.tracks {
1539 let stsd = track.stsd.expect("expected an stsd");
1540 let a = match stsd.descriptions.first().expect("expected a SampleEntry") {
1541 mp4::SampleEntry::Audio(ref v) => v,
1542 _ => panic!("expected a AudioSampleEntry"),
1543 };
1544 assert!(a.codec_type == mp4::CodecType::AMRWB);
1545 let _codec_specific = match a.codec_specific {
1546 mp4::AudioCodecSpecific::AMRSpecificBox(_) => true,
1547 _ => {
1548 panic!("expected a AMRSpecificBox",);
1549 }
1550 };
1551 }
1552 }
1553
1554 #[test]
1555 #[cfg(feature = "mp4v")]
1556 fn public_video_mp4v() {
1557 let mut fd = File::open(VIDEO_MP4V_MP4).expect("Unknown file");
1558 let mut buf = Vec::new();
1559 fd.read_to_end(&mut buf).expect("File error");
1560
1561 let mut c = Cursor::new(&buf);
1562 let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
1563 for track in context.tracks {
1564 let stsd = track.stsd.expect("expected an stsd");
1565 let v = match stsd.descriptions.first().expect("expected a SampleEntry") {
1566 mp4::SampleEntry::Video(ref v) => v,
1567 _ => panic!("expected a VideoSampleEntry"),
1568 };
1569 assert_eq!(v.codec_type, mp4::CodecType::MP4V);
1570 assert_eq!(v.width, 176);
1571 assert_eq!(v.height, 144);
1572 let _codec_specific = match v.codec_specific {
1573 mp4::VideoCodecSpecific::ESDSConfig(_) => true,
1574 _ => {
1575 panic!("expected a ESDSConfig",);
1576 }
1577 };
1578 }
1579 }