Merge svn changes up to r30165
[mplayer/glamo.git] / libvo / csputils.c
blob6f69ec4212e495a475ddac9b5324dc8bf47c17aa
1 /*
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.
23 #include <stdint.h>
24 #include <math.h>
25 #include "libavutil/common.h"
26 #include "csputils.h"
28 /**
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) {
35 int i;
36 if (gamma == 1.0) {
37 for (i = 0; i < size; i++)
38 map[i] = 255 * i / (size - 1);
39 return;
41 gamma = 1.0 / gamma;
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;
47 map[i] = 255 * tmp;
51 /**
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;
63 int i;
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] = {
70 [MP_CSP_DEFAULT] = {
71 {1, 0.000, 1.596},
72 {1, -0.391, -0.813},
73 {1, 2.018, 0.000}
75 [MP_CSP_BT_601] = {
76 {1, 0.000, 1.403},
77 {1, -0.344, -0.714},
78 {1, 1.773, 0.000}
80 [MP_CSP_BT_709] = {
81 {1, 0.0000, 1.5701},
82 {1, -0.1870, -0.4664},
83 {1, 1.8556, 0.0000}
85 [MP_CSP_SMPTE_240M] = {
86 {1, 0.0000, 1.5756},
87 {1, -0.2253, -0.5000},
88 {1, 1.8270, 0.0000}
90 [MP_CSP_EBU] = {
91 {1, 0.000, 1.140},
92 {1, -0.396, -0.581},
93 {1, 2.029, 0.000}
95 [MP_CSP_XYZ] = {
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) {
132 int i, j, k, l;
133 float step = 1.0 / size;
134 float y, u, v;
135 float yuv2rgb[3][4];
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;
144 v = 0;
145 for (i = -1; i <= size; i++) {
146 u = 0;
147 for (j = -1; j <= size; j++) {
148 y = 0;
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;