mpeg-ts
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 and0xFFstuffing; - packetise sections into TS packets (
SectionPacketiser); table repetition scheduling lives indvb_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-delimitedemsg); - 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:
= { = "0.11", = ["serde"] }
The crate is #![no_std] + alloc and #![forbid(unsafe_code)];
default-features = false builds on bare-metal targets that have a heap.
[]
= "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 ;
use well_known;
// PID 0x0000, PUSI set, payload-only; PAT section: transport_stream_id 1, one program.
let mut raw = ;
raw.copy_from_slice;
let pkt = parse?;
assert_eq!;
assert!;
let mut reasm = default;
reasm.feed;
let section = reasm.pop_section.expect;
assert_eq!; // table_id = PAT
assert_eq!; // transport_stream_id
# Ok::
Build and edit packets without manual byte twiddling:
use ;
use ;
let null = null_packet;
assert_eq!;
let mut raw = serialize_with_payload?;
set_continuity_counter;
assert_eq!;
let mut pcr_pkt = ;
pcr_pkt = 0x20; // adaptation only
pcr_pkt = 7; // adaptation_field_length
pcr_pkt = 0x10; // PCR flag
set_pcr?;
let parsed = parse?;
assert_eq!;
# Ok::
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.rsandtests/it/af_extension_roundtrip.rsre-serialize every adaptation field of every committed.tsfixture 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]withnew()/Default(reserved bits default to1, per the spec convention). - Checked widths. Serializers use
broadcast_core::lenchecked 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()andDisplay(workspace-guards/tests/guards/label.rsguards 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.