mpeg-ts 0.11.0

MPEG-2 Transport Stream framing (ITU-T H.222.0 / ISO/IEC 13818-1): TS packet, adaptation field, PCR, PSI section reassembly + packetisation, resync. no_std.
Documentation

mpeg-ts

crates.io docs.rs CI

MPEG-2 Transport Stream framing: TS packets, adaptation fields, PCR, PSI section reassembly and packetisation, byte-stream resync.

Lowest framing layer of the workspace, above broadcast-core/broadcast-common and below dvb-si, mpeg-pes, dvb-conformance and transmux. The crate-root guide has the quickstart, the How do I... task index, the module map, feature flags and spec citations.

What it does / what it does not

mpeg-ts is the lowest layer of the workspace's broadcast stack. It turns a stream of bytes into typed 188-byte transport packets (and back), and it turns the payload of PSI/SI PIDs into complete sections (and back). It is for anyone who needs raw TS framing without the SI/descriptor stack: demuxers, muxers, stream repair tools, analyzers, embedded firmware.

It does:

  • parse and serialize the 4-byte TS header and the adaptation field (every bit, including stuffing and reserved bits, so parse -> serialize is byte-exact on real streams);
  • reassemble PSI/SI sections from TS payloads (pointer_field, multi-packet, several sections per packet) and packetise sections back, with continuity counters and 0xFF stuffing;
  • packetise sections into TS packets (SectionPacketiser); table repetition scheduling lives in dvb_si::mux::SiMux;
  • recover packet alignment from a raw byte stream (188-, 192- and 204-byte strides);
  • reassemble a payload unit across TS packets (unit::UnitReassembler, including PUSI-delimited emsg);
  • provide an owned, mutable packet type for in-place edits (continuity counter, PCR).

It deliberately does not: parse PES (see mpeg-pes), interpret PSI/SI table bodies or descriptors (see dvb-si), descramble (see dvb-csa), check stream conformance (see dvb-conformance), or do any I/O.

Standards supported

Standard Edition Scope Status Notes
ITU-T H.222.0 / ISO/IEC 13818-1, Generic coding of moving pictures and associated audio information: Systems H.222.0 v9 (08/2023); vendored copy verified in private/specs-md §2.4.3.2 TS packet layer, §2.4.3.3 field semantics, §2.4.3.4 adaptation field (incl. PCR/OPCR, splice, LTW, piecewise rate, extension), §2.4.4 PSI pointer_field and section framing Full for the listed clauses Other H.222.0 clauses (PES §2.4.3.6, program streams §2.5, descriptors) are not this crate's job. Layouts cited by clause in module docs; 2007-era layouts for these clauses are unchanged.
ETSI EN 300 468 V1.19.1 (the edition vendored; a V1.20.1 copy is also present) §5.1.1 generic section header, §5.1.3 Table 1 well-known PIDs, CRC_32 placement Partial: framing and PID constants only Table bodies and descriptors live in dvb-si. The TOT's CRC is not handled by the short-form path (use dvb-si).
ISO/IEC 23009-1 not pinned by this crate §5.10.3.3.5 PUSI-delimited emsg carriage on PID 0x0004 Partial: generic PUSI reassembly only The emsg box itself is parsed by isobmff.
204-byte RS-coded packets (188 + 16 parity) n/a Stride detection in TsResync Partial Parity bytes are stripped, never checked.
192-byte M2TS/BDAV packets (188 + 4 TP_extra_header) n/a Stride detection in TsResync (PacketStride::M2ts192, M2TS_PACKET_SIZE, M2TS_EXTRA_HEADER_LEN) Full The 4-byte extra header is stripped; a packet is emitted as soon as its 188 bytes arrive.

Feature flags

Feature Default What it enables
std yes std::error::Error impls; turn off for no_std
serde no serde::Serialize on packet/section types (serialize-only)

serde is opt-in and off by default; enable it explicitly:

mpeg-ts = { version = "0.11", features = ["serde"] }

The crate is #![no_std] + alloc and #![forbid(unsafe_code)]; default-features = false builds on bare-metal targets that have a heap.

[dependencies]
mpeg-ts = "0.11"
# embedded:
# mpeg-ts = { version = "0.11", default-features = false }

Quick start

Parse a real PAT packet (extracted from the committed m6-single.ts fixture) and recover its section:

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

