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.
25 #include "libavutil/common.h"
29 * \brief little helper function to create a lookup table for gamma
30 * \param map buffer to create map into
31 * \param size size of buffer
32 * \param gamma gamma value
34 void mp_gen_gamma_map(uint8_t *map
, int size
, float gamma
) {
37 for (i
= 0; i
< size
; i
++)
38 map
[i
] = 255 * i
/ (size
- 1);
42 for (i
= 0; i
< size
; i
++) {
43 float tmp
= (float)i
/ (size
- 1.0);
44 tmp
= pow(tmp
, gamma
);
45 if (tmp
> 1.0) tmp
= 1.0;
46 if (tmp
< 0.0) tmp
= 0.0;
52 * \brief get the coefficients of the yuv -> rgb conversion matrix
53 * \param params struct specifying the properties of the conversion like brightness, ...
54 * \param yuv2rgb array to store coefficients into
56 * Note: contrast, hue and saturation will only work as expected with YUV formats,
57 * not with e.g. MP_CSP_XYZ
59 void mp_get_yuv2rgb_coeffs(struct mp_csp_params
*params
, float yuv2rgb
[3][4]) {
60 float uvcos
= params
->saturation
* cos(params
->hue
);
61 float uvsin
= params
->saturation
* sin(params
->hue
);
62 int format
= params
->format
;
64 const float (*uv_coeffs
)[3];
65 const float *level_adjust
;
66 static const float yuv_pc_level_adjust
[4] = {-16 / 255.0, -128 / 255.0, -128 / 255.0, 1.164};
67 static const float yuv_tv_level_adjust
[4] = {0, -128 / 255.0, -128 / 255.0, 0};
68 static const float xyz_level_adjust
[4] = {0, 0, 0, 0};
69 static const float uv_coeffs_table
[MP_CSP_COUNT
][3][3] = {
82 {1, -0.1870, -0.4664},
85 [MP_CSP_SMPTE_240M
] = {
87 {1, -0.2253, -0.5000},
96 { 3.2404542, -1.5371385, -0.4985314},
97 {-0.9692660, 1.8760108, 0.0415560},
98 { 0.0556434, -0.2040259, 1.0572252}
102 if (format
< 0 || format
>= MP_CSP_COUNT
)
103 format
= MP_CSP_DEFAULT
;
104 uv_coeffs
= uv_coeffs_table
[format
];
105 level_adjust
= yuv_pc_level_adjust
;
106 if (format
== MP_CSP_XYZ
)
107 level_adjust
= xyz_level_adjust
;
109 for (i
= 0; i
< 3; i
++) {
110 yuv2rgb
[i
][COL_C
] = params
->brightness
;
111 yuv2rgb
[i
][COL_Y
] = uv_coeffs
[i
][COL_Y
] * level_adjust
[COL_C
] * params
->contrast
;
112 yuv2rgb
[i
][COL_C
] += level_adjust
[COL_Y
] * yuv2rgb
[i
][COL_Y
];
113 yuv2rgb
[i
][COL_U
] = uv_coeffs
[i
][COL_U
] * uvcos
+ uv_coeffs
[i
][COL_V
] * uvsin
;
114 yuv2rgb
[i
][COL_C
] += level_adjust
[COL_U
] * yuv2rgb
[i
][COL_U
];
115 yuv2rgb
[i
][COL_V
] = uv_coeffs
[i
][COL_U
] * uvsin
+ uv_coeffs
[i
][COL_V
] * uvcos
;
116 yuv2rgb
[i
][COL_C
] += level_adjust
[COL_V
] * yuv2rgb
[i
][COL_V
];
117 // this "centers" contrast control so that e.g. a contrast of 0
118 // leads to a grey image, not a black one
119 yuv2rgb
[i
][COL_C
] += 0.5 - params
->contrast
/ 2.0;
123 //! size of gamma map use to avoid slow exp function in gen_yuv2rgb_map
124 #define GMAP_SIZE (1024)
126 * \brief generate a 3D YUV -> RGB map
127 * \param params struct containing parameters like brightness, gamma, ...
128 * \param map where to store map. Must provide space for (size + 2)^3 elements
129 * \param size size of the map, excluding border
131 void mp_gen_yuv2rgb_map(struct mp_csp_params
*params
, unsigned char *map
, int size
) {
133 float step
= 1.0 / size
;
136 unsigned char gmaps
[3][GMAP_SIZE
];
137 mp_gen_gamma_map(gmaps
[0], GMAP_SIZE
, params
->rgamma
);
138 mp_gen_gamma_map(gmaps
[1], GMAP_SIZE
, params
->ggamma
);
139 mp_gen_gamma_map(gmaps
[2], GMAP_SIZE
, params
->bgamma
);
140 mp_get_yuv2rgb_coeffs(params
, yuv2rgb
);
141 for (i
= 0; i
< 3; i
++)
142 for (j
= 0; j
< 4; j
++)
143 yuv2rgb
[i
][j
] *= GMAP_SIZE
- 1;
145 for (i
= -1; i
<= size
; i
++) {
147 for (j
= -1; j
<= size
; j
++) {
149 for (k
= -1; k
<= size
; k
++) {
150 for (l
= 0; l
< 3; l
++) {
151 float rgb
= yuv2rgb
[l
][COL_Y
] * y
+ yuv2rgb
[l
][COL_U
] * u
+ yuv2rgb
[l
][COL_V
] * v
+ yuv2rgb
[l
][COL_C
];
152 *map
++ = gmaps
[l
][av_clip(rgb
, 0, GMAP_SIZE
- 1)];
154 y
+= (k
== -1 || k
== size
- 1) ? step
/ 2 : step
;
156 u
+= (j
== -1 || j
== size
- 1) ? step
/ 2 : step
;
158 v
+= (i
== -1 || i
== size
- 1) ? step
/ 2 : step
;