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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//! Time base aware timestamps.

use std::{
    cmp::{Eq, Ordering, PartialEq, PartialOrd},
    fmt::{self, Debug, Formatter},
    ops::{Add, AddAssign, Sub, SubAssign},
    time::Duration,
};

extern "C" {
    fn ffw_rescale_q(n: i64, aq_num: u32, aq_den: u32, bq_num: u32, bq_den: u32) -> i64;
    fn ffw_null_timestamp() -> i64;
}

/// A rational time base (e.g. 1/1000 is a millisecond time base).
#[derive(Copy, Clone)]
pub struct TimeBase {
    num: u32,
    den: u32,
}

impl TimeBase {
    /// A microseconds time base.
    pub const MICROSECONDS: TimeBase = TimeBase::new(1, 1_000_000);

    /// Create a new time base as a rational number with a given numerator and
    /// denominator.
    pub const fn new(num: u32, den: u32) -> Self {
        Self { num, den }
    }

    /// Get the numerator.
    pub fn num(&self) -> u32 {
        self.num
    }

    /// Get the denominator.
    pub fn den(&self) -> u32 {
        self.den
    }
}

impl Debug for TimeBase {
    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        write!(f, "{}/{}", self.num(), self.den())
    }
}

/// A timestamp supporting various time bases. All comparisons are done within
/// microsecond time base.
#[derive(Copy, Clone)]
pub struct Timestamp {
    timestamp: i64,
    time_base: TimeBase,
}

impl Timestamp {
    /// Create a "null" timestamp (i.e. a timestamp set to the AV_NOPTS_VALUE).
    pub fn null() -> Self {
        unsafe {
            Self {
                timestamp: ffw_null_timestamp(),
                time_base: TimeBase::MICROSECONDS,
            }
        }
    }

    /// Create a new timestamp with a given time base.
    pub const fn new(timestamp: i64, time_base: TimeBase) -> Self {
        Self {
            timestamp,
            time_base,
        }
    }

    /// Create a new timestamp with 1/1 time base.
    pub const fn from_secs(timestamp: i64) -> Self {
        Self::new(timestamp, TimeBase::new(1, 1))
    }

    /// Create a new timestamp with 1/1_000 time base.
    pub const fn from_millis(timestamp: i64) -> Self {
        Self::new(timestamp, TimeBase::new(1, 1_000))
    }

    /// Create a new timestamp with 1/1_000_000 time base.
    pub const fn from_micros(timestamp: i64) -> Self {
        Self::new(timestamp, TimeBase::new(1, 1_000_000))
    }

    /// Create a new timestamp with 1/1_000_000_000 time base.
    pub const fn from_nanos(timestamp: i64) -> Self {
        Self::new(timestamp, TimeBase::new(1, 1_000_000_000))
    }

    /// Get the time base.
    pub fn time_base(&self) -> TimeBase {
        self.time_base
    }

    /// Get the raw timestamp value.
    pub fn timestamp(&self) -> i64 {
        self.timestamp
    }

    /// Set the timestamp with the current time base.
    pub fn with_raw_timestamp(mut self, timestamp: i64) -> Self {
        self.timestamp = timestamp;

        self
    }

    /// Check if this is the "null" timestamp (i.e. it is equal to the
    /// AV_NOPTS_VALUE).
    pub fn is_null(&self) -> bool {
        unsafe { self.timestamp == ffw_null_timestamp() }
    }

    /// Rescale the timestamp value to a given time base.
    pub fn with_time_base(&self, time_base: TimeBase) -> Self {
        let timestamp = if self.is_null() {
            self.timestamp
        } else {
            unsafe {
                ffw_rescale_q(
                    self.timestamp,
                    self.time_base.num,
                    self.time_base.den,
                    time_base.num,
                    time_base.den,
                )
            }
        };

        Self {
            timestamp,
            time_base,
        }
    }

    /// Get the timestamp value in seconds.
    pub fn as_secs(&self) -> Option<i64> {
        if self.is_null() {
            None
        } else {
            let ts = self.with_time_base(TimeBase::new(1, 1));

            Some(ts.timestamp)
        }
    }

    /// Get the timestamp value in milliseconds.
    pub fn as_millis(&self) -> Option<i64> {
        if self.is_null() {
            None
        } else {
            let ts = self.with_time_base(TimeBase::new(1, 1_000));

            Some(ts.timestamp)
        }
    }

    /// Get the timestamp value in microseconds.
    pub fn as_micros(&self) -> Option<i64> {
        if self.is_null() {
            None
        } else {
            let ts = self.with_time_base(TimeBase::new(1, 1_000_000));

            Some(ts.timestamp)
        }
    }

    /// Get the timestamp value in nanoseconds.
    pub fn as_nanos(&self) -> Option<i64> {
        if self.is_null() {
            None
        } else {
            let ts = self.with_time_base(TimeBase::new(1, 1_000_000_000));

            Some(ts.timestamp)
        }
    }

