vl: seperate shader buffers from components
[mesa.git] / src / gallium / auxiliary / vl / vl_csc.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Younes Manton.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "util/u_math.h"
29 #include "util/u_debug.h"
30
31 #include "vl_csc.h"
32
33 /*
34 * Color space conversion formulas
35 *
36 * To convert YCbCr to RGB,
37 * vec4 ycbcr, rgb
38 * mat44 csc
39 * rgb = csc * ycbcr
40 *
41 * To calculate the color space conversion matrix csc with ProcAmp adjustments,
42 * mat44 csc, cstd, procamp, bias
43 * csc = cstd * (procamp * bias)
44 *
45 * Where cstd is a matrix corresponding to one of the color standards (BT.601, BT.709, etc)
46 * adjusted for the kind of YCbCr -> RGB mapping wanted (1:1, full),
47 * bias is a matrix corresponding to the kind of YCbCr -> RGB mapping wanted (1:1, full)
48 *
49 * To calculate procamp,
50 * mat44 procamp, hue, saturation, brightness, contrast
51 * procamp = brightness * (saturation * (contrast * hue))
52 * Alternatively,
53 * procamp = saturation * (brightness * (contrast * hue))
54 *
55 * contrast
56 * [ c, 0, 0, 0]
57 * [ 0, c, 0, 0]
58 * [ 0, 0, c, 0]
59 * [ 0, 0, 0, 1]
60 *
61 * brightness
62 * [ 1, 0, 0, b]
63 * [ 0, 1, 0, 0]
64 * [ 0, 0, 1, 0]
65 * [ 0, 0, 0, 1]
66 *
67 * saturation
68 * [ 1, 0, 0, 0]
69 * [ 0, s, 0, 0]
70 * [ 0, 0, s, 0]
71 * [ 0, 0, 0, 1]
72 *
73 * hue
74 * [ 1, 0, 0, 0]
75 * [ 0, cos(h), sin(h), 0]
76 * [ 0, -sin(h), cos(h), 0]
77 * [ 0, 0, 0, 1]
78 *
79 * procamp
80 * [ c, 0, 0, b]
81 * [ 0, c*s*cos(h), c*s*sin(h), 0]
82 * [ 0, -c*s*sin(h), c*s*cos(h), 0]
83 * [ 0, 0, 0, 1]
84 *
85 * bias
86 * [ 1, 0, 0, ybias]
87 * [ 0, 1, 0, cbbias]
88 * [ 0, 0, 1, crbias]
89 * [ 0, 0, 0, 1]
90 *
91 * csc
92 * [ c*cstd[ 0], c*cstd[ 1]*s*cos(h) - c*cstd[ 2]*s*sin(h), c*cstd[ 2]*s*cos(h) + c*cstd[ 1]*s*sin(h), cstd[ 3] + cstd[ 0]*(b + c*ybias) + cstd[ 1]*(c*cbbias*s*cos(h) + c*crbias*s*sin(h)) + cstd[ 2]*(c*crbias*s*cos(h) - c*cbbias*s*sin(h))]
93 * [ c*cstd[ 4], c*cstd[ 5]*s*cos(h) - c*cstd[ 6]*s*sin(h), c*cstd[ 6]*s*cos(h) + c*cstd[ 5]*s*sin(h), cstd[ 7] + cstd[ 4]*(b + c*ybias) + cstd[ 5]*(c*cbbias*s*cos(h) + c*crbias*s*sin(h)) + cstd[ 6]*(c*crbias*s*cos(h) - c*cbbias*s*sin(h))]
94 * [ c*cstd[ 8], c*cstd[ 9]*s*cos(h) - c*cstd[10]*s*sin(h), c*cstd[10]*s*cos(h) + c*cstd[ 9]*s*sin(h), cstd[11] + cstd[ 8]*(b + c*ybias) + cstd[ 9]*(c*cbbias*s*cos(h) + c*crbias*s*sin(h)) + cstd[10]*(c*crbias*s*cos(h) - c*cbbias*s*sin(h))]
95 * [ c*cstd[12], c*cstd[13]*s*cos(h) - c*cstd[14]*s*sin(h), c*cstd[14]*s*cos(h) + c*cstd[13]*s*sin(h), cstd[15] + cstd[12]*(b + c*ybias) + cstd[13]*(c*cbbias*s*cos(h) + c*crbias*s*sin(h)) + cstd[14]*(c*crbias*s*cos(h) - c*cbbias*s*sin(h))]
96 */
97
98 /*
99 * Converts ITU-R BT.601 YCbCr pixels to RGB pixels where:
100 * Y is in [16,235], Cb and Cr are in [16,240]
101 * R, G, and B are in [16,235]
102 */
103 static const float bt_601[16] =
104 {
105 1.0f, 0.0f, 1.371f, 0.0f,
106 1.0f, -0.336f, -0.698f, 0.0f,
107 1.0f, 1.732f, 0.0f, 0.0f,
108 0.0f, 0.0f, 0.0f, 1.0f
109 };
110
111 /*
112 * Converts ITU-R BT.601 YCbCr pixels to RGB pixels where:
113 * Y is in [16,235], Cb and Cr are in [16,240]
114 * R, G, and B are in [0,255]
115 */
116 static const float bt_601_full[16] =
117 {
118 1.164f, 0.0f, 1.596f, 0.0f,
119 1.164f, -0.391f, -0.813f, 0.0f,
120 1.164f, 2.018f, 0.0f, 0.0f,
121 0.0f, 0.0f, 0.0f, 1.0f
122 };
123
124 /*
125 * Converts ITU-R BT.709 YCbCr pixels to RGB pixels where:
126 * Y is in [16,235], Cb and Cr are in [16,240]
127 * R, G, and B are in [16,235]
128 */
129 static const float bt_709[16] =
130 {
131 1.0f, 0.0f, 1.540f, 0.0f,
132 1.0f, -0.183f, -0.459f, 0.0f,
133 1.0f, 1.816f, 0.0f, 0.0f,
134 0.0f, 0.0f, 0.0f, 1.0f
135 };
136
137 /*
138 * Converts ITU-R BT.709 YCbCr pixels to RGB pixels where:
139 * Y is in [16,235], Cb and Cr are in [16,240]
140 * R, G, and B are in [0,255]
141 */
142 static const float bt_709_full[16] =
143 {
144 1.164f, 0.0f, 1.793f, 0.0f,
145 1.164f, -0.213f, -0.534f, 0.0f,
146 1.164f, 2.115f, 0.0f, 0.0f,
147 0.0f, 0.0f, 0.0f, 1.0f
148 };
149
150 static const float smpte240m[16] =
151 {
152 1.0f, 0.0f, 1.582f, 0.0f,
153 1.0f, -0.228f, -0.478f, 0.0f,
154 1.0f, 1.833f, 0.0f, 0.0f,
155 0.0f, 0.0f, 0.0f, 1.0f
156 };
157
158 static const float smpte240m_full[16] =
159 {
160 1.164f, 0.0f, 1.794f, 0.0f,
161 1.164f, -0.258f, -0.543f, 0.0f,
162 1.164f, 2.079f, 0.0f, 0.0f,
163 0.0f, 0.0f, 0.0f, 1.0f
164 };
165
166 static const float identity[16] =
167 {
168 1.0f, 0.0f, 0.0f, 0.0f,
169 0.0f, 1.0f, 0.0f, 0.0f,
170 0.0f, 0.0f, 1.0f, 0.0f,
171 0.0f, 0.0f, 0.0f, 1.0f
172 };
173
174 const struct vl_procamp vl_default_procamp = {
175 0.0f, /* brightness */
176 1.0f, /* contrast */
177 1.0f, /* saturation */
178 0.0f /* hue */
179 };
180
181 void vl_csc_get_matrix(enum VL_CSC_COLOR_STANDARD cs,
182 struct vl_procamp *procamp,
183 bool full_range,
184 float *matrix)
185 {
186 float ybias = full_range ? -16.0f/255.0f : 0.0f;
187 float cbbias = -128.0f/255.0f;
188 float crbias = -128.0f/255.0f;
189
190 const struct vl_procamp *p = procamp ? procamp : &vl_default_procamp;
191 float c = p->contrast;
192 float s = p->saturation;
193 float b = p->brightness;
194 float h = p->hue;
195
196 const float *cstd;
197
198 assert(matrix);
199
200 switch (cs) {
201 case VL_CSC_COLOR_STANDARD_BT_601:
202 cstd = full_range ? &bt_601_full[0] : &bt_601[0];
203 break;
204 case VL_CSC_COLOR_STANDARD_BT_709:
205 cstd = full_range ? &bt_709_full[0] : &bt_709[0];
206 break;
207 case VL_CSC_COLOR_STANDARD_SMPTE_240M:
208 cstd = full_range ? &smpte240m_full[0] : &smpte240m[0];
209 break;
210 case VL_CSC_COLOR_STANDARD_IDENTITY:
211 default:
212 assert(cs == VL_CSC_COLOR_STANDARD_IDENTITY);
213 memcpy(matrix, &identity[0], sizeof(float) * 16);
214 return;
215 }
216
217 matrix[ 0] = c*cstd[ 0];
218 matrix[ 1] = c*cstd[ 1]*s*cosf(h) - c*cstd[ 2]*s*sinf(h);
219 matrix[ 2] = c*cstd[ 2]*s*cosf(h) + c*cstd[ 1]*s*sinf(h);
220 matrix[ 3] = cstd[ 3] + cstd[ 0]*(b + c*ybias) + cstd[ 1]*(c*cbbias*s*cosf(h) + c*crbias*s*sinf(h)) + cstd[ 2]*(c*crbias*s*cosf(h) - c*cbbias*s*sinf(h));
221
222 matrix[ 4] = c*cstd[ 4];
223 matrix[ 5] = c*cstd[ 5]*s*cosf(h) - c*cstd[ 6]*s*sinf(h);
224 matrix[ 6] = c*cstd[ 6]*s*cosf(h) + c*cstd[ 5]*s*sinf(h);
225 matrix[ 7] = cstd[ 7] + cstd[ 4]*(b + c*ybias) + cstd[ 5]*(c*cbbias*s*cosf(h) + c*crbias*s*sinf(h)) + cstd[ 6]*(c*crbias*s*cosf(h) - c*cbbias*s*sinf(h));
226
227 matrix[ 8] = c*cstd[ 8];
228 matrix[ 9] = c*cstd[ 9]*s*cosf(h) - c*cstd[10]*s*sinf(h);
229 matrix[10] = c*cstd[10]*s*cosf(h) + c*cstd[ 9]*s*sinf(h);
230 matrix[11] = cstd[11] + cstd[ 8]*(b + c*ybias) + cstd[ 9]*(c*cbbias*s*cosf(h) + c*crbias*s*sinf(h)) + cstd[10]*(c*crbias*s*cosf(h) - c*cbbias*s*sinf(h));
231
232 matrix[12] = c*cstd[12];
233 matrix[13] = c*cstd[13]*s*cos(h) - c*cstd[14]*s*sin(h);
234 matrix[14] = c*cstd[14]*s*cos(h) + c*cstd[13]*s*sin(h);
235 matrix[15] = cstd[15] + cstd[12]*(b + c*ybias) + cstd[13]*(c*cbbias*s*cos(h) + c*crbias*s*sin(h)) + cstd[14]*(c*crbias*s*cos(h) - c*cbbias*s*sin(h));
236 }