FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/csp.c
Date: 2026-04-24 19:58:39
Exec Total Coverage
Lines: 230 260 88.5%
Functions: 55 59 93.2%
Branches: 120 164 73.2%

Line Branch Exec 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 167750 const struct AVLumaCoefficients *av_csp_luma_coeffs_from_avcsp(enum AVColorSpace csp)
59 {
60 const AVLumaCoefficients *coeffs;
61
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 167750 times.
167750 if ((unsigned)csp >= AVCOL_SPC_NB)
63 return NULL;
64 167750 coeffs = &luma_coefficients[csp];
65
2/2
✓ Branch 0 taken 53284 times.
✓ Branch 1 taken 114466 times.
167750 if (!coeffs->cr.num)
66 53284 return NULL;
67
68 114466 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 643449 const AVColorPrimariesDesc *av_csp_primaries_desc_from_id(enum AVColorPrimaries prm)
96 {
97 643449 const AVColorPrimariesDesc *p = NULL;
98
1/2
✓ Branch 0 taken 643449 times.
✗ Branch 1 not taken.
643449 if ((unsigned)prm < AVCOL_PRI_NB)
99 643449 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
3/4
✓ Branch 0 taken 643449 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 530393 times.
✓ Branch 3 taken 113056 times.
643449 if (!p || !p->prim.r.x.num)
104 530393 return NULL;
105 113056 return p;
106 }
107
108 48 static av_always_inline AVRational abs_sub_q(AVRational r1, AVRational r2)
109 {
110 48 AVRational diff = av_sub_q(r1, r2);
111 /* denominator assumed to be positive */
112 48 return av_make_q(abs(diff.num), diff.den);
113 }
114
115 6 enum AVColorPrimaries av_csp_primaries_id_from_desc(const AVColorPrimariesDesc *prm)
116 {
117 AVRational delta;
118
119
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 for (enum AVColorPrimaries p = 0; p < AVCOL_PRI_NB; p++) {
120 12 const AVColorPrimariesDesc *ref = &color_primaries[p];
121
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 if (!ref->prim.r.x.num)
122 6 continue;
123
124 6 delta = abs_sub_q(prm->prim.r.x, ref->prim.r.x);
125 6 delta = av_add_q(delta, abs_sub_q(prm->prim.r.y, ref->prim.r.y));
126 6 delta = av_add_q(delta, abs_sub_q(prm->prim.g.x, ref->prim.g.x));
127 6 delta = av_add_q(delta, abs_sub_q(prm->prim.g.y, ref->prim.g.y));
128 6 delta = av_add_q(delta, abs_sub_q(prm->prim.b.x, ref->prim.b.x));
129 6 delta = av_add_q(delta, abs_sub_q(prm->prim.b.y, ref->prim.b.y));
130 6 delta = av_add_q(delta, abs_sub_q(prm->wp.x, ref->wp.x));
131 6 delta = av_add_q(delta, abs_sub_q(prm->wp.y, ref->wp.y));
132
133
1/2
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 if (av_cmp_q(delta, av_make_q(1, 1000)) < 0)
134 6 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 249 double av_csp_approximate_eotf_gamma(enum AVColorTransferCharacteristic trc)
188 {
189
1/2
✓ Branch 0 taken 249 times.
✗ Branch 1 not taken.
249 if ((unsigned)trc < AVCOL_TRC_NB)
190 249 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 76 static double trc_bt709(double Lc)
201 {
202 76 const double a = BT709_alpha;
203 76 const double b = BT709_beta;
204
205 return (0.0 > Lc) ? 0.0
206
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 60 times.
136 : ( b > Lc) ? 4.500 * Lc
207
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 40 times.
60 : a * pow(Lc, 0.45) - (a - 1.0);
208 }
209
210 76 static double trc_bt709_inv(double E)
211 {
212 76 const double a = BT709_alpha;
213 76 const double b = 4.500 * BT709_beta;
214
215 return (0.0 > E) ? 0.0
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
152 : ( b > E) ? E / 4.500
217
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 40 times.
76 : pow((E + (a - 1.0)) / a, 1.0 / 0.45);
218 }
219
220 799 static double trc_gamma22(double Lc)
221 {
222
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 795 times.
799 return (0.0 > Lc) ? 0.0 : pow(Lc, 1.0/ 2.2);
223 }
224
225 799 static double trc_gamma22_inv(double E)
226 {
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 799 times.
799 return (0.0 > E) ? 0.0 : pow(E, 2.2);
228 }
229
230 799 static double trc_gamma28(double Lc)
231 {
232
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 795 times.
799 return (0.0 > Lc) ? 0.0 : pow(Lc, 1.0/ 2.8);
233 }
234
235 799 static double trc_gamma28_inv(double E)
236 {
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 799 times.
799 return (0.0 > E) ? 0.0 : pow(E, 2.8);
238 }
239
240 19 static double trc_smpte240M(double Lc)
241 {
242 19 const double a = 1.1115;
243 19 const double b = 0.0228;
244
245 return (0.0 > Lc) ? 0.0
246
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
34 : ( b > Lc) ? 4.000 * Lc
247
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 10 times.
15 : a * pow(Lc, 0.45) - (a - 1.0);
248 }
249
250 19 static double trc_smpte240M_inv(double E)
251 {
252 19 const double a = 1.1115;
253 19 const double b = 4.000 * 0.0228;
254
255 return (0.0 > E) ? 0.0
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
38 : ( b > E) ? E / 4.000
257
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 10 times.
19 : pow((E + (a - 1.0)) / a, 1.0 / 0.45);
258 }
259
260 38 static double trc_linear(double Lc)
261 {
262 38 return Lc;
263 }
264
265 19 static double trc_log(double Lc)
266 {
267
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 11 times.
19 return (0.01 > Lc) ? 0.0 : 1.0 + log10(Lc) / 2.0;
268 }
269
270 19 static double trc_log_inv(double E)
271 {
272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 return (0.0 > E) ? 0.01 : pow(10.0, 2.0 * (E - 1.0));
273 }
274
275 19 static double trc_log_sqrt(double Lc)
276 {
277 // sqrt(10) / 1000
278
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 13 times.
19 return (0.00316227766 > Lc) ? 0.0 : 1.0 + log10(Lc) / 2.5;
279 }
280
281 19 static double trc_log_sqrt_inv(double E)
282 {
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 return (0.0 > E) ? 0.00316227766 : pow(10.0, 2.5 * (E - 1.0));
284 }
285
286 19 static double trc_iec61966_2_4(double Lc)
287 {
288 19 const double a = BT709_alpha;
289 19 const double b = BT709_beta;
290
291 21 return (-b >= Lc) ? -a * pow(-Lc, 0.45) + (a - 1.0)
292
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 17 times.
36 : ( b > Lc) ? 4.500 * Lc
293
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 10 times.
17 : a * pow( Lc, 0.45) - (a - 1.0);
294 }
295
296 19 static double trc_iec61966_2_4_inv(double E)
297 {
298 19 const double a = BT709_alpha;
299 19 const double b = 4.500 * BT709_beta;
300
301 21 return (-b >= E) ? -pow((-E + (a - 1.0)) / a, 1.0 / 0.45)
302
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 17 times.
36 : ( b > E) ? E / 4.500
303
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 10 times.
17 : pow(( E + (a - 1.0)) / a, 1.0 / 0.45);
304 }
305
306 19 static double trc_bt1361(double Lc)
307 {
308 19 const double a = BT709_alpha;
309 19 const double b = BT709_beta;
310
311 3 return (-0.0045 >= Lc) ? -(a * pow(-4.0 * Lc, 0.45) + (a - 1.0)) / 4.0
312
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16 times.
35 : ( b > Lc) ? 4.500 * Lc
313
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 10 times.
16 : a * pow( Lc, 0.45) - (a - 1.0);
314 }
315
316 19 static double trc_bt1361_inv(double E)
317 {
318 19 const double a = BT709_alpha;
319 19 const double b = 4.500 * BT709_beta;
320
321 3 return (-0.02025 >= E) ? -pow((-4.0 * E - (a - 1.0)) / a, 1.0 / 0.45) / 4.0
322
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16 times.
35 : ( b > E) ? E / 4.500
323
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 10 times.
16 : pow(( E + (a - 1.0)) / a, 1.0 / 0.45);
324 }
325
326 799 static double trc_iec61966_2_1(double Lc)
327 {
328 799 const double a = 1.055;
329 799 const double b = 0.0031308;
330
331 return (0.0 > Lc) ? 0.0
332
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 795 times.
1594 : ( b > Lc) ? 12.92 * Lc
333
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 674 times.
795 : a * pow(Lc, 1.0 / 2.4) - (a - 1.0);
334 }
335
336 799 static double trc_iec61966_2_1_inv(double E)
337 {
338 799 const double a = 1.055;
339 799 const double b = 12.92 * 0.0031308;
340
341 return (0.0 > E) ? 0.0
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 799 times.
1598 : ( b > E) ? E / 12.92
343
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 674 times.
799 : 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 19 static double trc_smpte_st2084(double Lc)
354 {
355 19 const double c1 = PQ_c1;
356 19 const double c2 = PQ_c2;
357 19 const double c3 = PQ_c3;
358 19 const double m = PQ_m;
359 19 const double n = PQ_n;
360 19 const double L = Lc / 10000.0;
361 19 const double Ln = pow(L, n);
362
363 return (0.0 > Lc) ? 0.0
364
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
19 : pow((c1 + c2 * Ln) / (1.0 + c3 * Ln), m);
365
366 }
367
368 19 static double trc_smpte_st2084_inv(double E)
369 {
370 19 const double c1 = PQ_c1;
371 19 const double c2 = PQ_c2;
372 19 const double c3 = PQ_c3;
373 19 const double m = PQ_m;
374 19 const double n = PQ_n;
375 19 const double Em = pow(E, 1.0 / m);
376
377 return (c1 > Em) ? 0.0
378
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
19 : 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 19 static double trc_smpte_st428_1(double Lc)
385 {
386
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
19 return (0.0 > Lc) ? 0.0 : pow(DCI_L / DCI_P * Lc, 1.0 / 2.6);
387 }
388
389 19 static double trc_smpte_st428_1_inv(double E)
390 {
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 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 799 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 799 const double a = HLG_a;
403 799 const double b = HLG_b;
404 799 const double c = HLG_c;
405
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 795 times.
1594 return (0.0 > Lc) ? 0.0 :
406
2/2
✓ Branch 0 taken 368 times.
✓ Branch 1 taken 427 times.
795 (Lc <= 1.0 / 12.0 ? sqrt(3.0 * Lc) : a * log(12.0 * Lc - b) + c);
407 }
408
409 799 static double trc_arib_std_b67_inv(double E)
410 {
411 799 const double a = HLG_a;
412 799 const double b = HLG_b;
413 799 const double c = HLG_c;
414
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 799 times.
1598 return (0.0 > E) ? 0.0 :
415
2/2
✓ Branch 0 taken 375 times.
✓ Branch 1 taken 424 times.
799 (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 19 static double trc_v_log(double E)
425 {
426 19 const double c1 = VLOG_c1;
427 19 const double b = VLOG_b;
428 19 const double c = VLOG_c;
429 19 const double d = VLOG_d;
430
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 11 times.
30 return (E < c1) ? (5.6 * E + 0.125) :
431 11 (c * log10(E + b) + d);
432 }
433
434 19 static double trc_v_log_inv(double E)
435 {
436 19 const double c2 = VLOG_c2;
437 19 const double b = VLOG_b;
438 19 const double c = VLOG_c;
439 19 const double d = VLOG_d;
440
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 11 times.
30 return (E < c2) ? (E - 0.125) / 5.6 :
441 11 (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 37459 av_csp_trc_function av_csp_trc_func_from_id(enum AVColorTransferCharacteristic trc)
469 {
470
2/2
✓ Branch 0 taken 37458 times.
✓ Branch 1 taken 1 times.
37459 if ((unsigned)trc < AVCOL_TRC_NB)
471 37458 return trc_funcs[trc];
472
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 else if (((unsigned)trc >= AVCOL_TRC_EXT_BASE) &&
473 ((unsigned)trc < AVCOL_TRC_EXT_NB))
474 1 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 20 av_csp_trc_function av_csp_trc_func_inv_from_id(enum AVColorTransferCharacteristic trc)
503 {
504
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1 times.
20 if ((unsigned)trc < AVCOL_TRC_NB)
505 19 return trc_inv_funcs[trc];
506
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 else if (((unsigned)trc >= AVCOL_TRC_EXT_BASE) &&
507 ((unsigned)trc < AVCOL_TRC_EXT_NB))
508 1 return trc_inv_funcs_ext[trc - AVCOL_TRC_EXT_BASE];
509 return NULL;
510 }
511
512 1040 static void eotf_linear(const double Lw, const double Lb, double E[3])
513 {
514
2/2
✓ Branch 0 taken 3120 times.
✓ Branch 1 taken 1040 times.
4160 for (int i = 0; i < 3; i++)
515 3120 E[i] = (Lw - Lb) * E[i] + Lb;
516 1040 }
517
518 1040 static void eotf_linear_inv(const double Lw, const double Lb, double L[3])
519 {
520
2/2
✓ Branch 0 taken 3120 times.
✓ Branch 1 taken 1040 times.
4160 for (int i = 0; i < 3; i++)
521 3120 L[i] = (L[i] - Lb) / (Lw - Lb);
522 1040 }
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
2/2
✓ Branch 1 taken 1560 times.
✓ Branch 2 taken 520 times.
4160 WRAP_SDR_OETF(gamma22)
538
2/2
✓ Branch 1 taken 1560 times.
✓ Branch 2 taken 520 times.
4160 WRAP_SDR_OETF(gamma28)
539
2/2
✓ Branch 1 taken 1560 times.
✓ Branch 2 taken 520 times.
4160 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 1040 WRAP_SDR_EOTF(gamma22)
555 1040 WRAP_SDR_EOTF(gamma28)
556 1040 WRAP_SDR_EOTF(iec61966_2_1)
557
558 1820 static void eotf_bt1886(const double Lw, const double Lb, double E[3])
559 {
560 1820 const double Lw_inv = pow(Lw, 1.0 / 2.4);
561 1820 const double Lb_inv = pow(Lb, 1.0 / 2.4);
562 1820 const double a = pow(Lw_inv - Lb_inv, 2.4);
563 1820 const double b = Lb_inv / (Lw_inv - Lb_inv);
564
565
2/2
✓ Branch 0 taken 5460 times.
✓ Branch 1 taken 1820 times.
7280 for (int i = 0; i < 3; i++)
566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5460 times.
5460 E[i] = (-b > E[i]) ? 0.0 : a * pow(E[i] + b, 2.4);
567 1820 }
568
569 1820 static void eotf_bt1886_inv(const double Lw, const double Lb, double L[3])
570 {
571 1820 const double Lw_inv = pow(Lw, 1.0 / 2.4);
572 1820 const double Lb_inv = pow(Lb, 1.0 / 2.4);
573 1820 const double a = pow(Lw_inv - Lb_inv, 2.4);
574 1820 const double b = Lb_inv / (Lw_inv - Lb_inv);
575
576
2/2
✓ Branch 0 taken 5460 times.
✓ Branch 1 taken 1820 times.
7280 for (int i = 0; i < 3; i++)
577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5460 times.
5460 L[i] = (0.0 > L[i]) ? 0.0 : pow(L[i] / a, 1.0 / 2.4) - b;
578 1820 }
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 10 static void eotf_smpte_st428_1(const double Lw_Y, const double Lb_Y, double E[3])
599 {
600 10 const double Lw[3] = { DCI_X * Lw_Y, Lw_Y, DCI_Z * Lw_Y };
601 10 const double Lb[3] = { DCI_X * Lb_Y, Lb_Y, DCI_Z * Lb_Y };
602
603
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
40 for (int i = 0; i < 3; i++) {
604
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 E[i] = (0.0 > E[i]) ? 0.0 : pow(E[i], 2.6) * DCI_P / DCI_L;
605 30 E[i] = E[i] * (Lw[i] - Lb[i]) + Lb[i];
606 }
607 10 }
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 260 static void eotf_arib_std_b67(const double Lw, const double Lb, double E[3])
621 {
622 260 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 260 const double beta = sqrt(3 * pow(Lb / Lw, 1.0 / gamma));
632 double luma;
633
634
2/2
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 260 times.
1040 for (int i = 0; i < 3; i++)
635 780 E[i] = trc_arib_std_b67_inv((1 - beta) * E[i] + beta);
636
637 260 luma = 0.2627 * E[0] + 0.6780 * E[1] + 0.0593 * E[2];
638 260 luma = pow(fmax(luma, 0.0), gamma - 1.0);
639
2/2
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 260 times.
1040 for (int i = 0; i < 3; i++)
640 780 E[i] *= Lw * luma;
641 260 }
642
643 260 static void eotf_arib_std_b67_inv(const double Lw, const double Lb, double L[3])
644 {
645 260 const double gamma = fmax(1.2 + 0.42 * log10(Lw / 1000.0), 1.0);
646 260 const double beta = sqrt(3 * pow(Lb / Lw, 1 / gamma));
647 260 double luma = 0.2627 * L[0] + 0.6780 * L[1] + 0.0593 * L[2];
648
649
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 10 times.
260 if (luma > 0.0) {
650 250 luma = pow(luma / Lw, (1 - gamma) / gamma);
651
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 250 times.
1000 for (int i = 0; i < 3; i++)
652 750 L[i] *= luma / Lw;
653 } else {
654 10 L[0] = L[1] = L[2] = 0.0;
655 }
656
657
2/2
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 260 times.
1040 for (int i = 0; i < 3; i++)
658 780 L[i] = (trc_arib_std_b67(L[i]) - beta) / (1 - beta);
659 260 }
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 156362 av_csp_eotf_function av_csp_itu_eotf(enum AVColorTransferCharacteristic trc)
685 {
686
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 156361 times.
156362 if ((unsigned)trc >= AVCOL_TRC_NB)
687 1 return NULL;
688 156361 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 156362 av_csp_eotf_function av_csp_itu_eotf_inv(enum AVColorTransferCharacteristic trc)
711 {
712
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 156361 times.
156362 if ((unsigned)trc >= AVCOL_TRC_NB)
713 1 return NULL;
714 156361 return eotf_inv_funcs[trc];
715 }
716