1#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub struct ColorInfo {
18 pub primaries: u16,
20 pub transfer: u16,
22 pub matrix: u16,
24}
25
26impl ColorInfo {
27 pub const UNSPECIFIED_CODE: u16 = 2;
29
30 pub const UNSPECIFIED: ColorInfo = ColorInfo {
32 primaries: 2,
33 transfer: 2,
34 matrix: 2,
35 };
36
37 pub fn is_unspecified(&self) -> bool {
40 *self == ColorInfo::UNSPECIFIED
41 }
42}
43
44impl Default for ColorInfo {
45 fn default() -> ColorInfo {
46 ColorInfo::UNSPECIFIED
47 }
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
54pub struct MasteringDisplay {
55 pub red: (u16, u16),
57 pub green: (u16, u16),
59 pub blue: (u16, u16),
61 pub white_point: (u16, u16),
63 pub max_luminance: u32,
65 pub min_luminance: u32,
67}
68
69impl MasteringDisplay {
70 pub const CHROMA_UNIT: f64 = 0.00002;
72 pub const LUMA_UNIT: f64 = 0.0001;
74
75 pub fn chroma_from_f64(v: f64) -> u16 {
78 (v / Self::CHROMA_UNIT).round().clamp(0.0, u16::MAX as f64) as u16
79 }
80
81 pub fn luma_from_f64(v: f64) -> u32 {
83 (v / Self::LUMA_UNIT).round().clamp(0.0, u32::MAX as f64) as u32
84 }
85}
86
87#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
90pub struct ContentLightLevel {
91 pub max_cll: u16,
93 pub max_fall: u16,
95}
96
97#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
101pub struct CleanAperture {
102 pub width: (u32, u32),
103 pub height: (u32, u32),
104 pub horiz_offset: (i32, u32),
105 pub vert_offset: (i32, u32),
106}
107
108#[derive(Debug, Clone, PartialEq, Eq, Hash)]
114pub struct Timecode {
115 pub start_frame: u32,
117 pub frames_per_second: u8,
119 pub drop_frame: bool,
121 pub timescale: u32,
124 pub frame_duration: u32,
125 pub flags: u32,
128 pub reel_name: Option<String>,
130}
131
132impl Timecode {
133 pub fn new(start_frame: u32, frames_per_second: u8) -> Timecode {
135 let fps = u32::from(frames_per_second.max(1));
136 Timecode {
137 start_frame,
138 frames_per_second: frames_per_second.max(1),
139 drop_frame: false,
140 timescale: fps,
141 frame_duration: 1,
142 flags: 0,
143 reel_name: None,
144 }
145 }
146
147 pub fn parse(text: &str, frames_per_second: u8) -> Option<Timecode> {
150 let drop = text.contains(';');
151 let parts: Vec<u32> = text
152 .split([':', ';', '.'])
153 .map(|p| p.trim().parse().ok())
154 .collect::<Option<Vec<u32>>>()?;
155 let [h, m, s, f] = parts[..] else { return None };
156 let fps = u32::from(frames_per_second.max(1));
157 if m > 59 || s > 59 || f >= fps || (drop && fps % 30 != 0) {
158 return None;
159 }
160 let mut frame = ((h * 60 + m) * 60 + s) * fps + f;
161 if drop {
162 let per_min = 2 * (fps / 30);
163 let minutes = h * 60 + m;
164 frame -= per_min * (minutes - minutes / 10);
165 }
166 let mut tc = Timecode::new(frame, frames_per_second);
167 tc.drop_frame = drop;
168 if drop {
169 tc.timescale = fps * 1000;
170 tc.frame_duration = 1001;
171 }
172 Some(tc)
173 }
174
175 pub fn components(&self) -> (u32, u32, u32, u32) {
177 let fps = u64::from(self.frames_per_second.max(1));
178 let mut f = u64::from(self.start_frame);
179 if self.drop_frame && fps % 30 == 0 {
180 let d = 2 * (fps / 30);
182 let per_10min = fps * 600 - 9 * d;
183 let per_min = fps * 60 - d;
184 let tens = f / per_10min;
185 let rem = f % per_10min;
186 f += 9 * d * tens
187 + if rem > d {
188 d * ((rem - d) / per_min)
189 } else {
190 0
191 };
192 }
193 let frames = f % fps;
194 let secs = f / fps;
195 (
196 (secs / 3600 % 24) as u32,
197 (secs / 60 % 60) as u32,
198 (secs % 60) as u32,
199 frames as u32,
200 )
201 }
202}
203
204impl std::fmt::Display for Timecode {
205 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
206 let (h, m, s, fr) = self.components();
207 let sep = if self.drop_frame { ';' } else { ':' };
208 write!(f, "{h:02}:{m:02}:{s:02}{sep}{fr:02}")
209 }
210}
211
212#[cfg(test)]
213mod tests {
214 use super::*;
215
216 #[test]
217 fn non_drop_timecode_round_trips_through_text() {
218 let tc = Timecode::parse("01:00:00:00", 25).unwrap();
219 assert_eq!(tc.start_frame, 90_000);
220 assert_eq!(tc.to_string(), "01:00:00:00");
221 let tc = Timecode::parse("10:59:59:23", 24).unwrap();
222 assert_eq!(tc.to_string(), "10:59:59:23");
223 }
224
225 #[test]
226 fn drop_frame_skips_two_numbers_a_minute_except_every_tenth() {
227 let tc = Timecode::parse("00:01:00;02", 30).unwrap();
229 assert_eq!(tc.start_frame, 1800);
230 assert_eq!(tc.to_string(), "00:01:00;02");
231 let tc = Timecode::parse("00:10:00;00", 30).unwrap();
233 assert_eq!(tc.start_frame, 17_982);
234 assert_eq!(tc.to_string(), "00:10:00;00");
235 assert_eq!(
237 Timecode::parse("01:00:00;00", 30).unwrap().start_frame,
238 107_892
239 );
240 for frame in [
241 0u32, 1799, 1800, 17_981, 17_982, 107_891, 107_892, 1_000_003,
242 ] {
243 let mut tc = Timecode::new(frame, 30);
244 tc.drop_frame = true;
245 let back = Timecode::parse(&tc.to_string(), 30).unwrap();
246 assert_eq!(back.start_frame, frame, "{tc}");
247 }
248 }
249
250 #[test]
251 fn invalid_timecodes_are_refused() {
252 assert!(Timecode::parse("00:00:00:25", 25).is_none());
253 assert!(Timecode::parse("00:60:00:00", 25).is_none());
254 assert!(Timecode::parse("00:00:00;00", 25).is_none());
255 assert!(Timecode::parse("00:00:00", 25).is_none());
256 }
257
258 #[test]
259 fn colour_defaults_to_unspecified() {
260 assert!(ColorInfo::default().is_unspecified());
261 assert_eq!(MasteringDisplay::chroma_from_f64(0.3127), 15_635);
262 assert_eq!(MasteringDisplay::luma_from_f64(1000.0), 10_000_000);
263 }
264}