Libvpx Loop Filtering to Reduce Blocking Artifacts
In video compression, block-based coding often introduces distracting grid-like patterns known as blocking artifacts. This article explains how the libvpx library—the open-source reference implementation for the VP8 and VP9 video codecs—employs an in-loop filtering mechanism to smooth out these block boundaries, balance sharp details with blur reduction, and significantly enhance overall visual quality.
What is In-Loop Filtering?
The loop filter in libvpx is called “in-loop” because it is integrated directly into the encoding and decoding loop. Instead of post-processing the video after it is fully decoded, the filter is applied to reconstructed frames before they are saved to the frame buffer. This ensures that the filtered, higher-quality frames are used as reference frames for future motion estimation, preventing compression errors from compounding over time.
Step 1: Boundary Detection and Selection
The loop filter targets the borders between adjacent blocks (typically 4x4, 8x8, or 16x16 pixels). Because block-based transforms (like the Discrete Cosine Transform) process each block independently, the sharpest mismatches occur at these boundaries. Libvpx analyzes both horizontal and vertical edges to determine where smoothing is required.
Step 2: Thresholding and Edge Preservation
To avoid blurring actual image details (such as the edge of an object or text), libvpx must distinguish between artificial blockiness and real image content. It does this using mathematical thresholds based on two main factors:
- Quantization Parameter (QP): Higher quantization levels (lower bitrates) cause more severe blocking, requiring stronger filtering.
- Local Pixel Variance: The filter analyzes the difference in pixel values across the boundary. If the difference is small, it is assumed to be a blocking artifact, and the filter is applied. If the difference is large, it is identified as a natural edge, and the filter is bypassed or applied with minimal strength.
Step 3: Simple vs. Normal Filter Modes
Libvpx offers two primary modes of loop filtering to balance computational cost and visual performance:
- Simple Filter: This mode only processes the boundary pixels of luminance (brightness) blocks. It uses fewer CPU cycles, making it ideal for real-time encoding on low-power devices.
- Normal Filter: This mode is more thorough. It filters both luminance and chrominance (color) blocks, analyzing a wider span of pixels surrounding the boundary. It adjusts the filtering strength dynamically on a pixel-by-pixel basis, resulting in superior visual quality at the expense of higher CPU usage.
Step 4: Mathematical Smoothing
Once a boundary is selected for filtering, libvpx applies a low-pass filter to the adjacent pixels. This mathematical operation averages the values of pixels on either side of the edge, gently blending the transition. The strength of this blend is carefully controlled so that the block grid disappears while the underlying textures and gradients of the video remain intact.