Query libavif Version and Capabilities at Runtime

This article demonstrates how to query the libavif library at runtime to retrieve its version string and determine which AV1 codecs and features (such as encoding and decoding) are compiled and available. You will learn the specific C APIs required to fetch this information dynamically within your applications.

Retrieving the libavif Version String

To get the exact version of the libavif library currently running in your environment, use the avifVersion() function. This function takes no arguments and returns a constant string containing the version number (for example, "1.0.4").

Here is a simple C example showing how to retrieve and print the version:

#include <avif/avif.h>
#include <stdio.h>

int main() {
    const char *version = avifVersion();
    printf("Running libavif version: %s\n", version);
    return 0;
}

Querying Codec Capabilities at Runtime

Because libavif can be built with different underlying AV1 codecs (such as AOM, dav1d, rav1e, or SVT-AV1), you must check which capabilities are compiled into the library at runtime before attempting to encode or decode AVIF images.

You can query codec availability using the avifCodecName() function:

const char *avifCodecName(avifCodecChoice choice, avifCodecFlags flags);

If the requested configuration is available, avifCodecName() returns the name of the codec as a string. If the capability is not supported, it returns NULL.

Example: Checking for Decoders and Encoders

The following code checks if the runtime library is capable of decoding and encoding AVIF files:

#include <avif/avif.h>
#include <stdio.h>

int main() {
    // Check if any decoder is available
    const char *decoderName = avifCodecName(AVIF_CODEC_CHOICE_AUTO, AVIF_CODEC_FLAG_CAN_DECODE);
    if (decoderName != NULL) {
        printf("AV1 Decoding is supported via: %s\n", decoderName);
    } else {
        printf("AV1 Decoding is NOT supported by this build of libavif.\n");
    }

    // Check if any encoder is available
    const char *encoderName = avifCodecName(AVIF_CODEC_CHOICE_AUTO, AVIF_CODEC_FLAG_CAN_ENCODE);
    if (encoderName != NULL) {
        printf("AV1 Encoding is supported via: %s\n", encoderName);
    } else {
        printf("AV1 Encoding is NOT supported by this build of libavif.\n");
    }

    return 0;
}

By utilizing avifVersion() and avifCodecName(), your application can gracefully handle environments with varying levels of AVIF support.