Skip to content

Parse ESDS. #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 90 additions & 9 deletions mp4parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern crate afl;
extern crate byteorder;
use byteorder::ReadBytesExt;
use std::io::{Read, Take};
use std::io::Cursor;
use std::cmp;

mod boxes;
Expand Down Expand Up @@ -203,10 +204,17 @@ pub enum SampleEntry {
Unknown,
}

#[allow(non_camel_case_types)]
#[derive(Debug, Clone)]
pub struct ES_Descriptor {
pub audio_codec: CodecType,
pub codec_specific_config: Vec<u8>,
}

#[allow(non_camel_case_types)]
#[derive(Debug, Clone)]
pub enum AudioCodecSpecific {
ES_Descriptor(Vec<u8>),
ES_Descriptor(ES_Descriptor),
FLACSpecificBox(FLACSpecificBox),
OpusSpecificBox(OpusSpecificBox),
}
Expand Down Expand Up @@ -310,9 +318,10 @@ impl Default for TrackType {
fn default() -> Self { TrackType::Unknown }
}

#[derive(Debug)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CodecType {
Unknown,
MP3,
AAC,
FLAC,
Opus,
Expand Down Expand Up @@ -1105,16 +1114,84 @@ fn read_flac_metadata<T: Read>(src: &mut BMFFBox<T>) -> Result<FLACMetadataBlock
})
}

fn read_esds<T: Read>(src: &mut BMFFBox<T>) -> Result<AudioCodecSpecific> {
fn read_esds<T: Read>(src: &mut BMFFBox<T>) -> Result<ES_Descriptor> {
// Tags for elementary stream description
const ESDESCR_TAG: u8 = 0x03;
const DECODER_CONFIG_TAG: u8 = 0x04;

let (_, _) = try!(read_fullbox_extra(src));

let esds_size = src.head.size - src.head.offset - 4;
if esds_size > BUF_SIZE_LIMIT {
return Err(Error::InvalidData("esds box exceeds BUF_SIZE_LIMIT"));
}
let esds = try!(read_buf(&mut src.content, esds_size as usize));
let esds_array = try!(read_buf(src, esds_size as usize));

// Parsing DecoderConfig descriptor to get the object_profile_indicator
// for correct codec type.
let object_profile_indicator = {
// clone a esds cursor for parsing.
let esds = &mut Cursor::new(&esds_array);
let esds_tag = try!(esds.read_u8());

if esds_tag != ESDESCR_TAG {
return Err(Error::Unsupported("fail to parse ES descriptor"));
}

let esds_extend = try!(esds.read_u8());
// extension tag start from 0x80.
if esds_extend >= 0x80 {
// skip remaining extension and length.
try!(skip(esds, 5));
} else {
try!(skip(esds, 2));
}

let esds_flags = try!(esds.read_u8());

Ok(AudioCodecSpecific::ES_Descriptor(esds))
// Stream dependency flag, first bit from left most.
if esds_flags & 0x80 > 0 {
// Skip uninteresting fields.
try!(skip(esds, 2));
}

// Url flag, second bit from left most.
if esds_flags & 0x40 > 0 {
// Skip uninteresting fields.
let skip_es_len: usize = try!(esds.read_u8()) as usize + 2;
try!(skip(esds, skip_es_len));
}

// find DecoderConfig descriptor (tag = DECODER_CONFIG_TAG)
let dcds = try!(esds.read_u8());
if dcds != DECODER_CONFIG_TAG {
return Err(Error::Unsupported("fail to parse decoder config descriptor"));
}

let dcds_extend = try!(esds.read_u8());
// extension tag start from 0x80.
if dcds_extend >= 0x80 {
// skip remains extension and length.
try!(skip(esds, 3));
}

try!(esds.read_u8())
};

let codec = match object_profile_indicator {
0x40 | 0x41 => CodecType::AAC,
0x6B => CodecType::MP3,
_ => CodecType::Unknown,
};

if codec == CodecType::Unknown {
return Err(Error::Unsupported("unknown audio codec"));
}

Ok(ES_Descriptor {
audio_codec: codec,
codec_specific_config: esds_array,
})
}

/// Parse `FLACSpecificBox`.
Expand Down Expand Up @@ -1325,7 +1402,7 @@ fn read_video_sample_entry<T: Read>(src: &mut BMFFBox<T>, track: &mut Track) ->
.ok_or_else(|| Error::InvalidData("malformed video sample entry"))
}

