Vapoursynth Saturation (GLSL)

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

Saturation (GLSL) changes the color saturation with bSat, a small GLSL shader originally written for mpv, run on the GPU by libplacebo (vs-placebo).

Filtering → Vapoursynth → Color → Grading

Saturation (GLSL) controls in Hybrid
Saturation (GLSL) controls in Hybrid

What it does

For every pixel the shader averages R, G and B to a gray value and blends between that gray and the original color by Factor:

  • 0 = grayscale,
  • below 1 = less saturation,
  • 1 = no change,
  • above 1 = more saturation.

Very large factors clip strongly saturated colors.

Requirements

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

Settings

Control Shader parameter Range Hybrid default Meaning
Factor SatFactor 0 – 100 1.00 Saturation factor; 1 = no change.

Example

Saturation raised by 30 % (Factor 1.3) on a 10-bit YUV 4:2:0 source:

# adjusting color space from YUV420P10 to YUV444P16 for vsGLSLSaturation
clip = core.resize.Bicubic(clip=clip, format=vs.YUV444P16)
# adjusting saturation using GLSL bSat
with open("F:/Hybrid/64bit/vsfilters/GLSL/parameterized/bSat.hook.glsl") as glslf:
  glsl = glslf.read()
glsl = glsl.replace('#define SatFactor 2.43', '#define SatFactor 1.3')
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")

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 bSat.hook.glsl from its GLSL folder, writes the factor into its SatFactor line and runs it with placebo.Shader. The frame size stays the same.
  • The shader works on RGB inside libplacebo's pipeline; the result comes back as YUV444P16.
  • The filter is only offered when Hybrid's GLSL shader folder is present.
  • Saturation (GLSL) also offers the common options (Masked, ApplyOnlyTo, Merge, OnRegion, Gimmick).

Background

The shader

bSat (v0.1 by butterw) is a lightweight RGB saturation shader for mpv. It computes gray = (R+G+B)/3 with equal weights (not luma weights) and returns mix(gray, color, SatFactor) without clamping.

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; the conversion to and from RGB uses the matrix, trc and chroma_loc settings (default BT.709).

AviSynth

The closest AviSynth core filter is Tweak with its sat parameter (0–10, 1 = no change), which works in YUV instead of RGB. See Avisynth Tweak.

Sources