isobmff/boxes/
entity_grouping.rs

1use scuffle_bytes_util::zero_copy::{Deserialize, Serialize};
2
3use crate::{FullBoxHeader, IsoBox, IsoSized, UnknownBox};
4
5/// Groups list box
6///
7/// ISO/IEC 14496-12 - 8.18.2
8#[derive(IsoBox, Debug, PartialEq, Eq)]
9#[iso_box(box_type = b"grpl", crate_path = crate)]
10pub struct GroupsListBox<'a> {
11    /// The contained [`AltrEntityToGroupBox`]es. (one or more)
12    #[iso_box(nested_box(collect))]
13    pub altr: Vec<AltrEntityToGroupBox>,
14    /// A list of unknown boxes that were not recognized during deserialization.
15    #[iso_box(nested_box(collect_unknown))]
16    pub unknown_boxes: Vec<UnknownBox<'a>>,
17}
18
19/// General entity to group box
20///
21/// ISO/IEC 14496-12 - 8.18.3
22#[derive(Debug, PartialEq, Eq)]
23pub struct EntityToGroupBox {
24    /// A non-negative integer assigned to the particular grouping that shall not be equal to any
25    /// `group_id` value of any other [`EntityToGroupBox`], any `item_ID` value of the hierarchy level (file, movie
26    /// or track) that contains the [`GroupsListBox`], or any `track_ID` value (when the [`GroupsListBox`] is
27    /// contained in the file level).
28    pub group_id: u32,
29    /// Specifies the number of `entity_id` values mapped to this entity group.
30    pub num_entities_in_group: u32,
31    /// Resolved to an item, when an item with `item_ID` equal to `entity_id` is present in the
32    /// hierarchy level (file, movie or track) that contains the [`GroupsListBox`], or to a track, when a track
33    /// with `track_ID` equal to `entity_id` is present and the [`GroupsListBox`] is contained in the file level.
34    pub entity_id: Vec<u32>,
35}
36
37impl<'a> Deserialize<'a> for EntityToGroupBox {
38    fn deserialize<R>(mut reader: R) -> std::io::Result<Self>
39    where
40        R: scuffle_bytes_util::zero_copy::ZeroCopyReader<'a>,
41    {
42        let group_id = u32::deserialize(&mut reader)?;
43        let num_entities_in_group = u32::deserialize(&mut reader)?;
44
45        let mut entity_id = Vec::with_capacity(num_entities_in_group as usize);
46        for _ in 0..num_entities_in_group {
47            entity_id.push(u32::deserialize(&mut reader)?);
48        }
49
50        Ok(EntityToGroupBox {
51            group_id,
52            num_entities_in_group,
53            entity_id,
54        })
55    }
56}
57
58impl Serialize for EntityToGroupBox {
59    fn serialize<W>(&self, mut writer: W) -> std::io::Result<()>
60    where
61        W: std::io::Write,
62    {
63        self.group_id.serialize(&mut writer)?;
64        self.num_entities_in_group.serialize(&mut writer)?;
65
66        for id in &self.entity_id {
67            id.serialize(&mut writer)?;
68        }
69
70        Ok(())
71    }
72}
73
74impl IsoSized for EntityToGroupBox {
75    fn size(&self) -> usize {
76        self.group_id.size() + self.num_entities_in_group.size() + self.entity_id.size()
77    }
78}
79
80/// 'altr' entity to group box
81///
82/// ISO/IEC 14496-12 - 8.18.3
83#[derive(IsoBox, Debug, PartialEq, Eq)]
84#[iso_box(box_type = b"altr", crate_path = crate)]
85pub struct AltrEntityToGroupBox {
86    /// The full box header.
87    pub full_header: FullBoxHeader,
88    /// The contained [`EntityToGroupBox`].
89    pub entity_to_group: EntityToGroupBox,
90}