JPEG Define Huffman Table DHT Marker Explained
The Define Huffman Table (DHT) marker is a fundamental metadata
segment in a JPEG file stream, designated by the byte sequence
0xFFC4. This article explains the primary function of the
DHT marker, outlines its internal binary structure, and describes how
JPEG decoders rely on it to reconstruct compressed visual data from
variable-length entropy codes.
The Purpose of the DHT Marker
In baseline JPEG compression, image data undergoes Discrete Cosine Transform (DCT) and quantization before being losslessly compressed using Huffman coding. Huffman coding replaces frequently occurring numerical values with shorter bit sequences and rarer values with longer bit sequences.
Because these variable-length codes are written sequentially as a continuous stream of bits, a decoder cannot determine where one value ends and the next begins without a translation map. The DHT marker supplies this exact map. It defines the mapping between the variable-length bit sequences in the image stream and the original quantized coefficients.
Structural Breakdown of the DHT Marker
A DHT segment contains specific fields that allow a decoder to build the necessary decoding trees:
- Marker Identifier (
0xFFC4): The two-byte signature that signals the start of a Huffman table definition segment. - Payload Length: A two-byte integer indicating the total length of the DHT segment, including the length bytes themselves but excluding the marker bytes.
- Table Information Byte: An 8-bit field divided into
two 4-bit nibbles:
- Table Class (Upper 4 bits): Indicates whether the
table is for DC coefficients (
0) or AC coefficients (1). DC coefficients represent the average block brightness, while AC coefficients represent higher-frequency texture and detail. - Destination Identifier (Lower 4 bits): Assigns a numerical ID (typically 0 to 3) to the table. This allows different color components (such as luminance \(Y\) and chrominance \(Cb/Cr\)) to reference distinct Huffman tables.
- Table Class (Upper 4 bits): Indicates whether the
table is for DC coefficients (
- Code Length Counts (16 bytes): An array of 16 individual bytes. Each byte specifies the number of distinct Huffman codes of a given bit length, ranging from 1 to 16 bits.
- Symbol Values: A list of bytes representing the actual symbols assigned to the generated codes. The total number of symbols matches the sum of the 16 code length count bytes.
How Decoders Utilize DHT Data
JPEG relies on canonical Huffman coding. Because canonical Huffman codes follow strict sequential ordering rules, an encoder does not need to store the entire binary tree explicitly.
Instead, the decoder reads the 16 count bytes from the DHT marker to determine the exact length and numerical value of each prefix code. It then reads the symbol list to map each code to its corresponding coefficient value. Once this tree is reconstructed in memory, the decoder parses the incoming Entropy-Coded Segment (ECS) bit by bit, matching variable-length bit patterns to the symbols defined by the DHT marker and restoring the quantized DCT blocks.