FFmpeg
Loading...
Searching...
No Matches
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
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
37static 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 */
47typedef struct SwsMatrix3x3 {
48 float m[3][3];
50
53void ff_sws_matrix3x3_apply(const SwsMatrix3x3 *mat, float vec[3]);
54
57
58/* Converts to/from XYZ (relative to the given white point, no adaptation) */
61
62/* Returns an RGB -> RGB adaptation matrix */
66
67/* Integer math definitions / helpers */
68typedef struct v2u16_t {
69 uint16_t x, y;
70} v2u16_t;
71
72typedef struct v3u16_t {
73 uint16_t x, y, z;
74} v3u16_t;
75
76typedef struct v3f32_t {
77 float x, y, z;
78} v3f32_t;
79
80/* Fast perceptual quantizer */
81static const float PQ_M1 = 2610./4096 * 1./4,
82 PQ_M2 = 2523./4096 * 128,
83 PQ_C1 = 3424./4096,
84 PQ_C2 = 2413./4096 * 32,
85 PQ_C3 = 2392./4096 * 32;
86
87enum { PQ_LUT_SIZE = 1024 };
88extern const float ff_pq_eotf_lut[PQ_LUT_SIZE+1];
89
90static inline float pq_eotf(float x)
91{
92 float idxf = av_clipf(x, 0.0f, 1.0f) * (PQ_LUT_SIZE - 1);
93 int ipart = floorf(idxf);
94 float fpart = idxf - ipart;
95 return fmixf(ff_pq_eotf_lut[ipart], ff_pq_eotf_lut[ipart + 1], fpart);
96}
97
98static inline float pq_oetf(float x)
99{
100 x = powf(fmaxf(x * 1e-4f, 0.0f), PQ_M1);
101 x = (PQ_C1 + PQ_C2 * x) / (1.0f + PQ_C3 * x);
102 return powf(x, PQ_M2);
103}
104
105/* Misc colorspace math / helpers */
106
107/**
108 * Returns true if 'b' is entirely contained in 'a'. Useful for figuring out if
109 * colorimetric clipping will occur or not.
110 */
112
113#endif /* SWSCALE_CSPUTILS_H */
common internal and external API header
#define av_clipf
Definition common.h:145
Colorspace value utility functions for libavutil.
const float ff_pq_eotf_lut[PQ_LUT_SIZE+1]
Definition csputils.c:275
void ff_sws_matrix3x3_invert(SwsMatrix3x3 *mat)
Definition csputils.c:39
SwsMatrix3x3 ff_sws_get_adaptation(const AVPrimaryCoefficients *prim, AVWhitepointCoefficients from, AVWhitepointCoefficients to)
Definition csputils.c:191
SwsMatrix3x3 ff_sws_ipt_lms2rgb(const AVColorPrimariesDesc *prim)
Definition csputils.c:239
static float smoothstepf(float edge0, float edge1, float x)
Definition csputils.h:37
static const float PQ_C3
Definition csputils.h:85
bool ff_prim_superset(const AVPrimaryCoefficients *a, const AVPrimaryCoefficients *b)
Returns true if 'b' is entirely contained in 'a'.
Definition csputils.c:268
SwsMatrix3x3 ff_sws_xyz2rgb(const AVColorPrimariesDesc *prim)
Definition csputils.c:130
void ff_sws_matrix3x3_apply(const SwsMatrix3x3 *mat, float vec[3])
Definition csputils.c:72
static const float PQ_C2
Definition csputils.h:84
@ PQ_LUT_SIZE
Definition csputils.h:87
static const float PQ_M2
Definition csputils.h:82
#define fmixf(a, b, x)
Definition csputils.h:35
static const float PQ_M1
Definition csputils.h:81
static float pq_oetf(float x)
Definition csputils.h:98
SwsMatrix3x3 ff_sws_rgb2xyz(const AVColorPrimariesDesc *prim)
Definition csputils.c:92
static float pq_eotf(float x)
Definition csputils.h:90
static const float PQ_C1
Definition csputils.h:83
SwsMatrix3x3 ff_sws_ipt_rgb2lms(const AVColorPrimariesDesc *prim)
Definition csputils.c:219
void ff_sws_matrix3x3_mul(SwsMatrix3x3 *a, const SwsMatrix3x3 *b)
Definition csputils.c:26
static __device__ float floorf(float a)
float fmaxf(float, float)
AVCIExy AVWhitepointCoefficients
Struct defining white point location in terms of CIE 1931 chromaticity x and y.
Definition csp.h:72
int a
#define b
Definition input.c:43
const char * from
Definition jacosubdec.c:64
const char * to
Definition webvttdec.c:36
Macro definitions for various function/variable attributes.
#define powf(x, y)
Definition libm.h:52
pixel format definitions
Struct that contains both white point location and primaries location, providing the complete descrip...
Definition csp.h:78
Struct defining the red, green, and blue primary locations in terms of CIE 1931 chromaticity x and y.
Definition csp.h:64
float m[3][3]
Definition csputils.h:48
uint16_t y
Definition csputils.h:69
uint16_t x
Definition csputils.h:69
float y
Definition csputils.h:77
float z
Definition csputils.h:77
float x
Definition csputils.h:77
uint16_t z
Definition csputils.h:73
uint16_t x
Definition csputils.h:73
uint16_t y
Definition csputils.h:73