use super::{Ratio, TimecodeError};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FrameRate {
nominal: u8,
ntsc: bool,
drop_frame: bool,
}
impl FrameRate {
pub const FPS_23_976: Self = Self::known(24, true, false);
pub const FPS_24: Self = Self::known(24, false, false);
pub const FPS_25: Self = Self::known(25, false, false);
pub const FPS_29_97: Self = Self::known(30, true, false);
pub const FPS_29_97_DF: Self = Self::known(30, true, true);
pub const FPS_30: Self = Self::known(30, false, false);
pub const FPS_30_DF: Self = Self::known(30, false, true);
pub const FPS_47_952: Self = Self::known(48, true, false);
pub const FPS_48: Self = Self::known(48, false, false);
pub const FPS_50: Self = Self::known(50, false, false);
pub const FPS_59_94: Self = Self::known(60, true, false);
pub const FPS_59_94_DF: Self = Self::known(60, true, true);
pub const FPS_60: Self = Self::known(60, false, false);
pub const FPS_60_DF: Self = Self::known(60, false, true);
pub const FPS_100: Self = Self::known(100, false, false);
pub const FPS_119_88: Self = Self::known(120, true, false);
pub const FPS_119_88_DF: Self = Self::known(120, true, true);
pub const FPS_120: Self = Self::known(120, false, false);
pub const FPS_120_DF: Self = Self::known(120, false, true);
pub const ALL: [Self; 19] = [
Self::FPS_23_976,
Self::FPS_24,
Self::FPS_25,
Self::FPS_29_97,
Self::FPS_29_97_DF,
Self::FPS_30,
Self::FPS_30_DF,
Self::FPS_47_952,
Self::FPS_48,
Self::FPS_50,
Self::FPS_59_94,
Self::FPS_59_94_DF,
Self::FPS_60,
Self::FPS_60_DF,
Self::FPS_100,
Self::FPS_119_88,
Self::FPS_119_88_DF,
Self::FPS_120,
Self::FPS_120_DF,
];
pub const fn new(nominal: u8, ntsc: bool, drop_frame: bool) -> Result<Self, TimecodeError> {
let mut index = 0;
while index < Self::ALL.len() {
let rate = Self::ALL[index];
if rate.nominal == nominal && rate.ntsc == ntsc && rate.drop_frame == drop_frame {
return Ok(rate);
}
index += 1;
}
Err(TimecodeError::UnsupportedFrameRate)
}
pub const fn nominal(self) -> u8 {
self.nominal
}
pub const fn is_ntsc(self) -> bool {
self.ntsc
}
pub const fn is_drop_frame(self) -> bool {
self.drop_frame
}
pub const fn frames_per_second(self) -> Ratio {
if self.ntsc {
Ratio::reduced(self.nominal as u64 * 1000, 1001)
} else {
Ratio::from_integer(self.nominal as u64)
}
}
pub const fn frames_per_day(self) -> u32 {
self.frames_per_ten_minutes() * 6 * 24
}
pub(crate) const fn dropped_per_minute(self) -> u32 {
if self.drop_frame {
self.nominal as u32 / 15
} else {
0
}
}
pub(crate) const fn frames_per_minute(self) -> u32 {
self.nominal as u32 * 60 - self.dropped_per_minute()
}
pub(crate) const fn frames_per_ten_minutes(self) -> u32 {
self.nominal as u32 * 600 - self.dropped_per_minute() * 9
}
const fn known(nominal: u8, ntsc: bool, drop_frame: bool) -> Self {
Self {
nominal,
ntsc,
drop_frame,
}
}
}