Skip to main content

mpeg_ts/
lib.rs

1//! MPEG-2 Transport Stream framing (ITU-T H.222.0 / ISO/IEC 13818-1 §2.4.3):
2//! 188-byte packet headers, adaptation fields, PCR, PSI section reassembly and
3//! packetisation, and byte-stream resync. It sits below `dvb-si` (which
4//! interprets table bodies), `mpeg-pes`, `dvb-conformance` and `transmux`; it
5//! depends only on `broadcast-core`, `broadcast-common`, `bytes` and
6//! `thiserror`, and does no I/O.
7//!
8//! # Quickstart
9//!
10//! Parse a TS packet and recover a complete PSI section from its payload:
11//!
12//! ```rust
13//! use mpeg_ts::pid::well_known;
14//! use mpeg_ts::ts::{SectionReassembler, TsPacket};
15//!
16//! let mut raw = [0xFFu8; 188];
17//! raw[..21].copy_from_slice(&[
18//!     0x47, 0x40, 0x00, 0x10,
19//!     0x00,
20//!     0x00, 0xB0, 0x0D, 0x00, 0x01, 0xC1, 0x00, 0x00, 0x04, 0x01, 0xE0, 0x64,
21//!     0xF9, 0xB4, 0x63, 0xEF,
22//! ]);
23//!
24//! let packet = TsPacket::parse(&raw)?;
25//! assert_eq!(packet.header.pid, well_known::PAT);
26//! assert!(packet.header.pusi);
27//! let mut reassembler = SectionReassembler::default();
28//! reassembler.feed(packet.payload.expect("payload"), packet.header.pusi);
29//! let section = reassembler.pop_section().expect("one complete section");
30//! assert_eq!(section[0], 0x00);
31//! # Ok::<(), mpeg_ts::Error>(())
32//! ```
33//!
34//! # How do I...
35//!
36//! - Parse or serialize one packet: [`ts::TsPacket`], `examples/iter_packets.rs`.
37//! - Edit a packet in place (continuity counter, PCR): [`OwnedTsPacket`],
38//!   `examples/edit_packet.rs`.
39//! - Reassemble PSI sections across packets: [`ts::SectionReassembler`],
40//!   `examples/demux.rs`.
41//! - Packetise sections into TS packets: [`mux::SectionPacketiser`],
42//!   `examples/demux.rs`.
43//! - Recover alignment in an unknown byte stream: [`resync::TsResync`],
44//!   `examples/iter_packets.rs`.
45//! - Reassemble a PES-like payload unit: [`unit::UnitReassembler`],
46//!   `examples/demux.rs`.
47//! - Work with PCR values and step detection: [`pcr`], `examples/edit_packet.rs`.
48//! - Enumerate reserved PIDs: [`pid::well_known`], `examples/demux.rs`.
49//!
50//! # Modules
51//!
52//! Packet layer: [`ts`] (borrowed packets and headers), [`owned`] (mutable
53//! packet bytes), [`pid`] (PID values), [`pcr`] (PCR and OPCR values),
54//! [`bitrate`] (packet-rate arithmetic), [`ts_dup`] (duplicate-packet
55//! detection).
56//!
57//! Section and payload layer: [`section`] (section syntax and CRC),
58//! [`descriptor_loop`] (borrowed descriptor loops), [`mux`] (section
59//! packetisation), [`mod@unit`] (payload-unit reassembly).
60//!
61//! Framing recovery: [`resync`] (stride and phase search), [`reader`]
62//! (`std`-only byte reader).
63//!
64//! Shared vocabulary: [`error`] (typed errors).
65//!
66//! # Feature flags
67//!
68//! - `std` (default): `std::error::Error` impls and [`reader`].
69//! - `serde`: serialize-only `serde::Serialize` on packet and section types.
70//!
71//! # Spec citations
72//!
73//! ITU-T H.222.0 / ISO/IEC 13818-1 §2.4.3.2 (packet layer), §2.4.3.4
74//! (adaptation field), §2.4.4 (`pointer_field` and sections), §2.4.3.5
75//! (adaptation-field extension); ETSI EN 300 468 §5.1.1 (section header).
76#![deny(missing_docs)]
77#![no_std]
78#![forbid(unsafe_code)]
79#![cfg_attr(docsrs, feature(doc_cfg))]
80extern crate alloc;
81#[cfg(feature = "std")]
82extern crate std;
83
84#[cfg(doctest)]
85#[doc = include_str!("../README.md")]
86struct ReadmeDoctests;
87
88pub mod bitrate;
89pub mod descriptor_loop;
90pub mod error;
91pub mod mux;
92pub mod owned;
93pub mod pcr;
94pub mod pid;
95#[cfg(feature = "std")]
96pub mod reader;
97pub mod resync;
98pub mod section;
99pub mod ts;
100pub mod ts_dup;
101pub mod unit;
102
103pub use error::{Error, Result};
104pub use owned::OwnedTsPacket;
105pub use ts::{
106    AdaptationField, AdaptationFieldControl, AdaptationFieldExtension, ContinuityCounter, Ltw,
107    LtwOffset, Pcr, PiecewiseRate, ScramblingControl, SeamlessSplice, TS_BYTES_PER_DATAGRAM,
108    TS_PACKET_SIZE, TS_PACKETS_PER_DATAGRAM, TS_SYNC_BYTE, TsHeader, TsPacket,
109};