Configure libx264 Psycho-Visual RD Strength in FFmpeg
Adjusting the psycho-visual rate-distortion (psy-RD) strength in
FFmpeg’s libx264 encoder allows you to control how the
encoder prioritizes perceived visual quality—such as detail and texture
retention—over mathematical compression metrics. This guide explains how
to use the -psy-rd parameter, detailing its syntax, the
function of its two primary components, and practical examples for
different video types.
Understanding the -psy-rd Parameter
The psycho-visual rate-distortion setting is configured in FFmpeg
using the -psy-rd flag. This option accepts two float
values separated by a colon:
-psy-rd <psy-rd-strength>:<psy-trellis-strength>- Psy-RD Strength (First Value): Controls the overall strength of the psycho-visual rate-distortion optimization. It prevents the encoder from discarding complex textures (like film grain, grass, or water) in an effort to save bitrate. A higher value keeps the video looking sharper and more detailed, while a lower value may result in a softer image but higher compression efficiency.
- Psy-Trellis Strength (Second Value): Controls the psycho-visual optimization during trellis quantization. It helps preserve fine details and sharp edges, reducing “blurriness” around high-contrast areas. However, setting this too high can introduce unwanted noise or blocky artifacts.
Recommended Values and Use Cases
By default, the libx264 encoder applies a setting of
1.0:0.0 for most standard presets. Depending on your source
material, you can override these defaults to optimize your output:
1. High-Detail and Film Grain (Film Tune)
For movies with natural film grain or highly detailed textures, you
want a strong psy-RD setting to prevent the encoder from smoothing out
the image. * Recommended Setting: 1.0:0.15
* FFmpeg Command:
bash ffmpeg -i input.mp4 -c:v libx264 -crf 20 -psy-rd 1.0:0.15 output.mp4
2. Animation and Anime (Flat Graphics)
Animation contains flat color gradients and sharp lines. High psy-RD
values can cause ringing artifacts around lines. Lowering both values
produces cleaner, smoother gradients. * Recommended
Setting: 0.4:0.0 * FFmpeg
Command:
bash ffmpeg -i input.mp4 -c:v libx264 -crf 20 -psy-rd 0.4:0.0 output.mp4
3. Disabling Psycho-Visual Optimizations
If your goal is pure mathematical encoding efficiency (such as
testing peak signal-to-noise ratio / PSNR metrics), you can disable
psycho-visual optimizations entirely. * Recommended
Setting: 0.0:0.0 (Note: This is automatically done
if you use -tune psnr). * FFmpeg Command:
bash ffmpeg -i input.mp4 -c:v libx264 -crf 20 -psy-rd 0.0:0.0 output.mp4
Interaction with the -tune Flag
FFmpeg’s libx264 has built-in tuning profiles (via the
-tune option) that automatically set these parameters for
you. For example, -tune film sets -psy-rd to
1.0:0.15, while -tune animation adjusts it to
0.4:0.0.
If you define both -tune and -psy-rd in the
same command, the values specified in the explicit -psy-rd
flag will override the tuning profile’s defaults, allowing you to
fine-tune the preset configurations.