Move some sh_audio field setting out of demuxer.c
[mplayer/glamo.git] / libvo / csputils.c
blob57362f6725cb9a6a98890ba752d81adc85337eb8
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 levelconv = params->levelconv;
64 int i;
65 const float (*uv_coeffs)[3];
66 const float *level_adjust;
67 static const float yuv_level_adjust[MP_CSP_LEVELCONV_COUNT][4] = {
68 {-16 / 255.0, -128 / 255.0, -128 / 255.0, 1.164},
69 { 16 / 255.0 * 1.164, -128 / 255.0, -128 / 255.0, 1.0/1.164},
70 { 0, -128 / 255.0, -128 / 255.0, 1},
72 static const float xyz_level_adjust[4] = {0, 0, 0, 0};
73 static const float uv_coeffs_table[MP_CSP_COUNT][3][3] = {
74 [MP_CSP_DEFAULT] = {
75 {1, 0.000, 1.596},
76 {1, -0.391, -0.813},
77 {1, 2.018, 0.000}
79 [MP_CSP_BT_601] = {
80 {1, 0.000, 1.403},
81 {1, -0.344, -0.714},
82 {1, 1.773, 0.000}
84 [MP_CSP_BT_709] = {
85 {1, 0.0000, 1.5701},
86 {1, -0.1870, -0.4664},
87 {1, 1.8556, 0.0000}
89 [MP_CSP_SMPTE_240M] = {
90 {1, 0.0000, 1.5756},
91 {1, -0.2253, -0.5000},
92 {1, 1.8270, 0.0000}
94 [MP_CSP_EBU] = {
95 {1, 0.000, 1.140},
96 {1, -0.396, -0.581},
97 {1, 2.029, 0.000}
99 [MP_CSP_XYZ] = {
100 { 3.2404542, -1.5371385, -0.4985314},
101 {-0.9692660, 1.8760108, 0.0415560},
102 { 0.0556434, -0.2040259, 1.0572252}
106 if (format < 0 || format >= MP_CSP_COUNT)
107 format = MP_CSP_DEFAULT;
108 uv_coeffs = uv_coeffs_table[format];
109 if (levelconv < 0 || levelconv >= MP_CSP_LEVELCONV_COUNT)
110 levelconv = MP_CSP_LEVELCONV_TV_TO_PC;
111 level_adjust = yuv_level_adjust[levelconv];
112 if (format == MP_CSP_XYZ)
113 level_adjust = xyz_level_adjust;
115 for (i = 0; i < 3; i++) {
116 yuv2rgb[i][COL_C] = params->brightness;
117 yuv2rgb[i][COL_Y] = uv_coeffs[i][COL_Y] * level_adjust[COL_C] * params->contrast;
118 yuv2rgb[i][COL_C] += level_adjust[COL_Y] * yuv2rgb[i][COL_Y];
119 yuv2rgb[i][COL_U] = uv_coeffs[i][COL_U] * uvcos + uv_coeffs[i][COL_V] * uvsin;
120 yuv2rgb[i][COL_C] += level_adjust[COL_U] * yuv2rgb[i][COL_U];
121 yuv2rgb[i][COL_V] = uv_coeffs[i][COL_U] * uvsin + uv_coeffs[i][COL_V] * uvcos;
122 yuv2rgb[i][COL_C] += level_adjust[COL_V] * yuv2rgb[i][COL_V];
123 // this "centers" contrast control so that e.g. a contrast of 0
124 // leads to a grey image, not a black one
125 yuv2rgb[i][COL_C] += 0.5 - params->contrast / 2.0;
129 //! size of gamma map use to avoid slow exp function in gen_yuv2rgb_map
130 #define GMAP_SIZE (1024)
132 * \brief generate a 3D YUV -> RGB map
133 * \param params struct containing parameters like brightness, gamma, ...
134 * \param map where to store map. Must provide space for (size + 2)^3 elements
135 * \param size size of the map, excluding border
137 void mp_gen_yuv2rgb_map(struct mp_csp_params *params, unsigned char *map, int size) {
138 int i, j, k, l;
139 float step = 1.0 / size;
140 float y, u, v;
141 float yuv2rgb[3][4];
142 unsigned char gmaps[3][GMAP_SIZE];
143 mp_gen_gamma_map(gmaps[0], GMAP_SIZE, params->rgamma);
144 mp_gen_gamma_map(gmaps[1], GMAP_SIZE, params->ggamma);
145 mp_gen_gamma_map(gmaps[2], GMAP_SIZE, params->bgamma);
146 mp_get_yuv2rgb_coeffs(params, yuv2rgb);
147 for (i = 0; i < 3; i++)
148 for (j = 0; j < 4; j++)
149 yuv2rgb[i][j] *= GMAP_SIZE - 1;
150 v = 0;
151 for (i = -1; i <= size; i++) {
152 u = 0;
153 for (j = -1; j <= size; j++) {
154 y = 0;
155 for (k = -1; k <= size; k++) {
156 for (l = 0; l < 3; l++) {
157 float rgb = yuv2rgb[l][COL_Y] * y + yuv2rgb[l][COL_U] * u + yuv2rgb[l][COL_V] * v + yuv2rgb[l][COL_C];
158 *map++ = gmaps[l][av_clip(rgb, 0, GMAP_SIZE - 1)];
160 y += (k == -1 || k == size - 1) ? step / 2 : step;
162 u += (j == -1 || j == size - 1) ? step / 2 : step;
164 v += (i == -1 || i == size - 1) ? step / 2 : step;