Skip to content

Commit d06b166

Browse files
committed
Make read_iloc return a map rather than a vec
This is more natural since we want to ensure the uniqueness of the IDs
1 parent 724bcc1 commit d06b166

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

mp4parse/src/lib.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ struct AvifMeta {
804804
item_references: TryVec<SingleItemTypeReferenceBox>,
805805
properties: TryVec<AssociatedProperty>,
806806
primary_item_id: u32,
807-
iloc_items: TryVec<ItemLocationBoxItem>,
807+
iloc_items: TryHashMap<u32, ItemLocationBoxItem>,
808808
}
809809

810810
/// A Media Data Box
@@ -1060,7 +1060,6 @@ impl TryFrom<u8> for IlocVersion {
10601060
/// `data_reference_index` is omitted, since only 0 (i.e., this file) is supported
10611061
#[derive(Debug)]
10621062
struct ItemLocationBoxItem {
1063-
item_id: u32,
10641063
construction_method: ConstructionMethod,
10651064
/// Unused for ConstructionMethod::Idat
10661065
extents: TryVec<Extent>,
@@ -1449,10 +1448,10 @@ pub fn read_avif<T: Read>(f: &mut T) -> Result<AvifContext> {
14491448
let mut alpha_item = None;
14501449

14511450
// store data or record location of relevant items
1452-
for loc in meta.iloc_items {
1453-
let item = if loc.item_id == meta.primary_item_id {
1451+
for (item_id, loc) in meta.iloc_items {
1452+
let item = if item_id == meta.primary_item_id {
14541453
&mut primary_item
1455-
} else if Some(loc.item_id) == alpha_item_id {
1454+
} else if Some(item_id) == alpha_item_id {
14561455
&mut alpha_item
14571456
} else {
14581457
continue;
@@ -2114,7 +2113,7 @@ fn read_auxc<T: Read>(src: &mut BMFFBox<T>) -> Result<AuxiliaryTypeProperty> {
21142113

21152114
/// Parse an item location box inside a meta box
21162115
/// See ISOBMFF (ISO 14496-12:2015) § 8.11.3
2117-
fn read_iloc<T: Read>(src: &mut BMFFBox<T>) -> Result<TryVec<ItemLocationBoxItem>> {
2116+
fn read_iloc<T: Read>(src: &mut BMFFBox<T>) -> Result<TryHashMap<u32, ItemLocationBoxItem>> {
21182117
let version: IlocVersion = read_fullbox_version_no_flags(src)?.try_into()?;
21192118

21202119
let iloc = src.read_into_try_vec()?;
@@ -2137,7 +2136,7 @@ fn read_iloc<T: Read>(src: &mut BMFFBox<T>) -> Result<TryVec<ItemLocationBoxItem
21372136
IlocVersion::Two => iloc.read_u32(32)?,
21382137
};
21392138

2140-
let mut items = TryVec::with_capacity(item_count.to_usize())?;
2139+
let mut items = TryHashMap::with_capacity(item_count.to_usize())?;
21412140

21422141
for _ in 0..item_count {
21432142
let item_id = match version {
@@ -2226,19 +2225,14 @@ fn read_iloc<T: Read>(src: &mut BMFFBox<T>) -> Result<TryVec<ItemLocationBoxItem
22262225
extents.push(extent)?;
22272226
}
22282227

2229-
// TODO: change items to TryHashMap once https://github.com/vcombey/fallible_collections/pull/12 merges
2230-
if items
2231-
.iter()
2232-
.any(|prev_item: &ItemLocationBoxItem| prev_item.item_id == item_id)
2233-
{
2234-
return Err(Error::InvalidData("duplicate item_ID in iloc"));
2235-
}
2236-
2237-
items.push(ItemLocationBoxItem {
2238-
item_id,
2228+
let loc = ItemLocationBoxItem {
22392229
construction_method,
22402230
extents,
2241-
})?;
2231+
};
2232+
2233+
if items.insert(item_id, loc)?.is_some() {
2234+
return Err(Error::InvalidData("duplicate item_ID in iloc"));
2235+
}
22422236
}
22432237

22442238
if iloc.remaining() == 0 {

0 commit comments

Comments
 (0)