Skip to main content

rff_format/
group.rs

1//! Sample groups and track references: what [`SampleIndex::sample_groups`]
2//! and [`Demuxer::track_references`] give.
3//!
4//! [`SampleIndex::sample_groups`]: crate::SampleIndex::sample_groups
5//! [`Demuxer::track_references`]: crate::Demuxer::track_references
6
7/// One grouping of a stream's samples (MP4 `sbgp` + `sgpd`): the descriptions
8/// of its groups, and which samples are in which.
9///
10/// Samples are numbered from 0 in the **stored** sample table, the numbering
11/// of [`raw_sample_info`](crate::SampleIndex::raw_sample_info), fragments
12/// included. (Uncompressed audio whose index has one entry per chunk is the
13/// exception: groups then count `stsz` samples, i.e. PCM frames, as the file
14/// does.)
15#[derive(Debug, Clone, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct SampleGrouping {
18    /// `grouping_type`: `roll`, `rap `, `seig`, `sync`, `tscl`, ...
19    pub grouping_type: [u8; 4],
20    /// `grouping_type_parameter`, when the `sbgp` is of version 1.
21    pub parameter: Option<u32>,
22    /// The group descriptions as stored (the payload of each `sgpd` entry).
23    /// A fragment's own descriptions follow the movie's.
24    pub descriptions: Vec<Vec<u8>>,
25    /// The runs of samples the file assigns, in sample order.
26    pub runs: Vec<SampleGroupRun>,
27    /// The group of samples no run covers (`sgpd` version 2's
28    /// `default_group_description_index`), as an index into `descriptions`.
29    pub default: Option<usize>,
30}
31
32/// Consecutive samples with the same group assignment.
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34#[non_exhaustive]
35pub struct SampleGroupRun {
36    pub first_sample: u64,
37    pub sample_count: u32,
38    /// Index into [`SampleGrouping::descriptions`], or `None` for samples
39    /// the file puts in no group of this type.
40    pub description: Option<usize>,
41}
42
43impl SampleGroupRun {
44    pub fn new(first_sample: u64, sample_count: u32, description: Option<usize>) -> Self {
45        SampleGroupRun {
46            first_sample,
47            sample_count,
48            description,
49        }
50    }
51}
52
53impl SampleGrouping {
54    pub fn new(grouping_type: [u8; 4], parameter: Option<u32>) -> SampleGrouping {
55        SampleGrouping {
56            grouping_type,
57            parameter,
58            descriptions: Vec::new(),
59            runs: Vec::new(),
60            default: None,
61        }
62    }
63
64    /// The index in [`descriptions`](Self::descriptions) of stored sample
65    /// `sample`'s group; `None` when it is in no group of this type.
66    pub fn group_of(&self, sample: u64) -> Option<usize> {
67        let n = self.runs.partition_point(|r| r.first_sample <= sample);
68        match n.checked_sub(1).map(|i| &self.runs[i]) {
69            Some(r) if sample - r.first_sample < u64::from(r.sample_count) => r.description,
70            _ => self.default,
71        }
72    }
73
74    /// The description of stored sample `sample`'s group.
75    pub fn description_of(&self, sample: u64) -> Option<&[u8]> {
76        self.descriptions
77            .get(self.group_of(sample)?)
78            .map(Vec::as_slice)
79    }
80}
81
82/// A typed reference from one stream to others (MP4 `tref`): `tmcd` names the
83/// timecode streams, `chap` the chapter track, `cdsc` described content, ...
84#[derive(Debug, Clone, PartialEq, Eq)]
85#[non_exhaustive]
86pub struct TrackReference {
87    pub kind: [u8; 4],
88    /// Indices of the streams referred to.
89    pub streams: Vec<usize>,
90}
91
92impl TrackReference {
93    pub fn new(kind: [u8; 4], streams: Vec<usize>) -> TrackReference {
94        TrackReference { kind, streams }
95    }
96}
97
98#[cfg(test)]
99mod tests {
100    use super::*;
101
102    #[test]
103    fn samples_outside_every_run_take_the_default_group() {
104        let mut g = SampleGrouping::new(*b"roll", None);
105        g.descriptions = vec![vec![1], vec![2]];
106        g.runs = vec![
107            SampleGroupRun::new(0, 2, Some(0)),
108            SampleGroupRun::new(2, 1, None),
109            SampleGroupRun::new(10, 1, Some(7)),
110        ];
111        assert_eq!(g.group_of(1), Some(0));
112        assert_eq!(g.group_of(2), None);
113        assert_eq!(g.group_of(3), None);
114        g.default = Some(1);
115        assert_eq!(g.description_of(3), Some(&[2u8][..]));
116        assert_eq!(g.group_of(2), None); // explicitly in no group
117        assert_eq!(g.description_of(10), None); // a dangling index
118    }
119}