non linear yuv texture filtering
This commit is contained in:
		@ -1,6 +1,20 @@
 | 
				
			|||||||
#PY  <- Needed to identify #
 | 
					#PY  <- Needed to identify #
 | 
				
			||||||
# RozK
 | 
					
 | 
				
			||||||
# Custom preset for GoPro, 8:7 ratio, without hypersmooth, output widened scaled to 4K (2160p), high bitrate
 | 
					# Fisheye removal for GoPro 11+, 8:7 ratio, without hypersmooth
 | 
				
			||||||
 | 
					# Copyright (C) 2025 Roz K <roz@rozk.net>
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# This program is free software: you can redistribute it and/or modify
 | 
				
			||||||
 | 
					# it under the terms of the GNU Affero General Public License as
 | 
				
			||||||
 | 
					# published by the Free Software Foundation, either version 3 of the
 | 
				
			||||||
 | 
					# License, or (at your option) any later version.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# This program is distributed in the hope that it will be useful,
 | 
				
			||||||
 | 
					# but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
				
			||||||
 | 
					# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
				
			||||||
 | 
					# GNU Affero General Public License for more details.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# You should have received a copy of the GNU Affero General Public License
 | 
				
			||||||
 | 
					# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
adm = Avidemux()
 | 
					adm = Avidemux()
 | 
				
			||||||
