Vapoursynth Adaptive Sharpen (GLSL)

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

Adaptive Sharpen (GLSL) runs bacondither's Adaptive sharpen shader on the GPU through vs-placebo. It sharpens slightly blurry edges most and leaves already sharp edges and flat areas largely alone.

Filtering → Vapoursynth → Sharpen

Adaptive Sharpen (GLSL) controls in Hybrid
Adaptive Sharpen (GLSL) controls in Hybrid

What it does

The shader looks at a 25-pixel diamond around each pixel, detects edges on a slightly blurred copy and sharpens with a weighted Laplace. A sharpening curve, set by Curve height, decides how much each edge gets; a soft limiter compresses the overshoot against the local minimum and maximum, so ringing stays low. Only the luma plane is sharpened. The shader is tuned for use after resizing.

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
Curve height curve_height 0.0001 – 10 1.0 Main strength control; 0.3 – 2.0 is a reasonable range.
Anime mode anime_mode on/off off Only the dark side of edges is sharpened; the brightening part is dropped.
Overshoot control overshoot_ctrl on/off off Allows more overshoot where an edge pixel is surrounded by similar edge pixels.
Video level out video_level_out on/off off Tries to keep values below black and above white (with a small summation error); normally off.

Example

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

# adjusting color space from YUV420P10 to YUV444P16 for vsGLSLAdaptiveSharpen
clip = core.resize.Bicubic(clip=clip, format=vs.YUV444P16)
# changing range from limited to full range for GLSLAdaptiveSharpen
clip = core.resize.Bicubic(clip, format=vs.YUV444P16, range_in_s="limited", range_s="full")
# setting color range to PC (full) range.
prop_name = '_Range' if core.core_version.release_major >= 74 else '_ColorRange'
clip = core.std.SetFrameProps(clip=clip, **{prop_name: vs.RANGE_FULL})
# sharpening using GLSL Adaptive Sharpening
with open("F:/Hybrid/64bit/vsfilters/GLSL/parameterized/adaptive-sharpen.glsl") as glslf:
  glsl = glslf.read()
glsl = glsl.replace('#define curve_height    1.0', '#define curve_height    1.0000')
glsl = glsl.replace('#define anime_mode      false', '#define anime_mode    false')
glsl = glsl.replace('#define overshoot_ctrl  false', '#define overshoot_ctrl  false')
glsl = glsl.replace('#define video_level_out false', '#define video_level_out false')
clip = core.placebo.Shader(clip, shader_s=glsl, width=clip.width, height=clip.height, matrix=1)
# changing range from full to limited range for GLSLAdaptiveSharpen
clip = core.resize.Bicubic(clip, format=vs.YUV444P16,range_in_s="full", range_s="limited")
# setting color range to TV (limited) range.
prop_name = '_Range' if core.core_version.release_major >= 74 else '_ColorRange'
clip = core.std.SetFrameProps(clip=clip, **{prop_name: vs.RANGE_LIMITED})

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 four settings into the shader text and runs it with the input width and height, so the output size stays the same. The color matrix of the clip is passed to vs-placebo.
  • All four values are always written; they work on normalized values and are not scaled to the bit depth.
  • The shader expects full range: limited range input is converted to full range before the shader and back to limited range afterwards.
  • vs-placebo works in an RGB pipeline and converts back to YUV.
  • Adaptive Sharpen (GLSL) also offers the common options (Masked, ApplyOnlyTo, Merge, OnRegion, Gimmick).

Background

The shader

Adaptive sharpen was written by bacondither as an HLSL shader for MPC-HC/MPDN; Hybrid uses the mpv GLSL version (2020-11-14, BSD license). According to its author it shows less ringing, noise and banding than a normal sharpener. The shader header says it expects full-range, gamma-corrected input; the internal limits below the four user options are marked as tuned values that should not be changed. For a shader that adapts to local contrast see Vapoursynth CAS (GLSL).

Sources