A raw .h264 file follows the byte stream format of Annex B of ITU-T H.264,
also ISO/IEC 14496-10. It is a series of NAL units, each preceded by a start code. These
reference pages were written from public material: the IETF RFCs for carrying H.264 over
RTP and FFmpeg’s field-by-field syntax tables. The ITU-T text itself was not read for
them.
Start codes
Every NAL unit is preceded by the three-byte start_code_prefix_one_3bytes,
00 00 01. Zero bytes may come before it, and encoders commonly write a
four-byte start code, 00 00 00 01, at the start of each access unit and
before parameter sets. Trailing zero bytes may follow a NAL unit before the next start
code. A reader finds NAL units by searching for 00 00 01, and treats any
zero bytes before it as padding, not as part of the previous NAL unit.
A NAL unit cannot end in a zero byte, so the zero bytes before a start code always belong to the framing.
Emulation prevention
So that a start code cannot appear inside a NAL unit, the encoder inserts an
emulation_prevention_three_byte, 03, whenever two zero bytes
would otherwise be followed by a byte of 03 or less.
| Payload bytes | Bytes in the stream |
|---|---|
00 00 00 | 00 00 03 00 |
00 00 01 | 00 00 03 01 |
00 00 02 | 00 00 03 02 |
00 00 03 | 00 00 03 03 |
A reader removes each 03 that follows two zero bytes, to get the payload
called the RBSP (raw byte sequence payload). The same rule applies if the payload ends
in zero bytes, so the last byte of a NAL unit is never zero.
Bit syntax
Inside a NAL unit, fields are packed most-significant-bit first with no alignment. The pages here use the standard’s coding names:
| Name | Coding |
|---|---|
u(n) | unsigned integer of n bits |
f(n) | fixed pattern of n bits |
ue(v) | unsigned Exp-Golomb code |
se(v) | signed Exp-Golomb code |
flag | 1 bit, shown as u(1) |
u(v) | unsigned integer whose width depends on earlier fields |
Exp-Golomb codes
An Exp-Golomb code is some number of zero bits, a one bit, then that same number of
bits of value. Reading n leading zeros gives ue(v) =
2n − 1 + the value of the next n bits:
| Bits | ue(v) |
|---|---|
1 | 0 |
010 | 1 |
011 | 2 |
00100 | 3 |
00101 | 4 |
00110 | 5 |
00111 | 6 |
0001000 | 7 |
A signed code maps the same numbers: 0, then +1, −1, +2, −2 and so on, so
ue value k gives (−1)k+1 × ⌈k ÷ 2⌉.
Trailing bits
An RBSP ends with rbsp_trailing_bits: a single 1 bit, then 0 bits up to
the next byte boundary. To find where the real data ends, a reader scans back from the
last byte to the last 1 bit. CABAC-coded slice data may be followed by extra
00 00 03 sequences to reach a required size.
Worked example
The start of a stream, with an SPS and a PPS:
00000000 00 00 00 01 67 64 00 28 AC D9 40 78 02 27 E5 84
00000010 00 00 00 01 68 EB E3 CB 22 C0
0000001A 00 00 01 65 88 84 …
Each unit’s first byte, highlighted, is its NAL
unit header: 67 is a sequence parameter set, 68 a picture
parameter set and 65 a slice of an IDR picture. After the header,
64 is profile 100, High, and 28 is level 4.0.