日本語

Reducing Video Noise with hqdn3d and nlmeans in FFmpeg

​​FFmpeg​command line,H.264,video

To reduce grainy video noise in FFmpeg, you can use the hqdn3d or nlmeans filter. These are useful when you want to make the grain in footage shot in dim light or old video less visible, or when noise has made the video file larger and you want to make it smaller.

In my test environment, I added noise to a video, applied both filters at different strengths, and measured how close the result came to the original and how long it took. With the default settings, neither filter had much effect. nlmeans changed only 1 of the 125 frames, and in that frame just 2 values changed by 1.

Note

The test environment was Ubuntu 24.04.5, FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5), with a 4-core CPU. The source was a 1280×720, 25fps, 5-second (125-frame) video that I drew with Python (Pillow): sky, grass, thin stripes, text, and a red ball moving to the right. I added frame-by-frame changing noise with FFmpeg's noise filter (noise=alls=10:allf=t). I did not test noise in real camera footage, the GPU versions nlmeans_opencl and nlmeans_vulkan, or other denoising filters such as bm3d and atadenoise.

Command to Start With

If speed matters most, use hqdn3d. If quality matters most, use nlmeans. In both cases, raise the strength above the default.

bash
ffmpeg -i input.mp4 -vf hqdn3d=8 -c:v libx264 -crf 23 -c:a copy output_hq.mp4
ffmpeg -i input.mp4 -vf nlmeans=s=5:p=3:r=7 -c:v libx264 -crf 23 -c:a copy output_nl.mp4

I tested with an MP4 that included audio (about 33 MB). In both cases the video stayed H.264, the audio stayed AAC, and the length remained 5.000000 seconds. The file was about 1.3 MB with hqdn3d=8 and about 0.93 MB with nlmeans=s=5:p=3:r=7. -c:a copy tells FFmpeg to copy the audio stream without re-encoding it (Streamcopy section of the official FFmpeg documentation).

The values 8 and s=5 match the noise strength in this test. Noise strength varies between videos, so use the measurements below as a guide and try a short section before deciding.

Results at Different Strengths

I applied each filter to the video with added noise and compared the result with the original, noise-free video. For comparison I used the psnr filter and the ssim filter. PSNR is calculated from the difference from the original video, and higher values mean the result is closer to the original. SSIM also gets closer to the original as its value rises. In my test environment, comparing the original video with itself gave SSIM 1.000000 and PSNR inf (infinity). Both values are for the luma (Y) channel.

Setting Time taken PSNR SSIM MP4 size
Before filtering ― 33.35 dB 0.717 11,103,863 bytes
hqdn3d (default) 3.36 s 34.95 dB 0.784 5,707,216 bytes
hqdn3d=8 3.32 s 39.14 dB 0.907 1,314,028 bytes
hqdn3d=12 3.30 s 43.05 dB 0.963 619,378 bytes
hqdn3d=20 3.11 s 46.14 dB 0.988 407,272 bytes
nlmeans (default) 94.27 s 33.35 dB 0.717 ―
nlmeans=s=3 94.33 s 41.62 dB 0.980 ―
nlmeans=s=5 98.09 s 44.02 dB 0.994 651,783 bytes
nlmeans=s=8 98.07 s 43.80 dB 0.995 ―
nlmeans=s=5:r=7 22.85 s 42.74 dB 0.987 ―
nlmeans=s=5:p=3:r=7 22.18 s 44.80 dB 0.992 817,551 bytes

The time taken is the time from reading the 5-second video, applying the filter, and finishing writing it out as lossless (FFV1). The MP4 size is for the filtered video converted in the same way with libx264 -crf 23 -preset medium. The original noise-free video was 52,181 bytes after the same conversion.

Three images stacked vertically. The top shows stripes and text roughened by added noise, the middle shows some grain remaining, and the bottom shows a flat background
Enlarged 2x around the stripes and text at frame 60. Top: video with added noise. Middle: hqdn3d=8. Bottom: nlmeans=s=5

