]> git.sesse.net Git - ffmpeg/blob - libavfilter/opencl/colorspace_common.cl
avfilter/vf_minterpolate: use common scene sad functions
[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 __constant float yuv2rgb_bt2020[] = {
43     1.0f, 0.0f, 1.4746f,
44     1.0f, -0.16455f, -0.57135f,
45     1.0f, 1.8814f, 0.0f
46 };
47
48 __constant float yuv2rgb_bt709[] = {
49     1.0f, 0.0f, 1.5748f,
50     1.0f, -0.18732f, -0.46812f,
51     1.0f, 1.8556f, 0.0f
52 };
53
54 __constant float rgb2yuv_bt709[] = {
55     0.2126f, 0.7152f, 0.0722f,
56     -0.11457f, -0.38543f, 0.5f,
57     0.5f, -0.45415f, -0.04585f
58 };
59
60 __constant float rgb2yuv_bt2020[] ={
61     0.2627f, 0.678f, 0.0593f,
62     -0.1396f, -0.36037f, 0.5f,
63     0.5f, -0.4598f, -0.0402f,
64 };
65
66
67 float get_luma_dst(float3 c) {
68     return luma_dst.x * c.x + luma_dst.y * c.y + luma_dst.z * c.z;
69 }
70
71 float get_luma_src(float3 c) {
72     return luma_src.x * c.x + luma_src.y * c.y + luma_src.z * c.z;
73 }
74
75 float3 get_chroma_sample(float3 a, float3 b, float3 c, float3 d) {
76     return chroma_sample(a, b, c, d);
77 }
78
79 float eotf_st2084(float x) {
80     float p = powr(x, 1.0f / ST2084_M2);
81     float a = max(p -ST2084_C1, 0.0f);
82     float b = max(ST2084_C2 - ST2084_C3 * p, 1e-6f);
83     float c  = powr(a / b, 1.0f / ST2084_M1);
84     return x > 0.0f ? c * ST2084_MAX_LUMINANCE / REFERENCE_WHITE : 0.0f;
85 }
86
87 __constant const float HLG_A = 0.17883277f;
88 __constant const float HLG_B = 0.28466892f;
89 __constant const float HLG_C = 0.55991073f;
90
91 // linearizer for HLG
92 float inverse_oetf_hlg(float x) {
93     float a = 4.0f * x * x;
94     float b = exp((x - HLG_C) / HLG_A) + HLG_B;
95     return x < 0.5f ? a : b;
96 }
97
98 // delinearizer for HLG
99 float oetf_hlg(float x) {
100     float a = 0.5f * sqrt(x);
101     float b = HLG_A * log(x - HLG_B) + HLG_C;
102     return x <= 1.0f ? a : b;
103 }
104
105 float3 ootf_hlg(float3 c, float peak) {
106     float luma = get_luma_src(c);
107     float gamma =  1.2f + 0.42f * log10(peak * REFERENCE_WHITE / 1000.0f);
108     gamma = max(1.0f, gamma);
109     float factor = peak * powr(luma, gamma - 1.0f) / powr(12.0f, gamma);
110     return c * factor;
111 }
112
113 float3 inverse_ootf_hlg(float3 c, float peak) {
114     float gamma = 1.2f + 0.42f * log10(peak * REFERENCE_WHITE / 1000.0f);
115     c *=  powr(12.0f, gamma) / peak;
116     c /= powr(get_luma_dst(c), (gamma - 1.0f) / gamma);
117     return c;
118 }
119
120 float inverse_eotf_bt1886(float c) {
121     return c < 0.0f ? 0.0f : powr(c, 1.0f / 2.4f);
122 }
123
124 float oetf_bt709(float c) {
125     c = c < 0.0f ? 0.0f : c;
126     float r1 = 4.5f * c;
127     float r2 = 1.099f * powr(c, 0.45f) - 0.099f;
128     return c < 0.018f ? r1 : r2;
129 }
130 float inverse_oetf_bt709(float c) {
131     float r1 = c / 4.5f;
132     float r2 = powr((c + 0.099f) / 1.099f, 1.0f / 0.45f);
133     return c < 0.081f ? r1 : r2;
134 }
135
136 float3 yuv2rgb(float y, float u, float v) {
137 #ifdef FULL_RANGE_IN
138     u -= 0.5f; v -= 0.5f;
139 #else
140     y = (y * 255.0f -  16.0f) / 219.0f;
141     u = (u * 255.0f - 128.0f) / 224.0f;
142     v = (v * 255.0f - 128.0f) / 224.0f;
143 #endif
144     float r = y * rgb_matrix[0] + u * rgb_matrix[1] + v * rgb_matrix[2];
145     float g = y * rgb_matrix[3] + u * rgb_matrix[4] + v * rgb_matrix[5];
146     float b = y * rgb_matrix[6] + u * rgb_matrix[7] + v * rgb_matrix[8];
147     return (float3)(r, g, b);
148 }
149
150 float3 yuv2lrgb(float3 yuv) {
151     float3 rgb = yuv2rgb(yuv.x, yuv.y, yuv.z);
152     float r = linearize(rgb.x);
153     float g = linearize(rgb.y);
154     float b = linearize(rgb.z);
155     return (float3)(r, g, b);
156 }
157
158 float3 rgb2yuv(float r, float g, float b) {
159     float y = r*yuv_matrix[0] + g*yuv_matrix[1] + b*yuv_matrix[2];
160     float u = r*yuv_matrix[3] + g*yuv_matrix[4] + b*yuv_matrix[5];
161     float v = r*yuv_matrix[6] + g*yuv_matrix[7] + b*yuv_matrix[8];
162 #ifdef FULL_RANGE_OUT
163     u += 0.5f; v += 0.5f;
164 #else
165     y = (219.0f * y + 16.0f) / 255.0f;
166     u = (224.0f * u + 128.0f) / 255.0f;
167     v = (224.0f * v + 128.0f) / 255.0f;
168 #endif
169     return (float3)(y, u, v);
170 }
171
172 float rgb2y(float r, float g, float b) {
173     float y = r*yuv_matrix[0] + g*yuv_matrix[1] + b*yuv_matrix[2];
174     y = (219.0f * y + 16.0f) / 255.0f;
175     return y;
176 }
177
178 float3 lrgb2yuv(float3 c) {
179     float r = delinearize(c.x);
180     float g = delinearize(c.y);
181     float b = delinearize(c.z);
182
183     return rgb2yuv(r, g, b);
184 }
185
186 float lrgb2y(float3 c) {
187     float r = delinearize(c.x);
188     float g = delinearize(c.y);
189     float b = delinearize(c.z);
190
191     return rgb2y(r, g, b);
192 }
193
194 float3 lrgb2lrgb(float3 c) {
195 #ifdef RGB2RGB_PASSTHROUGH
196     return c;
197 #else
198     float r = c.x, g = c.y, b = c.z;
199     float rr = rgb2rgb[0] * r + rgb2rgb[1] * g + rgb2rgb[2] * b;
200     float gg = rgb2rgb[3] * r + rgb2rgb[4] * g + rgb2rgb[5] * b;
201     float bb = rgb2rgb[6] * r + rgb2rgb[7] * g + rgb2rgb[8] * b;
202     return (float3)(rr, gg, bb);
203 #endif
204 }
205
206 float3 ootf(float3 c, float peak) {
207 #ifdef ootf_impl
208     return ootf_impl(c, peak);
209 #else
210     return c;
211 #endif
212 }
213
214 float3 inverse_ootf(float3 c, float peak) {
215 #ifdef inverse_ootf_impl
216     return inverse_ootf_impl(c, peak);
217 #else
218     return c;
219 #endif
220 }