Sub topic 15
Huffman Coding in DEFLATE and JPEG
This chapter assumes you already know canonical Huffman codes, length-limited codes, and how a code table is stored with the data. It shows where those ideas appear in two common formats. DEFLATE is used by ZIP, gzip, and PNG. JPEG is used for photographs.
In both formats, Huffman coding is the last stage of the compressor. An earlier stage turns the input into symbols that have skewed frequencies, and Huffman coding then gives the common symbols short codes.
DEFLATE
DEFLATE (RFC 1951) first runs LZ77, which replaces repeated text with (length, distance) back-references. It then Huffman-codes the result using two alphabets:
- Literal/length alphabet (0–285). Values 0–255 are literal bytes, 256 marks the end of a block, and 257–285 are match-length codes followed by extra bits.
- Distance alphabet (0–29). Each value names a range of distances and is followed by extra bits.
Each block chooses one of three modes. A stored block holds raw bytes with no compression. A fixed block uses Huffman tables defined in the specification. A dynamic block ships its own tables.
A dynamic block's tables are canonical, so the header sends only code lengths, capped at 15 bits. The code lengths are themselves Huffman-coded with a small alphabet:
| Symbol | Meaning |
|---|---|
| 0–15 | A literal code length |
| 16 | Repeat the previous length 3–6 times |
| 17 | Repeat length 0 for 3–10 symbols |
| 18 | Repeat length 0 for 11–138 symbols |
The lengths of this code-length code are capped at 7 bits. They are sent as 3-bit values in a fixed order that puts the commonly used entries (16, 17, 18, 0, 8, 7, …) first, so the unused entries at the end can be left off.
JPEG
Baseline JPEG converts each 8×8 pixel block into frequency coefficients and quantizes them, which leaves mostly zeros. The encoder never Huffman-codes the coefficient values directly:
- DC coefficient: the encoder codes the difference from the previous block's DC value. The Huffman symbol is the size category, meaning the number of extra bits that follow.
- AC coefficients: each symbol is one byte that packs a run of zeros (high 4 bits) and a size category (low 4 bits).
0x00means end of block and0xF0means a run of 16 zeros.
The tables are stored in a DHT marker (0xFFC4). A DHT entry holds 16 counts, one for each code length from 1 to 16 bits, followed by the symbols listed in code order. This is a canonical code, so a decoder rebuilds the codes like this:
def codes_from_dht(counts, symbols):
codes, code, k = {}, 0, 0
for length in range(1, 17):
for _ in range(counts[length - 1]):
codes[symbols[k]] = format(code, "0{}b".format(length))
code += 1
k += 1
code <<= 1
return codes
# Standard luminance DC table from JPEG Annex K
counts = [0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
symbols = list(range(12))
for sym, code in codes_from_dht(counts, symbols).items():
print(sym, code)
# 0 00, 1 010, 2 011, ... 11 111111110JPEG does not allow a code made entirely of 1 bits. Encoders can use the example tables from Annex K or build optimized tables for each image. Tools such as jpegtran -optimize do the second, which usually saves a few percent of the file size with no loss of quality.
Side-by-side
| Feature | DEFLATE | Baseline JPEG |
|---|---|---|
| Max code length | 15 bits | 16 bits |
| Table format | Run-length-coded lengths | 16 counts plus symbol list |
| Tables per stream | New per dynamic block | Up to 2 DC and 2 AC |
| Extra bits after symbol | Length and distance offsets | Coefficient magnitude |
Exercise
A DHT segment has counts [0, 2, 1, 0, ...] (all remaining counts zero) and symbols [5, 9, 3]. What codes do the three symbols get?
Show answer
The two 2-bit codes are 00 for symbol 5 and 01 for symbol 9. The counter then reaches 2 and shifts left to 4, so symbol 3 gets the 3-bit code 100.
Exercise
Why does DEFLATE Huffman-code the code lengths instead of sending each one as a plain 4-bit value?
Show answer
The literal/length alphabet has 286 entries, and many lengths are zero or repeat. Run-length symbols 16–18 plus a Huffman code over them make the header much smaller, which matters most for small blocks.