Vapoursynth FilmGrain (GLSL)

From wiki.selur.net
Jump to navigation Jump to search

FilmGrain (GLSL) adds fine Gaussian film grain to the luma, computed on the GPU by a small mpv shader through libplacebo. The pattern changes every frame.

Filtering → Vapoursynth → Add Grain

FilmGrain (GLSL) controls in Hybrid
FilmGrain (GLSL) controls in Hybrid

What it does

For every pixel a Gaussian random value is added to the brightness, scaled by Intensity; chroma is not changed. At the default 0.05 the grain has a standard deviation of about 1 % of the full range (roughly 3 levels at 8 bit); at 0.2 about 4.5 %. Above about 0.2 it looks more like noise than grain.

Requirements

  • vs-placebo (libplacebo) and a GPU with Vulkan.
  • Hybrid's GLSL shader folder; without it the filter is disabled.

Settings

Control Parameter Range / values Hybrid default Meaning
Intensity INTENSITY 0.01 – 100 0.05 Amount of grain added to the normalized luma.

Example

Default settings on a 10-bit YUV 4:2:0 source (640x356):

# adjusting color space from YUV420P10 to YUV444P16 for vsGLSLFilmGrain
clip = core.resize.Bicubic(clip=clip, format=vs.YUV444P16)
# Adding grain using GLSL AddGrain
with open("F:/Hybrid/64bit/vsfilters/GLSL/parameterized/filmgrain.glsl") as glslf:
  glsl = glslf.read()
glsl = glsl.replace('#define INTENSITY 0.05', '#define INTENSITY 0.05')
clip = core.placebo.Shader(clip, shader_s=glsl, width=clip.width, height=clip.height, matrix=1)

How Hybrid calls it

  • Hybrid writes the Intensity into the film grain shader of its GLSL folder and runs it with vs-placebo's Shader at the current frame size and the clip's matrix.
  • The clip is converted to 16-bit YUV 4:4:4 first, which vs-placebo needs; the output is 16-bit YUV 4:4:4 as well.
  • The grain differs per frame but is the same on every run.
  • In the default filter order FilmGrain (GLSL) is the last of the grain filters.
  • FilmGrain (GLSL) also offers the common options (Masked, ApplyOnlyTo, Merge, OnRegion, Gimmick).

Background

The shader

The shader is mpv's "gaussian film grain" user shader by haasn. It hooks into the luma plane, takes libplacebo's per-frame random value, feeds it through a hash-based random number generator and turns the result into Gaussian noise with an approximation of the inverse normal distribution.

Sources