avfilter/avfiltergraph: fix constant string comparision
[ffmpeg.git] / libavutil / csp.c
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 {
60 const AVLumaCoefficients *coeffs;
61
62 if ((unsigned)csp >= AVCOL_SPC_NB)
63 return NULL;
64 coeffs = &luma_coefficients[csp];
65 if (!coeffs->cr.num)
66 return NULL;
67
68 return coeffs;
69 }
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 {
97 const AVColorPrimariesDesc *p = NULL;
98 if ((unsigned)prm < AVCOL_PRI_NB)
99 p = &color_primaries[prm];
100 else if (((unsigned)prm >= AVCOL_PRI_EXT_BASE) &&
101 ((unsigned)prm < AVCOL_PRI_EXT_NB))
102 p = &color_primaries_ext[prm - AVCOL_PRI_EXT_BASE];
103 if (!p || !p->prim.r.x.num)
104 return NULL;
105 return p;
106 }
107
108 static av_always_inline AVRational abs_sub_q(AVRational r1, AVRational r2)
109 {
110 AVRational diff = av_sub_q(r1, r2);
111 /* denominator assumed to be positive */
112 return av_make_q(abs(diff.num), diff.den);
113 }
114
115 enum AVColorPrimaries av_csp_primaries_id_from_desc(const AVColorPrimariesDesc *prm)
116 {
117 AVRational delta;
118
119 for (enum AVColorPrimaries p = 0; p < AVCOL_PRI_NB; p++) {
120 const AVColorPrimariesDesc *ref = &color_primaries[p];
121 if (!ref->prim.r.x.num)
122 continue;
123
124 delta = abs_sub_q(prm->prim.r.x, ref->prim.r.x);
125 delta = av_add_q(delta, abs_sub_q(prm->prim.r.y, ref->prim.r.y));
126 delta = av_add_q(delta, abs_sub_q(prm->prim.g.x, ref->prim.g.x));
127 delta = av_add_q(delta, abs_sub_q(prm->prim.g.y, ref->prim.g.y));
128 delta = av_add_q(delta, abs_sub_q(prm->prim.b.x, ref->prim.b.x));
129 delta = av_add_q(delta, abs_sub_q(prm->prim.b.y, ref->prim.b.y));
130 delta = av_add_q(delta, abs_sub_q(prm->wp.x, ref->wp.x));
131 delta = av_add_q(delta, abs_sub_q(prm->wp.y, ref->wp.y));
132
133 if (av_cmp_q(delta, av_make_q(1, 1000)) < 0)
134 return p;
135 }
136
137 return AVCOL_PRI_UNSPECIFIED;
138 }
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 {
161 if (trc < AVCOL_TRC_NB)
162 return approximate_gamma[trc];
163 else if ((trc >= AVCOL_TRC_EXT_BASE) && (trc < AVCOL_TRC_EXT_NB))
164 return approximate_gamma_ext[trc - AVCOL_TRC_EXT_BASE];
165 return 0.0;
166 }
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 {
189 if ((unsigned)trc < AVCOL_TRC_NB)
190 return approximate_eotf_gamma[trc];
191 else if (((unsigned)trc >= AVCOL_TRC_EXT_BASE) &&
192 ((unsigned)trc < AVCOL_TRC_EXT_NB))
193 return approximate_eotf_gamma_ext[trc - AVCOL_TRC_EXT_BASE];
194 return 0.0;
195 }
196
197 #define BT709_alpha 1.099296826809442
198 #define BT709_beta 0.018053968510807
199
200 static double trc_bt709(double Lc)
201 {
202 const double a = BT709_alpha;
203 const double b = BT709_beta;
204
205 return (0.0 > Lc) ? 0.0
206 : ( b > Lc) ? 4.500 * Lc
207 : a * pow(Lc, 0.45) - (a - 1.0);
208 }
209
210 static double trc_bt709_inv(double E)
211 {
212 const double a = BT709_alpha;
213 const double b = 4.500 * BT709_beta;
214
215 return (0.0 > E) ? 0.0
216 : ( b > E) ? E / 4.500
217 : pow((E + (a - 1.0)) / a, 1.0 / 0.45);
218 }
219
220 static double trc_gamma22(double Lc)
221 {
222 return (0.0 > Lc) ? 0.0 : pow(Lc, 1.0/ 2.2);
223 }
224
225 static double trc_gamma22_inv(double E)
226 {
227 return (0.0 > E) ? 0.0 : pow(E, 2.2);
228 }
229
230 static double trc_gamma28(double Lc)
231 {
232 return (0.0 > Lc) ? 0.0 : pow(Lc, 1.0/ 2.8);
233 }
234
235 static double trc_gamma28_inv(double E)
236 {
237 return (0.0 > E) ? 0.0 : pow(E, 2.8);
238 }
239
240 static double trc_smpte240M(double Lc)
241 {
242 const double a = 1.1115;
243 const double b = 0.0228;
244
245 return (0.0 > Lc) ? 0.0
246 : ( b > Lc) ? 4.000 * Lc
247 : a * pow(Lc, 0.45) - (a - 1.0);
248 }
249
250 static double trc_smpte240M_inv(double E)
251 {
252 const double a = 1.1115;
253 const double b = 4.000 * 0.0228;
254
255 return (0.0 > E) ? 0.0
256 : ( b > E) ? E / 4.000
257 : pow((E + (a - 1.0)) / a, 1.0 / 0.45);
258 }
259
260 static double trc_linear(double Lc)
261 {
262 return Lc;
263 }
264
265 static double trc_log(double Lc)
266 {
267 return (0.01 > Lc) ? 0.0 : 1.0 + log10(Lc) / 2.0;
268 }
269
270 static double trc_log_inv(double E)
271 {
272 return (0.0 > E) ? 0.01 : pow(10.0, 2.0 * (E - 1.0));
273 }
274
275 static double trc_log_sqrt(double Lc)
276 {
277 // sqrt(10) / 1000
278 return (0.00316227766 > Lc) ? 0.0 : 1.0 + log10(Lc) / 2.5;
279 }
280
281 static double trc_log_sqrt_inv(double E)
282 {
283 return (0.0 > E) ? 0.00316227766 : pow(10.0, 2.5 * (E - 1.0));
284 }
285
286 static double trc_iec61966_2_4(double Lc)
287 {
288 const double a = BT709_alpha;
289 const double b = BT709_beta;
290
291 return (-b >= Lc) ? -a * pow(-Lc, 0.45) + (a - 1.0)
292 : ( b > Lc) ? 4.500 * Lc
293 : a * pow( Lc, 0.45) - (a - 1.0);
294 }
295
296 static double trc_iec61966_2_4_inv(double E)
297 {
298 const double a = BT709_alpha;
299 const double b = 4.500 * BT709_beta;
300
301 return (-b >= E) ? -pow((-E + (a - 1.0)) / a, 1.0 / 0.45)
302 : ( b > E) ? E / 4.500
303 : pow(( E + (a - 1.0)) / a, 1.0 / 0.45);
304 }
305
306 static double trc_bt1361(double Lc)
307 {
308 const double a = BT709_alpha;
309 const double b = BT709_beta;
310
311 return (-0.0045 >= Lc) ? -(a * pow(-4.0 * Lc, 0.45) + (a - 1.0)) / 4.0
312 : ( b > Lc) ? 4.500 * Lc
313 : a * pow( Lc, 0.45) - (a - 1.0);
314 }
315
316 static double trc_bt1361_inv(double E)
317 {
318 const double a = BT709_alpha;
319 const double b = 4.500 * BT709_beta;
320
321 return (-0.02025 >= E) ? -pow((-4.0 * E - (a - 1.0)) / a, 1.0 / 0.45) / 4.0
322 : ( b > E) ? E / 4.500
323 : pow(( E + (a - 1.0)) / a, 1.0 / 0.45);
324 }
325
326 static double trc_iec61966_2_1(double Lc)
327 {
328 const double a = 1.055;
329 const double b = 0.0031308;
330
331 return (0.0 > Lc) ? 0.0
332 : ( b > Lc) ? 12.92 * Lc
333 : a * pow(Lc, 1.0 / 2.4) - (a - 1.0);
334 }
335
336 static double trc_iec61966_2_1_inv(double E)
337 {
338 const double a = 1.055;
339 const double b = 12.92 * 0.0031308;
340
341 return (0.0 > E) ? 0.0
342 : ( b > E) ? E / 12.92
343 : pow((E + (a - 1.0)) / a, 2.4);
344 return E;
345 }
346
347 #define PQ_c1 ( 3424.0 / 4096.0) /* c3-c2 + 1 */
348 #define PQ_c2 ( 32.0 * 2413.0 / 4096.0)
349 #define PQ_c3 ( 32.0 * 2392.0 / 4096.0)
350 #define PQ_m (128.0 * 2523.0 / 4096.0)
351 #define PQ_n ( 0.25 * 2610.0 / 4096.0)
352
353 static double trc_smpte_st2084(double Lc)
354 {
355 const double c1 = PQ_c1;
356 const double c2 = PQ_c2;
357 const double c3 = PQ_c3;
358 const double m = PQ_m;
359 const double n = PQ_n;
360 const double L = Lc / 10000.0;
361 const double Ln = pow(L, n);
362
363 return (0.0 > Lc) ? 0.0
364 : pow((c1 + c2 * Ln) / (1.0 + c3 * Ln), m);
365
366 }
367
368 static double trc_smpte_st2084_inv(double E)
369 {
370 const double c1 = PQ_c1;
371 const double c2 = PQ_c2;
372 const double c3 = PQ_c3;
373 const double m = PQ_m;
374 const double n = PQ_n;
375 const double Em = pow(E, 1.0 / m);
376
377 return (c1 > Em) ? 0.0
378 : 10000.0 * pow((Em - c1) / (c2 - c3 * Em), 1.0 / n);
379 }
380
381 #define DCI_L 48.00
382 #define DCI_P 52.37
383
384 static double trc_smpte_st428_1(double Lc)
385 {
386 return (0.0 > Lc) ? 0.0 : pow(DCI_L / DCI_P * Lc, 1.0 / 2.6);
387 }
388
389 static double trc_smpte_st428_1_inv(double E)
390 {
391 return (0.0 > E) ? 0.0 : DCI_P / DCI_L * pow(E, 2.6);
392 }
393
394 #define HLG_a 0.17883277
395 #define HLG_b 0.28466892
396 #define HLG_c 0.55991073
397
398 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 const double a = HLG_a;
403 const double b = HLG_b;
404 const double c = HLG_c;
405 return (0.0 > Lc) ? 0.0 :
406 (Lc <= 1.0 / 12.0 ? sqrt(3.0 * Lc) : a * log(12.0 * Lc - b) + c);
407 }
408
409 static double trc_arib_std_b67_inv(double E)
410 {
411 const double a = HLG_a;
412 const double b = HLG_b;
413 const double c = HLG_c;
414 return (0.0 > E) ? 0.0 :
415 (E <= 0.5 ? E * E / 3.0 : (exp((E - c) / a) + b) / 12.0);
416 }
417
418 #define VLOG_c1 0.01
419 #define VLOG_c2 0.181
420 #define VLOG_b 0.00873
421 #define VLOG_c 0.241514
422 #define VLOG_d 0.598206
423
424 static double trc_v_log(double E)
425 {
426 const double c1 = VLOG_c1;
427 const double b = VLOG_b;
428 const double c = VLOG_c;
429 const double d = VLOG_d;
430 return (E < c1) ? (5.6 * E + 0.125) :
431 (c * log10(E + b) + d);
432 }
433
434 static double trc_v_log_inv(double E)
435 {
436 const double c2 = VLOG_c2;
437 const double b = VLOG_b;
438 const double c = VLOG_c;
439 const double d = VLOG_d;
440 return (E < c2) ? (E - 0.125) / 5.6 :
441 (pow(10.0, ((E - d) / c)) - b);
442 }
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 {
470 if ((unsigned)trc < AVCOL_TRC_NB)
471 return trc_funcs[trc];
472 else if (((unsigned)trc >= AVCOL_TRC_EXT_BASE) &&
473 ((unsigned)trc < AVCOL_TRC_EXT_NB))
474 return trc_funcs_ext[trc - AVCOL_TRC_EXT_BASE];
475 return NULL;
476 }
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 {
504 if ((unsigned)trc < AVCOL_TRC_NB)
505 return trc_inv_funcs[trc];
506 else if (((unsigned)trc >= AVCOL_TRC_EXT_BASE) &&
507 ((unsigned)trc < AVCOL_TRC_EXT_NB))
508 return trc_inv_funcs_ext[trc - AVCOL_TRC_EXT_BASE];
509 return NULL;
510 }
511
512 static void eotf_linear(const double Lw, const double Lb, double E[3])
513 {
514 for (int i = 0; i < 3; i++)
515 E[i] = (Lw - Lb) * E[i] + Lb;
516 }
517
518 static void eotf_linear_inv(const double Lw, const double Lb, double L[3])
519 {
520 for (int i = 0; i < 3; i++)
521 L[i] = (L[i] - Lb) / (Lw - Lb);
522 }
523
524 #define WRAP_SDR_OETF(name) \
525 static void oetf_##name(double L[3]) \
526 { \
527 for (int i = 0; i < 3; i++) \
528 L[i] = trc_##name(L[i]); \
529 } \
530 \
531 static void oetf_##name##_inv(double E[3]) \
532 { \
533 for (int i = 0; i < 3; i++) \
534 E[i] = trc_##name##_inv(E[i]); \
535 }
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 static void eotf_##name(double Lw, double Lb, double E[3]) \
543 { \
544 oetf_##name##_inv(E); \
545 eotf_linear(Lw, Lb, E); \
546 } \
547 \
548 static void eotf_##name##_inv(double Lw, double Lb, double L[3]) \
549 { \
550 eotf_linear_inv(Lw, Lb, L); \
551 oetf_##name(L); \
552 }
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 {
560 const double Lw_inv = pow(Lw, 1.0 / 2.4);
561 const double Lb_inv = pow(Lb, 1.0 / 2.4);
562 const double a = pow(Lw_inv - Lb_inv, 2.4);
563 const double b = Lb_inv / (Lw_inv - Lb_inv);
564
565 for (int i = 0; i < 3; i++)
566 E[i] = (-b > E[i]) ? 0.0 : a * pow(E[i] + b, 2.4);
567 }
568
569 static void eotf_bt1886_inv(const double Lw, const double Lb, double L[3])
570 {
571 const double Lw_inv = pow(Lw, 1.0 / 2.4);
572 const double Lb_inv = pow(Lb, 1.0 / 2.4);
573 const double a = pow(Lw_inv - Lb_inv, 2.4);
574 const double b = Lb_inv / (Lw_inv - Lb_inv);
575
576 for (int i = 0; i < 3; i++)
577 L[i] = (0.0 > L[i]) ? 0.0 : pow(L[i] / a, 1.0 / 2.4) - b;
578 }
579
580 static void eotf_smpte_st2084(const double Lw, const double Lb, double E[3])
581 {
582 for (int i = 0; i < 3; i++)
583 E[i] = trc_smpte_st2084_inv(E[i]);
584 }
585
586 static void eotf_smpte_st2084_inv(const double Lw, const double Lb, double L[3])
587 {
588 for (int i = 0; i < 3; i++)
589 L[i] = trc_smpte_st2084(L[i]);
590 }
591
592 /* This implementation assumes an SMPTE RP 431-2 reference projector (DCI) */
593 #define DCI_L 48.00
594 #define DCI_P 52.37
595 #define DCI_X (42.94 / DCI_L)
596 #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 {
600 const double Lw[3] = { DCI_X * Lw_Y, Lw_Y, DCI_Z * Lw_Y };
601 const double Lb[3] = { DCI_X * Lb_Y, Lb_Y, DCI_Z * Lb_Y };
602
603 for (int i = 0; i < 3; i++) {
604 E[i] = (0.0 > E[i]) ? 0.0 : pow(E[i], 2.6) * DCI_P / DCI_L;
605 E[i] = E[i] * (Lw[i] - Lb[i]) + Lb[i];
606 }
607 }
608
609 static void eotf_smpte_st428_1_inv(const double Lw_Y, const double Lb_Y, double L[3])
610 {
611 const double Lw[3] = { DCI_X * Lw_Y, Lw_Y, DCI_Z * Lw_Y };
612 const double Lb[3] = { DCI_X * Lb_Y, Lb_Y, DCI_Z * Lb_Y };
613
614 for (int i = 0; i < 3; i++) {
615 L[i] = (L[i] - Lb[i]) / (Lw[i] - Lb[i]);
616 L[i] = (0.0 > L[i]) ? 0.0 : pow(L[i] * DCI_L / DCI_P, 1.0 / 2.6);
617 }
618 }
619
620 static void eotf_arib_std_b67(const double Lw, const double Lb, double E[3])
621 {
622 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 const double beta = sqrt(3 * pow(Lb / Lw, 1.0 / gamma));
632 double luma;
633
634 for (int i = 0; i < 3; i++)
635 E[i] = trc_arib_std_b67_inv((1 - beta) * E[i] + beta);
636
637 luma = 0.2627 * E[0] + 0.6780 * E[1] + 0.0593 * E[2];
638 luma = pow(fmax(luma, 0.0), gamma - 1.0);
639 for (int i = 0; i < 3; i++)
640 E[i] *= Lw * luma;
641 }
642
643 static void eotf_arib_std_b67_inv(const double Lw, const double Lb, double L[3])
644 {
645 const double gamma = fmax(1.2 + 0.42 * log10(Lw / 1000.0), 1.0);
646 const double beta = sqrt(3 * pow(Lb / Lw, 1 / gamma));
647 double luma = 0.2627 * L[0] + 0.6780 * L[1] + 0.0593 * L[2];
648
649 if (luma > 0.0) {
650 luma = pow(luma / Lw, (1 - gamma) / gamma);
651 for (int i = 0; i < 3; i++)
652 L[i] *= luma / Lw;
653 } else {
654 L[0] = L[1] = L[2] = 0.0;
655 }
656
657 for (int i = 0; i < 3; i++)
658 L[i] = (trc_arib_std_b67(L[i]) - beta) / (1 - beta);
659 }
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 {
686 if ((unsigned)trc >= AVCOL_TRC_NB)
687 return NULL;
688 return eotf_funcs[trc];
689 }
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 {
712 if ((unsigned)trc >= AVCOL_TRC_NB)
713 return NULL;
714 return eotf_inv_funcs[trc];
715 }