Principle · How it works

How video compression uses other frames

See how an encoder reuses an earlier picture and records the corrections needed to rebuild the next one.

Reuse a picture the decoder already has

An encoder can copy a block from a reconstructed reference picture and send its correction. The copy is the prediction; the correction is the residual. Aligning the copy is motion compensation. The decoder makes the same copy and adds the decoded residual.

The image-compression example explains the DCT (discrete cosine transform) and coefficient rounding. Here that stage codes a correction rather than a whole picture.

Start at frame 0: an independent I key frame, coded without another picture. Select frame 1 to see a P predicted frame: it uses the reconstructed frame 0 as its reference. Frame 2 uses reconstructed frame 1, and so on; deltas are not all measured from the key frame. With residual rounding off, reconstruction matches the input.

Predict a moving sequence

Now = selected frame; Ref = its reference. Underlined frames form the dependency chain from the key frame.

The moving comparison needs JavaScript. The figures in this article show the prediction and frame dependencies.

Read the connected pictures

Select frame 1, then click a block in Input. The solid outline on each reference marks the source copied into Prediction; the dashed outline marks the destination at the same coordinates as the input block. Coordinates are in pixels, increasing rightwards and downwards. R is the top-left red-channel code value of the selected block, not a measure of physical light.

Follow the subtraction to the signed delta, then its transform and rounding to the coefficient data. The decoder reverses that coding and adds its own prediction. The reference ID identifies an already decoded picture; that picture and Prediction are not resent as extra images. The matrix below the pictures shows the selected component before entropy coding, not file bytes.

On the frame strip, Now marks the selected frame and Ref its immediate reference. Underlines trace the dependency chain. Frame 8 starts a fresh chain in this model. On narrow screens, scroll the same diagram horizontally to retain its connections; keyboard users can focus the diagram and use arrow keys.

Test what changes the correction

Compare Previous decoded frame · no motion with Motion-compensated decoded frame during the pan. Alignment leaves a smaller residual. Brighter and darker preview values mean positive and negative corrections; grey is zero. The newly revealed right edge still needs correction.

With Colour test chart · moving patch, the patch reveals background that was hidden in the reference. With Scene cut, compare frames 5 and 6: the colour test chart is replaced by a separate procedural target, so the residual grows. The fixed independent refresh occurs later, at frame 8.

The Residual coefficient step Δ rounds those transform coefficients. Zero bypasses rounding. Increase the step to see coefficients become zero and the reconstruction change. Frame selection and parameter changes pause playback. While a new sequence is calculated, the Shown caption and all pictures retain the last completed settings together. The readouts report signed-residual magnitude, coefficient counts and RGB reconstruction error; they are not file size or perceptual-quality scores.

A motion vector is an address, not an object trajectory

The encoder searches a reference picture for a useful match to a current block. The motion vector tells the decoder which displacement to use. In our coordinate convention, (+2, 0) reads two pixels to the right in the reference, so a pattern that moved left can have a positive horizontal lookup vector. Coordinates increase rightwards and downwards.

A vector of plus two reads two columns to the right in the referenceOne row of eight current samples, at columns zero through seven, matches columns two through nine of the reference row. The outlined groups have identical samples. An arrow from the current block to its reference source marks the lookup, not the direction the pictured object travelled. The pattern moved left by two pixels; the reference-coordinate vector is plus two.Decoded reference · one row0123456789101101234567Lookup vector: (+2, 0) pixelsRead reference column x + 2.Current block · the same rowThe pictured pattern moved left.
Scroll horizontally to inspect the full diagram. Keyboard: focus the diagram and use the arrow keys.
The vector points to the source in the reference picture. Motion compensation copies samples from there to predict the current block.

The vector need not follow a physical object. Repeated textures can supply several matches, and an encoder may choose a slightly worse match that costs fewer bits to describe. Real codecs can use multiple references, different block sizes and fractional-pixel interpolation. The decoder receives the chosen instructions; it does not repeat the encoder’s search.

The encoder must use the decoder’s references

The encoder reconstructs each coded frame and stores it for prediction. When a codec uses in-loop filtering, the stored reference includes that filtering. Using the original instead would give the encoder detail the decoder has lost.

Choose Original reference at encoder · mismatch with a non-zero residual step. Different predictions now introduce error that can carry through later frames. Frame 8 breaks that dependency by refreshing independently.

For a sample of 85, an encoder prediction of 80 gives residual +5. If the decoder predicts 70, adding the same +5 produces 75.

Lossy residual coding can change quality even with matching references. Packet loss and concealment belong to the decoder’s separate failure handling.

