1 /*
2  * Common code related to colorspaces and conversion
3  *
4  * Copyleft (C) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
5  *
6  * mp_invert_cmat based on DarkPlaces engine (relicensed from GPL to LGPL)
7  *
8  * This file is part of mpv.
9  *
10  * mpv is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * mpv is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "config.h"
25 
26 #include <stdint.h>
27 #include <math.h>
28 #include <assert.h>
29 #include <libavutil/common.h>
30 #include <libavcodec/avcodec.h>
31 
32 #include "mp_image.h"
33 #include "csputils.h"
34 #include "options/m_config.h"
35 #include "options/m_option.h"
36 
37 const struct m_opt_choice_alternatives mp_csp_names[] = {
38     {"auto",        MP_CSP_AUTO},
39     {"bt.601",      MP_CSP_BT_601},
40     {"bt.709",      MP_CSP_BT_709},
41     {"smpte-240m",  MP_CSP_SMPTE_240M},
42     {"bt.2020-ncl", MP_CSP_BT_2020_NC},
43     {"bt.2020-cl",  MP_CSP_BT_2020_C},
44     {"rgb",         MP_CSP_RGB},
45     {"xyz",         MP_CSP_XYZ},
46     {"ycgco",       MP_CSP_YCGCO},
47     {0}
48 };
49 
50 const struct m_opt_choice_alternatives mp_csp_levels_names[] = {
51     {"auto",        MP_CSP_LEVELS_AUTO},
52     {"limited",     MP_CSP_LEVELS_TV},
53     {"full",        MP_CSP_LEVELS_PC},
54     {0}
55 };
56 
57 const struct m_opt_choice_alternatives mp_csp_prim_names[] = {
58     {"auto",        MP_CSP_PRIM_AUTO},
59     {"bt.601-525",  MP_CSP_PRIM_BT_601_525},
60     {"bt.601-625",  MP_CSP_PRIM_BT_601_625},
61     {"bt.709",      MP_CSP_PRIM_BT_709},
62     {"bt.2020",     MP_CSP_PRIM_BT_2020},
63     {"bt.470m",     MP_CSP_PRIM_BT_470M},
64     {"apple",       MP_CSP_PRIM_APPLE},
65     {"adobe",       MP_CSP_PRIM_ADOBE},
66     {"prophoto",    MP_CSP_PRIM_PRO_PHOTO},
67     {"cie1931",     MP_CSP_PRIM_CIE_1931},
68     {"dci-p3",      MP_CSP_PRIM_DCI_P3},
69     {"display-p3",  MP_CSP_PRIM_DISPLAY_P3},
70     {"v-gamut",     MP_CSP_PRIM_V_GAMUT},
71     {"s-gamut",     MP_CSP_PRIM_S_GAMUT},
72     {0}
73 };
74 
75 const struct m_opt_choice_alternatives mp_csp_trc_names[] = {
76     {"auto",        MP_CSP_TRC_AUTO},
77     {"bt.1886",     MP_CSP_TRC_BT_1886},
78     {"srgb",        MP_CSP_TRC_SRGB},
79     {"linear",      MP_CSP_TRC_LINEAR},
80     {"gamma1.8",    MP_CSP_TRC_GAMMA18},
81     {"gamma2.0",    MP_CSP_TRC_GAMMA20},
82     {"gamma2.2",    MP_CSP_TRC_GAMMA22},
83     {"gamma2.4",    MP_CSP_TRC_GAMMA24},
84     {"gamma2.6",    MP_CSP_TRC_GAMMA26},
85     {"gamma2.8",    MP_CSP_TRC_GAMMA28},
86     {"prophoto",    MP_CSP_TRC_PRO_PHOTO},
87     {"pq",          MP_CSP_TRC_PQ},
88     {"hlg",         MP_CSP_TRC_HLG},
89     {"v-log",       MP_CSP_TRC_V_LOG},
90     {"s-log1",      MP_CSP_TRC_S_LOG1},
91     {"s-log2",      MP_CSP_TRC_S_LOG2},
92     {0}
93 };
94 
95 const struct m_opt_choice_alternatives mp_csp_light_names[] = {
96     {"auto",        MP_CSP_LIGHT_AUTO},
97     {"display",     MP_CSP_LIGHT_DISPLAY},
98     {"hlg",         MP_CSP_LIGHT_SCENE_HLG},
99     {"709-1886",    MP_CSP_LIGHT_SCENE_709_1886},
100     {"gamma1.2",    MP_CSP_LIGHT_SCENE_1_2},
101     {0}
102 };
103 
104 const struct m_opt_choice_alternatives mp_chroma_names[] = {
105     {"unknown",     MP_CHROMA_AUTO},
106     {"uhd",         MP_CHROMA_TOPLEFT},
107     {"mpeg2/4/h264",MP_CHROMA_LEFT},
108     {"mpeg1/jpeg",  MP_CHROMA_CENTER},
109     {0}
110 };
111 
112 const struct m_opt_choice_alternatives mp_alpha_names[] = {
113     {"auto",        MP_ALPHA_AUTO},
114     {"straight",    MP_ALPHA_STRAIGHT},
115     {"premul",      MP_ALPHA_PREMUL},
116     {0}
117 };
118 
mp_colorspace_merge(struct mp_colorspace * orig,struct mp_colorspace * new)119 void mp_colorspace_merge(struct mp_colorspace *orig, struct mp_colorspace *new)
120 {
121     if (!orig->space)
122         orig->space = new->space;
123     if (!orig->levels)
124         orig->levels = new->levels;
125     if (!orig->primaries)
126         orig->primaries = new->primaries;
127     if (!orig->gamma)
128         orig->gamma = new->gamma;
129     if (!orig->sig_peak)
130         orig->sig_peak = new->sig_peak;
131     if (!orig->light)
132         orig->light = new->light;
133 }
134 
135 // The short name _must_ match with what vf_stereo3d accepts (if supported).
136 // The long name in comments is closer to the Matroska spec (StereoMode element).
137 // The numeric index matches the Matroska StereoMode value. If you add entries
138 // that don't match Matroska, make sure demux_mkv.c rejects them properly.
139 const struct m_opt_choice_alternatives mp_stereo3d_names[] = {
140     {"no",     -1}, // disable/invalid
141     {"mono",    0},
142     {"sbs2l",   1}, // "side_by_side_left"
143     {"ab2r",    2}, // "top_bottom_right"
144     {"ab2l",    3}, // "top_bottom_left"
145     {"checkr",  4}, // "checkboard_right" (unsupported by vf_stereo3d)
146     {"checkl",  5}, // "checkboard_left"  (unsupported by vf_stereo3d)
147     {"irr",     6}, // "row_interleaved_right"
148     {"irl",     7}, // "row_interleaved_left"
149     {"icr",     8}, // "column_interleaved_right" (unsupported by vf_stereo3d)
150     {"icl",     9}, // "column_interleaved_left" (unsupported by vf_stereo3d)
151     {"arcc",   10}, // "anaglyph_cyan_red" (Matroska: unclear which mode)
152     {"sbs2r",  11}, // "side_by_side_right"
153     {"agmc",   12}, // "anaglyph_green_magenta" (Matroska: unclear which mode)
154     {"al",     13}, // "alternating frames left first"
155     {"ar",     14}, // "alternating frames right first"
156     {0}
157 };
158 
avcol_spc_to_mp_csp(int avcolorspace)159 enum mp_csp avcol_spc_to_mp_csp(int avcolorspace)
160 {
161     switch (avcolorspace) {
162     case AVCOL_SPC_BT709:       return MP_CSP_BT_709;
163     case AVCOL_SPC_BT470BG:     return MP_CSP_BT_601;
164     case AVCOL_SPC_BT2020_NCL:  return MP_CSP_BT_2020_NC;
165     case AVCOL_SPC_BT2020_CL:   return MP_CSP_BT_2020_C;
166     case AVCOL_SPC_SMPTE170M:   return MP_CSP_BT_601;
167     case AVCOL_SPC_SMPTE240M:   return MP_CSP_SMPTE_240M;
168     case AVCOL_SPC_RGB:         return MP_CSP_RGB;
169     case AVCOL_SPC_YCOCG:       return MP_CSP_YCGCO;
170     default:                    return MP_CSP_AUTO;
171     }
172 }
173 
avcol_range_to_mp_csp_levels(int avrange)174 enum mp_csp_levels avcol_range_to_mp_csp_levels(int avrange)
175 {
176     switch (avrange) {
177     case AVCOL_RANGE_MPEG:      return MP_CSP_LEVELS_TV;
178     case AVCOL_RANGE_JPEG:      return MP_CSP_LEVELS_PC;
179     default:                    return MP_CSP_LEVELS_AUTO;
180     }
181 }
182 
avcol_pri_to_mp_csp_prim(int avpri)183 enum mp_csp_prim avcol_pri_to_mp_csp_prim(int avpri)
184 {
185     switch (avpri) {
186     case AVCOL_PRI_SMPTE240M:   // Same as below
187     case AVCOL_PRI_SMPTE170M:   return MP_CSP_PRIM_BT_601_525;
188     case AVCOL_PRI_BT470BG:     return MP_CSP_PRIM_BT_601_625;
189     case AVCOL_PRI_BT709:       return MP_CSP_PRIM_BT_709;
190     case AVCOL_PRI_BT2020:      return MP_CSP_PRIM_BT_2020;
191     case AVCOL_PRI_BT470M:      return MP_CSP_PRIM_BT_470M;
192     case AVCOL_PRI_SMPTE431:    return MP_CSP_PRIM_DCI_P3;
193     case AVCOL_PRI_SMPTE432:    return MP_CSP_PRIM_DISPLAY_P3;
194     default:                    return MP_CSP_PRIM_AUTO;
195     }
196 }
197 
avcol_trc_to_mp_csp_trc(int avtrc)198 enum mp_csp_trc avcol_trc_to_mp_csp_trc(int avtrc)
199 {
200     switch (avtrc) {
201     case AVCOL_TRC_BT709:
202     case AVCOL_TRC_SMPTE170M:
203     case AVCOL_TRC_SMPTE240M:
204     case AVCOL_TRC_BT1361_ECG:
205     case AVCOL_TRC_BT2020_10:
206     case AVCOL_TRC_BT2020_12:    return MP_CSP_TRC_BT_1886;
207     case AVCOL_TRC_IEC61966_2_1: return MP_CSP_TRC_SRGB;
208     case AVCOL_TRC_LINEAR:       return MP_CSP_TRC_LINEAR;
209     case AVCOL_TRC_GAMMA22:      return MP_CSP_TRC_GAMMA22;
210     case AVCOL_TRC_GAMMA28:      return MP_CSP_TRC_GAMMA28;
211     case AVCOL_TRC_SMPTEST2084:  return MP_CSP_TRC_PQ;
212     case AVCOL_TRC_ARIB_STD_B67: return MP_CSP_TRC_HLG;
213     default:                     return MP_CSP_TRC_AUTO;
214     }
215 }
216 
mp_csp_to_avcol_spc(enum mp_csp colorspace)217 int mp_csp_to_avcol_spc(enum mp_csp colorspace)
218 {
219     switch (colorspace) {
220     case MP_CSP_BT_709:         return AVCOL_SPC_BT709;
221     case MP_CSP_BT_601:         return AVCOL_SPC_BT470BG;
222     case MP_CSP_BT_2020_NC:     return AVCOL_SPC_BT2020_NCL;
223     case MP_CSP_BT_2020_C:      return AVCOL_SPC_BT2020_CL;
224     case MP_CSP_SMPTE_240M:     return AVCOL_SPC_SMPTE240M;
225     case MP_CSP_RGB:            return AVCOL_SPC_RGB;
226     case MP_CSP_YCGCO:          return AVCOL_SPC_YCOCG;
227     default:                    return AVCOL_SPC_UNSPECIFIED;
228     }
229 }
230 
mp_csp_levels_to_avcol_range(enum mp_csp_levels range)231 int mp_csp_levels_to_avcol_range(enum mp_csp_levels range)
232 {
233     switch (range) {
234     case MP_CSP_LEVELS_TV:      return AVCOL_RANGE_MPEG;
235     case MP_CSP_LEVELS_PC:      return AVCOL_RANGE_JPEG;
236     default:                    return AVCOL_RANGE_UNSPECIFIED;
237     }
238 }
239 
mp_csp_prim_to_avcol_pri(enum mp_csp_prim prim)240 int mp_csp_prim_to_avcol_pri(enum mp_csp_prim prim)
241 {
242     switch (prim) {
243     case MP_CSP_PRIM_BT_601_525: return AVCOL_PRI_SMPTE170M;
244     case MP_CSP_PRIM_BT_601_625: return AVCOL_PRI_BT470BG;
245     case MP_CSP_PRIM_BT_709:     return AVCOL_PRI_BT709;
246     case MP_CSP_PRIM_BT_2020:    return AVCOL_PRI_BT2020;
247     case MP_CSP_PRIM_BT_470M:    return AVCOL_PRI_BT470M;
248     case MP_CSP_PRIM_DCI_P3:     return AVCOL_PRI_SMPTE431;
249     case MP_CSP_PRIM_DISPLAY_P3: return AVCOL_PRI_SMPTE432;
250     default:                     return AVCOL_PRI_UNSPECIFIED;
251     }
252 }
253 
mp_csp_trc_to_avcol_trc(enum mp_csp_trc trc)254 int mp_csp_trc_to_avcol_trc(enum mp_csp_trc trc)
255 {
256     switch (trc) {
257     // We just call it BT.1886 since we're decoding, but it's still BT.709
258     case MP_CSP_TRC_BT_1886:      return AVCOL_TRC_BT709;
259     case MP_CSP_TRC_SRGB:         return AVCOL_TRC_IEC61966_2_1;
260     case MP_CSP_TRC_LINEAR:       return AVCOL_TRC_LINEAR;
261     case MP_CSP_TRC_GAMMA22:      return AVCOL_TRC_GAMMA22;
262     case MP_CSP_TRC_GAMMA28:      return AVCOL_TRC_GAMMA28;
263     case MP_CSP_TRC_PQ:           return AVCOL_TRC_SMPTEST2084;
264     case MP_CSP_TRC_HLG:          return AVCOL_TRC_ARIB_STD_B67;
265     default:                      return AVCOL_TRC_UNSPECIFIED;
266     }
267 }
268 
mp_csp_guess_colorspace(int width,int height)269 enum mp_csp mp_csp_guess_colorspace(int width, int height)
270 {
271     return width >= 1280 || height > 576 ? MP_CSP_BT_709 : MP_CSP_BT_601;
272 }
273 
mp_csp_guess_primaries(int width,int height)274 enum mp_csp_prim mp_csp_guess_primaries(int width, int height)
275 {
276     // HD content
277     if (width >= 1280 || height > 576)
278         return MP_CSP_PRIM_BT_709;
279 
280     switch (height) {
281     case 576: // Typical PAL content, including anamorphic/squared
282         return MP_CSP_PRIM_BT_601_625;
283 
284     case 480: // Typical NTSC content, including squared
285     case 486: // NTSC Pro or anamorphic NTSC
286         return MP_CSP_PRIM_BT_601_525;
287 
288     default: // No good metric, just pick BT.709 to minimize damage
289         return MP_CSP_PRIM_BT_709;
290     }
291 }
292 
avchroma_location_to_mp(int avloc)293 enum mp_chroma_location avchroma_location_to_mp(int avloc)
294 {
295     switch (avloc) {
296     case AVCHROMA_LOC_TOPLEFT:          return MP_CHROMA_TOPLEFT;
297     case AVCHROMA_LOC_LEFT:             return MP_CHROMA_LEFT;
298     case AVCHROMA_LOC_CENTER:           return MP_CHROMA_CENTER;
299     default:                            return MP_CHROMA_AUTO;
300     }
301 }
302 
mp_chroma_location_to_av(enum mp_chroma_location mploc)303 int mp_chroma_location_to_av(enum mp_chroma_location mploc)
304 {
305     switch (mploc) {
306     case MP_CHROMA_TOPLEFT:             return AVCHROMA_LOC_TOPLEFT;
307     case MP_CHROMA_LEFT:                return AVCHROMA_LOC_LEFT;
308     case MP_CHROMA_CENTER:              return AVCHROMA_LOC_CENTER;
309     default:                            return AVCHROMA_LOC_UNSPECIFIED;
310     }
311 }
312 
313 // Return location of chroma samples relative to luma samples. 0/0 means
314 // centered. Other possible values are -1 (top/left) and +1 (right/bottom).
mp_get_chroma_location(enum mp_chroma_location loc,int * x,int * y)315 void mp_get_chroma_location(enum mp_chroma_location loc, int *x, int *y)
316 {
317     *x = 0;
318     *y = 0;
319     if (loc == MP_CHROMA_LEFT || loc == MP_CHROMA_TOPLEFT)
320         *x = -1;
321     if (loc == MP_CHROMA_TOPLEFT)
322         *y = -1;
323 }
324 
mp_invert_matrix3x3(float m[3][3])325 void mp_invert_matrix3x3(float m[3][3])
326 {
327     float m00 = m[0][0], m01 = m[0][1], m02 = m[0][2],
328           m10 = m[1][0], m11 = m[1][1], m12 = m[1][2],
329           m20 = m[2][0], m21 = m[2][1], m22 = m[2][2];
330 
331     // calculate the adjoint
332     m[0][0] =  (m11 * m22 - m21 * m12);
333     m[0][1] = -(m01 * m22 - m21 * m02);
334     m[0][2] =  (m01 * m12 - m11 * m02);
335     m[1][0] = -(m10 * m22 - m20 * m12);
336     m[1][1] =  (m00 * m22 - m20 * m02);
337     m[1][2] = -(m00 * m12 - m10 * m02);
338     m[2][0] =  (m10 * m21 - m20 * m11);
339     m[2][1] = -(m00 * m21 - m20 * m01);
340     m[2][2] =  (m00 * m11 - m10 * m01);
341 
342     // calculate the determinant (as inverse == 1/det * adjoint,
343     // adjoint * m == identity * det, so this calculates the det)
344     float det = m00 * m[0][0] + m10 * m[0][1] + m20 * m[0][2];
345     det = 1.0f / det;
346 
347     for (int i = 0; i < 3; i++) {
348         for (int j = 0; j < 3; j++)
349             m[i][j] *= det;
350     }
351 }
352 
353 // A := A * B
mp_mul_matrix3x3(float a[3][3],float b[3][3])354 static void mp_mul_matrix3x3(float a[3][3], float b[3][3])
355 {
356     float a00 = a[0][0], a01 = a[0][1], a02 = a[0][2],
357           a10 = a[1][0], a11 = a[1][1], a12 = a[1][2],
358           a20 = a[2][0], a21 = a[2][1], a22 = a[2][2];
359 
360     for (int i = 0; i < 3; i++) {
361         a[0][i] = a00 * b[0][i] + a01 * b[1][i] + a02 * b[2][i];
362         a[1][i] = a10 * b[0][i] + a11 * b[1][i] + a12 * b[2][i];
363         a[2][i] = a20 * b[0][i] + a21 * b[1][i] + a22 * b[2][i];
364     }
365 }
366 
367 // return the primaries associated with a certain mp_csp_primaries val
mp_get_csp_primaries(enum mp_csp_prim spc)368 struct mp_csp_primaries mp_get_csp_primaries(enum mp_csp_prim spc)
369 {
370     /*
371     Values from: ITU-R Recommendations BT.470-6, BT.601-7, BT.709-5, BT.2020-0
372 
373     https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.470-6-199811-S!!PDF-E.pdf
374     https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.601-7-201103-I!!PDF-E.pdf
375     https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.709-5-200204-I!!PDF-E.pdf
376     https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.2020-0-201208-I!!PDF-E.pdf
377 
378     Other colorspaces from https://en.wikipedia.org/wiki/RGB_color_space#Specifications
379     */
380 
381     // CIE standard illuminant series
382     static const struct mp_csp_col_xy
383         d50 = {0.34577, 0.35850},
384         d65 = {0.31271, 0.32902},
385         c   = {0.31006, 0.31616},
386         dci = {0.31400, 0.35100},
387         e   = {1.0/3.0, 1.0/3.0};
388 
389     switch (spc) {
390     case MP_CSP_PRIM_BT_470M:
391         return (struct mp_csp_primaries) {
392             .red   = {0.670, 0.330},
393             .green = {0.210, 0.710},
394             .blue  = {0.140, 0.080},
395             .white = c
396         };
397     case MP_CSP_PRIM_BT_601_525:
398         return (struct mp_csp_primaries) {
399             .red   = {0.630, 0.340},
400             .green = {0.310, 0.595},
401             .blue  = {0.155, 0.070},
402             .white = d65
403         };
404     case MP_CSP_PRIM_BT_601_625:
405         return (struct mp_csp_primaries) {
406             .red   = {0.640, 0.330},
407             .green = {0.290, 0.600},
408             .blue  = {0.150, 0.060},
409             .white = d65
410         };
411     // This is the default assumption if no colorspace information could
412     // be determined, eg. for files which have no video channel.
413     case MP_CSP_PRIM_AUTO:
414     case MP_CSP_PRIM_BT_709:
415         return (struct mp_csp_primaries) {
416             .red   = {0.640, 0.330},
417             .green = {0.300, 0.600},
418             .blue  = {0.150, 0.060},
419             .white = d65
420         };
421     case MP_CSP_PRIM_BT_2020:
422         return (struct mp_csp_primaries) {
423             .red   = {0.708, 0.292},
424             .green = {0.170, 0.797},
425             .blue  = {0.131, 0.046},
426             .white = d65
427         };
428     case MP_CSP_PRIM_APPLE:
429         return (struct mp_csp_primaries) {
430             .red   = {0.625, 0.340},
431             .green = {0.280, 0.595},
432             .blue  = {0.115, 0.070},
433             .white = d65
434         };
435     case MP_CSP_PRIM_ADOBE:
436         return (struct mp_csp_primaries) {
437             .red   = {0.640, 0.330},
438             .green = {0.210, 0.710},
439             .blue  = {0.150, 0.060},
440             .white = d65
441         };
442     case MP_CSP_PRIM_PRO_PHOTO:
443         return (struct mp_csp_primaries) {
444             .red   = {0.7347, 0.2653},
445             .green = {0.1596, 0.8404},
446             .blue  = {0.0366, 0.0001},
447             .white = d50
448         };
449     case MP_CSP_PRIM_CIE_1931:
450         return (struct mp_csp_primaries) {
451             .red   = {0.7347, 0.2653},
452             .green = {0.2738, 0.7174},
453             .blue  = {0.1666, 0.0089},
454             .white = e
455         };
456     // From SMPTE RP 431-2 and 432-1
457     case MP_CSP_PRIM_DCI_P3:
458     case MP_CSP_PRIM_DISPLAY_P3:
459         return (struct mp_csp_primaries) {
460             .red   = {0.680, 0.320},
461             .green = {0.265, 0.690},
462             .blue  = {0.150, 0.060},
463             .white = spc == MP_CSP_PRIM_DCI_P3 ? dci : d65
464         };
465     // From Panasonic VARICAM reference manual
466     case MP_CSP_PRIM_V_GAMUT:
467         return (struct mp_csp_primaries) {
468             .red   = {0.730, 0.280},
469             .green = {0.165, 0.840},
470             .blue  = {0.100, -0.03},
471             .white = d65
472         };
473     // From Sony S-Log reference manual
474     case MP_CSP_PRIM_S_GAMUT:
475         return (struct mp_csp_primaries) {
476             .red   = {0.730, 0.280},
477             .green = {0.140, 0.855},
478             .blue  = {0.100, -0.05},
479             .white = d65
480         };
481     default:
482         return (struct mp_csp_primaries) {{0}};
483     }
484 }
485 
486 // Get the nominal peak for a given colorspace, relative to the reference white
487 // level. In other words, this returns the brightest encodable value that can
488 // be represented by a given transfer curve.
mp_trc_nom_peak(enum mp_csp_trc trc)489 float mp_trc_nom_peak(enum mp_csp_trc trc)
490 {
491     switch (trc) {
492     case MP_CSP_TRC_PQ:           return 10000.0 / MP_REF_WHITE;
493     case MP_CSP_TRC_HLG:          return 12.0 / MP_REF_WHITE_HLG;
494     case MP_CSP_TRC_V_LOG:        return 46.0855;
495     case MP_CSP_TRC_S_LOG1:       return 6.52;
496     case MP_CSP_TRC_S_LOG2:       return 9.212;
497     }
498 
499     return 1.0;
500 }
501 
mp_trc_is_hdr(enum mp_csp_trc trc)502 bool mp_trc_is_hdr(enum mp_csp_trc trc)
503 {
504     return mp_trc_nom_peak(trc) > 1.0;
505 }
506 
507 // Compute the RGB/XYZ matrix as described here:
508 // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
mp_get_rgb2xyz_matrix(struct mp_csp_primaries space,float m[3][3])509 void mp_get_rgb2xyz_matrix(struct mp_csp_primaries space, float m[3][3])
510 {
511     float S[3], X[4], Z[4];
512 
513     // Convert from CIE xyY to XYZ. Note that Y=1 holds true for all primaries
514     X[0] = space.red.x   / space.red.y;
515     X[1] = space.green.x / space.green.y;
516     X[2] = space.blue.x  / space.blue.y;
517     X[3] = space.white.x / space.white.y;
518 
519     Z[0] = (1 - space.red.x   - space.red.y)   / space.red.y;
520     Z[1] = (1 - space.green.x - space.green.y) / space.green.y;
521     Z[2] = (1 - space.blue.x  - space.blue.y)  / space.blue.y;
522     Z[3] = (1 - space.white.x - space.white.y) / space.white.y;
523 
524     // S = XYZ^-1 * W
525     for (int i = 0; i < 3; i++) {
526         m[0][i] = X[i];
527         m[1][i] = 1;
528         m[2][i] = Z[i];
529     }
530 
531     mp_invert_matrix3x3(m);
532 
533     for (int i = 0; i < 3; i++)
534         S[i] = m[i][0] * X[3] + m[i][1] * 1 + m[i][2] * Z[3];
535 
536     // M = [Sc * XYZc]
537     for (int i = 0; i < 3; i++) {
538         m[0][i] = S[i] * X[i];
539         m[1][i] = S[i] * 1;
540         m[2][i] = S[i] * Z[i];
541     }
542 }
543 
544 // M := M * XYZd<-XYZs
mp_apply_chromatic_adaptation(struct mp_csp_col_xy src,struct mp_csp_col_xy dest,float m[3][3])545 static void mp_apply_chromatic_adaptation(struct mp_csp_col_xy src,
546                                           struct mp_csp_col_xy dest, float m[3][3])
547 {
548     // If the white points are nearly identical, this is a wasteful identity
549     // operation.
550     if (fabs(src.x - dest.x) < 1e-6 && fabs(src.y - dest.y) < 1e-6)
551         return;
552 
553     // XYZd<-XYZs = Ma^-1 * (I*[Cd/Cs]) * Ma
554     // http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html
555     float C[3][2], tmp[3][3] = {{0}};
556 
557     // Ma = Bradford matrix, arguably most popular method in use today.
558     // This is derived experimentally and thus hard-coded.
559     float bradford[3][3] = {
560         {  0.8951,  0.2664, -0.1614 },
561         { -0.7502,  1.7135,  0.0367 },
562         {  0.0389, -0.0685,  1.0296 },
563     };
564 
565     for (int i = 0; i < 3; i++) {
566         // source cone
567         C[i][0] = bradford[i][0] * mp_xy_X(src)
568                 + bradford[i][1] * 1
569                 + bradford[i][2] * mp_xy_Z(src);
570 
571         // dest cone
572         C[i][1] = bradford[i][0] * mp_xy_X(dest)
573                 + bradford[i][1] * 1
574                 + bradford[i][2] * mp_xy_Z(dest);
575     }
576 
577     // tmp := I * [Cd/Cs] * Ma
578     for (int i = 0; i < 3; i++)
579         tmp[i][i] = C[i][1] / C[i][0];
580 
581     mp_mul_matrix3x3(tmp, bradford);
582 
583     // M := M * Ma^-1 * tmp
584     mp_invert_matrix3x3(bradford);
585     mp_mul_matrix3x3(m, bradford);
586     mp_mul_matrix3x3(m, tmp);
587 }
588 
589 // get the coefficients of the source -> dest cms matrix
mp_get_cms_matrix(struct mp_csp_primaries src,struct mp_csp_primaries dest,enum mp_render_intent intent,float m[3][3])590 void mp_get_cms_matrix(struct mp_csp_primaries src, struct mp_csp_primaries dest,
591                        enum mp_render_intent intent, float m[3][3])
592 {
593     float tmp[3][3];
594 
595     // In saturation mapping, we don't care about accuracy and just want
596     // primaries to map to primaries, making this an identity transformation.
597     if (intent == MP_INTENT_SATURATION) {
598         for (int i = 0; i < 3; i++)
599             m[i][i] = 1;
600         return;
601     }
602 
603     // RGBd<-RGBs = RGBd<-XYZd * XYZd<-XYZs * XYZs<-RGBs
604     // Equations from: http://www.brucelindbloom.com/index.html?Math.html
605     // Note: Perceptual is treated like relative colorimetric. There's no
606     // definition for perceptual other than "make it look good".
607 
608     // RGBd<-XYZd, inverted from XYZd<-RGBd
609     mp_get_rgb2xyz_matrix(dest, m);
610     mp_invert_matrix3x3(m);
611 
612     // Chromatic adaptation, except in absolute colorimetric intent
613     if (intent != MP_INTENT_ABSOLUTE_COLORIMETRIC)
614         mp_apply_chromatic_adaptation(src.white, dest.white, m);
615 
616     // XYZs<-RGBs
617     mp_get_rgb2xyz_matrix(src, tmp);
618     mp_mul_matrix3x3(m, tmp);
619 }
620 
621 // get the coefficients of an SMPTE 428-1 xyz -> rgb conversion matrix
622 // intent = the rendering intent used to convert to the target primaries
mp_get_xyz2rgb_coeffs(struct mp_csp_params * params,enum mp_render_intent intent,struct mp_cmat * m)623 static void mp_get_xyz2rgb_coeffs(struct mp_csp_params *params,
624                                   enum mp_render_intent intent, struct mp_cmat *m)
625 {
626     struct mp_csp_primaries prim = mp_get_csp_primaries(params->color.primaries);
627     float brightness = params->brightness;
628     mp_get_rgb2xyz_matrix(prim, m->m);
629     mp_invert_matrix3x3(m->m);
630 
631     // All non-absolute mappings want to map source white to target white
632     if (intent != MP_INTENT_ABSOLUTE_COLORIMETRIC) {
633         // SMPTE 428-1 defines the calibration white point as CIE xy (0.314, 0.351)
634         static const struct mp_csp_col_xy smpte428 = {0.314, 0.351};
635         mp_apply_chromatic_adaptation(smpte428, prim.white, m->m);
636     }
637 
638     // Since this outputs linear RGB rather than companded RGB, we
639     // want to linearize any brightness additions. 2 is a reasonable
640     // approximation for any sort of gamma function that could be in use.
641     // As this is an aesthetic setting only, any exact values do not matter.
642     brightness *= fabs(brightness);
643 
644     for (int i = 0; i < 3; i++)
645         m->c[i] = brightness;
646 }
647 
648 // Get multiplication factor required if image data is fit within the LSBs of a
649 // higher smaller bit depth fixed-point texture data.
650 // This is broken. Use mp_get_csp_uint_mul().
mp_get_csp_mul(enum mp_csp csp,int input_bits,int texture_bits)651 double mp_get_csp_mul(enum mp_csp csp, int input_bits, int texture_bits)
652 {
653     assert(texture_bits >= input_bits);
654 
655     // Convenience for some irrelevant cases, e.g. rgb565 or disabling expansion.
656     if (!input_bits)
657         return 1;
658 
659     // RGB always uses the full range available.
660     if (csp == MP_CSP_RGB)
661         return ((1LL << input_bits) - 1.) / ((1LL << texture_bits) - 1.);
662 
663     if (csp == MP_CSP_XYZ)
664         return 1;
665 
666     // High bit depth YUV uses a range shifted from 8 bit.
667     return (1LL << input_bits) / ((1LL << texture_bits) - 1.) * 255 / 256;
668 }
669 
670 // Return information about color fixed point representation.his is needed for
671 // converting color from integer formats to or from float. Use as follows:
672 //      float_val = uint_val * m + o
673 //      uint_val = clamp(round((float_val - o) / m))
674 // See H.264/5 Annex E.
675 //  csp: colorspace
676 //  levels: full range flag
677 //  component: ID of the channel, as in mp_regular_imgfmt:
678 //             1 is red/luminance/gray, 2 is green/Cb, 3 is blue/Cr, 4 is alpha.
679 //  bits: number of significant bits, e.g. 10 for yuv420p10, 16 for p010
680 //  out_m: returns factor to multiply the uint number with
681 //  out_o: returns offset to add after multiplication
mp_get_csp_uint_mul(enum mp_csp csp,enum mp_csp_levels levels,int bits,int component,double * out_m,double * out_o)682 void mp_get_csp_uint_mul(enum mp_csp csp, enum mp_csp_levels levels,
683                          int bits, int component, double *out_m, double *out_o)
684 {
685     uint16_t i_min = 0;
686     uint16_t i_max = (1u << bits) - 1;
687     double f_min = 0; // min. float value
688 
689     if (csp != MP_CSP_RGB && component != 4) {
690         if (component == 2 || component == 3) {
691             f_min = (1u << (bits - 1)) / -(double)i_max; // force center => 0
692 
693             if (levels != MP_CSP_LEVELS_PC && bits >= 8) {
694                 i_min = 16  << (bits - 8); // => -0.5
695                 i_max = 240 << (bits - 8); // =>  0.5
696                 f_min = -0.5;
697             }
698         } else {
699             if (levels != MP_CSP_LEVELS_PC && bits >= 8) {
700                 i_min = 16  << (bits - 8); // => 0
701                 i_max = 235 << (bits - 8); // => 1
702             }
703         }
704     }
705 
706     *out_m = 1.0 / (i_max - i_min);
707     *out_o = (1 + f_min) - i_max * *out_m;
708 }
709 
710 /* Fill in the Y, U, V vectors of a yuv-to-rgb conversion matrix
711  * based on the given luma weights of the R, G and B components (lr, lg, lb).
712  * lr+lg+lb is assumed to equal 1.
713  * This function is meant for colorspaces satisfying the following
714  * conditions (which are true for common YUV colorspaces):
715  * - The mapping from input [Y, U, V] to output [R, G, B] is linear.
716  * - Y is the vector [1, 1, 1].  (meaning input Y component maps to 1R+1G+1B)
717  * - U maps to a value with zero R and positive B ([0, x, y], y > 0;
718  *   i.e. blue and green only).
719  * - V maps to a value with zero B and positive R ([x, y, 0], x > 0;
720  *   i.e. red and green only).
721  * - U and V are orthogonal to the luma vector [lr, lg, lb].
722  * - The magnitudes of the vectors U and V are the minimal ones for which
723  *   the image of the set Y=[0...1],U=[-0.5...0.5],V=[-0.5...0.5] under the
724  *   conversion function will cover the set R=[0...1],G=[0...1],B=[0...1]
725  *   (the resulting matrix can be converted for other input/output ranges
726  *   outside this function).
727  * Under these conditions the given parameters lr, lg, lb uniquely
728  * determine the mapping of Y, U, V to R, G, B.
729  */
luma_coeffs(struct mp_cmat * mat,float lr,float lg,float lb)730 static void luma_coeffs(struct mp_cmat *mat, float lr, float lg, float lb)
731 {
732     assert(fabs(lr+lg+lb - 1) < 1e-6);
733     *mat = (struct mp_cmat) {
734         { {1, 0,                    2 * (1-lr)          },
735           {1, -2 * (1-lb) * lb/lg, -2 * (1-lr) * lr/lg  },
736           {1,  2 * (1-lb),          0                   } },
737         // Constant coefficients (mat->c) not set here
738     };
739 }
740 
741 // get the coefficients of the yuv -> rgb conversion matrix
mp_get_csp_matrix(struct mp_csp_params * params,struct mp_cmat * m)742 void mp_get_csp_matrix(struct mp_csp_params *params, struct mp_cmat *m)
743 {
744     enum mp_csp colorspace = params->color.space;
745     if (colorspace <= MP_CSP_AUTO || colorspace >= MP_CSP_COUNT)
746         colorspace = MP_CSP_BT_601;
747     enum mp_csp_levels levels_in = params->color.levels;
748     if (levels_in <= MP_CSP_LEVELS_AUTO || levels_in >= MP_CSP_LEVELS_COUNT)
749         levels_in = MP_CSP_LEVELS_TV;
750 
751     switch (colorspace) {
752     case MP_CSP_BT_601:     luma_coeffs(m, 0.299,  0.587,  0.114 ); break;
753     case MP_CSP_BT_709:     luma_coeffs(m, 0.2126, 0.7152, 0.0722); break;
754     case MP_CSP_SMPTE_240M: luma_coeffs(m, 0.2122, 0.7013, 0.0865); break;
755     case MP_CSP_BT_2020_NC: luma_coeffs(m, 0.2627, 0.6780, 0.0593); break;
756     case MP_CSP_BT_2020_C: {
757         // Note: This outputs into the [-0.5,0.5] range for chroma information.
758         // If this clips on any VO, a constant 0.5 coefficient can be added
759         // to the chroma channels to normalize them into [0,1]. This is not
760         // currently needed by anything, though.
761         *m = (struct mp_cmat){{{0, 0, 1}, {1, 0, 0}, {0, 1, 0}}};
762         break;
763     }
764     case MP_CSP_RGB: {
765         *m = (struct mp_cmat){{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}};
766         levels_in = -1;
767         break;
768     }
769     case MP_CSP_XYZ: {
770         // The vo should probably not be using a matrix generated by this
771         // function for XYZ sources, but if it does, let's just convert it to
772         // an equivalent RGB space based on the colorimetry metadata it
773         // provided in mp_csp_params. (At the risk of clipping, if the
774         // chosen primaries are too small to fit the actual data)
775         mp_get_xyz2rgb_coeffs(params, MP_INTENT_RELATIVE_COLORIMETRIC, m);
776         levels_in = -1;
777         break;
778     }
779     case MP_CSP_YCGCO: {
780         *m = (struct mp_cmat) {
781             {{1,  -1,  1},
782              {1,   1,  0},
783              {1,  -1, -1}},
784         };
785         break;
786     }
787     default:
788         abort();
789     };
790 
791     if (params->is_float)
792         levels_in = -1;
793 
794     if ((colorspace == MP_CSP_BT_601 || colorspace == MP_CSP_BT_709 ||
795          colorspace == MP_CSP_SMPTE_240M || colorspace == MP_CSP_BT_2020_NC))
796     {
797         // Hue is equivalent to rotating input [U, V] subvector around the origin.
798         // Saturation scales [U, V].
799         float huecos = params->gray ? 0 : params->saturation * cos(params->hue);
800         float huesin = params->gray ? 0 : params->saturation * sin(params->hue);
801         for (int i = 0; i < 3; i++) {
802             float u = m->m[i][1], v = m->m[i][2];
803             m->m[i][1] = huecos * u - huesin * v;
804             m->m[i][2] = huesin * u + huecos * v;
805         }
806     }
807 
808     // The values below are written in 0-255 scale - thus bring s into range.
809     double s =
810         mp_get_csp_mul(colorspace, params->input_bits, params->texture_bits) / 255;
811     // NOTE: The yuvfull ranges as presented here are arguably ambiguous,
812     // and conflict with at least the full-range YCbCr/ICtCp values as defined
813     // by ITU-R BT.2100. If somebody ever complains about full-range YUV looking
814     // different from their reference display, this comment is probably why.
815     struct yuvlevels { double ymin, ymax, cmax, cmid; }
816         yuvlim =  { 16*s, 235*s, 240*s, 128*s },
817         yuvfull = {  0*s, 255*s, 255*s, 128*s },
818         anyfull = {  0*s, 255*s, 255*s/2, 0 }, // cmax picked to make cmul=ymul
819         yuvlev;
820     switch (levels_in) {
821     case MP_CSP_LEVELS_TV: yuvlev = yuvlim; break;
822     case MP_CSP_LEVELS_PC: yuvlev = yuvfull; break;
823     case -1: yuvlev = anyfull; break;
824     default:
825         abort();
826     }
827 
828     int levels_out = params->levels_out;
829     if (levels_out <= MP_CSP_LEVELS_AUTO || levels_out >= MP_CSP_LEVELS_COUNT)
830         levels_out = MP_CSP_LEVELS_PC;
831     struct rgblevels { double min, max; }
832         rgblim =  { 16/255., 235/255. },
833         rgbfull = {      0,        1  },
834         rgblev;
835     switch (levels_out) {
836     case MP_CSP_LEVELS_TV: rgblev = rgblim; break;
837     case MP_CSP_LEVELS_PC: rgblev = rgbfull; break;
838     default:
839         abort();
840     }
841 
842     double ymul = (rgblev.max - rgblev.min) / (yuvlev.ymax - yuvlev.ymin);
843     double cmul = (rgblev.max - rgblev.min) / (yuvlev.cmax - yuvlev.cmid) / 2;
844 
845     // Contrast scales the output value range (gain)
846     ymul *= params->contrast;
847     cmul *= params->contrast;
848 
849     for (int i = 0; i < 3; i++) {
850         m->m[i][0] *= ymul;
851         m->m[i][1] *= cmul;
852         m->m[i][2] *= cmul;
853         // Set c so that Y=umin,UV=cmid maps to RGB=min (black to black),
854         // also add brightness offset (black lift)
855         m->c[i] = rgblev.min - m->m[i][0] * yuvlev.ymin
856                   - (m->m[i][1] + m->m[i][2]) * yuvlev.cmid
857                   + params->brightness;
858     }
859 }
860 
861 // Set colorspace related fields in p from f. Don't touch other fields.
mp_csp_set_image_params(struct mp_csp_params * params,const struct mp_image_params * imgparams)862 void mp_csp_set_image_params(struct mp_csp_params *params,
863                              const struct mp_image_params *imgparams)
864 {
865     struct mp_image_params p = *imgparams;
866     mp_image_params_guess_csp(&p); // ensure consistency
867     params->color = p.color;
868 }
869 
mp_colorspace_equal(struct mp_colorspace c1,struct mp_colorspace c2)870 bool mp_colorspace_equal(struct mp_colorspace c1, struct mp_colorspace c2)
871 {
872     return c1.space == c2.space &&
873            c1.levels == c2.levels &&
874            c1.primaries == c2.primaries &&
875            c1.gamma == c2.gamma &&
876            c1.light == c2.light &&
877            c1.sig_peak == c2.sig_peak;
878 }
879 
880 #define OPT_BASE_STRUCT struct mp_csp_equalizer_opts
881 
882 const struct m_sub_options mp_csp_equalizer_conf = {
883     .opts = (const m_option_t[]) {
884         {"brightness", OPT_INT(values[MP_CSP_EQ_BRIGHTNESS]),
885             M_RANGE(-100, 100)},
886         {"saturation", OPT_INT(values[MP_CSP_EQ_SATURATION]),
887             M_RANGE(-100, 100)},
888         {"contrast", OPT_INT(values[MP_CSP_EQ_CONTRAST]),
889             M_RANGE(-100, 100)},
890         {"hue", OPT_INT(values[MP_CSP_EQ_HUE]),
891             M_RANGE(-100, 100)},
892         {"gamma", OPT_INT(values[MP_CSP_EQ_GAMMA]),
893             M_RANGE(-100, 100)},
894         {"video-output-levels",
895             OPT_CHOICE_C(values[MP_CSP_EQ_OUTPUT_LEVELS], mp_csp_levels_names)},
896         {0}
897     },
898     .size = sizeof(struct mp_csp_equalizer_opts),
899 };
900 
901 // Copy settings from eq into params.
mp_csp_copy_equalizer_values(struct mp_csp_params * params,const struct mp_csp_equalizer_opts * eq)902 void mp_csp_copy_equalizer_values(struct mp_csp_params *params,
903                                   const struct mp_csp_equalizer_opts *eq)
904 {
905     params->brightness = eq->values[MP_CSP_EQ_BRIGHTNESS] / 100.0;
906     params->contrast = (eq->values[MP_CSP_EQ_CONTRAST] + 100) / 100.0;
907     params->hue = eq->values[MP_CSP_EQ_HUE] / 100.0 * M_PI;
908     params->saturation = (eq->values[MP_CSP_EQ_SATURATION] + 100) / 100.0;
909     params->gamma = exp(log(8.0) * eq->values[MP_CSP_EQ_GAMMA] / 100.0);
910     params->levels_out = eq->values[MP_CSP_EQ_OUTPUT_LEVELS];
911 }
912 
mp_csp_equalizer_create(void * ta_parent,struct mpv_global * global)913 struct mp_csp_equalizer_state *mp_csp_equalizer_create(void *ta_parent,
914                                                     struct mpv_global *global)
915 {
916     struct m_config_cache *c = m_config_cache_alloc(ta_parent, global,
917                                                     &mp_csp_equalizer_conf);
918     // The terrible, terrible truth.
919     return (struct mp_csp_equalizer_state *)c;
920 }
921 
mp_csp_equalizer_state_changed(struct mp_csp_equalizer_state * state)922 bool mp_csp_equalizer_state_changed(struct mp_csp_equalizer_state *state)
923 {
924     struct m_config_cache *c = (struct m_config_cache *)state;
925     return m_config_cache_update(c);
926 }
927 
mp_csp_equalizer_state_get(struct mp_csp_equalizer_state * state,struct mp_csp_params * params)928 void mp_csp_equalizer_state_get(struct mp_csp_equalizer_state *state,
929                                 struct mp_csp_params *params)
930 {
931     struct m_config_cache *c = (struct m_config_cache *)state;
932     m_config_cache_update(c);
933     struct mp_csp_equalizer_opts *opts = c->opts;
934     mp_csp_copy_equalizer_values(params, opts);
935 }
936 
mp_invert_cmat(struct mp_cmat * out,struct mp_cmat * in)937 void mp_invert_cmat(struct mp_cmat *out, struct mp_cmat *in)
938 {
939     *out = *in;
940     mp_invert_matrix3x3(out->m);
941 
942     // fix the constant coefficient
943     // rgb = M * yuv + C
944     // M^-1 * rgb = yuv + M^-1 * C
945     // yuv = M^-1 * rgb - M^-1 * C
946     //                  ^^^^^^^^^^
947     out->c[0] = -(out->m[0][0] * in->c[0] + out->m[0][1] * in->c[1] + out->m[0][2] * in->c[2]);
948     out->c[1] = -(out->m[1][0] * in->c[0] + out->m[1][1] * in->c[1] + out->m[1][2] * in->c[2]);
949     out->c[2] = -(out->m[2][0] * in->c[0] + out->m[2][1] * in->c[1] + out->m[2][2] * in->c[2]);
950 }
951 
952 // Multiply the color in c with the given matrix.
953 // i/o is {R, G, B} or {Y, U, V} (depending on input/output and matrix), using
954 // a fixed point representation with the given number of bits (so for bits==8,
955 // [0,255] maps to [0,1]). The output is clipped to the range as needed.
mp_map_fixp_color(struct mp_cmat * matrix,int ibits,int in[3],int obits,int out[3])956 void mp_map_fixp_color(struct mp_cmat *matrix, int ibits, int in[3],
957                                                int obits, int out[3])
958 {
959     for (int i = 0; i < 3; i++) {
960         double val = matrix->c[i];
961         for (int x = 0; x < 3; x++)
962             val += matrix->m[i][x] * in[x] / ((1 << ibits) - 1);
963         int ival = lrint(val * ((1 << obits) - 1));
964         out[i] = av_clip(ival, 0, (1 << obits) - 1);
965     }
966 }
967