1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
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:
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 **************************************************************************/
31 * YUV pixel format manipulation.
33 * @author Jose Fonseca <jfonseca@vmware.com>
37 #include "util/u_format.h"
39 #include "lp_bld_arit.h"
40 #include "lp_bld_init.h"
41 #include "lp_bld_type.h"
42 #include "lp_bld_const.h"
43 #include "lp_bld_conv.h"
44 #include "lp_bld_gather.h"
45 #include "lp_bld_format.h"
49 * Extract Y, U, V channels from packed UYVY.
50 * @param packed is a <n x i32> vector with the packed UYVY blocks
51 * @param i is a <n x i32> vector with the x pixel coordinate (0 or 1)
54 uyvy_to_yuv_soa(LLVMBuilderRef builder
,
63 LLVMValueRef shift
, mask
;
65 memset(&type
, 0, sizeof type
);
69 assert(lp_check_value(type
, packed
));
70 assert(lp_check_value(type
, i
));
73 * y = (uyvy >> 16*i) & 0xff
75 * v = (uyvy >> 16 ) & 0xff
78 shift
= LLVMBuildMul(builder
, i
, lp_build_const_int_vec(type
, 16), "");
79 shift
= LLVMBuildAdd(builder
, shift
, lp_build_const_int_vec(type
, 8), "");
80 *y
= LLVMBuildLShr(builder
, packed
, shift
, "");
82 *v
= LLVMBuildLShr(builder
, packed
, lp_build_const_int_vec(type
, 16), "");
84 mask
= lp_build_const_int_vec(type
, 0xff);
86 *y
= LLVMBuildAnd(builder
, *y
, mask
, "y");
87 *u
= LLVMBuildAnd(builder
, *u
, mask
, "u");
88 *v
= LLVMBuildAnd(builder
, *v
, mask
, "v");
93 * Extract Y, U, V channels from packed YUYV.
94 * @param packed is a <n x i32> vector with the packed YUYV blocks
95 * @param i is a <n x i32> vector with the x pixel coordinate (0 or 1)
98 yuyv_to_yuv_soa(LLVMBuilderRef builder
,
107 LLVMValueRef shift
, mask
;
109 memset(&type
, 0, sizeof type
);
113 assert(lp_check_value(type
, packed
));
114 assert(lp_check_value(type
, i
));
117 * y = (yuyv >> 16*i) & 0xff
118 * u = (yuyv >> 8 ) & 0xff
119 * v = (yuyv >> 24 ) & 0xff
122 shift
= LLVMBuildMul(builder
, i
, lp_build_const_int_vec(type
, 16), "");
123 *y
= LLVMBuildLShr(builder
, packed
, shift
, "");
124 *u
= LLVMBuildLShr(builder
, packed
, lp_build_const_int_vec(type
, 8), "");
125 *v
= LLVMBuildLShr(builder
, packed
, lp_build_const_int_vec(type
, 24), "");
127 mask
= lp_build_const_int_vec(type
, 0xff);
129 *y
= LLVMBuildAnd(builder
, *y
, mask
, "y");
130 *u
= LLVMBuildAnd(builder
, *u
, mask
, "u");
131 *v
= LLVMBuildAnd(builder
, *v
, mask
, "v");
136 yuv_to_rgb_soa(LLVMBuilderRef builder
,
138 LLVMValueRef y
, LLVMValueRef u
, LLVMValueRef v
,
139 LLVMValueRef
*r
, LLVMValueRef
*g
, LLVMValueRef
*b
)
142 struct lp_build_context bld
;
156 memset(&type
, 0, sizeof type
);
161 lp_build_context_init(&bld
, builder
, type
);
163 assert(lp_check_value(type
, y
));
164 assert(lp_check_value(type
, u
));
165 assert(lp_check_value(type
, v
));
171 c0
= lp_build_const_int_vec(type
, 0);
172 c8
= lp_build_const_int_vec(type
, 8);
173 c16
= lp_build_const_int_vec(type
, 16);
174 c128
= lp_build_const_int_vec(type
, 128);
175 c255
= lp_build_const_int_vec(type
, 255);
177 cy
= lp_build_const_int_vec(type
, 298);
178 cug
= lp_build_const_int_vec(type
, -100);
179 cub
= lp_build_const_int_vec(type
, 516);
180 cvr
= lp_build_const_int_vec(type
, 409);
181 cvg
= lp_build_const_int_vec(type
, -208);
189 y
= LLVMBuildSub(builder
, y
, c16
, "");
190 u
= LLVMBuildSub(builder
, u
, c128
, "");
191 v
= LLVMBuildSub(builder
, v
, c128
, "");
194 * r = 298 * _y + 409 * _v + 128;
195 * g = 298 * _y - 100 * _u - 208 * _v + 128;
196 * b = 298 * _y + 516 * _u + 128;
199 y
= LLVMBuildMul(builder
, y
, cy
, "");
200 y
= LLVMBuildAdd(builder
, y
, c128
, "");
202 *r
= LLVMBuildMul(builder
, v
, cvr
, "");
203 *g
= LLVMBuildAdd(builder
,
204 LLVMBuildMul(builder
, u
, cug
, ""),
205 LLVMBuildMul(builder
, v
, cvg
, ""),
207 *b
= LLVMBuildMul(builder
, u
, cub
, "");
209 *r
= LLVMBuildAdd(builder
, *r
, y
, "");
210 *g
= LLVMBuildAdd(builder
, *g
, y
, "");
211 *b
= LLVMBuildAdd(builder
, *b
, y
, "");
219 *r
= LLVMBuildAShr(builder
, *r
, c8
, "r");
220 *g
= LLVMBuildAShr(builder
, *g
, c8
, "g");
221 *b
= LLVMBuildAShr(builder
, *b
, c8
, "b");
227 *r
= lp_build_clamp(&bld
, *r
, c0
, c255
);
228 *g
= lp_build_clamp(&bld
, *g
, c0
, c255
);
229 *b
= lp_build_clamp(&bld
, *b
, c0
, c255
);
234 rgb_to_rgba_aos(LLVMBuilderRef builder
,
236 LLVMValueRef r
, LLVMValueRef g
, LLVMValueRef b
)
242 memset(&type
, 0, sizeof type
);
247 assert(lp_check_value(type
, r
));
248 assert(lp_check_value(type
, g
));
249 assert(lp_check_value(type
, b
));
252 * Make a 4 x unorm8 vector
256 g
= LLVMBuildShl(builder
, g
, lp_build_const_int_vec(type
, 8), "");
257 b
= LLVMBuildShl(builder
, b
, lp_build_const_int_vec(type
, 16), "");
258 a
= lp_build_const_int_vec(type
, 0xff000000);
261 rgba
= LLVMBuildOr(builder
, rgba
, g
, "");
262 rgba
= LLVMBuildOr(builder
, rgba
, b
, "");
263 rgba
= LLVMBuildOr(builder
, rgba
, a
, "");
265 rgba
= LLVMBuildBitCast(builder
, rgba
,
266 LLVMVectorType(LLVMInt8Type(), 4*n
), "");
273 * Convert from <n x i32> packed UYVY to <4n x i8> RGBA AoS
276 uyvy_to_rgba_aos(LLVMBuilderRef builder
,
281 LLVMValueRef y
, u
, v
;
282 LLVMValueRef r
, g
, b
;
285 uyvy_to_yuv_soa(builder
, n
, packed
, i
, &y
, &u
, &v
);
286 yuv_to_rgb_soa(builder
, n
, y
, u
, v
, &r
, &g
, &b
);
287 rgba
= rgb_to_rgba_aos(builder
, n
, r
, g
, b
);
294 * Convert from <n x i32> packed YUYV to <4n x i8> RGBA AoS
297 yuyv_to_rgba_aos(LLVMBuilderRef builder
,
302 LLVMValueRef y
, u
, v
;
303 LLVMValueRef r
, g
, b
;
306 yuyv_to_yuv_soa(builder
, n
, packed
, i
, &y
, &u
, &v
);
307 yuv_to_rgb_soa(builder
, n
, y
, u
, v
, &r
, &g
, &b
);
308 rgba
= rgb_to_rgba_aos(builder
, n
, r
, g
, b
);
315 * Convert from <n x i32> packed RG_BG to <4n x i8> RGBA AoS
318 rgbg_to_rgba_aos(LLVMBuilderRef builder
,
323 LLVMValueRef r
, g
, b
;
326 uyvy_to_yuv_soa(builder
, n
, packed
, i
, &g
, &r
, &b
);
327 rgba
= rgb_to_rgba_aos(builder
, n
, r
, g
, b
);
334 * Convert from <n x i32> packed GR_GB to <4n x i8> RGBA AoS
337 grgb_to_rgba_aos(LLVMBuilderRef builder
,
342 LLVMValueRef r
, g
, b
;
345 yuyv_to_yuv_soa(builder
, n
, packed
, i
, &g
, &r
, &b
);
346 rgba
= rgb_to_rgba_aos(builder
, n
, r
, g
, b
);
353 * @param n is the number of pixels processed
354 * @param packed is a <n x i32> vector with the packed YUYV blocks
355 * @param i is a <n x i32> vector with the x pixel coordinate (0 or 1)
356 * @return a <4*n x i8> vector with the pixel RGBA values in AoS
359 lp_build_fetch_subsampled_rgba_aos(LLVMBuilderRef builder
,
360 const struct util_format_description
*format_desc
,
362 LLVMValueRef base_ptr
,
370 assert(format_desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
);
371 assert(format_desc
->block
.bits
== 32);
372 assert(format_desc
->block
.width
== 2);
373 assert(format_desc
->block
.height
== 1);
375 packed
= lp_build_gather(builder
, n
, 32, 32, base_ptr
, offset
);
379 switch (format_desc
->format
) {
380 case PIPE_FORMAT_UYVY
:
381 rgba
= uyvy_to_rgba_aos(builder
, n
, packed
, i
);
383 case PIPE_FORMAT_YUYV
:
384 rgba
= yuyv_to_rgba_aos(builder
, n
, packed
, i
);
386 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
387 rgba
= rgbg_to_rgba_aos(builder
, n
, packed
, i
);
389 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
390 rgba
= grgb_to_rgba_aos(builder
, n
, packed
, i
);
394 rgba
= LLVMGetUndef(LLVMVectorType(LLVMInt8Type(), 4*n
));