    /// Get the timestamp value as a floating point number with 32-bit
    /// precision.
    pub fn as_f32(&self) -> Option<f32> {
        if self.is_null() {
            None
        } else {
            Some(self.timestamp as f32 * self.time_base.num as f32 / self.time_base.den as f32)
        }
    }

    /// Get the timestamp value as a floating point number with 64-bit
    /// precision.
    pub fn as_f64(&self) -> Option<f64> {
        if self.is_null() {
            None
        } else {
            Some(self.timestamp as f64 * self.time_base.num as f64 / self.time_base.den as f64)
        }
    }
}

impl Debug for Timestamp {
    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        if let Some(millis) = self.as_millis() {
            write!(f, "{}.{:03}s", millis / 1_000, millis % 1_000)
        } else {
            write!(f, "(null)")
        }
    }
}

impl Add<Duration> for Timestamp {
    type Output = Timestamp;

    fn add(mut self, rhs: Duration) -> Self::Output {
        self += rhs;
        self
    }
}

impl AddAssign<Duration> for Timestamp {
    fn add_assign(&mut self, rhs: Duration) {
        // do not add anything to null timestamps
        if self.is_null() {
            return;
        }

        self.timestamp += Self::from_secs(rhs.as_secs() as i64)
            .with_time_base(self.time_base)
            .timestamp();

        self.timestamp += Self::from_nanos(rhs.subsec_nanos() as i64)
            .with_time_base(self.time_base)
            .timestamp();
    }
}

impl Sub<Duration> for Timestamp {
    type Output = Timestamp;

    fn sub(mut self, rhs: Duration) -> Self::Output {
        self -= rhs;
        self
    }
}

impl SubAssign<Duration> for Timestamp {
    fn sub_assign(&mut self, rhs: Duration) {
        // do not subtract anything from null timestamps
        if self.is_null() {
            return;
        }

        self.timestamp -= Self::from_secs(rhs.as_secs() as i64)
            .with_time_base(self.time_base)
            .timestamp();

        self.timestamp -= Self::from_nanos(rhs.subsec_nanos() as i64)
            .with_time_base(self.time_base)
            .timestamp();
    }
}

impl Sub for Timestamp {
    type Output = Duration;

    fn sub(mut self, rhs: Self) -> Self::Output {
        assert!(!self.is_null());
        assert!(!rhs.is_null());

        let rhs = rhs.with_time_base(self.time_base);

        self.timestamp -= rhs.timestamp;

        if self.timestamp < 0 {
            panic!("out of range");
        }

        let secs = self.with_time_base(TimeBase::new(1, 1));

        // calculate the remainder
        self.timestamp -= secs.with_time_base(self.time_base).timestamp();

        let nanos = self.as_nanos().unwrap();

        Duration::new(secs.timestamp as u64, nanos as u32)
    }
}

impl PartialEq for Timestamp {
    fn eq(&self, other: &Timestamp) -> bool {
        let a = self.as_micros();
        let b = other.as_micros();

        a == b
    }
}

impl Eq for Timestamp {}

impl PartialOrd for Timestamp {
    fn partial_cmp(&self, other: &Timestamp) -> Option<Ordering> {
        if let Some(a) = self.as_micros() {
            if let Some(b) = other.as_micros() {
                return a.partial_cmp(&b);
            }
        }

        None
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::{TimeBase, Timestamp};

    #[test]
    fn test_duration_add() {
        let mut ts = Timestamp::new(333, TimeBase::new(1, 90_000));

        ts += Duration::from_millis(100);

        assert_eq!(ts.timestamp, 9333);
    }

    #[test]
    fn test_duration_sub() {
        let mut ts = Timestamp::new(333, TimeBase::new(1, 90_000));

        ts -= Duration::from_millis(50);

        assert_eq!(ts.timestamp, -4167);
    }

    #[test]
    fn test_timestamp_sub() {
        let a = Timestamp::new(333, TimeBase::new(1, 90_000));
        let b = Timestamp::new(79, TimeBase::new(1, 50_000));

        let delta = a - b;

        assert_eq!(delta.as_secs(), 0);
        assert_eq!(delta.subsec_nanos(), 2_122_222);
    }

    #[test]
    fn test_comparisons() {
        let a = Timestamp::from_secs(1);
        let b = Timestamp::from_millis(1_000);

        assert_eq!(a, b);

        let a = Timestamp::from_secs(1);
        let b = Timestamp::from_millis(1_001);

        assert_ne!(a, b);
        assert!(a < b);

        let a = Timestamp::from_secs(1);
        let b = Timestamp::from_micros(1_000_001);

        assert_ne!(a, b);
        assert!(a < b);

        // this is outside of the comparison scale
        let a = Timestamp::from_secs(1);
        let b = Timestamp::from_nanos(1_000_000_001);

        assert_eq!(a, b);
    }
}