5ecc43a5fa3e2110d3302169c050ed14cdae737f
[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 "vl_csc.h"
29 #include <util/u_math.h>
30 #include <util/u_debug.h>
31
32 /*
33 * Color space conversion formulas
34 *
35 * To convert YCbCr to RGB,
36 * vec4 ycbcr, rgb
37 * mat44 csc
38 * rgb = csc * ycbcr
39 *
40 * To calculate the color space conversion matrix csc with ProcAmp adjustments,
41 * mat44 csc, cstd, procamp, bias
42 * csc = cstd * (procamp * bias)
43 *
44 * Where cstd is a matrix corresponding to one of the color standards (BT.601, BT.709, etc)
45 * adjusted for the kind of YCbCr -> RGB mapping wanted (1:1, full),
46 * bias is a matrix corresponding to the kind of YCbCr -> RGB mapping wanted (1:1, full)
47 *
48 * To calculate procamp,
49 * mat44 procamp, hue, saturation, brightness, contrast
50 * procamp = brightness * (saturation * (contrast * hue))
51 * Alternatively,
52 * procamp = saturation * (brightness * (contrast * hue))
53 *
54 * contrast
55 * [ c, 0, 0, 0]
56 * [ 0, c, 0, 0]
57 * [ 0, 0, c, 0]
58 * [ 0, 0, 0, 1]
59 *
60 * brightness
61 * [ 1, 0, 0, b]
62 * [ 0, 1, 0, 0]
63 * [ 0, 0, 1, 0]
64 * [ 0, 0, 0, 1]
65 *
66 * saturation
67 * [ 1, 0, 0, 0]
68 * [ 0, s, 0, 0]
69 * [ 0, 0, s, 0]
70 * [ 0, 0, 0, 1]
71 *
72 * hue
73 * [ 1, 0, 0, 0]
74 * [ 0, cos(h), sin(h), 0]
75 * [ 0, -sin(h), cos(h), 0]
76 * [ 0, 0, 0, 1]
77 *
78 * procamp
79 * [ c, 0, 0, b]
80 * [ 0, c*s*cos(h), c*s*sin(h), 0]
81 * [ 0, -c*s*sin(h), c*s*cos(h), 0]
82 * [ 0, 0, 0, 1]
83 *
84 * bias
85 * [ 1, 0, 0, ybias]
86 * [ 0, 1, 0, cbbias]
87 * [ 0, 0, 1, crbias]
88 * [ 0, 0, 0, 1]
89 *
90 * csc
91 * [ 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))]
92 * [ 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))]
93 * [ 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))]
94 * [ 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))]
95 */
96
97 /*
98 * Converts ITU-R BT.601 YCbCr pixels to RGB pixels where:
99 * Y is in [16,235], Cb and Cr are in [16,240]
100 * R, G, and B are in [16,235]
101 */
102 static const float bt_601[16] =
103 {
104 1.0f, 0.0f, 1.371f, 0.0f,
105 1.0f, -0.336f, -0.698f, 0.0f,
106 1.0f, 1.732f, 0.0f, 0.0f,
107 0.0f, 0.0f, 0.0f, 1.0f
108 };
109
110 /*
111 * Converts ITU-R BT.601 YCbCr pixels to RGB pixels where:
112 * Y is in [16,235], Cb and Cr are in [16,240]
113 * R, G, and B are in [0,255]
114 */
115 static const float bt_601_full[16] =
116 {
117 1.164f, 0.0f, 1.596f, 0.0f,
118 1.164f, -0.391f, -0.813f, 0.0f,
119 1.164f, 2.018f, 0.0f, 0.0f,
120 0.0f, 0.0f, 0.0f, 1.0f
121 };
122
123 /*
124 * Converts ITU-R BT.709 YCbCr pixels to RGB pixels where:
125 * Y is in [16,235], Cb and Cr are in [16,240]
126 * R, G, and B are in [16,235]
127 */
128 static const float bt_709[16] =
129 {
130 1.0f, 0.0f, 1.540f, 0.0f,
131 1.0f, -0.183f, -0.459f, 0.0f,
132 1.0f, 1.816f, 0.0f, 0.0f,
133 0.0f, 0.0f, 0.0f, 1.0f
134 };
135
136 /*
137 * Converts ITU-R BT.709 YCbCr pixels to RGB pixels where:
138 * Y is in [16,235], Cb and Cr are in [16,240]
139 * R, G, and B are in [0,255]
140 */
141 static const float bt_709_full[16] =
142 {
143 1.164f, 0.0f, 1.793f, 0.0f,
144 1.164f, -0.213f, -0.534f, 0.0f,
145 1.164f, 2.115f, 0.0f, 0.0f,
146 0.0f, 0.0f, 0.0f, 1.0f
147 };
148
149 static const float identity[16] =
150 {
151 1.0f, 0.0f, 0.0f, 0.0f,
152 0.0f, 1.0f, 0.0f, 0.0f,
153 0.0f, 0.0f, 1.0f, 0.0f,
154 0.0f, 0.0f, 0.0f, 1.0f
155 };
156
157 void vl_csc_get_matrix(enum VL_CSC_COLOR_STANDARD cs,
158 struct vl_procamp *procamp,
159 bool full_range,
160 float *matrix)
161 {
162 float ybias = full_range ? -16.0f/255.0f : 0.0f;
163 float cbbias = -128.0f/255.0f;
164 float crbias = -128.0f/255.0f;
165 float c = procamp ? procamp->contrast : 1.0f;
166 float s = procamp ? procamp->saturation : 1.0f;
167 float b = procamp ? procamp->brightness : 0.0f;
168 float h = procamp ? procamp->hue : 0.0f;
169 const float *cstd;
170
171 assert(matrix);
172
173 switch (cs) {
174 case VL_CSC_COLOR_STANDARD_BT_601:
175 cstd = full_range ? &bt_601_full[0] : &bt_601[0];
176 break;
177 case VL_CSC_COLOR_STANDARD_BT_709:
178 cstd = full_range ? &bt_709_full[0] : &bt_709[0];
179 break;
180 case VL_CSC_COLOR_STANDARD_IDENTITY:
181 default:
182 assert(cs == VL_CSC_COLOR_STANDARD_IDENTITY);
183 memcpy(matrix, &identity[0], sizeof(float) * 16);
184 return;
185 }
186
187 matrix[ 0] = c*cstd[ 0];
188 matrix[ 1] = c*cstd[ 1]*s*cosf(h) - c*cstd[ 2]*s*sinf(h);
189 matrix[ 2] = c*cstd[ 2]*s*cosf(h) + c*cstd[ 1]*s*sinf(h);
190 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));
191
192 matrix[ 4] = c*cstd[ 4];
193 matrix[ 5] = c*cstd[ 5]*s*cosf(h) - c*cstd[ 6]*s*sinf(h);
194 matrix[ 6] = c*cstd[ 6]*s*cosf(h) + c*cstd[ 5]*s*sinf(h);
195 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));
196
197 matrix[ 8] = c*cstd[ 8];
198 matrix[ 9] = c*cstd[ 9]*s*cosf(h) - c*cstd[10]*s*sinf(h);
199 matrix[10] = c*cstd[10]*s*cosf(h) + c*cstd[ 9]*s*sinf(h);
200 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));
201
202 matrix[12] = c*cstd[12];
203 matrix[13] = c*cstd[13]*s*cos(h) - c*cstd[14]*s*sin(h);
204 matrix[14] = c*cstd[14]*s*cos(h) + c*cstd[13]*s*sin(h);
205 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));
206 }