JPEG Start of Scan SOS Marker Payload Structure
The Start of Scan (SOS) marker, designated by the byte sequence
0xFFDA, is a critical structural element in a JPEG file
that marks the end of the header metadata and the beginning of the
compressed image stream. Its payload specifies which color components
are included in the upcoming scan, assigns the entropy coding (Huffman
or arithmetic) tables to each component, and defines parameters for
progressive or sequential decoding. Following this control data, the
marker directly precedes the raw entropy-coded bitstream that represents
the actual visual image.
SOS Marker Segment Structure
The payload of the SOS marker segment immediately follows the
0xFFDA marker and consists of the following structured
fields:
Scan Header Length (\(Ls\)) – 2 Bytes
Specifies the total length of the SOS marker segment in bytes, including the two length bytes themselves, but excluding the0xFFDAmarker code.Number of Components in Scan (\(Ns\)) – 1 Byte
Indicates the number of image components included in this particular scan. For a standard baseline interleaved color scan (YCbCr), this value is typically3. For a grayscale image or non-interleaved progressive scan, it can be1. The maximum value is4.Component Specification Parameters – 2 Bytes per Component (\(2 \times Ns\) Bytes)
For each component specified by \(Ns\), a two-byte sequence defines:- Component Identifier (\(Cs_j\)) – 1 Byte: Identifies the specific image component (e.g., Luminance Y, Chrominance Cb, or Chrominance Cr). This value maps directly to the component identifiers previously established in the Start of Frame (SOF) marker.
- Entropy Coding Table Selectors (\(Td_j\) and \(Ta_j\)) – 1 Byte: A split 8-bit
field where:
- The upper 4 bits define the DC entropy coding table destination selector (0 to 3).
- The lower 4 bits define the AC entropy coding table destination
selector (0 to 3).
These selectors point to the Huffman or Arithmetic tables registered earlier via Define Huffman Table (DHT) or Define Arithmetic Coding (DAC) markers.
Spectral Selection Start (\(Ss\)) – 1 Byte
In progressive scans, this byte denotes the first Discrete Cosine Transform (DCT) coefficient position in the zig-zag sequence covered by this scan (values 0 to 63). For baseline sequential JPEG scans, this is always set to0(the DC coefficient).Spectral Selection End (\(Se\)) – 1 Byte
In progressive scans, this byte denotes the final DCT coefficient position covered by the scan. In standard baseline sequential JPEG scans, this is always set to63(the final AC coefficient).Successive Approximation Bit Positions (\(Ah\) and \(Al\)) – 1 Byte
Used exclusively for progressive scans to indicate bit-position shifts.- The upper 4 bits (\(Ah\)) represent the point transform used in the preceding scan (or zero on the initial scan).
- The lower 4 bits (\(Al\)) represent
the point transform for the current scan.
In baseline sequential JPEGs, this byte is always set to0x00.
The Compressed Image Data Stream
Immediately following the parameter payload of the SOS marker segment, the JPEG file transitions into the Entropy-Coded Scan (ECS) data. While technically following the marker segment rather than being enclosed in its length count, this payload consists of:
- Variable-length bitstream data encoding the quantized DCT coefficients using the designated tables.
- Byte Stuffing: Any occurrence of a raw
0xFFbyte generated during entropy coding is immediately followed by a0x00byte (byte-stuffing) to prevent decoders from mistakenly identifying it as a JPEG control marker. - Restart Markers: Optional periodic markers
(
0xFFD0through0xFFD7) embedded to allow decoder recovery in case of data corruption.
The scan data continues until the decoder encounters a non-stuffed
marker, typically another SOS marker in progressive files or the End of
Image (EOI, 0xFFD9) marker.