FFmpeg
csp.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Ronald S. Bultje <rsbultje@gmail.com>
3  * This file is part of FFmpeg.
4  *
5  * FFmpeg is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * FFmpeg is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with FFmpeg; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /**
21  * @file Colorspace functions for libavutil
22  * @author Ronald S. Bultje <rsbultje@gmail.com>
23  * @author Leo Izen <leo.izen@gmail.com>
24  */
25 
26 #include <stdlib.h>
27 
28 #include "attributes.h"
29 #include "csp.h"
30 #include "pixfmt.h"
31 #include "rational.h"
32 
33 #define AVR(d) { (int)(d * 100000 + 0.5), 100000 }
34 
35 /*
36  * All constants explained in e.g. https://linuxtv.org/downloads/v4l-dvb-apis/ch02s06.html
37  * The older ones (bt470bg/m) are also explained in their respective ITU docs
38  * (e.g. https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.470-5-199802-S!!PDF-E.pdf)
39  * whereas the newer ones can typically be copied directly from wikipedia :)
40  */
42  [AVCOL_SPC_FCC] = { AVR(0.30), AVR(0.59), AVR(0.11) },
43  [AVCOL_SPC_BT470BG] = { AVR(0.299), AVR(0.587), AVR(0.114) },
44  [AVCOL_SPC_SMPTE170M] = { AVR(0.299), AVR(0.587), AVR(0.114) },
45  [AVCOL_SPC_BT709] = { AVR(0.2126), AVR(0.7152), AVR(0.0722) },
46  [AVCOL_SPC_SMPTE240M] = { AVR(0.212), AVR(0.701), AVR(0.087) },
47  [AVCOL_SPC_YCOCG] = { AVR(0.25), AVR(0.5), AVR(0.25) },
48  [AVCOL_SPC_RGB] = { AVR(1), AVR(1), AVR(1) },
49  [AVCOL_SPC_BT2020_NCL] = { AVR(0.2627), AVR(0.6780), AVR(0.0593) },
50  [AVCOL_SPC_BT2020_CL] = { AVR(0.2627), AVR(0.6780), AVR(0.0593) },
51 };
52 
54 {
55  const AVLumaCoefficients *coeffs;
56 
57  if (csp >= AVCOL_SPC_NB)
58  return NULL;
59  coeffs = &luma_coefficients[csp];
60  if (!coeffs->cr.num)
61  return NULL;
62 
63  return coeffs;
64 }
65 
66 #define WP_D65 { AVR(0.3127), AVR(0.3290) }
67 #define WP_C { AVR(0.3100), AVR(0.3160) }
68 #define WP_DCI { AVR(0.3140), AVR(0.3510) }
69 #define WP_E { {1, 3}, {1, 3} }
70 
72  [AVCOL_PRI_BT709] = { WP_D65, { { AVR(0.640), AVR(0.330) }, { AVR(0.300), AVR(0.600) }, { AVR(0.150), AVR(0.060) } } },
73  [AVCOL_PRI_BT470M] = { WP_C, { { AVR(0.670), AVR(0.330) }, { AVR(0.210), AVR(0.710) }, { AVR(0.140), AVR(0.080) } } },
74  [AVCOL_PRI_BT470BG] = { WP_D65, { { AVR(0.640), AVR(0.330) }, { AVR(0.290), AVR(0.600) }, { AVR(0.150), AVR(0.060) } } },
75  [AVCOL_PRI_SMPTE170M] = { WP_D65, { { AVR(0.630), AVR(0.340) }, { AVR(0.310), AVR(0.595) }, { AVR(0.155), AVR(0.070) } } },
76  [AVCOL_PRI_SMPTE240M] = { WP_D65, { { AVR(0.630), AVR(0.340) }, { AVR(0.310), AVR(0.595) }, { AVR(0.155), AVR(0.070) } } },
77  [AVCOL_PRI_SMPTE428] = { WP_E, { { AVR(0.735), AVR(0.265) }, { AVR(0.274), AVR(0.718) }, { AVR(0.167), AVR(0.009) } } },
78  [AVCOL_PRI_SMPTE431] = { WP_DCI, { { AVR(0.680), AVR(0.320) }, { AVR(0.265), AVR(0.690) }, { AVR(0.150), AVR(0.060) } } },
79  [AVCOL_PRI_SMPTE432] = { WP_D65, { { AVR(0.680), AVR(0.320) }, { AVR(0.265), AVR(0.690) }, { AVR(0.150), AVR(0.060) } } },
80  [AVCOL_PRI_FILM] = { WP_C, { { AVR(0.681), AVR(0.319) }, { AVR(0.243), AVR(0.692) }, { AVR(0.145), AVR(0.049) } } },
81  [AVCOL_PRI_BT2020] = { WP_D65, { { AVR(0.708), AVR(0.292) }, { AVR(0.170), AVR(0.797) }, { AVR(0.131), AVR(0.046) } } },
82  [AVCOL_PRI_JEDEC_P22] = { WP_D65, { { AVR(0.630), AVR(0.340) }, { AVR(0.295), AVR(0.605) }, { AVR(0.155), AVR(0.077) } } },
83 };
84 
86 {
87  const AVColorPrimariesDesc *p;
88 
89  if (prm >= AVCOL_PRI_NB)
90  return NULL;
91  p = &color_primaries[prm];
92  if (!p->prim.r.x.num)
93  return NULL;
94 
95  return p;
96 }
97 
99 {
100  AVRational diff = av_sub_q(r1, r2);
101  /* denominator assumed to be positive */
102  return av_make_q(abs(diff.num), diff.den);
103 }
104 
106 {
108 
109  for (enum AVColorPrimaries p = 0; p < AVCOL_PRI_NB; p++) {
111  if (!ref->prim.r.x.num)
112  continue;
113 
114  delta = abs_sub_q(prm->prim.r.x, ref->prim.r.x);
115  delta = av_add_q(delta, abs_sub_q(prm->prim.r.y, ref->prim.r.y));
116  delta = av_add_q(delta, abs_sub_q(prm->prim.g.x, ref->prim.g.x));
117  delta = av_add_q(delta, abs_sub_q(prm->prim.g.y, ref->prim.g.y));
118  delta = av_add_q(delta, abs_sub_q(prm->prim.b.x, ref->prim.b.x));
119  delta = av_add_q(delta, abs_sub_q(prm->prim.b.y, ref->prim.b.y));
120  delta = av_add_q(delta, abs_sub_q(prm->wp.x, ref->wp.x));
121  delta = av_add_q(delta, abs_sub_q(prm->wp.y, ref->wp.y));
122 
123  if (av_cmp_q(delta, av_make_q(1, 1000)) < 0)
124  return p;
125  }
126 
127  return AVCOL_PRI_UNSPECIFIED;
128 }
AVLumaCoefficients::cr
AVRational cr
Definition: csp.h:40
abs_sub_q
static av_always_inline AVRational abs_sub_q(AVRational r1, AVRational r2)
Definition: csp.c:98
AVColorPrimariesDesc::wp
AVWhitepointCoefficients wp
Definition: csp.h:70
AVColorPrimariesDesc
Struct that contains both white point location and primaries location, providing the complete descrip...
Definition: csp.h:69
WP_D65
#define WP_D65
Definition: csp.c:66
rational.h
AVCOL_SPC_YCOCG
@ AVCOL_SPC_YCOCG
Definition: pixfmt.h:535
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:53
AVCOL_SPC_NB
@ AVCOL_SPC_NB
Not part of ABI.
Definition: pixfmt.h:542
AVCOL_PRI_JEDEC_P22
@ AVCOL_PRI_JEDEC_P22
Definition: pixfmt.h:488
AVCOL_SPC_RGB
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition: pixfmt.h:526
AVLumaCoefficients
Struct containing luma coefficients to be used for RGB to YUV/YCoCg, or similar calculations.
Definition: csp.h:39
AVColorPrimaries
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:471
av_sub_q
AVRational av_sub_q(AVRational b, AVRational c)
Subtract one rational from another.
Definition: rational.c:101
AVCOL_SPC_BT2020_CL
@ AVCOL_SPC_BT2020_CL
ITU-R BT2020 constant luminance system.
Definition: pixfmt.h:537
AVCOL_SPC_BT470BG
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition: pixfmt.h:531
WP_C
#define WP_C
Definition: csp.c:67
AVRational::num
int num
Numerator.
Definition: rational.h:59
AVCOL_PRI_NB
@ AVCOL_PRI_NB
Not part of ABI.
Definition: pixfmt.h:489
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:85
AVCOL_SPC_SMPTE170M
@ AVCOL_SPC_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
Definition: pixfmt.h:532
AVCOL_PRI_SMPTE428
@ AVCOL_PRI_SMPTE428
SMPTE ST 428-1 (CIE 1931 XYZ)
Definition: pixfmt.h:483
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:105
AVCOL_PRI_SMPTE240M
@ AVCOL_PRI_SMPTE240M
identical to above, also called "SMPTE C" even though it uses D65
Definition: pixfmt.h:480
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:474
AVCOL_PRI_BT470BG
@ AVCOL_PRI_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:478
AVCOL_PRI_SMPTE170M
@ AVCOL_PRI_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:479
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVCOL_PRI_BT709
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition: pixfmt.h:473
abs
#define abs(x)
Definition: cuda_runtime.h:35
AVCOL_PRI_BT2020
@ AVCOL_PRI_BT2020
ITU-R BT2020.
Definition: pixfmt.h:482
AVCIExy::x
AVRational x
Definition: csp.h:48
color_primaries
static const AVColorPrimariesDesc color_primaries[AVCOL_PRI_NB]
Definition: csp.c:71
AVPrimaryCoefficients::b
AVCIExy b
Definition: csp.h:56
AVCOL_PRI_SMPTE431
@ AVCOL_PRI_SMPTE431
SMPTE ST 431-2 (2011) / DCI P3.
Definition: pixfmt.h:485
AVPrimaryCoefficients::r
AVCIExy r
Definition: csp.h:56
AVCOL_PRI_FILM
@ AVCOL_PRI_FILM
colour filters using Illuminant C
Definition: pixfmt.h:481
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
AVPrimaryCoefficients::g
AVCIExy g
Definition: csp.h:56
csp.h
attributes.h
AVCOL_SPC_SMPTE240M
@ AVCOL_SPC_SMPTE240M
derived from 170M primaries and D65 white point, 170M is derived from BT470 System M's primaries
Definition: pixfmt.h:533
AVCOL_SPC_BT2020_NCL
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:536
AVColorSpace
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:525
delta
float delta
Definition: vorbis_enc_data.h:430
av_always_inline
#define av_always_inline
Definition: attributes.h:49
av_cmp_q
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:89
AVCOL_PRI_BT470M
@ AVCOL_PRI_BT470M
also FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition: pixfmt.h:476
pixfmt.h
WP_E
#define WP_E
Definition: csp.c:69
AVR
#define AVR(d)
Definition: csp.c:33
AVCIExy::y
AVRational y
Definition: csp.h:48
WP_DCI
#define WP_DCI
Definition: csp.c:68
AVCOL_SPC_FCC
@ AVCOL_SPC_FCC
FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition: pixfmt.h:530
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:112
AVCOL_PRI_SMPTE432
@ AVCOL_PRI_SMPTE432
SMPTE ST 432-1 (2010) / P3 D65 / Display P3.
Definition: pixfmt.h:486
diff
static av_always_inline int diff(const uint32_t a, const uint32_t b)
Definition: vf_palettegen.c:139
av_add_q
AVRational av_add_q(AVRational b, AVRational c)
Add two rationals.
Definition: rational.c:93
AVColorPrimariesDesc::prim
AVPrimaryCoefficients prim
Definition: csp.h:71
luma_coefficients
static const struct AVLumaCoefficients luma_coefficients[AVCOL_SPC_NB]
Definition: csp.c:41
AVCOL_SPC_BT709
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition: pixfmt.h:527