Skip to main content

dvb_si/
ts.rs

1//! MPEG-TS packet parser + section reassembler. Feature-gated under `ts`.
2
3use crate::error::{Error, Result};
4
5/// Size of one MPEG-TS packet (ETSI EN 300 468 §3.2, ISO/IEC 13818-1 §2.4.3.2).
6pub const TS_PACKET_SIZE: usize = 188;
7/// Sync byte that every TS packet starts with (ISO/IEC 13818-1 §2.4.3.2).
8pub const TS_SYNC_BYTE: u8 = 0x47;
9/// Upper bound on a single section: `section_length` is 12 bits (max 4095)
10/// plus the 3-byte header = 4098. (Long-form SI caps `section_length` at
11/// 4093 → total 4096, but maximal short-form private sections may reach
12/// 4098; the reassembler accepts the absolute ceiling.)
13const MAX_SECTION_SIZE: usize = 4098;
14
15/// ETSI EN 300 468 §3.2.3: transport header byte 1 bits 7 = tei (Transport Error Indicator).
16const TEI_MASK: u8 = 0x80;
17/// ETSI EN 300 468 §3.2.3: byte 1 bits 6 = pusi (Payload Unit Start Indicator).
18const PUSI_MASK: u8 = 0x40;
19/// ETSI EN 300 468 §3.2.3: byte 1 bits 5..=1 = 13-bit PID (upper 5 bits).
20pub const PID_MASK_HI: u8 = 0x1F;
21/// ETSI EN 300 468 §3.2.3: byte 3 bits 7..=6 = 2-bit scrambling control.
22pub const SCRAMBLING_MASK: u8 = 0xC0;
23/// ETSI EN 300 468 §3.2.3: byte 3 bit 4 = adaptation_field_control (bit 4 = 1 means adaptation present).
24pub const ADAPTATION_FLAG: u8 = 0x20;
25/// ETSI EN 300 468 §3.2.3: byte 3 bit 3 = adaptation_field_control (bit 3 = 1 means payload present).
26pub const PAYLOAD_FLAG: u8 = 0x10;
27/// ETSI EN 300 468 §3.2.3: byte 3 bits 3..=0 = 4-bit continuity_counter.
28pub const CC_MASK: u8 = 0x0F;
29
30/// Parsed TS header — the 4-byte transport header fields.
31#[derive(Clone, Debug, PartialEq, Eq)]
32#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
33pub struct TsHeader {
34    /// Transport Error Indicator — set by the demodulator when an
35    /// uncorrectable error is present in the packet.
36    pub tei: bool,
37    /// Payload Unit Start Indicator — first byte of the payload is a new
38    /// PES packet or PSI section header when set.
39    pub pusi: bool,
40    /// 13-bit Packet Identifier.
41    pub pid: u16,
42    /// 2-bit transport_scrambling_control (0 = not scrambled).
43    pub scrambling: u8,
44    /// Adaptation field present flag (adaptation_field_control bit 1).
45    pub has_adaptation: bool,
46    /// Payload present flag (adaptation_field_control bit 0).
47    pub has_payload: bool,
48    /// 4-bit continuity_counter (wraps 0..=15 per PID).
49    pub continuity_counter: u8,
50}
51
52/// Borrowed view into one 188-byte TS packet.
53///
54/// Serde: Serialize-only. Deserialize is omitted because `raw` is a
55/// reference to fixed-length 188-byte storage that cannot be reconstructed
56/// from a deserializer's lifetime budget; the field is also redundant once
57/// the header has been parsed. `raw` is excluded from the serialized form.
58#[derive(Clone, Debug)]
59#[cfg_attr(feature = "serde", derive(serde::Serialize))]
60pub struct TsPacket<'a> {
61    /// Parsed header fields.
62    pub header: TsHeader,
63    /// Slice into the packet's payload, or `None` when `has_payload == false`
64    /// or the adaptation field consumed the whole packet body.
65    pub payload: Option<&'a [u8]>,
66    /// The raw 188 bytes of the packet — kept for cheap forwarding.
67    #[cfg_attr(feature = "serde", serde(skip))]
68    pub raw: &'a [u8; TS_PACKET_SIZE],
69}
70
71impl TsHeader {
72    /// Parse a 4-byte TS transport header.
73    ///
74    /// Returns `None` if `raw4` is shorter than 4 bytes.
75    pub fn parse(raw4: &[u8]) -> Option<Self> {
76        if raw4.len() < 4 {
77            return None;
78        }
79        let b1 = raw4[1];
80        let b2 = raw4[2];
81        let b3 = raw4[3];
82
83        let tei = (b1 & TEI_MASK) != 0;
84        let pusi = (b1 & PUSI_MASK) != 0;
85        let pid = (((b1 & PID_MASK_HI) as u16) << 8) | (b2 as u16);
86        let scrambling = (b3 & SCRAMBLING_MASK) >> 6;
87        let has_adaptation = (b3 & ADAPTATION_FLAG) != 0;
88        let has_payload = (b3 & PAYLOAD_FLAG) != 0;
89        let continuity_counter = b3 & CC_MASK;
90
91        Some(Self {
92            tei,
93            pusi,
94            pid,
95            scrambling,
96            has_adaptation,
97            has_payload,
98            continuity_counter,
99        })
100    }
101
102    /// Serialize this header into the first 4 bytes of `buf`.
103    ///
104    /// Panics if `buf` is shorter than 4 bytes.
105    pub fn serialize_into(&self, buf: &mut [u8]) {
106        assert!(buf.len() >= 4, "buffer must have at least 4 bytes for TS header");
107        buf[0] = TS_SYNC_BYTE;
108        buf[1] = 0;
109        if self.tei {
110            buf[1] |= TEI_MASK;
111        }
112        if self.pusi {
113            buf[1] |= PUSI_MASK;
114        }
115        buf[1] |= ((self.pid >> 8) as u8) & PID_MASK_HI;
116        buf[2] = (self.pid & 0xFF) as u8;
117        buf[3] = (self.scrambling << 6) & SCRAMBLING_MASK;
118        if self.has_adaptation {
119            buf[3] |= ADAPTATION_FLAG;
120        }
121        if self.has_payload {
122            buf[3] |= PAYLOAD_FLAG;
123        }
124        buf[3] |= self.continuity_counter & CC_MASK;
125    }
126}
127
128impl<'a> TsPacket<'a> {
129    /// Parse a single 188-byte TS packet from a buffer.
130    ///
131    /// Returns `Err(Error::InvalidSyncByte)` if the first byte is not `0x47`,
132    /// `Err(Error::BufferTooShort)` if fewer than 188 bytes, or `Ok` with
133    /// the parsed packet otherwise.
134    pub fn parse(buf: &'a [u8]) -> Result<Self> {
135        if buf.len() < TS_PACKET_SIZE {
136            return Err(Error::BufferTooShort {
137                need: TS_PACKET_SIZE,
138                have: buf.len(),
139                what: "TsPacket::parse",
140            });
141        }
142        if buf[0] != TS_SYNC_BYTE {
143            return Err(Error::InvalidSyncByte { found: buf[0] });
144        }
145
146        let raw: &[u8; TS_PACKET_SIZE] =
147            buf[..TS_PACKET_SIZE]
148                .try_into()
149                .map_err(|_| Error::BufferTooShort {
150                    need: TS_PACKET_SIZE,
151                    have: buf.len(),
152                    what: "TsPacket::parse (array conversion)",
153                })?;
154
155        let header = TsHeader::parse(&raw[..4])
156            .expect("raw is 188 bytes so first 4 bytes are always present");
157
158        let mut cursor = 4usize;
159        let mut payload = None;
160
161        // Skip adaptation field if present (not parsed in detail — not needed for sections).
162        if header.has_adaptation && cursor < TS_PACKET_SIZE {
163            let af_len = raw[cursor] as usize;
164            cursor += 1 + af_len;
165        }
166
167        if header.has_payload && cursor < TS_PACKET_SIZE {
168            payload = Some(&raw[cursor..]);
169        }
170
171        Ok(TsPacket {
172            header,
173            payload,
174            raw,
175        })
176    }
177}
178
179/// Reassembles PSI/SI sections from TS packets on a single PID.
180///
181/// Feed each TS packet's payload with `feed`. Complete sections are
182/// appended to an internal queue; drain them with `pop_section`.
183#[derive(Default)]
184pub struct SectionReassembler {
185    buf: bytes::BytesMut,
186    expected: usize,
187    ready: std::collections::VecDeque<bytes::Bytes>,
188}
189
190impl SectionReassembler {
191    /// Feed a TS payload and whether its packet had PUSI set.
192    ///
193    /// Extracts complete SI sections into the internal queue. A single call
194    /// can produce zero or one section (the queue is for future-proofing
195    /// where one feed might yield multiple sections).
196    pub fn feed(&mut self, payload: &[u8], pusi: bool) {
197        if pusi {
198            // A PUSI packet whose adaptation field consumed the whole body is
199            // malformed but constructible — drop sync rather than panic.
200            if payload.is_empty() {
201                self.buf.clear();
202                self.expected = 0;
203                return;
204            }
205            let pointer = payload[0] as usize;
206            let start = 1 + pointer;
207            if start >= payload.len() {
208                self.buf.clear();
209                return;
210            }
211            self.buf.clear();
212            let new_data = &payload[start..];
213            if self.buf.len() + new_data.len() > MAX_SECTION_SIZE {
214                self.buf.clear();
215                self.expected = 0;
216                return;
217            }
218            self.buf.extend_from_slice(new_data);
219            if self.buf.len() >= 3 {
220                self.expected = 3 + (((self.buf[1] & 0x0F) as usize) << 8 | self.buf[2] as usize);
221            }
222        } else {
223            if self.buf.is_empty() {
224                return;
225            }
226            if self.buf.len() + payload.len() > MAX_SECTION_SIZE {
227                self.buf.clear();
228                self.expected = 0;
229                return;
230            }
231            self.buf.extend_from_slice(payload);
232        }
233
234        if self.expected > 0 && self.buf.len() >= self.expected {
235            // split_to returns the first `expected` bytes as an owned BytesMut,
236            // leaving the remaining bytes in self.buf — cheap (shifts pointers).
237            let section = self.buf.split_to(self.expected).freeze();
238            self.ready.push_back(section);
239            self.expected = 0;
240        }
241    }
242
243    /// Pop one complete section. Returns `None` when the queue is empty.
244    pub fn pop_section(&mut self) -> Option<bytes::Bytes> {
245        self.ready.pop_front()
246    }
247
248    /// Number of bytes currently buffered (incomplete section).
249    pub fn len(&self) -> usize {
250        self.buf.len()
251    }
252
253    /// True if no bytes are currently buffered.
254    pub fn is_empty(&self) -> bool {
255        self.buf.is_empty()
256    }
257}
258
259#[cfg(test)]
260mod tests {
261    use super::*;
262
263    /// Helper: construct a minimal 188-byte TS packet buffer with given header flags and payload.
264    fn make_packet(b1: u8, b2: u8, b3: u8, payload_data: &[u8]) -> [u8; TS_PACKET_SIZE] {
265        let mut pkt = [0u8; TS_PACKET_SIZE];
266        pkt[0] = TS_SYNC_BYTE;
267        pkt[1] = b1;
268        pkt[2] = b2;
269        pkt[3] = b3;
270        let payload_start = 4;
271        let end = (payload_start + payload_data.len()).min(TS_PACKET_SIZE);
272        let len = (end - payload_start).min(payload_data.len());
273        pkt[payload_start..payload_start + len].copy_from_slice(&payload_data[..len]);
274        pkt
275    }
276
277    #[test]
278    fn parse_rejects_non_0x47_sync_byte() {
279        let mut pkt = [0u8; TS_PACKET_SIZE];
280        pkt[0] = 0x46; // wrong sync byte
281        let err = TsPacket::parse(&pkt).unwrap_err();
282        match err {
283            Error::InvalidSyncByte { found } => assert_eq!(found, 0x46),
284            other => panic!("expected InvalidSyncByte, got {other:?}"),
285        }
286    }
287
288    #[test]
289    fn parse_extracts_pid_and_continuity_counter() {
290        // PID = 0x1234 → upper 5 bits = 0x12, lower 8 bits = 0x34
291        // CC = 5 → 0x05
292        // b1 = 0x47 (sync=0, tei=0, pusi=0) | (0x12) = 0x47 & 0xE0 | 0x12 = 0x47 & 0xE0 = 0x40 | 0x12 = 0x52
293        // Actually: b1 bits: [tei:1][pusi:1][pid_hi:5]
294        // pid_hi = 0x12 = 0b00100_10 → bits 5..=1 = 0x12
295        // b1 = 0b00_010010 = 0x12 (no tei, no pusi)
296        let pkt = make_packet(0x12, 0x34, 0x05, &[]);
297        let pkt = TsPacket::parse(&pkt).unwrap();
298        assert_eq!(pkt.header.pid, 0x1234);
299        assert_eq!(pkt.header.continuity_counter, 5);
300    }
301
302    #[test]
303    fn payload_unit_start_indicator_flag_extracted() {
304        // b1 = 0x40 → pusi = true (bit 6 set, no tei, no pid bits)
305        let pkt1 = make_packet(0x40, 0x00, 0x00, &[]);
306        let pkt1 = TsPacket::parse(&pkt1).unwrap();
307        assert!(pkt1.header.pusi);
308
309        // b1 = 0x00 → pusi = false
310        let pkt2 = make_packet(0x00, 0x00, 0x00, &[]);
311        let pkt2 = TsPacket::parse(&pkt2).unwrap();
312        assert!(!pkt2.header.pusi);
313    }
314
315    /// Build a PSI-carrying TS payload: `pointer_field` byte followed by
316    /// (optionally) some tail of a previous section, followed by a fresh
317    /// section. `pointer_field` is the number of bytes of the previous
318    /// section that precede the new one (per ETSI EN 300 468 §5.1.4).
319    fn build_pusi_payload(pointer_field: u8, previous_tail: &[u8], section: &[u8]) -> Vec<u8> {
320        assert_eq!(pointer_field as usize, previous_tail.len());
321        let mut v = Vec::with_capacity(1 + previous_tail.len() + section.len());
322        v.push(pointer_field);
323        v.extend_from_slice(previous_tail);
324        v.extend_from_slice(section);
325        v
326    }
327
328    /// Build a long-form section with the given table_id and body bytes.
329    /// Returns the full section including its 3-byte + 5-byte header and a
330    /// placeholder CRC — for reassembler testing we don't validate the CRC.
331    fn build_section(table_id: u8, body_after_length: &[u8]) -> Vec<u8> {
332        let section_length = body_after_length.len() as u16;
333        let mut v = Vec::with_capacity(3 + section_length as usize);
334        v.push(table_id);
335        // ssi=1, pi=0, reserved=11, length hi 4 bits
336        v.push(0xB0 | ((section_length >> 8) as u8 & 0x0F));
337        v.push((section_length & 0xFF) as u8);
338        v.extend_from_slice(body_after_length);
339        v
340    }
341
342    // The reassembler tests below feed raw payload slices directly to
343    // `feed()` rather than wrapping them in 188-byte TS packets. This avoids
344    // the TS stuffing-byte tail (0xFF padding) bleeding into the reassembled
345    // section and keeps the assertions exact.
346
347    #[test]
348    fn reassembler_accumulates_multi_packet_section() {
349        // 200-byte section that spans two payload slices.
350        let body = vec![0xAAu8; 197];
351        let section = build_section(0x02, &body);
352        assert_eq!(section.len(), 200);
353
354        let first_chunk = 100;
355        let payload1 = build_pusi_payload(0, &[], &section[..first_chunk]);
356        let payload2 = section[first_chunk..].to_vec();
357
358        let mut reasm = SectionReassembler::default();
359        reasm.feed(&payload1, true);
360        reasm.feed(&payload2, false);
361
362        let out = reasm.pop_section().expect("section should be ready");
363        assert_eq!(out.len(), 200);
364        assert_eq!(out.as_ref(), &section[..]);
365    }
366
367    #[test]
368    fn reassembler_yields_complete_section_once_length_satisfied() {
369        // 1-byte-body section: table_id=0x42, section_length=1, total=4 bytes.
370        let section = build_section(0x42, &[0xAA]);
371        assert_eq!(section.len(), 4);
372        let payload = build_pusi_payload(0, &[], &section);
373
374        let mut reasm = SectionReassembler::default();
375        reasm.feed(&payload, true);
376
377        let out = reasm
378            .pop_section()
379            .expect("single-packet section should pop");
380        assert_eq!(out.as_ref(), &section[..]);
381    }
382
383    #[test]
384    fn reassembler_discards_on_buffer_overflow() {
385        // Declare section_length larger than a single payload can carry. No
386        // pop happens until continuations arrive; if continuations push the
387        // buffer past MAX_SECTION_SIZE the reassembler must reset, not panic.
388        let mut section = Vec::with_capacity(3 + 4095);
389        section.push(0x00); // table_id
390        section.push(0xB0 | ((4095u16 >> 8) as u8 & 0x0F));
391        section.push(0xFF);
392        section.extend_from_slice(&[0u8; 160]);
393        let payload1 = build_pusi_payload(0, &[], &section);
394
395        let mut reasm = SectionReassembler::default();
396        reasm.feed(&payload1, true);
397        assert!(reasm.pop_section().is_none());
398
399        // Push enough continuation data to cross MAX_SECTION_SIZE.
400        let filler = vec![0u8; 180];
401        for _ in 0..(MAX_SECTION_SIZE / 180 + 1) {
402            reasm.feed(&filler, false);
403        }
404        assert!(
405            reasm.pop_section().is_none(),
406            "no section should pop after overflow reset"
407        );
408
409        // State must be resettable — a fresh valid PUSI section works.
410        let valid_section = build_section(0x00, &[0xAA]);
411        let payload2 = build_pusi_payload(0, &[], &valid_section);
412        reasm.feed(&payload2, true);
413        let out = reasm
414            .pop_section()
415            .expect("fresh section should pop after reset");
416        assert_eq!(out.as_ref(), &valid_section[..]);
417    }
418
419    #[test]
420    fn reassembler_handles_pusi_with_nonzero_pointer_field() {
421        // payload = pointer_field=3, 3 bytes of prior-section tail, then new section.
422        let prior_tail = vec![0x11, 0x22, 0x33];
423        let new_section = build_section(0x02, &[0xBB]);
424        assert_eq!(new_section.len(), 4);
425        let payload = build_pusi_payload(3, &prior_tail, &new_section);
426
427        let mut reasm = SectionReassembler::default();
428        reasm.feed(&payload, true);
429
430        let out = reasm
431            .pop_section()
432            .expect("section after pointer_field skip should pop");
433        assert_eq!(out.as_ref(), &new_section[..]);
434    }
435
436    #[test]
437    fn reassembler_ignores_continuation_before_pusi() {
438        // Feed a non-PUSI payload first (no prior PUSI seen).
439        // SectionReassembler should discard it and stay empty.
440        let pkt = make_packet(0x00, 0x00, PAYLOAD_FLAG, &[0xAA, 0xBB, 0xCC]);
441
442        let mut reasm = SectionReassembler::default();
443        reasm.feed(&pkt[4..], false); // no PUSI
444
445        assert!(
446            reasm.pop_section().is_none(),
447            "no section should appear without prior PUSI"
448        );
449        assert!(
450            reasm.pop_section().is_none(),
451            "second pop should also be none"
452        );
453    }
454
455    /// A PUSI packet with an empty payload (adaptation field ate the body)
456    /// is malformed but must not panic — it drops sync.
457    #[test]
458    fn reassembler_empty_pusi_payload_does_not_panic() {
459        let mut reasm = SectionReassembler::default();
460        reasm.feed(&[], true);
461        assert!(reasm.pop_section().is_none());
462        // Recovers on the next clean PUSI.
463        let mut payload = vec![0x00u8, 0x72, 0x70, 0x01, 0x00];
464        payload.resize(5, 0);
465        reasm.feed(&payload, true);
466        assert!(reasm.pop_section().is_some());
467    }
468
469    /// A maximal short-form private section (section_length 0xFFF, total
470    /// 4098 bytes) reassembles — the ceiling is 12-bit length + 3-byte
471    /// header, not 4096.
472    #[test]
473    fn reassembler_accepts_maximal_private_section() {
474        let mut section = vec![0x80u8, 0x7F, 0xFF]; // user-private tid, SSI=0, len 0xFFF
475        section.resize(3 + 0xFFF, 0xAB);
476
477        let mut reasm = SectionReassembler::default();
478        // First TS payload: pointer_field 0 then the section start.
479        let mut first = vec![0x00];
480        first.extend_from_slice(&section[..183]);
481        reasm.feed(&first, true);
482        for chunk in section[183..].chunks(184) {
483            reasm.feed(chunk, false);
484        }
485        let out = reasm.pop_section().expect("4098-byte section should pop");
486        assert_eq!(out.len(), 4098);
487        assert_eq!(out.as_ref(), &section[..]);
488    }
489}