Vapoursynth CAS (GLSL)

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

CAS (GLSL) runs AMD FidelityFX Contrast Adaptive Sharpening as an mpv shader on the GPU through vs-placebo. Like CAS it sharpens low-contrast detail more than high-contrast edges, and it can optionally work in linear light.

Filtering → Vapoursynth → Sharpen

CAS (GLSL) controls in Hybrid
CAS (GLSL) controls in Hybrid

What it does

The shader runs two passes on the luma plane. The first optionally converts luma to linear light according to Source TRC. The second applies CAS on a 3×3 neighborhood (or only the cross without diagonals) and then converts back according to Target TRC. The shader is meant for content that is not scaled; Hybrid runs it at the input size.

Requirements

  • vs-placebo (libplacebo) and a GPU with Vulkan support.
  • The GLSL shader folder in Hybrid's VapourSynth plugins; without it all GLSL filters are hidden.

Settings

Control Parameter Range / values Hybrid default Meaning
Sharpening SHARPENING 0 – 1 0.5 Range of contrast the shader adapts to; 0 is not off, higher values also sharpen high-contrast areas more.
Better diagonals CAS_BETTER_DIAGONALS on/off on Also use the diagonal neighbors; off is faster and only useful on very old renderers.
Accurate CAS_GO_SLOWER on/off off Exact division and square root instead of fast approximations.
Source TRC SOURCE_TRC None, Rec709, PQ, sRGB, BT.1886, HLG, Custom, auto auto Transfer curve used to linearize before sharpening. auto takes the transfer of the clip: PQ, sRGB or HLG when the clip is flagged so, None for linear clips, otherwise Rec709.
Gamma (source) CUSTOM_SGAMMA 0.1 – 10 2.2 Gamma for Source TRC = Custom; enabled only then.
Target TRC TARGET_TRC None, Rec709, PQ, sRGB, BT.1886, HLG, Custom, auto auto Transfer curve used to convert back after sharpening; follows Source TRC (and its custom gamma) when that is changed.
Gamma (target) CUSTOM_TGAMMA 0.1 – 10 2.2 Gamma for Target TRC = Custom; enabled only then.

Example

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

# adjusting color space from YUV420P10 to YUV444P16 for vsGLSLCAS
clip = core.resize.Bicubic(clip=clip, format=vs.YUV444P16)
# contrast sharpening using GLSL CAS
with open("F:/Hybrid/64bit/vsfilters/GLSL/parameterized/CAS.glsl") as glslf:
  glsl = glslf.read()
glsl = glsl.replace('#define SHARPENING 0.0', '#define SHARPENING 0.5')
glsl = glsl.replace('#define CAS_BETTER_DIAGONALS 1', '#define CAS_BETTER_DIAGONALS 1')
glsl = glsl.replace('#define CAS_GO_SLOWER 0', '#define CAS_GO_SLOWER 0')
glsl = glsl.replace('#define SOURCE_TRC 0', '#define SOURCE_TRC 1')
glsl = glsl.replace('#define TARGET_TRC 0', '#define TARGET_TRC 1')
clip = core.placebo.Shader(clip, shader_s=glsl, width=clip.width, height=clip.height, matrix=1)

How Hybrid calls it

  • The clip is converted to YUV 4:4:4 16 bit, the only format the shader filter accepts.
  • Hybrid writes the settings into the shader text and runs it with the input width and height; the color matrix of the clip is passed to vs-placebo. The custom gammas are only written when the matching TRC is Custom.
  • The values work on normalized data and are not scaled to the bit depth.
  • Target TRC follows Source TRC, so by default the clip is linearized with its own transfer curve, sharpened in linear light and converted back. If the two are set to different curves afterwards, the output stays linear (darker) or is converted a second time (brighter).
  • CAS (GLSL) also offers the common options (Masked, ApplyOnlyTo, Merge, OnRegion, Gimmick).

Background

The shader

The mpv port is by agyild (MIT license, algorithm by AMD). It works directly on luma for speed. The shader notes say CAS is designed for linear light and oversharpens in gamma light; with the default None/None it works in gamma light. The peak weight ranges from −1/8 to −1/5, set by Sharpening. A separate CAS-scaled variant exists for scaling, which Hybrid does not use. Note the different default strength compared to the CPU CAS (0 here, 0.5 there).

Sources