1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/base/simd/convert_yuv_to_rgb.h"
6 #include "media/base/simd/yuv_to_rgb_table.h"
10 #define packuswb(x) ((x) < 0 ? 0 : ((x) > 255 ? 255 : (x)))
11 #define paddsw(x, y) (((x) + (y)) < -32768 ? -32768 : \
12 (((x) + (y)) > 32767 ? 32767 : ((x) + (y))))
14 // On Android, pixel layout is RGBA (see skia/include/core/SkColorPriv.h);
15 // however, other Chrome platforms use BGRA (see skia/config/SkUserConfig.h).
16 // Ideally, android should not use the functions here due to performance issue
17 // (http://crbug.com/249980).
18 #if defined(OS_ANDROID)
19 #define SK_R32_SHIFT 0
20 #define SK_G32_SHIFT 8
21 #define SK_B32_SHIFT 16
22 #define SK_A32_SHIFT 24
28 #define SK_B32_SHIFT 0
29 #define SK_G32_SHIFT 8
30 #define SK_R32_SHIFT 16
31 #define SK_A32_SHIFT 24
38 static inline void ConvertYUVToRGB32_C(uint8 y
,
42 const int16 convert_table
[1024][4]) {
43 int b
= convert_table
[256+u
][B_INDEX
];
44 int g
= convert_table
[256+u
][G_INDEX
];
45 int r
= convert_table
[256+u
][R_INDEX
];
46 int a
= convert_table
[256+u
][A_INDEX
];
48 b
= paddsw(b
, convert_table
[512+v
][B_INDEX
]);
49 g
= paddsw(g
, convert_table
[512+v
][G_INDEX
]);
50 r
= paddsw(r
, convert_table
[512+v
][R_INDEX
]);
51 a
= paddsw(a
, convert_table
[512+v
][A_INDEX
]);
53 b
= paddsw(b
, convert_table
[y
][B_INDEX
]);
54 g
= paddsw(g
, convert_table
[y
][G_INDEX
]);
55 r
= paddsw(r
, convert_table
[y
][R_INDEX
]);
56 a
= paddsw(a
, convert_table
[y
][A_INDEX
]);
63 *reinterpret_cast<uint32
*>(rgb_buf
) = (packuswb(b
) << SK_B32_SHIFT
) |
64 (packuswb(g
) << SK_G32_SHIFT
) |
65 (packuswb(r
) << SK_R32_SHIFT
) |
66 (packuswb(a
) << SK_A32_SHIFT
);
69 static inline void ConvertYUVAToARGB_C(uint8 y
,
74 const int16 convert_table
[1024][4]) {
75 int b
= convert_table
[256+u
][0];
76 int g
= convert_table
[256+u
][1];
77 int r
= convert_table
[256+u
][2];
79 b
= paddsw(b
, convert_table
[512+v
][0]);
80 g
= paddsw(g
, convert_table
[512+v
][1]);
81 r
= paddsw(r
, convert_table
[512+v
][2]);
83 b
= paddsw(b
, convert_table
[y
][0]);
84 g
= paddsw(g
, convert_table
[y
][1]);
85 r
= paddsw(r
, convert_table
[y
][2]);
91 b
= packuswb(b
) * a
>> 8;
92 g
= packuswb(g
) * a
>> 8;
93 r
= packuswb(r
) * a
>> 8;
95 *reinterpret_cast<uint32
*>(rgb_buf
) = (b
<< SK_B32_SHIFT
) |
101 void ConvertYUVToRGB32Row_C(const uint8
* y_buf
,
106 const int16 convert_table
[1024][4]) {
107 for (int x
= 0; x
< width
; x
+= 2) {
108 uint8 u
= u_buf
[x
>> 1];
109 uint8 v
= v_buf
[x
>> 1];
111 ConvertYUVToRGB32_C(y0
, u
, v
, rgb_buf
, convert_table
);
112 if ((x
+ 1) < width
) {
113 uint8 y1
= y_buf
[x
+ 1];
114 ConvertYUVToRGB32_C(y1
, u
, v
, rgb_buf
+ 4, convert_table
);
116 rgb_buf
+= 8; // Advance 2 pixels.
120 void ConvertYUVAToARGBRow_C(const uint8
* y_buf
,
126 const int16 convert_table
[1024][4]) {
127 for (int x
= 0; x
< width
; x
+= 2) {
128 uint8 u
= u_buf
[x
>> 1];
129 uint8 v
= v_buf
[x
>> 1];
132 ConvertYUVAToARGB_C(y0
, u
, v
, a0
, rgba_buf
, convert_table
);
133 if ((x
+ 1) < width
) {
134 uint8 y1
= y_buf
[x
+ 1];
135 uint8 a1
= a_buf
[x
+ 1];
136 ConvertYUVAToARGB_C(y1
, u
, v
, a1
, rgba_buf
+ 4, convert_table
);
138 rgba_buf
+= 8; // Advance 2 pixels.
142 // 16.16 fixed point is used. A shift by 16 isolates the integer.
143 // A shift by 17 is used to further subsample the chrominence channels.
144 // & 0xffff isolates the fixed point fraction. >> 2 to get the upper 2 bits,
145 // for 1/65536 pixel accurate interpolation.
146 void ScaleYUVToRGB32Row_C(const uint8
* y_buf
,
152 const int16 convert_table
[1024][4]) {
154 for (int i
= 0; i
< width
; i
+= 2) {
155 int y
= y_buf
[x
>> 16];
156 int u
= u_buf
[(x
>> 17)];
157 int v
= v_buf
[(x
>> 17)];
158 ConvertYUVToRGB32_C(y
, u
, v
, rgb_buf
, convert_table
);
160 if ((i
+ 1) < width
) {
162 ConvertYUVToRGB32_C(y
, u
, v
, rgb_buf
+4, convert_table
);
169 void LinearScaleYUVToRGB32Row_C(const uint8
* y_buf
,
175 const int16 convert_table
[1024][4]) {
176 // Avoid point-sampling for down-scaling by > 2:1.
178 if (source_dx
>= 0x20000)
180 LinearScaleYUVToRGB32RowWithRange_C(y_buf
, u_buf
, v_buf
, rgb_buf
, width
,
181 source_x
, source_dx
, convert_table
);
184 void LinearScaleYUVToRGB32RowWithRange_C(const uint8
* y_buf
,
191 const int16 convert_table
[1024][4]) {
192 for (int i
= 0; i
< dest_width
; i
+= 2) {
193 int y0
= y_buf
[x
>> 16];
194 int y1
= y_buf
[(x
>> 16) + 1];
195 int u0
= u_buf
[(x
>> 17)];
196 int u1
= u_buf
[(x
>> 17) + 1];
197 int v0
= v_buf
[(x
>> 17)];
198 int v1
= v_buf
[(x
>> 17) + 1];
199 int y_frac
= (x
& 65535);
200 int uv_frac
= ((x
>> 1) & 65535);
201 int y
= (y_frac
* y1
+ (y_frac
^ 65535) * y0
) >> 16;
202 int u
= (uv_frac
* u1
+ (uv_frac
^ 65535) * u0
) >> 16;
203 int v
= (uv_frac
* v1
+ (uv_frac
^ 65535) * v0
) >> 16;
204 ConvertYUVToRGB32_C(y
, u
, v
, rgb_buf
, convert_table
);
206 if ((i
+ 1) < dest_width
) {
208 y1
= y_buf
[(x
>> 16) + 1];
209 y_frac
= (x
& 65535);
210 y
= (y_frac
* y1
+ (y_frac
^ 65535) * y0
) >> 16;
211 ConvertYUVToRGB32_C(y
, u
, v
, rgb_buf
+4, convert_table
);
218 void ConvertYUVToRGB32_C(const uint8
* yplane
,
228 unsigned int y_shift
= GetVerticalShift(yuv_type
);
229 for (int y
= 0; y
< height
; ++y
) {
230 uint8
* rgb_row
= rgbframe
+ y
* rgbstride
;
231 const uint8
* y_ptr
= yplane
+ y
* ystride
;
232 const uint8
* u_ptr
= uplane
+ (y
>> y_shift
) * uvstride
;
233 const uint8
* v_ptr
= vplane
+ (y
>> y_shift
) * uvstride
;
235 ConvertYUVToRGB32Row_C(y_ptr
,
240 GetLookupTable(yuv_type
));
244 void ConvertYUVAToARGB_C(const uint8
* yplane
,
256 unsigned int y_shift
= yuv_type
;
257 for (int y
= 0; y
< height
; y
++) {
258 uint8
* rgba_row
= rgbaframe
+ y
* rgbastride
;
259 const uint8
* y_ptr
= yplane
+ y
* ystride
;
260 const uint8
* u_ptr
= uplane
+ (y
>> y_shift
) * uvstride
;
261 const uint8
* v_ptr
= vplane
+ (y
>> y_shift
) * uvstride
;
262 const uint8
* a_ptr
= aplane
+ y
* astride
;
264 ConvertYUVAToARGBRow_C(y_ptr
,
270 GetLookupTable(yuv_type
));