Motion estimation and compensation
Nearby frames often hold the same content at slightly different positions. Motion estimation is the encoder's search of a reference picture for a useful match for each block; the answer is a motion vector. Motion compensation is the part both encoder and decoder do: build the prediction from the reference picture and the vector, with no search.
Why search at all
A good match can leave much less residual to code than an intra prediction would. The encoder sends how to get the prediction, here a vector into a reference picture the decoder already holds in reconstructed form, and codes the difference that remains. On this page the reference is the frame just before. The decoder never repeats the search; it only follows the vector.
Be the motion search
A ball has rolled across a lawn between two frames. Find a match for the outlined 16×16 block: move the vector with the sliders, or click a square of the map. The score is the sum of absolute differences (SAD): the absolute difference between each sample of the current block and of the reference block the vector points at, added up. The demo ranks by SAD alone; what the vector costs comes in the next section. The vector is in whole pixels and points from the block's own position to the reference block, so it is the reverse of the ball's travel.
- For this block, one vector stands out. SAD is 12,391 with no motion and 1,958 at (−5, 3), the lowest in the window.
- The best vector does not make the residual vanish. The lawn did not move with the ball, so the corners of the fetched block hold the wrong grass. One vector per block describes one motion.
- The map has more than one dip. (2, 3) scores 10,519, lower than each of the eight squares around it and far above the best. A search that tests only some of the 289 squares can settle in a dip like that one. The demo does not run any encoder's search; that is the subject of Motion search patterns.
Motion search in x264
x264 does not rank a candidate by distortion alone. The vector is coded as a difference from a predicted vector, which is often the median of the vectors of three neighboring blocks. So during the whole-pixel search a candidate's score is
search score = SAD + lambda × estimated bits(vector − predicted vector)
The estimate comes from a table that grows with the logarithm of the difference; it ranks candidates and is not the block's final coding cost. Lambda is the weight on a bit; see Rate-distortion optimization. A vector that agrees with its neighbors is cheap; a costlier one needs a correspondingly lower SAD. With tesa and --subme 2 or higher, the last candidates are compared by a transformed difference instead of SAD. The settings that shape the whole-pixel search:
| Setting | Medium preset | Other presets |
|---|---|---|
--me | hex | dia to tesa |
--merange | 16 | 24 in the two slowest |
--me picks the search pattern: dia in ultrafast and superfast, umh in slower and veryslow, tesa in placebo; esa is never a preset default. --merange sets the extent of the whole-pixel search, and what that means depends on the pattern. x264 accepts 4 to 1024, and for dia and hex it reduces anything above 16 to 16; smaller values stand. After the whole-pixel search comes refinement between pixels: see Sub-pixel interpolation.
Trade-offs
- The encoder wants a cheap block, not the true motion. It keeps the candidate with the lowest cost it tested. That often follows the real movement, and on repeating texture or flat areas it can point somewhere unrelated.
- A wider search costs time. Comparing every position within ±16 pixels, skipping none, is 1,089 block comparisons per block before any refinement, which is why patterns that test a few dozen exist.
- One vector per block models one slide. That is all this demo has. Rotation, zoom, a change of lighting, an object deforming, or two motions inside one block can then leave residual. Codecs also have smaller blocks and other prediction tools; see Block partitioning.
- Vectors are not free. At low bitrates the bits spent on vectors are a noticeable share of the total, which is the reason for the lambda term above. Not measured on this page.
Sources
Read 2026-11-13, at the commit linked.
- x264 at commit c24e06c (GitHub mirror):
BITS_MVDandCOST_MV, the logarithmic vector cost table, the median predictor inx264_mb_predict_mv, the whole-pixel measure: SAD, or a transformed difference fortesa, the--menames, the defaults, the presets, the--merangelimits. - Not checked: the search patterns themselves, how the predicted vector is formed beyond the median case, x265 and other encoders, bidirectional prediction, and how any codec writes vectors into the bitstream. The trade-offs are general reasoning, not measurements.