libavif Multithreading: Speeding Up AVIF Encoding

This article provides an overview of how the libavif library leverages multithreading to accelerate the computationally expensive process of encoding AVIF images. It covers the delegation of threading to underlying AV1 encoders, the utilization of tile-based parallel processing, and the implementation of row-based multithreading to optimize CPU usage.

Delegating to AV1 Encoders Because AVIF is an image format based on the AV1 video compression standard, libavif does not perform the heavy compression math itself. Instead, it acts as a wrapper around AV1 encoders such as libaom, rav1e, or SVT-AV1. When you enable multithreading in libavif, it passes these threading parameters directly to the underlying AV1 codec, instructing it to distribute the workload across multiple CPU cores.

Tile-Based Parallelism One of the primary ways libavif speeds up encoding is through AV1 “tiles.” An image can be split into a grid of independent rectangular regions called tiles. Since these tiles do not depend on neighboring tiles for spatial prediction, the encoder can assign different threads to process multiple tiles simultaneously. For example, dividing an image into a 2x2 grid of tiles allows up to four threads to encode the image at the same time, significantly reducing overall encoding time.

Row-Based Multithreading (Row-MT) While tiling is highly effective, it can sometimes degrade compression efficiency because block predictions cannot cross tile boundaries. To solve this, codecs like libaom support Row-Based Multithreading. Row-MT allows threads to work on different rows of pixel blocks within the same tile or image simultaneously. By introducing a slight delay between the processing of consecutive rows—ensuring top and top-right blocks are processed first—the encoder maintains high compression efficiency while still utilizing multiple CPU threads.

Configuring Threads in libavif Developers and users can control how libavif utilizes system resources through its API or command-line tools. In the avifenc command-line utility, the --jobs (or -j) option specifies the number of threads to allocate. Setting this to all or a specific core count allows libavif to maximize CPU saturation, dramatically cutting down the time required to output high-quality AVIF images.