FFmpeg
color_utils.c
Go to the documentation of this file.
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 static inline int fuzzy_equal(double a, double b)
31 {
32  const double epsilon = fmax(fmax(fabs(a), fabs(b)), 1.0) * 1e-7;
33  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 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  for (enum AVColorTransferCharacteristic trc = 0; trc < AVCOL_TRC_NB; trc++) {
63  const char *name = av_color_transfer_name(trc);
64  if (!func)
65  continue;
66 
67  for (int i = 0; i < FF_ARRAY_ELEMS(test_data); i++) {
68  double result = func(test_data[i]);
69  double roundtrip = func_inv(result);
70  printf("trc=%s calling func(%f) expected=%f roundtrip=%f\n",
71  name, test_data[i], result, roundtrip);
72 
73  if (result > 0.0 && fabs(roundtrip - test_data[i]) > 1e-7) {
74  printf(" FAIL\n");
75  return 1;
76  }
77  }
78  }
79 
80  for (enum AVColorTransferCharacteristic trc = 0; trc < AVCOL_TRC_NB; trc++) {
83  const char *trc_name = av_color_transfer_name(trc);
84  if (!eotf)
85  continue;
86 
87  if (trc == AVCOL_TRC_SMPTE2084) {
88  /* This one is equivalent to the TRC already tested above */
89  continue;
90  } else if (trc == AVCOL_TRC_SMPTE428) {
91  /* Test vectors from SMPTE RP-431-2 */
92  const struct { double E_xyz[3]; double luma; } tests[] = {
93  #define XYZ(X, Y, Z) { X / 4095.0, Y / 4095.0, Z / 4095.0 }
94  { XYZ( 379, 396, 389), 0.14 },
95  { XYZ( 759, 792, 778), 0.75 },
96  { XYZ(1138, 1188, 1167), 2.12 },
97  { XYZ(1518, 1584, 1556), 4.45 },
98  { XYZ(1897, 1980, 1945), 7.94 },
99  { XYZ(2276, 2376, 2334), 12.74 },
100  { XYZ(2656, 2772, 2723), 19.01 },
101  { XYZ(3035, 3168, 3112), 26.89 },
102  { XYZ(3415, 3564, 3501), 36.52 },
103  { XYZ(3794, 3960, 3890), 48.02 },
104  };
105  /* DCI reference display */
106  const double luminance = 48.00;
107  const double contrast = 2000;
108  /* Solve for Lw - Lb = luminance, Lw / Lb = contrast */
109  const double Lb = luminance / (contrast - 1);
110  const double Lw = Lb + luminance;
111 
112  for (int i = 0; i < FF_ARRAY_ELEMS(tests); i++) {
113  double L_xyz[3];
114  memcpy(L_xyz, tests[i].E_xyz, sizeof(L_xyz));
115  eotf(Lw, Lb, L_xyz);
116  printf("trc=%s EOTF(%g, %g, {%g, %g, %g}) = {%g, %g %g}, expected Y=%f\n",
117  trc_name, Lw, Lb,
118  tests[i].E_xyz[0], tests[i].E_xyz[1], tests[i].E_xyz[2],
119  L_xyz[0], L_xyz[1], L_xyz[2], tests[i].luma);
120 
121  if (fabs(L_xyz[1] - tests[i].luma) > 0.01) {
122  printf(" FAIL\n");
123  return 1;
124  }
125  }
126  } else {
127  /* Normal, display-relative RGB curve */
128  static const double black_points[] = { 0.0, 1e-6, 0.1, 1.5 };
129  static const double white_points[] = { 50.0, 100.0, 203.0, 1000.0, 10000.0 };
130 
131  for (int i = 0; i < FF_ARRAY_ELEMS(black_points); i++) {
132  for (int j = 0; j < FF_ARRAY_ELEMS(white_points); j++) {
133  const double Lb = black_points[i];
134  const double Lw = white_points[j];
135  const double all0[3] = { 0.0, 0.0, 0.0 };
136  const double all1[3] = { 1.0, 1.0, 1.0 };
137  const double black[3] = { Lb, Lb, Lb };
138  const double white[3] = { Lw, Lw, Lw };
139  double L_prev;
140 
141  TEST_EOTF(eotf, all0, black);
142  TEST_EOTF(eotf, all1, white);
143  TEST_EOTF(eotf_inv, black, all0);
144  TEST_EOTF(eotf_inv, white, all1);
145 
146  /* Test round-trip on grayscale ramp */
147  for (double x = 0.0; x < 1.0; x += 0.1) {
148  const double E[3] = { x, x, x };
149  double L[3] = { x, x, x };
150  eotf(Lw, Lb, L);
151 
152  printf("trc=%s EOTF(%g, %g, {%g}) = {%g}\n",
153  trc_name, Lw, Lb, E[1], L[1]);
154  TEST_EOTF(eotf_inv, L, E);
155 
156  if (x > 0.0 && L[1] <= L_prev) {
157  printf(" FAIL: non-monotonic!\n");
158  return 1;
159  }
160  L_prev = L[1];
161  }
162  }
163  }
164  }
165  }
166 }
func
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:68
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
AVColorTransferCharacteristic
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:611
av_csp_trc_func_inv_from_id
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:426
XYZ
#define XYZ(X, Y, Z)
AVCOL_TRC_NB
@ AVCOL_TRC_NB
Not part of ABI.
Definition: pixfmt.h:633
pixdesc.h
b
#define b
Definition: input.c:41
test_data
static const uint8_t test_data[]
Definition: encryption_info.c:33
av_csp_trc_func_from_id
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:400
macros.h
TEST_EOTF
#define TEST_EOTF(func, input, ref)
Definition: color_utils.c:36
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
main
int main(int argc, char *argv[])
Definition: color_utils.c:52
E
#define E
Definition: avdct.c:33
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
fuzzy_equal
static int fuzzy_equal(double a, double b)
Definition: color_utils.c:30
AVCOL_TRC_SMPTE2084
@ AVCOL_TRC_SMPTE2084
SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems.
Definition: pixfmt.h:628
printf
printf("static const uint8_t my_array[100] = {\n")
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
csp.h
av_csp_trc_function
double(* av_csp_trc_function)(double)
Function pointer representing a double -> double transfer function that performs either an OETF trans...
Definition: csp.h:91
tests
const TestCase tests[]
Definition: fifo_muxer.c:367
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
av_csp_eotf_function
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:162
pixfmt.h
fmax
double fmax(double, double)
L
#define L(x)
Definition: vpx_arith.h:36
av_csp_itu_eotf
av_csp_eotf_function av_csp_itu_eotf(enum AVColorTransferCharacteristic trc)
Returns the ITU EOTF corresponding to a given TRC.
Definition: csp.c:605
AVCOL_TRC_SMPTE428
@ AVCOL_TRC_SMPTE428
SMPTE ST 428-1.
Definition: pixfmt.h:630
av_color_transfer_name
const char * av_color_transfer_name(enum AVColorTransferCharacteristic transfer)
Definition: pixdesc.c:3525
av_csp_itu_eotf_inv
av_csp_eotf_function av_csp_itu_eotf_inv(enum AVColorTransferCharacteristic trc)
Returns the mathematical inverse of the corresponding EOTF.
Definition: csp.c:631