By PSNR and file size, hqdn3d=20 looks best. However, the source in this test has only one moving object, a ball, and most of the frame stays still. As described in a later section, hqdn3d=20 left a trail behind the moving ball. In a video where the whole frame moves, the PSNR of hqdn3d=20 dropped to almost the same as hqdn3d=12 and fell below nlmeans.

hqdn3d: Fast, but the Stronger the Setting, the More Trails Behind Moving Objects

The official hqdn3d documentation describes it as a filter that reduces noise and smooths the image, making still areas look stable, and it is expected to make the video easier to compress. It has four options:

  • luma_spatial: strength of smoothing for brightness (luma) within one frame (default 4.0)
  • chroma_spatial: strength of smoothing for color (chroma) within one frame (default 3.0*luma_spatial/4.0)
  • luma_tmp: strength of smoothing for brightness (luma) over time (default 6.0*luma_spatial/4.0)
  • chroma_tmp: strength of smoothing for color (chroma) over time (default luma_tmp*chroma_spatial/luma_spatial)

In the source code of FFmpeg 6.1.1, the temporal processing blends the current frame with the result of processing up to the previous frame (frame_ant). It does not use the next frame, which has not yet arrived.

If you give only the first value, as in hqdn3d=8, the other three are determined by these formulas. Adding -loglevel verbose shows the values actually used. I checked this with the noise-free original video (clean.mkv), and the result was as follows. The same line appeared twice, so only one is shown.

text
$ ffmpeg -loglevel verbose -i clean.mkv -vf hqdn3d=8 -frames:v 1 -f null - 2>&1 | grep Parsed_hqdn3d
[Parsed_hqdn3d_0] ls:8.000000 cs:6.000000 lt:12.000000 ct:9.000000

ffmpeg -h filter=hqdn3d shows "default 0" for all four options, but 0 means "determine it from the formula." In the source code of FFmpeg 6.1.1, a 0 is replaced with the default value. So hqdn3d=8:6:0:0 does not turn off temporal smoothing. Its output MD5 matched hqdn3d=8, and hqdn3d=4:3:0:0 matched the plain hqdn3d.

At the default strength (4.0), PSNR rose only from 33.35 dB to 34.95 dB. As I raised it to 8, 12, and 20, PSNR and SSIM kept rising, while the processing time stayed in the 3-second range.

Stronger Settings Leave Trails Behind the Moving Ball

Stronger temporal smoothing leaves trails of moving objects. I compared the difference from the original video (the average of the absolute brightness differences) in two places: just behind the ball, where it has just passed (sky), and just in front of the ball, where it has not yet arrived (sky). The target was frames 20 to 124.

Setting Behind the ball In front of the ball
Before filtering 4.89 4.92
hqdn3d=8 2.26 2.15
hqdn3d=12 1.44 1.20
hqdn3d=20 1.96 0.45
nlmeans=s=5 0.45 0.36

With hqdn3d=20, the difference in front of the ball fell to 0.45, but the difference behind the ball was 1.96, about four times as large. The difference behind the ball was larger than with hqdn3d=12.

Three black-and-white images showing the difference from the original video. With hqdn3d=20, a gray band extends to the left of the ball. With hqdn3d=8, dots are scattered across the image. With nlmeans=s=5, only the outline of the ball is dark
Differences around the ball at frame 60, multiplied by 16 and inverted to black and white. Darker means a larger difference. The ball is moving to the right

Full-Frame Motion Does Not Improve With Higher Strength

Instead of a video with a horizontal camera pan, I made a 1024×576 video in which the whole frame shifted 2 pixels to the side each frame, added the same noise, and tested it.

