Context modeling
An entropy coder is only as good as the probabilities it is given, and context modeling is how a codec supplies them. In the scheme shown here, each value is sorted into a context by what kind of value it is and what was coded nearby. Choosing the context says which probability estimate to use; adaptation changes that estimate after each value coded with it. The decoder keeps the same estimates, because it has decoded the same values.
Neighbors predict a flag
A flag here is a symbol with two possible values, zero or nonzero. This page's flags are the cat's: one per pixel, zero where the pixel equals the pixel to its left.
Over the whole picture 69% of the flags are zero, and a coder that knows only that needs 0.89 bits per flag. Sorted by their two neighbors, the flags fall into four groups with very different shares, so a coder with one estimate per group can price each flag closer to what it is worth. These percentages summarize the finished picture. The demo does not give them to the decoder.
Choose what the coder knows
Model chooses how the same 1,024 flags are priced, row by row. The fixed probability is the picture's own share of zeros, as if the decoder had been told it. An adapting probability starts at 50% and learns; with neighbor contexts there are four of them, one per tile. For each flag the model picks the context from flags already coded, prices the flag with the estimate as it stands, and only then moves that estimate toward the flag:
estimate += (target − estimate) / N
target = 100% after a zero,
0% after a nonzero
Adaptation sets the fraction 1/N. At 1/16, an estimate of 50% that sees a zero becomes 53.1%. The slider is switched off for the fixed model.
For this cat:
- Adapting beat the fixed share. One fixed probability costs 0.890 bits per flag. One adapting probability at 1/16 costs 0.821.
- Contexts lowered it further. A probability per neighbor context at 1/16 costs 0.618 bits per flag, 632 bits against 912 for the fixed share.
- Of the six settings, 1/16 gave the lowest modeled cost. With contexts it is 0.670 at 1/4, 0.618 at 1/16 and 0.691 at 1/128. Faster updates react more strongly to each flag; slower ones take longer to learn.
The fixed-share figure treats every flag as having the same probability, independent of the rest. The cat's flags are not like that: earlier flags give clues about later ones. Adapting, and choosing a context by the neighbors, use some of those clues, so the modeled cost can fall below the fixed-share figure without contradicting it.
Contexts in x264
x264's arithmetic coder keeps one byte per context: one of 64 probability states and which of the two values is currently the likely one. After each context-coded decision the byte is replaced from a table, x264_cabac_transition, which usually moves the estimate toward the value just coded. Repeated likely values stop at the strongest adapting state, and at even odds, coding the value that is not marked as likely swaps which one is. There are 460 contexts, or 1,024 for 4:4:4 video.
At the start of every slice each context is set from the slice's quantization parameter (QP):
initial value = clip(((m × QP) >> 4) + n, 1, 126)
with m and n from tables: one set for intra slices and three for the others, chosen by the library parameter cabac-idc. x264 converts the result into the state and the likely value.
Some coded-block flags, which say whether a block has any nonzero levels, use a similar four-way context from the blocks to the left and above. The base context and the handling of a missing neighbor depend on the block type. The flag for whether one coefficient of a 4×4 block is nonzero uses the coefficient's position in the scan.
libaom adapts by a rule like the demo's: update_cdf moves a two-valued symbol's probability 1/16 of the way at first, 1/32 once the context has coded 16 symbols and 1/64 after 32.
Trade-offs
- More contexts fit more closely, and each learns from fewer values. Here the Both context saw 176 of the 1,024 flags.
- Fast adaptation follows change and overreacts to noise. Another picture could favor a different rate from this one's 1/16.
- Coding becomes sequential, and a value's cost depends on what came before it. x264 sets every context afresh at each slice. Estimating bits and trellis quantization both have to allow for the dependence.
Sources
Read 2026-11-14 and 2026-11-15, at the commits linked.
- x264 at commit c24e06c (GitHub mirror): the context bytes, how a byte packs state and likely value,
x264_cabac_encode_decision_c,x264_cabac_transition, the context count, the starting state, the four table sets,x264_cabac_context_init, its call when a slice is written, thecabac-idcparameter, its default of 0, its range of 0 to 2,cabac_cbf_ctxidxinc, its rule for a missing neighbor of a chroma block, its general case, the significance flag.x264.cwas searched for a--cabac-idccommand-line option and lists none. - libaom at commit 1b7f9ef (GitHub mirror):
update_cdf, its rate. - Not checked: the H.264 specification's context tables and state numbering; how the contexts for the other decisions are chosen; x265's contexts; libaom's starting distributions and when it resets them; SVT-AV1. That x264's strongest adapting state holds and that its even-odds state swaps the likely value were read off its transition table for this page.