Skip to content

Commit 06cfc47

Browse files
committed
Pass ParseStrictness parameter in various places for later use.
1 parent 8f233dd commit 06cfc47

File tree

4 files changed

+130
-71
lines changed

4 files changed

+130
-71
lines changed

mp4parse/src/lib.rs

+86-32
Original file line numberDiff line numberDiff line change
@@ -2417,7 +2417,7 @@ pub fn read_avif<T: Read>(f: &mut T, strictness: ParseStrictness) -> Result<Avif
24172417
if image_sequence.is_some() {
24182418
return Status::MoovBadQuantity.into();
24192419
}
2420-
image_sequence = Some(read_moov(&mut b, None)?);
2420+
image_sequence = Some(read_moov(&mut b, None, strictness)?);
24212421
}
24222422
BoxType::MediaDataBox => {
24232423
let file_offset = b.offset();
@@ -4058,7 +4058,7 @@ fn read_iloc<T: Read>(src: &mut BMFFBox<T>) -> Result<TryHashMap<ItemId, ItemLoc
40584058
}
40594059

40604060
/// Read the contents of a box, including sub boxes.
4061-
pub fn read_mp4<T: Read>(f: &mut T) -> Result<MediaContext> {
4061+
pub fn read_mp4<T: Read>(f: &mut T, strictness: ParseStrictness) -> Result<MediaContext> {
40624062
let mut context = None;
40634063
let mut found_ftyp = false;
40644064
// TODO(kinetik): Top-level parsing should handle zero-sized boxes
@@ -4087,7 +4087,7 @@ pub fn read_mp4<T: Read>(f: &mut T) -> Result<MediaContext> {
40874087
debug!("{:?}", ftyp);
40884088
}
40894089
BoxType::MovieBox => {
4090-
context = Some(read_moov(&mut b, context)?);
4090+
context = Some(read_moov(&mut b, context, strictness)?);
40914091
}
40924092
#[cfg(feature = "meta-xml")]
40934093
BoxType::MetadataBox => {
@@ -4133,7 +4133,11 @@ fn parse_mvhd<T: Read>(f: &mut BMFFBox<T>) -> Result<Option<MediaTimeScale>> {
41334133
/// Note that despite the spec indicating "exactly one" moov box should exist at
41344134
/// the file container level, we support reading and merging multiple moov boxes
41354135
/// such as with tests/test_case_1185230.mp4.
4136-
fn read_moov<T: Read>(f: &mut BMFFBox<T>, context: Option<MediaContext>) -> Result<MediaContext> {
4136+
fn read_moov<T: Read>(
4137+
f: &mut BMFFBox<T>,
4138+
context: Option<MediaContext>,
4139+
strictness: ParseStrictness,
4140+
) -> Result<MediaContext> {
41374141
let MediaContext {
41384142
mut timescale,
41394143
mut tracks,
@@ -4152,7 +4156,7 @@ fn read_moov<T: Read>(f: &mut BMFFBox<T>, context: Option<MediaContext>) -> Resu
41524156
}
41534157
BoxType::TrackBox => {
41544158
let mut track = Track::new(tracks.len());
4155-
read_trak(&mut b, &mut track)?;
4159+
read_trak(&mut b, &mut track, strictness)?;
41564160
tracks.push(track)?;
41574161
}
41584162
BoxType::MovieExtendsBox => {
@@ -4262,7 +4266,11 @@ fn read_mehd<T: Read>(src: &mut BMFFBox<T>) -> Result<MediaScaledTime> {
42624266

42634267
/// Parse a Track Box
42644268
/// See ISOBMFF (ISO 14496-12:2020) § 8.3.1.
4265-
fn read_trak<T: Read>(f: &mut BMFFBox<T>, track: &mut Track) -> Result<()> {
4269+
fn read_trak<T: Read>(
4270+
f: &mut BMFFBox<T>,
4271+
track: &mut Track,
4272+
strictness: ParseStrictness,
4273+
) -> Result<()> {
42664274
let mut iter = f.box_iter();
42674275
while let Some(mut b) = iter.next_box()? {
42684276
match b.head.name {
@@ -4273,7 +4281,7 @@ fn read_trak<T: Read>(f: &mut BMFFBox<T>, track: &mut Track) -> Result<()> {
42734281
debug!("{:?}", tkhd);
42744282
}
42754283
BoxType::EditBox => read_edts(&mut b, track)?,
4276-
BoxType::MediaBox => read_mdia(&mut b, track)?,
4284+
BoxType::MediaBox => read_mdia(&mut b, track, strictness)?,
42774285
BoxType::TrackReferenceBox => track.tref = Some(read_tref(&mut b)?),
42784286
_ => skip_box_content(&mut b)?,
42794287
};
@@ -4346,7 +4354,11 @@ fn parse_mdhd<T: Read>(
43464354
Ok((mdhd, duration, timescale))
43474355
}
43484356

4349-
fn read_mdia<T: Read>(f: &mut BMFFBox<T>, track: &mut Track) -> Result<()> {
4357+
fn read_mdia<T: Read>(
4358+
f: &mut BMFFBox<T>,
4359+
track: &mut Track,
4360+
strictness: ParseStrictness,
4361+
) -> Result<()> {
43504362
let mut iter = f.box_iter();
43514363
while let Some(mut b) = iter.next_box()? {
43524364
match b.head.name {
@@ -4369,7 +4381,7 @@ fn read_mdia<T: Read>(f: &mut BMFFBox<T>, track: &mut Track) -> Result<()> {
43694381
}
43704382
debug!("{:?}", hdlr);
43714383
}
4372-
BoxType::MediaInformationBox => read_minf(&mut b, track)?,
4384+
BoxType::MediaInformationBox => read_minf(&mut b, track, strictness)?,
43734385
_ => skip_box_content(&mut b)?,
43744386
};
43754387
check_parser_state!(b.content);
@@ -4403,24 +4415,32 @@ fn read_tref_auxl<T: Read>(f: &mut BMFFBox<T>) -> Result<TrackReference> {
44034415
Ok(TrackReference { track_ids })
44044416
}
44054417

4406-
fn read_minf<T: Read>(f: &mut BMFFBox<T>, track: &mut Track) -> Result<()> {
4418+
fn read_minf<T: Read>(
4419+
f: &mut BMFFBox<T>,
4420+
track: &mut Track,
4421+
strictness: ParseStrictness,
4422+
) -> Result<()> {
44074423
let mut iter = f.box_iter();
44084424
while let Some(mut b) = iter.next_box()? {
44094425
match b.head.name {
4410-
BoxType::SampleTableBox => read_stbl(&mut b, track)?,
4426+
BoxType::SampleTableBox => read_stbl(&mut b, track, strictness)?,
44114427
_ => skip_box_content(&mut b)?,
44124428
};
44134429
check_parser_state!(b.content);
44144430
}
44154431
Ok(())
44164432
}
44174433

4418-
fn read_stbl<T: Read>(f: &mut BMFFBox<T>, track: &mut Track) -> Result<()> {
4434+
fn read_stbl<T: Read>(
4435+
f: &mut BMFFBox<T>,
4436+
track: &mut Track,
4437+
strictness: ParseStrictness,
4438+
) -> Result<()> {
44194439
let mut iter = f.box_iter();
44204440
while let Some(mut b) = iter.next_box()? {
44214441
match b.head.name {
44224442
BoxType::SampleDescriptionBox => {
4423-
let stsd = read_stsd(&mut b, track)?;
4443+
let stsd = read_stsd(&mut b, track, strictness)?;
44244444
debug!("{:?}", stsd);
44254445
track.stsd = Some(stsd);
44264446
}
@@ -4942,7 +4962,11 @@ fn read_flac_metadata<T: Read>(src: &mut BMFFBox<T>) -> Result<FLACMetadataBlock
49424962
}
49434963

49444964
/// See MPEG-4 Systems (ISO 14496-1:2010) § 7.2.6.5
4945-
fn find_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
4965+
fn find_descriptor(
4966+
data: &[u8],
4967+
esds: &mut ES_Descriptor,
4968+
strictness: ParseStrictness,
4969+
) -> Result<()> {
49464970
// Tags for elementary stream description
49474971
const ESDESCR_TAG: u8 = 0x03;
49484972
const DECODER_CONFIG_TAG: u8 = 0x04;
@@ -4982,13 +5006,13 @@ fn find_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
49825006

49835007
match tag {
49845008
ESDESCR_TAG => {
4985-
read_es_descriptor(descriptor, esds)?;
5009+
read_es_descriptor(descriptor, esds, strictness)?;
49865010
}
49875011
DECODER_CONFIG_TAG => {
4988-
read_dc_descriptor(descriptor, esds)?;
5012+
read_dc_descriptor(descriptor, esds, strictness)?;
49895013
}
49905014
DECODER_SPECIFIC_TAG => {
4991-
read_ds_descriptor(descriptor, esds)?;
5015+
read_ds_descriptor(descriptor, esds, strictness)?;
49925016
}
49935017
_ => {
49945018
debug!("Unsupported descriptor, tag {}", tag);
@@ -5014,7 +5038,11 @@ fn get_audio_object_type(bit_reader: &mut BitReader) -> Result<u16> {
50145038
}
50155039

50165040
/// See MPEG-4 Systems (ISO 14496-1:2010) § 7.2.6.7 and probably 14496-3 somewhere?
5017-
fn read_ds_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
5041+
fn read_ds_descriptor(
5042+
data: &[u8],
5043+
esds: &mut ES_Descriptor,
5044+
_strictness: ParseStrictness,
5045+
) -> Result<()> {
50185046
#[cfg(feature = "mp4v")]
50195047
// Check if we are in a Visual esda Box.
50205048
if esds.video_codec != CodecType::Unknown {
@@ -5191,7 +5219,11 @@ fn read_surround_channel_count(bit_reader: &mut BitReader, channels: u8) -> Resu
51915219
}
51925220

51935221
/// See MPEG-4 Systems (ISO 14496-1:2010) § 7.2.6.6
5194-
fn read_dc_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
5222+
fn read_dc_descriptor(
5223+
data: &[u8],
5224+
esds: &mut ES_Descriptor,
5225+
strictness: ParseStrictness,
5226+
) -> Result<()> {
51955227
let des = &mut Cursor::new(data);
51965228
let object_profile = des.read_u8()?;
51975229

@@ -5207,7 +5239,11 @@ fn read_dc_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
52075239
skip(des, 12)?;
52085240

52095241
if data.len().to_u64() > des.position() {
5210-
find_descriptor(&data[des.position().try_into()?..data.len()], esds)?;
5242+
find_descriptor(
5243+
&data[des.position().try_into()?..data.len()],
5244+
esds,
5245+
strictness,
5246+
)?;
52115247
}
52125248

52135249
esds.audio_codec = match object_profile {
@@ -5225,7 +5261,11 @@ fn read_dc_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
52255261
}
52265262

52275263
/// See MPEG-4 Systems (ISO 14496-1:2010) § 7.2.6.5
5228-
fn read_es_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
5264+
fn read_es_descriptor(
5265+
data: &[u8],
5266+
esds: &mut ES_Descriptor,
5267+
strictness: ParseStrictness,
5268+
) -> Result<()> {
52295269
let des = &mut Cursor::new(data);
52305270

52315271
skip(des, 2)?;
@@ -5246,20 +5286,24 @@ fn read_es_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
52465286
}
52475287

52485288
if data.len().to_u64() > des.position() {
5249-
find_descriptor(&data[des.position().try_into()?..data.len()], esds)?;
5289+
find_descriptor(
5290+
&data[des.position().try_into()?..data.len()],
5291+
esds,
5292+
strictness,
5293+
)?;
52505294
}
52515295

52525296
Ok(())
52535297
}
52545298

52555299
/// See MP4 (ISO 14496-14:2020) § 6.7.2
5256-
fn read_esds<T: Read>(src: &mut BMFFBox<T>) -> Result<ES_Descriptor> {
5300+
fn read_esds<T: Read>(src: &mut BMFFBox<T>, strictness: ParseStrictness) -> Result<ES_Descriptor> {
52575301
let (_, _) = read_fullbox_extra(src)?;
52585302

52595303
let esds_array = read_buf(src, src.bytes_left())?;
52605304

52615305
let mut es_data = ES_Descriptor::default();
5262-
find_descriptor(&esds_array, &mut es_data)?;
5306+
find_descriptor(&esds_array, &mut es_data, strictness)?;
52635307

52645308
es_data.codec_esds = esds_array;
52655309

@@ -5558,7 +5602,7 @@ fn read_video_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleEntry>
55585602
{
55595603
// Read ES_Descriptor inside an esds box.
55605604
// See ISOBMFF (ISO 14496-1:2010) § 7.2.6.5
5561-
let esds = read_esds(&mut b)?;
5605+
let esds = read_esds(&mut b, ParseStrictness::Normal)?;
55625606
codec_specific =
55635607
Some(VideoCodecSpecific::ESDSConfig(esds.decoder_specific_data));
55645608
}
@@ -5621,13 +5665,16 @@ fn read_video_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleEntry>
56215665
)
56225666
}
56235667

5624-
fn read_qt_wave_atom<T: Read>(src: &mut BMFFBox<T>) -> Result<ES_Descriptor> {
5668+
fn read_qt_wave_atom<T: Read>(
5669+
src: &mut BMFFBox<T>,
5670+
strictness: ParseStrictness,
5671+
) -> Result<ES_Descriptor> {
56255672
let mut codec_specific = None;
56265673
let mut iter = src.box_iter();
56275674
while let Some(mut b) = iter.next_box()? {
56285675
match b.head.name {
56295676
BoxType::ESDBox => {
5630-
let esds = read_esds(&mut b)?;
5677+
let esds = read_esds(&mut b, strictness)?;
56315678
codec_specific = Some(esds);
56325679
}
56335680
_ => skip_box_content(&mut b)?,
@@ -5639,7 +5686,10 @@ fn read_qt_wave_atom<T: Read>(src: &mut BMFFBox<T>) -> Result<ES_Descriptor> {
56395686

56405687
/// Parse an audio description inside an stsd box.
56415688
/// See ISOBMFF (ISO 14496-12:2020) § 12.2.3
5642-
fn read_audio_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleEntry> {
5689+
fn read_audio_sample_entry<T: Read>(
5690+
src: &mut BMFFBox<T>,
5691+
strictness: ParseStrictness,
5692+
) -> Result<SampleEntry> {
56435693
let name = src.get_header().name;
56445694

56455695
// Skip uninteresting fields.
@@ -5713,7 +5763,7 @@ fn read_audio_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleEntry>
57135763
{
57145764
return Status::StsdBadAudioSampleEntry.into();
57155765
}
5716-
let esds = read_esds(&mut b)?;
5766+
let esds = read_esds(&mut b, strictness)?;
57175767
codec_type = esds.audio_codec;
57185768
codec_specific = Some(AudioCodecSpecific::ES_Descriptor(esds));
57195769
}
@@ -5746,7 +5796,7 @@ fn read_audio_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleEntry>
57465796
codec_specific = Some(AudioCodecSpecific::ALACSpecificBox(alac));
57475797
}
57485798
BoxType::QTWaveAtom => {
5749-
let qt_esds = read_qt_wave_atom(&mut b)?;
5799+
let qt_esds = read_qt_wave_atom(&mut b, strictness)?;
57505800
codec_type = qt_esds.audio_codec;
57515801
codec_specific = Some(AudioCodecSpecific::ES_Descriptor(qt_esds));
57525802
}
@@ -5799,7 +5849,11 @@ fn read_audio_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleEntry>
57995849
/// Parse a stsd box.
58005850
/// See ISOBMFF (ISO 14496-12:2020) § 8.5.2
58015851
/// See MP4 (ISO 14496-14:2020) § 6.7.2
5802-
fn read_stsd<T: Read>(src: &mut BMFFBox<T>, track: &Track) -> Result<SampleDescriptionBox> {
5852+
fn read_stsd<T: Read>(
5853+
src: &mut BMFFBox<T>,
5854+
track: &Track,
5855+
strictness: ParseStrictness,
5856+
) -> Result<SampleDescriptionBox> {
58035857
let (_, flags) = read_fullbox_extra(src)?;
58045858

58055859
if flags != 0 {
@@ -5819,7 +5873,7 @@ fn read_stsd<T: Read>(src: &mut BMFFBox<T>, track: &Track) -> Result<SampleDescr
58195873
TrackType::Video => read_video_sample_entry(&mut b),
58205874
TrackType::Picture => read_video_sample_entry(&mut b),
58215875
TrackType::AuxiliaryVideo => read_video_sample_entry(&mut b),
5822-
TrackType::Audio => read_audio_sample_entry(&mut b),
5876+
TrackType::Audio => read_audio_sample_entry(&mut b, strictness),
58235877
TrackType::Metadata => Err(Error::Unsupported("metadata track")),
58245878
TrackType::Unknown => Err(Error::Unsupported("unknown track type")),
58255879
};

0 commit comments

Comments
 (0)