encoding.info
You are hereSourcePredictionTransformEntropy codingFiltersFramesDecisionsRate controlPerceptionQualityBitstreamSettingsEncoder map
Page 2 of 4 in In-loop filters

Sample adaptive offset

In-loop filters · concept · added 2026-11-15

In Brief:
After quantization, some kinds of pixel are wrong in the same direction on average. Sample adaptive offset, in High Efficiency Video Coding, sorts pixels into a few classes and adds one small correction per class, which the encoder measures and sends.

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:

Class 1
below both neighbors
Class 2
below one, level with one
Class 3
above one, level with one
Class 4
above both neighbors
Conceptual illustration. The teal bar is the pixel being classified, the gray bars its two neighbors along the chosen direction. The arrow shows the only direction x265 lets that class's offset take: up for classes 1 and 2, down for classes 3 and 4. A pixel on a slope, or level with both neighbors, is in none of the four.

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.

34
← finer quantizercoarser quantizer →
Source
Reconstructed
With offsets
Changed
Edge classPixelsMean errorOffset
Pixels changed
Error before
Error after
Simplified demo: the cat is invented, the transform is a floating-point 4×4 cosine transform, every block is predicted as flat mid-gray, and no deblocking filter runs first. The classes, the rounding, the ±7 limit and the sign rule follow x265's code for 8-bit video. Two things differ from x265: it then lowers each offset step by step and keeps the one with the lowest rate-distortion cost, and it chooses the four bands by that cost; the demo keeps the rounded mean and chooses the bands that remove the most squared error. The cat is left-right symmetric, so the two diagonals give the same numbers. For the band type the table shows the four bands chosen.

For this cat at QP 34:

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.

Trade-offs

Sources

Read 2026-11-15, at the commit linked.