@@ -208,6 +208,7 @@ pub enum SampleEntry {
208
208
#[ derive( Debug , Clone ) ]
209
209
pub struct ES_Descriptor {
210
210
pub audio_codec : CodecType ,
211
+ pub audio_sample_rate : Option < u32 > ,
211
212
pub codec_specific_config : Vec < u8 > ,
212
213
}
213
214
@@ -1116,8 +1117,15 @@ fn read_flac_metadata<T: Read>(src: &mut BMFFBox<T>) -> Result<FLACMetadataBlock
1116
1117
1117
1118
fn read_esds < T : Read > ( src : & mut BMFFBox < T > ) -> Result < ES_Descriptor > {
1118
1119
// Tags for elementary stream description
1119
- const ESDESCR_TAG : u8 = 0x03 ;
1120
- const DECODER_CONFIG_TAG : u8 = 0x04 ;
1120
+ const ESDESCR_TAG : u8 = 0x03 ;
1121
+ const DECODER_CONFIG_TAG : u8 = 0x04 ;
1122
+ const DECODER_SPECIFIC_TAG : u8 = 0x05 ;
1123
+
1124
+ let frequency_table =
1125
+ vec ! [ ( 0x1 , 96000 ) , ( 0x1 , 88200 ) , ( 0x2 , 64000 ) , ( 0x3 , 48000 ) ,
1126
+ ( 0x4 , 44100 ) , ( 0x5 , 32000 ) , ( 0x6 , 24000 ) , ( 0x7 , 22050 ) ,
1127
+ ( 0x8 , 16000 ) , ( 0x9 , 12000 ) , ( 0xa , 11025 ) , ( 0xb , 8000 ) ,
1128
+ ( 0xc , 7350 ) ] ;
1121
1129
1122
1130
let ( _, _) = try!( read_fullbox_extra ( src) ) ;
1123
1131
@@ -1128,24 +1136,29 @@ fn read_esds<T: Read>(src: &mut BMFFBox<T>) -> Result<ES_Descriptor> {
1128
1136
let esds_array = try!( read_buf ( src, esds_size as usize ) ) ;
1129
1137
1130
1138
// Parsing DecoderConfig descriptor to get the object_profile_indicator
1131
- // for correct codec type.
1132
- let object_profile_indicator = {
1139
+ // for correct codec type and audio sample rate.
1140
+ let ( object_profile_indicator, sample_frequency) = {
1141
+ let mut object_profile: u8 = 0 ;
1142
+ let mut sample_frequency = None ;
1143
+
1133
1144
// clone a esds cursor for parsing.
1134
1145
let esds = & mut Cursor :: new ( & esds_array) ;
1135
- let esds_tag = try!( esds. read_u8 ( ) ) ;
1146
+ let next_tag = try!( esds. read_u8 ( ) ) ;
1136
1147
1137
- if esds_tag != ESDESCR_TAG {
1148
+ if next_tag != ESDESCR_TAG {
1138
1149
return Err ( Error :: Unsupported ( "fail to parse ES descriptor" ) ) ;
1139
1150
}
1140
1151
1141
1152
let esds_extend = try!( esds. read_u8 ( ) ) ;
1142
1153
// extension tag start from 0x80.
1143
- if esds_extend >= 0x80 {
1144
- // skip remaining extension and length.
1145
- try!( skip ( esds, 5 ) ) ;
1146
- } else {
1154
+ let esds_end = if esds_extend >= 0x80 {
1155
+ // skip remaining extension.
1147
1156
try!( skip ( esds, 2 ) ) ;
1148
- }
1157
+ esds. position ( ) + try!( esds. read_u8 ( ) ) as u64
1158
+ } else {
1159
+ esds. position ( ) + esds_extend as u64
1160
+ } ;
1161
+ try!( skip ( esds, 2 ) ) ;
1149
1162
1150
1163
let esds_flags = try!( esds. read_u8 ( ) ) ;
1151
1164
@@ -1163,19 +1176,45 @@ fn read_esds<T: Read>(src: &mut BMFFBox<T>) -> Result<ES_Descriptor> {
1163
1176
}
1164
1177
1165
1178
// find DecoderConfig descriptor (tag = DECODER_CONFIG_TAG)
1166
- let dcds = try!( esds. read_u8 ( ) ) ;
1167
- if dcds != DECODER_CONFIG_TAG {
1168
- return Err ( Error :: Unsupported ( "fail to parse decoder config descriptor" ) ) ;
1179
+ if esds_end > esds. position ( ) {
1180
+ let next_tag = try!( esds. read_u8 ( ) ) ;
1181
+ if next_tag == DECODER_CONFIG_TAG {
1182
+ let dcds_extend = try!( esds. read_u8 ( ) ) ;
1183
+ // extension tag start from 0x80.
1184
+ if dcds_extend >= 0x80 {
1185
+ // skip remains extension and length.
1186
+ try!( skip ( esds, 3 ) ) ;
1187
+ }
1188
+
1189
+ object_profile = try!( esds. read_u8 ( ) ) ;
1190
+
1191
+ // Skip uninteresting fields.
1192
+ try!( skip ( esds, 12 ) ) ;
1193
+ }
1169
1194
}
1170
1195
1171
- let dcds_extend = try!( esds. read_u8 ( ) ) ;
1172
- // extension tag start from 0x80.
1173
- if dcds_extend >= 0x80 {
1174
- // skip remains extension and length.
1175
- try!( skip ( esds, 3 ) ) ;
1196
+
1197
+ // find DecoderSpecific descriptor (tag = DECODER_SPECIFIC_TAG)
1198
+ if esds_end > esds. position ( ) {
1199
+ let next_tag = try!( esds. read_u8 ( ) ) ;
1200
+ if next_tag == DECODER_SPECIFIC_TAG {
1201
+ let dsds_extend = try!( esds. read_u8 ( ) ) ;
1202
+ // extension tag start from 0x80.
1203
+ if dsds_extend >= 0x80 {
1204
+ // skip remains extension and length.
1205
+ try!( skip ( esds, 3 ) ) ;
1206
+ }
1207
+
1208
+ let audio_specific_config = try!( be_u16 ( esds) ) ;
1209
+
1210
+ let sample_index = ( audio_specific_config & 0x07FF ) >> 7 ;
1211
+
1212
+ sample_frequency =
1213
+ frequency_table. iter ( ) . find ( |item| item. 0 == sample_index) . map ( |x| x. 1 ) ;
1214
+ }
1176
1215
}
1177
1216
1178
- try! ( esds . read_u8 ( ) )
1217
+ ( object_profile , sample_frequency )
1179
1218
} ;
1180
1219
1181
1220
let codec = match object_profile_indicator {
@@ -1190,6 +1229,7 @@ fn read_esds<T: Read>(src: &mut BMFFBox<T>) -> Result<ES_Descriptor> {
1190
1229
1191
1230
Ok ( ES_Descriptor {
1192
1231
audio_codec : codec,
1232
+ audio_sample_rate : sample_frequency,
1193
1233
codec_specific_config : esds_array,
1194
1234
} )
1195
1235
}
0 commit comments