// PID 0x0000, PUSI set, payload-only; PAT section: transport_stream_id 1, one program.
let mut raw = [0xFFu8; 188];
raw[..21].copy_from_slice(&[
    0x47, 0x40, 0x00, 0x10, // sync, PUSI + PID 0, payload only, CC 0
    0x00,                   // pointer_field
    0x00, 0xB0, 0x0D, 0x00, 0x01, 0xC1, 0x00, 0x00, 0x04, 0x01, 0xE0, 0x64, // PAT
    0xF9, 0xB4, 0x63, 0xEF, // CRC_32
]);

let pkt = TsPacket::parse(&raw)?;
assert_eq!(pkt.header.pid, well_known::PAT);
assert!(pkt.header.pusi);

let mut reasm = SectionReassembler::default();
reasm.feed(pkt.payload.expect("payload"), pkt.header.pusi);
let section = reasm.pop_section().expect("one complete section");
assert_eq!(section[0], 0x00); // table_id = PAT
assert_eq!(u16::from_be_bytes([section[3], section[4]]), 1); // transport_stream_id
# Ok::<(), mpeg_ts::Error>(())

Build and edit packets without manual byte twiddling:

use mpeg_ts::ts::{ContinuityCounter, Pcr, TsPacket};
use mpeg_ts::{pid::{well_known, Pid}, OwnedTsPacket};

let null = OwnedTsPacket::null_packet(ContinuityCounter::try_new(3).unwrap());
assert_eq!(TsPacket::parse(&null)?.header.pid, well_known::NULL);

let mut raw = OwnedTsPacket::serialize_with_payload(
    Pid::try_new(0x0100).unwrap(),
    false,
    ContinuityCounter::ZERO,
    &[],
)?;
OwnedTsPacket::set_continuity_counter(&mut raw, ContinuityCounter::try_new(7).unwrap());
assert_eq!(TsPacket::parse(&raw)?.header.continuity_counter.value(), 7);

let mut pcr_pkt = [0x47u8; 188];
pcr_pkt[3] = 0x20; // adaptation only
pcr_pkt[4] = 7;    // adaptation_field_length
pcr_pkt[5] = 0x10; // PCR flag
OwnedTsPacket::set_pcr(&mut pcr_pkt, Pcr::from_27mhz(27_000_000)?)?;
let parsed = TsPacket::parse(&pcr_pkt)?;
assert_eq!(parsed.adaptation_field().unwrap()?.pcr.unwrap().as_27mhz(), 27_000_000);
# Ok::<(), mpeg_ts::Error>(())

Runnable examples: cargo run -p mpeg-ts --example demux, --example iter_packets, --example edit_packet.

What is implemented

