Skip to main content

mpeg_ts/
pid.rs

1//! MPEG-TS Packet Identifier (PID) types and well-known PID constants.
2//!
3//! Specified by ITU-T H.222.0 §2.4 (= ISO/IEC 13818-1). DVB-specific
4//! well-known PID assignments are also listed in ETSI EN 300 468 §5.1.3 Table 1.
5
6/// A 13-bit MPEG-TS Packet Identifier.
7///
8/// Thin newtype over the wire `u16`. Values are masked to 13 bits on
9/// construction (`0x0000..=0x1FFF`), matching the transport header field
10/// width (ISO/IEC 13818-1 §2.4.3.2).
11#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize))]
13#[cfg_attr(feature = "serde", serde(transparent))]
14pub struct Pid(u16);
15
16impl Pid {
17    /// Mask covering the 13 valid PID bits.
18    pub const MASK: u16 = 0x1FFF;
19
20    /// Construct a `Pid`, masking to the low 13 bits.
21    #[must_use]
22    pub const fn new(value: u16) -> Self {
23        Self(value & Self::MASK)
24    }
25
26    /// The raw 13-bit value.
27    #[must_use]
28    pub const fn value(self) -> u16 {
29        self.0
30    }
31}
32
33impl From<u16> for Pid {
34    fn from(value: u16) -> Self {
35        Self::new(value)
36    }
37}
38
39impl From<Pid> for u16 {
40    fn from(pid: Pid) -> Self {
41        pid.0
42    }
43}
44
45impl core::fmt::Debug for Pid {
46    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
47        write!(f, "Pid(0x{:04X})", self.0)
48    }
49}
50
51impl core::fmt::Display for Pid {
52    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
53        write!(f, "0x{:04X}", self.0)
54    }
55}
56
57/// Well-known PIDs. The transport stream MUST carry the corresponding tables
58/// on these PIDs.
59///
60/// **API 2.0 type change:** constants are now [`crate::pid::Pid`] instead of `u16`.
61/// Call `.value()` to obtain the raw `u16`, or use `Pid::from(u16)` /
62/// `u16::from(Pid)` for conversions.
63pub mod well_known {
64    use super::Pid;
65
66    /// Program Association Table (MPEG-2).
67    pub const PAT: Pid = Pid::new(0x0000);
68    /// Conditional Access Table (MPEG-2).
69    pub const CAT: Pid = Pid::new(0x0001);
70    /// Transport Stream Description Table (MPEG-2).
71    pub const TSDT: Pid = Pid::new(0x0002);
72    /// IPMP Control Information Table (MPEG-2).
73    pub const IPMP_CIT: Pid = Pid::new(0x0003);
74    /// Network Information Table (DVB).
75    pub const NIT: Pid = Pid::new(0x0010);
76    /// Service Description Table + Bouquet Association Table (DVB).
77    pub const SDT_BAT: Pid = Pid::new(0x0011);
78    /// Event Information Table (DVB).
79    pub const EIT: Pid = Pid::new(0x0012);
80    /// Running Status Table (DVB).
81    pub const RST: Pid = Pid::new(0x0013);
82    /// Time and Date + Time Offset + Stuffing (DVB).
83    pub const TDT_TOT: Pid = Pid::new(0x0014);
84    /// Network synchronisation.
85    pub const NETWORK_SYNC: Pid = Pid::new(0x0015);
86    /// Resolution Notification Table.
87    pub const RNT: Pid = Pid::new(0x0016);
88    /// Satellite Access Table (EN 300 468 Table 1).
89    pub const SAT: Pid = Pid::new(0x001B);
90    /// Link-local inband signalling (reserved).
91    pub const INBAND_SIGNALLING: Pid = Pid::new(0x001C);
92    /// Measurement (reserved).
93    pub const MEASUREMENT: Pid = Pid::new(0x001D);
94    /// Discontinuity Information Table.
95    pub const DIT: Pid = Pid::new(0x001E);
96    /// Selection Information Table.
97    pub const SIT: Pid = Pid::new(0x001F);
98
99    /// ATSC PSIP base PID.
100    pub const ATSC_PSIP: Pid = Pid::new(0x1FFB);
101
102    /// Null-packet padding PID. Payload is ignored.
103    pub const NULL: Pid = Pid::new(0x1FFF);
104}