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.

In C, this is configured directly on the encoder instance:

avifEncoder * encoder = avifEncoderCreate();
encoder->speed = 6; // Set speed to the default balanced preset

Configuring 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 quality

How 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: