| 1 |
/* |
| 2 |
* This file is part of Bino, a 3D video player. |
| 3 |
* |
| 4 |
* Copyright (C) 2022, 2023, 2024, 2025 |
| 5 |
* Martin Lambers <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
uniform sampler2D plane0; |
| 22 |
uniform sampler2D plane1; |
| 23 |
uniform sampler2D plane2; |
| 24 |
uniform sampler2D plane3; |
| 25 |
|
| 26 |
const int Format_RGB = 1; |
| 27 |
const int Format_YUVp = 2; |
| 28 |
const int Format_YVUp = 3; |
| 29 |
const int Format_YUVsp = 4; |
| 30 |
const int Format_Y = 5; |
| 31 |
const int planeFormat = $PLANE_FORMAT; |
| 32 |
|
| 33 |
const bool colorRangeSmall = $COLOR_RANGE_SMALL; |
| 34 |
|
| 35 |
const int CS_BT601 = 1; |
| 36 |
const int CS_BT709 = 2; |
| 37 |
const int CS_AdobeRGB = 3; |
| 38 |
const int CS_BT2020 = 4; |
| 39 |
const int colorSpace = $COLOR_SPACE; |
| 40 |
|
| 41 |
const int CT_NOOP = 1; |
| 42 |
const int CT_ST2084 = 2; |
| 43 |
const int CT_STD_B67 = 3; |
| 44 |
const int colorTransfer = $COLOR_TRANSFER; |
| 45 |
uniform float masteringWhite; |
| 46 |
|
| 47 |
smooth in vec2 vtexcoord; |
| 48 |
|
| 49 |
layout(location = 0) out vec4 fcolor; |
| 50 |
|
| 51 |
float to_linear(float x) |
| 52 |
{ |
| 53 |
const float c0 = 0.077399380805; // 1.0 / 12.92 |
| 54 |
const float c1 = 0.947867298578; // 1.0 / 1.055; |
| 55 |
return (x <= 0.04045 ? (x * c0) : pow((x + 0.055) * c1, 2.4)); |
| 56 |
} |
| 57 |
|
| 58 |
vec3 rgb_to_linear(vec3 rgb) |
| 59 |
{ |
| 60 |
return vec3(to_linear(rgb.r), to_linear(rgb.g), to_linear(rgb.b)); |
| 61 |
} |
| 62 |
|
| 63 |
void main(void) |
| 64 |
{ |
| 65 |
vec3 yuv = vec3(1.0, 0.0, 0.0); |
| 66 |
vec3 rgb = vec3(0.0, 1.0, 0.0); |
| 67 |
if (planeFormat == Format_RGB) { |
| 68 |
rgb = texture(plane0, vtexcoord).rgb; |
| 69 |
} else if (planeFormat == Format_Y) { |
| 70 |
rgb = texture(plane0, vtexcoord).rrr; |
| 71 |
} else { |
| 72 |
if (planeFormat == Format_YUVp) { |
| 73 |
yuv = vec3( |
| 74 |
texture(plane0, vtexcoord).r, |
| 75 |
texture(plane1, vtexcoord).r, |
| 76 |
texture(plane2, vtexcoord).r); |
| 77 |
} else if (planeFormat == Format_YVUp) { |
| 78 |
yuv = vec3( |
| 79 |
texture(plane0, vtexcoord).r, |
| 80 |
texture(plane2, vtexcoord).r, |
| 81 |
texture(plane1, vtexcoord).r); |
| 82 |
} else if (planeFormat == Format_YUVsp) { |
| 83 |
yuv = vec3( |
| 84 |
texture(plane0, vtexcoord).r, |
| 85 |
texture(plane1, vtexcoord).rg); |
| 86 |
} |
| 87 |
mat4 m; |
| 88 |
// The following matrices are the same as used by Qt, |
| 89 |
// see qtmultimedia/src/multimedia/video/qvideotexturehelper.cpp |
| 90 |
if (colorSpace == CS_AdobeRGB) { |
| 91 |
m = mat4( |
| 92 |
1.0, 1.0, 1.0, 0.0, |
| 93 |
0.0, -0.344, 1.772, 0.0, |
| 94 |
1.402, -0.714, 0.0, 0.0, |
| 95 |
-0.701, 0.529, -0.886, 1.0); |
| 96 |
} else if (colorSpace == CS_BT709) { |
| 97 |
if (colorRangeSmall) { |
| 98 |
m = mat4( |
| 99 |
1.1644, 1.1644, 1.1644, 0.0, |
| 100 |
0.0, -0.2132, 2.1124, 0.0, |
| 101 |
1.7927, -0.5329, 0.0, 0.0, |
| 102 |
-0.9729, 0.3015, -1.1334, 1.0); |
| 103 |
} else { |
| 104 |
m = mat4( |
| 105 |
1.0, 1.0, 1.0, 0.0, |
| 106 |
0.0, -0.187324, 1.8556, 0.0, |
| 107 |
1.5748, -0.468124, 0.0, 0.0, |
| 108 |
-0.790488, 0.329010, -0.931439, 1.0); |
| 109 |
} |
| 110 |
} else if (colorSpace == CS_BT2020) { |
| 111 |
if (colorRangeSmall) { |
| 112 |
m = mat4( |
| 113 |
1.1644, 1.1644, 1.1644, 0.0, |
| 114 |
0.0, -0.1874, 2.1418, 0.0, |
| 115 |
1.6787, -0.6504, 0.0, 0.0, |
| 116 |
-0.9157, 0.3475, -1.1483, 1.0); |
| 117 |
} else { |
| 118 |
m = mat4( |
| 119 |
1.0, 1.0, 1.0, 0.0, |
| 120 |
0.0, -0.1646, 1.8814, 0.0, |
| 121 |
1.4746, -0.5714, 0.0, 0.0, |
| 122 |
-0.7402, 0.3694, -0.9445, 1.0); |
| 123 |
} |
| 124 |
} else { |
| 125 |
if (colorRangeSmall) { |
| 126 |
m = mat4( |
| 127 |
1.164, 1.164, 1.164, 0.0, |
| 128 |
0.0, -0.392, 2.017, 0.0, |
| 129 |
1.596, -0.813, 0.0, 0.0, |
| 130 |
-0.8708, 0.5296, -1.081, 1.0); |
| 131 |
} else { |
| 132 |
m = mat4( |
| 133 |
1.0, 1.0, 1.0, 0.0, |
| 134 |
0.0, -0.1646, 1.42, 0.0, |
| 135 |
1.772, -0.57135, 0.0, 0.0, |
| 136 |
-0.886, 0.36795, -0.71, 1.0); |
| 137 |
} |
| 138 |
} |
| 139 |
rgb = (m * vec4(yuv, 1.0)).rgb; |
| 140 |
} |
| 141 |
if (colorTransfer == CT_ST2084 || colorTransfer == CT_STD_B67) { |
| 142 |
// This code was reconstructed from the mess in qtmultimedia/src/multimedia/video; |
| 143 |
// it is distributed there over various shaders and C++ files. |
| 144 |
// 1. scale |
| 145 |
const float maxLum = 1.0; |
| 146 |
float scale = 1.0; |
| 147 |
float y = (yuv.x - 16.0 / 256.0) * 256.0 / 219.0; // XXX This looks wrong!? |
| 148 |
float p = y / masteringWhite; |
| 149 |
float ks = 1.5 * maxLum - 0.5; |
| 150 |
if (p > ks) { |
| 151 |
float t = (p - ks) / (1.0 - ks); |
| 152 |
float t2 = t * t; |
| 153 |
float t3 = t * t2; |
| 154 |
p = (2.0 * t3 - 3.0 * t2 + 1.0) * ks + (t3 - 2.0 * t2 + t) * (1.0 - ks) + (-2.0 * t3 + 3.0 * t2) * maxLum; |
| 155 |
float newY = p * masteringWhite; |
| 156 |
scale = newY / y; |
| 157 |
} |
| 158 |
rgb *= scale; |
| 159 |
// 2. tonemap |
| 160 |
if (colorTransfer == CT_ST2084) { |
| 161 |
const vec3 one_over_m1 = vec3(8192.0 / 1305.0); |
| 162 |
const vec3 one_over_m2 = vec3(32.0 / 2523.0); |
| 163 |
const float c1 = 107.0 / 128.0; |
| 164 |
const float c2 = 2413.0 / 128.0; |
| 165 |
const float c3 = 2392.0 / 128.0; |
| 166 |
vec3 e = pow(rgb, one_over_m2); |
| 167 |
vec3 num = max(e - c1, 0.0); |
| 168 |
vec3 den = c2 - c3 * e; |
| 169 |
rgb = pow(num / den, one_over_m1) * 10000.0 / 100.0; |
| 170 |
} else if (colorTransfer == CT_STD_B67) { |
| 171 |
const float a = 0.17883277; |
| 172 |
const float b = 0.28466892; // = 1 - 4a |
| 173 |
const float c = 0.55991073; // = 0.5 - a ln(4a) |
| 174 |
bvec3 cutoff = lessThan(rgb, vec3(0.5)); |
| 175 |
vec3 low = rgb * rgb / 3.0; |
| 176 |
vec3 high = (exp((rgb - c) / a) + b) / 12.0; |
| 177 |
rgb = mix(high, low, cutoff); |
| 178 |
float lum = dot(rgb, vec3(0.2627, 0.6780, 0.0593)); |
| 179 |
float y = pow(lum, 0.2); // gamma-1 with gamma = 1.2 |
| 180 |
rgb *= y; |
| 181 |
} |
| 182 |
// 3. convert rec2020 to sRGB |
| 183 |
rgb = rgb * mat3( |
| 184 |
1.6605, -0.5876, -0.0728, |
| 185 |
-0.1246, 1.1329, -0.0083, |
| 186 |
-0.0182, -0.1006, 1.1187); |
| 187 |
} else { |
| 188 |
rgb = rgb_to_linear(rgb); |
| 189 |
} |
| 190 |
fcolor = vec4(rgb, 1.0); |
| 191 |
} |
| 192 |
|