dvb-si 16.0.0

ETSI EN 300 468 DVB Service Information parser + builder. MPEG-2 PSI included.
Documentation
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Program Association Table — MPEG-2 ISO/IEC 13818-1 (H.222.0 08/2023) §2.4.4.4.
//!
//! PAT is carried on PID 0x0000 with table_id 0x00. It maps
//! program_number values to the PID on which their PMT is carried.
//! program_number 0x0000 is special — its PID is the NIT PID.

use crate::error::{Error, Result};
use alloc::vec::Vec;
use broadcast_common::len::{need_in, need_out};
use broadcast_common::reserved::ReservedOnes;
use broadcast_common::{Parse, Serialize};
use mpeg_ts::pid::Pid;

/// PAT table_id (ISO/IEC 13818-1 Table 2-30).
pub const TABLE_ID: u8 = 0x00;
/// PAT well-known PID.
pub const PID: mpeg_ts::pid::Pid = mpeg_ts::pid::Pid::from_const(0x0000);
/// program_number value that carries the NIT PID rather than a PMT PID.
pub const PROGRAM_NUMBER_NIT: u16 = 0x0000;

const MIN_HEADER_LEN: usize = 3;
const EXTENSION_HEADER_LEN: usize = 5;
use super::CRC_LEN;
const MIN_SECTION_LEN: usize = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + CRC_LEN;
const ENTRY_LEN: usize = 4;

/// One entry in the PAT program loop.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct PatEntry {
    /// program_number. 0x0000 means "next PID is the NIT PID".
    pub program_number: u16,
    /// PMT PID (or NIT PID when program_number == 0).
    pub pid: Pid,
    /// `reserved(3)` before the PID (ISO/IEC 13818-1 §2.4.4.4, `111`);
    /// preserved from the wire.
    #[cfg_attr(feature = "serde", serde(skip))]
    pub reserved: ReservedOnes<3>,
}

impl PatEntry {
    /// Construct an entry with the spec `reserved` fill (`111`).
    #[must_use]
    pub const fn new(program_number: u16, pid: Pid) -> Self {
        Self {
            program_number,
            pid,
            reserved: ReservedOnes::FILL,
        }
    }
}

/// Program Association Table.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct PatSection {
    /// transport_stream_id from the section header.
    pub transport_stream_id: u16,
    /// 5-bit version_number from the section header.
    pub version_number: u8,
    /// current_next_indicator bit.
    pub current_next_indicator: bool,
    /// section_number in the sub-table sequence.
    pub section_number: u8,
    /// last_section_number in the sub-table sequence.
    pub last_section_number: u8,
    /// Program entries in wire order.
    pub entries: Vec<PatEntry>,
    /// Header bit fields (`'0'` after `section_syntax_indicator`, the two
    /// `reserved(2)`; default [`SectionBits::psi`](super::SectionBits::psi)).
    #[cfg_attr(feature = "serde", serde(skip))]
    pub bits: super::SectionBits,
}

impl Default for PatSection {
    fn default() -> Self {
        Self::new(0, 0, false, 0, 0, Vec::new())
    }
}

impl PatSection {
    /// Construct a PAT section with the spec `reserved` fill.
    #[must_use]
    pub fn new(
        transport_stream_id: u16,
        version_number: u8,
        current_next_indicator: bool,
        section_number: u8,
        last_section_number: u8,
        entries: Vec<PatEntry>,
    ) -> Self {
        Self {
            transport_stream_id,
            version_number,
            current_next_indicator,
            section_number,
            last_section_number,
            entries,
            bits: super::SectionBits::psi(),
        }
    }
}

impl<'a> Parse<'a> for PatSection {
    type Error = crate::error::Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        need_in(
            bytes,
            MIN_HEADER_LEN + EXTENSION_HEADER_LEN + CRC_LEN,
            "PatSection",
        )?;

        if bytes[0] != TABLE_ID {
            return Err(Error::UnexpectedTableId {
                table_id: bytes[0],
                what: "PatSection",
                expected: &[TABLE_ID],
            });
        }

