Vapoursynth Gamma (GLSL)

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

Gamma (GLSL) changes the gamma of the video with a small GLSL shader that libplacebo (vs-placebo) runs on the GPU.

Filtering → Vapoursynth → Color → Levels

Gamma (GLSL) controls in Hybrid
Gamma (GLSL) controls in Hybrid

What it does

Each color channel is raised to the power 1/Gamma. Values above 1 brighten the mid tones, values below 1 darken them; pure black and pure white stay where they are. The result is clamped to the valid range.

Requirements

  • vs-placebo (libplacebo) and a GPU that libplacebo can use.
  • Hybrid's GLSL shader folder.

Settings

Control Shader parameter Range Hybrid default Meaning
Gamma GammaFactor 0.01 – 100 1.00 1 = no change, > 1 brightens, < 1 darkens.

Example

Slightly brighter mid tones (Gamma 1.2) on a 10-bit YUV 4:2:0 source:

# adjusting color space from YUV420P10 to YUV444P16 for vsGLSLGamma
clip = core.resize.Bicubic(clip=clip, format=vs.YUV444P16)
# adjusting gamma using GLSL Gamma
with open("F:/Hybrid/64bit/vsfilters/GLSL/parameterized/gamma.glsl") as glslf:
  glsl = glslf.read()
glsl = glsl.replace('#define GammaFactor 2.0', '#define GammaFactor 1.2')
clip = core.placebo.Shader(clip, shader_s=glsl, width=clip.width, height=clip.height, matrix=1)
# adjusting output color from YUV444P16 to YUV420P10 for NVEncModel
clip = core.resize.Bicubic(clip=clip, format=vs.YUV420P10, dither_type="error_diffusion")

Before and after (drag the handle to compare), with this call:

# adjusting gamma using GLSL Gamma
with open("F:/Hybrid/64bit/vsfilters/GLSL/parameterized/gamma.glsl") as glslf:
  glsl = glslf.read()
glsl = glsl.replace('#define GammaFactor 2.0', '#define GammaFactor 1.25')
clip = core.placebo.Shader(clip, shader_s=glsl, width=clip.width, height=clip.height, matrix=2)

How Hybrid calls it

  • The clip is converted to YUV444P16 first, the only format the filter accepts in Hybrid.
  • Hybrid passes the clip's color matrix to placebo.Shader (matrix=1 for BT.601/470bg, 2 for BT.709, 4/5 for BT.2020), so the shader's RGB values match the clip.
  • Hybrid loads the shader gamma.glsl from its GLSL folder, writes the value into its GammaFactor line and runs it with placebo.Shader. The frame size stays the same.
  • The shader runs in libplacebo's RGB pipeline, so the curve acts on the R, G and B values; the result comes back as YUV444P16.
  • The filter is only offered when Hybrid's GLSL shader folder is present.
  • Gamma (GLSL) also offers the common options (Masked, ApplyOnlyTo, Merge, OnRegion, Gimmick).

Background

The shader

gamma.glsl is a lightweight gamma correction shader for mpv/libplacebo. It computes clamp(pow(rgb, 1/GammaFactor), 0, 1) and passes alpha through unchanged.

vs-placebo (placebo.Shader)

placebo.Shader runs mpv-syntax GLSL shaders through libplacebo. The shader is passed as a file or, as Hybrid does, as a string. Input is a 16-bit YUV clip, output is always YUV444P16. width and height set the output size.

VapourSynth

The closest core function is std.Levels with its gamma parameter, see Levels.

AviSynth

The gamma argument of AviSynth's Levels does the same (higher values brighten). See Avisynth Levels.

Sources