annotate libvo/csputils.c @ 30108:0898adc64a6f

Extract functions to generate yuv->rgb matrices and lookup tables into a separate file.
author reimar
date Thu, 31 Dec 2009 18:25:35 +0000
parents
children 0f25d3062987
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30108
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
1 /*
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
2 * Common code related to colorspaces and conversion
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
3 *
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
4 * Copyleft (C) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
5 *
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
6 * This file is part of MPlayer.
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
7 *
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
8 * MPlayer is free software; you can redistribute it and/or modify
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
11 * (at your option) any later version.
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
12 *
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
13 * MPlayer is distributed in the hope that it will be useful,
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
16 * GNU General Public License for more details.
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
17 *
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License along
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
21 */
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
22
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
23 #include <stdint.h>
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
24 #include <math.h>
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
25 #include "libavutil/common.h"
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
26 #include "csputils.h"
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
27
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
28 /**
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
29 * \brief little helper function to create a lookup table for gamma
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
30 * \param map buffer to create map into
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
31 * \param size size of buffer
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
32 * \param gamma gamma value
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
33 */
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
34 void mp_gen_gamma_map(uint8_t *map, int size, float gamma) {
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
35 int i;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
36 if (gamma == 1.0) {
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
37 for (i = 0; i < size; i++)
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
38 map[i] = 255 * i / (size - 1);
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
39 return;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
40 }
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
41 gamma = 1.0 / gamma;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
42 for (i = 0; i < size; i++) {
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
43 float tmp = (float)i / (size - 1.0);
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
44 tmp = pow(tmp, gamma);
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
45 if (tmp > 1.0) tmp = 1.0;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
46 if (tmp < 0.0) tmp = 0.0;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
47 map[i] = 255 * tmp;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
48 }
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
49 }
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
50
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
51 /**
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
52 * \brief get the coefficients of the yuv -> rgb conversion matrix
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
53 * \param params struct specifying the properties of the conversion like brightness, ...
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
54 * \param yuv2rgb array to store coefficients into
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
55 */
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
56 void mp_get_yuv2rgb_coeffs(struct mp_csp_params *params, float yuv2rgb[3][4]) {
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
57 float uvcos = params->saturation * cos(params->hue);
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
58 float uvsin = params->saturation * sin(params->hue);
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
59 int i;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
60 float uv_coeffs[3][2] = {
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
61 { 0.000, 1.596},
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
62 {-0.391, -0.813},
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
63 { 2.018, 0.000}
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
64 };
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
65 for (i = 0; i < 3; i++) {
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
66 yuv2rgb[i][COL_C] = params->brightness;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
67 yuv2rgb[i][COL_Y] = 1.164 * params->contrast;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
68 yuv2rgb[i][COL_C] += (-16 / 255.0) * yuv2rgb[i][COL_Y];
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
69 yuv2rgb[i][COL_U] = uv_coeffs[i][0] * uvcos + uv_coeffs[i][1] * uvsin;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
70 yuv2rgb[i][COL_C] += (-128 / 255.0) * yuv2rgb[i][COL_U];
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
71 yuv2rgb[i][COL_V] = uv_coeffs[i][0] * uvsin + uv_coeffs[i][1] * uvcos;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
72 yuv2rgb[i][COL_C] += (-128 / 255.0) * yuv2rgb[i][COL_V];
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
73 // this "centers" contrast control so that e.g. a contrast of 0
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
74 // leads to a grey image, not a black one
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
75 yuv2rgb[i][COL_C] += 0.5 - params->contrast / 2.0;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
76 }
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
77 }
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
78
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
79 //! size of gamma map use to avoid slow exp function in gen_yuv2rgb_map
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
80 #define GMAP_SIZE (1024)
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
81 /**
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
82 * \brief generate a 3D YUV -> RGB map
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
83 * \param params struct containing parameters like brightness, gamma, ...
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
84 * \param map where to store map. Must provide space for (size + 2)^3 elements
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
85 * \param size size of the map, excluding border
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
86 */
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
87 void mp_gen_yuv2rgb_map(struct mp_csp_params *params, unsigned char *map, int size) {
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
88 int i, j, k, l;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
89 float step = 1.0 / size;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
90 float y, u, v;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
91 float yuv2rgb[3][4];
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
92 unsigned char gmaps[3][GMAP_SIZE];
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
93 mp_gen_gamma_map(gmaps[0], GMAP_SIZE, params->rgamma);
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
94 mp_gen_gamma_map(gmaps[1], GMAP_SIZE, params->ggamma);
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
95 mp_gen_gamma_map(gmaps[2], GMAP_SIZE, params->bgamma);
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
96 mp_get_yuv2rgb_coeffs(params, yuv2rgb);
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
97 for (i = 0; i < 3; i++)
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
98 for (j = 0; j < 4; j++)
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
99 yuv2rgb[i][j] *= GMAP_SIZE - 1;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
100 v = 0;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
101 for (i = -1; i <= size; i++) {
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
102 u = 0;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
103 for (j = -1; j <= size; j++) {
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
104 y = 0;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
105 for (k = -1; k <= size; k++) {
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
106 for (l = 0; l < 3; l++) {
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
107 float rgb = yuv2rgb[l][COL_Y] * y + yuv2rgb[l][COL_U] * u + yuv2rgb[l][COL_V] * v + yuv2rgb[l][COL_C];
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
108 *map++ = gmaps[l][av_clip(rgb, 0, GMAP_SIZE - 1)];
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
109 }
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
110 y += (k == -1 || k == size - 1) ? step / 2 : step;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
111 }
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
112 u += (j == -1 || j == size - 1) ? step / 2 : step;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
113 }
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
114 v += (i == -1 || i == size - 1) ? step / 2 : step;
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
115 }
0898adc64a6f Extract functions to generate yuv->rgb matrices and lookup tables into a
reimar
parents:
diff changeset
116 }