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:

  1. 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 the 0xFFDA marker code.

  2. 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 typically 3. For a grayscale image or non-interleaved progressive scan, it can be 1. The maximum value is 4.

  3. 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.
  4. 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 to 0 (the DC coefficient).

  5. 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 to 63 (the final AC coefficient).

  6. 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 to 0x00.

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:

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.