FFmpeg
Loading...
Searching...
No Matches
csp.c
Go to the documentation of this file.
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 */
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
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
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
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
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))
103 if (!p || !p->prim.r.x.num)
104 return NULL;
105 return p;
106}
107
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
116{
118
119 for (enum AVColorPrimaries p = 0; p < AVCOL_PRI_NB; 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
138}
139
140static 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,
149 [AVCOL_TRC_GAMMA28] = 2.8,
150 [AVCOL_TRC_LINEAR] = 1.0,
151 [AVCOL_TRC_SMPTE428] = 2.6,
152};
153
158
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))
165 return 0.0;
166}
167
168static 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,
177 [AVCOL_TRC_GAMMA28] = 2.8,
178 [AVCOL_TRC_LINEAR] = 1.0,
179 [AVCOL_TRC_SMPTE428] = 2.6,
180};
181
186
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))
194 return 0.0;
195}
196
197#define BT709_alpha 1.099296826809442
198#define BT709_beta 0.018053968510807
199
200static 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
210static 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
220static double trc_gamma22(double Lc)
221{
222 return (0.0 > Lc) ? 0.0 : pow(Lc, 1.0/ 2.2);
223}
224
225static double trc_gamma22_inv(double E)
226{
227 return (0.0 > E) ? 0.0 : pow(E, 2.2);
228}
229
230static double trc_gamma28(double Lc)
231{
232 return (0.0 > Lc) ? 0.0 : pow(Lc, 1.0/ 2.8);
233}
234
235static double trc_gamma28_inv(double E)
236{
237 return (0.0 > E) ? 0.0 : pow(E, 2.8);
238}
239
240static 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
250static 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
260static double trc_linear(double Lc)
261{
262 return Lc;
263}
264
265static double trc_log(double Lc)
266{
267 return (0.01 > Lc) ? 0.0 : 1.0 + log10(Lc) / 2.0;
268}
269
270static double trc_log_inv(double E)
271{
272 return (0.0 > E) ? 0.01 : pow(10.0, 2.0 * (E - 1.0));
273}
274
275static 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
281static 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
286static 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
296static 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
306static 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
316static 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
326static 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
336static 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
353static 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
368static 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
384static 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
389static 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
398static 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
409static 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
424static 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
434static 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
462
467
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
496
501
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))
509 return NULL;
510}
511
512static 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
518static 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) \
525static void oetf_##name(double L[3]) \
526{ \
527 for (int i = 0; i < 3; i++) \
528 L[i] = trc_##name(L[i]); \
529} \
530 \
531static 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
537WRAP_SDR_OETF(gamma22)
538WRAP_SDR_OETF(gamma28)
539WRAP_SDR_OETF(iec61966_2_1)
540
541#define WRAP_SDR_EOTF(name) \
542static void eotf_##name(double Lw, double Lb, double E[3]) \
543{ \
544 oetf_##name##_inv(E); \
545 eotf_linear(Lw, Lb, E); \
546} \
547 \
548static 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
554WRAP_SDR_EOTF(gamma22)
555WRAP_SDR_EOTF(gamma28)
556WRAP_SDR_EOTF(iec61966_2_1)
557
558static 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
569static 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
580static void eotf_smpte_st2084(const double Lw, const double Lb, double E[3])
581{
582 for (int i = 0; i < 3; i++)
584}
585
586static 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
598static 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
609static 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
620static 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
643static 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
663 [AVCOL_TRC_GAMMA22] = eotf_gamma22,
664 [AVCOL_TRC_GAMMA28] = eotf_gamma28,
668 /* There is no EOTF associated with these logarithmic encodings, since they
669 * are defined purely for transmission of scene referred data. */
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 */
676 [AVCOL_TRC_IEC61966_2_1] = eotf_iec61966_2_1,
682};
683
685{
686 if ((unsigned)trc >= AVCOL_TRC_NB)
687 return NULL;
688 return eotf_funcs[trc];
689}
690
709
711{
712 if ((unsigned)trc >= AVCOL_TRC_NB)
713 return NULL;
714 return eotf_inv_funcs[trc];
715}
#define L(x)
Definition vpx_arith.h:36
#define E
Definition avdct.c:34
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
static double trc_smpte240M(double Lc)
Definition csp.c:240
static double trc_smpte_st2084(double Lc)
Definition csp.c:353
static double trc_iec61966_2_4(double Lc)
Definition csp.c:286
static void eotf_bt1886(const double Lw, const double Lb, double E[3])
Definition csp.c:558
static double trc_gamma22_inv(double E)
Definition csp.c:225
static void eotf_arib_std_b67_inv(const double Lw, const double Lb, double L[3])
Definition csp.c:643
static const AVColorPrimariesDesc color_primaries_ext[AVCOL_PRI_EXT_NB - AVCOL_PRI_EXT_BASE]
Definition csp.c:91
#define PQ_c3
Definition csp.c:349
#define VLOG_c
Definition csp.c:421
static double trc_log_sqrt(double Lc)
Definition csp.c:275
static double trc_iec61966_2_1(double Lc)
Definition csp.c:326
#define DCI_Z
Definition csp.c:596
static const av_csp_trc_function trc_funcs_ext[AVCOL_TRC_EXT_NB - AVCOL_TRC_EXT_BASE]
Definition csp.c:464
#define DCI_P
Definition csp.c:382
static void eotf_linear_inv(const double Lw, const double Lb, double L[3])
Definition csp.c:518
static void eotf_smpte_st2084(const double Lw, const double Lb, double E[3])
Definition csp.c:580
#define DCI_L
Definition csp.c:381
static void eotf_smpte_st428_1_inv(const double Lw_Y, const double Lb_Y, double L[3])
Definition csp.c:609
static double trc_bt1361_inv(double E)
Definition csp.c:316
static void eotf_linear(const double Lw, const double Lb, double E[3])
Definition csp.c:512
#define BT709_alpha
Definition csp.c:197
static const double approximate_gamma_ext[AVCOL_TRC_EXT_NB - AVCOL_TRC_EXT_BASE]
Definition csp.c:155
static double trc_arib_std_b67_inv(double E)
Definition csp.c:409
static void eotf_smpte_st2084_inv(const double Lw, const double Lb, double L[3])
Definition csp.c:586
static double trc_bt709_inv(double E)
Definition csp.c:210
static const double approximate_gamma[AVCOL_TRC_NB]
Definition csp.c:140
#define VLOG_c2
Definition csp.c:419
#define VLOG_c1
Definition csp.c:418
static double trc_bt1361(double Lc)
Definition csp.c:306
static const av_csp_eotf_function eotf_inv_funcs[AVCOL_TRC_NB]
Definition csp.c:691
static const av_csp_eotf_function eotf_funcs[AVCOL_TRC_NB]
Definition csp.c:661
static double trc_log(double Lc)
Definition csp.c:265
#define HLG_a
Definition csp.c:394
static double trc_linear(double Lc)
Definition csp.c:260
static void eotf_arib_std_b67(const double Lw, const double Lb, double E[3])
Definition csp.c:620
static double trc_gamma28_inv(double E)
Definition csp.c:235
static double trc_gamma28(double Lc)
Definition csp.c:230
#define HLG_b
Definition csp.c:395
static double trc_log_sqrt_inv(double E)
Definition csp.c:281
static const av_csp_trc_function trc_inv_funcs_ext[AVCOL_TRC_EXT_NB - AVCOL_TRC_EXT_BASE]
Definition csp.c:498
static void eotf_smpte_st428_1(const double Lw_Y, const double Lb_Y, double E[3])
Definition csp.c:598
static double trc_arib_std_b67(double Lc)
Definition csp.c:398
static const struct AVLumaCoefficients luma_coefficients[AVCOL_SPC_NB]
Definition csp.c:46
#define VLOG_d
Definition csp.c:422
#define WRAP_SDR_OETF(name)
Definition csp.c:524
static double trc_smpte_st2084_inv(double E)
Definition csp.c:368
static double trc_smpte240M_inv(double E)
Definition csp.c:250
static const av_csp_trc_function trc_funcs[AVCOL_TRC_NB]
Definition csp.c:444
static void eotf_bt1886_inv(const double Lw, const double Lb, double L[3])
Definition csp.c:569
static const double approximate_eotf_gamma[AVCOL_TRC_NB]
Definition csp.c:168
static double trc_iec61966_2_1_inv(double E)
Definition csp.c:336
static double trc_iec61966_2_4_inv(double E)
Definition csp.c:296
#define WP_D65
Definition csp.c:71
static const double approximate_eotf_gamma_ext[AVCOL_TRC_EXT_NB - AVCOL_TRC_EXT_BASE]
Definition csp.c:183
static av_always_inline AVRational abs_sub_q(AVRational r1, AVRational r2)
Definition csp.c:108
#define BT709_beta
Definition csp.c:198
static double trc_v_log_inv(double E)
Definition csp.c:434
#define PQ_c1
Definition csp.c:347
#define DCI_X
Definition csp.c:595
static double trc_gamma22(double Lc)
Definition csp.c:220
#define VLOG_b
Definition csp.c:420
#define PQ_n
Definition csp.c:351
#define WRAP_SDR_EOTF(name)
Definition csp.c:541
#define HLG_c
Definition csp.c:396
static double trc_log_inv(double E)
Definition csp.c:270
static double trc_smpte_st428_1(double Lc)
Definition csp.c:384
#define WP_C
Definition csp.c:72
static double trc_bt709(double Lc)
Definition csp.c:200
static const AVColorPrimariesDesc color_primaries[AVCOL_PRI_NB]
Definition csp.c:76
static double trc_smpte_st428_1_inv(double E)
Definition csp.c:389
static const av_csp_trc_function trc_inv_funcs[AVCOL_TRC_NB]
Definition csp.c:478
static double trc_v_log(double E)
Definition csp.c:424
#define PQ_c2
Definition csp.c:348
#define WP_DCI
Definition csp.c:73
#define PQ_m
Definition csp.c:350
#define AVR(d)
Definition csp.c:38
#define WP_E
Definition csp.c:74
Colorspace value utility functions for libavutil.
#define abs(x)
double fmax(double, double)
int8_t exp
Definition eval.c:76
const struct AVLumaCoefficients * av_csp_luma_coeffs_from_avcsp(enum AVColorSpace csp)
Retrieves the Luma coefficients necessary to construct a conversion matrix from an enum constant desc...
Definition csp.c:58
double(* av_csp_trc_function)(double)
Function pointer representing a double -> double transfer function that performs either an OETF trans...
Definition csp.h:91
enum AVColorPrimaries av_csp_primaries_id_from_desc(const AVColorPrimariesDesc *prm)
Detects which enum AVColorPrimaries constant corresponds to the given complete gamut description.
Definition csp.c:115
av_csp_trc_function av_csp_trc_func_from_id(enum AVColorTransferCharacteristic trc)
Determine the function needed to apply the given AVColorTransferCharacteristic to linear input.
Definition csp.c:468
const AVColorPrimariesDesc * av_csp_primaries_desc_from_id(enum AVColorPrimaries prm)
Retrieves a complete gamut description from an enum constant describing the color primaries.
Definition csp.c:95
av_csp_eotf_function av_csp_itu_eotf_inv(enum AVColorTransferCharacteristic trc)
Returns the mathematical inverse of the corresponding EOTF.
Definition csp.c:710
void(* av_csp_eotf_function)(double Lw, double Lb, double c[3])
Function pointer representing an ITU EOTF transfer for a given reference display configuration.
Definition csp.h:178
av_csp_trc_function av_csp_trc_func_inv_from_id(enum AVColorTransferCharacteristic trc)
Returns the mathematical inverse of the corresponding TRC function.
Definition csp.c:502
double av_csp_approximate_eotf_gamma(enum AVColorTransferCharacteristic trc)
Determine a suitable EOTF 'gamma' value to match the supplied AVColorTransferCharacteristic.
Definition csp.c:187
av_csp_eotf_function av_csp_itu_eotf(enum AVColorTransferCharacteristic trc)
Returns the ITU EOTF corresponding to a given TRC.
Definition csp.c:684
double av_csp_approximate_trc_gamma(enum AVColorTransferCharacteristic trc)
Determine a suitable 'gamma' value to match the supplied AVColorTransferCharacteristic.
Definition csp.c:159
AVRational av_add_q(AVRational b, AVRational c)
Add two rationals.
Definition rational.c:93
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition rational.h:71
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition rational.h:89
AVRational av_sub_q(AVRational b, AVRational c)
Subtract one rational from another.
Definition rational.c:101
int a
#define b
Definition input.c:43
Macro definitions for various function/variable attributes.
#define av_always_inline
Definition attributes.h:72
static const uint64_t c2
Definition murmur3.c:53
static const uint64_t c1
Definition murmur3.c:52
pixel format definitions
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition pixfmt.h:642
@ AVCOL_PRI_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition pixfmt.h:649
@ AVCOL_PRI_NB
Not part of ABI.
Definition pixfmt.h:660
@ AVCOL_PRI_FILM
colour filters using Illuminant C
Definition pixfmt.h:652
@ AVCOL_PRI_SMPTE432
SMPTE ST 432-1 (2010) / P3 D65 / Display P3.
Definition pixfmt.h:657
@ AVCOL_PRI_V_GAMUT
Definition pixfmt.h:664
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition pixfmt.h:644
@ AVCOL_PRI_EXT_NB
Not part of ABI.
Definition pixfmt.h:665
@ AVCOL_PRI_JEDEC_P22
Definition pixfmt.h:659
@ AVCOL_PRI_SMPTE240M
identical to above, also called "SMPTE C" even though it uses D65
Definition pixfmt.h:651
@ AVCOL_PRI_EXT_BASE
Definition pixfmt.h:663
@ AVCOL_PRI_UNSPECIFIED
Definition pixfmt.h:645
@ AVCOL_PRI_SMPTE431
SMPTE ST 431-2 (2011) / DCI P3.
Definition pixfmt.h:656
@ AVCOL_PRI_SMPTE428
SMPTE ST 428-1 (CIE 1931 XYZ)
Definition pixfmt.h:654
@ AVCOL_PRI_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition pixfmt.h:650
@ AVCOL_PRI_BT2020
ITU-R BT2020.
Definition pixfmt.h:653
@ AVCOL_PRI_BT470M
also FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition pixfmt.h:647
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition pixfmt.h:672
@ AVCOL_TRC_SMPTE170M
also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
Definition pixfmt.h:679
@ AVCOL_TRC_EXT_BASE
Definition pixfmt.h:697
@ AVCOL_TRC_SMPTE2084
SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems.
Definition pixfmt.h:689
@ AVCOL_TRC_GAMMA22
also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
Definition pixfmt.h:677
@ AVCOL_TRC_V_LOG
Definition pixfmt.h:698
@ AVCOL_TRC_BT1361_ECG
ITU-R BT1361 Extended Colour Gamut.
Definition pixfmt.h:685
@ AVCOL_TRC_SMPTE240M
Definition pixfmt.h:680
@ AVCOL_TRC_LOG
"Logarithmic transfer characteristic (100:1 range)"
Definition pixfmt.h:682
@ AVCOL_TRC_IEC61966_2_4
IEC 61966-2-4.
Definition pixfmt.h:684
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition pixfmt.h:681
@ AVCOL_TRC_GAMMA28
also ITU-R BT470BG
Definition pixfmt.h:678
@ AVCOL_TRC_ARIB_STD_B67
ARIB STD-B67, known as "Hybrid log-gamma".
Definition pixfmt.h:693
@ AVCOL_TRC_SMPTE428
SMPTE ST 428-1.
Definition pixfmt.h:691
@ AVCOL_TRC_LOG_SQRT
"Logarithmic transfer characteristic (100 * Sqrt(10) : 1 range)"
Definition pixfmt.h:683
@ AVCOL_TRC_BT2020_12
ITU-R BT2020 for 12-bit system.
Definition pixfmt.h:688
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
Definition pixfmt.h:686
@ AVCOL_TRC_EXT_NB
Not part of ABI.
Definition pixfmt.h:699
@ AVCOL_TRC_BT2020_10
ITU-R BT2020 for 10-bit system.
Definition pixfmt.h:687
@ AVCOL_TRC_BT709
also ITU-R BT1361
Definition pixfmt.h:674
@ AVCOL_TRC_NB
Not part of ABI.
Definition pixfmt.h:694
AVColorSpace
YUV colorspace type.
Definition pixfmt.h:706
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition pixfmt.h:708
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition pixfmt.h:712
@ AVCOL_SPC_YCOCG
Definition pixfmt.h:716
@ AVCOL_SPC_BT2020_CL
ITU-R BT2020 constant luminance system.
Definition pixfmt.h:718
@ AVCOL_SPC_NB
Not part of ABI.
Definition pixfmt.h:726
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition pixfmt.h:707
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition pixfmt.h:717
@ AVCOL_SPC_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
Definition pixfmt.h:713
@ AVCOL_SPC_FCC
FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition pixfmt.h:711
@ AVCOL_SPC_SMPTE240M
derived from 170M primaries and D65 white point, 170M is derived from BT470 System M's primaries
Definition pixfmt.h:714
Utilities for rational number calculation.
AVRational x
Definition csp.h:57
AVRational y
Definition csp.h:57
Struct that contains both white point location and primaries location, providing the complete descrip...
Definition csp.h:78
AVWhitepointCoefficients wp
Definition csp.h:79
AVPrimaryCoefficients prim
Definition csp.h:80
Struct containing luma coefficients to be used for RGB to YUV/YCoCg, or similar calculations.
Definition csp.h:48
AVRational cr
Definition csp.h:49
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
static int ref[MAX_W *MAX_W]
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
float delta
static double c[64]