FFmpeg
csputils.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 Niklas Haas
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 #ifndef SWSCALE_CSPUTILS_H
22 #define SWSCALE_CSPUTILS_H
23 
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include <math.h>
27 
28 #include "libavutil/attributes.h"
29 #include "libavutil/common.h"
30 #include "libavutil/csp.h"
31 #include "libavutil/pixfmt.h"
32 
33 /* Shared constants and helpers for colorspace mapping */
34 
35 #define fmixf(a, b, x) ((b) * (x) + (a) * (1 - (x)))
36 
37 static inline float smoothstepf(float edge0, float edge1, float x)
38 {
39  if (edge0 == edge1)
40  return x >= edge0;
41  x = (x - edge0) / (edge1 - edge0);
42  x = av_clipf(x, 0.0f, 1.0f);
43  return x * x * (3.0f - 2.0f * x);
44 }
45 
46 /* 3x3 matrix math */
47 typedef struct SwsMatrix3x3 {
48  float m[3][3];
49 } SwsMatrix3x3;
50 
54 void ff_sws_matrix3x3_apply(const SwsMatrix3x3 *mat, float vec[3]);
55 
58 
59 /* Converts to/from XYZ (relative to the given white point, no adaptation) */
62 
63 /* Returns an RGB -> RGB adaptation matrix */
67 
68 /* Integer math definitions / helpers */
69 typedef struct v3u8_t {
70  uint8_t x, y, z;
71 } v3u8_t;
72 
73 typedef struct v2u16_t {
74  uint16_t x, y;
75 } v2u16_t;
76 
77 typedef struct v3u16_t {
78  uint16_t x, y, z;
79 } v3u16_t;
80 
81 /* Fast perceptual quantizer */
82 static const float PQ_M1 = 2610./4096 * 1./4,
83  PQ_M2 = 2523./4096 * 128,
84  PQ_C1 = 3424./4096,
85  PQ_C2 = 2413./4096 * 32,
86  PQ_C3 = 2392./4096 * 32;
87 
88 enum { PQ_LUT_SIZE = 1024 };
89 extern const float ff_pq_eotf_lut[PQ_LUT_SIZE+1];
90 
91 static inline float pq_eotf(float x)
92 {
93  float idxf = av_clipf(x, 0.0f, 1.0f) * (PQ_LUT_SIZE - 1);
94  int ipart = floorf(idxf);
95  float fpart = idxf - ipart;
96  return fmixf(ff_pq_eotf_lut[ipart], ff_pq_eotf_lut[ipart + 1], fpart);
97 }
98 
99 static inline float pq_oetf(float x)
100 {
101  x = powf(fmaxf(x * 1e-4f, 0.0f), PQ_M1);
102  x = (PQ_C1 + PQ_C2 * x) / (1.0f + PQ_C3 * x);
103  return powf(x, PQ_M2);
104 }
105 
106 /* Misc colorspace math / helpers */
107 
108 /**
109  * Returns true if 'b' is entirely contained in 'a'. Useful for figuring out if
110  * colorimetric clipping will occur or not.
111  */
113 
114 #endif /* SWSCALE_CSPUTILS_H */
ff_sws_matrix3x3_rmul
void ff_sws_matrix3x3_rmul(const SwsMatrix3x3 *a, SwsMatrix3x3 *b)
Definition: csputils.c:39
v3u8_t::y
uint8_t y
Definition: csputils.h:70
AVColorPrimariesDesc
Struct that contains both white point location and primaries location, providing the complete descrip...
Definition: csp.h:78
PQ_C2
static const float PQ_C2
Definition: csputils.h:85
v2u16_t::x
uint16_t x
Definition: csputils.h:74
floorf
static __device__ float floorf(float a)
Definition: cuda_runtime.h:172
v3u16_t
Definition: csputils.h:77
smoothstepf
static float smoothstepf(float edge0, float edge1, float x)
Definition: csputils.h:37
b
#define b
Definition: input.c:41
SwsMatrix3x3::m
float m[3][3]
Definition: csputils.h:48
v3u16_t::y
uint16_t y
Definition: csputils.h:78
AVPrimaryCoefficients
Struct defining the red, green, and blue primary locations in terms of CIE 1931 chromaticity x and y.
Definition: csp.h:64
ff_sws_ipt_rgb2lms
SwsMatrix3x3 ff_sws_ipt_rgb2lms(const AVColorPrimariesDesc *prim)
Definition: csputils.c:232
PQ_C1
static const float PQ_C1
Definition: csputils.h:84
v3u8_t::z
uint8_t z
Definition: csputils.h:70
ff_sws_matrix3x3_mul
void ff_sws_matrix3x3_mul(SwsMatrix3x3 *a, const SwsMatrix3x3 *b)
Definition: csputils.c:26
pq_eotf
static float pq_eotf(float x)
Definition: csputils.h:91
from
const char * from
Definition: jacosubdec.c:66
to
const char * to
Definition: webvttdec.c:35
ff_sws_get_adaptation
SwsMatrix3x3 ff_sws_get_adaptation(const AVPrimaryCoefficients *prim, AVWhitepointCoefficients from, AVWhitepointCoefficients to)
Definition: csputils.c:204
PQ_M2
static const float PQ_M2
Definition: csputils.h:83
v2u16_t
Definition: csputils.h:73
av_clipf
av_clipf
Definition: af_crystalizer.c:122
AVCIExy
Struct containing chromaticity x and y values for the standard CIE 1931 chromaticity definition.
Definition: csp.h:56
ff_sws_matrix3x3_invert
void ff_sws_matrix3x3_invert(SwsMatrix3x3 *mat)
Definition: csputils.c:52
f
f
Definition: af_crystalizer.c:122
powf
#define powf(x, y)
Definition: libm.h:50
ff_prim_superset
bool ff_prim_superset(const AVPrimaryCoefficients *a, const AVPrimaryCoefficients *b)
Returns true if 'b' is entirely contained in 'a'.
Definition: csputils.c:281
fmaxf
float fmaxf(float, float)
fmixf
#define fmixf(a, b, x)
Definition: csputils.h:35
ff_sws_xyz2rgb
SwsMatrix3x3 ff_sws_xyz2rgb(const AVColorPrimariesDesc *prim)
Definition: csputils.c:143
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
attributes.h
SwsMatrix3x3
Definition: csputils.h:47
v3u8_t
Definition: csputils.h:69
ff_sws_ipt_lms2rgb
SwsMatrix3x3 ff_sws_ipt_lms2rgb(const AVColorPrimariesDesc *prim)
Definition: csputils.c:252
common.h
ff_sws_matrix3x3_apply
void ff_sws_matrix3x3_apply(const SwsMatrix3x3 *mat, float vec[3])
Definition: csputils.c:85
PQ_M1
static const float PQ_M1
Definition: csputils.h:82
v3u8_t::x
uint8_t x
Definition: csputils.h:70
pixfmt.h
v2u16_t::y
uint16_t y
Definition: csputils.h:74
ff_sws_rgb2xyz
SwsMatrix3x3 ff_sws_rgb2xyz(const AVColorPrimariesDesc *prim)
Definition: csputils.c:105
v3u16_t::x
uint16_t x
Definition: csputils.h:78
pq_oetf
static float pq_oetf(float x)
Definition: csputils.h:99
v3u16_t::z
uint16_t z
Definition: csputils.h:78
PQ_LUT_SIZE
@ PQ_LUT_SIZE
Definition: csputils.h:88
ff_pq_eotf_lut
const float ff_pq_eotf_lut[PQ_LUT_SIZE+1]
Definition: csputils.c:288
PQ_C3
static const float PQ_C3
Definition: csputils.h:86