2 * Common code related to colorspaces and conversion
4 * Copyleft (C) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer 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 General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * You can alternatively redistribute this file and/or
23 * modify it under the terms of the GNU Lesser General Public
24 * License as published by the Free Software Foundation; either
25 * version 2.1 of the License, or (at your option) any later version.
30 #include "libavutil/common.h"
34 * \brief little helper function to create a lookup table for gamma
35 * \param map buffer to create map into
36 * \param size size of buffer
37 * \param gamma gamma value
39 void mp_gen_gamma_map(uint8_t *map
, int size
, float gamma
) {
42 for (i
= 0; i
< size
; i
++)
43 map
[i
] = 255 * i
/ (size
- 1);
47 for (i
= 0; i
< size
; i
++) {
48 float tmp
= (float)i
/ (size
- 1.0);
49 tmp
= pow(tmp
, gamma
);
50 if (tmp
> 1.0) tmp
= 1.0;
51 if (tmp
< 0.0) tmp
= 0.0;
57 * \brief get the coefficients of the yuv -> rgb conversion matrix
58 * \param params struct specifying the properties of the conversion like brightness, ...
59 * \param yuv2rgb array to store coefficients into
61 * Note: contrast, hue and saturation will only work as expected with YUV formats,
62 * not with e.g. MP_CSP_XYZ
64 void mp_get_yuv2rgb_coeffs(struct mp_csp_params
*params
, float yuv2rgb
[3][4]) {
65 float uvcos
= params
->saturation
* cos(params
->hue
);
66 float uvsin
= params
->saturation
* sin(params
->hue
);
67 int format
= params
->format
;
68 int levelconv
= params
->levelconv
;
70 const float (*uv_coeffs
)[3];
71 const float *level_adjust
;
72 static const float yuv_level_adjust
[MP_CSP_LEVELCONV_COUNT
][4] = {
73 {-16 / 255.0, -128 / 255.0, -128 / 255.0, 1.164},
74 { 16 / 255.0 * 1.164, -128 / 255.0, -128 / 255.0, 1.0/1.164},
75 { 0, -128 / 255.0, -128 / 255.0, 1},
77 static const float xyz_level_adjust
[4] = {0, 0, 0, 0};
78 static const float uv_coeffs_table
[MP_CSP_COUNT
][3][3] = {
91 {1, -0.1870, -0.4664},
94 [MP_CSP_SMPTE_240M
] = {
96 {1, -0.2253, -0.5000},
105 { 3.2404542, -1.5371385, -0.4985314},
106 {-0.9692660, 1.8760108, 0.0415560},
107 { 0.0556434, -0.2040259, 1.0572252}
111 if (format
< 0 || format
>= MP_CSP_COUNT
)
112 format
= MP_CSP_DEFAULT
;
113 uv_coeffs
= uv_coeffs_table
[format
];
114 if (levelconv
< 0 || levelconv
>= MP_CSP_LEVELCONV_COUNT
)
115 levelconv
= MP_CSP_LEVELCONV_TV_TO_PC
;
116 level_adjust
= yuv_level_adjust
[levelconv
];
117 if (format
== MP_CSP_XYZ
)
118 level_adjust
= xyz_level_adjust
;
120 for (i
= 0; i
< 3; i
++) {
121 yuv2rgb
[i
][COL_C
] = params
->brightness
;
122 yuv2rgb
[i
][COL_Y
] = uv_coeffs
[i
][COL_Y
] * level_adjust
[COL_C
] * params
->contrast
;
123 yuv2rgb
[i
][COL_C
] += level_adjust
[COL_Y
] * yuv2rgb
[i
][COL_Y
];
124 yuv2rgb
[i
][COL_U
] = uv_coeffs
[i
][COL_U
] * uvcos
+ uv_coeffs
[i
][COL_V
] * uvsin
;
125 yuv2rgb
[i
][COL_C
] += level_adjust
[COL_U
] * yuv2rgb
[i
][COL_U
];
126 yuv2rgb
[i
][COL_V
] = uv_coeffs
[i
][COL_U
] * uvsin
+ uv_coeffs
[i
][COL_V
] * uvcos
;
127 yuv2rgb
[i
][COL_C
] += level_adjust
[COL_V
] * yuv2rgb
[i
][COL_V
];
128 // this "centers" contrast control so that e.g. a contrast of 0
129 // leads to a grey image, not a black one
130 yuv2rgb
[i
][COL_C
] += 0.5 - params
->contrast
/ 2.0;
134 //! size of gamma map use to avoid slow exp function in gen_yuv2rgb_map
135 #define GMAP_SIZE (1024)
137 * \brief generate a 3D YUV -> RGB map
138 * \param params struct containing parameters like brightness, gamma, ...
139 * \param map where to store map. Must provide space for (size + 2)^3 elements
140 * \param size size of the map, excluding border
142 void mp_gen_yuv2rgb_map(struct mp_csp_params
*params
, unsigned char *map
, int size
) {
144 float step
= 1.0 / size
;
147 unsigned char gmaps
[3][GMAP_SIZE
];
148 mp_gen_gamma_map(gmaps
[0], GMAP_SIZE
, params
->rgamma
);
149 mp_gen_gamma_map(gmaps
[1], GMAP_SIZE
, params
->ggamma
);
150 mp_gen_gamma_map(gmaps
[2], GMAP_SIZE
, params
->bgamma
);
151 mp_get_yuv2rgb_coeffs(params
, yuv2rgb
);
152 for (i
= 0; i
< 3; i
++)
153 for (j
= 0; j
< 4; j
++)
154 yuv2rgb
[i
][j
] *= GMAP_SIZE
- 1;
156 for (i
= -1; i
<= size
; i
++) {
158 for (j
= -1; j
<= size
; j
++) {
160 for (k
= -1; k
<= size
; k
++) {
161 for (l
= 0; l
< 3; l
++) {
162 float rgb
= yuv2rgb
[l
][COL_Y
] * y
+ yuv2rgb
[l
][COL_U
] * u
+ yuv2rgb
[l
][COL_V
] * v
+ yuv2rgb
[l
][COL_C
];
163 *map
++ = gmaps
[l
][av_clip(rgb
, 0, GMAP_SIZE
- 1)];
165 y
+= (k
== -1 || k
== size
- 1) ? step
/ 2 : step
;
167 u
+= (j
== -1 || j
== size
- 1) ? step
/ 2 : step
;
169 v
+= (i
== -1 || i
== size
- 1) ? step
/ 2 : step
;