        let section_length = super::section_length_of(bytes);
        let total = super::check_section_length(
            bytes.len(),
            MIN_HEADER_LEN,
            section_length,
            MIN_SECTION_LEN,
        )?;

        let transport_stream_id = crate::wire::be_u16(bytes, 3, "pat")?;
        let version_number = super::version_number_of(bytes[5]);
        let current_next_indicator = super::current_next_of(bytes[5]);
        let section_number = bytes[6];
        let last_section_number = bytes[7];

        let end = total - CRC_LEN;
        let mut entries = Vec::new();
        let mut pos = 8;
        while pos < end {
            if pos + ENTRY_LEN > end {
                return Err(Error::BufferTooShort(
                    broadcast_common::error::BufferTooShort {
                        need: ENTRY_LEN,
                        have: end - pos,
                        what: "PatSection trailing entry bytes",
                    },
                ));
            }
            let chunk = &bytes[pos..pos + ENTRY_LEN];
            let program_number = crate::wire::be_u16(chunk, 0, "pat")?;
            let pid = super::pid_of(chunk[2], chunk[3]);
            entries.push(PatEntry {
                program_number,
                pid,
                reserved: ReservedOnes::from_bits(u64::from(super::pid_reserved_of(chunk[2]))),
            });
            pos += ENTRY_LEN;
        }

        Ok(PatSection {
            transport_stream_id,
            version_number,
            current_next_indicator,
            section_number,
            last_section_number,
            entries,
            bits: super::SectionBits::parse(bytes[1], Some(bytes[5])),
        })
    }
}

impl Serialize for PatSection {
    type Error = crate::error::Error;
    fn serialized_len(&self) -> usize {
        MIN_HEADER_LEN + EXTENSION_HEADER_LEN + self.entries.len() * ENTRY_LEN + CRC_LEN
    }

    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let len = self.serialized_len();
        need_out(buf, len)?;

        let section_length = EXTENSION_HEADER_LEN + self.entries.len() * ENTRY_LEN + CRC_LEN;

        buf[0] = TABLE_ID;
        buf[1] = self.bits.b1()?;
        super::write_section_length(buf, section_length)?;
        buf[3..5].copy_from_slice(&self.transport_stream_id.to_be_bytes());
        buf[5] = self
            .bits
            .b5(self.version_number, self.current_next_indicator)?;
        buf[6] = self.section_number;
        buf[7] = self.last_section_number;

        let mut pos = 8;
        for entry in &self.entries {
            buf[pos..pos + 2].copy_from_slice(&entry.program_number.to_be_bytes());
            super::write_pid_r(
                &mut buf[pos + 2..pos + 4],
                entry.pid,
                entry.reserved.get() as u8,
            )?;
            pos += ENTRY_LEN;
        }

        let crc_pos = len - CRC_LEN;
        super::write_crc(buf, crc_pos);

        Ok(len)
    }
}
impl crate::traits::TableDef<'_> for PatSection {
    const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID, TABLE_ID)];
    const NAME: &'static str = "PROGRAM_ASSOCIATION";
}

impl PatSection {
    /// Program entries excluding the NIT entry.
    pub fn programmes(&self) -> impl Iterator<Item = &PatEntry> {
        self.entries
            .iter()
            .filter(|e| e.program_number != PROGRAM_NUMBER_NIT)
    }

