VideoHelp Forum
+ Reply to Thread
Results 1 to 2 of 2
Thread
  1. Using libplacebo in Vapoursynth, I thought I try to create an alternative to Levels() and ended up with:
    Code:
    //!HOOK MAIN
    //!BIND HOOKED
    //!DESC Levels
    
    // Define levels adjustment parameters
    #define input_low 16.0
    #define input_high 235.0
    #define gamma 1.0
    #define output_low 0.0
    #define output_high 255.0
    
    /* A comprehensive Levels adjustment shader for mpv
       Similar to AviSynth Levels filter
       v0.1
    */
    
    vec4 hook() {
        vec4 color = HOOKED_texOff(0);
        
        // Normalize input parameters to [0, 1] range
        float input_low_norm = input_low / 255.0;
        float input_high_norm = input_high / 255.0;
        float output_low_norm = output_low / 255.0;
        float output_high_norm = output_high / 255.0;
    
        // Apply input low/high levels
        color.rgb = (color.rgb - input_low_norm) / (input_high_norm - input_low_norm);
        color.rgb = clamp(color.rgb, 0.0, 1.0);
        
        // Apply gamma correction using 1/gamma
        color.rgb = pow(color.rgb, vec3(1.0 / gamma));
        
        // Apply output low/high levels
        color.rgb = color.rgb * (output_high_norm - output_low_norm) + output_low_norm;
        
        return color;
    }
    using:
    Code:
    clip = core.resize.Bicubic(clip=clip, format=vs.YUV444P16, range_s="limited")
    with open("F:/Hybrid/64bit/vsfilters/GLSL/parameterized/Levels.glsl") as glslf:
      glsl = glslf.read()
    glsl = glsl.replace('#define input_low 16.0', '#define input_low 16')
    glsl = glsl.replace('#define input_high 235.0', '#define input_high 235')
    glsl = glsl.replace('#define output_low 0.0', '#define output_low 16')
    glsl = glsl.replace('#define output_high 255.0', '#define output_high 235')
    glsl = glsl.replace('#define gamma 1.0', '#define gamma 1')
    clip = core.placebo.Shader(clip=clip, shader_s=glsl, width=clip.width, height=clip.height)
    which properly changes the defines to:
    Code:
    #define input_low 16
    #define input_high 235
    #define gamma 1
    #define output_low 16
    #define output_high 235
    I expected the in- and output to be identical, but there seems to be a mistake, since the lower values all get clamped.


    Does anyone see the mistake I made?
    I suspect I made a logical mistake in the scaling of the input parameters,..

    Cu Selur
    Last edited by Selur; 13th Jul 2024 at 02:01.
    users currently on my ignore list: deadrats, Stears555
    Quote Quote  
  2. Suspecting that the issue to early clamping, I used:
    Code:
    vec4 hook() {
        vec4 color = HOOKED_texOff(0);
    
        // Normalize input and output parameters to [0, 1] range
        float input_low_norm = input_low / 255.0;
        float input_high_norm = input_high / 255.0;
        float output_low_norm = output_low / 255.0;
        float output_high_norm = output_high / 255.0;
    
        // Apply input low/high levels
        vec3 normalized = (color.rgb - input_low_norm) / (input_high_norm - input_low_norm);
    
        // Apply gamma correction using 1/gamma
        vec3 gamma_corrected = pow(normalized, vec3(1.0 / gamma));
    
        // Apply output low/high levels
        vec3 adjusted_output = gamma_corrected * (output_high_norm - output_low_norm) + output_low_norm;
    
        // Ensure values are within the [0, 1] range
        color.rgb = clamp(adjusted_output, 0.0, 1.0);
    
        return color;
    }

    => that looks different, but still wrong.

    Cu Selur
    users currently on my ignore list: deadrats, Stears555
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!