Variable-length versus arithmetic coding
A variable-length code spends whole bits on codewords, and gets below one bit per symbol only by letting symbols share a codeword. An arithmetic coder narrows one interval and spends fractions. This page puts the two side by side on the same flags, against the entropy that bounds them both.
Group the flags
Group the flags
A flag here is a symbol with two possible values, zero or nonzero. The demo prices 1,024 invented flags under a model that treats each flag as independent. The slider fixes how many are zero; a fixed shuffle fixes where. The first three readouts are bits per flag; the last counts codewords.
Grouped, expected is a variable-length code: a Huffman code built for the model, with one codeword for each possible group of flags. The readout is its expected length under the model. With one flag per codeword it is exactly one bit.
Arithmetic, counted runs an arithmetic coder's integer arithmetic on the flags shown, with their share of zeros as its fixed probability, and counts the bits it would write. Entropy is the bound for the model.
Measured:
- At 90%, grouping moved the expected length toward the entropy. With 2, 4 and 8 flags per codeword it is 0.644, 0.492 and 0.474 bits per flag, against an entropy of 0.468. The table grows from 4 codewords to 256.
- At 90%, the arithmetic coder's count was 480 bits for the 1,024 flags, 0.469 per flag, with no table of codewords. At every slider position the count was at most 2.0 bits above 1,024 times the entropy.
- At 99% the gap is wider. 8 flags per codeword has an expected 0.156 bits per flag; the entropy is 0.079 and the arithmetic coder's count 0.081.
The two coders in x264
x264 has both kinds. Its arithmetic coder, context-adaptive binary arithmetic coding (CABAC), is on by default. --no-cabac selects context-adaptive variable-length coding (CAVLC) instead, and so do the ultrafast preset, the fastdecode tuning and the Baseline profile.
| Encoder | Coder | Codes |
|---|---|---|
x264, --no-cabac | Variable-length | Whole codewords |
| x264, default | Arithmetic | Two-valued decisions |
| x265 | Arithmetic | Two-valued decisions |
| libaom | Arithmetic | Symbols of up to 16 values |
x265's Entropy::encodeBin is an arithmetic coder of the same kind as x264's, and its option list has no switch for another. libaom's od_ec_encode_cdf_q15 codes a symbol of up to 16 values in one step.
Trade-offs
- Grouping buys fractions of a bit with table size. In the demo, 8 flags per codeword needs 256 codewords and its expected length is still above the entropy.
- An arithmetic coder can use a different probability for every symbol. The demo's never changes. Context modeling is about choosing it.
- Both preserve the symbols they are given exactly. With the same prediction information and levels, the reconstructed picture is the same. Switching an encoder's coder can change its bit counts and so its decisions (x264's
rd_cost_mbcounts by a different path for each), so two complete encodes need not reconstruct identically.
Sources
Read 2026-11-14 and 2026-11-15, at the commits linked.
- x264 at commit c24e06c (GitHub mirror): the default, ultrafast, fastdecode, Baseline,
--no-cabac, the two counting paths inrd_cost_mb. - x265 at commit 4191822 (GitHub mirror):
Entropy::encodeBin, its range table lookup.source/common/param.cppandsource/x265cli.hwere searched for an entropy coder option and have none. - libaom at commit 1b7f9ef (GitHub mirror):
od_ec_encode_cdf_q15and its limit of 16 symbols. - Not checked: the H.264, HEVC and AV1 specifications, including which profiles allow which coder; that a Huffman code is the best code of whole codewords for known probabilities, which is standard theory and was not taken from a source read for this page; whether switching x264's coder changes its decisions on any given video, which was not tried; decoding speed; SVT-AV1.