Setting PSNR, still frame PSNR, moving frame
hqdn3d=12 43.05 dB 41.78 dB
hqdn3d=20 46.14 dB 41.99 dB
nlmeans=s=5 44.02 dB 43.76 dB
nlmeans=s=5:p=3:r=7 44.80 dB 44.77 dB

With hqdn3d=20, the PSNR dropped from 46.14 dB to 41.99 dB, and the gap from hqdn3d=12 was only 0.21 dB. nlmeans gave nearly the same values for both videos.

nlmeans: The Default s=1 Changes Nothing

According to the official nlmeans documentation, nlmeans searches a surrounding r×r area for patches that look similar to the patch around each pixel (p×p), and then averages those similar pixels.

  • s: strength, from 1.0 to 30.0, default 1.0
  • p: size of the patch being compared, an odd number, default 7
  • r: size of the search area for similar patches, an odd number, default 15
  • pc, rc: p and r for the color planes, default 0, which means they are chosen automatically

Applying -vf nlmeans with the defaults left both PSNR and SSIM exactly as they were before filtering. Comparing the MD5 of each frame, 124 of the 125 frames were identical. In the remaining frame, only 2 values in the color planes differed by 1. Even so, the processing took 94.27 seconds.

Reading the FFmpeg 6.1.1 source explains why. nlmeans measures how different two p×p patches are by summing the squared pixel differences across both patches (vf_nlmeans_init.h). Any candidate whose difference is at or above log(255) × (s × 10)² gets a weight of 0 and is not used (vf_nlmeans.c). The center pixel itself is always added with weight 1 (weight_averages in vf_nlmeans.c).

This limit is 554 for s=1, 4987 for s=3, and 13853 for s=5. From the PSNR, the noise in this test corresponds to a squared difference of about 30 per pixel from the original. Even when the original patches are the same 7×7 area, both carry noise, so the squared difference is about double, roughly 60 per pixel. The total difference is 49 × 2 × 30, or about 2950. That is well above the limit of 554 for s=1, so almost no other patches were used, and the center pixel was left as it was.

Raising s improved the result: 41.62 dB at s=3 and 44.02 dB at s=5. At s=8 it was 43.80 dB, slightly below s=5. When I tried the first 10 frames of a video with stronger noise (alls=20), s=3 was almost the same as before filtering (still 27.035 dB), while s=5 gave 31.41 dB and s=8 gave 37.55 dB. For this noise, the same calculation gives a difference of about 12600, which is above the s=3 limit of 4987. In the two strengths I tested, stronger noise needed a larger s before the filter had any effect.

Reducing r Takes About a Quarter of the Time

With the defaults, nlmeans took 94 to 98 seconds on the 5-second video. That is about 20 times the video length, and about 30 times the time of hqdn3d.

Setting the search area to r=7 brought the time down to 22.85 seconds. PSNR dropped from 44.02 dB to 42.74 dB. Setting the comparison patch to p=3 as well took 22.18 seconds, and PSNR rose to 44.80 dB, higher than s=5 with the default area.

Almost all of the time saving came from r. Adding p=3 reduced the time by only 0.67 seconds.

Looking at details alone shows clear differences. The SSIM of the flat gray panel was 0.997 with s=5, and 0.988 with r=7, so the smoothing became a little weaker (0.992 with p=3:r=7). The SSIM of the text area was 0.974 with s=5 and 0.972 with r=7, nearly unchanged, and rose to 0.994 with p=3. For this source, a smaller p kept the text sharp.

Less Noise Makes the MP4 Smaller

Encoded to H.264 with the same -crf 23, the video with added noise was 11,103,863 bytes. With hqdn3d=8 it was 1,314,028 bytes, about one-eighth. With nlmeans=s=5 it was 651,783 bytes, about one-seventeenth.

With the stronger hqdn3d=20, the file was 407,272 bytes, about one twenty-seventh. However, as the previous section showed, it leaves trails behind moving objects. Even when your goal is a smaller file, you need to judge the strength by watching the video.

​Posted by 86