mp4_atom/moov/trak/mdia/minf/stbl/stsd/
av01.rs1use crate::coding::{Decode, Encode};
2use crate::{Any, Atom, Buf, BufMut, DecodeMaybe, Error, FourCC, Result};
3
4use super::{Btrt, Colr, Pasp, Taic, Visual};
5
6#[derive(Debug, Clone, PartialEq, Eq, Default)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct Av01 {
9 pub visual: Visual,
10 pub av1c: Av1c,
11 pub btrt: Option<Btrt>,
12 pub colr: Option<Colr>,
13 pub pasp: Option<Pasp>,
14 pub taic: Option<Taic>,
15}
16
17impl Atom for Av01 {
18 const KIND: FourCC = FourCC::new(b"av01");
19
20 fn decode_body<B: Buf>(buf: &mut B) -> Result<Self> {
21 let visual = Visual::decode(buf)?;
22
23 let mut av1c = None;
24 let mut btrt = None;
25 let mut colr = None;
26 let mut pasp = None;
27 let mut taic = None;
28 while let Some(atom) = Any::decode_maybe(buf)? {
29 match atom {
30 Any::Av1c(atom) => av1c = atom.into(),
31 Any::Btrt(atom) => btrt = atom.into(),
32 Any::Colr(atom) => colr = atom.into(),
33 Any::Pasp(atom) => pasp = atom.into(),
34 Any::Taic(atom) => taic = atom.into(),
35 _ => tracing::warn!("unknown atom: {:?}", atom),
36 }
37 }
38
39 Ok(Av01 {
40 visual,
41 av1c: av1c.ok_or(Error::MissingBox(Av1c::KIND))?,
42 btrt,
43 colr,
44 pasp,
45 taic,
46 })
47 }
48
49 fn encode_body<B: BufMut>(&self, buf: &mut B) -> Result<()> {
50 self.visual.encode(buf)?;
51 self.av1c.encode(buf)?;
52 if self.btrt.is_some() {
53 self.btrt.encode(buf)?;
54 }
55 if self.colr.is_some() {
56 self.colr.encode(buf)?;
57 }
58 if self.pasp.is_some() {
59 self.pasp.encode(buf)?;
60 }
61 if self.taic.is_some() {
62 self.taic.encode(buf)?;
63 }
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}