    /// The NIT PID: the entry with `program_number == 0` (ISO/IEC 13818-1
    /// Table 2-30 `network_PID`), if the PAT carries one.
    pub fn nit_pid(&self) -> Option<Pid> {
        self.entries
            .iter()
            .find(|e| e.program_number == PROGRAM_NUMBER_NIT)
            .map(|e| e.pid)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::vec;

    /// Build a PAT section with the given entries and a placeholder CRC.
    fn build_pat(tsid: u16, version: u8, entries: &[(u16, u16)]) -> Vec<u8> {
        let section_length: usize = EXTENSION_HEADER_LEN + entries.len() * ENTRY_LEN + CRC_LEN;
        let mut v = Vec::new();
        v.push(TABLE_ID);
        crate::tables::push_section_header(
            &mut v,
            crate::tables::SECTION_B1_FLAGS_PSI,
            section_length,
        );
        v.extend_from_slice(&tsid.to_be_bytes());
        v.push(crate::tables::version_byte(version, true));
        v.push(0x00); // section_number
        v.push(0x00); // last_section_number
        for &(pn, pid) in entries {
            v.extend_from_slice(&pn.to_be_bytes());
            let mut pid_bytes = [0u8; 2];
            crate::tables::write_pid(&mut pid_bytes, Pid::from_wire_masked(pid));
            v.extend_from_slice(&pid_bytes);
        }
        v.extend_from_slice(&[0, 0, 0, 0]); // placeholder CRC
        v
    }

    #[test]
    fn parse_empty_pat_zero_programs() {
        let bytes = build_pat(0x1234, 5, &[]);
        let pat = PatSection::parse(&bytes).expect("parse");
        assert_eq!(pat.transport_stream_id, 0x1234);
        assert_eq!(pat.version_number, 5);
        assert!(pat.current_next_indicator);
        assert_eq!(pat.section_number, 0);
        assert_eq!(pat.last_section_number, 0);
        assert_eq!(pat.entries.len(), 0);
    }

    #[test]
    fn parse_single_program_extracts_pmt_pid() {
        let bytes = build_pat(1, 0, &[(42, 0x1234)]);
        let pat = PatSection::parse(&bytes).unwrap();
        assert_eq!(pat.entries.len(), 1);
        assert_eq!(pat.entries[0].program_number, 42);
        assert_eq!(pat.entries[0].pid, Pid::from_const(0x1234));
    }

    #[test]
    fn parse_many_programs_preserves_order() {
        let entries: Vec<(u16, u16)> = (1..=10).map(|i| (i, 0x1000 + i)).collect();
        let bytes = build_pat(1, 0, &entries);
        let pat = PatSection::parse(&bytes).unwrap();
        assert_eq!(pat.entries.len(), 10);
        for (i, e) in pat.entries.iter().enumerate() {
            assert_eq!(e.program_number, (i + 1) as u16);
            assert_eq!(
                e.pid,
                Pid::try_new(0x1000 + (i + 1) as u16).expect("pid fits the 13-bit field")
            );
        }
    }

    #[test]
    fn parse_rejects_wrong_table_id() {
        let mut bytes = build_pat(1, 0, &[]);
        bytes[0] = 0x02; // PMT table_id
        let err = PatSection::parse(&bytes).unwrap_err();
        assert!(matches!(
            err,
            Error::UnexpectedTableId { table_id: 0x02, .. }
        ));
    }

    #[test]
    fn parse_rejects_short_buffer() {
        let err = PatSection::parse(&[0x00, 0x00]).unwrap_err();
        assert!(matches!(err, Error::BufferTooShort(..)));
    }

    #[test]
    fn serialize_round_trip_empty() {
        let pat = PatSection::new(0x0001, 0, true, 0, 0, vec![]);
        let mut buf = vec![0u8; pat.serialized_len()];
        pat.serialize_into(&mut buf).expect("serialize");
        let reparsed = PatSection::parse(&buf).expect("reparse");
        assert_eq!(pat, reparsed);
    }

    #[test]
    fn serialize_round_trip_many_programs() {
        let entries: Vec<PatEntry> = (1..=5)
            .map(|i| {
                PatEntry::new(
                    i,
                    Pid::try_new(0x1000 + i).expect("pid fits the 13-bit field"),
                )
            })
            .collect();
        let pat = PatSection::new(0xABCD, 3, true, 0, 0, entries);
        let mut buf = vec![0u8; pat.serialized_len()];
        pat.serialize_into(&mut buf).unwrap();
        let reparsed = PatSection::parse(&buf).unwrap();
        assert_eq!(pat, reparsed);
    }

    /// section_length is 12 bits (max 4095). 1021 entries is the largest
    /// program loop that still fits (section_length 4093); 1022 pushes it to
    /// 4097 and must be rejected, not silently wrapped.
    #[test]
    fn serialize_rejects_program_loop_exceeding_section_length() {
        let entries: Vec<PatEntry> = (0..1022u32)
            .map(|i| PatEntry::new((i % u16::MAX as u32) as u16, Pid::from_const(0x0100)))
            .collect();
        let pat = PatSection::new(1, 0, true, 0, 0, entries);
        let mut buf = vec![0u8; pat.serialized_len()];
        assert!(matches!(
            pat.serialize_into(&mut buf).unwrap_err(),
            Error::FieldOverflow(_)
        ));
    }

    #[test]
    fn serialize_accepts_program_loop_at_section_length_boundary() {
        let entries: Vec<PatEntry> = (0..1021u32)
            .map(|i| PatEntry::new((i % u16::MAX as u32) as u16, Pid::from_const(0x0100)))
            .collect();
        let pat = PatSection::new(1, 0, true, 0, 0, entries);
        let mut buf = vec![0u8; pat.serialized_len()];
        pat.serialize_into(&mut buf).expect("boundary fits");
        let reparsed = PatSection::parse(&buf).expect("reparse");
        assert_eq!(pat, reparsed);
    }

    #[test]
    fn parse_rejects_zero_section_length() {
        let mut buf = vec![0u8; 64];
        buf[0] = TABLE_ID;
        buf[1] = 0xB0;
        buf[2] = 0x00;
        for b in &mut buf[3..] {
            *b = 0xFF;
        }
        assert!(matches!(
            PatSection::parse(&buf).unwrap_err(),
            Error::SectionLengthOverflow { .. }
        ));
    }

    #[test]
    fn network_pid_entry_identified_by_program_number_0() {
        let bytes = build_pat(1, 0, &[(0, 0x0010), (1, 0x0100)]);
        let pat = PatSection::parse(&bytes).unwrap();
        assert_eq!(pat.nit_pid(), Some(Pid::from_const(0x0010)));
        assert_eq!(pat.programmes().count(), 1);
        assert_eq!(pat.programmes().next().unwrap().program_number, 1);
    }

    #[test]
    fn parse_rejects_trailing_slack_bytes() {
        let mut bytes = build_pat(1, 0, &[(1, 0x100)]);
        let sl = (bytes.len() - MIN_HEADER_LEN) as u16;
        bytes[1] = (bytes[1] & 0xF0) | (((sl + 2) >> 8) as u8 & 0x0F);
        bytes[2] = ((sl + 2) & 0xFF) as u8;
        bytes.extend_from_slice(&[0xFF, 0xFF]);
        let crc_pos = bytes.len();
        let crc = broadcast_common::crc32_mpeg2::compute(&bytes[..crc_pos - CRC_LEN]);
        bytes[crc_pos - CRC_LEN..crc_pos].copy_from_slice(&crc.to_be_bytes());
        assert!(matches!(
            PatSection::parse(&bytes).unwrap_err(),
            Error::BufferTooShort(broadcast_common::error::BufferTooShort {
                what: "PatSection trailing entry bytes",
                ..
            })
        ));
    }

    /// The field is 3 bits wide, so a wider value is unrepresentable and cannot
    /// spill into the 13-bit PID below it.
    #[test]
    fn reserved_cannot_hold_more_than_three_bits() {
        let e = PatEntry {
            program_number: 1,
            pid: Pid::from_const(0x01FF),
            reserved: ReservedOnes::from_bits(0xFF),
        };
        assert_eq!(e.reserved.get(), 0x07);
        let section = PatSection::new(1, 0, true, 0, 0, vec![e]);
        let mut b = vec![0u8; section.serialized_len()];
        section.serialize_into(&mut b).unwrap();
        assert_eq!(b[8..12], [0x00, 0x01, 0xE1, 0xFF]);
    }
}