RGB to YUV Conversion Matrix Coefficients in libavif

When encoding images to the AVIF format using the libavif library, RGB color data must typically be converted to the YUV color space to enable efficient compression. This article explains the vital role of matrix coefficients in this conversion process, detailing how they define the mathematical transformations between color models, preserve color accuracy through metadata signaling, and prevent color distortion during image decoding.

Understanding RGB to YUV Conversion

The AV1 video codec, which powers the AVIF image format, achieves its high compression efficiency by operating in the YUV (or YCbCr) color space. Unlike RGB, which treats Red, Green, and Blue channels with equal weight, YUV separates brightness (Luma, Y) from color information (Chroma, U and V). Since the human eye is more sensitive to brightness than color details, the chroma channels can be compressed or subsampled without a noticeable loss in perceived image quality.

The Role of Matrix Coefficients

Matrix coefficients are the specific mathematical weights used to calculate Y, U, and V values from the original RGB pixels. Because different color standards (such as standard definition, high definition, or wide color gamut) define “red,” “green,” and “blue” coordinates differently, different mathematical equations are required to convert them to YUV.

The primary roles of these coefficients in libavif include:

1. Determining the Transformation Equations

The matrix coefficients dictate the exact formulas libavif uses during the encoding phase. Different standards allocate different weights to the color channels to calculate luminance (Y). For example: * BT.709 (HD standard): \(Y = 0.2126R + 0.7152G + 0.0722B\) * BT.2020 (UHD/HDR standard): \(Y = 0.2627R + 0.6780G + 0.0593B\) * BT.601 (SD standard): \(Y = 0.2990R + 0.5870G + 0.1140B\)

Using the correct matrix ensures that the luminance and chrominance are calculated accurately based on the target color space of the input image.

2. Metadata Signaling (CICP)

libavif embeds the chosen matrix coefficient value into the AVIF container as part of the CICP (Coding-Independent Code Points) metadata. The matrixCoefficients field (defined by the ITU-T H.273 standard) is represented by an integer.

For instance, a value of 1 represents BT.709, while 9 represents BT.2020. This metadata is crucial because it tells the decoder (such as a web browser or image viewer) exactly which inverse matrix to apply when converting the YUV data back to RGB for display.

3. Preventing Color Shifts

If the matrix coefficients are misconfigured or missing, the decoder must guess which matrix to use. If an image is encoded using BT.2020 coefficients but decoded using BT.709, the resulting colors will appear shifted, desaturated, or distorted. Accurate signaling of matrix coefficients in libavif guarantees color consistency across all compliant viewing platforms.

4. Supporting RGB Pass-through (Lossless)

In some cases, users want to avoid YUV conversion altogether to preserve perfect RGB data. libavif supports a matrix coefficient value of 0 (Identity/BRG). This value tells the encoder and decoder that no YUV matrix transformation was applied, preserving the original RGB channels as-is inside the container.

Common Coefficients Used in libavif