2 * This file is part of FFmpeg.
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.
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.
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
19 #define ST2084_MAX_LUMINANCE 10000.0f
20 #define REFERENCE_WHITE 100.0f
23 #define chroma_sample(a,b,c,d) (((a) + (c)) * 0.5f)
25 #define chroma_sample(a,b,c,d) (a)
27 #define chroma_sample(a,b,c,d) (((a) + (b)) * 0.5f)
29 #define chroma_sample(a,b,c,d) (c)
31 #define chroma_sample(a,b,c,d) (((c) + (d)) * 0.5f)
33 #define chroma_sample(a,b,c,d) (((a) + (b) + (c) + (d)) * 0.25f)
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;
42 float get_luma_dst(float3 c) {
43 return luma_dst.x * c.x + luma_dst.y * c.y + luma_dst.z * c.z;
46 float get_luma_src(float3 c) {
47 return luma_src.x * c.x + luma_src.y * c.y + luma_src.z * c.z;
50 float3 get_chroma_sample(float3 a, float3 b, float3 c, float3 d) {
51 return chroma_sample(a, b, c, d);
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;
62 __constant const float HLG_A = 0.17883277f;
63 __constant const float HLG_B = 0.28466892f;
64 __constant const float HLG_C = 0.55991073f;
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;
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;
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);
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);
95 float inverse_eotf_bt1886(float c) {
96 return c < 0.0f ? 0.0f : powr(c, 1.0f / 2.4f);
99 float oetf_bt709(float c) {
100 c = c < 0.0f ? 0.0f : c;
102 float r2 = 1.099f * powr(c, 0.45f) - 0.099f;
103 return c < 0.018f ? r1 : r2;
105 float inverse_oetf_bt709(float c) {
107 float r2 = powr((c + 0.099f) / 1.099f, 1.0f / 0.45f);
108 return c < 0.081f ? r1 : r2;
111 float3 yuv2rgb(float y, float u, float v) {
113 u -= 0.5f; v -= 0.5f;
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;
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);
125 float3 yuv2lrgb(float3 yuv) {
126 float3 rgb = yuv2rgb(yuv.x, yuv.y, yuv.z);
128 float r = linearize(rgb.x);
129 float g = linearize(rgb.y);
130 float b = linearize(rgb.z);
131 return (float3)(r, g, b);
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;
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;
148 return (float3)(y, u, v);
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;
157 float3 lrgb2yuv(float3 c) {
159 float r = delinearize(c.x);
160 float g = delinearize(c.y);
161 float b = delinearize(c.z);
162 return rgb2yuv(r, g, b);
164 return rgb2yuv(c.x, c.y, c.z);
168 float lrgb2y(float3 c) {
170 float r = delinearize(c.x);
171 float g = delinearize(c.y);
172 float b = delinearize(c.z);
173 return rgb2y(r, g, b);
175 return rgb2y(c.x, c.y, c.z);
179 float3 lrgb2lrgb(float3 c) {
180 #ifdef RGB2RGB_PASSTHROUGH
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);
191 float3 ootf(float3 c, float peak) {
193 return ootf_impl(c, peak);
199 float3 inverse_ootf(float3 c, float peak) {
200 #ifdef inverse_ootf_impl
201 return inverse_ootf_impl(c, peak);