Force 4:4:4 Chroma Retention in libavif and cavif
By default, AVIF encoders use 4:2:0 chroma subsampling to maximize
file size reduction, which often leads to color bleeding, blurry
saturated edges, and degraded UI text. This article covers the explicit
command-line flags required by both libavif
(avifenc) and cavif to disable chroma
subsampling and force 4:4:4 chroma retention, ensuring maximum color
fidelity for graphics, text, and sharp illustrations.
Forcing 4:4:4 in libavif (avifenc)
In libavif, encoding is handled via the command-line
utility avifenc. By default, avifenc targets
420 subsampling. To force full 4:4:4 chroma retention, use
the -y or --yuv flag followed by
444.
Syntax and Example
avifenc -y 444 input.png output.avifOr using the full argument name:
avifenc --yuv 444 input.png output.avifSupported YUV Values in libavif
444: Full chroma retention (no subsampling).422: Horizontal subsampling only.420: Horizontal and vertical subsampling (default).400: Grayscale (discards all chroma data).
If you are performing lossless encoding using
--lossless, avifenc automatically defaults to
444 to preserve all image information. However, when
performing lossy compression, explicitly declaring -y 444
is required to prevent the encoder from dropping chroma resolution.
Forcing 4:4:4 in cavif
cavif is a standalone AVIF encoder written in Rust that
utilizes the rav1e encoding engine. To dictate the pixel
format and prevent chroma downsampling in cavif, use the
--pixel-format flag.
Syntax and Example
cavif --pixel-format 444 input.png -o output.avifDepending on the specific release build of cavif-rs, the
argument can be specified as 444 or
yuv444:
cavif --pixel-format=yuv444 input.pngSupported Pixel Formats in cavif
444/yuv444: Full color resolution with no subsampling.422/yuv422: Subsampled chroma horizontally.420/yuv420: Standard subsampling (default).
When to Use 4:4:4 Chroma
- Pixel Art and Sharp Graphics: Prevents colors from bleeding across hard boundaries.
- Text on Colored Backgrounds: Preserves high contrast and eliminates halos around colored text.
- Red and Blue High-Frequency Elements: Human vision is particularly sensitive to chroma subsampling artifacts when bright reds and blues intersect with neutral backgrounds.
- Trade-Off: Using 4:4:4 increases the overall file size compared to 4:2:0, typically by 10% to 30%, depending on image complexity and quantization parameters.