Summary: This module gives a clever method of coding, JPEG entropy coding.
The entropy plots of the Quantisation of the DCT coefficients show the theoretical entropies of each DCT sub-band. In practise this would be a poor way to code the data because:
The code is applied to each block of
Each block of
The first coefficient (0) of each block (vector) is the DC coef, which represents the mean value of the pels in the block (see the top left basis function in subfigure (a) from previous discussion).
The DC coefs still exhibit significant local correlations (top left of subfigure (b)), so differential coding is used in which the value to be coded is the difference between the current DC coef and that of the previous block - the blocks are scanned from left to right, row by row. The first block in each row is coded with respect to zero.
The histogram of entropies of the DC coef differences is compared in Figure 1 with that of the raw DC coefs from this previous figure. We note the histogram peak around zero and see that the entropy is reduced from 6.42 bits to 6.07 bits.
![]() |
The size of the differences can in theory be up to
| DC Coef Difference | Size | Typical Huffman codes for Size | Additional Bits (in binary) |
|---|---|---|---|
| 0 | 0 | 00 | - |
| -1,1 | 1 | 010 | 0,1 |
| -3,-2,2,3 | 2 | 011 | 00,01,10,11 |
| -7,…,-4,4,…,7 | 3 | 100 | 000,…,011,100,…111 |
| -15,…-8,8,…,15 | 4 | 101 | 0000,…,0111,1000,…,1111 |
| ⋮ | ⋮ | ⋮ | ⋮ |
| -1023,…-512,512,…,1023 | 10 | 1111 1110 | 00 0000 0000,…,11 1111 1111 |
| -2047,…-1024,1024,…2047 | 11 | 1 1111 1110 | 000 0000 0000,…,111 1111 1111 |
Only Size needs to be Huffman coded in the above scheme, since, within a given Size, all the input values have sufficiently similar probabilities for there to be little gain from entropy coding the Additional Bits (hence they are coded in simple binary as listed). Each coded Size is followed by the appropriate number of Additional Bits (equal to Size) to define the sign and magnitude of the coefficient difference exactly.
There are only 12 Sizes to be Huffman coded, so specifying the code table can be very simple and require relatively few bits in the header.
In JPEG all Huffman code tables are defined in the image
header. Each table requires
The first 16 bytes list the number of codewords of each length
from 1 to 16 bits (codewords longer than 16 bits are
forbidden). The remaining
Hence
The remaining 63 coefs (the AC coefs) of each 64-element vector usually contain many zeros and so are coded with a combined run-amplitude Huffman code.
The codeword represents the run-length of zeros before a non-zero coef and the Size of that coef. This is then followed by the Additional Bits which define the coef amplitude and sign precisely. Size and Additional Bits are defined just as for DC coefs.
This 2-dimensional Huffman code (Run, Size) is efficient because there is a strong correlation between the Size of a coef and the expected Run of zeros which precedes it - small coefs usually follow long runs; larger coefs tend to follow shorter runs. No single 2-D event is so probable that the Huffman code becomes inefficient.
In order to keep the code table size
EOB is transmitted after the last non-zero coef in a 64-vector. It is the most efficient way of coding the final run of zeros. It is omitted in the rare case that the final element of the vector is non-zero.
ZRL is transmitted whenever
(ZRL) (4,3) 010
When the code tables are defined in the image header, each
codeword is assigned to a given (Run,Size) pair by making the
decoded output byte Code Byte equal to (
The default JPEG code for (Run,Size) of AC luminance DCT coefficients is summarised below in order of decreasing code probability:
| (Run,Size) | Code Byte (hex) | Code Word (binary) | (Run,Size) | Code Byte (hex) | Code Word (binary) |
|---|---|---|---|---|---|
| (0,1) | 01 | 00 | (0,6) | 06 | 1111000 |
| (0,2) | 02 | 01 | (1,3) | 13 | 1111001 |
| (0,3) | 03 | 100 | (5,1) | 51 | 1111010 |
| (EOB) | 00 | 1010 | (6,1) | 61 | 1111011 |
| (0,4) | 04 | 1011 | (0,7) | 07 | 11111000 |
| (1,1) | 11 | 1100 | (2,2) | 22 | 11111001 |
| (0,5) | 05 | 11010 | (7,1) | 71 | 11111010 |
| (1,2) | 12 | 11011 | (1,4) | 14 | 111110110 |
| (2,1) | 21 | 11100 | ⋮ | ||
| (3,1) | 31 | 111010 | (ZRL) | F0 | 11111111001 |
| (4,1) | 41 | 111011 | ⋮ |
As an example, let us code the following
(4) -13 (0,2) -3 (0,3) 6 (2,2) 2 (3,1) -1 (ZRL) (1,1) 1 (EOB)
101 0010 01 00 100 110 11111001 10 111010 0 11111111001 1100 1 1010
The compressed bitstream for this block is listed on the lower line, assuming that the default Huffman code tables, given above, are used.
Figure 2 shows the histogram of
probabilities for the (Run,Size) codewords used to code Lenna
using the
Figure 3 shows the equivalent
histogram when the quantisation matrix is
![]() |
![]() |
Note the strong similarity between these histograms, despite
the fact that Figure 3
represents only
It turns out that the (Run,Size) histogram remains relatively constant over a wide range of image material and across different regions of each image. This is because of the strong correlation between the run lengths and expected coef sizes. The number of events per block varies considerably depending on the local activity in the image, but the probability distribution of those events (except for EOB) changes much less.
Figure 2 and Figure 3 also give the mean bit rates to code Lenna for the two quantisation matrices. Comparing these with the theoretical entropies from this figure (lower row) we get:
| Q matrix | Mean Entropy (b/pel) | JPEG Bit Rate (b/pel) | JPEG efficiency |
|---|---|---|---|
|
|
0.8595 | 0.8709 | 98.7% |
|
|
0.5551 | 0.5595 | 99.21% |
Hence we see the high efficiency of the (Run,Size) code at two quite different compression factors. This tends to apply over a wide range of images and compression factors and is an impressive achievement.
There is even very Little efficiency lost if a single code
table is used for many images, which can avoid the need to
transmit the