/src/mpv/video/csputils.c
Line | Count | Source |
1 | | /* |
2 | | * Common code related to colorspaces and conversion |
3 | | * |
4 | | * Copyleft (C) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de> |
5 | | * |
6 | | * This file is part of mpv. |
7 | | * |
8 | | * mpv is free software; you can redistribute it and/or |
9 | | * modify it under the terms of the GNU Lesser General Public |
10 | | * License as published by the Free Software Foundation; either |
11 | | * version 2.1 of the License, or (at your option) any later version. |
12 | | * |
13 | | * mpv is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public |
19 | | * License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
20 | | */ |
21 | | |
22 | | #include <stdint.h> |
23 | | #include <math.h> |
24 | | #include <assert.h> |
25 | | #include <libavutil/common.h> |
26 | | #include <libavutil/stereo3d.h> |
27 | | #include <libavcodec/avcodec.h> |
28 | | |
29 | | #include "mp_image.h" |
30 | | #include "csputils.h" |
31 | | #include "common/msg.h" |
32 | | #include "options/m_config.h" |
33 | | #include "options/m_option.h" |
34 | | |
35 | | const struct m_opt_choice_alternatives pl_csp_names[] = { |
36 | | {"auto", PL_COLOR_SYSTEM_UNKNOWN}, |
37 | | {"bt.601", PL_COLOR_SYSTEM_BT_601}, |
38 | | {"bt.709", PL_COLOR_SYSTEM_BT_709}, |
39 | | {"smpte-240m", PL_COLOR_SYSTEM_SMPTE_240M}, |
40 | | {"bt.2020-ncl", PL_COLOR_SYSTEM_BT_2020_NC}, |
41 | | {"bt.2020-cl", PL_COLOR_SYSTEM_BT_2020_C}, |
42 | | {"bt.2100-pq", PL_COLOR_SYSTEM_BT_2100_PQ}, |
43 | | {"bt.2100-hlg", PL_COLOR_SYSTEM_BT_2100_HLG}, |
44 | | {"dolbyvision", PL_COLOR_SYSTEM_DOLBYVISION}, |
45 | | {"rgb", PL_COLOR_SYSTEM_RGB}, |
46 | | {"xyz", PL_COLOR_SYSTEM_XYZ}, |
47 | | {"ycgco", PL_COLOR_SYSTEM_YCGCO}, |
48 | | {"ycgco-re", PL_COLOR_SYSTEM_YCGCO_RE}, |
49 | | {"ycgco-ro", PL_COLOR_SYSTEM_YCGCO_RO}, |
50 | | {0} |
51 | | }; |
52 | | |
53 | | const struct m_opt_choice_alternatives pl_csp_levels_names[] = { |
54 | | {"auto", PL_COLOR_LEVELS_UNKNOWN}, |
55 | | {"limited", PL_COLOR_LEVELS_LIMITED}, |
56 | | {"full", PL_COLOR_LEVELS_FULL}, |
57 | | {0} |
58 | | }; |
59 | | |
60 | | const struct m_opt_choice_alternatives pl_csp_prim_names[] = { |
61 | | {"auto", PL_COLOR_PRIM_UNKNOWN}, |
62 | | {"bt.601-525", PL_COLOR_PRIM_BT_601_525}, |
63 | | {"bt.601-625", PL_COLOR_PRIM_BT_601_625}, |
64 | | {"bt.709", PL_COLOR_PRIM_BT_709}, |
65 | | {"bt.2020", PL_COLOR_PRIM_BT_2020}, |
66 | | {"bt.470m", PL_COLOR_PRIM_BT_470M}, |
67 | | {"apple", PL_COLOR_PRIM_APPLE}, |
68 | | {"adobe", PL_COLOR_PRIM_ADOBE}, |
69 | | {"prophoto", PL_COLOR_PRIM_PRO_PHOTO}, |
70 | | {"cie1931", PL_COLOR_PRIM_CIE_1931}, |
71 | | {"dci-p3", PL_COLOR_PRIM_DCI_P3}, |
72 | | {"display-p3", PL_COLOR_PRIM_DISPLAY_P3}, |
73 | | {"v-gamut", PL_COLOR_PRIM_V_GAMUT}, |
74 | | {"s-gamut", PL_COLOR_PRIM_S_GAMUT}, |
75 | | {"ebu3213", PL_COLOR_PRIM_EBU_3213}, |
76 | | {"film-c", PL_COLOR_PRIM_FILM_C}, |
77 | | {"aces-ap0", PL_COLOR_PRIM_ACES_AP0}, |
78 | | {"aces-ap1", PL_COLOR_PRIM_ACES_AP1}, |
79 | | {0} |
80 | | }; |
81 | | |
82 | | const struct m_opt_choice_alternatives pl_csp_trc_names[] = { |
83 | | {"auto", PL_COLOR_TRC_UNKNOWN}, |
84 | | {"bt.1886", PL_COLOR_TRC_BT_1886}, |
85 | | {"srgb", PL_COLOR_TRC_SRGB}, |
86 | | {"linear", PL_COLOR_TRC_LINEAR}, |
87 | | {"gamma1.8", PL_COLOR_TRC_GAMMA18}, |
88 | | {"gamma2.0", PL_COLOR_TRC_GAMMA20}, |
89 | | {"gamma2.2", PL_COLOR_TRC_GAMMA22}, |
90 | | {"gamma2.4", PL_COLOR_TRC_GAMMA24}, |
91 | | {"gamma2.6", PL_COLOR_TRC_GAMMA26}, |
92 | | {"gamma2.8", PL_COLOR_TRC_GAMMA28}, |
93 | | {"prophoto", PL_COLOR_TRC_PRO_PHOTO}, |
94 | | {"pq", PL_COLOR_TRC_PQ}, |
95 | | {"hlg", PL_COLOR_TRC_HLG}, |
96 | | {"v-log", PL_COLOR_TRC_V_LOG}, |
97 | | {"s-log1", PL_COLOR_TRC_S_LOG1}, |
98 | | {"s-log2", PL_COLOR_TRC_S_LOG2}, |
99 | | {"st428", PL_COLOR_TRC_ST428}, |
100 | | {"scrgb", PL_COLOR_TRC_SCRGB}, |
101 | | {0} |
102 | | }; |
103 | | |
104 | | const struct m_opt_choice_alternatives mp_csp_light_names[] = { |
105 | | {"auto", MP_CSP_LIGHT_AUTO}, |
106 | | {"display", MP_CSP_LIGHT_DISPLAY}, |
107 | | {"hlg", MP_CSP_LIGHT_SCENE_HLG}, |
108 | | {"709-1886", MP_CSP_LIGHT_SCENE_709_1886}, |
109 | | {"gamma1.2", MP_CSP_LIGHT_SCENE_1_2}, |
110 | | {0} |
111 | | }; |
112 | | |
113 | | const struct m_opt_choice_alternatives pl_chroma_names[] = { |
114 | | {"unknown", PL_CHROMA_UNKNOWN}, |
115 | | {"uhd", PL_CHROMA_TOP_LEFT}, |
116 | | {"mpeg2/4/h264",PL_CHROMA_LEFT}, |
117 | | {"mpeg1/jpeg", PL_CHROMA_CENTER}, |
118 | | {"top", PL_CHROMA_TOP_CENTER}, |
119 | | {"bottom-left", PL_CHROMA_BOTTOM_LEFT}, |
120 | | {"bottom", PL_CHROMA_BOTTOM_CENTER}, |
121 | | {0} |
122 | | }; |
123 | | |
124 | | const struct m_opt_choice_alternatives pl_alpha_names[] = { |
125 | | {"auto", PL_ALPHA_UNKNOWN}, |
126 | | {"straight", PL_ALPHA_INDEPENDENT}, |
127 | | {"premul", PL_ALPHA_PREMULTIPLIED}, |
128 | | {"none", PL_ALPHA_NONE}, |
129 | | {0} |
130 | | }; |
131 | | |
132 | | // The short names follow the input format names of the vf_stereo3d. |
133 | | // The long name in comments is closer to the Matroska spec (StereoMode element). |
134 | | // The numeric index matches the Matroska StereoMode value. If you add entries |
135 | | // that don't match Matroska, make sure demux_mkv.c rejects them properly. |
136 | | const struct m_opt_choice_alternatives mp_stereo3d_names[] = { |
137 | | {"no", -1}, // unset, vf_format keeps the source mode |
138 | | {"mono", 0}, |
139 | | {"sbs2l", 1}, // "side_by_side_left" |
140 | | {"ab2r", 2}, // "top_bottom_right" |
141 | | {"ab2l", 3}, // "top_bottom_left" |
142 | | {"checkr", 4}, // "checkboard_right" (unsupported by vf_stereo3d) |
143 | | {"checkl", 5}, // "checkboard_left" (unsupported by vf_stereo3d) |
144 | | {"irr", 6}, // "row_interleaved_right" |
145 | | {"irl", 7}, // "row_interleaved_left" |
146 | | {"icr", 8}, // "column_interleaved_right" |
147 | | {"icl", 9}, // "column_interleaved_left" |
148 | | {"arcc", 10}, // "anaglyph_cyan_red" (Matroska: unclear which mode) |
149 | | {"sbs2r", 11}, // "side_by_side_right" |
150 | | {"agmc", 12}, // "anaglyph_green_magenta" (Matroska: unclear which mode) |
151 | | {"al", 13}, // "alternating frames left first" |
152 | | {"ar", 14}, // "alternating frames right first" |
153 | | {0} |
154 | | }; |
155 | | |
156 | | enum mp_stereo3d_mode mp_stereo3d_from_av(const struct AVStereo3D *s3d) |
157 | 1.32k | { |
158 | 1.32k | bool inv = s3d->flags & AV_STEREO3D_FLAG_INVERT; |
159 | 1.32k | switch (s3d->type) { |
160 | 39 | case AV_STEREO3D_SIDEBYSIDE: |
161 | 78 | case AV_STEREO3D_SIDEBYSIDE_QUINCUNX: |
162 | 78 | return inv ? MP_STEREO3D_SBS2R : MP_STEREO3D_SBS2L; |
163 | 67 | case AV_STEREO3D_TOPBOTTOM: |
164 | 67 | return inv ? MP_STEREO3D_AB2R : MP_STEREO3D_AB2L; |
165 | 9 | case AV_STEREO3D_CHECKERBOARD: |
166 | 9 | return inv ? MP_STEREO3D_CHECKR : MP_STEREO3D_CHECKL; |
167 | 0 | case AV_STEREO3D_LINES: |
168 | 0 | return inv ? MP_STEREO3D_IRR : MP_STEREO3D_IRL; |
169 | 0 | case AV_STEREO3D_COLUMNS: |
170 | 0 | return inv ? MP_STEREO3D_ICR : MP_STEREO3D_ICL; |
171 | 12 | case AV_STEREO3D_FRAMESEQUENCE: |
172 | 12 | return inv ? MP_STEREO3D_AR : MP_STEREO3D_AL; |
173 | 1.15k | default: |
174 | | // AV_STEREO3D_2D and types mpv has no mode for. |
175 | 1.15k | return MP_STEREO3D_MONO; |
176 | 1.32k | } |
177 | 1.32k | } |
178 | | |
179 | | void mp_get_3d_side_by_side(int stereo_mode, int div[2]) |
180 | 0 | { |
181 | 0 | div[0] = div[1] = 1; |
182 | 0 | switch (stereo_mode) { |
183 | 0 | case MP_STEREO3D_SBS2L: |
184 | 0 | case MP_STEREO3D_SBS2R: div[0] = 2; break; |
185 | 0 | case MP_STEREO3D_AB2R: |
186 | 0 | case MP_STEREO3D_AB2L: div[1] = 2; break; |
187 | 0 | } |
188 | 0 | } |
189 | | |
190 | | enum pl_color_system mp_csp_guess_colorspace(int width, int height) |
191 | 95.0k | { |
192 | 95.0k | return width >= 1280 || height > 576 ? PL_COLOR_SYSTEM_BT_709 : PL_COLOR_SYSTEM_BT_601; |
193 | 95.0k | } |
194 | | |
195 | | enum pl_color_primaries mp_csp_guess_primaries(int width, int height) |
196 | 88.0k | { |
197 | | // HD content |
198 | 88.0k | if (width >= 1280 || height > 576) |
199 | 238 | return PL_COLOR_PRIM_BT_709; |
200 | | |
201 | 87.8k | switch (height) { |
202 | 59 | case 576: // Typical PAL content, including anamorphic/squared |
203 | 59 | return PL_COLOR_PRIM_BT_601_625; |
204 | | |
205 | 2.33k | case 480: // Typical NTSC content, including squared |
206 | 2.33k | case 486: // NTSC Pro or anamorphic NTSC |
207 | 2.33k | return PL_COLOR_PRIM_BT_601_525; |
208 | | |
209 | 85.4k | default: // No good metric, just pick BT.709 to minimize damage |
210 | 85.4k | return PL_COLOR_PRIM_BT_709; |
211 | 87.8k | } |
212 | 87.8k | } |
213 | | |
214 | | // Get multiplication factor required if image data is fit within the LSBs of a |
215 | | // higher smaller bit depth fixed-point texture data. |
216 | | // This is broken. Use mp_get_csp_uint_mul(). |
217 | | double mp_get_csp_mul(enum pl_color_system csp, int input_bits, int texture_bits) |
218 | 0 | { |
219 | 0 | mp_assert(texture_bits >= input_bits); |
220 | | |
221 | | // Convenience for some irrelevant cases, e.g. rgb565 or disabling expansion. |
222 | 0 | if (!input_bits) |
223 | 0 | return 1; |
224 | | |
225 | | // RGB always uses the full range available. |
226 | 0 | if (csp == PL_COLOR_SYSTEM_RGB) |
227 | 0 | return ((1LL << input_bits) - 1.) / ((1LL << texture_bits) - 1.); |
228 | | |
229 | 0 | if (csp == PL_COLOR_SYSTEM_XYZ) |
230 | 0 | return 1; |
231 | | |
232 | | // High bit depth YUV uses a range shifted from 8 bit. |
233 | 0 | return (1LL << input_bits) / ((1LL << texture_bits) - 1.) * 255 / 256; |
234 | 0 | } |
235 | | |
236 | | // Return information about color fixed point representation.his is needed for |
237 | | // converting color from integer formats to or from float. Use as follows: |
238 | | // float_val = uint_val * m + o |
239 | | // uint_val = clamp(round((float_val - o) / m)) |
240 | | // See H.264/5 Annex E. |
241 | | // csp: colorspace |
242 | | // levels: full range flag |
243 | | // component: ID of the channel, as in mp_regular_imgfmt: |
244 | | // 1 is red/luminance/gray, 2 is green/Cb, 3 is blue/Cr, 4 is alpha. |
245 | | // bits: number of significant bits, e.g. 10 for yuv420p10, 16 for p010 |
246 | | // out_m: returns factor to multiply the uint number with |
247 | | // out_o: returns offset to add after multiplication |
248 | | void mp_get_csp_uint_mul(enum pl_color_system csp, enum pl_color_levels levels, |
249 | | int bits, int component, double *out_m, double *out_o) |
250 | 68.1k | { |
251 | 68.1k | uint16_t i_min = 0; |
252 | 68.1k | uint16_t i_max = (1u << bits) - 1; |
253 | 68.1k | double f_min = 0; // min. float value |
254 | | |
255 | 68.1k | if (csp != PL_COLOR_SYSTEM_RGB && component != 4) { |
256 | 64.1k | if (component == 2 || component == 3) { |
257 | 41.1k | f_min = (1u << (bits - 1)) / -(double)i_max; // force center => 0 |
258 | | |
259 | 41.1k | if (levels != PL_COLOR_LEVELS_FULL && bits >= 8) { |
260 | 39.4k | i_min = 16 << (bits - 8); // => -0.5 |
261 | 39.4k | i_max = 240 << (bits - 8); // => 0.5 |
262 | 39.4k | f_min = -0.5; |
263 | 39.4k | } |
264 | 41.1k | } else { |
265 | 23.0k | if (levels != PL_COLOR_LEVELS_FULL && bits >= 8) { |
266 | 22.1k | i_min = 16 << (bits - 8); // => 0 |
267 | 22.1k | i_max = 235 << (bits - 8); // => 1 |
268 | 22.1k | } |
269 | 23.0k | } |
270 | 64.1k | } |
271 | | |
272 | 68.1k | *out_m = 1.0 / (i_max - i_min); |
273 | 68.1k | *out_o = (1 + f_min) - i_max * *out_m; |
274 | 68.1k | } |
275 | | |
276 | | /* Fill in the Y, U, V vectors of a yuv-to-rgb conversion matrix |
277 | | * based on the given luma weights of the R, G and B components (lr, lg, lb). |
278 | | * lr+lg+lb is assumed to equal 1. |
279 | | * This function is meant for colorspaces satisfying the following |
280 | | * conditions (which are true for common YUV colorspaces): |
281 | | * - The mapping from input [Y, U, V] to output [R, G, B] is linear. |
282 | | * - Y is the vector [1, 1, 1]. (meaning input Y component maps to 1R+1G+1B) |
283 | | * - U maps to a value with zero R and positive B ([0, x, y], y > 0; |
284 | | * i.e. blue and green only). |
285 | | * - V maps to a value with zero B and positive R ([x, y, 0], x > 0; |
286 | | * i.e. red and green only). |
287 | | * - U and V are orthogonal to the luma vector [lr, lg, lb]. |
288 | | * - The magnitudes of the vectors U and V are the minimal ones for which |
289 | | * the image of the set Y=[0...1],U=[-0.5...0.5],V=[-0.5...0.5] under the |
290 | | * conversion function will cover the set R=[0...1],G=[0...1],B=[0...1] |
291 | | * (the resulting matrix can be converted for other input/output ranges |
292 | | * outside this function). |
293 | | * Under these conditions the given parameters lr, lg, lb uniquely |
294 | | * determine the mapping of Y, U, V to R, G, B. |
295 | | */ |
296 | | static void luma_coeffs(struct pl_transform3x3 *mat, float lr, float lg, float lb) |
297 | 0 | { |
298 | 0 | mp_assert(fabs(lr+lg+lb - 1) < 1e-6); |
299 | 0 | *mat = (struct pl_transform3x3) { |
300 | 0 | { {{1, 0, 2 * (1-lr) }, |
301 | 0 | {1, -2 * (1-lb) * lb/lg, -2 * (1-lr) * lr/lg }, |
302 | 0 | {1, 2 * (1-lb), 0 }} }, |
303 | | // Constant coefficients (mat->c) not set here |
304 | 0 | }; |
305 | 0 | } |
306 | | |
307 | | // get the coefficients of the yuv -> rgb conversion matrix |
308 | | void mp_get_csp_matrix(struct mp_csp_params *params, struct pl_transform3x3 *m) |
309 | 0 | { |
310 | 0 | enum pl_color_system colorspace = params->repr.sys; |
311 | 0 | if (colorspace <= PL_COLOR_SYSTEM_UNKNOWN || colorspace >= PL_COLOR_SYSTEM_COUNT) |
312 | 0 | colorspace = PL_COLOR_SYSTEM_BT_601; |
313 | | // Not supported. TODO: replace with pl_color_repr_decode |
314 | 0 | if (colorspace == PL_COLOR_SYSTEM_BT_2100_PQ || |
315 | 0 | colorspace == PL_COLOR_SYSTEM_BT_2100_HLG || |
316 | 0 | colorspace == PL_COLOR_SYSTEM_DOLBYVISION) { |
317 | 0 | colorspace = PL_COLOR_SYSTEM_BT_2020_NC; |
318 | 0 | } |
319 | 0 | enum pl_color_levels levels_in = params->repr.levels; |
320 | 0 | if (levels_in <= PL_COLOR_LEVELS_UNKNOWN || levels_in >= PL_COLOR_LEVELS_COUNT) |
321 | 0 | levels_in = PL_COLOR_LEVELS_LIMITED; |
322 | |
|
323 | 0 | switch (colorspace) { |
324 | 0 | case PL_COLOR_SYSTEM_BT_601: luma_coeffs(m, 0.299, 0.587, 0.114 ); break; |
325 | 0 | case PL_COLOR_SYSTEM_BT_709: luma_coeffs(m, 0.2126, 0.7152, 0.0722); break; |
326 | 0 | case PL_COLOR_SYSTEM_SMPTE_240M: luma_coeffs(m, 0.212, 0.701, 0.087 ); break; |
327 | 0 | case PL_COLOR_SYSTEM_BT_2020_NC: luma_coeffs(m, 0.2627, 0.6780, 0.0593); break; |
328 | 0 | case PL_COLOR_SYSTEM_BT_2020_C: { |
329 | | // Note: This outputs into the [-0.5,0.5] range for chroma information. |
330 | | // If this clips on any VO, a constant 0.5 coefficient can be added |
331 | | // to the chroma channels to normalize them into [0,1]. This is not |
332 | | // currently needed by anything, though. |
333 | 0 | *m = (struct pl_transform3x3){{{{0, 0, 1}, {1, 0, 0}, {0, 1, 0}}}}; |
334 | 0 | break; |
335 | 0 | } |
336 | 0 | case PL_COLOR_SYSTEM_RGB: { |
337 | 0 | *m = (struct pl_transform3x3){{{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}}}; |
338 | 0 | levels_in = -1; |
339 | 0 | break; |
340 | 0 | } |
341 | 0 | case PL_COLOR_SYSTEM_XYZ: { |
342 | | // XYZ is decoded into the tagged RGB primaries, DCI-P3 for DCDM X'Y'Z' |
343 | 0 | enum pl_color_primaries prim = params->color.primaries; |
344 | 0 | if (!prim) |
345 | 0 | prim = PL_COLOR_PRIM_DCI_P3; |
346 | 0 | pl_matrix3x3 mat = pl_get_xyz2rgb_matrix(pl_raw_primaries_get(prim)); |
347 | 0 | *m = (struct pl_transform3x3) { .mat = mat }; |
348 | 0 | levels_in = -1; |
349 | 0 | break; |
350 | 0 | } |
351 | 0 | case PL_COLOR_SYSTEM_YCGCO: { |
352 | 0 | *m = (struct pl_transform3x3) { |
353 | 0 | {{{1, -1, 1}, |
354 | 0 | {1, 1, 0}, |
355 | 0 | {1, -1, -1}}}, |
356 | 0 | }; |
357 | 0 | break; |
358 | 0 | } |
359 | 0 | default: |
360 | 0 | MP_ASSERT_UNREACHABLE(); |
361 | 0 | }; |
362 | |
|
363 | 0 | if (params->is_float) |
364 | 0 | levels_in = -1; |
365 | |
|
366 | 0 | if ((colorspace == PL_COLOR_SYSTEM_BT_601 || colorspace == PL_COLOR_SYSTEM_BT_709 || |
367 | 0 | colorspace == PL_COLOR_SYSTEM_SMPTE_240M || colorspace == PL_COLOR_SYSTEM_BT_2020_NC)) |
368 | 0 | { |
369 | | // Hue is equivalent to rotating input [U, V] subvector around the origin. |
370 | | // Saturation scales [U, V]. |
371 | 0 | float huecos = params->gray ? 0 : params->saturation * cos(params->hue); |
372 | 0 | float huesin = params->gray ? 0 : params->saturation * sin(params->hue); |
373 | 0 | for (int i = 0; i < 3; i++) { |
374 | 0 | float u = m->mat.m[i][1], v = m->mat.m[i][2]; |
375 | 0 | m->mat.m[i][1] = huecos * u - huesin * v; |
376 | 0 | m->mat.m[i][2] = huesin * u + huecos * v; |
377 | 0 | } |
378 | 0 | } |
379 | | |
380 | | // The values below are written in 0-255 scale - thus bring s into range. |
381 | 0 | double s = |
382 | 0 | mp_get_csp_mul(colorspace, params->input_bits, params->texture_bits) / 255; |
383 | | // NOTE: The yuvfull ranges as presented here are arguably ambiguous, |
384 | | // and conflict with at least the full-range YCbCr/ICtCp values as defined |
385 | | // by ITU-R BT.2100. If somebody ever complains about full-range YUV looking |
386 | | // different from their reference display, this comment is probably why. |
387 | 0 | struct yuvlevels { double ymin, ymax, cmax, cmid; } |
388 | 0 | yuvlim = { 16*s, 235*s, 240*s, 128*s }, |
389 | 0 | yuvfull = { 0*s, 255*s, 255*s, 128*s }, |
390 | 0 | anyfull = { 0*s, 255*s, 255*s/2, 0 }, // cmax picked to make cmul=ymul |
391 | 0 | yuvlev; |
392 | 0 | switch (levels_in) { |
393 | 0 | case PL_COLOR_LEVELS_LIMITED: yuvlev = yuvlim; break; |
394 | 0 | case PL_COLOR_LEVELS_FULL: yuvlev = yuvfull; break; |
395 | 0 | case -1: yuvlev = anyfull; break; |
396 | 0 | default: |
397 | 0 | MP_ASSERT_UNREACHABLE(); |
398 | 0 | } |
399 | | |
400 | 0 | int levels_out = params->levels_out; |
401 | 0 | if (levels_out <= PL_COLOR_LEVELS_UNKNOWN || levels_out >= PL_COLOR_LEVELS_COUNT) |
402 | 0 | levels_out = PL_COLOR_LEVELS_FULL; |
403 | 0 | struct rgblevels { double min, max; } |
404 | 0 | rgblim = { 16/255., 235/255. }, |
405 | 0 | rgbfull = { 0, 1 }, |
406 | 0 | rgblev; |
407 | 0 | switch (levels_out) { |
408 | 0 | case PL_COLOR_LEVELS_LIMITED: rgblev = rgblim; break; |
409 | 0 | case PL_COLOR_LEVELS_FULL: rgblev = rgbfull; break; |
410 | 0 | default: |
411 | 0 | MP_ASSERT_UNREACHABLE(); |
412 | 0 | } |
413 | | |
414 | 0 | double ymul = (rgblev.max - rgblev.min) / (yuvlev.ymax - yuvlev.ymin); |
415 | 0 | double cmul = (rgblev.max - rgblev.min) / (yuvlev.cmax - yuvlev.cmid) / 2; |
416 | | |
417 | | // Contrast scales the output value range (gain) |
418 | 0 | ymul *= params->contrast; |
419 | 0 | cmul *= params->contrast; |
420 | |
|
421 | 0 | for (int i = 0; i < 3; i++) { |
422 | 0 | m->mat.m[i][0] *= ymul; |
423 | 0 | m->mat.m[i][1] *= cmul; |
424 | 0 | m->mat.m[i][2] *= cmul; |
425 | | // Set c so that Y=umin,UV=cmid maps to RGB=min (black to black), |
426 | | // also add brightness offset (black lift) |
427 | 0 | m->c[i] = rgblev.min - m->mat.m[i][0] * yuvlev.ymin |
428 | 0 | - (m->mat.m[i][1] + m->mat.m[i][2]) * yuvlev.cmid |
429 | 0 | + params->brightness; |
430 | 0 | } |
431 | 0 | } |
432 | | |
433 | | // Set colorspace related fields in p from f. Don't touch other fields. |
434 | | void mp_csp_set_image_params(struct mp_csp_params *params, |
435 | | const struct mp_image_params *imgparams) |
436 | 0 | { |
437 | 0 | struct mp_image_params p = *imgparams; |
438 | 0 | mp_image_params_guess_csp(&p); // ensure consistency |
439 | 0 | params->repr = p.repr; |
440 | 0 | params->color = p.color; |
441 | 0 | } |
442 | | |
443 | | enum mp_csp_equalizer_param { |
444 | | MP_CSP_EQ_BRIGHTNESS, |
445 | | MP_CSP_EQ_CONTRAST, |
446 | | MP_CSP_EQ_HUE, |
447 | | MP_CSP_EQ_SATURATION, |
448 | | MP_CSP_EQ_GAMMA, |
449 | | MP_CSP_EQ_COUNT, |
450 | | }; |
451 | | |
452 | | // Default initialization with 0 is enough, except for the capabilities field |
453 | | struct mp_csp_equalizer_opts { |
454 | | // Value for each property is in the range [-100.0, 100.0]. |
455 | | // 0.0 is default, meaning neutral or no change. |
456 | | float values[MP_CSP_EQ_COUNT]; |
457 | | int output_levels; |
458 | | }; |
459 | | |
460 | | #define OPT_BASE_STRUCT struct mp_csp_equalizer_opts |
461 | | |
462 | | const struct m_sub_options mp_csp_equalizer_conf = { |
463 | | .opts = (const m_option_t[]) { |
464 | | {"brightness", OPT_FLOAT(values[MP_CSP_EQ_BRIGHTNESS]), |
465 | | M_RANGE(-100, 100)}, |
466 | | {"saturation", OPT_FLOAT(values[MP_CSP_EQ_SATURATION]), |
467 | | M_RANGE(-100, 100)}, |
468 | | {"contrast", OPT_FLOAT(values[MP_CSP_EQ_CONTRAST]), |
469 | | M_RANGE(-100, 100)}, |
470 | | {"hue", OPT_FLOAT(values[MP_CSP_EQ_HUE]), |
471 | | M_RANGE(-100, 100)}, |
472 | | {"gamma", OPT_FLOAT(values[MP_CSP_EQ_GAMMA]), |
473 | | M_RANGE(-100, 100)}, |
474 | | {"video-output-levels", |
475 | | OPT_CHOICE_C(output_levels, pl_csp_levels_names)}, |
476 | | {0} |
477 | | }, |
478 | | .size = sizeof(struct mp_csp_equalizer_opts), |
479 | | .change_flags = UPDATE_VIDEO, |
480 | | }; |
481 | | |
482 | | // Copy settings from eq into params. |
483 | | static void mp_csp_copy_equalizer_values(struct mp_csp_params *params, |
484 | | const struct mp_csp_equalizer_opts *eq) |
485 | 0 | { |
486 | 0 | params->brightness = eq->values[MP_CSP_EQ_BRIGHTNESS] / 100.0; |
487 | 0 | params->contrast = (eq->values[MP_CSP_EQ_CONTRAST] + 100) / 100.0; |
488 | 0 | params->hue = eq->values[MP_CSP_EQ_HUE] / 100.0 * M_PI; |
489 | 0 | params->saturation = (eq->values[MP_CSP_EQ_SATURATION] + 100) / 100.0; |
490 | 0 | params->gamma = exp(log(8.0) * eq->values[MP_CSP_EQ_GAMMA] / 100.0); |
491 | 0 | params->levels_out = eq->output_levels; |
492 | 0 | } |
493 | | |
494 | | struct mp_csp_equalizer_state *mp_csp_equalizer_create(void *ta_parent, |
495 | | struct mpv_global *global) |
496 | 0 | { |
497 | 0 | struct m_config_cache *c = m_config_cache_alloc(ta_parent, global, |
498 | 0 | &mp_csp_equalizer_conf); |
499 | | // The terrible, terrible truth. |
500 | 0 | return (struct mp_csp_equalizer_state *)c; |
501 | 0 | } |
502 | | |
503 | | bool mp_csp_equalizer_state_changed(struct mp_csp_equalizer_state *state) |
504 | 0 | { |
505 | 0 | struct m_config_cache *c = (struct m_config_cache *)state; |
506 | 0 | return m_config_cache_update(c); |
507 | 0 | } |
508 | | |
509 | | void mp_csp_equalizer_state_get(struct mp_csp_equalizer_state *state, |
510 | | struct mp_csp_params *params) |
511 | 0 | { |
512 | 0 | struct m_config_cache *c = (struct m_config_cache *)state; |
513 | 0 | m_config_cache_update(c); |
514 | 0 | struct mp_csp_equalizer_opts *opts = c->opts; |
515 | 0 | mp_csp_copy_equalizer_values(params, opts); |
516 | 0 | } |
517 | | |
518 | | // Multiply the color in c with the given matrix. |
519 | | // i/o is {R, G, B} or {Y, U, V} (depending on input/output and matrix), using |
520 | | // a fixed point representation with the given number of bits (so for bits==8, |
521 | | // [0,255] maps to [0,1]). The output is clipped to the range as needed. |
522 | | void mp_map_fixp_color(struct pl_transform3x3 *matrix, int ibits, int in[3], |
523 | | int obits, int out[3]) |
524 | 0 | { |
525 | 0 | for (int i = 0; i < 3; i++) { |
526 | 0 | double val = matrix->c[i]; |
527 | 0 | for (int x = 0; x < 3; x++) |
528 | 0 | val += matrix->mat.m[i][x] * in[x] / ((1 << ibits) - 1); |
529 | 0 | int ival = lrint(val * ((1 << obits) - 1)); |
530 | 0 | out[i] = av_clip(ival, 0, (1 << obits) - 1); |
531 | 0 | } |
532 | 0 | } |
533 | | |
534 | | enum pl_color_primaries mp_get_best_prim_container(const struct pl_raw_primaries *gamut) |
535 | 0 | { |
536 | 0 | enum pl_color_primaries container = PL_COLOR_PRIM_UNKNOWN; |
537 | |
|
538 | 0 | if (!pl_primaries_valid(gamut)) |
539 | 0 | return container; |
540 | | |
541 | 0 | const struct pl_raw_primaries *best = NULL; |
542 | 0 | for (enum pl_color_primaries prim = 1; prim < PL_COLOR_PRIM_COUNT; prim++) { |
543 | 0 | const struct pl_raw_primaries *raw = pl_raw_primaries_get(prim); |
544 | 0 | if (pl_raw_primaries_similar(raw, gamut)) { |
545 | 0 | container = prim; |
546 | 0 | best = raw; |
547 | 0 | break; |
548 | 0 | } |
549 | | |
550 | 0 | if (pl_primaries_superset(raw, gamut) && |
551 | 0 | (!best || pl_primaries_superset(best, raw))) |
552 | 0 | { |
553 | 0 | container = prim; |
554 | 0 | best = raw; |
555 | 0 | } |
556 | 0 | } |
557 | |
|
558 | 0 | if (!best) |
559 | 0 | container = PL_COLOR_PRIM_BT_2020; |
560 | |
|
561 | 0 | return container; |
562 | 0 | } |
563 | | |
564 | | int mp_parse_raw_primaries(struct mp_log *log, const char *str, |
565 | | struct pl_raw_primaries *out) |
566 | 1.00k | { |
567 | 1.00k | if (!str) |
568 | 36 | return M_OPT_INVALID; |
569 | | |
570 | 973 | if (!*str) |
571 | 37 | return M_OPT_MISSING_PARAM; |
572 | | |
573 | | // Comma-separated CIE xy values: Rx,Ry,Gx,Gy,Bx,By,Wx,Wy |
574 | 936 | struct pl_raw_primaries prim; |
575 | 936 | if (sscanf(str, "%f,%f,%f,%f,%f,%f,%f,%f", |
576 | 936 | &prim.red.x, &prim.red.y, &prim.green.x, &prim.green.y, |
577 | 936 | &prim.blue.x, &prim.blue.y, &prim.white.x, &prim.white.y) == 8) |
578 | 517 | { |
579 | 517 | if (!pl_primaries_valid(&prim)) |
580 | 125 | return M_OPT_OUT_OF_RANGE; |
581 | 392 | *out = prim; |
582 | 392 | return 1; |
583 | 517 | } |
584 | | |
585 | 419 | const m_option_t choice_opt = { |
586 | 419 | .priv = (void *)pl_csp_prim_names, |
587 | 419 | }; |
588 | | |
589 | 419 | int prim_choice; |
590 | 419 | int ret = m_option_type_choice.parse(log, &choice_opt, bstr0("target-gamut"), |
591 | 419 | bstr0(str), &prim_choice); |
592 | 419 | if (ret >= 0) { |
593 | 330 | if (!prim_choice) |
594 | 264 | return 0; |
595 | 66 | *out = *pl_raw_primaries_get(prim_choice); |
596 | 89 | } else if (ret != M_OPT_MISSING_PARAM) { |
597 | 89 | mp_info(log, " Rx,Ry,Gx,Gy,Bx,By,Wx,Wy (custom CIE xy primaries)\n"); |
598 | 89 | } |
599 | | |
600 | 155 | return ret; |
601 | 419 | } |