smpte_timecode/timecode/
rate.rs1use super::{Ratio, TimecodeError};
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
11pub struct FrameRate {
12 nominal: u8,
13 ntsc: bool,
14 drop_frame: bool,
15}
16
17impl FrameRate {
18 pub const FPS_23_976: Self = Self::known(24, true, false);
20 pub const FPS_24: Self = Self::known(24, false, false);
22 pub const FPS_25: Self = Self::known(25, false, false);
24 pub const FPS_29_97: Self = Self::known(30, true, false);
26 pub const FPS_29_97_DF: Self = Self::known(30, true, true);
28 pub const FPS_30: Self = Self::known(30, false, false);
30 pub const FPS_30_DF: Self = Self::known(30, false, true);
32 pub const FPS_47_952: Self = Self::known(48, true, false);
34 pub const FPS_48: Self = Self::known(48, false, false);
36 pub const FPS_50: Self = Self::known(50, false, false);
38 pub const FPS_59_94: Self = Self::known(60, true, false);
40 pub const FPS_59_94_DF: Self = Self::known(60, true, true);
42 pub const FPS_60: Self = Self::known(60, false, false);
44 pub const FPS_60_DF: Self = Self::known(60, false, true);
46 pub const FPS_100: Self = Self::known(100, false, false);
48 pub const FPS_119_88: Self = Self::known(120, true, false);
50 pub const FPS_119_88_DF: Self = Self::known(120, true, true);
52 pub const FPS_120: Self = Self::known(120, false, false);
54 pub const FPS_120_DF: Self = Self::known(120, false, true);
56
57 pub const ALL: [Self; 19] = [
59 Self::FPS_23_976,
60 Self::FPS_24,
61 Self::FPS_25,
62 Self::FPS_29_97,
63 Self::FPS_29_97_DF,
64 Self::FPS_30,
65 Self::FPS_30_DF,
66 Self::FPS_47_952,
67 Self::FPS_48,
68 Self::FPS_50,
69 Self::FPS_59_94,
70 Self::FPS_59_94_DF,
71 Self::FPS_60,
72 Self::FPS_60_DF,
73 Self::FPS_100,
74 Self::FPS_119_88,
75 Self::FPS_119_88_DF,
76 Self::FPS_120,
77 Self::FPS_120_DF,
78 ];
79
80 pub const fn new(nominal: u8, ntsc: bool, drop_frame: bool) -> Result<Self, TimecodeError> {
88 let mut index = 0;
89 while index < Self::ALL.len() {
90 let rate = Self::ALL[index];
91 if rate.nominal == nominal && rate.ntsc == ntsc && rate.drop_frame == drop_frame {
92 return Ok(rate);
93 }
94 index += 1;
95 }
96 Err(TimecodeError::UnsupportedFrameRate)
97 }
98
99 pub const fn nominal(self) -> u8 {
101 self.nominal
102 }
103
104 pub const fn is_ntsc(self) -> bool {
106 self.ntsc
107 }
108
109 pub const fn is_drop_frame(self) -> bool {
111 self.drop_frame
112 }
113
114 pub const fn frames_per_second(self) -> Ratio {
116 if self.ntsc {
117 Ratio::reduced(self.nominal as u64 * 1000, 1001)
118 } else {
119 Ratio::from_integer(self.nominal as u64)
120 }
121 }
122
123 pub const fn frames_per_day(self) -> u32 {
125 self.frames_per_ten_minutes() * 6 * 24
126 }
127
128 pub(crate) const fn dropped_per_minute(self) -> u32 {
130 if self.drop_frame {
131 self.nominal as u32 / 15
132 } else {
133 0
134 }
135 }
136
137 pub(crate) const fn frames_per_minute(self) -> u32 {
138 self.nominal as u32 * 60 - self.dropped_per_minute()
139 }
140
141 pub(crate) const fn frames_per_ten_minutes(self) -> u32 {
142 self.nominal as u32 * 600 - self.dropped_per_minute() * 9
144 }
145
146 const fn known(nominal: u8, ntsc: bool, drop_frame: bool) -> Self {
147 Self {
148 nominal,
149 ntsc,
150 drop_frame,
151 }
152 }
153}