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 __constant float yuv2rgb_bt2020[] = {
44 1.0f, -0.16455f, -0.57135f,
48 __constant float yuv2rgb_bt709[] = {
50 1.0f, -0.18732f, -0.46812f,
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
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,
67 float get_luma_dst(float3 c) {
68 return luma_dst.x * c.x + luma_dst.y * c.y + luma_dst.z * c.z;
71 float get_luma_src(float3 c) {
72 return luma_src.x * c.x + luma_src.y * c.y + luma_src.z * c.z;
75 float3 get_chroma_sample(float3 a, float3 b, float3 c, float3 d) {
76 return chroma_sample(a, b, c, d);
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;
87 __constant const float HLG_A = 0.17883277f;
88 __constant const float HLG_B = 0.28466892f;
89 __constant const float HLG_C = 0.55991073f;
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;
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;
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);
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);
120 float inverse_eotf_bt1886(float c) {
121 return c < 0.0f ? 0.0f : powr(c, 1.0f / 2.4f);
124 float oetf_bt709(float c) {
125 c = c < 0.0f ? 0.0f : c;
127 float r2 = 1.099f * powr(c, 0.45f) - 0.099f;
128 return c < 0.018f ? r1 : r2;
130 float inverse_oetf_bt709(float c) {
132 float r2 = powr((c + 0.099f) / 1.099f, 1.0f / 0.45f);
133 return c < 0.081f ? r1 : r2;
136 float3 yuv2rgb(float y, float u, float v) {
138 u -= 0.5f; v -= 0.5f;
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;
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);
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);
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;
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;
169 return (float3)(y, u, v);
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;
178 float3 lrgb2yuv(float3 c) {
179 float r = delinearize(c.x);
180 float g = delinearize(c.y);
181 float b = delinearize(c.z);
183 return rgb2yuv(r, g, b);
186 float lrgb2y(float3 c) {
187 float r = delinearize(c.x);
188 float g = delinearize(c.y);
189 float b = delinearize(c.z);
191 return rgb2y(r, g, b);
194 float3 lrgb2lrgb(float3 c) {
195 #ifdef RGB2RGB_PASSTHROUGH
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);
206 float3 ootf(float3 c, float peak) {
208 return ootf_impl(c, peak);
214 float3 inverse_ootf(float3 c, float peak) {
215 #ifdef inverse_ootf_impl
216 return inverse_ootf_impl(c, peak);