mp4_atom/moov/trak/mdia/minf/stbl/stsd/
av01.rs1use crate::coding::{Decode, Encode};
2use crate::{
3 skip_trailing_padding, Any, Atom, Buf, BufMut, Ccst, DecodeMaybe, Error, FourCC, Result,
4};
5
6use super::{Btrt, Colr, Pasp, Taic, Visual};
7
8#[derive(Debug, Clone, PartialEq, Eq, Default)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub struct Av01 {
11 pub visual: Visual,
12 pub av1c: Av1c,
13 pub btrt: Option<Btrt>,
14 pub ccst: Option<Ccst>,
15 pub colr: Option<Colr>,
16 pub pasp: Option<Pasp>,
17 pub taic: Option<Taic>,
18}
19
20impl Atom for Av01 {
21 const KIND: FourCC = FourCC::new(b"av01");
22
23 fn decode_body<B: Buf>(buf: &mut B) -> Result<Self> {
24 let visual = Visual::decode(buf)?;
25
26 let mut av1c = None;
27 let mut btrt = None;
28 let mut ccst = None;
29 let mut colr = None;
30 let mut pasp = None;
31 let mut taic = None;
32 while let Some(atom) = Any::decode_maybe(buf)? {
33 match atom {
34 Any::Av1c(atom) => av1c = atom.into(),
35 Any::Btrt(atom) => btrt = atom.into(),
36 Any::Ccst(atom) => ccst = atom.into(),
37 Any::Colr(atom) => colr = atom.into(),
38 Any::Pasp(atom) => pasp = atom.into(),
39 Any::Taic(atom) => taic = atom.into(),
40 unknown => Self::decode_unknown(&unknown)?,
41 }
42 }
43 skip_trailing_padding(buf);
44
45 Ok(Av01 {
46 visual,
47 av1c: av1c.ok_or(Error::MissingBox(Av1c::KIND))?,
48 btrt,
49 ccst,
50 colr,
51 pasp,
52 taic,
53 })
54 }
55
56 fn encode_body<B: BufMut>(&self, buf: &mut B) -> Result<()> {
57 self.visual.encode(buf)?;
58 self.av1c.encode(buf)?;
59 self.btrt.encode(buf)?;
60 self.ccst.encode(buf)?;
61 self.colr.encode(buf)?;
62 self.pasp.encode(buf)?;
63 self.taic.encode(buf)?;
64
65 Ok(())
66 }
67}
68
69#[derive(Debug, Clone, PartialEq, Eq, Default)]
71#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
72pub struct Av1c {
73 pub seq_profile: u8,
74 pub seq_level_idx_0: u8,
75 pub seq_tier_0: bool,
76 pub high_bitdepth: bool,
77 pub twelve_bit: bool,
78 pub monochrome: bool,
79 pub chroma_subsampling_x: bool,
80 pub chroma_subsampling_y: bool,
81 pub chroma_sample_position: u8, pub initial_presentation_delay: Option<u8>,
83 pub config_obus: Vec<u8>,
84}
85
86impl Atom for Av1c {
87 const KIND: FourCC = FourCC::new(b"av1C");
88
89 fn decode_body<B: Buf>(buf: &mut B) -> Result<Self> {
90 let version = u8::decode(buf)?;
91 if version != 0b1000_0001 {
92 return Err(Error::UnknownVersion(version));
93 }
94
95 let v = u8::decode(buf)?;
96 let seq_profile = v >> 5;
97 let seq_level_idx_0 = v & 0b11111;
98
99 let v = u8::decode(buf)?;
100 let seq_tier_0 = (v >> 7) == 1;
101 let high_bitdepth = ((v >> 6) & 0b1) == 1;
102 let twelve_bit = ((v >> 5) & 0b1) == 1;
103 let monochrome = ((v >> 4) & 0b1) == 1;
104 let chroma_subsampling_x = ((v >> 3) & 0b1) == 1;
105 let chroma_subsampling_y = ((v >> 2) & 0b1) == 1;
106 let chroma_sample_position = v & 0b11;
107
108 let v = u8::decode(buf)?;
109 let reserved = v >> 5;
110 if reserved != 0 {
111 return Err(Error::Reserved);
112 }
113
114 let initial_presentation_delay_present = (v >> 4) & 0b1;
115 let initial_presentation_delay_minus_one = v & 0b1111;
116
117 let initial_presentation_delay = if initial_presentation_delay_present == 1 {
118 Some(initial_presentation_delay_minus_one + 1)
119 } else {
120 if initial_presentation_delay_minus_one != 0 {
121 return Err(Error::Reserved);
122 }
123
124 None
125 };
126
127 let config_obus = Vec::decode(buf)?;
128
129 Ok(Self {
130 seq_profile,
131 seq_level_idx_0,
132 seq_tier_0,
133 high_bitdepth,
134 twelve_bit,
135 monochrome,
136 chroma_subsampling_x,
137 chroma_subsampling_y,
138 chroma_sample_position,
139 initial_presentation_delay,
140 config_obus,
141 })
142 }
143
144 fn encode_body<B: BufMut>(&self, buf: &mut B) -> Result<()> {
145 0b1000_0001_u8.encode(buf)?;
146 ((self.seq_profile << 5) | self.seq_level_idx_0).encode(buf)?;
147
148 (((self.seq_tier_0 as u8) << 7)
149 | ((self.high_bitdepth as u8) << 6)
150 | ((self.twelve_bit as u8) << 5)
151 | ((self.monochrome as u8) << 4)
152 | ((self.chroma_subsampling_x as u8) << 3)
153 | ((self.chroma_subsampling_y as u8) << 2)
154 | self.chroma_sample_position)
155 .encode(buf)?;
156
157 if let Some(initial_presentation_delay) = self.initial_presentation_delay {
158 ((initial_presentation_delay - 1) | 0b0001_0000).encode(buf)?;
159 } else {
160 0b0000_0000_u8.encode(buf)?;
161 }
162
163 self.config_obus.encode(buf)?;
164
165 Ok(())
166 }
167}