@@ -214,6 +214,8 @@ pub type Result<T> = std::result::Result<T, Error>;
214
214
/// begins with a header describing the length of the box's data and a
215
215
/// four-byte box type which identifies the type of the box. Together these
216
216
/// are enough to interpret the contents of that section of the file.
217
+ ///
218
+ /// See ISO 14496-12:2015 § 4.2
217
219
#[ derive( Debug , Clone , Copy ) ]
218
220
struct BoxHeader {
219
221
/// Box type.
@@ -226,6 +228,11 @@ struct BoxHeader {
226
228
uuid : Option < [ u8 ; 16 ] > ,
227
229
}
228
230
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
+
229
236
/// File type box 'ftyp'.
230
237
#[ derive( Debug ) ]
231
238
struct FileTypeBox {
@@ -367,6 +374,8 @@ pub enum SampleEntry {
367
374
Unknown ,
368
375
}
369
376
377
+ /// An Elementary Stream Descriptor
378
+ /// See ISO 14496-1:2010 § 7.2.6.5
370
379
#[ allow( non_camel_case_types) ]
371
380
#[ derive( Debug , Default ) ]
372
381
pub struct ES_Descriptor {
@@ -1007,6 +1016,7 @@ impl Track {
1007
1016
}
1008
1017
}
1009
1018
1019
+ /// See ISO 14496-12:2015 § 4.2
1010
1020
struct BMFFBox < ' a , T : ' a > {
1011
1021
head : BoxHeader ,
1012
1022
content : Take < & ' a mut T > ,
@@ -1115,6 +1125,8 @@ impl<'a, T> Drop for BMFFBox<'a, T> {
1115
1125
/// and its length. Used internally for dispatching to specific
1116
1126
/// parsers for the internal content, or to get the length to
1117
1127
/// skip unknown or uninteresting boxes.
1128
+ ///
1129
+ /// See ISO 14496-12:2015 § 4.2
1118
1130
fn read_box_header < T : ReadBytesExt > ( src : & mut T ) -> Result < BoxHeader > {
1119
1131
let size32 = be_u32 ( src) ?;
1120
1132
let name = BoxType :: from ( be_u32 ( src) ?) ;
@@ -1123,17 +1135,21 @@ fn read_box_header<T: ReadBytesExt>(src: &mut T) -> Result<BoxHeader> {
1123
1135
0 => return Err ( Error :: Unsupported ( "unknown sized box" ) ) ,
1124
1136
1 => {
1125
1137
let size64 = be_u64 ( src) ?;
1126
- if size64 < 16 {
1138
+ if size64 < BoxHeader :: MIN_LARGE_SIZE {
1127
1139
return Err ( Error :: InvalidData ( "malformed wide size" ) ) ;
1128
1140
}
1129
1141
size64
1130
1142
}
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
+ }
1133
1149
} ;
1134
1150
let mut offset = match size32 {
1135
- 1 => 4 + 4 + 8 ,
1136
- _ => 4 + 4 ,
1151
+ 1 => BoxHeader :: MIN_LARGE_SIZE ,
1152
+ _ => BoxHeader :: MIN_SIZE ,
1137
1153
} ;
1138
1154
let uuid = if name == BoxType :: UuidBox {
1139
1155
if size >= offset + 16 {
@@ -2389,6 +2405,7 @@ fn read_flac_metadata<T: Read>(src: &mut BMFFBox<T>) -> Result<FLACMetadataBlock
2389
2405
Ok ( FLACMetadataBlock { block_type, data } )
2390
2406
}
2391
2407
2408
+ /// See ISO 14496-1:2010 § 7.2.6.5
2392
2409
fn find_descriptor ( data : & [ u8 ] , esds : & mut ES_Descriptor ) -> Result < ( ) > {
2393
2410
// Tags for elementary stream description
2394
2411
const ESDESCR_TAG : u8 = 0x03 ;
@@ -2402,6 +2419,8 @@ fn find_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
2402
2419
let des = & mut Cursor :: new ( remains) ;
2403
2420
let tag = des. read_u8 ( ) ?;
2404
2421
2422
+ // See ISO 14496-1:2010 § 8.3.3 for interpreting size of exandable classes
2423
+
2405
2424
let mut end: u32 = 0 ; // It's u8 without declaration type that is incorrect.
2406
2425
// MSB of extend_or_len indicates more bytes, up to 4 bytes.
2407
2426
for _ in 0 ..4 {
@@ -2413,7 +2432,7 @@ fn find_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
2413
2432
}
2414
2433
let extend_or_len = des. read_u8 ( ) ?;
2415
2434
end = ( end << 7 ) + u32:: from ( extend_or_len & 0x7F ) ;
2416
- if ( extend_or_len & 0x80 ) == 0 {
2435
+ if ( extend_or_len & 0b1000_0000 ) == 0 {
2417
2436
end += des. position ( ) as u32 ;
2418
2437
break ;
2419
2438
}
@@ -2441,6 +2460,7 @@ fn find_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
2441
2460
}
2442
2461
2443
2462
remains = & remains[ end. to_usize ( ) ..remains. len ( ) ] ;
2463
+ debug ! ( "remains.len(): {}" , remains. len( ) ) ;
2444
2464
}
2445
2465
2446
2466
Ok ( ( ) )
@@ -2457,6 +2477,7 @@ fn get_audio_object_type(bit_reader: &mut BitReader) -> Result<u16> {
2457
2477
Ok ( audio_object_type)
2458
2478
}
2459
2479
2480
+ /// See ISO 14496-1:2010 § 7.2.6.7 and probably 14496-3 somewhere?
2460
2481
fn read_ds_descriptor ( data : & [ u8 ] , esds : & mut ES_Descriptor ) -> Result < ( ) > {
2461
2482
let frequency_table = vec ! [
2462
2483
( 0x0 , 96000 ) ,
@@ -2623,6 +2644,7 @@ fn read_surround_channel_count(bit_reader: &mut BitReader, channels: u8) -> Resu
2623
2644
Ok ( count)
2624
2645
}
2625
2646
2647
+ /// See ISO 14496-1:2010 § 7.2.6.6
2626
2648
fn read_dc_descriptor ( data : & [ u8 ] , esds : & mut ES_Descriptor ) -> Result < ( ) > {
2627
2649
let des = & mut Cursor :: new ( data) ;
2628
2650
let object_profile = des. read_u8 ( ) ?;
@@ -2643,6 +2665,7 @@ fn read_dc_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
2643
2665
Ok ( ( ) )
2644
2666
}
2645
2667
2668
+ /// See ISO 14496-1:2010 § 7.2.6.5
2646
2669
fn read_es_descriptor ( data : & [ u8 ] , esds : & mut ES_Descriptor ) -> Result < ( ) > {
2647
2670
let des = & mut Cursor :: new ( data) ;
2648
2671
@@ -2673,14 +2696,7 @@ fn read_es_descriptor(data: &[u8], esds: &mut ES_Descriptor) -> Result<()> {
2673
2696
fn read_esds < T : Read > ( src : & mut BMFFBox < T > ) -> Result < ES_Descriptor > {
2674
2697
let ( _, _) = read_fullbox_extra ( src) ?;
2675
2698
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 ( ) ) ?;
2684
2700
2685
2701
let mut es_data = ES_Descriptor :: default ( ) ;
2686
2702
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> {
2983
2999
}
2984
3000
2985
3001
/// Parse an audio description inside an stsd box.
3002
+ /// See ISO 14496-12:2015 § 12.2.3
2986
3003
fn read_audio_sample_entry < T : Read > ( src : & mut BMFFBox < T > ) -> Result < SampleEntry > {
2987
3004
let name = src. get_header ( ) . name ;
2988
3005
@@ -3115,6 +3132,7 @@ fn read_audio_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleEntry>
3115
3132
}
3116
3133
3117
3134
/// Parse a stsd box.
3135
+ /// See ISO 14496-12:2015 § 8.5.2
3118
3136
fn read_stsd < T : Read > ( src : & mut BMFFBox < T > , track : & mut Track ) -> Result < SampleDescriptionBox > {
3119
3137
let ( _, _) = read_fullbox_extra ( src) ?;
3120
3138
0 commit comments