An MPEG video elementary stream is a series of headers and data, each started by the
three bytes 00 00 01 and one code byte. Start codes begin on a byte boundary.
The standard is ITU-T H.262, also ISO/IEC 13818-2; MPEG-1 video, ISO/IEC 11172-2, uses the
same codes. The first edition of H.262 is dated July 1995, with later editions in 2000 and
2012.
Code values
| Code | Name | Meaning |
|---|---|---|
| 00 | picture_start_code | a picture header |
| 01–AF | slice_start_code | a slice; the code gives the slice’s row |
| B0, B1 | reserved | |
| B2 | user_data_start_code | user data, running to the next start code |
| B3 | sequence_header_code | a sequence header |
| B4 | sequence_error_code | marks data lost in transmission |
| B5 | extension_start_code | an extension |
| B6 | reserved | |
| B7 | sequence_end_code | the end of a sequence |
| B8 | group_start_code | a group of pictures header |
| B9–FF | system start codes | used by the MPEG program and transport layers, not inside a video elementary stream |
The system codes include B9 program end, BA pack header,
BB system header, BC program stream map, BD and
BF private streams, BE padding, C0–DF
audio streams and E0–EF video streams. An .m2v
file has none of these, and a file that starts with 00 00 01 BA is a program
stream.
Order of headers
A coded sequence in the MPEG-2 syntax goes:
- Sequence header (
B3), and straight after it a sequence extension (B5). Optional sequence display and sequence scalable extensions and user data may follow. - Optionally a group of pictures header (
B8) and its user data. - For each picture: a picture header (
00), a picture coding extension (B5), any further picture extensions and user data, then the slices (01–AF). - More group headers and pictures. The sequence header may be repeated.
- A sequence end (
B7).
In a stream, pictures come in coded order, which differs from display order when B
pictures are used. The temporal_reference of each picture gives its display
position within its group.
MPEG-1 streams
A file is MPEG-2 if a sequence extension (B5 with identifier 1) directly
follows the first sequence header. Otherwise it is MPEG-1.
- MPEG-1 streams have no
B5extensions at all. They are always progressive 4:2:0. - The picture header’s motion vector
f_codefields are used in MPEG-1; MPEG-2 sets them to 7 and carries the real values in the picture coding extension. - The D picture type (4), DC coefficients only, exists only in MPEG-1.
- The
aspect_ratio_informationfield means something different: see the sequence header. - In MPEG-1 the slice code is the row number with no extension, and the
constrained_parameters_flagmarks streams of limited size that every decoder must play.
Finding the start codes
Scan the bytes for 00 00 01 and read the next byte. The syntax is designed
so that the pattern cannot occur inside the headers and data; unlike
H.264 there are no emulation prevention
bytes.