SOF0 vs SOF2 JPEG Markers: Key Differences

This article examines the structural and functional differences between the SOF0 and SOF2 markers within the JPEG file format. While both define the frame header and establish core dimensions and component specifications for an image, they instruct the decoder to use fundamentally different Discrete Cosine Transform (DCT) processes. The following sections outline their marker definitions, internal payload layouts, and how they alter the structure of the surrounding JPEG data stream.

Marker Byte Definition and DCT Mode

The primary identifier difference lies in the marker code itself:

Frame Header Payload Structure

At the binary level, the internal payload structure following both markers is identical in format. Both SOF0 and SOF2 segments contain the following sequence of fields:

  1. Segment Length (2 bytes): Total length of the marker segment, excluding the marker bytes.
  2. Data Precision (1 byte): Bit depth per sample (fixed at 0x08 for 8-bit baseline and standard progressive).
  3. Image Height (2 bytes): The vertical dimension of the image in pixels.
  4. Image Width (2 bytes): The horizontal dimension of the image in pixels.
  5. Number of Components (1 byte): Typically 1 for grayscale or 3 for YCbCr/RGB.
  6. Component Specifications (3 bytes per component):
    • Component ID (1 byte)
    • Horizontal and vertical sampling factors (1 byte, split into two 4-bit nibbles)
    • Quantization table destination selector (1 byte)

Because the payload syntax matches, a parser uses the marker byte itself (0xC0 versus 0xC2) to determine which decoding state machine to instantiate.

Structural Impact on the Bitstream

While the frame header payloads share the same structure, SOF0 and SOF2 mandate drastically different structures for the subsequent entropy-coded scan data: