FFmpeg
csp.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <stdio.h>
20 
21 #include "libavutil/csp.h"
22 #include "libavutil/pixdesc.h"
23 #include "libavutil/pixfmt.h"
24 
25 static void print_cie(const char *label, AVCIExy xy)
26 {
27  printf(" %s=(%d/%d, %d/%d)\n", label,
28  xy.x.num, xy.x.den, xy.y.num, xy.y.den);
29 }
30 
31 static int cie_eq(AVCIExy a, AVCIExy b)
32 {
33  return av_cmp_q(a.x, b.x) == 0 && av_cmp_q(a.y, b.y) == 0;
34 }
35 
37 {
38  return cie_eq(a->wp, b->wp) &&
39  cie_eq(a->prim.r, b->prim.r) &&
40  cie_eq(a->prim.g, b->prim.g) &&
41  cie_eq(a->prim.b, b->prim.b);
42 }
43 
44 int main(void)
45 {
46  /* av_csp_luma_coeffs_from_avcsp: iterate every defined colorspace */
47  printf("Testing av_csp_luma_coeffs_from_avcsp()\n");
48  for (enum AVColorSpace csp = 0; csp < AVCOL_SPC_NB; csp++) {
50  const char *name = av_color_space_name(csp);
51  if (!c) {
52  printf("csp=%-16s -> NULL\n", name ? name : "?");
53  continue;
54  }
55  printf("csp=%-16s -> cr=%d/%d cg=%d/%d cb=%d/%d\n",
56  name ? name : "?",
57  c->cr.num, c->cr.den,
58  c->cg.num, c->cg.den,
59  c->cb.num, c->cb.den);
60  }
61  /* out-of-range enum */
62  printf("csp=AVCOL_SPC_NB -> %s\n",
63  av_csp_luma_coeffs_from_avcsp(AVCOL_SPC_NB) ? "FAIL" : "NULL");
64 
65  /* av_csp_primaries_desc_from_id + av_csp_primaries_id_from_desc */
66  printf("\nTesting av_csp_primaries_desc_from_id() round trip\n");
67  for (enum AVColorPrimaries prm = 0; prm < AVCOL_PRI_EXT_NB; prm++) {
68  const AVColorPrimariesDesc *d;
69  const AVColorPrimariesDesc *d_back;
70  enum AVColorPrimaries back;
71  const char *name, *back_name, *status;
72 
73  if (prm == AVCOL_PRI_NB)
74  prm = AVCOL_PRI_EXT_BASE;
75 
78  if (!d) {
79  printf("prm=%-16s -> NULL\n", name ? name : "?");
80  continue;
81  }
82  printf("prm=%-16s ->\n", name ? name : "?");
83  print_cie("wp ", d->wp);
84  print_cie("r ", d->prim.r);
85  print_cie("g ", d->prim.g);
86  print_cie("b ", d->prim.b);
87 
88  /* For colorspaces with identical primaries (e.g. smpte170m and
89  * smpte240m), the canonical first match may differ from the input
90  * enum, so compare descs not enums. id_from_desc only searches
91  * the base AVCOL_PRI_* range and returns UNSPECIFIED for
92  * extended-range inputs that have no base match. */
94  d_back = av_csp_primaries_desc_from_id(back);
95  back_name = av_color_primaries_name(back);
96  if (back == AVCOL_PRI_UNSPECIFIED)
97  status = prm >= AVCOL_PRI_EXT_BASE ? "ext-no-base-match"
98  : "MISMATCH";
99  else
100  status = d_back && desc_eq(d, d_back) ? "OK" : "MISMATCH";
101  printf(" round-trip id=%-16s desc=%s\n",
102  back_name ? back_name : "?", status);
103  }
104  /* out-of-range enum */
105  printf("prm=AVCOL_PRI_NB -> %s\n",
106  av_csp_primaries_desc_from_id(AVCOL_PRI_NB) ? "FAIL" : "NULL");
107 
108  /* id_from_desc on a garbage description returns UNSPECIFIED */
109  {
111  .wp = { { 0, 1 }, { 0, 1 } },
112  .prim = {
113  .r = { { 0, 1 }, { 0, 1 } },
114  .g = { { 0, 1 }, { 0, 1 } },
115  .b = { { 0, 1 }, { 0, 1 } },
116  },
117  };
119  printf("garbage desc -> %s (expect AVCOL_PRI_UNSPECIFIED)\n",
121  }
122 
123  /* av_csp_approximate_trc_gamma + av_csp_approximate_eotf_gamma:
124  * both return values from static tables, so output is bitexact. */
125  printf("\nTesting av_csp_approximate_{trc,eotf}_gamma()\n");
126  for (enum AVColorTransferCharacteristic trc = 0; trc < AVCOL_TRC_EXT_NB; trc++) {
127  const char *name;
128  double g_trc, g_eotf;
129 
130  if (trc == AVCOL_TRC_NB)
131  trc = AVCOL_TRC_EXT_BASE;
132 
134  g_trc = av_csp_approximate_trc_gamma(trc);
135  g_eotf = av_csp_approximate_eotf_gamma(trc);
136  printf("trc=%-16s trc_gamma=%.6f eotf_gamma=%.6f\n",
137  name ? name : "?", g_trc, g_eotf);
138  }
139  /* out-of-range enum: both must return 0.0 */
140  printf("trc=AVCOL_TRC_NB trc_gamma=%.6f eotf_gamma=%.6f\n",
143 
144  return 0;
145 }
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
printf
__device__ int printf(const char *,...)
AVColorTransferCharacteristic
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:666
AVColorPrimariesDesc::wp
AVWhitepointCoefficients wp
Definition: csp.h:79
AVColorPrimariesDesc
Struct that contains both white point location and primaries location, providing the complete descrip...
Definition: csp.h:78
print_cie
static void print_cie(const char *label, AVCIExy xy)
Definition: csp.c:25
AVCOL_TRC_NB
@ AVCOL_TRC_NB
Not part of ABI.
Definition: pixfmt.h:688
pixdesc.h
av_csp_luma_coeffs_from_avcsp
const struct AVLumaCoefficients * av_csp_luma_coeffs_from_avcsp(enum AVColorSpace csp)
Retrieves the Luma coefficients necessary to construct a conversion matrix from an enum constant desc...
Definition: csp.c:58
AVCOL_SPC_NB
@ AVCOL_SPC_NB
Not part of ABI.
Definition: pixfmt.h:720
b
#define b
Definition: input.c:43
AVLumaCoefficients
Struct containing luma coefficients to be used for RGB to YUV/YCoCg, or similar calculations.
Definition: csp.h:48
AVColorPrimaries
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:636
av_color_space_name
const char * av_color_space_name(enum AVColorSpace space)
Definition: pixdesc.c:3856
AVRational::num
int num
Numerator.
Definition: rational.h:59
garbage
static const uint8_t garbage[]
Definition: api-enc-parser-test.c:44
AVCOL_PRI_NB
@ AVCOL_PRI_NB
Not part of ABI.
Definition: pixfmt.h:654
av_csp_primaries_desc_from_id
const AVColorPrimariesDesc * av_csp_primaries_desc_from_id(enum AVColorPrimaries prm)
Retrieves a complete gamut description from an enum constant describing the color primaries.
Definition: csp.c:95
cie_eq
static int cie_eq(AVCIExy a, AVCIExy b)
Definition: csp.c:31
av_csp_primaries_id_from_desc
enum AVColorPrimaries av_csp_primaries_id_from_desc(const AVColorPrimariesDesc *prm)
Detects which enum AVColorPrimaries constant corresponds to the given complete gamut description.
Definition: csp.c:115
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:639
av_color_primaries_name
const char * av_color_primaries_name(enum AVColorPrimaries primaries)
Definition: pixdesc.c:3790
AVCIExy
Struct containing chromaticity x and y values for the standard CIE 1931 chromaticity definition.
Definition: csp.h:56
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
av_csp_approximate_eotf_gamma
double av_csp_approximate_eotf_gamma(enum AVColorTransferCharacteristic trc)
Determine a suitable EOTF 'gamma' value to match the supplied AVColorTransferCharacteristic.
Definition: csp.c:187
AVCIExy::x
AVRational x
Definition: csp.h:57
AVPrimaryCoefficients::b
AVCIExy b
Definition: csp.h:65
AVPrimaryCoefficients::r
AVCIExy r
Definition: csp.h:65
AVCOL_TRC_EXT_BASE
@ AVCOL_TRC_EXT_BASE
Definition: pixfmt.h:691
av_csp_approximate_trc_gamma
double av_csp_approximate_trc_gamma(enum AVColorTransferCharacteristic trc)
Determine a suitable 'gamma' value to match the supplied AVColorTransferCharacteristic.
Definition: csp.c:159
AVPrimaryCoefficients::g
AVCIExy g
Definition: csp.h:65
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
AVCOL_TRC_EXT_NB
@ AVCOL_TRC_EXT_NB
Not part of ABI.
Definition: pixfmt.h:693
AVColorSpace
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:700
av_cmp_q
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:89
pixfmt.h
AVCIExy::y
AVRational y
Definition: csp.h:57
desc_eq
static int desc_eq(const AVColorPrimariesDesc *a, const AVColorPrimariesDesc *b)
Definition: csp.c:36
status
ov_status_e status
Definition: dnn_backend_openvino.c:100
AVCOL_PRI_EXT_NB
@ AVCOL_PRI_EXT_NB
Not part of ABI.
Definition: pixfmt.h:659
AVRational::den
int den
Denominator.
Definition: rational.h:60
main
int main(void)
Definition: csp.c:44
AVColorPrimariesDesc::prim
AVPrimaryCoefficients prim
Definition: csp.h:80
av_color_transfer_name
const char * av_color_transfer_name(enum AVColorTransferCharacteristic transfer)
Definition: pixdesc.c:3823
AVCOL_PRI_EXT_BASE
@ AVCOL_PRI_EXT_BASE
Definition: pixfmt.h:657