Line |
Branch |
Exec |
Source |
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 |
|
|
|
51 |
|
|
void ff_sws_matrix3x3_mul(SwsMatrix3x3 *a, const SwsMatrix3x3 *b); |
52 |
|
|
void ff_sws_matrix3x3_rmul(const SwsMatrix3x3 *a, SwsMatrix3x3 *b); |
53 |
|
|
void ff_sws_matrix3x3_invert(SwsMatrix3x3 *mat); |
54 |
|
|
void ff_sws_matrix3x3_apply(const SwsMatrix3x3 *mat, float vec[3]); |
55 |
|
|
|
56 |
|
|
SwsMatrix3x3 ff_sws_ipt_rgb2lms(const AVColorPrimariesDesc *prim); |
57 |
|
|
SwsMatrix3x3 ff_sws_ipt_lms2rgb(const AVColorPrimariesDesc *prim); |
58 |
|
|
|
59 |
|
|
/* Converts to/from XYZ (relative to the given white point, no adaptation) */ |
60 |
|
|
SwsMatrix3x3 ff_sws_rgb2xyz(const AVColorPrimariesDesc *prim); |
61 |
|
|
SwsMatrix3x3 ff_sws_xyz2rgb(const AVColorPrimariesDesc *prim); |
62 |
|
|
|
63 |
|
|
/* Returns an RGB -> RGB adaptation matrix */ |
64 |
|
|
SwsMatrix3x3 ff_sws_get_adaptation(const AVPrimaryCoefficients *prim, |
65 |
|
|
AVWhitepointCoefficients from, |
66 |
|
|
AVWhitepointCoefficients to); |
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 |
|
|
*/ |
112 |
|
|
bool ff_prim_superset(const AVPrimaryCoefficients *a, const AVPrimaryCoefficients *b); |
113 |
|
|
|
114 |
|
|
#endif /* SWSCALE_CSPUTILS_H */ |
115 |
|
|
|