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:
- Quantization Bypass: The quantization parameter (QP) is set to zero. This ensures that no data is rounded or discarded during the compression process.
- Transform Bypass (Transquant Bypass): Because
quantization is disabled, performing mathematical transforms like DCT or
ADST can actually introduce rounding errors.
libvpxbypasses these transforms entirely. The spatial prediction residuals (the difference between the actual pixels and the predicted pixels) are coded directly. - Entropy Coding: The raw prediction residuals are passed straight to VP9’s boolean entropy encoder (an arithmetic coder). The entropy coder compresses the remaining prediction residuals using probability models without losing any fidelity.
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.webmUsing 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:
- RGB and YUV 4:4:4: Standard video compression often
downsamples color data to YUV 4:2:0. For true lossless screen recordings
or graphics,
libvpxsupports YUV 4:4:4 and RGB color spaces (often mapped to GBR in VP9). This prevents chroma subsampling loss before the encoder even processes the frames. - Prediction Modes: Even in lossless mode,
libvpxutilizes intra-frame (spatial) and inter-frame (temporal) prediction. By predicting pixel values from neighboring pixels or adjacent frames, the encoder minimizes the magnitude of the residuals, allowing the entropy encoder to compress the data highly efficiently despite the lack of quantization.