Shannon entropy explained for video: why frequent values get short codes and rare ones long codes, and how Huffman and arithmetic coding approach the limit.
In 1948 Claude Shannon defined 'information' mathematically. He turned a simple intuition into a formula: the rarer an event is, the more information the news of its occurrence carries.
Information of one event I(x) = -log2 P(x) [unit: bit]
Average information over everything (entropy)
H(X) = - Σ P(x) · log2 P(x)
Example) Coin toss (heads/tails 50% each)
H = -(0.5·log2 0.5 + 0.5·log2 0.5) = 1 bit
Example) Biased coin (heads 90%, tails 10%)
H = -(0.9·log2 0.9 + 0.1·log2 0.1) ≈ 0.469 bitEntropy H is the theoretical minimum average number of bits needed to represent this data losslessly. A biased coin (easy to predict) can be represented with far fewer than 1 bit. Predictability is compressibility.
The same goes for pixel values in video. In natural video, the probability that a residual is 0 is very high, and the probability of a large residual value is low. This lopsided probability distribution is exactly what entropy coding exploits.
Huffman can only give each symbol a whole number of bits, so unless the probability is a power of 1/2, some bits are wasted. Arithmetic coding can give a symbol a fractional number of bits, like 0.3 bits, so it gets much closer to the theoretical limit.
Huffman coding is a vending machine that takes only coins. Even if the price is $2.30, you can only pay in whole-dollar amounts like $2 or $3. Arithmetic coding is a card payment that can charge exactly $2.30. That's why the more extreme the probabilities get (say, a 99% chance of 0), the bigger arithmetic coding's advantage becomes.
H.264 supports both CAVLC (Huffman family) and CABAC (arithmetic coding). CABAC is 10–15% smaller on average, but it takes more computation, so early mobile chips sometimes used only CAVLC. From HEVC on, only CABAC remains — because hardware became fast enough.
The uncertainty, or average information, of a source. The theoretical lower bound for lossless compression.
Lossless coding that assigns whole-number bit lengths to probabilities.
Coding that can assign fractional bits, getting close to the theoretical limit.
Context-Adaptive Binary Arithmetic Coding. The main entropy coder since H.264.
Context-Adaptive Variable-Length Coding. H.264's Huffman-family entropy coder. Lighter than CABAC but less compact.
Why does arithmetic coding get closer to the theoretical limit than Huffman coding?