Transport Stream Analysis

A Transport Stream packet is a fixed length packet, there are a few Transport Stream packet sizes to be aware of:

188 Transport Stream

192 Transport Stream + 4 bit Timecode (MT2S Blu Ray)

204 Transport Stream + FEC (DVB-T/C/S)

208 Transport Stream + FEC (ATSC 8-VSB, 208)

208 Transport Stream + Reed Solomon (ATSC)

Transport Stream Packets are the basic units of a Transport Stream, each TS packet starts with a Sync Byte (0x47) and this is used to identify each packet (so 0x47 shouldn’t be used elsewhere in the packet).
Each TS packet has a 32 bit header, shown below.
The TS Header above (also called the TS 4 Byte Prefix) is from a real TS packet, below is the TS header in Hex.

Name	Bits	Bin	Hex	Dec	Description
Sync Byte	8	01000111	0x47	Sync Byte
TS Error	1	0	0x00	Error indicator (0=No Error, 1=Error)
Start Indicator	1	0	0x00	1 means start of PES or PSI otherwise 0
Transport Priority	1	0	0x00	see table below
PID	13	1111111111111	1FFF	8191	PID is 8191 (1FFF) = stuffing packet
Scrambling	2	00	0x00	00 = not scrambled
Adaptation Field Exist	2	01	0x01	01 means no adaptation fields
CC Counter	4	1001	0x09	continuity counter
Total	32

Looking at the bits of the example TS Header tells us a few things about the packet we are analysing. Importantly we can see this packet has no (apparent) errors, the PID tells us it is a stuffing packet, there is no adaptation field and we have a continuity counter that helps us determine whether there are any missing packets.
In the above diagram it can be seen that the overall format of the TS packet is as follows:
TS Header → Adaptation Field → Data Payload
A PES packet is a TS packet that has had its TS and Adaptation components removed (so the payload basically) and the PES packet has a 32bit (4 Byte) PES header.

PCR PTS DTS

The PCR is a sample of the Encoders 27Mhz clock, the PCR comprises of 33bits + 9 bits.
The format of the 42-bit 27MHz clock is slightly peculiar in that the 33 MSBs count normally at a rate of 90kHz and the 9 LSBs count mod 300 (i.e.0-299). This strange arrangement allows a 27MHz clock, which is readily compatible with the 90kHz time stamps in the program material.
PCR is for the synchronization of the decoders clock, the 27Mhz clock in the Encoder is sampled, and this is used to synchronise the 27Mhz clock in the decoder. With the clocks in sync (so any error on the Encoder clock is also on the decoder clock) the timestamps in the PTS and DTS will still match the decoded PCR.
For DVB Transport Streams the a PCR needs to be present every 40mS (so every frame).
The DTS is the Decode Timestamp, while the encoding process GoP might be IBBP, the frames can not be decoded in that order, so encoder frames are reordered and time stamped in their Decode order.
Below is an example, the I frame must be decoded first, then P frame can then be decoded, once the I and P are decoded the B frames can then also be decoded. So the correct decode order is IPBB.
The PTS is the Presentations Timestamp, this is the order in which the frames must be displayed (presented) to the user (the screen)
So having PTS and DTS timestamps in the PES header allows the decoder to decode and display the frames in the correct order.
The PTS and DTS are 33bit, 90kHz versions of the clock.

Transport Stream Packet Description

Adaption Field Format Description

Name	Number	Description
of bits
sync byte
8	0x47
Transport Error Indicator (TEI)	1	Set by demodulator if can't correct errors in the stream, to tell the demultiplexer that the packet has an uncorrectable error [11]
Payload Unit Start Indicator	1	1 means start of PES data or PSI otherwise zero only.
Transport Priority	1	1 means higher priority than other packets with the same PID.
PID	13	Packet ID
Scrambling control	2	00' = Not scrambled
01' = Reserved for future use
10' = Scrambled with even key,
11' = Scrambled with odd key
Adaptation field exist	2	01 = no adaptation fields, payload only
10 = adaptation field only
11 = adaptation field and payload
Continuity counter	4	Incremented only when a payload is present (i.e., adaptation field exist is 01 or 11)[13]
Note: the total number of bits above is 32 and is called the transport stream 4-byte prefix or Transport Stream Header.
Adaptation field	0 or more	Depends on flags
Payload Data	0 or more	Depends on flags
Adaptation Field Format Description
Name	Number	Adaptation Field Format Description
of bits
Adaptation Field Length	8	Number of bytes in the adaptation field immediately following this byte
Discontinuity indicator	1	Set to 1 if current TS packet is in a discontinuity state with respect to either the continuity counter or the program clock reference
Random Access indicator	1	Set to 1 if the PES packet in this TS packet starts a video/audio sequence
Elementary stream priority indicator	1	1 = higher priority
PCR flag	1	1 means adaptation field does contain a PCR field
OPCR flag	1	1 means adaptation field does contain an OPCR field
Splicing point flag	1	1 means presence of splice countdown field in adaptation field
Transport private data flag	1	1 means presence of private data bytes in adaptation field
Adaptation field extension flag	1	1 means presence of adaptation field extension
Below fields are optional	variable	Depends on flags
PCR	33+9	Program clock reference, stored in 6 octets in big-endian as 33 bits base, 6 bits padding, 9 bits extension.
OPCR	33+9	Original Program clock reference. Helps when one TS is copied into another
Splice countdown	8	Indicates how many TS packets from this one a splicing point occurs (may be negative)
stuffing bytes	variable