VP9 Lossless Encoding in libvpx

This article explains how the libvpx codec library implements and manages lossless encoding for the VP9 video format. It covers the underlying technical mechanisms of VP9’s lossless mode, the configuration parameters required to trigger it, and how the library handles color space conversions and entropy coding to ensure bit-exact output reconstruction.

The Technical Mechanism of VP9 Lossless Mode

Lossless compression in VP9 is achieved primarily by bypassing the quantization and transform steps of the standard video encoding pipeline. In a typical lossy VP9 encoding workflow, spatial prediction residuals undergo a Discrete Cosine Transform (DCT) or Asymmetric Discrete Sine Transform (ADST), followed by quantization—a step that discards less perceptible visual data to reduce file size.

When lossless mode is enabled in libvpx, the encoder performs the following adjustments:

Enabling Lossless Encoding in libvpx

To initiate lossless encoding, the user must explicitly instruct libvpx to use these bypass paths. This can be done via the API or command-line interfaces like vpxenc and FFmpeg.

Using the libvpx API

Developers integrating the libvpx library directly can enable lossless mode by setting the VP9E_SET_LOSSLESS control parameter to 1.

vpx_codec_control(&codec, VP9E_SET_LOSSLESS, 1);

Using vpxenc (Command Line)

When using the native vpxenc command-line tool, the --lossless flag must be set to 1.

vpxenc --codec=vp9 --lossless=1 input.y4m -o output.webm

Using FFmpeg

In FFmpeg, which utilizes libvpx-vp9 under the hood, lossless encoding is triggered by setting the constant rate factor (-crf) to 0 and the bitrate (-b:v) to 0.

ffmpeg -i input.mp4 -c:v libvpx-vp9 -lossless 1 output.webm

(Alternatively, setting -crf 0 with libvpx-vp9 automatically activates the lossless profile).

Color Space Handling and Coding Efficiency

To achieve true, bit-exact lossless reproduction of the source material, the input color space must be handled carefully: