mp4_atom/moov/trak/mdia/minf/stbl/stsd/hevc/
hev1.rs1use crate::*;
2
3#[derive(Debug, Clone, PartialEq, Eq, Default)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5pub struct Hev1 {
6 pub visual: Visual,
7 pub hvcc: Hvcc,
8 pub btrt: Option<Btrt>,
9 pub colr: Option<Colr>,
10 pub pasp: Option<Pasp>,
11 pub taic: Option<Taic>,
12}
13
14impl Atom for Hev1 {
15 const KIND: FourCC = FourCC::new(b"hev1");
16
17 fn decode_body<B: Buf>(buf: &mut B) -> Result<Self> {
18 let visual = Visual::decode(buf)?;
19
20 let mut hvcc = None;
21 let mut btrt = None;
22 let mut colr = None;
23 let mut pasp = None;
24 let mut taic = None;
25 while let Some(atom) = Any::decode_maybe(buf)? {
26 match atom {
27 Any::Hvcc(atom) => hvcc = atom.into(),
28 Any::Btrt(atom) => btrt = atom.into(),
29 Any::Colr(atom) => colr = atom.into(),
30 Any::Pasp(atom) => pasp = atom.into(),
31 Any::Taic(atom) => taic = atom.into(),
32 _ => tracing::warn!("unknown atom: {:?}", atom),
33 }
34 }
35
36 Ok(Hev1 {
37 visual,
38 hvcc: hvcc.ok_or(Error::MissingBox(Hvcc::KIND))?,
39 btrt,
40 colr,
41 pasp,
42 taic,
43 })
44 }
45
46 fn encode_body<B: BufMut>(&self, buf: &mut B) -> Result<()> {
47 self.visual.encode(buf)?;
48 self.hvcc.encode(buf)?;
49 if self.btrt.is_some() {
50 self.btrt.encode(buf)?;
51 }
52 if self.colr.is_some() {
53 self.colr.encode(buf)?;
54 }
55 if self.pasp.is_some() {
56 self.pasp.encode(buf)?;
57 }
58 if self.taic.is_some() {
59 self.taic.encode(buf)?
60 }
61
62 Ok(())
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_hev1() {
72 let expected = Hev1 {
73 visual: Visual {
74 data_reference_index: 1,
75 width: 320,
76 height: 240,
77 horizresolution: 0x48.into(),
78 vertresolution: 0x48.into(),
79 frame_count: 1,
80 compressor: "ya boy".into(),
81 depth: 24,
82 },
83 hvcc: Hvcc {
84 configuration_version: 1,
85 ..Default::default()
86 },
87 btrt: None,
88 colr: None,
89 pasp: None,
90 taic: None,
91 };
92 let mut buf = Vec::new();
93 expected.encode(&mut buf).unwrap();
94
95 let mut buf = buf.as_ref();
96 let decoded = Hev1::decode(&mut buf).unwrap();
97 assert_eq!(decoded, expected);
98 }
99}