1 /*
2  * This file is part of mpv.
3  *
4  * mpv 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  * mpv 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
12  * GNU 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 mpv.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef MPLAYER_CSPUTILS_H
19 #define MPLAYER_CSPUTILS_H
20 
21 #include <stdbool.h>
22 #include <stdint.h>
23 
24 #include "options/m_option.h"
25 
26 /* NOTE: the csp and levels AUTO values are converted to specific ones
27  * above vf/vo level. At least vf_scale relies on all valid settings being
28  * nonzero at vf/vo level.
29  */
30 
31 enum mp_csp {
32     MP_CSP_AUTO,
33     MP_CSP_BT_601,
34     MP_CSP_BT_709,
35     MP_CSP_SMPTE_240M,
36     MP_CSP_BT_2020_NC,
37     MP_CSP_BT_2020_C,
38     MP_CSP_RGB,
39     MP_CSP_XYZ,
40     MP_CSP_YCGCO,
41     MP_CSP_COUNT
42 };
43 
44 extern const struct m_opt_choice_alternatives mp_csp_names[];
45 
46 enum mp_csp_levels {
47     MP_CSP_LEVELS_AUTO,
48     MP_CSP_LEVELS_TV,
49     MP_CSP_LEVELS_PC,
50     MP_CSP_LEVELS_COUNT,
51 };
52 
53 extern const struct m_opt_choice_alternatives mp_csp_levels_names[];
54 
55 enum mp_csp_prim {
56     MP_CSP_PRIM_AUTO,
57     MP_CSP_PRIM_BT_601_525,
58     MP_CSP_PRIM_BT_601_625,
59     MP_CSP_PRIM_BT_709,
60     MP_CSP_PRIM_BT_2020,
61     MP_CSP_PRIM_BT_470M,
62     MP_CSP_PRIM_APPLE,
63     MP_CSP_PRIM_ADOBE,
64     MP_CSP_PRIM_PRO_PHOTO,
65     MP_CSP_PRIM_CIE_1931,
66     MP_CSP_PRIM_DCI_P3,
67     MP_CSP_PRIM_DISPLAY_P3,
68     MP_CSP_PRIM_V_GAMUT,
69     MP_CSP_PRIM_S_GAMUT,
70     MP_CSP_PRIM_COUNT
71 };
72 
73 extern const struct m_opt_choice_alternatives mp_csp_prim_names[];
74 
75 enum mp_csp_trc {
76     MP_CSP_TRC_AUTO,
77     MP_CSP_TRC_BT_1886,
78     MP_CSP_TRC_SRGB,
79     MP_CSP_TRC_LINEAR,
80     MP_CSP_TRC_GAMMA18,
81     MP_CSP_TRC_GAMMA20,
82     MP_CSP_TRC_GAMMA22,
83     MP_CSP_TRC_GAMMA24,
84     MP_CSP_TRC_GAMMA26,
85     MP_CSP_TRC_GAMMA28,
86     MP_CSP_TRC_PRO_PHOTO,
87     MP_CSP_TRC_PQ,
88     MP_CSP_TRC_HLG,
89     MP_CSP_TRC_V_LOG,
90     MP_CSP_TRC_S_LOG1,
91     MP_CSP_TRC_S_LOG2,
92     MP_CSP_TRC_COUNT
93 };
94 
95 extern const struct m_opt_choice_alternatives mp_csp_trc_names[];
96 
97 enum mp_csp_light {
98     MP_CSP_LIGHT_AUTO,
99     MP_CSP_LIGHT_DISPLAY,
100     MP_CSP_LIGHT_SCENE_HLG,
101     MP_CSP_LIGHT_SCENE_709_1886,
102     MP_CSP_LIGHT_SCENE_1_2,
103     MP_CSP_LIGHT_COUNT
104 };
105 
106 extern const struct m_opt_choice_alternatives mp_csp_light_names[];
107 
108 // These constants are based on the ICC specification (Table 23) and match
109 // up with the API of LittleCMS, which treats them as integers.
110 enum mp_render_intent {
111     MP_INTENT_PERCEPTUAL = 0,
112     MP_INTENT_RELATIVE_COLORIMETRIC = 1,
113     MP_INTENT_SATURATION = 2,
114     MP_INTENT_ABSOLUTE_COLORIMETRIC = 3
115 };
116 
117 // The numeric values (except -1) match the Matroska StereoMode element value.
118 enum mp_stereo3d_mode {
119     MP_STEREO3D_INVALID = -1,
120     /* only modes explicitly referenced in the code are listed */
121     MP_STEREO3D_MONO = 0,
122     MP_STEREO3D_SBS2L = 1,
123     MP_STEREO3D_AB2R = 2,
124     MP_STEREO3D_AB2L = 3,
125     MP_STEREO3D_SBS2R = 11,
126     /* no explicit enum entries for most valid values */
127     MP_STEREO3D_COUNT = 15, // 14 is last valid mode
128 };
129 
130 extern const struct m_opt_choice_alternatives mp_stereo3d_names[];
131 
132 #define MP_STEREO3D_NAME(x) m_opt_choice_str(mp_stereo3d_names, x)
133 
134 #define MP_STEREO3D_NAME_DEF(x, def) \
135     (MP_STEREO3D_NAME(x) ? MP_STEREO3D_NAME(x) : (def))
136 
137 struct mp_colorspace {
138     enum mp_csp space;
139     enum mp_csp_levels levels;
140     enum mp_csp_prim primaries;
141     enum mp_csp_trc gamma;
142     enum mp_csp_light light;
143     float sig_peak; // highest relative value in signal. 0 = unknown/auto
144 };
145 
146 // For many colorspace conversions, in particular those involving HDR, an
147 // implicit reference white level is needed. Since this magic constant shows up
148 // a lot, give it an explicit name. The value of 203 cd/m² comes from ITU-R
149 // Report BT.2408, and the value for HLG comes from the cited HLG 75% level
150 // (transferred to scene space).
151 #define MP_REF_WHITE 203.0
152 #define MP_REF_WHITE_HLG 3.17955
153 
154 // Replaces unknown values in the first struct by those of the second struct
155 void mp_colorspace_merge(struct mp_colorspace *orig, struct mp_colorspace *new);
156 
157 struct mp_csp_params {
158     struct mp_colorspace color; // input colorspace
159     enum mp_csp_levels levels_out; // output device
160     float brightness;
161     float contrast;
162     float hue;
163     float saturation;
164     float gamma;
165     // discard U/V components
166     bool gray;
167     // input is already centered and range-expanded
168     bool is_float;
169     // texture_bits/input_bits is for rescaling fixed point input to range [0,1]
170     int texture_bits;
171     int input_bits;
172 };
173 
174 #define MP_CSP_PARAMS_DEFAULTS {                                \
175     .color = { .space = MP_CSP_BT_601,                          \
176                .levels = MP_CSP_LEVELS_TV },                    \
177     .levels_out = MP_CSP_LEVELS_PC,                             \
178     .brightness = 0, .contrast = 1, .hue = 0, .saturation = 1,  \
179     .gamma = 1, .texture_bits = 8, .input_bits = 8}
180 
181 struct mp_image_params;
182 void mp_csp_set_image_params(struct mp_csp_params *params,
183                              const struct mp_image_params *imgparams);
184 
185 bool mp_colorspace_equal(struct mp_colorspace c1, struct mp_colorspace c2);
186 
187 enum mp_chroma_location {
188     MP_CHROMA_AUTO,
189     MP_CHROMA_TOPLEFT,  // uhd
190     MP_CHROMA_LEFT,     // mpeg2/4, h264
191     MP_CHROMA_CENTER,   // mpeg1, jpeg
192     MP_CHROMA_COUNT,
193 };
194 
195 extern const struct m_opt_choice_alternatives mp_chroma_names[];
196 
197 enum mp_alpha_type {
198     MP_ALPHA_AUTO,
199     MP_ALPHA_STRAIGHT,
200     MP_ALPHA_PREMUL,
201 };
202 
203 extern const struct m_opt_choice_alternatives mp_alpha_names[];
204 
205 extern const struct m_sub_options mp_csp_equalizer_conf;
206 
207 enum mp_csp_equalizer_param {
208     MP_CSP_EQ_BRIGHTNESS,
209     MP_CSP_EQ_CONTRAST,
210     MP_CSP_EQ_HUE,
211     MP_CSP_EQ_SATURATION,
212     MP_CSP_EQ_GAMMA,
213     MP_CSP_EQ_OUTPUT_LEVELS,
214     MP_CSP_EQ_COUNT,
215 };
216 
217 // Default initialization with 0 is enough, except for the capabilities field
218 struct mp_csp_equalizer_opts {
219     // Value for each property is in the range [-100, 100].
220     // 0 is default, meaning neutral or no change.
221     int values[MP_CSP_EQ_COUNT];
222 };
223 
224 void mp_csp_copy_equalizer_values(struct mp_csp_params *params,
225                                   const struct mp_csp_equalizer_opts *eq);
226 
227 struct mpv_global;
228 struct mp_csp_equalizer_state *mp_csp_equalizer_create(void *ta_parent,
229                                                     struct mpv_global *global);
230 bool mp_csp_equalizer_state_changed(struct mp_csp_equalizer_state *state);
231 void mp_csp_equalizer_state_get(struct mp_csp_equalizer_state *state,
232                                 struct mp_csp_params *params);
233 
234 struct mp_csp_col_xy {
235     float x, y;
236 };
237 
mp_xy_X(struct mp_csp_col_xy xy)238 static inline float mp_xy_X(struct mp_csp_col_xy xy) {
239     return xy.x / xy.y;
240 }
241 
mp_xy_Z(struct mp_csp_col_xy xy)242 static inline float mp_xy_Z(struct mp_csp_col_xy xy) {
243     return (1 - xy.x - xy.y) / xy.y;
244 }
245 
246 struct mp_csp_primaries {
247     struct mp_csp_col_xy red, green, blue, white;
248 };
249 
250 enum mp_csp avcol_spc_to_mp_csp(int avcolorspace);
251 enum mp_csp_levels avcol_range_to_mp_csp_levels(int avrange);
252 enum mp_csp_prim avcol_pri_to_mp_csp_prim(int avpri);
253 enum mp_csp_trc avcol_trc_to_mp_csp_trc(int avtrc);
254 
255 int mp_csp_to_avcol_spc(enum mp_csp colorspace);
256 int mp_csp_levels_to_avcol_range(enum mp_csp_levels range);
257 int mp_csp_prim_to_avcol_pri(enum mp_csp_prim prim);
258 int mp_csp_trc_to_avcol_trc(enum mp_csp_trc trc);
259 
260 enum mp_csp mp_csp_guess_colorspace(int width, int height);
261 enum mp_csp_prim mp_csp_guess_primaries(int width, int height);
262 
263 enum mp_chroma_location avchroma_location_to_mp(int avloc);
264 int mp_chroma_location_to_av(enum mp_chroma_location mploc);
265 void mp_get_chroma_location(enum mp_chroma_location loc, int *x, int *y);
266 
267 struct mp_csp_primaries mp_get_csp_primaries(enum mp_csp_prim csp);
268 float mp_trc_nom_peak(enum mp_csp_trc trc);
269 bool mp_trc_is_hdr(enum mp_csp_trc trc);
270 
271 /* Color conversion matrix: RGB = m * YUV + c
272  * m is in row-major matrix, with m[row][col], e.g.:
273  *     [ a11 a12 a13 ]     float m[3][3] = { { a11, a12, a13 },
274  *     [ a21 a22 a23 ]                       { a21, a22, a23 },
275  *     [ a31 a32 a33 ]                       { a31, a32, a33 } };
276  * This is accessed as e.g.: m[2-1][1-1] = a21
277  * In particular, each row contains all the coefficients for one of R, G, B,
278  * while each column contains all the coefficients for one of Y, U, V:
279  *     m[r,g,b][y,u,v] = ...
280  * The matrix could also be viewed as group of 3 vectors, e.g. the 1st column
281  * is the Y vector (1, 1, 1), the 2nd is the U vector, the 3rd the V vector.
282  * The matrix might also be used for other conversions and colorspaces.
283  */
284 struct mp_cmat {
285     float m[3][3];
286     float c[3];
287 };
288 
289 void mp_get_rgb2xyz_matrix(struct mp_csp_primaries space, float m[3][3]);
290 void mp_get_cms_matrix(struct mp_csp_primaries src, struct mp_csp_primaries dest,
291                        enum mp_render_intent intent, float cms_matrix[3][3]);
292 
293 double mp_get_csp_mul(enum mp_csp csp, int input_bits, int texture_bits);
294 void mp_get_csp_uint_mul(enum mp_csp csp, enum mp_csp_levels levels,
295                          int bits, int component, double *out_m, double *out_o);
296 void mp_get_csp_matrix(struct mp_csp_params *params, struct mp_cmat *out);
297 
298 void mp_invert_matrix3x3(float m[3][3]);
299 void mp_invert_cmat(struct mp_cmat *out, struct mp_cmat *in);
300 void mp_map_fixp_color(struct mp_cmat *matrix, int ibits, int in[3],
301                                                int obits, int out[3]);
302 
303 #endif /* MPLAYER_CSPUTILS_H */
304