How libavif Encodes and Decodes AVIF Images
The open-source libavif library is the official
reference implementation for the AV1 Image File Format (AVIF), providing
developers with a portable, C-based toolkit for reading and writing AVIF
files. Rather than reinventing the wheel, libavif acts as
an intermediary layer that pairs AV1 video compression engines with the
ISO Base Media File Format (ISOBMFF) container structure. By abstracting
the complexities of bitstream parsing, chroma subsampling, metadata
management, and color transformations, the library allows software
applications to seamlessly convert raw pixel data into compressed AVIF
images and reconstruct them back into displayable formats.
Architecture and Codec Abstraction
At its core, libavif is a container parser and
multiplexer rather than a standalone image compressor. AVIF relies on
AV1 video compression frames encapsulated in a HEIF/ISOBMFF container.
To handle the actual heavy lifting of raw pixel compression and
decompression, libavif relies on external AV1 codecs
through an abstraction layer.
- Supported Encoders: It can interface with libraries
such as
libaom(the reference AV1 encoder),rav1e(a fast, memory-safe Rust encoder), andSVT-AV1(a highly scalable, multi-threaded encoder). - Supported Decoders: It predominantly uses
dav1d(optimized for speed and low memory usage),libgav1, orlibaom.
Developers can compile libavif with one or more of these
backend codecs enabled, and the library automatically selects the best
available engine or allows the user to specify a preferred backend at
runtime.
The Encoding Workflow
Encoding an image with libavif involves taking
uncompressed image buffers and transforming them into a
standard-compliant .avif file through several distinct
stages:
- Pixel Ingestion and Color Conversion: The input data—typically RGB or RGBA—is converted into the YUV color space. The library supports various chroma subsampling modes, including 4:4:4 (full resolution), 4:2:2, and 4:2:0 (standard color compression), as well as monochrome.
- Channel Separation: If the input image contains an
alpha channel (transparency),
libavifseparates the alpha plane from the color planes. The primary color payload and the alpha channel are encoded as two distinct AV1 bitstreams to maintain compliance with the AVIF specification. - AV1 Frame Compression: The library configures the underlying AV1 encoder with parameters such as quality factor, speed preset, tile configurations, and bit depth (8, 10, or 12 bits). The encoder processes the YUV and alpha frames, outputting compressed AV1 temporal units.
- ISOBMFF Multiplexing:
libavifpacks the compressed bitstreams into the ISOBMFF container. It creates the required box hierarchy (such asftyp,meta,hdlr, andiloc), links the alpha item as an auxiliary image to the primary color item, and embeds metadata like ICC profiles, Exif, and XMP payloads directly into container boxes.
The Decoding Workflow
Decoding operates in reverse, reading the container format and rendering raw pixel arrays for application use:
- Container Parsing: When an AVIF file is fed into
libavif, the parser inspects the container headers, identifies the primary image item, discovers associated alpha or depth auxiliary items, and extracts image dimensions, bit depth, and color profile information (CICP or ICC). - Bitstream Extraction: The library locates the raw
AV1 chunks within the media data boxes (
mdat) and passes the primary color bitstream to the configured decoder (such asdav1d). If an auxiliary alpha channel exists, it is sent to the decoder concurrently or sequentially. - Reconstruction and Post-Processing: The decoder
outputs raw YUV and alpha planes.
libavifrecombines these planes, performs YUV-to-RGB conversion according to the embedded color matrix, applies any required spatial transformations (such as rotation or mirroring specified in the container metadata), and outputs a final RGB/RGBA buffer.
Advanced Feature Handling
Beyond basic single-frame operations, libavif
facilitates advanced media handling:
- High Dynamic Range (HDR): It supports 10- and 12-bit color pipelines, alongside SMPTE ST 2084 (PQ) and Hybrid Log-Gamma (HLG) transfer characteristics.
- Image Grids: Large images can be split into a grid of smaller tiles, encoded independently, and assembled by the container. This enables parallelized encoding and low-memory decoding on resource-constrained devices.
- Image Sequences:
libavifhandles animated AVIF files by encoding frames over a temporal timeline, utilizing native AV1 inter-frame prediction to drastically reduce file sizes compared to animated GIFs or WebP.