Skip to content

Commit 778d47d

Browse files
authored
Merge pull request #226 from mozilla/bug-1641972-fixes
Miscellaneous fixes related to bug investigation
2 parents 309b9b8 + e77b7bb commit 778d47d

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

mp4parse/src/lib.rs

+32-14
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ pub type Result<T> = std::result::Result<T, Error>;
214214
/// begins with a header describing the length of the box's data and a
215215
/// four-byte box type which identifies the type of the box. Together these
216216
/// are enough to interpret the contents of that section of the file.
217+
///
218+
/// See ISO 14496-12:2015 § 4.2
217219
#[derive(Debug, Clone, Copy)]
218220
struct BoxHeader {
219221
/// Box type.
@@ -226,6 +228,11 @@ struct BoxHeader {
226228
uuid: Option<[u8; 16]>,
227229
}
228230

231+
impl BoxHeader {
232+
const MIN_SIZE: u64 = 8; // 4-byte size + 4-byte type
233+
const MIN_LARGE_SIZE: u64 = 16; // 4-byte size + 4-byte type + 16-byte size
234+
}
235+
229236
/// File type box 'ftyp'.
230237
#[derive(Debug)]
231238
struct FileTypeBox {
@@ -367,6 +374,8 @@ pub enum SampleEntry {
367374
Unknown,
368375
}
369376

377+
/// An Elementary Stream Descriptor
378+
/// See ISO 14496-1:2010 § 7.2.6.5
370379
#[allow(non_camel_case_types)]
371380
#[derive(Debug, Default)]
372381
pub struct ES_Descriptor {
@@ -1007,6 +1016,7 @@ impl Track {
10071016
}
10081017
}
10091018

1019+
/// See ISO 14496-12:2015 § 4.2
10101020
struct BMFFBox<'a, T: 'a> {
10111021
head: BoxHeader,
10121022
content: Take<&'a mut T>,
@@ -1115,6 +1125,8 @@ impl<'a, T> Drop for BMFFBox<'a, T> {
11151125
/// and its length. Used internally for dispatching to specific
11161126
/// parsers for the internal content, or to get the length to
11171127
/// skip unknown or uninteresting boxes.
1128+
///
1129+
/// See ISO 14496-12:2015 § 4.2
11181130
fn read_box_header<T: ReadBytesExt>(src: &mut T) -> Result<BoxHeader> {
11191131
let size32 = be_u32(src)?;
11201132
let name = BoxType::from(be_u32(src)?);
@@ -1123,17 +1135,21 @@ fn read_box_header<T: ReadBytesExt>(src: &mut T) -> Result<BoxHeader> {
11231135
0 => return Err(Error::Unsupported("unknown sized box")),
11241136
1 => {
11251137
let size64 = be_u64(src)?;
1126-
if size64 < 16 {
1138+
if size64 < BoxHeader::MIN_LARGE_SIZE {
11271139
return Err(Error::InvalidData("malformed wide size"));
11281140
}
11291141
size64
11301142
}
1131-
2..=7 => return Err(Error::InvalidData("malformed size")),
1132-
_ => u64::from(size32),
1143+
_ => {
1144+
if u64::from(size32) < BoxHeader::MIN_SIZE {
1145+
return Err(Error::InvalidData("malformed size"));
1146+
}
1147+
u64::from(size32)
1148+
}
11331149
};
11341150
let mut offset = match size32 {
1135-
1 => 4 + 4 + 8,
1136-
_ => 4 + 4,
1151+
1 => BoxHeader::MIN_LARGE_SIZE,
1152+
_ => BoxHeader::MIN_SIZE,
11371153
};
11381154
let uuid = if name == BoxType::UuidBox {
11391155
if size >= offset + 16 {
@@ -2389,6 +2405,7 @@ fn read_flac_metadata<T: Read>(src: &mut BMFFBox<T>) -> Result<FLACMetadataBlock
23892405
Ok(FLACMetadataBlock { block_type, data })
23902406
}
23912407

2408+
/// See ISO 14496-1:2010 § 7.2.6.5
23922409
fn find_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
23932410
// Tags for elementary stream description
23942411
const ESDESCR_TAG: u8 = 0x03;
@@ -2402,6 +2419,8 @@ fn find_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
24022419
let des = &mut Cursor::new(remains);
24032420
let tag = des.read_u8()?;
24042421

2422+
// See ISO 14496-1:2010 § 8.3.3 for interpreting size of exandable classes
2423+
24052424
let mut end: u32 = 0; // It's u8 without declaration type that is incorrect.
24062425
// MSB of extend_or_len indicates more bytes, up to 4 bytes.
24072426
for _ in 0..4 {
@@ -2413,7 +2432,7 @@ fn find_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
24132432
}
24142433
let extend_or_len = des.read_u8()?;
24152434
end = (end << 7) + u32::from(extend_or_len & 0x7F);
2416-
if (extend_or_len & 0x80) == 0 {
2435+
if (extend_or_len & 0b1000_0000) == 0 {
24172436
end += des.position() as u32;
24182437
break;
24192438
}
@@ -2441,6 +2460,7 @@ fn find_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
24412460
}
24422461

24432462
remains = &remains[end.to_usize()..remains.len()];
2463+
debug!("remains.len(): {}", remains.len());
24442464
}
24452465

24462466
Ok(())
@@ -2457,6 +2477,7 @@ fn get_audio_object_type(bit_reader: &mut BitReader) -> Result<u16> {
24572477
Ok(audio_object_type)
24582478
}
24592479

2480+
/// See ISO 14496-1:2010 § 7.2.6.7 and probably 14496-3 somewhere?
24602481
fn read_ds_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
24612482
let frequency_table = vec![
24622483
(0x0, 96000),
@@ -2623,6 +2644,7 @@ fn read_surround_channel_count(bit_reader: &mut BitReader, channels: u8) -> Resu
26232644
Ok(count)
26242645
}
26252646

2647+
/// See ISO 14496-1:2010 § 7.2.6.6
26262648
fn read_dc_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
26272649
let des = &mut Cursor::new(data);
26282650
let object_profile = des.read_u8()?;
@@ -2643,6 +2665,7 @@ fn read_dc_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
26432665
Ok(())
26442666
}
26452667

2668+
/// See ISO 14496-1:2010 § 7.2.6.5
26462669
fn read_es_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
26472670
let des = &mut Cursor::new(data);
26482671

@@ -2673,14 +2696,7 @@ fn read_es_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
26732696
fn read_esds<T: Read>(src: &mut BMFFBox<T>) -> Result<ES_Descriptor> {
26742697
let (_, _) = read_fullbox_extra(src)?;
26752698

2676-
// Subtract 4 extra to offset the members of fullbox not accounted for in
2677-
// head.offset
2678-
let esds_size = src
2679-
.head
2680-
.size
2681-
.checked_sub(src.head.offset + 4)
2682-
.expect("offset invalid");
2683-
let esds_array = read_buf(src, esds_size)?;
2699+
let esds_array = read_buf(src, src.bytes_left())?;
26842700

26852701
let mut es_data = ES_Descriptor::default();
26862702
find_descriptor(&esds_array, &mut es_data)?;
@@ -2983,6 +2999,7 @@ fn read_qt_wave_atom<T: Read>(src: &mut BMFFBox<T>) -> Result<ES_Descriptor> {
29832999
}
29843000

29853001
/// Parse an audio description inside an stsd box.
3002+
/// See ISO 14496-12:2015 § 12.2.3
29863003
fn read_audio_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleEntry> {
29873004
let name = src.get_header().name;
29883005

@@ -3115,6 +3132,7 @@ fn read_audio_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleEntry>
31153132
}
31163133

31173134
/// Parse a stsd box.
3135+
/// See ISO 14496-12:2015 § 8.5.2
31183136
fn read_stsd<T: Read>(src: &mut BMFFBox<T>, track: &mut Track) -> Result<SampleDescriptionBox> {
31193137
let (_, _) = read_fullbox_extra(src)?;
31203138

mp4parse_capi/examples/dump.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn dump_file(filename: &str) {
3434
match rv {
3535
Mp4parseStatus::Ok => (),
3636
_ => {
37-
println!("-- fail to parse, '-v' for more info");
37+
println!("-- fail to parse: {:?}, '-v' for more info", rv);
3838
return;
3939
}
4040
}
@@ -44,8 +44,8 @@ fn dump_file(filename: &str) {
4444
Mp4parseStatus::Ok => {
4545
println!("-- mp4parse_fragment_info {:?}", frag_info);
4646
}
47-
_ => {
48-
println!("-- mp4parse_fragment_info failed");
47+
rv => {
48+
println!("-- mp4parse_fragment_info failed with {:?}", rv);
4949
return;
5050
}
5151
}

0 commit comments

Comments
 (0)