Can libavif encode grayscale images efficiently?

This article explores the efficiency of the libavif library when encoding and decoding monochrome grayscale images. It covers how the AVIF format natively handles single-channel data, the file size and performance benefits of using monochrome configurations, and how to implement this optimization.

Native Monochrome Support in AVIF

The AV1 Image File Format (AVIF) natively supports monochrome images. Unlike older image formats that often require saving grayscale images in an RGB color space—which wastes space on redundant color channels—AVIF can store images using a true monochrome color profile (YUV 4:0:0).

The reference library, libavif, fully implements this capability. When configured for monochrome, the encoder completely discards chroma (color) information, processing only the luminance (brightness) channel.

Encoding Efficiency and File Sizes

Using libavif to encode grayscale images yields highly efficient results:

Decoding Performance

Decoding monochrome AVIF files via libavif is highly efficient in terms of both speed and resource consumption:

How to Encode Grayscale with libavif

To achieve maximum efficiency, you must explicitly instruct libavif to use a monochrome format. In the C API, this is done by setting the pixel format to AVIF_PIXEL_FORMAT_YUV400.

If you are using the command-line tool avifenc, you can encode a grayscale image efficiently by using the --yuv 400 flag:

avifenc --yuv 400 input.png output.avif

This command forces the encoder to discard any color data and write a highly optimized, single-channel AVIF file.