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