A JPEG file is a run of segments, and each segment begins with a marker. The markers and what each segment holds are defined in Annex B of ITU-T T.81 (ISO/IEC 10918-1). Every number in a JPEG file is big-endian.
Markers
A marker is two bytes: FF, then a code that is neither 00 nor
FF. Any marker may be preceded by any number of extra
FF bytes, called fill bytes.
| Code | Name | What it is | Segment body |
|---|---|---|---|
C0 | SOF0 | Frame header: baseline DCT | Length and frame header |
C1 | SOF1 | Frame header: extended sequential DCT | As SOF0 |
C2 | SOF2 | Frame header: progressive DCT | As SOF0 |
C3 | SOF3 | Frame header: lossless (sequential) | As SOF0 |
C4 | DHT | Define Huffman tables | Length and Huffman tables |
C5–C7 | SOF5–SOF7 | Frame headers: differential (hierarchical), Huffman coding | As SOF0 |
C8 | JPG | Reserved for JPEG extensions | Not defined |
C9–CB | SOF9–SOF11 | Frame headers: extended, progressive and lossless, arithmetic coding | As SOF0 |
CC | DAC | Define arithmetic coding conditioning | Length, then pairs of bytes |
CD–CF | SOF13–SOF15 | Frame headers: differential (hierarchical), arithmetic coding | As SOF0 |
D0–D7 | RST0–RST7 | Restart markers, inside entropy-coded data | None: the marker stands alone |
D8 | SOI | Start of image | None |
D9 | EOI | End of image | None |
DA | SOS | Start of scan | Length, scan header, then entropy-coded data |
DB | DQT | Define quantisation tables | Length and tables |
DC | DNL | Define number of lines | Length and 2-byte line count |
DD | DRI | Define restart interval | Length and 2-byte count of MCUs |
DE | DHP | Define hierarchical progression | As a frame header |
DF | EXP | Expand reference components | Length and 1 byte |
E0–EF | APP0–APP15 | Application data | Length and data: see application segments |
F0–FD | JPG0–JPG13 | Reserved for JPEG extensions | Not defined |
FE | COM | Comment | Length and text |
01 | TEM | Temporary private use in arithmetic coding | None |
02–BF | RES | Reserved | Not defined |
Segments
All markers except SOI, EOI, RSTn and TEM are followed by a 2-byte length. It counts itself and the rest of the segment, but not the marker, so its smallest value is 2.
0MarkerFFand a code2 bytes2Length big-endian, including these 2 bytes2 bytes4Body as the code sayslength − 2 bytes
Order in a file
The file starts with SOI and ends with EOI. Frame headers, tables and scans come between them in this order; tables can appear anywhere before the scan that first uses them, and they stay in force until replaced.
…SOI2 bytes…Application data, comments, tables APPn, COM, DQT, DHT, DRIvaries…Frame header SOF0, SOF1, SOF2 or another SOFnvaries…Tables DQT, DHT, DRI, as needed before each scanvaries…Scans each one SOS and its entropy-coded data; DNL may follow the firstvaries…EOI2 bytes
A file made only of tables, with no frame, is called an abbreviated format; it is how some programs store shared tables for a set of images.
The bytes after a scan
Unlike other segments, a scan has no length that covers its data. To find its end, read
the bytes after the scan header until a marker that is not a restart marker. A
FF byte that belongs to the data is written FF 00; see
Scan.