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>
6 * This file is part of FFmpeg.
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.
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.
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
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>
33 #include "attributes.h"
38 #define AVR(d) { (int)(d * 100000 + 0.5), 100000 }
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 :)
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) },
58 const struct AVLumaCoefficients
*av_csp_luma_coeffs_from_avcsp(enum AVColorSpace csp
)
60 const AVLumaCoefficients
*coeffs
;
62 if ((unsigned)csp
>= AVCOL_SPC_NB
)
64 coeffs
= &luma_coefficients
[csp
];
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} }
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) } } },
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) } } },
95 const AVColorPrimariesDesc
*av_csp_primaries_desc_from_id(enum AVColorPrimaries prm
)
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
)
108 static av_always_inline AVRational
abs_sub_q(AVRational r1
, AVRational r2
)
110 AVRational diff
= av_sub_q(r1
, r2
);
111 /* denominator assumed to be positive */
112 return av_make_q(abs(diff
.num
), diff
.den
);
115 enum AVColorPrimaries
av_csp_primaries_id_from_desc(const AVColorPrimariesDesc
*prm
)
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
)
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
));
133 if (av_cmp_q(delta
, av_make_q(1, 1000)) < 0)
137 return AVCOL_PRI_UNSPECIFIED
;
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,
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,
159 double av_csp_approximate_trc_gamma(enum AVColorTransferCharacteristic trc
)
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
];
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,
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,
187 double av_csp_approximate_eotf_gamma(enum AVColorTransferCharacteristic trc
)
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
];
197 #define BT709_alpha 1.099296826809442
198 #define BT709_beta 0.018053968510807
200 static double trc_bt709(double Lc
)
202 const double a
= BT709_alpha
;
203 const double b
= BT709_beta
;
205 return (0.0 > Lc
) ? 0.0
206 : ( b
> Lc
) ? 4.500 * Lc
207 : a
* pow(Lc
, 0.45) - (a
- 1.0);
210 static double trc_bt709_inv(double E
)
212 const double a
= BT709_alpha
;
213 const double b
= 4.500 * BT709_beta
;
215 return (0.0 > E
) ? 0.0
216 : ( b
> E
) ? E
/ 4.500
217 : pow((E
+ (a
- 1.0)) / a
, 1.0 / 0.45);
220 static double trc_gamma22(double Lc
)
222 return (0.0 > Lc
) ? 0.0 : pow(Lc
, 1.0/ 2.2);
225 static double trc_gamma22_inv(double E
)
227 return (0.0 > E
) ? 0.0 : pow(E
, 2.2);
230 static double trc_gamma28(double Lc
)
232 return (0.0 > Lc
) ? 0.0 : pow(Lc
, 1.0/ 2.8);
235 static double trc_gamma28_inv(double E
)
237 return (0.0 > E
) ? 0.0 : pow(E
, 2.8);
240 static double trc_smpte240M(double Lc
)
242 const double a
= 1.1115;
243 const double b
= 0.0228;
245 return (0.0 > Lc
) ? 0.0
246 : ( b
> Lc
) ? 4.000 * Lc
247 : a
* pow(Lc
, 0.45) - (a
- 1.0);
250 static double trc_smpte240M_inv(double E
)
252 const double a
= 1.1115;
253 const double b
= 4.000 * 0.0228;
255 return (0.0 > E
) ? 0.0
256 : ( b
> E
) ? E
/ 4.000
257 : pow((E
+ (a
- 1.0)) / a
, 1.0 / 0.45);
260 static double trc_linear(double Lc
)
265 static double trc_log(double Lc
)
267 return (0.01 > Lc
) ? 0.0 : 1.0 + log10(Lc
) / 2.0;
270 static double trc_log_inv(double E
)
272 return (0.0 > E
) ? 0.01 : pow(10.0, 2.0 * (E
- 1.0));
275 static double trc_log_sqrt(double Lc
)
278 return (0.00316227766 > Lc
) ? 0.0 : 1.0 + log10(Lc
) / 2.5;
281 static double trc_log_sqrt_inv(double E
)
283 return (0.0 > E
) ? 0.00316227766 : pow(10.0, 2.5 * (E
- 1.0));
286 static double trc_iec61966_2_4(double Lc
)
288 const double a
= BT709_alpha
;
289 const double b
= BT709_beta
;
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);
296 static double trc_iec61966_2_4_inv(double E
)
298 const double a
= BT709_alpha
;
299 const double b
= 4.500 * BT709_beta
;
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);
306 static double trc_bt1361(double Lc
)
308 const double a
= BT709_alpha
;
309 const double b
= BT709_beta
;
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);
316 static double trc_bt1361_inv(double E
)
318 const double a
= BT709_alpha
;
319 const double b
= 4.500 * BT709_beta
;
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);
326 static double trc_iec61966_2_1(double Lc
)
328 const double a
= 1.055;
329 const double b
= 0.0031308;
331 return (0.0 > Lc
) ? 0.0
332 : ( b
> Lc
) ? 12.92 * Lc
333 : a
* pow(Lc
, 1.0 / 2.4) - (a
- 1.0);
336 static double trc_iec61966_2_1_inv(double E
)
338 const double a
= 1.055;
339 const double b
= 12.92 * 0.0031308;
341 return (0.0 > E
) ? 0.0
342 : ( b
> E
) ? E
/ 12.92
343 : pow((E
+ (a
- 1.0)) / a
, 2.4);
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)
353 static double trc_smpte_st2084(double Lc
)
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
);
363 return (0.0 > Lc
) ? 0.0
364 : pow((c1
+ c2
* Ln
) / (1.0 + c3
* Ln
), m
);
368 static double trc_smpte_st2084_inv(double E
)
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
);
377 return (c1
> Em
) ? 0.0
378 : 10000.0 * pow((Em
- c1
) / (c2
- c3
* Em
), 1.0 / n
);
384 static double trc_smpte_st428_1(double Lc
)
386 return (0.0 > Lc
) ? 0.0 : pow(DCI_L
/ DCI_P
* Lc
, 1.0 / 2.6);
389 static double trc_smpte_st428_1_inv(double E
)
391 return (0.0 > E
) ? 0.0 : DCI_P
/ DCI_L
* pow(E
, 2.6);
394 #define HLG_a 0.17883277
395 #define HLG_b 0.28466892
396 #define HLG_c 0.55991073
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
);
409 static double trc_arib_std_b67_inv(double E
)
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);
419 #define VLOG_c2 0.181
420 #define VLOG_b 0.00873
421 #define VLOG_c 0.241514
422 #define VLOG_d 0.598206
424 static double trc_v_log(double E
)
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
);
434 static double trc_v_log_inv(double E
)
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
);
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
,
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
,
468 av_csp_trc_function
av_csp_trc_func_from_id(enum AVColorTransferCharacteristic trc
)
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
];
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
,
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
,
502 av_csp_trc_function
av_csp_trc_func_inv_from_id(enum AVColorTransferCharacteristic trc
)
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
];
512 static void eotf_linear(const double Lw
, const double Lb
, double E
[3])
514 for (int i
= 0; i
< 3; i
++)
515 E
[i
] = (Lw
- Lb
) * E
[i
] + Lb
;
518 static void eotf_linear_inv(const double Lw
, const double Lb
, double L
[3])
520 for (int i
= 0; i
< 3; i
++)
521 L
[i
] = (L
[i
] - Lb
) / (Lw
- Lb
);
524 #define WRAP_SDR_OETF(name) \
525 static void oetf_##name(double L[3]) \
527 for (int i = 0; i < 3; i++) \
528 L[i] = trc_##name(L[i]); \
531 static void oetf_##name##_inv(double E[3]) \
533 for (int i = 0; i < 3; i++) \
534 E[i] = trc_##name##_inv(E[i]); \
537 WRAP_SDR_OETF(gamma22
)
538 WRAP_SDR_OETF(gamma28
)
539 WRAP_SDR_OETF(iec61966_2_1
)
541 #define WRAP_SDR_EOTF(name) \
542 static void eotf_##name(double Lw, double Lb, double E[3]) \
544 oetf_##name##_inv(E); \
545 eotf_linear(Lw, Lb, E); \
548 static void eotf_##name##_inv(double Lw, double Lb, double L[3]) \
550 eotf_linear_inv(Lw, Lb, L); \
554 WRAP_SDR_EOTF(gamma22
)
555 WRAP_SDR_EOTF(gamma28
)
556 WRAP_SDR_EOTF(iec61966_2_1
)
558 static void eotf_bt1886(const double Lw
, const double Lb
, double E
[3])
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
);
565 for (int i
= 0; i
< 3; i
++)
566 E
[i
] = (-b
> E
[i
]) ? 0.0 : a
* pow(E
[i
] + b
, 2.4);
569 static void eotf_bt1886_inv(const double Lw
, const double Lb
, double L
[3])
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
);
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
;
580 static void eotf_smpte_st2084(const double Lw
, const double Lb
, double E
[3])
582 for (int i
= 0; i
< 3; i
++)
583 E
[i
] = trc_smpte_st2084_inv(E
[i
]);
586 static void eotf_smpte_st2084_inv(const double Lw
, const double Lb
, double L
[3])
588 for (int i
= 0; i
< 3; i
++)
589 L
[i
] = trc_smpte_st2084(L
[i
]);
592 /* This implementation assumes an SMPTE RP 431-2 reference projector (DCI) */
595 #define DCI_X (42.94 / DCI_L)
596 #define DCI_Z (45.82 / DCI_L)
598 static void eotf_smpte_st428_1(const double Lw_Y
, const double Lb_Y
, double E
[3])
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
};
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
];
609 static void eotf_smpte_st428_1_inv(const double Lw_Y
, const double Lb_Y
, double L
[3])
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
};
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);
620 static void eotf_arib_std_b67(const double Lw
, const double Lb
, double E
[3])
622 const double gamma
= fmax(1.2 + 0.42 * log10(Lw
/ 1000.0), 1.0);
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.
631 const double beta
= sqrt(3 * pow(Lb
/ Lw
, 1.0 / gamma
));
634 for (int i
= 0; i
< 3; i
++)
635 E
[i
] = trc_arib_std_b67_inv((1 - beta
) * E
[i
] + beta
);
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
++)
643 static void eotf_arib_std_b67_inv(const double Lw
, const double Lb
, double L
[3])
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];
650 luma
= pow(luma
/ Lw
, (1 - gamma
) / gamma
);
651 for (int i
= 0; i
< 3; i
++)
654 L
[0] = L
[1] = L
[2] = 0.0;
657 for (int i
= 0; i
< 3; i
++)
658 L
[i
] = (trc_arib_std_b67(L
[i
]) - beta
) / (1 - beta
);
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
,
684 av_csp_eotf_function
av_csp_itu_eotf(enum AVColorTransferCharacteristic trc
)
686 if ((unsigned)trc
>= AVCOL_TRC_NB
)
688 return eotf_funcs
[trc
];
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
,
710 av_csp_eotf_function
av_csp_itu_eotf_inv(enum AVColorTransferCharacteristic trc
)
712 if ((unsigned)trc
>= AVCOL_TRC_NB
)
714 return eotf_inv_funcs
[trc
];