What is the avifEncoderWrite Function in libavif

This article explains the purpose, functionality, and usage of the avifEncoderWrite function within the libavif library. It covers how this function facilitates the compression of image data into the AVIF format and outlines its role in the overall encoding pipeline.

Purpose of avifEncoderWrite

The avifEncoderWrite function is a core component of the libavif library, which is the official open-source library used to encode and decode AVIF (AV1 Image File Format) files.

The primary purpose of avifEncoderWrite is to take a raw image, compress it using an AV1 encoder (such as aom, rav1e, or svt-av1), and package the compressed data into the standardized AVIF container format. It outputs the finalized AVIF file data directly into a memory buffer.

Function Signature

In the C API of libavif, the function is declared as follows:

avifResult avifEncoderWrite(avifEncoder * encoder, const avifImage * image, avifRWData * raw);

Parameters

How it Works in the Encoding Pipeline

To use avifEncoderWrite, developers typically follow these steps:

  1. Initialize Structures: Create and configure an avifImage to hold the raw pixels, and an avifEncoder to define the compression parameters.
  2. Call avifEncoderWrite: Invoke the function, passing the encoder configuration, the source image, and an empty avifRWData buffer.
  3. Process the Output: Once the function executes successfully, the raw buffer contains the complete AVIF image file. This buffer can then be written directly to a file on disk or sent over a network.
  4. Clean Up: Free the allocated memory for the image, encoder, and the output buffer using the library’s cleanup functions (such as avifRWDataFree).

Return Values

The function returns an avifResult status code.