FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/tests/color_utils.c
Date: 2025-11-10 11:20:04
Exec Total Coverage
Lines: 61 67 91.0%
Functions: 2 2 100.0%
Branches: 48 56 85.7%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2015 Kevin Wheatley <kevin.j.wheatley@gmail.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <math.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "libavutil/csp.h"
26 #include "libavutil/macros.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/pixfmt.h"
29
30 10800 static inline int fuzzy_equal(double a, double b)
31 {
32 10800 const double epsilon = fmax(fmax(fabs(a), fabs(b)), 1.0) * 1e-7;
33 10800 return fabs(a - b) <= epsilon;
34 }
35
36 #define TEST_EOTF(func, input, ref) do \
37 { \
38 const double _b[3] = { (ref)[0], (ref)[1], (ref)[2] }; \
39 double _a[3] = { (input)[0], (input)[1], (input)[2] }; \
40 func(Lw, Lb, _a); \
41 for (int _i = 0; _i < 3; _i++) { \
42 if (!fuzzy_equal(_a[_i], _b[_i])) { \
43 printf("FAIL: trc=%s %s(%g, %g, %s) != %s\n" \
44 " expected {%g, %g, %g}, got {%g, %g, %g}\n", \
45 trc_name, #func, Lw, Lb, #input, #ref, \
46 _b[0], _b[1], _b[2], _a[0], _a[1], _a[2]); \
47 return 1; \
48 } \
49 } \
50 } while (0)
51
52 1 int main(int argc, char *argv[])
53 {
54 static const double test_data[] = {
55 -0.1, -0.018053968510807, -0.01, -0.00449, 0.0, 0.00316227760, 0.005,
56 0.009, 0.015, 0.1, 1.0, 52.37, 125.098765, 1999.11123, 6945.443,
57 15123.4567, 19845.88923, 98678.4231, 99999.899998
58 };
59
60
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 for (enum AVColorTransferCharacteristic trc = 0; trc < AVCOL_TRC_EXT_NB; trc++) {
61
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 19 times.
20 if (trc == AVCOL_TRC_NB)
62 1 trc = AVCOL_TRC_EXT_BASE;
63
64 20 av_csp_trc_function func = av_csp_trc_func_from_id(trc);
65 20 av_csp_trc_function func_inv = av_csp_trc_func_inv_from_id(trc);
66 20 const char *name = av_color_transfer_name(trc);
67
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 17 times.
20 if (!func)
68 3 continue;
69
70
2/2
✓ Branch 0 taken 323 times.
✓ Branch 1 taken 17 times.
340 for (int i = 0; i < FF_ARRAY_ELEMS(test_data); i++) {
71 323 double result = func(test_data[i]);
72 323 double roundtrip = func_inv(result);
73 323 printf("trc=%s calling func(%f) expected=%f roundtrip=%f\n",
74 323 name, test_data[i], result, roundtrip);
75
76
3/4
✓ Branch 0 taken 239 times.
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 239 times.
323 if (result > 0.0 && fabs(roundtrip - test_data[i]) > 1e-7) {
77 printf(" FAIL\n");
78 return 1;
79 }
80 }
81 }
82
83
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 for (enum AVColorTransferCharacteristic trc = 0; trc < AVCOL_TRC_EXT_NB; trc++) {
84
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 19 times.
20 if (trc == AVCOL_TRC_NB)
85 1 trc = AVCOL_TRC_EXT_BASE;
86
87 20 av_csp_eotf_function eotf = av_csp_itu_eotf(trc);
88 20 av_csp_eotf_function eotf_inv = av_csp_itu_eotf_inv(trc);
89 20 const char *trc_name = av_color_transfer_name(trc);
90
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 14 times.
20 if (!eotf)
91 6 continue;
92
93
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
14 if (trc == AVCOL_TRC_SMPTE2084) {
94 /* This one is equivalent to the TRC already tested above */
95 1 continue;
96
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 } else if (trc == AVCOL_TRC_SMPTE428) {
97 /* Test vectors from SMPTE RP-431-2 */
98 1 const struct { double E_xyz[3]; double luma; } tests[] = {
99 #define XYZ(X, Y, Z) { X / 4095.0, Y / 4095.0, Z / 4095.0 }
100 { XYZ( 379, 396, 389), 0.14 },
101 { XYZ( 759, 792, 778), 0.75 },
102 { XYZ(1138, 1188, 1167), 2.12 },
103 { XYZ(1518, 1584, 1556), 4.45 },
104 { XYZ(1897, 1980, 1945), 7.94 },
105 { XYZ(2276, 2376, 2334), 12.74 },
106 { XYZ(2656, 2772, 2723), 19.01 },
107 { XYZ(3035, 3168, 3112), 26.89 },
108 { XYZ(3415, 3564, 3501), 36.52 },
109 { XYZ(3794, 3960, 3890), 48.02 },
110 };
111 /* DCI reference display */
112 1 const double luminance = 48.00;
113 1 const double contrast = 2000;
114 /* Solve for Lw - Lb = luminance, Lw / Lb = contrast */
115 1 const double Lb = luminance / (contrast - 1);
116 1 const double Lw = Lb + luminance;
117
118
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 for (int i = 0; i < FF_ARRAY_ELEMS(tests); i++) {
119 double L_xyz[3];
120 10 memcpy(L_xyz, tests[i].E_xyz, sizeof(L_xyz));
121 10 eotf(Lw, Lb, L_xyz);
122 10 printf("trc=%s EOTF(%g, %g, {%g, %g, %g}) = {%g, %g %g}, expected Y=%f\n",
123 trc_name, Lw, Lb,
124 10 tests[i].E_xyz[0], tests[i].E_xyz[1], tests[i].E_xyz[2],
125 10 L_xyz[0], L_xyz[1], L_xyz[2], tests[i].luma);
126
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (fabs(L_xyz[1] - tests[i].luma) > 0.01) {
128 printf(" FAIL\n");
129 return 1;
130 }
131 }
132 } else {
133 /* Normal, display-relative RGB curve */
134 static const double black_points[] = { 0.0, 1e-6, 0.1, 1.5 };
135 static const double white_points[] = { 50.0, 100.0, 203.0, 1000.0, 10000.0 };
136
137
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 12 times.
60 for (int i = 0; i < FF_ARRAY_ELEMS(black_points); i++) {
138
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 48 times.
288 for (int j = 0; j < FF_ARRAY_ELEMS(white_points); j++) {
139 240 const double Lb = black_points[i];
140 240 const double Lw = white_points[j];
141 240 const double all0[3] = { 0.0, 0.0, 0.0 };
142 const double all1[3] = { 1.0, 1.0, 1.0 };
143 240 const double black[3] = { Lb, Lb, Lb };
144 240 const double white[3] = { Lw, Lw, Lw };
145 double L_prev;
146
147
3/4
✗ Branch 2 not taken.
✓ Branch 3 taken 720 times.
✓ Branch 5 taken 720 times.
✓ Branch 6 taken 240 times.
960 TEST_EOTF(eotf, all0, black);
148
3/4
✗ Branch 2 not taken.
✓ Branch 3 taken 720 times.
✓ Branch 5 taken 720 times.
✓ Branch 6 taken 240 times.
960 TEST_EOTF(eotf, all1, white);
149
3/4
✗ Branch 2 not taken.
✓ Branch 3 taken 720 times.
✓ Branch 5 taken 720 times.
✓ Branch 6 taken 240 times.
960 TEST_EOTF(eotf_inv, black, all0);
150
3/4
✗ Branch 2 not taken.
✓ Branch 3 taken 720 times.
✓ Branch 5 taken 720 times.
✓ Branch 6 taken 240 times.
960 TEST_EOTF(eotf_inv, white, all1);
151
152 /* Test round-trip on grayscale ramp */
153
2/2
✓ Branch 0 taken 2640 times.
✓ Branch 1 taken 240 times.
2880 for (double x = 0.0; x < 1.0; x += 0.1) {
154 2640 const double E[3] = { x, x, x };
155 2640 double L[3] = { x, x, x };
156 2640 eotf(Lw, Lb, L);
157
158 2640 printf("trc=%s EOTF(%g, %g, {%g}) = {%g}\n",
159 2640 trc_name, Lw, Lb, E[1], L[1]);
160
3/4
✗ Branch 2 not taken.
✓ Branch 3 taken 7920 times.
✓ Branch 5 taken 7920 times.
✓ Branch 6 taken 2640 times.
10560 TEST_EOTF(eotf_inv, L, E);
161
162
3/4
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 240 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2400 times.
2640 if (x > 0.0 && L[1] <= L_prev) {
163 printf(" FAIL: non-monotonic!\n");
164 return 1;
165 }
166 2640 L_prev = L[1];
167 }
168 }
169 }
170 }
171 }
172 }
173