I, P and B describe prediction dependencies

An I (intra) picture uses no other picture for prediction. It can still use within-picture prediction and lossy coefficient coding. P (predictive) pictures permit inter prediction using one reference list; B (bi-predictive) pictures permit two lists and can combine two predictions. P and B pictures can also contain intra-coded blocks. These are conventional picture-level names; H.264 signals prediction types at slice level.

A B picture is not necessarily predicted from one earlier and one later picture in display time, and it is not necessarily disposable. Some B pictures are themselves references. Here B1 and B2 use I0 and P3 and are not themselves references.

For example, a patch of wall hidden in I0 may be visible in P3. A block in B1 can use that later picture to predict the wall, leaving less residual to code. Other blocks can use I0 or combine the two predictions.

Display I0 B1 B2 P3; first decode the references I0 and P3In this example I0 needs no other picture. P3 uses I0. Both B1 and B2 use I0 and P3. Display order is I0, B1, B2, P3. One valid decode order is I0, P3, B1, B2. The decoder holds P3 until its display time. B1 and B2 are not used as references in this example; other structures can use reference B pictures.Display order · arrows run from reference to dependent pictureI0B1B2P3One valid decode orderI0P3B1B2
Scroll horizontally to inspect the full diagram. Keyboard: focus the diagram and use the arrow keys.
Upper arrows show reference dependencies; lower arrows show a valid decode sequence. Picture numbers describe display time, so P3 can already be decoded when B1 needs it.

P3 must be decoded before B1 and B2 can use it, even though it appears later on screen. The decoder buffers pictures and presents them in display order. Waiting for a later-display reference can add delay. A low-delay encoder can avoid this particular reordering; motion prediction does not inherently require future pictures.

A refresh point and a bit budget solve different problems

In this model I0 and I8 break all earlier dependencies. Real codecs distinguish an intra picture from a complete refresh boundary.

A group of pictures, or GOP, describes a coding structure over a run of pictures. An I picture does not by itself promise that later pictures will stop referring to earlier pictures. In H.264 an IDR (instantaneous decoding refresh) picture establishes a stronger boundary: pictures following it in decode order do not depend on references before it. Parameter sets are still needed to interpret the stream. Other codecs define their own random-access and refresh rules.

More frequent refreshes improve access but usually cost more bits. Scene cuts often favour intra coding; the encoder chooses where to refresh.

Bitrate is the number of encoded bits per second. Rate control allocates that bit budget across pictures. Difficult motion, noise or a scene cut may require more residual data. Under a constrained bit budget, an encoder can spend extra bits there or increase quantisation. Constant bitrate does not mean constant quality; constant quantisation does not mean constant bitrate. A poor motion match alone does not force a visible artifact if the coded residual corrects it accurately.

Where you have seen these choices

H.264/AVC and H.265/HEVC camera recordings, streamed video and video calls use inter prediction in common delivery configurations. AV1 also uses reference pictures and motion compensation, with its own syntax and reference structure. Editing configurations can be all-intra. Motion JPEG always codes pictures independently. An MP4 container does not specify these coding choices.

Experiment boundaries

The model fits the colour test chart into a 160×120 bitmap (120×120 image with black side margins), then crops it into 12 RGB8 frames of 128×80. Camera motion is a two-pixel pan per frame; the added patterned patch moves four pixels per frame. The scene cut switches to a separate procedural target. Playback is slowed for inspection. The residual preview adds 128 and clips extreme differences for display; calculations retain the signed values.

Each 8×8 block searches integer vectors in a horizontal range of −4…+4 and a vertical range of −2…+2 pixels. It compares every second row and column (one quarter of the pixels), using all three RGB absolute differences. Ties prefer the smallest sum of absolute horizontal and vertical displacements. Reference lookup repeats edge samples beyond the picture boundary. Every RGB residual component uses the existing orthonormal 8×8 DCT and uniform coefficient rounding, then inverse transform, add prediction, round and clip to RGB8 before storing the reference. The independent pictures use a fixed value of 128 rather than real intra prediction. The model uses I/P pictures, integer vectors and RGB components, without in-loop filters, entropy coding or rate control. Frame 8 is a fixed refresh, not scene-cut detection. The B-picture figure describes a separate structure.

For one reference picture, the block lookup and residual are:

Here t is the frame index, f an input picture, a hat marks reconstructed samples, p the prediction and r the signed residual.

Before final rounding and clipping:

C applies the final sample rounding and clipping; in the experiment it produces RGB8 values. The tilde marks the sum before that step. Its error separates reference mismatch from residual-coding error. With matching references the first term is zero.

References