forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

units.rs134 lines, 3.7 KiBHistoryRawDownload
1//! Time, timestamps and the rational base between them.
2
3/// A media timestamp, in [`TimeBase`] ticks.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
5pub struct Timestamp(pub i64);
6
7impl Timestamp {
8 /// A timestamp of `ticks`.
9 pub fn new(ticks: i64) -> Timestamp {
10 Timestamp(ticks)
11 }
12
13 /// The tick count.
14 pub fn value(&self) -> i64 {
15 self.0
16 }
17}
18
19/// A duration in [`TimeBase`] ticks.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
21pub struct Duration(pub u64);
22
23impl Duration {
24 /// A duration of `ticks`.
25 pub fn new(ticks: u64) -> Duration {
26 Duration(ticks)
27 }
28
29 /// The tick count.
30 pub fn value(&self) -> u64 {
31 self.0
32 }
33}
34
35/// Wall-clock time, split so that no fraction is lost to a float.
36#[derive(Debug, Clone, Copy, PartialEq, Default)]
37pub struct Time {
38 /// Whole seconds.
39 pub seconds: u64,
40 /// Fraction of a second, 0.0..1.0.
41 pub frac: f64,
42}
43
44impl Time {
45 /// A time from whole seconds and a fraction.
46 pub fn new(seconds: u64, frac: f64) -> Time {
47 Time { seconds, frac }
48 }
49
50 /// A time from seconds, or [`None`] for a negative or non-finite input —
51 /// which is what makes a caller check before seeking to it.
52 pub fn try_from_secs_f64(secs: f64) -> Option<Time> {
53 if !secs.is_finite() || secs < 0.0 {
54 return None;
55 }
56 let seconds = secs.trunc() as u64;
57 Some(Time {
58 seconds,
59 frac: secs - secs.trunc(),
60 })
61 }
62
63 /// This time in seconds.
64 pub fn as_secs_f64(&self) -> f64 {
65 self.seconds as f64 + self.frac
66 }
67}
68
69/// A rational tick duration: `numer / denom` seconds.
70#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
71pub struct TimeBase {
72 /// Numerator, in seconds.
73 pub numer: u32,
74 /// Denominator.
75 pub denom: u32,
76}
77
78impl TimeBase {
79 /// A time base of `numer / denom` seconds per tick.
80 pub fn new(numer: u32, denom: u32) -> TimeBase {
81 TimeBase { numer, denom }
82 }
83
84 /// `ts` as wall-clock time; a base with a zero denominator answers zero
85 /// rather than dividing by it.
86 pub fn calc_time_saturating(&self, ts: Timestamp) -> Time {
87 if self.denom == 0 {
88 return Time::default();
89 }
90 let secs = ts.0 as f64 * f64::from(self.numer) / f64::from(self.denom);
91 Time::try_from_secs_f64(secs).unwrap_or_default()
92 }
93
94 /// The timestamp `time` falls on in this base.
95 pub fn calc_timestamp(&self, time: Time) -> Timestamp {
96 if self.numer == 0 {
97 return Timestamp::default();
98 }
99 Timestamp((time.as_secs_f64() * f64::from(self.denom) / f64::from(self.numer)) as i64)
100 }
101}
102
103impl From<ec_core::TimeBase> for TimeBase {
104 fn from(tb: ec_core::TimeBase) -> TimeBase {
105 TimeBase::new(tb.num().max(0) as u32, tb.den().max(1) as u32)
106 }
107}
108
109impl From<TimeBase> for ec_core::TimeBase {
110 fn from(tb: TimeBase) -> ec_core::TimeBase {
111 ec_core::TimeBase::new(i64::from(tb.numer.max(1)), i64::from(tb.denom.max(1)))
112 }
113}
114
115#[cfg(test)]
116mod tests {
117 use super::*;
118
119 #[test]
120 fn a_timestamp_round_trips_through_its_base() {
121 let base = TimeBase::new(1, 48_000);
122 let time = base.calc_time_saturating(Timestamp::new(96_000));
123 assert_eq!(time.as_secs_f64(), 2.0);
124 assert_eq!(base.calc_timestamp(time), Timestamp::new(96_000));
125 // A negative instant is not a time, and says so rather than wrapping.
126 assert_eq!(Time::try_from_secs_f64(-1.0), None);
127 assert_eq!(
128 TimeBase::new(1, 0)
129 .calc_time_saturating(Timestamp::new(5))
130 .as_secs_f64(),
131 0.0
132 );
133 }
134}