mp4_atom/moov/trak/mdia/minf/stbl/stsd/h264/
avc1.rs

1use crate::*;
2
3#[derive(Debug, Clone, PartialEq, Eq, Default)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5pub struct Avc1 {
6    pub visual: Visual,
7    pub avcc: Avcc,
8}
9
10impl Atom for Avc1 {
11    const KIND: FourCC = FourCC::new(b"avc1");
12
13    fn decode_body<B: Buf>(buf: &mut B) -> Result<Self> {
14        let visual = Visual::decode(buf)?;
15
16        let mut avcc = None;
17        while let Some(atom) = Any::decode_maybe(buf)? {
18            match atom {
19                Any::Avcc(atom) => avcc = atom.into(),
20                _ => tracing::warn!("unknown atom: {:?}", atom),
21            }
22        }
23
24        Ok(Avc1 {
25            visual,
26            avcc: avcc.ok_or(Error::MissingBox(Avcc::KIND))?,
27        })
28    }
29
30    fn encode_body<B: BufMut>(&self, buf: &mut B) -> Result<()> {
31        self.visual.encode(buf)?;
32        self.avcc.encode(buf)?;
33
34        Ok(())
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn test_avc1() {
44        let expected = Avc1 {
45            visual: Visual {
46                data_reference_index: 1,
47                width: 320,
48                height: 240,
49                horizresolution: 0x48.into(),
50                vertresolution: 0x48.into(),
51                frame_count: 1,
52                compressor: "ya boy".into(),
53                depth: 24,
54            },
55            avcc: Avcc {
56                configuration_version: 1,
57                avc_profile_indication: 100,
58                profile_compatibility: 0,
59                avc_level_indication: 13,
60                length_size: 4,
61                sequence_parameter_sets: vec![vec![
62                    0x67, 0x64, 0x00, 0x0D, 0xAC, 0xD9, 0x41, 0x41, 0xFA, 0x10, 0x00, 0x00, 0x03,
63                    0x00, 0x10, 0x00, 0x00, 0x03, 0x03, 0x20, 0xF1, 0x42, 0x99, 0x60,
64                ]],
65                picture_parameter_sets: vec![vec![0x68, 0xEB, 0xE3, 0xCB, 0x22, 0xC0]],
66                ..Default::default()
67            },
68        };
69        let mut buf = Vec::new();
70        expected.encode(&mut buf).unwrap();
71
72        let mut buf = buf.as_ref();
73        let decoded = Avc1::decode(&mut buf).unwrap();
74        assert_eq!(decoded, expected);
75    }
76}