Sample adaptive offset
After quantization, some kinds of pixel are wrong in the same direction on average: the peaks of ringing beside an edge sit too high, the valleys too low. Sample adaptive offset (SAO), a filter in High Efficiency Video Coding (HEVC), sorts the reconstructed pixels of a region into a few classes and adds one small correction to every pixel of a class. The encoder measures the corrections against the source and sends them; the decoder only has to sort and add.
Sorting pixels by their neighbors
The edge offset compares each pixel with its two neighbors along one direction: horizontal, vertical or one of the two diagonals. Four shapes get an offset, and every other pixel is left alone:
With those signs the edge offset can only raise a low pixel or lower a high one. It is one value for a whole class, so it can carry a single pixel past its neighbors. The other type, the band offset, ignores neighbors: it splits the value range into 32 equal bands and sends offsets for four bands in a row, which can correct a shifted brightness in a smooth area.
Try the types on the cat
The cat is coded in 4×4 blocks at the quantizer step the quantization parameter (QP) nominally stands for. The demo isolates the offset stage: no deblocking runs first, and the whole picture is one SAO region. For each class it averages source minus reconstructed (the column "Mean error"), rounds that to a whole number, limits it to ±7 and, for edge classes, to the allowed sign. That is the offset. It is chosen to lower squared error with no charge for its bits; x265 also weighs what each offset costs to send. Error is the mean squared error against the source: the average of the squared pixel differences.
| Edge class | Pixels | Mean error | Offset |
|---|---|---|---|
For this cat at QP 34:
- The classes had different mean errors. Along the horizontal direction, class 1 pixels were 5.57 too low on average and class 4 pixels 5.65 too high, so the demo gave them offsets of +6 and −6. x265 could send a smaller offset, reuse a neighbor's parameters or send none, if that cost less. The other 642 pixels got no offset and averaged 0.67: slopes, flat neighborhoods and the 64 pixels of the first and last columns, which lack a neighbor (x265's filter skips those columns of the picture too).
- Every type lowered the error, by different amounts. From 42.50: vertical 34.96, horizontal 35.85, either diagonal 37.30, band 39.72.
- One offset serves a whole class. Class 3 averaged −0.51 and got −1, the nearest whole number. An offset corrects a class's average, not each pixel in it.
How x265 chooses
x265 runs SAO on each coding tree unit (CTU) after deblocking. For every type it adds up source minus reconstructed per class, rounds the mean, and in estIterOffset walks each offset toward zero, keeping the value with the lowest sum of error change and lambda times an estimate of the bits. A CTU can also reuse the parameters of the CTU to its left or above, or use no offset at all.
--saois on by default;--no-saoturns it off. The ultrafast and superfast presets and the grain and fast-decode tunings turn it off.--limit-saohas x265 try fewer types in B slices and for skipped blocks in P slices.
Trade-offs
- New offsets cost bits. A CTU that sends its own parameters sends a type, four offsets and, for the band type, a band position. Reusing a neighbor's parameters or using no offset costs less, which is why x265 weighs each choice with lambda.
- The encoder needs the source; the decoder does not. The deblocking filter decides from reconstructed pixels alone. SAO's offsets are measured against the source and sent.
- Lower error is not the same as a better-looking picture. The demo measures squared error for one invented picture. x265's grain tuning turns SAO off.
Sources
Read 2026-11-15, at the commit linked.
- x265 at commit 4191822: in
source/encoder/sao.h, the four edge types and the band type, 32 bands and the offset limit, 8 for 8-bit video; insource/encoder/sao.cpp, the class from two sign comparisons throughs_eoTable, the per-class sums, the rounding and the limit, the sign rule,estIterOffsetwith its bit estimate and its error change, the search for four bands, merging with the left or upper unit, the cost of no offset, tried first, the picture's first and last columns skipped by the horizontal edge filter and--limit-sao; insource/encoder/framefilter.cpp, deblocking before the SAO decision and the SAO filter; insource/common/param.cpp, the default,--sao, the presets ultrafast and superfast, the tunings grain and fast-decode,--sao-non-deblockand--selective-saowith its range; the options in the command-line list. - Not checked: the HEVC specification text (the page describes x265's code); chroma; how SAO statistics are gathered at CTU and picture edges and what
--sao-non-deblockand--selective-saochange; how often x265 chooses each type on real video; the comparable tools in AV1 and Versatile Video Coding.