1
0
Files
avidemux_shaders/gopro_8:7/unfish_gopro_8:7.glsl
2025-09-27 05:26:34 +02:00

201 lines
6.3 KiB
GLSL

#version 130
// 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/>.
#extension GL_ARB_texture_rectangle: enable
#define color_conversion
#define texture_filtering
#define rotate_180
//#define debug_borders
#undef highp // defined by Qt-OpenGl
precision highp float;
// uniforms
uniform sampler2DRect myTextureY;
uniform sampler2DRect myTextureU;
uniform sampler2DRect myTextureV;
uniform vec2 myResolution;
uniform float pts;
// parameters
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 rectilinear_focal_length = 2.167601;
// constants
const int subsampling = 8;
const vec2 subsampling_step = vec2(1.0 / float(subsampling));
const vec2 subsampling_start = subsampling_step * 0.5 - vec2(0.5);
const float subsampling_scale = 1.0 / float(subsampling * subsampling);
// reprojection
vec2 texture_center;
vec2 texture_to_sensor;
vec2 sensor_to_texture;
void initialize() {
texture_center = myResolution * 0.5;
texture_to_sensor = (sensor_size / myResolution) * rectilinear_scale;
#ifdef rotate_180
texture_to_sensor *= -1.0;
#endif
sensor_to_texture = myResolution / sensor_size;
}
vec2 equidistant_to_rectilinear(const in vec2 equidistant) {
float rectilinear_radius = length(equidistant);
float rectilinear_angle = atan(rectilinear_radius / rectilinear_focal_length);
float equidistant_radius = rectilinear_angle * equidistant_focal_length;
return equidistant * (equidistant_radius / (rectilinear_radius > 0.0 ? rectilinear_radius : 1.0));
}
// color space (https://en.wikipedia.org/wiki/Rec._709)
#ifdef color_conversion
const float luminance_min = 16.0 / 255.0;
const float luminance_max = 235.0 / 255.0;
const float luminance_range = luminance_max - luminance_min;
const vec2 chroma_min = vec2( 16.0 / 255.0);
const vec2 chroma_max = vec2(240.0 / 255.0);
const vec2 chroma_range = chroma_max - chroma_min;
const vec2 chroma_center = vec2(0.5, 0.5);
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);
}
vec2 decode_chroma(const in vec2 yz) {
return (yz - chroma_min) / chroma_range - chroma_center;
}
vec3 decode_pixel(const in vec3 pixel) {
return vec3(decode_luminance(pixel.x), decode_chroma(pixel.yz));
}
vec3 encode_color(const in vec3 color) {
float y = (color.x < 0.018) ? (color.x * 4.5) : (pow(color.x, 0.45) * 1.099 - 0.099);
return (vec3(y, color.yz) + yuv_center) * yuv_range + yuv_min;
}
#else
#define decode_luminance(_x) (_x)
#define decode_chroma(_yz) (_yz)
#define decode_pixel(_pixel) (_pixel)
#define encode_color(_color) (_color)
#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() {
initialize();
vec2 coord = gl_TexCoord[0].xy;
vec3 color = vec3(0.0);
for (int y = 0; y < subsampling; y++) {
for (int x = 0; x < subsampling; x++) {
vec2 offset = subsampling_start + subsampling_step * vec2(x, y);
color += texture_sample(coord + offset);
}
}
gl_FragColor = vec4(encode_color(color * subsampling_scale), 1.0);
}