Skip to main content

Crate mpeg_ts

Crate mpeg_ts 

Source
Expand description

MPEG-2 Transport Stream framing (ITU-T H.222.0 / ISO/IEC 13818-1 §2.4.3): 188-byte packet headers, adaptation fields, PCR, PSI section reassembly and packetisation, and byte-stream resync. It sits below dvb-si (which interprets table bodies), mpeg-pes, dvb-conformance and transmux; it depends only on broadcast-core, broadcast-common, bytes and thiserror, and does no I/O.

§Quickstart

Parse a TS packet and recover a complete PSI section from its payload:

use mpeg_ts::pid::well_known;
use mpeg_ts::ts::{SectionReassembler, TsPacket};

let mut raw = [0xFFu8; 188];
raw[..21].copy_from_slice(&[
    0x47, 0x40, 0x00, 0x10,
    0x00,
    0x00, 0xB0, 0x0D, 0x00, 0x01, 0xC1, 0x00, 0x00, 0x04, 0x01, 0xE0, 0x64,
    0xF9, 0xB4, 0x63, 0xEF,
]);

let packet = TsPacket::parse(&raw)?;
assert_eq!(packet.header.pid, well_known::PAT);
assert!(packet.header.pusi);
let mut reassembler = SectionReassembler::default();
reassembler.feed(packet.payload.expect("payload"), packet.header.pusi);
let section = reassembler.pop_section().expect("one complete section");
assert_eq!(section[0], 0x00);

§How do I…

  • Parse or serialize one packet: ts::TsPacket, examples/iter_packets.rs.
  • Edit a packet in place (continuity counter, PCR): OwnedTsPacket, examples/edit_packet.rs.
  • Reassemble PSI sections across packets: ts::SectionReassembler, examples/demux.rs.
  • Packetise sections into TS packets: mux::SectionPacketiser, examples/demux.rs.
  • Recover alignment in an unknown byte stream: resync::TsResync, examples/iter_packets.rs.
  • Reassemble a PES-like payload unit: unit::UnitReassembler, examples/demux.rs.
  • Work with PCR values and step detection: pcr, examples/edit_packet.rs.
  • Enumerate reserved PIDs: pid::well_known, examples/demux.rs.

§Modules

Packet layer: ts (borrowed packets and headers), owned (mutable packet bytes), pid (PID values), pcr (PCR and OPCR values), bitrate (packet-rate arithmetic), ts_dup (duplicate-packet detection).

Section and payload layer: section (section syntax and CRC), descriptor_loop (borrowed descriptor loops), mux (section packetisation), unit (payload-unit reassembly).

Framing recovery: resync (stride and phase search), reader (std-only byte reader).

Shared vocabulary: error (typed errors).

§Feature flags

  • std (default): std::error::Error impls and reader.
  • serde: serialize-only serde::Serialize on packet and section types.

§Spec citations

ITU-T H.222.0 / ISO/IEC 13818-1 §2.4.3.2 (packet layer), §2.4.3.4 (adaptation field), §2.4.4 (pointer_field and sections), §2.4.3.5 (adaptation-field extension); ETSI EN 300 468 §5.1.1 (section header).

Re-exports§

pub use error::Error;
pub use error::Result;
pub use owned::OwnedTsPacket;
pub use ts::AdaptationField;
pub use ts::AdaptationFieldControl;
pub use ts::AdaptationFieldExtension;
pub use ts::ContinuityCounter;
pub use ts::Ltw;
pub use ts::LtwOffset;
pub use ts::Pcr;
pub use ts::PiecewiseRate;
pub use ts::ScramblingControl;
pub use ts::SeamlessSplice;
pub use ts::TS_BYTES_PER_DATAGRAM;
pub use ts::TS_PACKET_SIZE;
pub use ts::TS_PACKETS_PER_DATAGRAM;
pub use ts::TS_SYNC_BYTE;
pub use ts::TsHeader;
pub use ts::TsPacket;

Modules§

bitrate
Multiplex bitrate estimated from the PCRs of one PCR PID (ISO/IEC 13818-1 §2.4.3.5, PCR PIDs per program §2.4.4.9).
descriptor_loop
The raw descriptor() loop of ISO/IEC 13818-1 §2.6 (Tables 2-42 through 2-45): one tag/length framing walk for every consumer of a PMT ES_info loop, a Program Stream Map loop or any other descriptor region.
error
Error type returned by every parser and builder in this crate.
mux
Section → TS packetiser (the byte-exact inverse of SectionReassembler::feed).
owned
Owned 188-byte TS packet with pre-parsed header fields.
pcr
PCR step classification: the per-PID PCR memory and the step predicate behind ETSI TR 101 290 v1.4.1 Table 5.0b indicator 2.3b (PCR_discontinuity_indicator_error).
pid
MPEG-TS Packet Identifier (PID) types and well-known PID constants.
readerstd
Streaming TS packet framing over any std::io::Read.
resync
Stateful TS byte-stream resynchroniser — ITU-T H.222.0 §2.4.3.2 (= ISO/IEC 13818-1 §2.4.3.2).
section
Generic PSI/SI section framing — ITU-T H.222.0 §2.4 (= ISO/IEC 13818-1) and ETSI EN 300 468 §5.1.1.
ts
MPEG-TS packet parser and section reassembler — ITU-T H.222.0 §2.4 (= ISO/IEC 13818-1).
ts_dup
Legal-duplicate transport-stream-packet detection — ITU-T H.222.0 (08/2023) / ISO/IEC 13818-1 §2.4.3.3.
unit
One reassembler for a payload unit carried across TS packets: a PSI section (ISO/IEC 13818-1 §2.4.4), a T2-MI packet (ETSI TS 102 773 §6.1.1) or a ULE SNDU (RFC 4326 §3, §6, §7) all travel as a byte stream cut into TS payloads, with payload_unit_start_indicator and a one-byte pointer naming where the first unit of the packet starts.