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