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`, always in `0x0000..=0x1FFF` (the
9/// transport header field width, ISO/IEC 13818-1 §2.4.3.2). Construction never
10/// masks a caller-supplied value: [`Pid::try_new`] rejects an out-of-range
11/// one, [`Pid::from_wire_masked`] is the explicit parse-site constructor, and
12/// [`Pid::from_const`] is the compile-time constructor for constants.
13#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14#[cfg_attr(feature = "serde", derive(serde::Serialize))]
15#[cfg_attr(feature = "serde", serde(transparent))]
16pub struct Pid(u16);
17
18impl Pid {
19 /// Mask covering the 13 valid PID bits.
20 pub const MASK: u16 = 0x1FFF;
21
22 /// Construct a constant `Pid`, **failing compilation** (a `const` panic)
23 /// when `value` exceeds [`Pid::MASK`].
24 ///
25 /// This is the constructor for well-known PIDs and table constants: a
26 /// mistyped literal is a build error, never a silently masked different
27 /// PID. Runtime values use [`Pid::try_new`].
28 ///
29 /// ```
30 /// use mpeg_ts::pid::Pid;
31 /// const PMT: Pid = Pid::from_const(0x0100);
32 /// assert_eq!(PMT.value(), 0x0100);
33 /// ```
34 #[must_use]
35 pub const fn from_const(value: u16) -> Self {
36 assert!(
37 value <= Self::MASK,
38 "PID constant exceeds the 13-bit range 0x0000..=0x1FFF"
39 );
40 Self(value)
41 }
42
43 /// Construct a `Pid` from the low 13 bits of `value`, **deliberately**
44 /// discarding the rest: the explicit name for extracting the 13-bit PID
45 /// field out of a wider wire word (e.g. `reserved(3)` + `PID(13)`), where
46 /// masking is the specified behaviour. Use this only at parse sites, so a
47 /// caller-supplied value is never masked by accident; use
48 /// [`Pid::try_new`] for those and [`Pid::from_const`] for constants.
49 ///
50 /// ```
51 /// use mpeg_ts::pid::Pid;
52 /// // reserved(3) = 0b111 followed by PID 0x0100.
53 /// assert_eq!(Pid::from_wire_masked(0xE100).value(), 0x0100);
54 /// assert_eq!(Pid::try_new(0xE100).unwrap_err().value, 0xE100);
55 /// ```
56 #[must_use]
57 pub const fn from_wire_masked(value: u16) -> Self {
58 Self(value & Self::MASK)
59 }
60
61 /// Construct a `Pid`, rejecting any value above [`Pid::MASK`] (`0x1FFF`).
62 ///
63 /// Use this at API boundaries that take a caller-supplied value.
64 ///
65 /// # Errors
66 /// [`PidOutOfRange`] when `value > 0x1FFF`.
67 pub const fn try_new(value: u16) -> Result<Self, PidOutOfRange> {
68 if value > Self::MASK {
69 Err(PidOutOfRange { value })
70 } else {
71 Ok(Self(value))
72 }
73 }
74
75 /// The raw 13-bit value.
76 #[must_use]
77 pub const fn value(self) -> u16 {
78 self.0
79 }
80}
81
82/// A value that does not fit the 13-bit PID field (ISO/IEC 13818-1 §2.4.3.2).
83#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
84#[error("PID 0x{value:04X} exceeds the 13-bit range 0x0000..=0x1FFF")]
85#[non_exhaustive]
86pub struct PidOutOfRange {
87 /// The rejected value.
88 pub value: u16,
89}
90
91impl TryFrom<u16> for Pid {
92 type Error = PidOutOfRange;
93 fn try_from(value: u16) -> Result<Self, Self::Error> {
94 Self::try_new(value)
95 }
96}
97
98impl From<Pid> for u16 {
99 fn from(pid: Pid) -> Self {
100 pid.0
101 }
102}
103
104impl core::fmt::Debug for Pid {
105 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
106 write!(f, "Pid(0x{:04X})", self.0)
107 }
108}
109
110impl core::fmt::UpperHex for Pid {
111 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
112 core::fmt::UpperHex::fmt(&self.0, f)
113 }
114}
115
116impl core::fmt::LowerHex for Pid {
117 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
118 core::fmt::LowerHex::fmt(&self.0, f)
119 }
120}
121
122impl core::fmt::Display for Pid {
123 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
124 write!(f, "0x{:04X}", self.0)
125 }
126}
127
128/// Well-known PIDs. The transport stream MUST carry the corresponding tables
129/// on these PIDs.
130///
131/// Each constant is a [`crate::pid::Pid`]; call `.value()` to obtain the raw
132/// `u16`, or use `Pid::try_from(u16)` / `u16::from(Pid)` for conversions.
133pub mod well_known {
134 use super::Pid;
135
136 /// Program Association Table (MPEG-2).
137 pub const PAT: Pid = Pid::from_const(0x0000);
138 /// Conditional Access Table (MPEG-2).
139 pub const CAT: Pid = Pid::from_const(0x0001);
140 /// Transport Stream Description Table (MPEG-2).
141 pub const TSDT: Pid = Pid::from_const(0x0002);
142 /// IPMP Control Information Table (MPEG-2).
143 pub const IPMP_CIT: Pid = Pid::from_const(0x0003);
144 /// Network Information Table (DVB).
145 pub const NIT: Pid = Pid::from_const(0x0010);
146 /// Service Description Table + Bouquet Association Table (DVB).
147 pub const SDT_BAT: Pid = Pid::from_const(0x0011);
148 /// Event Information Table (DVB).
149 pub const EIT: Pid = Pid::from_const(0x0012);
150 /// Running Status Table (DVB).
151 pub const RST: Pid = Pid::from_const(0x0013);
152 /// Time and Date + Time Offset + Stuffing (DVB).
153 pub const TDT_TOT: Pid = Pid::from_const(0x0014);
154 /// Network synchronisation.
155 pub const NETWORK_SYNC: Pid = Pid::from_const(0x0015);
156 /// Resolution Notification Table.
157 pub const RNT: Pid = Pid::from_const(0x0016);
158 /// Satellite Access Table (EN 300 468 Table 1).
159 pub const SAT: Pid = Pid::from_const(0x001B);
160 /// Link-local inband signalling (EN 300 468 Table 1); only for use between
161 /// devices in a controlled environment, never on a broadcast signal.
162 pub const INBAND_SIGNALLING: Pid = Pid::from_const(0x001C);
163 /// Measurement (reserved).
164 pub const MEASUREMENT: Pid = Pid::from_const(0x001D);
165 /// Discontinuity Information Table.
166 pub const DIT: Pid = Pid::from_const(0x001E);
167 /// Selection Information Table.
168 pub const SIT: Pid = Pid::from_const(0x001F);
169
170 /// ATSC PSIP base PID.
171 pub const ATSC_PSIP: Pid = Pid::from_const(0x1FFB);
172
173 /// Null-packet padding PID. Payload is ignored.
174 pub const NULL: Pid = Pid::from_const(0x1FFF);
175}
176
177#[cfg(test)]
178mod tests {
179 use super::*;
180
181 #[test]
182 fn from_const_is_the_compile_time_constructor() {
183 const PMT: Pid = Pid::from_const(0x0100);
184 assert_eq!(PMT.value(), 0x0100);
185 assert_eq!(Pid::from_const(0).value(), 0);
186 assert_eq!(Pid::from_const(Pid::MASK).value(), 0x1FFF);
187 }
188
189 #[test]
190 fn try_new_and_from_wire_masked_differ_only_in_policy() {
191 assert_eq!(Pid::try_new(0x0000), Ok(well_known::PAT));
192 assert_eq!(Pid::try_new(Pid::MASK), Ok(well_known::NULL));
193 assert!(Pid::try_new(0x2000).is_err());
194 assert_eq!(Pid::from_wire_masked(0xE100).value(), 0x0100);
195 }
196
197 #[test]
198 fn inband_signalling_sits_between_sat_and_measurement() {
199 assert_eq!(well_known::INBAND_SIGNALLING.value(), 0x001C);
200 assert_eq!(
201 well_known::INBAND_SIGNALLING.value(),
202 well_known::SAT.value() + 1
203 );
204 assert_eq!(
205 well_known::MEASUREMENT.value(),
206 well_known::INBAND_SIGNALLING.value() + 1
207 );
208 }
209}