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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
mod avc1;
mod hev1;
mod mp4a;
mod tx3g;
mod vp09;
mod vpcc;

pub use avc1::*;
pub use hev1::*;
pub use mp4a::*;
pub use tx3g::*;
pub use vp09::*;
pub use vpcc::*;

use crate::*;

#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Stsd {
    pub avc1: Option<Avc1>,
    pub hev1: Option<Hev1>,
    pub vp09: Option<Vp09>,
    pub mp4a: Option<Mp4a>,
    pub tx3g: Option<Tx3g>,
}

impl AtomExt for Stsd {
    type Ext = ();

    const KIND_EXT: FourCC = FourCC::new(b"stsd");

    fn decode_body_ext(buf: &mut Bytes, _ext: ()) -> Result<Self> {
        let entries = u32::decode(buf)?;

        let mut avc1 = None;
        let mut hev1 = None;
        let mut vp09 = None;
        let mut mp4a = None;
        let mut tx3g = None;

        for _ in 0..entries {
            let atom = Any::decode(buf)?;
            match atom {
                Any::Avc1(atom) => avc1 = atom.into(),
                Any::Hev1(atom) => hev1 = atom.into(),
                Any::Vp09(atom) => vp09 = atom.into(),
                Any::Mp4a(atom) => mp4a = atom.into(),
                Any::Tx3g(atom) => tx3g = atom.into(),
                Any::Unknown(kind, _) => tracing::warn!("unknown atom: {:?}", kind),
                _ => return Err(Error::UnexpectedBox(atom.kind())),
            }
        }

        Ok(Stsd {
            avc1,
            hev1,
            vp09,
            mp4a,
            tx3g,
        })
    }

    fn encode_body_ext(&self, buf: &mut BytesMut) -> Result<()> {
        (self.avc1.is_some() as u32
            + self.hev1.is_some() as u32
            + self.vp09.is_some() as u32
            + self.mp4a.is_some() as u32
            + self.tx3g.is_some() as u32)
            .encode(buf)?;

        self.avc1.encode(buf)?;
        self.hev1.encode(buf)?;
        self.vp09.encode(buf)?;
        self.mp4a.encode(buf)?;
        self.tx3g.encode(buf)?;

        Ok(())
    }
}