Configure FFmpeg libx264 Adaptive Quantization

This article explains how to configure and fine-tune the adaptive quantization (AQ) settings in the libx264 encoder using FFmpeg. Adaptive quantization is a crucial encoding technique that redistributes bits within a frame to improve visual quality, particularly by reducing blocking artifacts in flat, dark, or low-complexity areas. You will learn the key parameters, their available modes, and how to apply them in your FFmpeg commands.

Understanding Adaptive Quantization Parameters

In libx264, adaptive quantization is controlled by two main parameters: AQ Mode (aq-mode) and AQ Strength (aq-strength).

To configure these parameters in FFmpeg, you should pass them via the -x264-params option, separating the arguments with a colon (:).

1. AQ Mode (aq-mode)

The AQ mode determines how the encoder identifies complex versus flat areas in a frame to redistribute bits.

2. AQ Strength (aq-strength)

The AQ strength controls the intensity of the bit redistribution.

FFmpeg Command Examples

To apply these settings, use the -x264-params flag in your FFmpeg command.

Example 1: Enabling Auto-Variance AQ with Default Strength This command sets AQ mode to 2 and keeps the default strength of 1.0:

ffmpeg -i input.mp4 -c:v libx264 -crf 23 -x264-params aq-mode=2:aq-strength=1.0 output.mp4

Example 2: Optimizing for Dark Scenes and Preventing Banding This command utilizes AQ mode 3 (dark-biased) with a slightly stronger redistribution rate of 1.3 to clean up blocky shadows:

ffmpeg -i input.mp4 -c:v libx264 -crf 20 -x264-params aq-mode=3:aq-strength=1.3 output.mp4

Example 3: Disabling Adaptive Quantization If you prefer a constant quantization parameter across the entire frame without any bit redistribution, disable AQ completely:

ffmpeg -i input.mp4 -c:v libx264 -crf 23 -x264-params aq-mode=0 output.mp4