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"
38 #include "util/u_memory.h"
39 #include "util/u_math.h"
40 #include "util/u_string.h"
42 #include "lp_bld_arit.h"
43 #include "lp_bld_init.h"
44 #include "lp_bld_type.h"
45 #include "lp_bld_const.h"
46 #include "lp_bld_conv.h"
47 #include "lp_bld_gather.h"
48 #include "lp_bld_format.h"
52 * Extract Y, U, V channels from packed UYVY.
53 * @param packed is a <n x i32> vector with the packed UYVY blocks
54 * @param i is a <n x i32> vector with the x pixel coordinate (0 or 1)
57 uyvy_to_yuv_soa(LLVMBuilderRef builder
,
66 LLVMValueRef shift
, mask
;
68 memset(&type
, 0, sizeof type
);
72 assert(lp_check_value(type
, packed
));
73 assert(lp_check_value(type
, i
));
76 * y = (uyvy >> 16*i) & 0xff
78 * v = (uyvy >> 16 ) & 0xff
81 shift
= LLVMBuildMul(builder
, i
, lp_build_const_int_vec(type
, 16), "");
82 shift
= LLVMBuildAdd(builder
, shift
, lp_build_const_int_vec(type
, 8), "");
83 *y
= LLVMBuildLShr(builder
, packed
, shift
, "");
85 *v
= LLVMBuildLShr(builder
, packed
, lp_build_const_int_vec(type
, 16), "");
87 mask
= lp_build_const_int_vec(type
, 0xff);
89 *y
= LLVMBuildAnd(builder
, *y
, mask
, "y");
90 *u
= LLVMBuildAnd(builder
, *u
, mask
, "u");
91 *v
= LLVMBuildAnd(builder
, *v
, mask
, "v");
96 * Extract Y, U, V channels from packed YUYV.
97 * @param packed is a <n x i32> vector with the packed YUYV blocks
98 * @param i is a <n x i32> vector with the x pixel coordinate (0 or 1)
101 yuyv_to_yuv_soa(LLVMBuilderRef builder
,
110 LLVMValueRef shift
, mask
;
112 memset(&type
, 0, sizeof type
);
116 assert(lp_check_value(type
, packed
));
117 assert(lp_check_value(type
, i
));
120 * y = (yuyv >> 16*i) & 0xff
121 * u = (yuyv >> 8 ) & 0xff
122 * v = (yuyv >> 24 ) & 0xff
125 shift
= LLVMBuildMul(builder
, i
, lp_build_const_int_vec(type
, 16), "");
126 *y
= LLVMBuildLShr(builder
, packed
, shift
, "");
127 *u
= LLVMBuildLShr(builder
, packed
, lp_build_const_int_vec(type
, 8), "");
128 *v
= LLVMBuildLShr(builder
, packed
, lp_build_const_int_vec(type
, 24), "");
130 mask
= lp_build_const_int_vec(type
, 0xff);
132 *y
= LLVMBuildAnd(builder
, *y
, mask
, "y");
133 *u
= LLVMBuildAnd(builder
, *u
, mask
, "u");
134 *v
= LLVMBuildAnd(builder
, *v
, mask
, "v");
139 yuv_to_rgb_soa(LLVMBuilderRef builder
,
141 LLVMValueRef y
, LLVMValueRef u
, LLVMValueRef v
,
142 LLVMValueRef
*r
, LLVMValueRef
*g
, LLVMValueRef
*b
)
145 struct lp_build_context bld
;
159 memset(&type
, 0, sizeof type
);
164 lp_build_context_init(&bld
, builder
, type
);
166 assert(lp_check_value(type
, y
));
167 assert(lp_check_value(type
, u
));
168 assert(lp_check_value(type
, v
));
174 c0
= lp_build_const_int_vec(type
, 0);
175 c8
= lp_build_const_int_vec(type
, 8);
176 c16
= lp_build_const_int_vec(type
, 16);
177 c128
= lp_build_const_int_vec(type
, 128);
178 c255
= lp_build_const_int_vec(type
, 255);
180 cy
= lp_build_const_int_vec(type
, 298);
181 cug
= lp_build_const_int_vec(type
, -100);
182 cub
= lp_build_const_int_vec(type
, 516);
183 cvr
= lp_build_const_int_vec(type
, 409);
184 cvg
= lp_build_const_int_vec(type
, -208);
192 y
= LLVMBuildSub(builder
, y
, c16
, "");
193 u
= LLVMBuildSub(builder
, u
, c128
, "");
194 v
= LLVMBuildSub(builder
, v
, c128
, "");
197 * r = 298 * _y + 409 * _v + 128;
198 * g = 298 * _y - 100 * _u - 208 * _v + 128;
199 * b = 298 * _y + 516 * _u + 128;
202 y
= LLVMBuildMul(builder
, y
, cy
, "");
203 y
= LLVMBuildAdd(builder
, y
, c128
, "");
205 *r
= LLVMBuildMul(builder
, v
, cvr
, "");
206 *g
= LLVMBuildAdd(builder
,
207 LLVMBuildMul(builder
, u
, cug
, ""),
208 LLVMBuildMul(builder
, v
, cvg
, ""),
210 *b
= LLVMBuildMul(builder
, u
, cub
, "");
212 *r
= LLVMBuildAdd(builder
, *r
, y
, "");
213 *g
= LLVMBuildAdd(builder
, *g
, y
, "");
214 *b
= LLVMBuildAdd(builder
, *b
, y
, "");
222 *r
= LLVMBuildAShr(builder
, *r
, c8
, "r");
223 *g
= LLVMBuildAShr(builder
, *g
, c8
, "g");
224 *b
= LLVMBuildAShr(builder
, *b
, c8
, "b");
230 *r
= lp_build_clamp(&bld
, *r
, c0
, c255
);
231 *g
= lp_build_clamp(&bld
, *g
, c0
, c255
);
232 *b
= lp_build_clamp(&bld
, *b
, c0
, c255
);
237 rgb_to_rgba_aos(LLVMBuilderRef builder
,
239 LLVMValueRef r
, LLVMValueRef g
, LLVMValueRef b
)
245 memset(&type
, 0, sizeof type
);
250 assert(lp_check_value(type
, r
));
251 assert(lp_check_value(type
, g
));
252 assert(lp_check_value(type
, b
));
255 * Make a 4 x unorm8 vector
259 g
= LLVMBuildShl(builder
, g
, lp_build_const_int_vec(type
, 8), "");
260 b
= LLVMBuildShl(builder
, b
, lp_build_const_int_vec(type
, 16), "");
261 a
= lp_build_const_int_vec(type
, 0xff000000);
264 rgba
= LLVMBuildOr(builder
, rgba
, g
, "");
265 rgba
= LLVMBuildOr(builder
, rgba
, b
, "");
266 rgba
= LLVMBuildOr(builder
, rgba
, a
, "");
268 rgba
= LLVMBuildBitCast(builder
, rgba
,
269 LLVMVectorType(LLVMInt8Type(), 4*n
), "");
276 * Convert from <n x i32> packed UYVY to <4n x i8> RGBA AoS
279 uyvy_to_rgba_aos(LLVMBuilderRef builder
,
284 LLVMValueRef y
, u
, v
;
285 LLVMValueRef r
, g
, b
;
288 uyvy_to_yuv_soa(builder
, n
, packed
, i
, &y
, &u
, &v
);
289 yuv_to_rgb_soa(builder
, n
, y
, u
, v
, &r
, &g
, &b
);
290 rgba
= rgb_to_rgba_aos(builder
, n
, r
, g
, b
);
297 * Convert from <n x i32> packed YUYV to <4n x i8> RGBA AoS
300 yuyv_to_rgba_aos(LLVMBuilderRef builder
,
305 LLVMValueRef y
, u
, v
;
306 LLVMValueRef r
, g
, b
;
309 yuyv_to_yuv_soa(builder
, n
, packed
, i
, &y
, &u
, &v
);
310 yuv_to_rgb_soa(builder
, n
, y
, u
, v
, &r
, &g
, &b
);
311 rgba
= rgb_to_rgba_aos(builder
, n
, r
, g
, b
);
318 * Convert from <n x i32> packed RG_BG to <4n x i8> RGBA AoS
321 rgbg_to_rgba_aos(LLVMBuilderRef builder
,
326 LLVMValueRef r
, g
, b
;
329 uyvy_to_yuv_soa(builder
, n
, packed
, i
, &g
, &r
, &b
);
330 rgba
= rgb_to_rgba_aos(builder
, n
, r
, g
, b
);
337 * Convert from <n x i32> packed GR_GB to <4n x i8> RGBA AoS
340 grgb_to_rgba_aos(LLVMBuilderRef builder
,
345 LLVMValueRef r
, g
, b
;
348 yuyv_to_yuv_soa(builder
, n
, packed
, i
, &g
, &r
, &b
);
349 rgba
= rgb_to_rgba_aos(builder
, n
, r
, g
, b
);
356 * @param n is the number of pixels processed
357 * @param packed is a <n x i32> vector with the packed YUYV blocks
358 * @param i is a <n x i32> vector with the x pixel coordinate (0 or 1)
359 * @return a <4*n x i8> vector with the pixel RGBA values in AoS
362 lp_build_unpack_subsampled_to_rgba_aos(LLVMBuilderRef builder
,
363 const struct util_format_description
*format_desc
,
371 assert(format_desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
);
375 switch (format_desc
->format
) {
376 case PIPE_FORMAT_UYVY
:
377 rgba
= uyvy_to_rgba_aos(builder
, n
, packed
, i
);
379 case PIPE_FORMAT_YUYV
:
380 rgba
= yuyv_to_rgba_aos(builder
, n
, packed
, i
);
382 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
383 rgba
= rgbg_to_rgba_aos(builder
, n
, packed
, i
);
385 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
386 rgba
= grgb_to_rgba_aos(builder
, n
, packed
, i
);
390 rgba
= LLVMGetUndef(LLVMVectorType(LLVMInt8Type(), 4*n
));