How to Enable Strict AVIF Compliance in libavif

This article explains how the libavif library enforces strict adherence to the AV1 Image File Format (AVIF) specifications using its dedicated API flags. You will learn about the avifStrictFlag enumeration, the specific validation checks each flag performs during image decoding, and how developers can configure these settings to ensure security, standard compliance, and consistent rendering across different platforms.

The libavif library, the de facto standard open-source library for encoding and decoding AVIF files, provides a robust validation mechanism through its decoder configuration. By default, image decoders often attempt to be lenient, parsing and displaying files even if they contain minor specification violations. However, for applications requiring high security, strict standards compliance, or precise rendering, libavif offers the strictFlags member in its avifDecoder struct.

To define and control compliance, libavif utilizes the avifStrictFlag enum. Developers can combine these bitwise flags to customize the level of strictness required during the decoding process.

The Strictness API Flags

The core compliance flags defined in libavif include:

Implementing Strict Mode in Code

To implement strict validation, you must configure the strictFlags property of the avifDecoder instance before reading the container.

#include "avif/avif.h"

void decode_strict_avif(const uint8_t* raw_data, size_t data_size) {
    avifDecoder * decoder = avifDecoderCreate();
    
    // Enable maximum strict adherence to the AVIF specification
    decoder->strictFlags = AVIF_STRICT_ENABLED;

    // Alternatively, enable only specific checks (e.g., CLAP and PIXI)
    // decoder->strictFlags = AVIF_STRICT_CLAP | AVIF_STRICT_PIXI;

    avifResult result = avifDecoderSetIOMemory(decoder, raw_data, data_size);
    if (result == AVIF_RESULT_OK) {
        result = avifDecoderParse(decoder);
        if (result != AVIF_RESULT_OK) {
            // If the file violates specified AVIF rules, parsing fails here
            printf("AVIF validation failed: %s\n", avifResultToString(result));
        }
    }
    
    avifDecoderDestroy(decoder);
}

By leveraging these API flags, developers can safeguard their applications against malformed files that could exploit parser vulnerabilities, while guaranteeing that all processed AVIF images strictly conform to standardized specifications.