adm.videoCodec(
 | 
					adm.videoCodec(
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,7 @@
 | 
				
			|||||||
#version 130
 | 
					#version 130
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Fisheye removal for GoPro 11+, 8:7 ratio, without hypersmooth
 | 
					// Fisheye removal for GoPro 11+, 8:7 ratio, without hypersmooth
 | 
				
			||||||
// Copyright (C) 2025 Jean-Baptiste Berlioz
 | 
					// Copyright (C) 2025 Roz K <roz@rozk.net>
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
// This program is free software: you can redistribute it and/or modify
 | 
					// This program is free software: you can redistribute it and/or modify
 | 
				
			||||||
// it under the terms of the GNU Affero General Public License as
 | 
					// it under the terms of the GNU Affero General Public License as
 | 
				
			||||||
@ -17,14 +18,16 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#extension GL_ARB_texture_rectangle: enable
 | 
					#extension GL_ARB_texture_rectangle: enable
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define non_linear_decoding
 | 
					#define color_conversion
 | 
				
			||||||
#define rotate_180
 | 
					#define texture_filtering
 | 
				
			||||||
 | 
					//#define rotate_180
 | 
				
			||||||
//#define debug_borders
 | 
					//#define debug_borders
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#undef highp // defined by Qt-OpenGl
 | 
					#undef highp // defined by Qt-OpenGl
 | 
				
			||||||
precision highp float;
 | 
					precision highp float;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// uniforms
 | 
					// uniforms
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uniform sampler2DRect myTextureY;
 | 
					uniform sampler2DRect myTextureY;
 | 
				
			||||||
uniform sampler2DRect myTextureU;
 | 
					uniform sampler2DRect myTextureU;
 | 
				
			||||||
uniform sampler2DRect myTextureV;
 | 
					uniform sampler2DRect myTextureV;
 | 
				
			||||||
@ -32,18 +35,21 @@ uniform vec2 myResolution;
 | 
				
			|||||||
uniform float pts;
 | 
					uniform float pts;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// parameters
 | 
					// parameters
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const vec2  sensor_size              = vec2(5.949440, 5.205760);
 | 
					const vec2  sensor_size              = vec2(5.949440, 5.205760);
 | 
				
			||||||
 | 
					const vec2  rectilinear_scale        = vec2(1.138819, 1.0);
 | 
				
			||||||
const float equidistant_focal_length = 2.970000;
 | 
					const float equidistant_focal_length = 2.970000;
 | 
				
			||||||
const float rectilinear_focal_length = 2.167601;
 | 
					const float rectilinear_focal_length = 2.167601;
 | 
				
			||||||
const vec2 rectilinear_scale = vec2(1.138819, 1.0);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
// constants
 | 
					// constants
 | 
				
			||||||
const int subsampling = 4;
 | 
					
 | 
				
			||||||
 | 
					const int   subsampling       = 8;
 | 
				
			||||||
const vec2  subsampling_step  = vec2(1.0 / float(subsampling));
 | 
					const vec2  subsampling_step  = vec2(1.0 / float(subsampling));
 | 
				
			||||||
const vec2  subsampling_start = subsampling_step * 0.5 - vec2(0.5);
 | 
					const vec2  subsampling_start = subsampling_step * 0.5 - vec2(0.5);
 | 
				
			||||||
const float subsampling_scale = 1.0 / float(subsampling * subsampling);
 | 
					const float subsampling_scale = 1.0 / float(subsampling * subsampling);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// variables
 | 
					// reprojection
 | 
				
			||||||
 | 
					
 | 
				
			||||||
vec2 texture_center;
 | 
					vec2 texture_center;
 | 
				
			||||||
vec2 texture_to_sensor;
 | 
					vec2 texture_to_sensor;
 | 
				
			||||||
vec2 sensor_to_texture;
 | 
					vec2 sensor_to_texture;
 | 
				
			||||||
@ -57,47 +63,41 @@ void initialize() {
 | 
				
			|||||||
    sensor_to_texture = myResolution / sensor_size;
 | 
					    sensor_to_texture = myResolution / sensor_size;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
vec2 equidistant_to_rectilinear(const in vec2 coord) {
 | 
					vec2 equidistant_to_rectilinear(const in vec2 equidistant) {
 | 
				
			||||||
    float rectilinear_radius = length(coord);
 | 
					    float rectilinear_radius = length(equidistant);
 | 
				
			||||||
    float rectilinear_angle = atan(rectilinear_radius / rectilinear_focal_length);
 | 
					    float rectilinear_angle = atan(rectilinear_radius / rectilinear_focal_length);
 | 
				
			||||||
    float equidistant_radius = rectilinear_angle * equidistant_focal_length;
 | 
					    float equidistant_radius = rectilinear_angle * equidistant_focal_length;
 | 
				
			||||||
    return coord * (equidistant_radius / rectilinear_radius);
 | 
					    return equidistant * (equidistant_radius / rectilinear_radius);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
vec3 sample_texture(const in vec2 coord) {
 | 
					// color space (https://en.wikipedia.org/wiki/Rec._709)
 | 
				
			||||||
    vec2 rectilinear = equidistant_to_rectilinear((coord - texture_center) * texture_to_sensor);
 | 
					
 | 
				
			||||||
    vec2 y_coord = texture_center + rectilinear * sensor_to_texture;
 | 
					#ifdef color_conversion
 | 
				
			||||||
#ifdef debug_borders
 | 
					
 | 
				
			||||||
    if (y_coord.x < 0.0 || y_coord.y < 0.0 || y_coord.x > myResolution.x || y_coord.y > myResolution.y) {
 | 
					const float luminance_min   =  16.0 / 255.0;
 | 
				
			||||||
        return vec3(235.0 / 255.0, 240.0 / 255.0, 240.0 / 255.0);
 | 
					const float luminance_max   = 235.0 / 255.0;
 | 
				
			||||||
    }
 | 
					const float luminance_range = luminance_max - luminance_min;
 | 
				
			||||||
#endif
 | 
					
 | 
				
			||||||
    vec2 uv_coord = y_coord * 0.5;
 | 
					const vec2 chroma_min    = vec2( 16.0 / 255.0);
 | 
				
			||||||
    return vec3(
 | 
					const vec2 chroma_max    = vec2(240.0 / 255.0);
 | 
				
			||||||
        texture2DRect(myTextureY, y_coord).x,
 | 
					const vec2 chroma_range  = chroma_max - chroma_min;
 | 
				
			||||||
        texture2DRect(myTextureU, uv_coord).x,
 | 
					const vec2 chroma_center = vec2(0.5, 0.5);
 | 
				
			||||||
        texture2DRect(myTextureV, uv_coord).x);
 | 
					
 | 
				
			||||||
 | 
					const vec3 yuv_min    = vec3(luminance_min,   chroma_min);
 | 
				
			||||||
 | 
					const vec3 yuv_range  = vec3(luminance_range, chroma_range);
 | 
				
			||||||
 | 
					const vec3 yuv_center = vec3(0.0, chroma_center);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					float decode_luminance(const in float x) {
 | 
				
			||||||
 | 
					    float y = (x - luminance_min) / luminance_range;
 | 
				
			||||||
 | 
					    return (y < 0.081) ? (y / 4.5) : pow((y + 0.099) / 1.099, 1.0 / 0.45);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef non_linear_decoding
 | 
					vec2 decode_chroma(const in vec2 yz) {
 | 
				
			||||||
 | 
					    return (yz - chroma_min) / chroma_range - chroma_center;
 | 
				
			||||||
// https://en.wikipedia.org/wiki/Rec._709
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
#define y_min    ( 16.0 / 255.0)
 | 
					 | 
				
			||||||
#define y_max    (235.0 / 255.0)
 | 
					 | 
				
			||||||
#define uv_min   ( 16.0 / 255.0)
 | 
					 | 
				
			||||||
#define uv_max   (240.0 / 255.0)
 | 
					 | 
				
			||||||
#define y_range  (y_max - y_min)
 | 
					 | 
				
			||||||
#define uv_range (uv_max - uv_min)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const vec3 yuv_min = vec3(y_min, uv_min, uv_min);
 | 
					 | 
				
			||||||
const vec3 yuv_range = vec3(y_range, uv_range, uv_range);
 | 
					 | 
				
			||||||
const vec3 yuv_center = vec3(0.0, 0.5, 0.5);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
vec3 decode_pixel(const in vec3 pixel) {
 | 
					vec3 decode_pixel(const in vec3 pixel) {
 | 
				
			||||||
    vec3 color = (pixel - yuv_min) / yuv_range - yuv_center;
 | 
					    return vec3(decode_luminance(pixel.x), decode_chroma(pixel.yz));
 | 
				
			||||||
    float l = (color.x < 0.081) ? (color.x / 4.5) : pow((color.x + 0.099) / 1.099, 1.0 / 0.45);
 | 
					 | 
				
			||||||
    return vec3(l, color.yz);
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
vec3 encode_color(const in vec3 color) {
 | 
					vec3 encode_color(const in vec3 color) {
 | 
				
			||||||
@ -107,10 +107,88 @@ vec3 encode_color(const in vec3 color) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define decode_pixel(_pixel) _pixel
 | 
					#define decode_luminance(_x) (_x)
 | 
				
			||||||
#define encode_color(_color) _color
 | 
					#define decode_chroma(_yz)   (_yz)
 | 
				
			||||||
 | 
					#define decode_pixel(_pixel) (_pixel)
 | 
				
			||||||
 | 
					#define encode_color(_color) (_color)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif // non_linear_decoding
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// texture filtering
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef texture_filtering
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const vec2 half_pixel = vec2(0.5);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const vec2 corner_a = vec2(0.0, 0.0) + half_pixel;
 | 
				
			||||||
 | 
					const vec2 corner_b = vec2(1.0, 0.0) + half_pixel;
 | 
				
			||||||
 | 
					const vec2 corner_c = vec2(0.0, 1.0) + half_pixel;
 | 
				
			||||||
 | 
					const vec2 corner_d = vec2(1.0, 1.0) + half_pixel;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					vec2 bilinear_params(const in vec2 coord, out vec2 a, out vec2 b, out vec2 c, out vec2 d) {
 | 
				
			||||||
 | 
					    vec2 i, f = modf(coord - half_pixel, i);
 | 
				
			||||||
 | 
					    a = i + corner_a;
 | 
				
			||||||
 | 
					    b = i + corner_b;
 | 
				
			||||||
 | 
					    c = i + corner_c;
 | 
				
			||||||
 | 
					    d = i + corner_d;
 | 
				
			||||||
 | 
					    return f;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					vec3 texture_filter(const in vec2 y_coord) {
 | 
				
			||||||
 | 
					    vec2 a, b, c, d, f = bilinear_params(y_coord, a, b, c, d);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    float luminance = mix(
 | 
				
			||||||
 | 
					        mix(
 | 
				
			||||||
 | 
					            decode_luminance(texture2DRect(myTextureY, a).x),
 | 
				
			||||||
 | 
					            decode_luminance(texture2DRect(myTextureY, b).x),
 | 
				
			||||||
 | 
					            f.x),
 | 
				
			||||||
 | 
					        mix(
 | 
				
			||||||
 | 
					            decode_luminance(texture2DRect(myTextureY, c).x),
 | 
				
			||||||
 | 
					            decode_luminance(texture2DRect(myTextureY, d).x),
 | 
				
			||||||
 | 
					            f.x),
 | 
				
			||||||
 | 
					        f.y);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    f = bilinear_params(y_coord * 0.5, a, b, c, d);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    vec2 chroma = mix(
 | 
				
			||||||
 | 
					        mix(
 | 
				
			||||||
 | 
					            decode_chroma(vec2(texture2DRect(myTextureU, a).x, texture2DRect(myTextureV, a).x)),
 | 
				
			||||||
 | 
					            decode_chroma(vec2(texture2DRect(myTextureU, b).x, texture2DRect(myTextureV, b).x)),
 | 
				
			||||||
 | 
					            f.x),
 | 
				
			||||||
 | 
					        mix(
 | 
				
			||||||
 | 
					            decode_chroma(vec2(texture2DRect(myTextureU, c).x, texture2DRect(myTextureV, c).x)),
 | 
				
			||||||
 | 
					            decode_chroma(vec2(texture2DRect(myTextureU, d).x, texture2DRect(myTextureV, d).x)),
 | 
				
			||||||
 | 
					            f.x),
 | 
				
			||||||
 | 
					        f.y);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return vec3(luminance, chroma);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					vec3 texture_filter(const in vec2 y_coord) {
 | 
				
			||||||
 | 
					    vec2 uv_coord = y_coord * 0.5;
 | 
				
			||||||
 | 
					    return decode_pixel(vec3(
 | 
				
			||||||
 | 
					        texture2DRect(myTextureY, y_coord).x,
 | 
				
			||||||
 | 
					        texture2DRect(myTextureU, uv_coord).x,
 | 
				
			||||||
 | 
					        texture2DRect(myTextureV, uv_coord).x));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					vec3 texture_sample(const in vec2 equidistant) {
 | 
				
			||||||
 | 
					    vec2 rectilinear = texture_center + sensor_to_texture *
 | 
				
			||||||
 | 
					        equidistant_to_rectilinear((equidistant - texture_center) * texture_to_sensor);
 | 
				
			||||||
 | 
					#ifdef debug_borders
 | 
				
			||||||
 | 
					    if (rectilinear.x < 0.0 || rectilinear.y < 0.0 ||
 | 
				
			||||||
 | 
					        rectilinear.x > myResolution.x || rectilinear.y > myResolution.y) {
 | 
				
			||||||
 | 
					        return vec3(235.0 / 255.0, 240.0 / 255.0, 240.0 / 255.0);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					    return texture_filter(rectilinear);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void main() {
 | 
					void main() {
 | 
				
			||||||
    initialize();
 | 
					    initialize();
 | 
				
			||||||
@ -119,7 +197,7 @@ void main() {
 | 
				
			|||||||
    for (int y = 0; y < subsampling; y++) {
 | 
					    for (int y = 0; y < subsampling; y++) {
 | 
				
			||||||
        for (int x = 0; x < subsampling; x++) {
 | 
					        for (int x = 0; x < subsampling; x++) {
 | 
				
			||||||
            vec2 offset = subsampling_start + subsampling_step * vec2(x, y);
 | 
					            vec2 offset = subsampling_start + subsampling_step * vec2(x, y);
 | 
				
			||||||
            color += decode_pixel(sample_texture(coord + offset));
 | 
					            color += texture_sample(coord + offset);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    gl_FragColor = vec4(encode_color(color * subsampling_scale), 1.0);
 | 
					    gl_FragColor = vec4(encode_color(color * subsampling_scale), 1.0);
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,5 @@
 | 
				
			|||||||
# Fisheye removal for GoPro 11+, 8:7 ratio, without hypersmooth
 | 
					# Fisheye removal for GoPro 11+, 8:7 ratio, without hypersmooth
 | 
				
			||||||
# Copyright (C) 2025 Jean-Baptiste Berlioz
 | 
					# Copyright (C) 2025 Roz K <roz@rozk.net>
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# This program is free software: you can redistribute it and/or modify
 | 
					# This program is free software: you can redistribute it and/or modify
 | 
				
			||||||
# it under the terms of the GNU Affero General Public License as
 | 
					# it under the terms of the GNU Affero General Public License as
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user