fn read_qt_wave_atom<T: Read>(src: &mut BMFFBox<T>) -> Result<AudioCodecSpecific> {
fn read_qt_wave_atom<T: Read>(src: &mut BMFFBox<T>) -> Result<ES_Descriptor> {
let mut codec_specific = None;
let mut iter = src.box_iter();
while let Some(mut b) = try!(iter.next_box()) {
Expand Down Expand Up @@ -1398,14 +1475,16 @@ fn read_audio_sample_entry<T: Read>(src: &mut BMFFBox<T>, track: &mut Track) ->
}

let esds = try!(read_esds(&mut b));
codec_specific = Some(esds);
track.codec_type = esds.audio_codec;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, set track.codec_type and codec_specific in the same order in each of the branches - right now there's a mix of both variants.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

codec_specific = Some(AudioCodecSpecific::ES_Descriptor(esds));
}
BoxType::FLACSpecificBox => {
if name != BoxType::FLACSampleEntry ||
codec_specific.is_some() {
return Err(Error::InvalidData("malformed audio sample entry"));
}
let dfla = try!(read_dfla(&mut b));
track.codec_type = CodecType::FLAC;
codec_specific = Some(AudioCodecSpecific::FLACSpecificBox(dfla));
}
BoxType::OpusSpecificBox => {
Expand All @@ -1414,11 +1493,13 @@ fn read_audio_sample_entry<T: Read>(src: &mut BMFFBox<T>, track: &mut Track) ->
return Err(Error::InvalidData("malformed audio sample entry"));
}
let dops = try!(read_dops(&mut b));
track.codec_type = CodecType::Opus;
codec_specific = Some(AudioCodecSpecific::OpusSpecificBox(dops));
}
BoxType::QTWaveAtom => {
let qt_desc = try!(read_qt_wave_atom(&mut b));
codec_specific = Some(qt_desc);
let qt_esds = try!(read_qt_wave_atom(&mut b));
track.codec_type = qt_esds.audio_codec;
codec_specific = Some(AudioCodecSpecific::ES_Descriptor(qt_esds));
}
_ => try!(skip_box_content(&mut b)),
}
Expand Down
10 changes: 9 additions & 1 deletion mp4parse/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,14 @@ fn skip_padding_in_stsd() {
#[test]
fn read_qt_wave_atom() {
let esds = make_fullbox(BoxSize::Auto, b"esds", 0, |s| {
s.append_repeated(0, 30)
s.B8(0x03) // elementary stream descriptor tag
.B8(0x0b) // esds length
.append_repeated(0, 2)
.B8(0x00) // flags
.B8(0x04) // decoder config descriptor tag
.B8(0x06) // dcds length
.B8(0x6b) // mp3
.append_repeated(0, 5)
}).into_inner();
let wave = make_box(BoxSize::Auto, b"wave", |s| {
s.append_bytes(esds.as_slice())
Expand All @@ -849,4 +856,5 @@ fn read_qt_wave_atom() {
let mut track = super::Track::new(0);
super::read_audio_sample_entry(&mut stream, &mut track)
.expect("fail to read qt wave atom");
assert_eq!(track.codec_type, super::CodecType::MP3);
}
4 changes: 2 additions & 2 deletions mp4parse/tests/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ fn public_api() {

// track.data part
assert_eq!(match a.codec_specific {
mp4::AudioCodecSpecific::ES_Descriptor(v) => {
assert!(v.len() > 0);
mp4::AudioCodecSpecific::ES_Descriptor(esds) => {
assert_eq!(esds.audio_codec, mp4::CodecType::AAC);
"ES"
}
mp4::AudioCodecSpecific::FLACSpecificBox(_) => {
Expand Down
14 changes: 10 additions & 4 deletions mp4parse_capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use mp4parse::MediaScaledTime;
use mp4parse::TrackTimeScale;
use mp4parse::TrackScaledTime;
use mp4parse::serialize_opus_header;
use mp4parse::CodecType;

// rusty-cheddar's C enum generation doesn't namespace enum members by
// prefixing them, so we're forced to do it in our member names until
Expand Down Expand Up @@ -88,6 +89,7 @@ pub enum mp4parse_codec {
MP4PARSE_CODEC_OPUS,
MP4PARSE_CODEC_AVC,
MP4PARSE_CODEC_VP9,
MP4PARSE_CODEC_MP3,
}

#[repr(C)]
Expand Down Expand Up @@ -345,8 +347,12 @@ pub unsafe extern fn mp4parse_get_track_info(parser: *mut mp4parse_parser, track
mp4parse_codec::MP4PARSE_CODEC_OPUS,
AudioCodecSpecific::FLACSpecificBox(_) =>
mp4parse_codec::MP4PARSE_CODEC_FLAC,
AudioCodecSpecific::ES_Descriptor(_) =>
AudioCodecSpecific::ES_Descriptor(ref esds) if esds.audio_codec == CodecType::AAC =>
mp4parse_codec::MP4PARSE_CODEC_AAC,
AudioCodecSpecific::ES_Descriptor(ref esds) if esds.audio_codec == CodecType::MP3 =>
mp4parse_codec::MP4PARSE_CODEC_MP3,
AudioCodecSpecific::ES_Descriptor(_) =>
mp4parse_codec::MP4PARSE_CODEC_UNKNOWN,
},
Some(SampleEntry::Video(ref video)) => match video.codec_specific {
VideoCodecSpecific::VPxConfig(_) =>
Expand Down Expand Up @@ -433,11 +439,11 @@ pub unsafe extern fn mp4parse_get_track_audio_info(parser: *mut mp4parse_parser,

match audio.codec_specific {
AudioCodecSpecific::ES_Descriptor(ref v) => {
if v.len() > std::u32::MAX as usize {
if v.codec_specific_config.len() > std::u32::MAX as usize {
return MP4PARSE_ERROR_INVALID;
}
(*info).codec_specific_config.length = v.len() as u32;
(*info).codec_specific_config.data = v.as_ptr();
(*info).codec_specific_config.length = v.codec_specific_config.len() as u32;
(*info).codec_specific_config.data = v.codec_specific_config.as_ptr();
}
AudioCodecSpecific::FLACSpecificBox(_) => {
return MP4PARSE_ERROR_UNSUPPORTED;
Expand Down