Ch. 17 · FFmpeg Filters in Practice

한국어日本語
8 min

Denoising and Compression Efficiency: hqdn3d and nlmeans

Noise is unpredictable information, so it eats a lot of bits. hqdn3d for light removal, nlmeans for heavy removal, and why "the more you remove, the better" isn't true.

In Chapter 3 you learned that an encoder saves bits by reducing redundancy through prediction, then transforming and quantizing the remaining difference (the residual) (Inter prediction, Quantization). Camera sensor noise and film grain differ randomly from frame to frame, so they are almost impossible to predict. That is why, for the same quality target, a noisy video tends to make the encoder spend more bits. This leads to the question: "if I reduce noise before encoding, will the file get smaller?"

hqdn3d: a fast, light spatio-temporal filter

hqdn3d is a fast filter that reduces noise both spatially (within a frame) and temporally (across neighboring frames). Its parameters are in the order luma_spatial:chroma_spatial:luma_tmp:chroma_tmp, and the defaults in the FFmpeg documentation are 4.0 : 3.0 : 6.0 : 4.5. Bigger numbers remove more strongly.

Applying hqdn3d: defaults, and a lighter setting
# With defaults
ffmpeg -i noisy.mp4 -vf "hqdn3d" -c:v libx264 -crf 22 -c:a copy out.mp4

# Lighter: luma spatial 2, chroma spatial 1.5, luma temporal 3, chroma temporal 3
ffmpeg -i noisy.mp4 -vf "hqdn3d=2:1.5:3:3" -c:v libx264 -crf 22 -c:a copy out_light.mp4

nlmeans: slow but refined

nlmeans (Non-Local Means) finds pieces with similar texture (patches) within the frame and averages them. It is generally said to be much slower than hqdn3d but better at preserving detail. Strength is set with the s option, and the larger the patch size p and search range r, the slower it gets. Check the defaults with ffmpeg -h filter=nlmeans. Before using it on a long video, it's a good idea to time it on a short section first.

Testing on a short section
ffmpeg -ss 30 -t 10 -i noisy.mp4 -vf "nlmeans=s=3" -c:v libx264 -crf 22 -an test_nlmeans.mp4
hqdn3dnlmeans
MethodA fast filter in the spatial + temporal low-pass familyAveraging based on patch similarity
SpeedFastSlow (gets slower in proportion to patch size and search range)
StrengthLight, so easy to use in everyday workSaid to preserve detail relatively better
Watch out forSmearing and ghosting if applied stronglyTakes a long time; texture can look "plastic"

The performance and quality comparison reflects generally known tendencies. These are not figures measured on this site.

Does removing noise really shrink the file and improve quality?

  • File size: Reducing noise tends to make the output smaller at the same CRF. How much smaller depends on how noisy the source is and how strong the filter is, so there is no general number to memorize. You have to measure with your own source.
  • Quality: Along with the noise, real texture (skin, fabric, grass, film grain) can be removed too. In film footage where the grain is "part of the look," removing it makes the picture look flat.
  • Measurement pitfall: VMAF treats the original (noise included) as the ground truth. A result where noise was removed before encoding differs from the original, so the score can come out low. A low score doesn't mean it looks bad, and conversely you shouldn't judge by the score alone (How to read VMAF).
You can also leave it to the codec instead of removing it

AV1 has film grain synthesis in its specification. The encoder analyzes the grain and stores it as parameters, and the decoder recreates and applies the grain at playback. SVT-AV1 also offers a related option (film-grain). Instead of preserving the grain as bits, it is removed and synthesized, which is used for content where grain matters; the effect varies a lot with the source and settings, and you also need to check decoder support. x264 has -tune grain, which tunes toward preserving grain.

In the field: how to decide

(1) First check whether the noise in the source is noticeable. (2) Render a 10-second section at a light strength (hqdn3d, light) with the same CRF and look at both the file size and the picture. (3) Use it only if the file size drops meaningfully and there is no visible loss. You can see the general relationship between CRF, file size, and VMAF in the CRF sweep experiment, but that experiment did not cover denoising, so it doesn't directly tell you the effect of denoising.

hqdn3dhqdn3d filter

A fast filter that reduces noise both spatially and temporally. Parameters are luma_spatial:chroma_spatial:luma_tmp:chroma_tmp.

nlmeansNon-Local Means

A denoising filter that finds similar patches and averages them. Refined but slow.

Film grain synthesisFilm grain synthesis

A method where grain is removed and only parameters are stored, and the decoder recreates and applies the grain at playback. Part of the AV1 specification.

ResidualResidual

The difference between the predicted and actual values. Noise can't be predicted, so the residual grows and eats a lot of bits.

Check yourself

You compared a denoised video with the original using VMAF and the score came out low. What is the most appropriate interpretation?

Check yourself

Why does a noisy video tend to use more bits at the same target quality?