mp4_atom/moov/trak/mdia/minf/stbl/
stsc.rs

1use crate::*;
2
3#[derive(Debug, Clone, PartialEq, Eq, Default)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5pub struct Stsc {
6    pub entries: Vec<StscEntry>,
7}
8
9#[derive(Debug, Clone, PartialEq, Eq, Default)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct StscEntry {
12    pub first_chunk: u32,
13    pub samples_per_chunk: u32,
14    pub sample_description_index: u32,
15}
16
17impl AtomExt for Stsc {
18    type Ext = ();
19
20    const KIND_EXT: FourCC = FourCC::new(b"stsc");
21
22    fn decode_body_ext<B: Buf>(buf: &mut B, _ext: ()) -> Result<Self> {
23        let entry_count = u32::decode(buf)?;
24
25        let mut entries = Vec::new();
26        for _ in 0..entry_count {
27            let entry = StscEntry {
28                first_chunk: u32::decode(buf)?,
29                samples_per_chunk: u32::decode(buf)?,
30                sample_description_index: u32::decode(buf)?,
31            };
32            entries.push(entry);
33        }
34
35        Ok(Stsc { entries })
36    }
37
38    fn encode_body_ext<B: BufMut>(&self, buf: &mut B) -> Result<()> {
39        (self.entries.len() as u32).encode(buf)?;
40        for entry in self.entries.iter() {
41            entry.first_chunk.encode(buf)?;
42            entry.samples_per_chunk.encode(buf)?;
43            entry.sample_description_index.encode(buf)?;
44        }
45
46        Ok(())
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_stsc() {
56        let expected = Stsc {
57            entries: vec![
58                StscEntry {
59                    first_chunk: 1,
60                    samples_per_chunk: 1,
61                    sample_description_index: 1,
62                },
63                StscEntry {
64                    first_chunk: 19026,
65                    samples_per_chunk: 14,
66                    sample_description_index: 1,
67                },
68            ],
69        };
70        let mut buf = Vec::new();
71        expected.encode(&mut buf).unwrap();
72
73        let mut buf = buf.as_ref();
74        let decoded = Stsc::decode(&mut buf).unwrap();
75        assert_eq!(decoded, expected);
76    }
77}