Vapoursynth RGBAdjust (GLSL)

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

RGBAdjust (GLSL) changes the red, green and blue channels separately on the GPU — scaling, bias and gamma per channel — with a GLSL shader run by libplacebo (vs-placebo). It is the GPU counterpart of RGBAdjust and uses the same order of steps and the same gamma direction.

Filtering → Vapoursynth → Color → Grading

RGBAdjust (GLSL) controls in Hybrid
RGBAdjust (GLSL) controls in Hybrid

What it does

For each channel the shader computes, in this order:

  1. multiply by Scaling,
  2. add Bias (on the 8-bit scale, divided by 255),
  3. clamp to the valid range,
  4. apply Gamma (the value is raised to the power 1/Gamma) and clamp again.

With the defaults (1 / 0 / 1) the picture is unchanged. Gamma values above 1 brighten and below 1 darken, as in RGBAdjust and AviSynth.

Requirements

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

Settings

Control Shader parameter Range Hybrid default Meaning
Scaling (R/G/B) RedScale, GreenScale, BlueScale 0 – 100 1.00 Multiplies the channel.
Bias (R/G/B) RedBias, GreenBias, BlueBias -255 – 255 0.00 Offset added after scaling, on the 8-bit scale.
Gamma (R/G/B) RedGamma, GreenGamma, BlueGamma 0.01 – 100 1.00 Gamma per channel; > 1 brightens, < 1 darkens.

Example

A slightly warmer picture (red × 1.05, blue × 0.95) on a 10-bit YUV 4:2:0 source:

# adjusting color space from YUV420P10 to YUV444P16 for vsGLSLRGBAdjust
clip = core.resize.Bicubic(clip=clip, format=vs.YUV444P16)
# adjusting colors using GLSL RGBAdjust
with open("F:/Hybrid/64bit/vsfilters/GLSL/parameterized/RGBAdjust.glsl") as glslf:
  glsl = glslf.read()
glsl = glsl.replace('#define RedScale 1.0', '#define RedScale 1.05')
glsl = glsl.replace('#define GreenScale 1.0', '#define GreenScale 1')
glsl = glsl.replace('#define BlueScale 1.0', '#define BlueScale 0.95')
glsl = glsl.replace('#define RedBias 0.0', '#define RedBias 0')
glsl = glsl.replace('#define GreenBias 0.0', '#define GreenBias 0')
glsl = glsl.replace('#define BlueBias 0.0', '#define BlueBias 0')
glsl = glsl.replace('#define RedGamma 1.0', '#define RedGamma 1')
glsl = glsl.replace('#define GreenGamma 1.0', '#define GreenGamma 1')
glsl = glsl.replace('#define BlueGamma 1.0', '#define BlueGamma 1')
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 RGBAdjust.glsl from its GLSL folder, writes all nine values into its #define lines 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.
  • RGBAdjust (GLSL) also offers the common options (Masked, ApplyOnlyTo, Merge, OnRegion, Gimmick).

Background

The shader

RGBAdjust.glsl (v0.2) is an RGB adjustment shader for mpv in the same order as AviSynth's RGBAdjust. Per channel it computes c = clamp(c * Scale + Bias/255, 0, 1) and then c = clamp(pow(c, 1/Gamma), 0, 1).

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.

VapourSynth

For the CPU version see RGBAdjust.

AviSynth (RGBAdjust)

AviSynth's RGBAdjust applies gain first, then bias, then gamma (with > 1 brightening) and clips the result, like the shader; it adds analyze, dither and per-frame values. See Avisynth RGBAdjust.

Sources