1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use num::Rational64;
use regex::Match;
use std::convert::TryFrom;
use std::fmt::Debug;
use crate::consts::{
FEET_AND_FRAMES_REGEX, FRAMES_PER_FOOT, SECONDS_PER_HOUR_I64, SECONDS_PER_MINUTE_I64,
TIMECODE_REGEX,
};
use crate::{timecode_parse, Framerate, Ntsc, TimecodeParseError, TimecodeSections};
pub type FramesSourceResult = Result<i64, TimecodeParseError>;
pub trait FramesSource: Debug {
fn to_frames(&self, rate: Framerate) -> FramesSourceResult;
}
impl<T> FramesSource for &T
where
T: FramesSource,
{
fn to_frames(&self, rate: Framerate) -> FramesSourceResult {
(*self).to_frames(rate)
}
}
impl FramesSource for &dyn FramesSource {
fn to_frames(&self, rate: Framerate) -> FramesSourceResult {
(*self).to_frames(rate)
}
}
impl FramesSource for i64 {
fn to_frames(&self, _: Framerate) -> FramesSourceResult {
Ok(*self)
}
}
impl FramesSource for isize {
fn to_frames(&self, _: Framerate) -> FramesSourceResult {
let i64_val = match i64::try_from(*self) {
Ok(converted) => converted,
Err(err) => {
return Err(TimecodeParseError::Conversion(format!(
"error converting isize to i64 : {}",
err.to_string()
)))
}
};
Ok(i64_val)
}
}
impl FramesSource for usize {
fn to_frames(&self, _: Framerate) -> FramesSourceResult {
let i64_val = match i64::try_from(*self) {
Ok(converted) => converted,
Err(err) => {
return Err(TimecodeParseError::Conversion(format!(
"error converting usize to i64 : {}",
err.to_string()
)))
}
};
Ok(i64_val)
}
}
impl FramesSource for u64 {
fn to_frames(&self, _: Framerate) -> FramesSourceResult {
let i64_val = match i64::try_from(*self) {
Ok(converted) => converted,
Err(err) => {
return Err(TimecodeParseError::Conversion(format!(
"error converting u64 to i64 : {}",
err.to_string()
)))
}
};
Ok(i64_val)
}
}
impl FramesSource for i32 {
fn to_frames(&self, _: Framerate) -> FramesSourceResult {
Ok(i64::from(*self))
}
}
impl FramesSource for u32 {
fn to_frames(&self, _: Framerate) -> FramesSourceResult {
Ok(i64::from(*self))
}
}
impl FramesSource for i16 {
fn to_frames(&self, _: Framerate) -> FramesSourceResult {
Ok(i64::from(*self))
}
}
impl FramesSource for u16 {
fn to_frames(&self, _: Framerate) -> FramesSourceResult {
Ok(i64::from(*self))
}
}
impl FramesSource for i8 {
fn to_frames(&self, _: Framerate) -> FramesSourceResult {
Ok(i64::from(*self))
}
}
impl FramesSource for u8 {
fn to_frames(&self, _: Framerate) -> FramesSourceResult {
Ok(i64::from(*self))
}
}
impl FramesSource for &str {
fn to_frames(&self, rate: Framerate) -> FramesSourceResult {
if let Some(matched) = TIMECODE_REGEX.captures(self) {
return parse_timecode_string(matched, rate);
}
if let Some(matched) = FEET_AND_FRAMES_REGEX.captures(self) {
return parse_feet_and_frames_str(matched);
}
Err(TimecodeParseError::UnknownStrFormat(format!(
"{} is not a known frame-count timecode format",
self
)))
}
}
impl FramesSource for String {
fn to_frames(&self, rate: Framerate) -> FramesSourceResult {
self.as_str().to_frames(rate)
}
}
fn parse_timecode_string(matched: regex::Captures, rate: Framerate) -> FramesSourceResult {
let frames =
timecode_parse::convert_tc_int(matched.name("frames").unwrap().as_str(), "frames")?;
let mut sections: Vec<Match> = Vec::new();
if let Some(section) = matched.name("section1") {
sections.push(section);
};
if let Some(section) = matched.name("section2") {
sections.push(section);
};
if let Some(section) = matched.name("section3") {
sections.push(section);
};
let is_negative = matched.name("negative").is_some();
let seconds: i64 = match sections.pop() {
None => 0,
Some(section) => timecode_parse::convert_tc_int(section.as_str(), "seconds")?,
};
let minutes: i64 = match sections.pop() {
None => 0,
Some(section) => timecode_parse::convert_tc_int(section.as_str(), "minutes")?,
};
let hours: i64 = match sections.pop() {
None => 0,
Some(section) => timecode_parse::convert_tc_int(section.as_str(), "frames")?,
};
let drop_adjustment = if rate.ntsc() == Ntsc::DropFrame {
drop_frame_tc_adjustment(
TimecodeSections {
negative: is_negative,
hours,
minutes,
seconds,
frames,
},
rate,
)?
} else {
0
};
let seconds = seconds + minutes * SECONDS_PER_MINUTE_I64 + hours * SECONDS_PER_HOUR_I64;
let frames_rat =
Rational64::from_integer(seconds) * rate.timebase() + Rational64::from_integer(frames);
let mut frames = frames_rat.round().to_integer();
frames += drop_adjustment;
if is_negative {
frames *= -1
}
Ok(frames)
}
fn drop_frame_tc_adjustment(sections: TimecodeSections, rate: Framerate) -> FramesSourceResult {
let drop_frames = rate.drop_frames().unwrap();
let has_bad_frames = sections.frames < drop_frames;
let is_tenth_minute = sections.minutes % 10 == 0 || sections.minutes == 0;
if has_bad_frames && !is_tenth_minute {
return Err(TimecodeParseError::DropFrameValue(format!(
"drop-frame tc cannot have a frames value of less than {} on minutes not divisible by 10, found '{}'",
drop_frames,
sections.frames,
)));
};
let total_minutes = 60 * sections.hours + sections.minutes;
let adjustment = drop_frames * (total_minutes - total_minutes / 10);
Ok(-adjustment)
}
fn parse_feet_and_frames_str(matched: regex::Captures) -> FramesSourceResult {
let feet = timecode_parse::convert_tc_int(matched.name("feet").unwrap().as_str(), "feet")?;
let mut frames =
timecode_parse::convert_tc_int(matched.name("frames").unwrap().as_str(), "frames")?;
let is_negative = matched.name("negative").is_some();
frames += feet * FRAMES_PER_FOOT;
if is_negative {
frames = -frames;
};
Ok(frames)
}