Best lag-in-frames Settings for libvpx-vp9 Encoding
The lag-in-frames parameter in the libvpx-vp9 encoder is
a crucial setting that controls the look-ahead buffer, directly
impacting video quality, compression efficiency, and latency. This
article explains how the parameter works and provides the optimal
configurations for different encoding scenarios, including high-quality
video-on-demand (VOD) and ultra-low-latency live streaming.
How lag-in-frames Works
The lag-in-frames parameter determines how many frames
the encoder queues and analyzes before compressing them. By looking
ahead, the encoder can make smarter decisions regarding: *
Bitrate allocation: Distributing bits more efficiently
between complex and simple scenes. * Frame type
selection: Properly placing keyframes and golden frames
(alternate reference frames). * Temporal noise
reduction: Analyzing motion across multiple frames to reduce
compression artifacts.
Optimal Configuration Settings
The optimal value for lag-in-frames depends entirely on
your target delivery method.
1. Best Quality for VOD (Two-Pass Encoding)
For pre-recorded content where encoding speed and latency are not concerns, you should maximize the look-ahead buffer to achieve the best possible visual quality.
- Optimal Value:
25(the maximum effective limit for libvpx-vp9 in FFmpeg). - Required Companion Settings: You must enable
alternate reference frames for this to work effectively.
-lag-in-frames 25-auto-alt-ref 1
- Why: A value of 25 allows the encoder to look a full second ahead (at 24-30 fps) to create highly optimized alternate reference (ALT-REF) frames, resulting in significantly higher quality at lower bitrates.
2. Live Streaming and Low-Latency Encoding
For real-time applications like video conferencing, live streaming, or cloud gaming, encoding latency must be kept to an absolute minimum.
- Optimal Value:
0(disables look-ahead) or1(minimal look-ahead). - Required Companion Settings:
-lag-in-frames 0-auto-alt-ref 0(must be disabled if lag-in-frames is 0)-speed 5or higher (real-time encoding speed)
- Why: Setting the parameter to
0forces the encoder to compress each frame immediately as it arrives, eliminating buffer delay at the cost of some compression efficiency.
3. Balanced Speed and Quality (One-Pass Encoding)
If you are doing standard one-pass encoding and want a compromise between compression efficiency, memory usage, and CPU processing time.
- Optimal Value:
16to20. - Why: This range provides enough look-ahead frames to utilize ALT-REF frames and improve quality without consuming excessive system memory or slowing down the encoding pipeline as much as the maximum setting of 25.