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"
9 #define packuswb(x) ((x) < 0 ? 0 : ((x) > 255 ? 255 : (x)))
10 #define paddsw(x, y) (((x) + (y)) < -32768 ? -32768 : \
11 (((x) + (y)) > 32767 ? 32767 : ((x) + (y))))
13 // On Android, pixel layout is RGBA (see skia/include/core/SkColorPriv.h);
14 // however, other Chrome platforms use BGRA (see skia/config/SkUserConfig.h).
15 // Ideally, android should not use the functions here due to performance issue
16 // (http://crbug.com/249980).
17 #if defined(OS_ANDROID)
18 #define SK_R32_SHIFT 0
19 #define SK_G32_SHIFT 8
20 #define SK_B32_SHIFT 16
21 #define SK_A32_SHIFT 24
27 #define SK_B32_SHIFT 0
28 #define SK_G32_SHIFT 8
29 #define SK_R32_SHIFT 16
30 #define SK_A32_SHIFT 24
37 static inline void ConvertYUVToRGB32_C(uint8 y
,
41 const int16
* convert_table
) {
42 int b
= convert_table
[4 * (256 + u
) + B_INDEX
];
43 int g
= convert_table
[4 * (256 + u
) + G_INDEX
];
44 int r
= convert_table
[4 * (256 + u
) + R_INDEX
];
45 int a
= convert_table
[4 * (256 + u
) + A_INDEX
];
47 b
= paddsw(b
, convert_table
[4 * (512 + v
) + B_INDEX
]);
48 g
= paddsw(g
, convert_table
[4 * (512 + v
) + G_INDEX
]);
49 r
= paddsw(r
, convert_table
[4 * (512 + v
) + R_INDEX
]);
50 a
= paddsw(a
, convert_table
[4 * (512 + v
) + A_INDEX
]);
52 b
= paddsw(b
, convert_table
[4 * y
+ B_INDEX
]);
53 g
= paddsw(g
, convert_table
[4 * y
+ G_INDEX
]);
54 r
= paddsw(r
, convert_table
[4 * y
+ R_INDEX
]);
55 a
= paddsw(a
, convert_table
[4 * y
+ A_INDEX
]);
62 *reinterpret_cast<uint32
*>(rgb_buf
) = (packuswb(b
) << SK_B32_SHIFT
) |
63 (packuswb(g
) << SK_G32_SHIFT
) |
64 (packuswb(r
) << SK_R32_SHIFT
) |
65 (packuswb(a
) << SK_A32_SHIFT
);
68 static inline void ConvertYUVAToARGB_C(uint8 y
,
73 const int16
* convert_table
) {
74 int b
= convert_table
[4 * (256 + u
) + 0];
75 int g
= convert_table
[4 * (256 + u
) + 1];
76 int r
= convert_table
[4 * (256 + u
) + 2];
78 b
= paddsw(b
, convert_table
[4 * (512 + v
) + 0]);
79 g
= paddsw(g
, convert_table
[4 * (512 + v
) + 1]);
80 r
= paddsw(r
, convert_table
[4 * (512 + v
) + 2]);
82 b
= paddsw(b
, convert_table
[4 * y
+ 0]);
83 g
= paddsw(g
, convert_table
[4 * y
+ 1]);
84 r
= paddsw(r
, convert_table
[4 * y
+ 2]);
90 b
= packuswb(b
) * a
>> 8;
91 g
= packuswb(g
) * a
>> 8;
92 r
= packuswb(r
) * a
>> 8;
94 *reinterpret_cast<uint32
*>(rgb_buf
) = (b
<< SK_B32_SHIFT
) |
100 void ConvertYUVToRGB32Row_C(const uint8
* y_buf
,
105 const int16
* convert_table
) {
106 for (int x
= 0; x
< width
; x
+= 2) {
107 uint8 u
= u_buf
[x
>> 1];
108 uint8 v
= v_buf
[x
>> 1];
110 ConvertYUVToRGB32_C(y0
, u
, v
, rgb_buf
, convert_table
);
111 if ((x
+ 1) < width
) {
112 uint8 y1
= y_buf
[x
+ 1];
113 ConvertYUVToRGB32_C(y1
, u
, v
, rgb_buf
+ 4, convert_table
);
115 rgb_buf
+= 8; // Advance 2 pixels.
119 void ConvertYUVAToARGBRow_C(const uint8
* y_buf
,
125 const int16
* convert_table
) {
126 for (int x
= 0; x
< width
; x
+= 2) {
127 uint8 u
= u_buf
[x
>> 1];
128 uint8 v
= v_buf
[x
>> 1];
131 ConvertYUVAToARGB_C(y0
, u
, v
, a0
, rgba_buf
, convert_table
);
132 if ((x
+ 1) < width
) {
133 uint8 y1
= y_buf
[x
+ 1];
134 uint8 a1
= a_buf
[x
+ 1];
135 ConvertYUVAToARGB_C(y1
, u
, v
, a1
, rgba_buf
+ 4, convert_table
);
137 rgba_buf
+= 8; // Advance 2 pixels.
141 // 16.16 fixed point is used. A shift by 16 isolates the integer.
142 // A shift by 17 is used to further subsample the chrominence channels.
143 // & 0xffff isolates the fixed point fraction. >> 2 to get the upper 2 bits,
144 // for 1/65536 pixel accurate interpolation.
145 void ScaleYUVToRGB32Row_C(const uint8
* y_buf
,
151 const int16
* convert_table
) {
153 for (int i
= 0; i
< width
; i
+= 2) {
154 int y
= y_buf
[x
>> 16];
155 int u
= u_buf
[(x
>> 17)];
156 int v
= v_buf
[(x
>> 17)];
157 ConvertYUVToRGB32_C(y
, u
, v
, rgb_buf
, convert_table
);
159 if ((i
+ 1) < width
) {
161 ConvertYUVToRGB32_C(y
, u
, v
, rgb_buf
+4, convert_table
);
168 void LinearScaleYUVToRGB32Row_C(const uint8
* y_buf
,
174 const int16
* convert_table
) {
175 // Avoid point-sampling for down-scaling by > 2:1.
177 if (source_dx
>= 0x20000)
179 LinearScaleYUVToRGB32RowWithRange_C(y_buf
, u_buf
, v_buf
, rgb_buf
, width
,
180 source_x
, source_dx
, convert_table
);
183 void LinearScaleYUVToRGB32RowWithRange_C(const uint8
* y_buf
,
190 const int16
* convert_table
) {
191 for (int i
= 0; i
< dest_width
; i
+= 2) {
192 int y0
= y_buf
[x
>> 16];
193 int y1
= y_buf
[(x
>> 16) + 1];
194 int u0
= u_buf
[(x
>> 17)];
195 int u1
= u_buf
[(x
>> 17) + 1];
196 int v0
= v_buf
[(x
>> 17)];
197 int v1
= v_buf
[(x
>> 17) + 1];
198 int y_frac
= (x
& 65535);
199 int uv_frac
= ((x
>> 1) & 65535);
200 int y
= (y_frac
* y1
+ (y_frac
^ 65535) * y0
) >> 16;
201 int u
= (uv_frac
* u1
+ (uv_frac
^ 65535) * u0
) >> 16;
202 int v
= (uv_frac
* v1
+ (uv_frac
^ 65535) * v0
) >> 16;
203 ConvertYUVToRGB32_C(y
, u
, v
, rgb_buf
, convert_table
);
205 if ((i
+ 1) < dest_width
) {
207 y1
= y_buf
[(x
>> 16) + 1];
208 y_frac
= (x
& 65535);
209 y
= (y_frac
* y1
+ (y_frac
^ 65535) * y0
) >> 16;
210 ConvertYUVToRGB32_C(y
, u
, v
, rgb_buf
+4, convert_table
);
217 void ConvertYUVToRGB32_C(const uint8
* yplane
,
227 unsigned int y_shift
= GetVerticalShift(yuv_type
);
228 const int16
* lookup_table
= GetLookupTable(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
,
244 void ConvertYUVAToARGB_C(const uint8
* yplane
,
256 unsigned int y_shift
= GetVerticalShift(yuv_type
);
257 const int16
* lookup_table
= GetLookupTable(yuv_type
);
258 for (int y
= 0; y
< height
; y
++) {
259 uint8
* rgba_row
= rgbaframe
+ y
* rgbastride
;
260 const uint8
* y_ptr
= yplane
+ y
* ystride
;
261 const uint8
* u_ptr
= uplane
+ (y
>> y_shift
) * uvstride
;
262 const uint8
* v_ptr
= vplane
+ (y
>> y_shift
) * uvstride
;
263 const uint8
* a_ptr
= aplane
+ y
* astride
;
265 ConvertYUVAToARGBRow_C(y_ptr
,