How Animated AVIF Encodes Keyframes and Delta Frames
Animated AVIF files rely on the underlying AV1 video compression standard to dynamically balance file size and visual fidelity by choosing between keyframes and delta frames. This article explains the technical mechanics behind this selection process, exploring how encoders detect scene changes, analyze temporal redundancy, enforce structural frame limits, and utilize rate-distortion optimization to decide whether a frame should be coded independently or in reference to prior frames.
The Foundation: AV1 Video Compression
Animated AVIF is not a traditional multi-frame image format like
basic GIF; it encapsulates an AV1 video bitstream inside an ISO Base
Media File Format (ISOBMFF) container. Because of this architecture,
frame-type decisions are handled directly by the AV1 video encoder (such
as libaom, rav1e, or SVT-AV1).
The encoder must categorize each frame into one of two fundamental
categories: an intra-frame (keyframe) or an inter-frame (delta
frame).
Scene Cut and Change Detection
The primary trigger for generating a keyframe is a scene cut. When the visual content changes abruptly—such as an angle shift or a completely new image sequence—the correlation between the current frame and the preceding frames drops sharply.
Encoders monitor this using metrics like the Sum of Absolute Differences (SAD) or the Sum of Absolute Transformed Differences (SATD). If the difference metric between adjacent frames exceeds a predetermined threshold, the encoder determines that referencing previous frames will require more data than simply storing a fresh image. Consequently, it marks the frame as a keyframe.
Rate-Distortion Optimization (RDO)
When a frame does not trigger a definite scene cut, the encoder utilizes Rate-Distortion Optimization (RDO) to determine the most efficient coding method. RDO calculates the mathematical trade-off between the number of bits required to encode the frame (rate) and the resulting visual quality loss (distortion).
During RDO analysis:
- Intra-prediction (Keyframe) evaluates the cost of encoding spatial details solely using data within the current frame.
- Inter-prediction (Delta frame) evaluates the cost of motion vectors (tracking where pixels moved from previous frames) combined with residual data (the visual differences remaining after motion compensation).
If the bit cost of storing motion vectors and residuals approaches or exceeds the cost of a full intra-coded frame, the encoder automatically chooses a keyframe. If motion compensation provides substantial data savings, it selects a delta frame.
Group of Pictures (GOP) Boundaries
Even in sequences with minimal motion, encoders do not produce delta
frames indefinitely. Encoders enforce keyframe placement based on Group
of Pictures (GOP) rules, defined by minimum and maximum keyframe
interval parameters (often configured via flags such as
--min-keyint and --max-keyint):
- Maximum Interval: Ensures that a keyframe is forced after a set number of frames (e.g., every 250 frames), preventing error propagation and ensuring the animation can be decoded, buffered, or scrubbed efficiently.
- Minimum Interval: Prevents the encoder from placing keyframes too close together during periods of noisy or chaotic motion, forcing the use of delta frames to preserve bandwidth.
Through this combination of scene detection thresholds, rate-distortion mathematics, and GOP structural rules, animated AVIF encoders precisely determine the optimal frame type to maximize compression without compromising playback performance.