FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/csp.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 94 101 93.1%
Functions: 19 19 100.0%
Branches: 50 58 86.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 12332 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 12332 times.
12332 if (csp >= AVCOL_SPC_NB)
63 return NULL;
64 12332 coeffs = &luma_coefficients[csp];
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12332 times.
12332 if (!coeffs->cr.num)
66 return NULL;
67
68 12332 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 246 const AVColorPrimariesDesc *av_csp_primaries_desc_from_id(enum AVColorPrimaries prm)
91 {
92 const AVColorPrimariesDesc *p;
93
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
246 if (prm >= AVCOL_PRI_NB)
95 return NULL;
96 246 p = &color_primaries[prm];
97
2/2
✓ Branch 0 taken 245 times.
✓ Branch 1 taken 1 times.
246 if (!p->prim.r.x.num)
98 245 return NULL;
99
100 1 return p;
101 }
102
103 48 static av_always_inline AVRational abs_sub_q(AVRational r1, AVRational r2)
104 {
105 48 AVRational diff = av_sub_q(r1, r2);
106 /* denominator assumed to be positive */
107 48 return av_make_q(abs(diff.num), diff.den);
108 }
109
110 6 enum AVColorPrimaries av_csp_primaries_id_from_desc(const AVColorPrimariesDesc *prm)
111 {
112 AVRational delta;
113
114
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 for (enum AVColorPrimaries p = 0; p < AVCOL_PRI_NB; p++) {
115 12 const AVColorPrimariesDesc *ref = &color_primaries[p];
116
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 if (!ref->prim.r.x.num)
117 6 continue;
118
119 6 delta = abs_sub_q(prm->prim.r.x, ref->prim.r.x);
120 6 delta = av_add_q(delta, abs_sub_q(prm->prim.r.y, ref->prim.r.y));
121 6 delta = av_add_q(delta, abs_sub_q(prm->prim.g.x, ref->prim.g.x));
122 6 delta = av_add_q(delta, abs_sub_q(prm->prim.g.y, ref->prim.g.y));
123 6 delta = av_add_q(delta, abs_sub_q(prm->prim.b.x, ref->prim.b.x));
124 6 delta = av_add_q(delta, abs_sub_q(prm->prim.b.y, ref->prim.b.y));
125 6 delta = av_add_q(delta, abs_sub_q(prm->wp.x, ref->wp.x));
126 6 delta = av_add_q(delta, abs_sub_q(prm->wp.y, ref->wp.y));
127
128
1/2
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 if (av_cmp_q(delta, av_make_q(1, 1000)) < 0)
129 6 return p;
130 }
131
132 return AVCOL_PRI_UNSPECIFIED;
133 }
134
135 static const double approximate_gamma[AVCOL_TRC_NB] = {
136 [AVCOL_TRC_BT709] = 1.961,
137 [AVCOL_TRC_SMPTE170M] = 1.961,
138 [AVCOL_TRC_SMPTE240M] = 1.961,
139 [AVCOL_TRC_BT1361_ECG] = 1.961,
140 [AVCOL_TRC_BT2020_10] = 1.961,
141 [AVCOL_TRC_BT2020_12] = 1.961,
142 [AVCOL_TRC_GAMMA22] = 2.2,
143 [AVCOL_TRC_IEC61966_2_1] = 2.2,
144 [AVCOL_TRC_GAMMA28] = 2.8,
145 [AVCOL_TRC_LINEAR] = 1.0,
146 [AVCOL_TRC_SMPTE428] = 2.6,
147 };
148
149 246 double av_csp_approximate_trc_gamma(enum AVColorTransferCharacteristic trc)
150 {
151 double gamma;
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
246 if (trc >= AVCOL_TRC_NB)
153 return 0.0;
154 246 gamma = approximate_gamma[trc];
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
246 if (gamma > 0)
156 return gamma;
157 246 return 0.0;
158 }
159
160 #define BT709_alpha 1.099296826809442
161 #define BT709_beta 0.018053968510807
162
163 76 static double trc_bt709(double Lc)
164 {
165 76 const double a = BT709_alpha;
166 76 const double b = BT709_beta;
167
168 return (0.0 > Lc) ? 0.0
169
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 60 times.
136 : ( b > Lc) ? 4.500 * Lc
170
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 40 times.
60 : a * pow(Lc, 0.45) - (a - 1.0);
171 }
172
173 19 static double trc_gamma22(double Lc)
174 {
175
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
19 return (0.0 > Lc) ? 0.0 : pow(Lc, 1.0/ 2.2);
176 }
177
178 19 static double trc_gamma28(double Lc)
179 {
180
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
19 return (0.0 > Lc) ? 0.0 : pow(Lc, 1.0/ 2.8);
181 }
182
183 19 static double trc_smpte240M(double Lc)
184 {
185 19 const double a = 1.1115;
186 19 const double b = 0.0228;
187
188 return (0.0 > Lc) ? 0.0
189
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
34 : ( b > Lc) ? 4.000 * Lc
190
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 10 times.
15 : a * pow(Lc, 0.45) - (a - 1.0);
191 }
192
193 19 static double trc_linear(double Lc)
194 {
195 19 return Lc;
196 }
197
198 19 static double trc_log(double Lc)
199 {
200
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;
201 }
202
203 19 static double trc_log_sqrt(double Lc)
204 {
205 // sqrt(10) / 1000
206
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;
207 }
208
209 19 static double trc_iec61966_2_4(double Lc)
210 {
211 19 const double a = BT709_alpha;
212 19 const double b = BT709_beta;
213
214 21 return (-b >= Lc) ? -a * pow(-Lc, 0.45) + (a - 1.0)
215
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 17 times.
36 : ( b > Lc) ? 4.500 * Lc
216
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 10 times.
17 : a * pow( Lc, 0.45) - (a - 1.0);
217 }
218
219 19 static double trc_bt1361(double Lc)
220 {
221 19 const double a = BT709_alpha;
222 19 const double b = BT709_beta;
223
224 3 return (-0.0045 >= Lc) ? -(a * pow(-4.0 * Lc, 0.45) + (a - 1.0)) / 4.0
225
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16 times.
35 : ( b > Lc) ? 4.500 * Lc
226
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 10 times.
16 : a * pow( Lc, 0.45) - (a - 1.0);
227 }
228
229 19 static double trc_iec61966_2_1(double Lc)
230 {
231 19 const double a = 1.055;
232 19 const double b = 0.0031308;
233
234 return (0.0 > Lc) ? 0.0
235
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
34 : ( b > Lc) ? 12.92 * Lc
236
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
15 : a * pow(Lc, 1.0 / 2.4) - (a - 1.0);
237 }
238
239 19 static double trc_smpte_st2084(double Lc)
240 {
241 19 const double c1 = 3424.0 / 4096.0; // c3-c2 + 1
242 19 const double c2 = 32.0 * 2413.0 / 4096.0;
243 19 const double c3 = 32.0 * 2392.0 / 4096.0;
244 19 const double m = 128.0 * 2523.0 / 4096.0;
245 19 const double n = 0.25 * 2610.0 / 4096.0;
246 19 const double L = Lc / 10000.0;
247 19 const double Ln = pow(L, n);
248
249 return (0.0 > Lc) ? 0.0
250
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
19 : pow((c1 + c2 * Ln) / (1.0 + c3 * Ln), m);
251
252 }
253
254 19 static double trc_smpte_st428_1(double Lc)
255 {
256 return (0.0 > Lc) ? 0.0
257
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
19 : pow(48.0 * Lc / 52.37, 1.0 / 2.6);
258 }
259
260
261 19 static double trc_arib_std_b67(double Lc) {
262 // The function uses the definition from HEVC, which assumes that the peak
263 // white is input level = 1. (this is equivalent to scaling E = Lc * 12 and
264 // using the definition from the ARIB STD-B67 spec)
265 19 const double a = 0.17883277;
266 19 const double b = 0.28466892;
267 19 const double c = 0.55991073;
268
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
34 return (0.0 > Lc) ? 0.0 :
269
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 10 times.
15 (Lc <= 1.0 / 12.0 ? sqrt(3.0 * Lc) : a * log(12.0 * Lc - b) + c);
270 }
271
272 static const av_csp_trc_function trc_funcs[AVCOL_TRC_NB] = {
273 [AVCOL_TRC_BT709] = trc_bt709,
274 [AVCOL_TRC_GAMMA22] = trc_gamma22,
275 [AVCOL_TRC_GAMMA28] = trc_gamma28,
276 [AVCOL_TRC_SMPTE170M] = trc_bt709,
277 [AVCOL_TRC_SMPTE240M] = trc_smpte240M,
278 [AVCOL_TRC_LINEAR] = trc_linear,
279 [AVCOL_TRC_LOG] = trc_log,
280 [AVCOL_TRC_LOG_SQRT] = trc_log_sqrt,
281 [AVCOL_TRC_IEC61966_2_4] = trc_iec61966_2_4,
282 [AVCOL_TRC_BT1361_ECG] = trc_bt1361,
283 [AVCOL_TRC_IEC61966_2_1] = trc_iec61966_2_1,
284 [AVCOL_TRC_BT2020_10] = trc_bt709,
285 [AVCOL_TRC_BT2020_12] = trc_bt709,
286 [AVCOL_TRC_SMPTE2084] = trc_smpte_st2084,
287 [AVCOL_TRC_SMPTE428] = trc_smpte_st428_1,
288 [AVCOL_TRC_ARIB_STD_B67] = trc_arib_std_b67,
289 };
290
291 37458 av_csp_trc_function av_csp_trc_func_from_id(enum AVColorTransferCharacteristic trc)
292 {
293 av_csp_trc_function func;
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37458 times.
37458 if (trc >= AVCOL_TRC_NB)
295 return NULL;
296 37458 func = trc_funcs[trc];
297
2/2
✓ Branch 0 taken 37442 times.
✓ Branch 1 taken 16 times.
37458 if (!func)
298 37442 return NULL;
299 16 return func;
300 }
301