How to Adjust libavif Speed and Quality Settings
When encoding AVIF images using the libavif library,
balancing compression speed and visual quality is crucial for optimizing
application performance. This article explains how applications
configure this tradeoff using specific parameters like the
speed setting, quality quantizers, and the choice of the
underlying AV1 codec.
The Speed Parameter
The primary control for managing the encoding time versus file size
tradeoff in libavif is the speed member of the
avifEncoder structure.
- Range: The
speedsetting accepts integers from0to10(defined by constantsAVIF_SPEED_SLOWESTtoAVIF_SPEED_FASTEST). - Effect:
- Lower values (0–3): Instruct the encoder to use highly exhaustive search algorithms for motion estimation, intra prediction, and partition selection. This results in the smallest possible file sizes and highest quality, but takes the longest time to encode.
- Default value (6): Provides a balanced default that offers decent compression efficiency with reasonable encoding times.
- Higher values (8–10): Enable fast presets that skip complex encoding tools. This dramatically speeds up the encoding process but results in larger file sizes for the same target quality.
In C, this is configured directly on the encoder instance:
avifEncoder * encoder = avifEncoderCreate();
encoder->speed = 6; // Set speed to the default balanced presetConfiguring Target Quality
Speed settings do not work in isolation; they dictate how efficiently
the encoder achieves a target quality. Quality in libavif
is typically configured using the quality parameter
(ranging from 0 to 100, where 100
represents lossless compression).
encoder->quality = 80; // Set main image quality (0-100)
encoder->qualityAlpha = 80; // Set alpha channel quality (0-100)Alternatively, applications can directly control the quantization
parameters using minQuantizer, maxQuantizer,
minQuantizerAlpha, and maxQuantizerAlpha.
These values range from 0 (lossless, highest quality) to
63 (worst quality).
encoder->minQuantizer = 0; // AVIF_QUANTIZER_LOSSLESS
encoder->maxQuantizer = 30; // Caps the worst allowed qualityHow the Speed and Quality Tradeoff Works
When an application increases the speed value, the
encoder spends less CPU effort searching for optimal data-saving
patterns. As a result: * To maintain a fixed quality
(constant quality setting), a faster speed will yield a
larger file size. * To maintain a fixed file
size (constant bitrate/target size), a faster speed will yield
lower visual quality.
The Influence of Underlying AV1 Codecs
Because libavif is a container wrapper, the actual
encoding is performed by an underlying AV1 codec, such as AOM
(libaom), SVT-AV1, or rav1e.
The speed value configured in libavif is
mapped directly to the speed presets of these underlying encoders:
- AOM (libaom): The industry standard for still
images. Its speed settings map directly to libaom’s CPU-used parameters.
Settings lower than
4are extremely slow but offer superior quality. - SVT-AV1: Highly optimized for multi-threaded performance. It handles higher speed settings exceptionally well and is ideal for server-side, real-time image generation.
- rav1e: Written in Rust, it provides a safe and predictable speed-to-quality mapping across its ranges.