Log in

View Full Version : Fixing mpv .hook shader to use in avslibplacebo


Reel.Deel
10th September 2022, 19:02
Hello there,

Wasn't sure where to put this but since I am using it for AviSynth+ I think here is a good spot.

So I have a shader meant to be used in mpv. The shader works in AviSynth+ with one minor problem, it outputs RGB stuffed in YUV444P16. So in order to view it properly I have to use CombinePlanes(planes="rgb", source_planes="YUV", pixel_type="RGBP16") afterwards.

Here is the actual shader (source (https://www.reddit.com/r/mpv/comments/hvwvsl/crt_scanline_glsl_shader/)):

//!HOOK OUTPUT
//!BIND OUTPUT
//!BIND MAIN

// Tunable parameters
#define BRIGHT_BOOST 1.2
#define DILATION 1.0
#define GAMMA_INPUT 2.0
#define GAMMA_OUTPUT 1.8
#define MASK_SIZE 1.0
#define MASK_STAGGER 0
#define MASK_STRENGTH 0.3
#define MASK_DOT_HEIGHT 1.0
#define MASK_DOT_WIDTH 1
#define SCANLINE_BEAM_WIDTH_MAX 1.5
#define SCANLINE_BEAM_WIDTH_MIN 1.5
#define SCANLINE_BRIGHT_MAX 0.65
#define SCANLINE_BRIGHT_MIN 0.35
#define SCANLINE_CUTOFF 400.0
#define SCANLINE_STRENGTH 1.0
#define SHARPNESS_H 0.5
#define SHARPNESS_V 1.0

#define PI 3.141592653589

const vec2 sharp = vec2(SHARPNESS_H, SHARPNESS_V);

vec4 hook()
{
vec2 fcoord = fract(MAIN_pos * MAIN_size - vec2(0.5));
vec2 base = MAIN_pos - MAIN_pt * fcoord;

// Apply half-circle S-Curve to distance for sharper interpolation
vec2 fstep = step(0.5, fcoord);
vec2 fcurve = fcoord - fstep;
fcurve = vec2(0.5) - sqrt(vec2(0.25) - fcurve * fcurve) * sign(vec2(0.5) - fcoord);
fcoord = mix(fcoord, fcurve, sharp);

vec4 color = MAIN_tex(base + MAIN_pt * fcoord);
color.rgb = pow(color.rgb, vec3(GAMMA_INPUT / (DILATION + 1.0)));

float luma = dot(color.rgb, vec3(0.2126, 0.7152, 0.0722));
float bright = (max(color.r, max(color.g, color.b)) + luma) / 2.0;
float scan_bright = clamp(bright, SCANLINE_BRIGHT_MIN, SCANLINE_BRIGHT_MAX);
float scan_beam = clamp(bright * SCANLINE_BEAM_WIDTH_MAX, SCANLINE_BEAM_WIDTH_MIN, SCANLINE_BEAM_WIDTH_MAX);
float scan_weight = 1.0 - pow(cos(MAIN_pos.y * 2.0 * PI * MAIN_size.y) * 0.5 + 0.5, scan_beam) * SCANLINE_STRENGTH;

if (MAIN_size.y >= SCANLINE_CUTOFF)
scan_weight = 1.0;

vec3 orig = color.rgb;
color.rgb *= vec3(scan_weight);
color.rgb = mix(color.rgb, orig, scan_bright);

float mask = 1.0 - MASK_STRENGTH;
ivec2 mod_fac = ivec2(MAIN_pos * OUTPUT_size / vec2(MASK_SIZE, MASK_DOT_HEIGHT * MASK_SIZE));
int dot_no = (mod_fac.x + (mod_fac.y % 2) * MASK_STAGGER) / MASK_DOT_WIDTH % 3;

vec3 mask_weight = vec3(mask);
mask_weight[dot_no] = 1.0;
color.rgb *= mask_weight;

color.rgb = pow(color.rgb, vec3(1.0 / GAMMA_OUTPUT));
color.rgb *= BRIGHT_BOOST;
return color;
}

What can be added to the shader code for it to convert RGB to YUV444?

Dogway
11th September 2022, 13:21
Simply convert to YUV, add the function, and then return with the call (output is full range though):

vec3 r709_YCC(vec3 RGB)
{
const mat3 conv_mat = mat3(
0.212649, 0.715169, 0.072182,
-0.114596, -0.385404, 0.500000,
0.500000, -0.454162, -0.045838);

return RGB.rgb * conv_mat;
}


return r709_YCC(color);

Reel.Deel
13th September 2022, 19:31
Dogway,

Thanks for the reply and the code. Excuse my ignorance but I have not experience with editing glsl code. If I put your code at the end then it just returns the video without the crt effect.

Dogway
14th September 2022, 03:13
Looks like it isn't working as it used when I made the retroarch shaders...

This isn't ignored, yet it doesn't output correct YUV444

mat3 conv_mat = mat3(
0.212649, 0.715169, 0.072182,
-0.114596, -0.385404, 0.500000,
0.500000, -0.454162, -0.045838);

color.rgb = clamp(color.rgb,0,1) * conv_mat;

return color;

I also tried to add vec3(0,-0.5,-0.5) as float YUV expects chroma planes in range [-0.5,0.5] but no dice.
Maybe there's something else going on after the output.

StvG
14th September 2022, 12:05
Looks like it isn't working as it used when I made the retroarch shaders...

This isn't ignored, yet it doesn't output correct YUV444

mat3 conv_mat = mat3(
0.212649, 0.715169, 0.072182,
-0.114596, -0.385404, 0.500000,
0.500000, -0.454162, -0.045838);

color.rgb = clamp(color.rgb,0,1) * conv_mat;

return color;

I also tried to add vec3(0,-0.5,-0.5) as float YUV expects chroma planes in range [-0.5,0.5] but no dice.
Maybe there's something else going on after the output.


This one works:

mat3 conv_mat = mat3(
0.212649, 0.715169, 0.072182,
-0.114596, -0.385404, 0.500000,
0.500000, -0.454162, -0.045838);

color.rgb = clamp(color.rgb,0,1) * (conv_mat) * vec3(0.8588, 1.0, 1.0) / vec3(1.0, 1.143, 1.143) + vec3(0.0627 , 0.5, 0.5);

return color;

Reel.Deel
18th September 2022, 18:54
This one works:

mat3 conv_mat = mat3(
0.212649, 0.715169, 0.072182,
-0.114596, -0.385404, 0.500000,
0.500000, -0.454162, -0.045838);

color.rgb = clamp(color.rgb,0,1) * (conv_mat) * vec3(0.8588, 1.0, 1.0) / vec3(1.0, 1.143, 1.143) + vec3(0.0627 , 0.5, 0.5);

return color;

Thanks StvG! That does indeed work.