]> git.sesse.net Git - ffmpeg/blob - libavfilter/opencl/colorspace_common.cl
avfilter/vf_psnr: remove unnecessary check
[ffmpeg] / libavfilter / opencl / colorspace_common.cl
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #define ST2084_MAX_LUMINANCE 10000.0f
20 #define REFERENCE_WHITE 100.0f
21
22 #if chroma_loc == 1
23     #define chroma_sample(a,b,c,d) (((a) + (c)) * 0.5f)
24 #elif chroma_loc == 3
25     #define chroma_sample(a,b,c,d) (a)
26 #elif chroma_loc == 4
27     #define chroma_sample(a,b,c,d) (((a) + (b)) * 0.5f)
28 #elif chroma_loc == 5
29     #define chroma_sample(a,b,c,d) (c)
30 #elif chroma_loc == 6
31     #define chroma_sample(a,b,c,d) (((c) + (d)) * 0.5f)
32 #else
33     #define chroma_sample(a,b,c,d) (((a) + (b) + (c) + (d)) * 0.25f)
34 #endif
35
36 constant const float ST2084_M1 = 0.1593017578125f;
37 constant const float ST2084_M2 = 78.84375f;
38 constant const float ST2084_C1 = 0.8359375f;
39 constant const float ST2084_C2 = 18.8515625f;
40 constant const float ST2084_C3 = 18.6875f;
41
42 float get_luma_dst(float3 c) {
43     return luma_dst.x * c.x + luma_dst.y * c.y + luma_dst.z * c.z;
44 }
45
46 float get_luma_src(float3 c) {
47     return luma_src.x * c.x + luma_src.y * c.y + luma_src.z * c.z;
48 }
49
50 float3 get_chroma_sample(float3 a, float3 b, float3 c, float3 d) {
51     return chroma_sample(a, b, c, d);
52 }
53
54 float eotf_st2084(float x) {
55     float p = powr(x, 1.0f / ST2084_M2);
56     float a = max(p -ST2084_C1, 0.0f);
57     float b = max(ST2084_C2 - ST2084_C3 * p, 1e-6f);
58     float c  = powr(a / b, 1.0f / ST2084_M1);
59     return x > 0.0f ? c * ST2084_MAX_LUMINANCE / REFERENCE_WHITE : 0.0f;
60 }
61
62 __constant const float HLG_A = 0.17883277f;
63 __constant const float HLG_B = 0.28466892f;
64 __constant const float HLG_C = 0.55991073f;
65
66 // linearizer for HLG
67 float inverse_oetf_hlg(float x) {
68     float a = 4.0f * x * x;
69     float b = exp((x - HLG_C) / HLG_A) + HLG_B;
70     return x < 0.5f ? a : b;
71 }
72
73 // delinearizer for HLG
74 float oetf_hlg(float x) {
75     float a = 0.5f * sqrt(x);
76     float b = HLG_A * log(x - HLG_B) + HLG_C;
77     return x <= 1.0f ? a : b;
78 }
79
80 float3 ootf_hlg(float3 c, float peak) {
81     float luma = get_luma_src(c);
82     float gamma =  1.2f + 0.42f * log10(peak * REFERENCE_WHITE / 1000.0f);
83     gamma = max(1.0f, gamma);
84     float factor = peak * powr(luma, gamma - 1.0f) / powr(12.0f, gamma);
85     return c * factor;
86 }
87
88 float3 inverse_ootf_hlg(float3 c, float peak) {
89     float gamma = 1.2f + 0.42f * log10(peak * REFERENCE_WHITE / 1000.0f);
90     c *=  powr(12.0f, gamma) / peak;
91     c /= powr(get_luma_dst(c), (gamma - 1.0f) / gamma);
92     return c;
93 }
94
95 float inverse_eotf_bt1886(float c) {
96     return c < 0.0f ? 0.0f : powr(c, 1.0f / 2.4f);
97 }
98
99 float oetf_bt709(float c) {
100     c = c < 0.0f ? 0.0f : c;
101     float r1 = 4.5f * c;
102     float r2 = 1.099f * powr(c, 0.45f) - 0.099f;
103     return c < 0.018f ? r1 : r2;
104 }
105 float inverse_oetf_bt709(float c) {
106     float r1 = c / 4.5f;
107     float r2 = powr((c + 0.099f) / 1.099f, 1.0f / 0.45f);
108     return c < 0.081f ? r1 : r2;
109 }
110
111 float3 yuv2rgb(float y, float u, float v) {
112 #ifdef FULL_RANGE_IN
113     u -= 0.5f; v -= 0.5f;
114 #else
115     y = (y * 255.0f -  16.0f) / 219.0f;
116     u = (u * 255.0f - 128.0f) / 224.0f;
117     v = (v * 255.0f - 128.0f) / 224.0f;
118 #endif
119     float r = y * rgb_matrix[0] + u * rgb_matrix[1] + v * rgb_matrix[2];
120     float g = y * rgb_matrix[3] + u * rgb_matrix[4] + v * rgb_matrix[5];
121     float b = y * rgb_matrix[6] + u * rgb_matrix[7] + v * rgb_matrix[8];
122     return (float3)(r, g, b);
123 }
124
125 float3 yuv2lrgb(float3 yuv) {
126     float3 rgb = yuv2rgb(yuv.x, yuv.y, yuv.z);
127 #ifdef linearize
128     float r = linearize(rgb.x);
129     float g = linearize(rgb.y);
130     float b = linearize(rgb.z);
131     return (float3)(r, g, b);
132 #else
133     return rgb;
134 #endif
135 }
136
137 float3 rgb2yuv(float r, float g, float b) {
138     float y = r*yuv_matrix[0] + g*yuv_matrix[1] + b*yuv_matrix[2];
139     float u = r*yuv_matrix[3] + g*yuv_matrix[4] + b*yuv_matrix[5];
140     float v = r*yuv_matrix[6] + g*yuv_matrix[7] + b*yuv_matrix[8];
141 #ifdef FULL_RANGE_OUT
142     u += 0.5f; v += 0.5f;
143 #else
144     y = (219.0f * y + 16.0f) / 255.0f;
145     u = (224.0f * u + 128.0f) / 255.0f;
146     v = (224.0f * v + 128.0f) / 255.0f;
147 #endif
148     return (float3)(y, u, v);
149 }
150
151 float rgb2y(float r, float g, float b) {
152     float y = r*yuv_matrix[0] + g*yuv_matrix[1] + b*yuv_matrix[2];
153     y = (219.0f * y + 16.0f) / 255.0f;
154     return y;
155 }
156
157 float3 lrgb2yuv(float3 c) {
158 #ifdef delinearize
159     float r = delinearize(c.x);
160     float g = delinearize(c.y);
161     float b = delinearize(c.z);
162     return rgb2yuv(r, g, b);
163 #else
164     return rgb2yuv(c.x, c.y, c.z);
165 #endif
166 }
167
168 float lrgb2y(float3 c) {
169 #ifdef delinearize
170     float r = delinearize(c.x);
171     float g = delinearize(c.y);
172     float b = delinearize(c.z);
173     return rgb2y(r, g, b);
174 #else
175     return rgb2y(c.x, c.y, c.z);
176 #endif
177 }
178
179 float3 lrgb2lrgb(float3 c) {
180 #ifdef RGB2RGB_PASSTHROUGH
181     return c;
182 #else
183     float r = c.x, g = c.y, b = c.z;
184     float rr = rgb2rgb[0] * r + rgb2rgb[1] * g + rgb2rgb[2] * b;
185     float gg = rgb2rgb[3] * r + rgb2rgb[4] * g + rgb2rgb[5] * b;
186     float bb = rgb2rgb[6] * r + rgb2rgb[7] * g + rgb2rgb[8] * b;
187     return (float3)(rr, gg, bb);
188 #endif
189 }
190
191 float3 ootf(float3 c, float peak) {
192 #ifdef ootf_impl
193     return ootf_impl(c, peak);
194 #else
195     return c;
196 #endif
197 }
198
199 float3 inverse_ootf(float3 c, float peak) {
200 #ifdef inverse_ootf_impl
201     return inverse_ootf_impl(c, peak);
202 #else
203     return c;
204 #endif
205 }