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

1mod ac3;
2mod audio;
3mod av01;
4mod btrt;
5mod ccst;
6mod colr;
7mod eac3;
8mod flac;
9mod h264;
10mod hevc;
11mod mp4a;
12mod opus;
13mod pasp;
14mod taic;
15mod tx3g;
16mod uncv;
17mod visual;
18mod vp9;
19
20pub use ac3::*;
21pub use audio::*;
22pub use av01::*;
23pub use btrt::*;
24pub use ccst::*;
25pub use colr::*;
26pub use eac3::*;
27pub use flac::*;
28pub use h264::*;
29pub use hevc::*;
30pub use mp4a::*;
31pub use opus::*;
32pub use pasp::*;
33pub use taic::*;
34pub use tx3g::*;
35pub use uncv::*;
36pub use visual::*;
37pub use vp9::*;
38
39use crate::*;
40use derive_more::From;
41
42#[derive(Debug, Clone, PartialEq, Eq, Default)]
43#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
44pub struct Stsd {
45    pub codecs: Vec<Codec>,
46}
47
48/// Called a "sample entry" in the ISOBMFF specification.
49#[derive(Debug, Clone, PartialEq, Eq, From)]
50#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
51pub enum Codec {
52    // H264
53    Avc1(Avc1),
54
55    // HEVC: SPS/PPS/VPS is inline
56    Hev1(Hev1),
57
58    // HEVC: SPS/PPS/VPS is in a separate atom
59    Hvc1(Hvc1),
60
61    // VP8
62    Vp08(Vp08),
63
64    // VP9
65    Vp09(Vp09),
66
67    // AV1
68    Av01(Av01),
69
70    // AAC
71    Mp4a(Mp4a),
72
73    // Text
74    Tx3g(Tx3g),
75
76    // Opus
77    Opus(Opus),
78
79    // Uncompressed video
80    Uncv(Uncv),
81
82    // FLAC audio
83    Flac(Flac),
84
85    // AC-3 audio
86    Ac3(Ac3),
87
88    // EAC-3 audio
89    Eac3(Eac3),
90
91    // Unknown
92    Unknown(FourCC),
93}
94
95impl Decode for Codec {
96    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
97        let atom = Any::decode(buf)?;
98        Ok(match atom {
99            Any::Avc1(atom) => atom.into(),
100            Any::Hev1(atom) => atom.into(),
101            Any::Hvc1(atom) => atom.into(),
102            Any::Vp08(atom) => atom.into(),
103            Any::Vp09(atom) => atom.into(),
104            Any::Mp4a(atom) => atom.into(),
105            Any::Tx3g(atom) => atom.into(),
106            Any::Av01(atom) => atom.into(),
107            Any::Opus(atom) => atom.into(),
108            Any::Uncv(atom) => atom.into(),
109            Any::Flac(atom) => atom.into(),
110            Any::Ac3(atom) => atom.into(),
111            Any::Eac3(atom) => atom.into(),
112            Any::Unknown(kind, _) => Self::Unknown(kind),
113            _ => return Err(Error::UnexpectedBox(atom.kind())),
114        })
115    }
116}
117
118impl Encode for Codec {
119    fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
120        match self {
121            Self::Unknown(kind) => kind.encode(buf),
122            Self::Avc1(atom) => atom.encode(buf),
123            Self::Hev1(atom) => atom.encode(buf),
124            Self::Hvc1(atom) => atom.encode(buf),
125            Self::Vp08(atom) => atom.encode(buf),
126            Self::Vp09(atom) => atom.encode(buf),
127            Self::Mp4a(atom) => atom.encode(buf),
128            Self::Tx3g(atom) => atom.encode(buf),
129            Self::Av01(atom) => atom.encode(buf),
130            Self::Opus(atom) => atom.encode(buf),
131            Self::Uncv(atom) => atom.encode(buf),
132            Self::Flac(atom) => atom.encode(buf),
133            Self::Ac3(atom) => atom.encode(buf),
134            Self::Eac3(atom) => atom.encode(buf),
135        }
136    }
137}
138
139impl AtomExt for Stsd {
140    type Ext = ();
141
142    const KIND_EXT: FourCC = FourCC::new(b"stsd");
143
144    fn decode_body_ext<B: Buf>(buf: &mut B, _ext: ()) -> Result<Self> {
145        let codec_count = u32::decode(buf)?;
146        let mut codecs = Vec::new();
147
148        for _ in 0..codec_count {
149            let codec = Codec::decode(buf)?;
150            codecs.push(codec);
151        }
152
153        Ok(Stsd { codecs })
154    }
155
156    fn encode_body_ext<B: BufMut>(&self, buf: &mut B) -> Result<()> {
157        (self.codecs.len() as u32).encode(buf)?;
158        for codec in &self.codecs {
159            codec.encode(buf)?;
160        }
161
162        Ok(())
163    }
164}