FFmpeg
Loading...
Searching...
No Matches
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
25static 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
31static 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
44int 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",
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)
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",
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}
static const uint8_t garbage[]
Colorspace value utility functions for libavutil.
__device__ int printf(const char *,...)
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
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
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
double av_csp_approximate_eotf_gamma(enum AVColorTransferCharacteristic trc)
Determine a suitable EOTF 'gamma' value to match the supplied AVColorTransferCharacteristic.
Definition csp.c:187
double av_csp_approximate_trc_gamma(enum AVColorTransferCharacteristic trc)
Determine a suitable 'gamma' value to match the supplied AVColorTransferCharacteristic.
Definition csp.c:159
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition rational.h:89
int a
#define b
Definition input.c:43
const char * av_color_space_name(enum AVColorSpace space)
Definition pixdesc.c:3860
const char * av_color_transfer_name(enum AVColorTransferCharacteristic transfer)
Definition pixdesc.c:3827
const char * av_color_primaries_name(enum AVColorPrimaries primaries)
Definition pixdesc.c:3794
pixel format definitions
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition pixfmt.h:642
@ AVCOL_PRI_NB
Not part of ABI.
Definition pixfmt.h:660
@ AVCOL_PRI_EXT_NB
Not part of ABI.
Definition pixfmt.h:665
@ AVCOL_PRI_EXT_BASE
Definition pixfmt.h:663
@ AVCOL_PRI_UNSPECIFIED
Definition pixfmt.h:645
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition pixfmt.h:672
@ AVCOL_TRC_EXT_BASE
Definition pixfmt.h:697
@ AVCOL_TRC_EXT_NB
Not part of ABI.
Definition pixfmt.h:699
@ AVCOL_TRC_NB
Not part of ABI.
Definition pixfmt.h:694
AVColorSpace
YUV colorspace type.
Definition pixfmt.h:706
@ AVCOL_SPC_NB
Not part of ABI.
Definition pixfmt.h:726
const char * name
Definition qsvenc.c:142
Struct containing chromaticity x and y values for the standard CIE 1931 chromaticity definition.
Definition csp.h:56
AVRational x
Definition csp.h:57
AVRational y
Definition csp.h:57
Struct that contains both white point location and primaries location, providing the complete descrip...
Definition csp.h:78
AVWhitepointCoefficients wp
Definition csp.h:79
AVPrimaryCoefficients prim
Definition csp.h:80
Struct containing luma coefficients to be used for RGB to YUV/YCoCg, or similar calculations.
Definition csp.h:48
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
static int cie_eq(AVCIExy a, AVCIExy b)
Definition csp.c:31
static int desc_eq(const AVColorPrimariesDesc *a, const AVColorPrimariesDesc *b)
Definition csp.c:36
int main(void)
Definition csp.c:44
static void print_cie(const char *label, AVCIExy xy)
Definition csp.c:25
static double c[64]