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

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