Skip to content

Commit 48aca82

Browse files
kornelskibaumanj
andcommitted
Read auxC subtype
Co-authored-by: baumanj <[email protected]>
1 parent d32b098 commit 48aca82

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

mp4parse/src/lib.rs

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use bitreader::{BitReader, ReadInto};
1515
use byteorder::{ReadBytesExt, WriteBytesExt};
1616
use fallible_collections::TryClone;
1717
use fallible_collections::TryRead;
18+
use fallible_collections::TryReserveError;
1819
use num_traits::Num;
1920
use std::convert::{TryFrom, TryInto as _};
2021
use std::io::Cursor;
@@ -201,14 +202,14 @@ impl From<Error> for std::io::Error {
201202
}
202203
}
203204

204-
impl From<fallible_collections::TryReserveError> for Error {
205-
fn from(_: fallible_collections::TryReserveError) -> Error {
205+
impl From<TryReserveError> for Error {
206+
fn from(_: TryReserveError) -> Error {
206207
Error::OutOfMemory
207208
}
208209
}
209210

210211
/// Result shorthand using our Error enum.
211-
pub type Result<T> = std::result::Result<T, Error>;
212+
pub type Result<T, E = Error> = std::result::Result<T, E>;
212213

213214
/// Basic ISO box structure.
214215
///
@@ -1281,7 +1282,7 @@ pub fn read_avif<T: Read>(f: &mut T, context: &mut AvifContext) -> Result<()> {
12811282
prop.item_id == item_id
12821283
&& match &prop.property {
12831284
ItemProperty::AuxiliaryType(urn) => {
1284-
urn.as_slice()
1285+
urn.aux_type.as_slice()
12851286
== "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha".as_bytes()
12861287
}
12871288
_ => false,
@@ -1572,7 +1573,7 @@ fn read_iprp<T: Read>(src: &mut BMFFBox<T>) -> Result<TryVec<AssociatedProperty>
15721573
if *prop != ItemProperty::Unsupported {
15731574
associated.push(AssociatedProperty {
15741575
item_id: a.item_id,
1575-
property: prop.clone()?,
1576+
property: prop.try_clone()?,
15761577
})?;
15771578
}
15781579
}
@@ -1583,12 +1584,12 @@ fn read_iprp<T: Read>(src: &mut BMFFBox<T>) -> Result<TryVec<AssociatedProperty>
15831584
#[derive(Debug, PartialEq)]
15841585
pub enum ItemProperty {
15851586
Channels(TryVec<u8>),
1586-
AuxiliaryType(TryString),
1587+
AuxiliaryType(AuxiliaryTypeProperty),
15871588
Unsupported,
15881589
}
15891590

1590-
impl ItemProperty {
1591-
fn clone(&self) -> Result<Self> {
1591+
impl TryClone for ItemProperty {
1592+
fn try_clone(&self) -> Result<Self, TryReserveError> {
15921593
Ok(match self {
15931594
Self::Channels(val) => Self::Channels(val.try_clone()?),
15941595
Self::AuxiliaryType(val) => Self::AuxiliaryType(val.try_clone()?),
@@ -1674,20 +1675,44 @@ fn read_pixi<T: Read>(src: &mut BMFFBox<T>) -> Result<TryVec<u8>> {
16741675
Ok(channels)
16751676
}
16761677

1677-
fn read_auxc<T: Read>(src: &mut BMFFBox<T>) -> Result<TryString> {
1678+
#[derive(Debug, PartialEq)]
1679+
pub struct AuxiliaryTypeProperty {
1680+
aux_type: TryString,
1681+
aux_subtype: TryString,
1682+
}
1683+
1684+
impl TryClone for AuxiliaryTypeProperty {
1685+
fn try_clone(&self) -> Result<Self, TryReserveError> {
1686+
Ok(AuxiliaryTypeProperty {
1687+
aux_type: self.aux_type.try_clone()?,
1688+
aux_subtype: self.aux_subtype.try_clone()?,
1689+
})
1690+
}
1691+
}
1692+
1693+
fn read_auxc<T: Read>(src: &mut BMFFBox<T>) -> Result<AuxiliaryTypeProperty> {
16781694
let version = read_fullbox_version_no_flags(src)?;
16791695
if version != 0 {
16801696
return Err(Error::Unsupported("auxC version"));
16811697
}
16821698

16831699
let mut aux = TryString::new();
1684-
loop {
1685-
match src.read_u8()? {
1686-
0 => break,
1687-
c => aux.push(c)?,
1688-
}
1700+
src.try_read_to_end(&mut aux)?;
1701+
1702+
let (aux_type, aux_subtype): (TryString, TryVec<u8>);
1703+
if let Some(nul_byte_pos) = aux.iter().position(|&b| b == b'\0') {
1704+
let (a, b) = aux.as_slice().split_at(nul_byte_pos);
1705+
aux_type = a.try_into()?;
1706+
aux_subtype = (&b[1..]).try_into()?;
1707+
} else {
1708+
aux_type = aux;
1709+
aux_subtype = TryVec::new();
16891710
}
1690-
Ok(aux)
1711+
1712+
Ok(AuxiliaryTypeProperty {
1713+
aux_type,
1714+
aux_subtype,
1715+
})
16911716
}
16921717

16931718
/// Parse an item location box inside a meta box

0 commit comments

Comments
 (0)