| 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_invert(SwsMatrix3x3 *mat); | ||
| 53 | void ff_sws_matrix3x3_apply(const SwsMatrix3x3 *mat, float vec[3]); | ||
| 54 | |||
| 55 | SwsMatrix3x3 ff_sws_ipt_rgb2lms(const AVColorPrimariesDesc *prim); | ||
| 56 | SwsMatrix3x3 ff_sws_ipt_lms2rgb(const AVColorPrimariesDesc *prim); | ||
| 57 | |||
| 58 | /* Converts to/from XYZ (relative to the given white point, no adaptation) */ | ||
| 59 | SwsMatrix3x3 ff_sws_rgb2xyz(const AVColorPrimariesDesc *prim); | ||
| 60 | SwsMatrix3x3 ff_sws_xyz2rgb(const AVColorPrimariesDesc *prim); | ||
| 61 | |||
| 62 | /* Returns an RGB -> RGB adaptation matrix */ | ||
| 63 | SwsMatrix3x3 ff_sws_get_adaptation(const AVPrimaryCoefficients *prim, | ||
| 64 | AVWhitepointCoefficients from, | ||
| 65 | AVWhitepointCoefficients to); | ||
| 66 | |||
| 67 | /* Integer math definitions / helpers */ | ||
| 68 | typedef struct v3u8_t { | ||
| 69 | uint8_t x, y, z; | ||
| 70 | } v3u8_t; | ||
| 71 | |||
| 72 | typedef struct v2u16_t { | ||
| 73 | uint16_t x, y; | ||
| 74 | } v2u16_t; | ||
| 75 | |||
| 76 | typedef struct v3u16_t { | ||
| 77 | uint16_t x, y, z; | ||
| 78 | } v3u16_t; | ||
| 79 | |||
| 80 | /* Fast perceptual quantizer */ | ||
| 81 | static 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 | |||
| 87 | enum { PQ_LUT_SIZE = 1024 }; | ||
| 88 | extern const float ff_pq_eotf_lut[PQ_LUT_SIZE+1]; | ||
| 89 | |||
| 90 | ✗ | static 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 | |||
| 98 | ✗ | static 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 | */ | ||
| 111 | bool ff_prim_superset(const AVPrimaryCoefficients *a, const AVPrimaryCoefficients *b); | ||
| 112 | |||
| 113 | #endif /* SWSCALE_CSPUTILS_H */ | ||
| 114 |