Skip to main content

mpeg_pes/
timestamp.rs

1//! PTS / DTS — 33-bit presentation / decoding timestamps at 90 kHz
2//! (ISO/IEC 13818-1 §2.4.3.7). Encoded across 5 bytes with a 4-bit prefix and
3//! three interleaved `marker_bit`s.
4
5use crate::error::{Error, Result};
6
7/// 33-bit value mask.
8const TS_MASK: u64 = (1 << 33) - 1;
9
10/// Presentation Time Stamp (33-bit, 90 kHz units).
11#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize))]
13pub struct Pts(pub u64);
14
15/// Decoding Time Stamp (33-bit, 90 kHz units).
16#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize))]
18pub struct Dts(pub u64);
19
20impl Pts {
21    /// Value in 90 kHz units (0..=2³³−1).
22    #[must_use]
23    pub const fn ticks(self) -> u64 {
24        self.0
25    }
26    /// Value in seconds.
27    #[must_use]
28    pub fn seconds(self) -> f64 {
29        self.0 as f64 / 90_000.0
30    }
31    /// Encode as a standalone 5-byte PTS field (prefix `0010`, for `PTS_DTS_flags = 10`).
32    #[must_use]
33    pub fn to_field_bytes(self) -> [u8; 5] {
34        write(self.0, 0b0010)
35    }
36}
37
38impl Dts {
39    /// Value in 90 kHz units (0..=2³³−1).
40    #[must_use]
41    pub const fn ticks(self) -> u64 {
42        self.0
43    }
44    /// Value in seconds.
45    #[must_use]
46    pub fn seconds(self) -> f64 {
47        self.0 as f64 / 90_000.0
48    }
49    /// Encode as a 5-byte DTS field (prefix `0001`, the DTS half of a PTS+DTS pair).
50    #[must_use]
51    pub fn to_field_bytes(self) -> [u8; 5] {
52        write(self.0, 0b0001)
53    }
54}
55
56/// Decode a 5-byte PTS/DTS field. `prefix` is the expected leading 4-bit value
57/// (`0b0010` PTS-only, `0b0011` PTS in a PTS+DTS pair, `0b0001` DTS). The three
58/// `marker_bit`s must be `1`.
59pub(crate) fn read(b: &[u8], prefix: u8, what: &'static str) -> Result<u64> {
60    if b.len() < 5 {
61        return Err(Error::BufferTooShort {
62            need: 5,
63            have: b.len(),
64            what,
65        });
66    }
67    if (b[0] >> 4) != prefix {
68        return Err(Error::BadTimestampPrefix(what));
69    }
70    if b[0] & 0x01 == 0 || b[2] & 0x01 == 0 || b[4] & 0x01 == 0 {
71        return Err(Error::BadTimestampMarker(what));
72    }
73    let hi = u64::from((b[0] >> 1) & 0x07); // [32:30]
74    let mid = (u64::from(b[1]) << 7) | u64::from(b[2] >> 1); // [29:15]
75    let lo = (u64::from(b[3]) << 7) | u64::from(b[4] >> 1); // [14:0]
76    Ok((hi << 30) | (mid << 15) | lo)
77}
78
79/// Encode a 33-bit value into a 5-byte PTS/DTS field with the given 4-bit prefix.
80pub(crate) fn write(ts: u64, prefix: u8) -> [u8; 5] {
81    let ts = ts & TS_MASK;
82    [
83        (prefix << 4) | ((((ts >> 30) & 0x07) as u8) << 1) | 0x01,
84        ((ts >> 22) & 0xFF) as u8,
85        ((((ts >> 15) & 0x7F) as u8) << 1) | 0x01,
86        ((ts >> 7) & 0xFF) as u8,
87        (((ts & 0x7F) as u8) << 1) | 0x01,
88    ]
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    #[test]
96    fn pts_round_trip_boundary_values() {
97        for ts in [0u64, 1, 90_000, 0x1_2345_6789, TS_MASK] {
98            let enc = write(ts, 0b0010);
99            assert_eq!(read(&enc, 0b0010, "pts").unwrap(), ts, "ts={ts:#x}");
100        }
101    }
102
103    #[test]
104    fn rejects_bad_prefix() {
105        let enc = write(0, 0b0011);
106        assert!(matches!(
107            read(&enc, 0b0010, "pts"),
108            Err(Error::BadTimestampPrefix(_))
109        ));
110    }
111
112    #[test]
113    fn rejects_bad_marker() {
114        let mut enc = write(0, 0b0010);
115        enc[2] &= 0xFE; // clear a marker bit
116        assert!(matches!(
117            read(&enc, 0b0010, "pts"),
118            Err(Error::BadTimestampMarker(_))
119        ));
120    }
121
122    #[test]
123    fn seconds() {
124        assert!((Pts(90_000).seconds() - 1.0).abs() < 1e-9);
125    }
126}