Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Format:MPEG transport stream

From ReverseEngineering.wiki
Format
On this wiki Facts · Code · Notes · Topics

MPEG-2 transport stream (.ts, .m2ts, .mts): a stream of fixed 188-byte packets (192 with a 4-byte timestamp prefix in M2TS/DVHS, 204 with Reed-Solomon FEC) each starting with sync byte 0x47 and a 13-bit PID; PID 0 carries the PAT that points to the PMTs listing each elementary stream's PID and stream type.

Overview

A transport stream is a sequence of fixed-size packets with no file header, so it is identified only by the repeating sync byte 0x47 at a stride of 188, 192 or 204 bytes. FFmpeg's probe scores each stride (TS_PACKET_SIZE 188, TS_DVHS_PACKET_SIZE 192, TS_FEC_PACKET_SIZE 204) and picks the best.

Packet header

Byte 0 is the sync byte 0x47. The 13-bit PID is (buf[1]<<8 | buf[2]) & 0x1FFF; bit 0x40 of byte 1 is payload_unit_start_indicator (FFmpeg's is_start). Packets with PID 0x1FFF are null padding. Adaptation field and payload flags follow in byte 3 (FFmpeg libavformat/mpegts.c at c2bbf2b83d, lines 3223-3234: afc = (packet[3] >> 4) & 3 with afc 0 reserved, bit 1 of afc = adaptation field present, bit 0 = payload present, and cc = packet[3] & 0xf is the 4-bit continuity counter; the expected counter advances by one modulo 16 only for packets with a payload).

Tables and well-known PIDs

PID 0x0000 is the PAT (table id 0x00): it lists program numbers and the PID of each program's PMT (table id 0x02). The PMT gives the PCR PID and, per elementary stream, a stream_type and a 13-bit PID. Other fixed PIDs: CAT 0x0001, TSDT 0x0002, NIT 0x0010, SDT/BAT 0x0011 (SDT table id 0x42), EIT 0x0012, TDT/TOT 0x0014. Stream types seen in FFmpeg: 0x02 MPEG-2 video, 0x06 private data (PES with descriptors, e.g. subtitles or AC-3), 0x0F AAC (ADTS), 0x1B H.264, 0x24 HEVC.

Variants

  • M2TS (Blu-ray, AVCHD): 192-byte packets, a 4-byte TP_extra_header (timestamp) before each 188-byte packet; FFmpeg's header names M2TS_PMT_PID 0x0100, M2TS_PCR_PID 0x1001, M2TS_VIDEO_PID 0x1011.
  • FEC: 204 bytes, 16 bytes of Reed-Solomon parity appended.
  • HLS segments are plain 188-byte transport streams.

Reversing notes

Games that ship .ts/.m2ts cutscenes can be cut at 188-byte boundaries; look for 47 at offsets 0, 188, 376. Sections (PAT, PMT) start after a pointer_field byte when the payload_unit_start bit is set. FFmpeg mpegts.c (c2bbf2b83d, lines 3278-3281) does the same: for a section PID with is_start set it reads len = *p++ as the pointer field, writes those len bytes as the tail of the previous section and starts the new section after them.

Code & repositories

  • https://github.com/FFmpeg/FFmpeg @ c2bbf2b83d064313ca3739f81c651553cfbb26fb · license: LGPL-2.1-or-later (declared by the contributor; GitHub's detector did not recognize COPYING.GPLv2)
    FFmpeg libavformat demuxers smacker.c, idroqdec.c, flvdec.c, mpegts.c
    Tested on: source read, not run: source reading only · checked 2026-10-18; last activity 2026-10-18

Evidence & references

  • FFmpeg libavformat/mpegts.h and mpegts.c (LGPL-2.1+).
  • ISO/IEC 13818-1.