Module Public API Purpose
ts TsPacket<'a>, TsHeader, ContinuityCounter, AdaptationField<'a>, AdaptationFieldExtension<'a>, Pcr, Ltw, LtwOffset, PiecewiseRate, SeamlessSplice, ScramblingControl, AdaptationFieldControl, SectionReassembler, iter_packets, extract_ts_payload, payload_range, TS_PACKET_SIZE, TS_SYNC_BYTE, TS_PACKETS_PER_DATAGRAM, TS_BYTES_PER_DATAGRAM, AF_DISCONTINUITY, DTS_NEXT_AU_FIELD_LEN Zero-copy packet view, bit-exact adaptation field, PCR (27 MHz) and per-PID PSI section reassembly (SectionReassembler::feed_with_continuity drops a legally retransmitted same-counter continuation packet, and processes it when discontinuity_indicator is set)
owned OwnedTsPacket Owned 188-byte packet: build (null_packet, serialize_with_payload), mutate (set_continuity_counter, set_pcr, set_opcr, payload_mut), inspect
pid Pid (try_new, from_wire_masked, from_const, decimal or 0x FromStr), PidOutOfRange, ParsePidError, well_known::{PAT, CAT, TSDT, IPMP_CIT, NIT, SDT_BAT, EIT, RST, TDT_TOT, NETWORK_SYNC, RNT, SAT, MEASUREMENT, DIT, SIT, ATSC_PSIP, INBAND_SIGNALLING, NULL} Typed 13-bit PID newtype
section Section<'a>, validate_crc, crc_ok Generic section header (table_id, syntax indicator, length, version, section numbers, CRC validation)
mux SectionPacketiser, split_sections Sections to TS packets with continuity and stuffing. Repeating SI tables are scheduled by dvb_si::mux::SiMux.
descriptor_loop RawDescriptorLoop (entries, tags, parse_with, raw), DescriptorEntry, frame_at, DESCRIPTOR_HEADER_LEN The one ISO/IEC 13818-1 §2.6 descriptor() tag/length walk, zero-copy and byte-verbatim; a truncated entry is a typed error.
resync TsResync, ResyncConfig, Unlocked, PacketStride (Ts188, M2ts192, Rs204, ALL, TS_ONLY), ResyncStats, ResyncLoss, M2TS_PACKET_SIZE, M2TS_EXTRA_HEADER_LEN, LOCK_CONFIRMATIONS; stateless resync, resync_confirmed, unconfirmed_candidate Alignment recovery from raw bytes; TsResync::new takes the caller's ResyncConfig and feed_into appends to a caller-owned vector. The reader (reader::PacketReader, feature std) drains the loss list itself while keeping the counts in ResyncStats; direct callers drain take_losses(). take_tail_packets returns the aligned packets finish recovered at end of input.
ts_dup is_legal_duplicate_pair, check_duplicate, DuplicateVerdict (NotDuplicate, Legal, IllegalThirdRepeat) One reading of H.222.0 §2.4.3.3 legal duplicate packets, shared by dvb-conformance, media-doctor and ts-fix (moved here from broadcast-common)
pcr pcr_step_exceeds, PcrDiscDetector The TR 101 290 indicator 2.3b PCR step classifier (a step wider than the caller's limit, in either direction and across the wrap), one per-PID PCR memory shared by dvb-conformance, ts-fix and ts-remux; ITU-T H.222.0 clauses 2.4.2.3 and 2.4.3.5
reader (feature std) PacketReader, resync_stats Yields aligned 188-byte packets from any std::io::Read, with a caller-supplied chunk length and ResyncConfig.
unit UnitReassembler, UnitOptions, UnitLength, AfterContinuation, PointerCheck, Duplicates, Continuity, Fed The one caller-bounded payload-unit reassembler across TS packets (PUSI plus pointer_field), on which SectionReassembler is built.
bitrate PcrBitrateEstimator, PcrBitrate Multiplex bitrate from the PCR PID with the most samples, one clock per PID, unwrapped across the PCR wrap.
error Error, Result Structured thiserror errors (BufferTooShort, CrcMismatch, InvalidSyncByte, OutputBufferTooSmall, FieldOverflow, ...)

Typed PIDs: Pid is used everywhere (TsHeader::pid, the mux types, OwnedTsPacket). Pid::try_new / TryFrom<u16> reject values above 0x1FFF; Pid::new / Pid::from_wire_masked mask bits already taken from the wire; there is deliberately no From<u16>. Pid implements UpperHex/LowerHex and is serde(transparent).

Design notes

  • Zero-copy, symmetric. Parsed types borrow the input; every wire type has a matching serializer. tests/it/fixture_roundtrip.rs and tests/it/af_extension_roundtrip.rs re-serialize every adaptation field of every committed .ts fixture and require byte-identical output.
  • No information dropped. Adaptation-field stuffing, pcr_reserved / opcr_reserved, splice marker bits and every extension reserved bit are carried; the structs are #[non_exhaustive] with new() / Default (reserved bits default to 1, per the spec convention).
  • Checked widths. Serializers use broadcast_core::len checked helpers (FieldOverflow), never a silent mask.
  • Shared clock arithmetic. PCR and the marker-bit timestamp come from broadcast_common::clock33.
  • Label convention. Public field enums expose name() and Display (workspace-guards/tests/guards/label.rs guards it).

Related crates

Depends on broadcast-common, broadcast-core, bytes and thiserror (optional serde). Built on by dvb-si (tables/descriptors), mpeg-pes, dvb-stream, dvb-conformance, ts-fix, media-doctor and transmux. Module docs cite ISO/IEC 13818-1 (H.222.0 v9, 08/2023) by clause and ETSI EN 300 468 V1.19.1; verified transcriptions live in private/specs-verified/. Users migrating from dvb-si <= 7.x: ts, mux, resync and pid moved here unchanged in name.

MSRV 1.95.0. Licensed under MIT or Apache-2.0, at your option.