#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ColorInfo {
pub primaries: u16,
pub transfer: u16,
pub matrix: u16,
}
impl ColorInfo {
pub const UNSPECIFIED_CODE: u16 = 2;
pub const UNSPECIFIED: ColorInfo = ColorInfo {
primaries: 2,
transfer: 2,
matrix: 2,
};
pub fn is_unspecified(&self) -> bool {
*self == ColorInfo::UNSPECIFIED
}
}
impl Default for ColorInfo {
fn default() -> ColorInfo {
ColorInfo::UNSPECIFIED
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct MasteringDisplay {
pub red: (u16, u16),
pub green: (u16, u16),
pub blue: (u16, u16),
pub white_point: (u16, u16),
pub max_luminance: u32,
pub min_luminance: u32,
}
impl MasteringDisplay {
pub const CHROMA_UNIT: f64 = 0.00002;
pub const LUMA_UNIT: f64 = 0.0001;
pub fn chroma_from_f64(v: f64) -> u16 {
(v / Self::CHROMA_UNIT).round().clamp(0.0, u16::MAX as f64) as u16
}
pub fn luma_from_f64(v: f64) -> u32 {
(v / Self::LUMA_UNIT).round().clamp(0.0, u32::MAX as f64) as u32
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct ContentLightLevel {
pub max_cll: u16,
pub max_fall: u16,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CleanAperture {
pub width: (u32, u32),
pub height: (u32, u32),
pub horiz_offset: (i32, u32),
pub vert_offset: (i32, u32),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Timecode {
pub start_frame: u32,
pub frames_per_second: u8,
pub drop_frame: bool,
pub timescale: u32,
pub frame_duration: u32,
pub flags: u32,
pub reel_name: Option<String>,
}
impl Timecode {
pub fn new(start_frame: u32, frames_per_second: u8) -> Timecode {
let fps = u32::from(frames_per_second.max(1));
Timecode {
start_frame,
frames_per_second: frames_per_second.max(1),
drop_frame: false,
timescale: fps,
frame_duration: 1,
flags: 0,
reel_name: None,
}
}
pub fn parse(text: &str, frames_per_second: u8) -> Option<Timecode> {
let drop = text.contains(';');
let parts: Vec<u32> = text
.split([':', ';', '.'])
.map(|p| p.trim().parse().ok())
.collect::<Option<Vec<u32>>>()?;
let [h, m, s, f] = parts[..] else { return None };
let fps = u32::from(frames_per_second.max(1));
if m > 59 || s > 59 || f >= fps || (drop && fps % 30 != 0) {
return None;
}
let mut frame = ((h * 60 + m) * 60 + s) * fps + f;
if drop {
let per_min = 2 * (fps / 30);
let minutes = h * 60 + m;
frame -= per_min * (minutes - minutes / 10);
}
let mut tc = Timecode::new(frame, frames_per_second);
tc.drop_frame = drop;
if drop {
tc.timescale = fps * 1000;
tc.frame_duration = 1001;
}
Some(tc)
}
pub fn components(&self) -> (u32, u32, u32, u32) {
let fps = u64::from(self.frames_per_second.max(1));
let mut f = u64::from(self.start_frame);
if self.drop_frame && fps % 30 == 0 {
let d = 2 * (fps / 30);
let per_10min = fps * 600 - 9 * d;
let per_min = fps * 60 - d;
let tens = f / per_10min;
let rem = f % per_10min;
f += 9 * d * tens
+ if rem > d {
d * ((rem - d) / per_min)
} else {
0
};
}
let frames = f % fps;
let secs = f / fps;
(
(secs / 3600 % 24) as u32,
(secs / 60 % 60) as u32,
(secs % 60) as u32,
frames as u32,
)
}
}
impl std::fmt::Display for Timecode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let (h, m, s, fr) = self.components();
let sep = if self.drop_frame { ';' } else { ':' };
write!(f, "{h:02}:{m:02}:{s:02}{sep}{fr:02}")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn non_drop_timecode_round_trips_through_text() {
let tc = Timecode::parse("01:00:00:00", 25).unwrap();
assert_eq!(tc.start_frame, 90_000);
assert_eq!(tc.to_string(), "01:00:00:00");
let tc = Timecode::parse("10:59:59:23", 24).unwrap();
assert_eq!(tc.to_string(), "10:59:59:23");
}
#[test]
fn drop_frame_skips_two_numbers_a_minute_except_every_tenth() {
let tc = Timecode::parse("00:01:00;02", 30).unwrap();
assert_eq!(tc.start_frame, 1800);
assert_eq!(tc.to_string(), "00:01:00;02");
let tc = Timecode::parse("00:10:00;00", 30).unwrap();
assert_eq!(tc.start_frame, 17_982);
assert_eq!(tc.to_string(), "00:10:00;00");
assert_eq!(
Timecode::parse("01:00:00;00", 30).unwrap().start_frame,
107_892
);
for frame in [
0u32, 1799, 1800, 17_981, 17_982, 107_891, 107_892, 1_000_003,
] {
let mut tc = Timecode::new(frame, 30);
tc.drop_frame = true;
let back = Timecode::parse(&tc.to_string(), 30).unwrap();
assert_eq!(back.start_frame, frame, "{tc}");
}
}
#[test]
fn invalid_timecodes_are_refused() {
assert!(Timecode::parse("00:00:00:25", 25).is_none());
assert!(Timecode::parse("00:60:00:00", 25).is_none());
assert!(Timecode::parse("00:00:00;00", 25).is_none());
assert!(Timecode::parse("00:00:00", 25).is_none());
}
#[test]
fn colour_defaults_to_unspecified() {
assert!(ColorInfo::default().is_unspecified());
assert_eq!(MasteringDisplay::chroma_from_f64(0.3127), 15_635);
assert_eq!(MasteringDisplay::luma_from_f64(1000.0), 10_000_000);
}
}