/src/ffmpeg/libavutil/csp.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2015 Kevin Wheatley <kevin.j.wheatley@gmail.com> |
3 | | * Copyright (c) 2016 Ronald S. Bultje <rsbultje@gmail.com> |
4 | | * Copyright (c) 2023 Leo Izen <leo.izen@gmail.com> |
5 | | * |
6 | | * This file is part of FFmpeg. |
7 | | * |
8 | | * FFmpeg is free software; you can redistribute it and/or |
9 | | * modify it under the terms of the GNU Lesser General Public |
10 | | * License as published by the Free Software Foundation; either |
11 | | * version 2.1 of the License, or (at your option) any later version. |
12 | | * |
13 | | * FFmpeg is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | | * Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public |
19 | | * License along with FFmpeg; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | | */ |
22 | | |
23 | | /** |
24 | | * @file Colorspace functions for libavutil |
25 | | * @author Ronald S. Bultje <rsbultje@gmail.com> |
26 | | * @author Leo Izen <leo.izen@gmail.com> |
27 | | * @author Kevin Wheatley <kevin.j.wheatley@gmail.com> |
28 | | */ |
29 | | |
30 | | #include <stdlib.h> |
31 | | #include <math.h> |
32 | | |
33 | | #include "attributes.h" |
34 | | #include "csp.h" |
35 | | #include "pixfmt.h" |
36 | | #include "rational.h" |
37 | | |
38 | | #define AVR(d) { (int)(d * 100000 + 0.5), 100000 } |
39 | | |
40 | | /* |
41 | | * All constants explained in e.g. https://linuxtv.org/downloads/v4l-dvb-apis/ch02s06.html |
42 | | * The older ones (bt470bg/m) are also explained in their respective ITU docs |
43 | | * (e.g. https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.470-5-199802-S!!PDF-E.pdf) |
44 | | * whereas the newer ones can typically be copied directly from wikipedia :) |
45 | | */ |
46 | | static const struct AVLumaCoefficients luma_coefficients[AVCOL_SPC_NB] = { |
47 | | [AVCOL_SPC_FCC] = { AVR(0.30), AVR(0.59), AVR(0.11) }, |
48 | | [AVCOL_SPC_BT470BG] = { AVR(0.299), AVR(0.587), AVR(0.114) }, |
49 | | [AVCOL_SPC_SMPTE170M] = { AVR(0.299), AVR(0.587), AVR(0.114) }, |
50 | | [AVCOL_SPC_BT709] = { AVR(0.2126), AVR(0.7152), AVR(0.0722) }, |
51 | | [AVCOL_SPC_SMPTE240M] = { AVR(0.212), AVR(0.701), AVR(0.087) }, |
52 | | [AVCOL_SPC_YCOCG] = { AVR(0.25), AVR(0.5), AVR(0.25) }, |
53 | | [AVCOL_SPC_RGB] = { AVR(1), AVR(1), AVR(1) }, |
54 | | [AVCOL_SPC_BT2020_NCL] = { AVR(0.2627), AVR(0.6780), AVR(0.0593) }, |
55 | | [AVCOL_SPC_BT2020_CL] = { AVR(0.2627), AVR(0.6780), AVR(0.0593) }, |
56 | | }; |
57 | | |
58 | | const struct AVLumaCoefficients *av_csp_luma_coeffs_from_avcsp(enum AVColorSpace csp) |
59 | 0 | { |
60 | 0 | const AVLumaCoefficients *coeffs; |
61 | |
|
62 | 0 | if ((unsigned)csp >= AVCOL_SPC_NB) |
63 | 0 | return NULL; |
64 | 0 | coeffs = &luma_coefficients[csp]; |
65 | 0 | if (!coeffs->cr.num) |
66 | 0 | return NULL; |
67 | | |
68 | 0 | return coeffs; |
69 | 0 | } |
70 | | |
71 | | #define WP_D65 { AVR(0.3127), AVR(0.3290) } |
72 | | #define WP_C { AVR(0.3100), AVR(0.3160) } |
73 | | #define WP_DCI { AVR(0.3140), AVR(0.3510) } |
74 | | #define WP_E { {1, 3}, {1, 3} } |
75 | | |
76 | | static const AVColorPrimariesDesc color_primaries[AVCOL_PRI_NB] = { |
77 | | [AVCOL_PRI_BT709] = { WP_D65, { { AVR(0.640), AVR(0.330) }, { AVR(0.300), AVR(0.600) }, { AVR(0.150), AVR(0.060) } } }, |
78 | | [AVCOL_PRI_BT470M] = { WP_C, { { AVR(0.670), AVR(0.330) }, { AVR(0.210), AVR(0.710) }, { AVR(0.140), AVR(0.080) } } }, |
79 | | [AVCOL_PRI_BT470BG] = { WP_D65, { { AVR(0.640), AVR(0.330) }, { AVR(0.290), AVR(0.600) }, { AVR(0.150), AVR(0.060) } } }, |
80 | | [AVCOL_PRI_SMPTE170M] = { WP_D65, { { AVR(0.630), AVR(0.340) }, { AVR(0.310), AVR(0.595) }, { AVR(0.155), AVR(0.070) } } }, |
81 | | [AVCOL_PRI_SMPTE240M] = { WP_D65, { { AVR(0.630), AVR(0.340) }, { AVR(0.310), AVR(0.595) }, { AVR(0.155), AVR(0.070) } } }, |
82 | | [AVCOL_PRI_SMPTE428] = { WP_E, { { AVR(0.735), AVR(0.265) }, { AVR(0.274), AVR(0.718) }, { AVR(0.167), AVR(0.009) } } }, |
83 | | [AVCOL_PRI_SMPTE431] = { WP_DCI, { { AVR(0.680), AVR(0.320) }, { AVR(0.265), AVR(0.690) }, { AVR(0.150), AVR(0.060) } } }, |
84 | | [AVCOL_PRI_SMPTE432] = { WP_D65, { { AVR(0.680), AVR(0.320) }, { AVR(0.265), AVR(0.690) }, { AVR(0.150), AVR(0.060) } } }, |
85 | | [AVCOL_PRI_FILM] = { WP_C, { { AVR(0.681), AVR(0.319) }, { AVR(0.243), AVR(0.692) }, { AVR(0.145), AVR(0.049) } } }, |
86 | | [AVCOL_PRI_BT2020] = { WP_D65, { { AVR(0.708), AVR(0.292) }, { AVR(0.170), AVR(0.797) }, { AVR(0.131), AVR(0.046) } } }, |
87 | | [AVCOL_PRI_JEDEC_P22] = { WP_D65, { { AVR(0.630), AVR(0.340) }, { AVR(0.295), AVR(0.605) }, { AVR(0.155), AVR(0.077) } } }, |
88 | | }; |
89 | | |
90 | | static const AVColorPrimariesDesc color_primaries_ext[AVCOL_PRI_EXT_NB - |
91 | | AVCOL_PRI_EXT_BASE] = { |
92 | | [AVCOL_PRI_V_GAMUT - AVCOL_PRI_EXT_BASE] = { WP_D65, { { AVR(0.730), AVR(0.280) }, { AVR(0.165), AVR(0.840) }, { AVR(0.100), AVR(-0.030) } } }, |
93 | | }; |
94 | | |
95 | | const AVColorPrimariesDesc *av_csp_primaries_desc_from_id(enum AVColorPrimaries prm) |
96 | 0 | { |
97 | 0 | const AVColorPrimariesDesc *p = NULL; |
98 | 0 | if ((unsigned)prm < AVCOL_PRI_NB) |
99 | 0 | p = &color_primaries[prm]; |
100 | 0 | else if (((unsigned)prm >= AVCOL_PRI_EXT_BASE) && |
101 | 0 | ((unsigned)prm < AVCOL_PRI_EXT_NB)) |
102 | 0 | p = &color_primaries_ext[prm - AVCOL_PRI_EXT_BASE]; |
103 | 0 | if (!p || !p->prim.r.x.num) |
104 | 0 | return NULL; |
105 | 0 | return p; |
106 | 0 | } |
107 | | |
108 | | static av_always_inline AVRational abs_sub_q(AVRational r1, AVRational r2) |
109 | 0 | { |
110 | 0 | AVRational diff = av_sub_q(r1, r2); |
111 | | /* denominator assumed to be positive */ |
112 | 0 | return av_make_q(abs(diff.num), diff.den); |
113 | 0 | } |
114 | | |
115 | | enum AVColorPrimaries av_csp_primaries_id_from_desc(const AVColorPrimariesDesc *prm) |
116 | 0 | { |
117 | 0 | AVRational delta; |
118 | |
|
119 | 0 | for (enum AVColorPrimaries p = 0; p < AVCOL_PRI_NB; p++) { |
120 | 0 | const AVColorPrimariesDesc *ref = &color_primaries[p]; |
121 | 0 | if (!ref->prim.r.x.num) |
122 | 0 | continue; |
123 | | |
124 | 0 | delta = abs_sub_q(prm->prim.r.x, ref->prim.r.x); |
125 | 0 | delta = av_add_q(delta, abs_sub_q(prm->prim.r.y, ref->prim.r.y)); |
126 | 0 | delta = av_add_q(delta, abs_sub_q(prm->prim.g.x, ref->prim.g.x)); |
127 | 0 | delta = av_add_q(delta, abs_sub_q(prm->prim.g.y, ref->prim.g.y)); |
128 | 0 | delta = av_add_q(delta, abs_sub_q(prm->prim.b.x, ref->prim.b.x)); |
129 | 0 | delta = av_add_q(delta, abs_sub_q(prm->prim.b.y, ref->prim.b.y)); |
130 | 0 | delta = av_add_q(delta, abs_sub_q(prm->wp.x, ref->wp.x)); |
131 | 0 | delta = av_add_q(delta, abs_sub_q(prm->wp.y, ref->wp.y)); |
132 | |
|
133 | 0 | if (av_cmp_q(delta, av_make_q(1, 1000)) < 0) |
134 | 0 | return p; |
135 | 0 | } |
136 | | |
137 | 0 | return AVCOL_PRI_UNSPECIFIED; |
138 | 0 | } |
139 | | |
140 | | static const double approximate_gamma[AVCOL_TRC_NB] = { |
141 | | [AVCOL_TRC_BT709] = 1.961, |
142 | | [AVCOL_TRC_SMPTE170M] = 1.961, |
143 | | [AVCOL_TRC_SMPTE240M] = 1.961, |
144 | | [AVCOL_TRC_BT1361_ECG] = 1.961, |
145 | | [AVCOL_TRC_BT2020_10] = 1.961, |
146 | | [AVCOL_TRC_BT2020_12] = 1.961, |
147 | | [AVCOL_TRC_GAMMA22] = 2.2, |
148 | | [AVCOL_TRC_IEC61966_2_1] = 2.2, |
149 | | [AVCOL_TRC_GAMMA28] = 2.8, |
150 | | [AVCOL_TRC_LINEAR] = 1.0, |
151 | | [AVCOL_TRC_SMPTE428] = 2.6, |
152 | | }; |
153 | | |
154 | | static const double approximate_gamma_ext[AVCOL_TRC_EXT_NB - |
155 | | AVCOL_TRC_EXT_BASE] = { |
156 | | [AVCOL_TRC_V_LOG - AVCOL_TRC_EXT_BASE] = 2.2, |
157 | | }; |
158 | | |
159 | | double av_csp_approximate_trc_gamma(enum AVColorTransferCharacteristic trc) |
160 | 0 | { |
161 | 0 | if (trc < AVCOL_TRC_NB) |
162 | 0 | return approximate_gamma[trc]; |
163 | 0 | else if ((trc >= AVCOL_TRC_EXT_BASE) && (trc < AVCOL_TRC_EXT_NB)) |
164 | 0 | return approximate_gamma_ext[trc - AVCOL_TRC_EXT_BASE]; |
165 | 0 | return 0.0; |
166 | 0 | } |
167 | | |
168 | | static const double approximate_eotf_gamma[AVCOL_TRC_NB] = { |
169 | | [AVCOL_TRC_BT709] = 2.2, |
170 | | [AVCOL_TRC_SMPTE170M] = 2.2, |
171 | | [AVCOL_TRC_SMPTE240M] = 2.2, |
172 | | [AVCOL_TRC_BT1361_ECG] = 2.2, |
173 | | [AVCOL_TRC_BT2020_10] = 2.2, |
174 | | [AVCOL_TRC_BT2020_12] = 2.2, |
175 | | [AVCOL_TRC_GAMMA22] = 2.2, |
176 | | [AVCOL_TRC_IEC61966_2_1] = 2.2, |
177 | | [AVCOL_TRC_GAMMA28] = 2.8, |
178 | | [AVCOL_TRC_LINEAR] = 1.0, |
179 | | [AVCOL_TRC_SMPTE428] = 2.6, |
180 | | }; |
181 | | |
182 | | static const double approximate_eotf_gamma_ext[AVCOL_TRC_EXT_NB - |
183 | | AVCOL_TRC_EXT_BASE] = { |
184 | | [AVCOL_TRC_V_LOG - AVCOL_TRC_EXT_BASE] = 2.2, |
185 | | }; |
186 | | |
187 | | double av_csp_approximate_eotf_gamma(enum AVColorTransferCharacteristic trc) |
188 | 0 | { |
189 | 0 | if ((unsigned)trc < AVCOL_TRC_NB) |
190 | 0 | return approximate_eotf_gamma[trc]; |
191 | 0 | else if (((unsigned)trc >= AVCOL_TRC_EXT_BASE) && |
192 | 0 | ((unsigned)trc < AVCOL_TRC_EXT_NB)) |
193 | 0 | return approximate_eotf_gamma_ext[trc - AVCOL_TRC_EXT_BASE]; |
194 | 0 | return 0.0; |
195 | 0 | } |
196 | | |
197 | 0 | #define BT709_alpha 1.099296826809442 |
198 | 0 | #define BT709_beta 0.018053968510807 |
199 | | |
200 | | static double trc_bt709(double Lc) |
201 | 0 | { |
202 | 0 | const double a = BT709_alpha; |
203 | 0 | const double b = BT709_beta; |
204 | |
|
205 | 0 | return (0.0 > Lc) ? 0.0 |
206 | 0 | : ( b > Lc) ? 4.500 * Lc |
207 | 0 | : a * pow(Lc, 0.45) - (a - 1.0); |
208 | 0 | } |
209 | | |
210 | | static double trc_bt709_inv(double E) |
211 | 0 | { |
212 | 0 | const double a = BT709_alpha; |
213 | 0 | const double b = 4.500 * BT709_beta; |
214 | |
|
215 | 0 | return (0.0 > E) ? 0.0 |
216 | 0 | : ( b > E) ? E / 4.500 |
217 | 0 | : pow((E + (a - 1.0)) / a, 1.0 / 0.45); |
218 | 0 | } |
219 | | |
220 | | static double trc_gamma22(double Lc) |
221 | 0 | { |
222 | 0 | return (0.0 > Lc) ? 0.0 : pow(Lc, 1.0/ 2.2); |
223 | 0 | } |
224 | | |
225 | | static double trc_gamma22_inv(double E) |
226 | 0 | { |
227 | 0 | return (0.0 > E) ? 0.0 : pow(E, 2.2); |
228 | 0 | } |
229 | | |
230 | | static double trc_gamma28(double Lc) |
231 | 0 | { |
232 | 0 | return (0.0 > Lc) ? 0.0 : pow(Lc, 1.0/ 2.8); |
233 | 0 | } |
234 | | |
235 | | static double trc_gamma28_inv(double E) |
236 | 0 | { |
237 | 0 | return (0.0 > E) ? 0.0 : pow(E, 2.8); |
238 | 0 | } |
239 | | |
240 | | static double trc_smpte240M(double Lc) |
241 | 0 | { |
242 | 0 | const double a = 1.1115; |
243 | 0 | const double b = 0.0228; |
244 | |
|
245 | 0 | return (0.0 > Lc) ? 0.0 |
246 | 0 | : ( b > Lc) ? 4.000 * Lc |
247 | 0 | : a * pow(Lc, 0.45) - (a - 1.0); |
248 | 0 | } |
249 | | |
250 | | static double trc_smpte240M_inv(double E) |
251 | 0 | { |
252 | 0 | const double a = 1.1115; |
253 | 0 | const double b = 4.000 * 0.0228; |
254 | |
|
255 | 0 | return (0.0 > E) ? 0.0 |
256 | 0 | : ( b > E) ? E / 4.000 |
257 | 0 | : pow((E + (a - 1.0)) / a, 1.0 / 0.45); |
258 | 0 | } |
259 | | |
260 | | static double trc_linear(double Lc) |
261 | 0 | { |
262 | 0 | return Lc; |
263 | 0 | } |
264 | | |
265 | | static double trc_log(double Lc) |
266 | 0 | { |
267 | 0 | return (0.01 > Lc) ? 0.0 : 1.0 + log10(Lc) / 2.0; |
268 | 0 | } |
269 | | |
270 | | static double trc_log_inv(double E) |
271 | 0 | { |
272 | 0 | return (0.0 > E) ? 0.01 : pow(10.0, 2.0 * (E - 1.0)); |
273 | 0 | } |
274 | | |
275 | | static double trc_log_sqrt(double Lc) |
276 | 0 | { |
277 | | // sqrt(10) / 1000 |
278 | 0 | return (0.00316227766 > Lc) ? 0.0 : 1.0 + log10(Lc) / 2.5; |
279 | 0 | } |
280 | | |
281 | | static double trc_log_sqrt_inv(double E) |
282 | 0 | { |
283 | 0 | return (0.0 > E) ? 0.00316227766 : pow(10.0, 2.5 * (E - 1.0)); |
284 | 0 | } |
285 | | |
286 | | static double trc_iec61966_2_4(double Lc) |
287 | 0 | { |
288 | 0 | const double a = BT709_alpha; |
289 | 0 | const double b = BT709_beta; |
290 | |
|
291 | 0 | return (-b >= Lc) ? -a * pow(-Lc, 0.45) + (a - 1.0) |
292 | 0 | : ( b > Lc) ? 4.500 * Lc |
293 | 0 | : a * pow( Lc, 0.45) - (a - 1.0); |
294 | 0 | } |
295 | | |
296 | | static double trc_iec61966_2_4_inv(double E) |
297 | 0 | { |
298 | 0 | const double a = BT709_alpha; |
299 | 0 | const double b = 4.500 * BT709_beta; |
300 | |
|
301 | 0 | return (-b >= E) ? -pow((-E + (a - 1.0)) / a, 1.0 / 0.45) |
302 | 0 | : ( b > E) ? E / 4.500 |
303 | 0 | : pow(( E + (a - 1.0)) / a, 1.0 / 0.45); |
304 | 0 | } |
305 | | |
306 | | static double trc_bt1361(double Lc) |
307 | 0 | { |
308 | 0 | const double a = BT709_alpha; |
309 | 0 | const double b = BT709_beta; |
310 | |
|
311 | 0 | return (-0.0045 >= Lc) ? -(a * pow(-4.0 * Lc, 0.45) + (a - 1.0)) / 4.0 |
312 | 0 | : ( b > Lc) ? 4.500 * Lc |
313 | 0 | : a * pow( Lc, 0.45) - (a - 1.0); |
314 | 0 | } |
315 | | |
316 | | static double trc_bt1361_inv(double E) |
317 | 0 | { |
318 | 0 | const double a = BT709_alpha; |
319 | 0 | const double b = 4.500 * BT709_beta; |
320 | |
|
321 | 0 | return (-0.02025 >= E) ? -pow((-4.0 * E - (a - 1.0)) / a, 1.0 / 0.45) / 4.0 |
322 | 0 | : ( b > E) ? E / 4.500 |
323 | 0 | : pow(( E + (a - 1.0)) / a, 1.0 / 0.45); |
324 | 0 | } |
325 | | |
326 | | static double trc_iec61966_2_1(double Lc) |
327 | 0 | { |
328 | 0 | const double a = 1.055; |
329 | 0 | const double b = 0.0031308; |
330 | |
|
331 | 0 | return (0.0 > Lc) ? 0.0 |
332 | 0 | : ( b > Lc) ? 12.92 * Lc |
333 | 0 | : a * pow(Lc, 1.0 / 2.4) - (a - 1.0); |
334 | 0 | } |
335 | | |
336 | | static double trc_iec61966_2_1_inv(double E) |
337 | 0 | { |
338 | 0 | const double a = 1.055; |
339 | 0 | const double b = 12.92 * 0.0031308; |
340 | |
|
341 | 0 | return (0.0 > E) ? 0.0 |
342 | 0 | : ( b > E) ? E / 12.92 |
343 | 0 | : pow((E + (a - 1.0)) / a, 2.4); |
344 | 0 | return E; |
345 | 0 | } |
346 | | |
347 | 0 | #define PQ_c1 ( 3424.0 / 4096.0) /* c3-c2 + 1 */ |
348 | 0 | #define PQ_c2 ( 32.0 * 2413.0 / 4096.0) |
349 | 0 | #define PQ_c3 ( 32.0 * 2392.0 / 4096.0) |
350 | 0 | #define PQ_m (128.0 * 2523.0 / 4096.0) |
351 | 0 | #define PQ_n ( 0.25 * 2610.0 / 4096.0) |
352 | | |
353 | | static double trc_smpte_st2084(double Lc) |
354 | 0 | { |
355 | 0 | const double c1 = PQ_c1; |
356 | 0 | const double c2 = PQ_c2; |
357 | 0 | const double c3 = PQ_c3; |
358 | 0 | const double m = PQ_m; |
359 | 0 | const double n = PQ_n; |
360 | 0 | const double L = Lc / 10000.0; |
361 | 0 | const double Ln = pow(L, n); |
362 | |
|
363 | 0 | return (0.0 > Lc) ? 0.0 |
364 | 0 | : pow((c1 + c2 * Ln) / (1.0 + c3 * Ln), m); |
365 | |
|
366 | 0 | } |
367 | | |
368 | | static double trc_smpte_st2084_inv(double E) |
369 | 0 | { |
370 | 0 | const double c1 = PQ_c1; |
371 | 0 | const double c2 = PQ_c2; |
372 | 0 | const double c3 = PQ_c3; |
373 | 0 | const double m = PQ_m; |
374 | 0 | const double n = PQ_n; |
375 | 0 | const double Em = pow(E, 1.0 / m); |
376 | |
|
377 | 0 | return (c1 > Em) ? 0.0 |
378 | 0 | : 10000.0 * pow((Em - c1) / (c2 - c3 * Em), 1.0 / n); |
379 | 0 | } |
380 | | |
381 | 0 | #define DCI_L 48.00 |
382 | 0 | #define DCI_P 52.37 |
383 | | |
384 | | static double trc_smpte_st428_1(double Lc) |
385 | 0 | { |
386 | 0 | return (0.0 > Lc) ? 0.0 : pow(DCI_L / DCI_P * Lc, 1.0 / 2.6); |
387 | 0 | } |
388 | | |
389 | | static double trc_smpte_st428_1_inv(double E) |
390 | 0 | { |
391 | 0 | return (0.0 > E) ? 0.0 : DCI_P / DCI_L * pow(E, 2.6); |
392 | 0 | } |
393 | | |
394 | 0 | #define HLG_a 0.17883277 |
395 | 0 | #define HLG_b 0.28466892 |
396 | 0 | #define HLG_c 0.55991073 |
397 | | |
398 | 0 | static double trc_arib_std_b67(double Lc) { |
399 | | // The function uses the definition from HEVC, which assumes that the peak |
400 | | // white is input level = 1. (this is equivalent to scaling E = Lc * 12 and |
401 | | // using the definition from the ARIB STD-B67 spec) |
402 | 0 | const double a = HLG_a; |
403 | 0 | const double b = HLG_b; |
404 | 0 | const double c = HLG_c; |
405 | 0 | return (0.0 > Lc) ? 0.0 : |
406 | 0 | (Lc <= 1.0 / 12.0 ? sqrt(3.0 * Lc) : a * log(12.0 * Lc - b) + c); |
407 | 0 | } |
408 | | |
409 | | static double trc_arib_std_b67_inv(double E) |
410 | 0 | { |
411 | 0 | const double a = HLG_a; |
412 | 0 | const double b = HLG_b; |
413 | 0 | const double c = HLG_c; |
414 | 0 | return (0.0 > E) ? 0.0 : |
415 | 0 | (E <= 0.5 ? E * E / 3.0 : (exp((E - c) / a) + b) / 12.0); |
416 | 0 | } |
417 | | |
418 | 0 | #define VLOG_c1 0.01 |
419 | 0 | #define VLOG_c2 0.181 |
420 | 0 | #define VLOG_b 0.00873 |
421 | 0 | #define VLOG_c 0.241514 |
422 | 0 | #define VLOG_d 0.598206 |
423 | | |
424 | | static double trc_v_log(double E) |
425 | 0 | { |
426 | 0 | const double c1 = VLOG_c1; |
427 | 0 | const double b = VLOG_b; |
428 | 0 | const double c = VLOG_c; |
429 | 0 | const double d = VLOG_d; |
430 | 0 | return (E < c1) ? (5.6 * E + 0.125) : |
431 | 0 | (c * log10(E + b) + d); |
432 | 0 | } |
433 | | |
434 | | static double trc_v_log_inv(double E) |
435 | 0 | { |
436 | 0 | const double c2 = VLOG_c2; |
437 | 0 | const double b = VLOG_b; |
438 | 0 | const double c = VLOG_c; |
439 | 0 | const double d = VLOG_d; |
440 | 0 | return (E < c2) ? (E - 0.125) / 5.6 : |
441 | 0 | (pow(10.0, ((E - d) / c)) - b); |
442 | 0 | } |
443 | | |
444 | | static const av_csp_trc_function trc_funcs[AVCOL_TRC_NB] = { |
445 | | [AVCOL_TRC_BT709] = trc_bt709, |
446 | | [AVCOL_TRC_GAMMA22] = trc_gamma22, |
447 | | [AVCOL_TRC_GAMMA28] = trc_gamma28, |
448 | | [AVCOL_TRC_SMPTE170M] = trc_bt709, |
449 | | [AVCOL_TRC_SMPTE240M] = trc_smpte240M, |
450 | | [AVCOL_TRC_LINEAR] = trc_linear, |
451 | | [AVCOL_TRC_LOG] = trc_log, |
452 | | [AVCOL_TRC_LOG_SQRT] = trc_log_sqrt, |
453 | | [AVCOL_TRC_IEC61966_2_4] = trc_iec61966_2_4, |
454 | | [AVCOL_TRC_BT1361_ECG] = trc_bt1361, |
455 | | [AVCOL_TRC_IEC61966_2_1] = trc_iec61966_2_1, |
456 | | [AVCOL_TRC_BT2020_10] = trc_bt709, |
457 | | [AVCOL_TRC_BT2020_12] = trc_bt709, |
458 | | [AVCOL_TRC_SMPTE2084] = trc_smpte_st2084, |
459 | | [AVCOL_TRC_SMPTE428] = trc_smpte_st428_1, |
460 | | [AVCOL_TRC_ARIB_STD_B67] = trc_arib_std_b67, |
461 | | }; |
462 | | |
463 | | static const av_csp_trc_function trc_funcs_ext[AVCOL_TRC_EXT_NB - |
464 | | AVCOL_TRC_EXT_BASE] = { |
465 | | [AVCOL_TRC_V_LOG - AVCOL_TRC_EXT_BASE] = trc_v_log, |
466 | | }; |
467 | | |
468 | | av_csp_trc_function av_csp_trc_func_from_id(enum AVColorTransferCharacteristic trc) |
469 | 0 | { |
470 | 0 | if ((unsigned)trc < AVCOL_TRC_NB) |
471 | 0 | return trc_funcs[trc]; |
472 | 0 | else if (((unsigned)trc >= AVCOL_TRC_EXT_BASE) && |
473 | 0 | ((unsigned)trc < AVCOL_TRC_EXT_NB)) |
474 | 0 | return trc_funcs_ext[trc - AVCOL_TRC_EXT_BASE]; |
475 | 0 | return NULL; |
476 | 0 | } |
477 | | |
478 | | static const av_csp_trc_function trc_inv_funcs[AVCOL_TRC_NB] = { |
479 | | [AVCOL_TRC_BT709] = trc_bt709_inv, |
480 | | [AVCOL_TRC_GAMMA22] = trc_gamma22_inv, |
481 | | [AVCOL_TRC_GAMMA28] = trc_gamma28_inv, |
482 | | [AVCOL_TRC_SMPTE170M] = trc_bt709_inv, |
483 | | [AVCOL_TRC_SMPTE240M] = trc_smpte240M_inv, |
484 | | [AVCOL_TRC_LINEAR] = trc_linear, |
485 | | [AVCOL_TRC_LOG] = trc_log_inv, |
486 | | [AVCOL_TRC_LOG_SQRT] = trc_log_sqrt_inv, |
487 | | [AVCOL_TRC_IEC61966_2_4] = trc_iec61966_2_4_inv, |
488 | | [AVCOL_TRC_BT1361_ECG] = trc_bt1361_inv, |
489 | | [AVCOL_TRC_IEC61966_2_1] = trc_iec61966_2_1_inv, |
490 | | [AVCOL_TRC_BT2020_10] = trc_bt709_inv, |
491 | | [AVCOL_TRC_BT2020_12] = trc_bt709_inv, |
492 | | [AVCOL_TRC_SMPTE2084] = trc_smpte_st2084_inv, |
493 | | [AVCOL_TRC_SMPTE428] = trc_smpte_st428_1_inv, |
494 | | [AVCOL_TRC_ARIB_STD_B67] = trc_arib_std_b67_inv, |
495 | | }; |
496 | | |
497 | | static const av_csp_trc_function trc_inv_funcs_ext[AVCOL_TRC_EXT_NB - |
498 | | AVCOL_TRC_EXT_BASE] = { |
499 | | [AVCOL_TRC_V_LOG - AVCOL_TRC_EXT_BASE] = trc_v_log_inv, |
500 | | }; |
501 | | |
502 | | av_csp_trc_function av_csp_trc_func_inv_from_id(enum AVColorTransferCharacteristic trc) |
503 | 0 | { |
504 | 0 | if ((unsigned)trc < AVCOL_TRC_NB) |
505 | 0 | return trc_inv_funcs[trc]; |
506 | 0 | else if (((unsigned)trc >= AVCOL_TRC_EXT_BASE) && |
507 | 0 | ((unsigned)trc < AVCOL_TRC_EXT_NB)) |
508 | 0 | return trc_inv_funcs_ext[trc - AVCOL_TRC_EXT_BASE]; |
509 | 0 | return NULL; |
510 | 0 | } |
511 | | |
512 | | static void eotf_linear(const double Lw, const double Lb, double E[3]) |
513 | 0 | { |
514 | 0 | for (int i = 0; i < 3; i++) |
515 | 0 | E[i] = (Lw - Lb) * E[i] + Lb; |
516 | 0 | } |
517 | | |
518 | | static void eotf_linear_inv(const double Lw, const double Lb, double L[3]) |
519 | 0 | { |
520 | 0 | for (int i = 0; i < 3; i++) |
521 | 0 | L[i] = (L[i] - Lb) / (Lw - Lb); |
522 | 0 | } |
523 | | |
524 | | #define WRAP_SDR_OETF(name) \ |
525 | 0 | static void oetf_##name(double L[3]) \ |
526 | 0 | { \ |
527 | 0 | for (int i = 0; i < 3; i++) \ |
528 | 0 | L[i] = trc_##name(L[i]); \ |
529 | 0 | } \ Unexecuted instantiation: csp.c:oetf_gamma22 Unexecuted instantiation: csp.c:oetf_gamma28 Unexecuted instantiation: csp.c:oetf_iec61966_2_1 |
530 | | \ |
531 | 0 | static void oetf_##name##_inv(double E[3]) \ |
532 | 0 | { \ |
533 | 0 | for (int i = 0; i < 3; i++) \ |
534 | 0 | E[i] = trc_##name##_inv(E[i]); \ |
535 | 0 | } Unexecuted instantiation: csp.c:oetf_gamma22_inv Unexecuted instantiation: csp.c:oetf_gamma28_inv Unexecuted instantiation: csp.c:oetf_iec61966_2_1_inv |
536 | | |
537 | | WRAP_SDR_OETF(gamma22) |
538 | | WRAP_SDR_OETF(gamma28) |
539 | | WRAP_SDR_OETF(iec61966_2_1) |
540 | | |
541 | | #define WRAP_SDR_EOTF(name) \ |
542 | 0 | static void eotf_##name(double Lw, double Lb, double E[3]) \ |
543 | 0 | { \ |
544 | 0 | oetf_##name##_inv(E); \ |
545 | 0 | eotf_linear(Lw, Lb, E); \ |
546 | 0 | } \ Unexecuted instantiation: csp.c:eotf_gamma22 Unexecuted instantiation: csp.c:eotf_gamma28 Unexecuted instantiation: csp.c:eotf_iec61966_2_1 |
547 | | \ |
548 | 0 | static void eotf_##name##_inv(double Lw, double Lb, double L[3]) \ |
549 | 0 | { \ |
550 | 0 | eotf_linear_inv(Lw, Lb, L); \ |
551 | 0 | oetf_##name(L); \ |
552 | 0 | } Unexecuted instantiation: csp.c:eotf_gamma22_inv Unexecuted instantiation: csp.c:eotf_gamma28_inv Unexecuted instantiation: csp.c:eotf_iec61966_2_1_inv |
553 | | |
554 | | WRAP_SDR_EOTF(gamma22) |
555 | | WRAP_SDR_EOTF(gamma28) |
556 | | WRAP_SDR_EOTF(iec61966_2_1) |
557 | | |
558 | | static void eotf_bt1886(const double Lw, const double Lb, double E[3]) |
559 | 0 | { |
560 | 0 | const double Lw_inv = pow(Lw, 1.0 / 2.4); |
561 | 0 | const double Lb_inv = pow(Lb, 1.0 / 2.4); |
562 | 0 | const double a = pow(Lw_inv - Lb_inv, 2.4); |
563 | 0 | const double b = Lb_inv / (Lw_inv - Lb_inv); |
564 | |
|
565 | 0 | for (int i = 0; i < 3; i++) |
566 | 0 | E[i] = (-b > E[i]) ? 0.0 : a * pow(E[i] + b, 2.4); |
567 | 0 | } |
568 | | |
569 | | static void eotf_bt1886_inv(const double Lw, const double Lb, double L[3]) |
570 | 0 | { |
571 | 0 | const double Lw_inv = pow(Lw, 1.0 / 2.4); |
572 | 0 | const double Lb_inv = pow(Lb, 1.0 / 2.4); |
573 | 0 | const double a = pow(Lw_inv - Lb_inv, 2.4); |
574 | 0 | const double b = Lb_inv / (Lw_inv - Lb_inv); |
575 | |
|
576 | 0 | for (int i = 0; i < 3; i++) |
577 | 0 | L[i] = (0.0 > L[i]) ? 0.0 : pow(L[i] / a, 1.0 / 2.4) - b; |
578 | 0 | } |
579 | | |
580 | | static void eotf_smpte_st2084(const double Lw, const double Lb, double E[3]) |
581 | 0 | { |
582 | 0 | for (int i = 0; i < 3; i++) |
583 | 0 | E[i] = trc_smpte_st2084_inv(E[i]); |
584 | 0 | } |
585 | | |
586 | | static void eotf_smpte_st2084_inv(const double Lw, const double Lb, double L[3]) |
587 | 0 | { |
588 | 0 | for (int i = 0; i < 3; i++) |
589 | 0 | L[i] = trc_smpte_st2084(L[i]); |
590 | 0 | } |
591 | | |
592 | | /* This implementation assumes an SMPTE RP 431-2 reference projector (DCI) */ |
593 | 0 | #define DCI_L 48.00 |
594 | 0 | #define DCI_P 52.37 |
595 | 0 | #define DCI_X (42.94 / DCI_L) |
596 | 0 | #define DCI_Z (45.82 / DCI_L) |
597 | | |
598 | | static void eotf_smpte_st428_1(const double Lw_Y, const double Lb_Y, double E[3]) |
599 | 0 | { |
600 | 0 | const double Lw[3] = { DCI_X * Lw_Y, Lw_Y, DCI_Z * Lw_Y }; |
601 | 0 | const double Lb[3] = { DCI_X * Lb_Y, Lb_Y, DCI_Z * Lb_Y }; |
602 | |
|
603 | 0 | for (int i = 0; i < 3; i++) { |
604 | 0 | E[i] = (0.0 > E[i]) ? 0.0 : pow(E[i], 2.6) * DCI_P / DCI_L; |
605 | 0 | E[i] = E[i] * (Lw[i] - Lb[i]) + Lb[i]; |
606 | 0 | } |
607 | 0 | } |
608 | | |
609 | | static void eotf_smpte_st428_1_inv(const double Lw_Y, const double Lb_Y, double L[3]) |
610 | 0 | { |
611 | 0 | const double Lw[3] = { DCI_X * Lw_Y, Lw_Y, DCI_Z * Lw_Y }; |
612 | 0 | const double Lb[3] = { DCI_X * Lb_Y, Lb_Y, DCI_Z * Lb_Y }; |
613 | |
|
614 | 0 | for (int i = 0; i < 3; i++) { |
615 | 0 | L[i] = (L[i] - Lb[i]) / (Lw[i] - Lb[i]); |
616 | 0 | L[i] = (0.0 > L[i]) ? 0.0 : pow(L[i] * DCI_L / DCI_P, 1.0 / 2.6); |
617 | 0 | } |
618 | 0 | } |
619 | | |
620 | | static void eotf_arib_std_b67(const double Lw, const double Lb, double E[3]) |
621 | 0 | { |
622 | 0 | const double gamma = fmax(1.2 + 0.42 * log10(Lw / 1000.0), 1.0); |
623 | | |
624 | | /** |
625 | | * Note: This equation is technically only accurate if the contrast ratio |
626 | | * Lw:Lb is greater than 12:1; otherwise we would need to use a different, |
627 | | * significantly more complicated solution. Ignore this as a highly |
628 | | * degenerate case, since any real world reference display will have a |
629 | | * static contrast ratio multiple orders of magnitude higher. |
630 | | */ |
631 | 0 | const double beta = sqrt(3 * pow(Lb / Lw, 1.0 / gamma)); |
632 | 0 | double luma; |
633 | |
|
634 | 0 | for (int i = 0; i < 3; i++) |
635 | 0 | E[i] = trc_arib_std_b67_inv((1 - beta) * E[i] + beta); |
636 | |
|
637 | 0 | luma = 0.2627 * E[0] + 0.6780 * E[1] + 0.0593 * E[2]; |
638 | 0 | luma = pow(fmax(luma, 0.0), gamma - 1.0); |
639 | 0 | for (int i = 0; i < 3; i++) |
640 | 0 | E[i] *= Lw * luma; |
641 | 0 | } |
642 | | |
643 | | static void eotf_arib_std_b67_inv(const double Lw, const double Lb, double L[3]) |
644 | 0 | { |
645 | 0 | const double gamma = fmax(1.2 + 0.42 * log10(Lw / 1000.0), 1.0); |
646 | 0 | const double beta = sqrt(3 * pow(Lb / Lw, 1 / gamma)); |
647 | 0 | double luma = 0.2627 * L[0] + 0.6780 * L[1] + 0.0593 * L[2]; |
648 | |
|
649 | 0 | if (luma > 0.0) { |
650 | 0 | luma = pow(luma / Lw, (1 - gamma) / gamma); |
651 | 0 | for (int i = 0; i < 3; i++) |
652 | 0 | L[i] *= luma / Lw; |
653 | 0 | } else { |
654 | 0 | L[0] = L[1] = L[2] = 0.0; |
655 | 0 | } |
656 | |
|
657 | 0 | for (int i = 0; i < 3; i++) |
658 | 0 | L[i] = (trc_arib_std_b67(L[i]) - beta) / (1 - beta); |
659 | 0 | } |
660 | | |
661 | | static const av_csp_eotf_function eotf_funcs[AVCOL_TRC_NB] = { |
662 | | [AVCOL_TRC_BT709] = eotf_bt1886, |
663 | | [AVCOL_TRC_GAMMA22] = eotf_gamma22, |
664 | | [AVCOL_TRC_GAMMA28] = eotf_gamma28, |
665 | | [AVCOL_TRC_SMPTE170M] = eotf_bt1886, |
666 | | [AVCOL_TRC_SMPTE240M] = eotf_bt1886, |
667 | | [AVCOL_TRC_LINEAR] = eotf_linear, |
668 | | /* There is no EOTF associated with these logarithmic encodings, since they |
669 | | * are defined purely for transmission of scene referred data. */ |
670 | | [AVCOL_TRC_LOG] = NULL, |
671 | | [AVCOL_TRC_LOG_SQRT] = NULL, |
672 | | /* BT.1886 is already defined for values below 0.0, as far as physically |
673 | | * meaningful, so we can directly use it for extended range encodings */ |
674 | | [AVCOL_TRC_IEC61966_2_4] = eotf_bt1886, |
675 | | [AVCOL_TRC_BT1361_ECG] = eotf_bt1886, |
676 | | [AVCOL_TRC_IEC61966_2_1] = eotf_iec61966_2_1, |
677 | | [AVCOL_TRC_BT2020_10] = eotf_bt1886, |
678 | | [AVCOL_TRC_BT2020_12] = eotf_bt1886, |
679 | | [AVCOL_TRC_SMPTE2084] = eotf_smpte_st2084, |
680 | | [AVCOL_TRC_SMPTE428] = eotf_smpte_st428_1, |
681 | | [AVCOL_TRC_ARIB_STD_B67] = eotf_arib_std_b67, |
682 | | }; |
683 | | |
684 | | av_csp_eotf_function av_csp_itu_eotf(enum AVColorTransferCharacteristic trc) |
685 | 0 | { |
686 | 0 | if ((unsigned)trc >= AVCOL_TRC_NB) |
687 | 0 | return NULL; |
688 | 0 | return eotf_funcs[trc]; |
689 | 0 | } |
690 | | |
691 | | static const av_csp_eotf_function eotf_inv_funcs[AVCOL_TRC_NB] = { |
692 | | [AVCOL_TRC_BT709] = eotf_bt1886_inv, |
693 | | [AVCOL_TRC_GAMMA22] = eotf_gamma22_inv, |
694 | | [AVCOL_TRC_GAMMA28] = eotf_gamma28_inv, |
695 | | [AVCOL_TRC_SMPTE170M] = eotf_bt1886_inv, |
696 | | [AVCOL_TRC_SMPTE240M] = eotf_bt1886_inv, |
697 | | [AVCOL_TRC_LINEAR] = eotf_linear_inv, |
698 | | [AVCOL_TRC_LOG] = NULL, |
699 | | [AVCOL_TRC_LOG_SQRT] = NULL, |
700 | | [AVCOL_TRC_IEC61966_2_4] = eotf_bt1886_inv, |
701 | | [AVCOL_TRC_BT1361_ECG] = eotf_bt1886_inv, |
702 | | [AVCOL_TRC_IEC61966_2_1] = eotf_iec61966_2_1_inv, |
703 | | [AVCOL_TRC_BT2020_10] = eotf_bt1886_inv, |
704 | | [AVCOL_TRC_BT2020_12] = eotf_bt1886_inv, |
705 | | [AVCOL_TRC_SMPTE2084] = eotf_smpte_st2084_inv, |
706 | | [AVCOL_TRC_SMPTE428] = eotf_smpte_st428_1_inv, |
707 | | [AVCOL_TRC_ARIB_STD_B67] = eotf_arib_std_b67_inv, |
708 | | }; |
709 | | |
710 | | av_csp_eotf_function av_csp_itu_eotf_inv(enum AVColorTransferCharacteristic trc) |
711 | 0 | { |
712 | 0 | if ((unsigned)trc >= AVCOL_TRC_NB) |
713 | 0 | return NULL; |
714 | 0 | return eotf_inv_funcs[trc]; |
715 | 0 | } |