How to Configure SAO in FFmpeg libx265
This article provides a straightforward guide on how to configure the
Sample Adaptive Offset (SAO) filter in the libx265 encoder
using FFmpeg. You will learn what the SAO filter does, how it impacts
video quality, and the exact command-line parameters required to enable,
disable, or adjust this setting to achieve the best results for your
HEVC encodes.
Understanding SAO in HEVC
Sample Adaptive Offset (SAO) is an in-loop filtering process in the HEVC (H.265) standard that occurs after the deblocking filter. Its primary purpose is to reduce ringing artifacts and banding by classifying reconstructed pixels into categories and applying an offset value to them.
While SAO improves objective quality metrics (like PSNR) and reduces file size at lower bitrates, it often introduces a “blurring” or “smudging” effect on fine details. For high-quality encodes, particularly those aiming to preserve film grain or complex textures, disabling SAO is highly recommended.
How to Configure SAO in FFmpeg
In FFmpeg, SAO is controlled using the -x265-params
flag. By default, the libx265 encoder has SAO enabled
(sao=1).
Disabling SAO (Recommended for High Quality)
To preserve fine details, sharpness, and film grain, you should
disable SAO by setting sao=0.
Use the following command:
ffmpeg -i input.mp4 -c:v libx265 -crf 20 -x265-params sao=0 output.mp4Enabling SAO (Recommended for Low Bitrates)
If you are encoding at very low bitrates and want to prioritize
compression efficiency and smooth gradients over fine details, you can
explicitly enable SAO by setting sao=1.
Use the following command:
ffmpeg -i input.mp4 -c:v libx265 -crf 28 -x265-params sao=1 output.mp4Configuring SAO Non-Deblock Filtering
By default, SAO uses the deblocked pixels for offset classification.
You can customize whether SAO filters the non-deblocked pixels using the
sao-non-deblock parameter.
To disable SAO on non-deblocked pixels (which can slightly improve edge sharpness when SAO is on):
ffmpeg -i input.mp4 -c:v libx265 -x265-params sao=1:sao-non-deblock=0 output.mp4Combining SAO with Other x265 Parameters
In most encoding workflows, you will want to combine the SAO
configuration with other parameters such as presets, CRF (Constant Rate
Factor), and adaptive quantization (AQ) mode. Multiple parameters inside
the -x265-params argument must be separated by colons
(:).
For a high-quality, detail-preserving 10-bit encode, use this configuration:
ffmpeg -i input.mp4 -c:v libx265 -profile:v main10 -pix_fmt yuv420p10le -crf 18 -preset slow -x265-params sao=0:aq-mode=3 output.mp4