1 // Copyright 2014 Google Inc. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
9 //
10 // YUV->RGB conversion functions
11 //
12 // Author: Skal (pascal.massimino@gmail.com)
13
14 #include "src/dsp/yuv.h"
15
16 #if defined(WEBP_USE_SSE2)
17
18 #include <stdlib.h>
19 #include <emmintrin.h>
20
21 #include "src/dsp/common_sse2.h"
22 #include "src/utils/utils.h"
23
24 //-----------------------------------------------------------------------------
25 // Convert spans of 32 pixels to various RGB formats for the fancy upsampler.
26
27 // These constants are 14b fixed-point version of ITU-R BT.601 constants.
28 // R = (19077 * y + 26149 * v - 14234) >> 6
29 // G = (19077 * y - 6419 * u - 13320 * v + 8708) >> 6
30 // B = (19077 * y + 33050 * u - 17685) >> 6
ConvertYUV444ToRGB_SSE2(const __m128i * const Y0,const __m128i * const U0,const __m128i * const V0,__m128i * const R,__m128i * const G,__m128i * const B)31 static void ConvertYUV444ToRGB_SSE2(const __m128i* const Y0,
32 const __m128i* const U0,
33 const __m128i* const V0,
34 __m128i* const R,
35 __m128i* const G,
36 __m128i* const B) {
37 const __m128i k19077 = _mm_set1_epi16(19077);
38 const __m128i k26149 = _mm_set1_epi16(26149);
39 const __m128i k14234 = _mm_set1_epi16(14234);
40 // 33050 doesn't fit in a signed short: only use this with unsigned arithmetic
41 const __m128i k33050 = _mm_set1_epi16((short)33050);
42 const __m128i k17685 = _mm_set1_epi16(17685);
43 const __m128i k6419 = _mm_set1_epi16(6419);
44 const __m128i k13320 = _mm_set1_epi16(13320);
45 const __m128i k8708 = _mm_set1_epi16(8708);
46
47 const __m128i Y1 = _mm_mulhi_epu16(*Y0, k19077);
48
49 const __m128i R0 = _mm_mulhi_epu16(*V0, k26149);
50 const __m128i R1 = _mm_sub_epi16(Y1, k14234);
51 const __m128i R2 = _mm_add_epi16(R1, R0);
52
53 const __m128i G0 = _mm_mulhi_epu16(*U0, k6419);
54 const __m128i G1 = _mm_mulhi_epu16(*V0, k13320);
55 const __m128i G2 = _mm_add_epi16(Y1, k8708);
56 const __m128i G3 = _mm_add_epi16(G0, G1);
57 const __m128i G4 = _mm_sub_epi16(G2, G3);
58
59 // be careful with the saturated *unsigned* arithmetic here!
60 const __m128i B0 = _mm_mulhi_epu16(*U0, k33050);
61 const __m128i B1 = _mm_adds_epu16(B0, Y1);
62 const __m128i B2 = _mm_subs_epu16(B1, k17685);
63
64 // use logical shift for B2, which can be larger than 32767
65 *R = _mm_srai_epi16(R2, 6); // range: [-14234, 30815]
66 *G = _mm_srai_epi16(G4, 6); // range: [-10953, 27710]
67 *B = _mm_srli_epi16(B2, 6); // range: [0, 34238]
68 }
69
70 // Load the bytes into the *upper* part of 16b words. That's "<< 8", basically.
Load_HI_16_SSE2(const uint8_t * src)71 static WEBP_INLINE __m128i Load_HI_16_SSE2(const uint8_t* src) {
72 const __m128i zero = _mm_setzero_si128();
73 return _mm_unpacklo_epi8(zero, _mm_loadl_epi64((const __m128i*)src));
74 }
75
76 // Load and replicate the U/V samples
Load_UV_HI_8_SSE2(const uint8_t * src)77 static WEBP_INLINE __m128i Load_UV_HI_8_SSE2(const uint8_t* src) {
78 const __m128i zero = _mm_setzero_si128();
79 const __m128i tmp0 = _mm_cvtsi32_si128(WebPMemToInt32(src));
80 const __m128i tmp1 = _mm_unpacklo_epi8(zero, tmp0);
81 return _mm_unpacklo_epi16(tmp1, tmp1); // replicate samples
82 }
83
84 // Convert 32 samples of YUV444 to R/G/B
YUV444ToRGB_SSE2(const uint8_t * WEBP_RESTRICT const y,const uint8_t * WEBP_RESTRICT const u,const uint8_t * WEBP_RESTRICT const v,__m128i * const R,__m128i * const G,__m128i * const B)85 static void YUV444ToRGB_SSE2(const uint8_t* WEBP_RESTRICT const y,
86 const uint8_t* WEBP_RESTRICT const u,
87 const uint8_t* WEBP_RESTRICT const v,
88 __m128i* const R, __m128i* const G,
89 __m128i* const B) {
90 const __m128i Y0 = Load_HI_16_SSE2(y), U0 = Load_HI_16_SSE2(u),
91 V0 = Load_HI_16_SSE2(v);
92 ConvertYUV444ToRGB_SSE2(&Y0, &U0, &V0, R, G, B);
93 }
94
95 // Convert 32 samples of YUV420 to R/G/B
YUV420ToRGB_SSE2(const uint8_t * WEBP_RESTRICT const y,const uint8_t * WEBP_RESTRICT const u,const uint8_t * WEBP_RESTRICT const v,__m128i * const R,__m128i * const G,__m128i * const B)96 static void YUV420ToRGB_SSE2(const uint8_t* WEBP_RESTRICT const y,
97 const uint8_t* WEBP_RESTRICT const u,
98 const uint8_t* WEBP_RESTRICT const v,
99 __m128i* const R, __m128i* const G,
100 __m128i* const B) {
101 const __m128i Y0 = Load_HI_16_SSE2(y), U0 = Load_UV_HI_8_SSE2(u),
102 V0 = Load_UV_HI_8_SSE2(v);
103 ConvertYUV444ToRGB_SSE2(&Y0, &U0, &V0, R, G, B);
104 }
105
106 // Pack R/G/B/A results into 32b output.
PackAndStore4_SSE2(const __m128i * const R,const __m128i * const G,const __m128i * const B,const __m128i * const A,uint8_t * WEBP_RESTRICT const dst)107 static WEBP_INLINE void PackAndStore4_SSE2(const __m128i* const R,
108 const __m128i* const G,
109 const __m128i* const B,
110 const __m128i* const A,
111 uint8_t* WEBP_RESTRICT const dst) {
112 const __m128i rb = _mm_packus_epi16(*R, *B);
113 const __m128i ga = _mm_packus_epi16(*G, *A);
114 const __m128i rg = _mm_unpacklo_epi8(rb, ga);
115 const __m128i ba = _mm_unpackhi_epi8(rb, ga);
116 const __m128i RGBA_lo = _mm_unpacklo_epi16(rg, ba);
117 const __m128i RGBA_hi = _mm_unpackhi_epi16(rg, ba);
118 _mm_storeu_si128((__m128i*)(dst + 0), RGBA_lo);
119 _mm_storeu_si128((__m128i*)(dst + 16), RGBA_hi);
120 }
121
122 // Pack R/G/B/A results into 16b output.
PackAndStore4444_SSE2(const __m128i * const R,const __m128i * const G,const __m128i * const B,const __m128i * const A,uint8_t * WEBP_RESTRICT const dst)123 static WEBP_INLINE void PackAndStore4444_SSE2(
124 const __m128i* const R, const __m128i* const G, const __m128i* const B,
125 const __m128i* const A, uint8_t* WEBP_RESTRICT const dst) {
126 #if (WEBP_SWAP_16BIT_CSP == 0)
127 const __m128i rg0 = _mm_packus_epi16(*R, *G);
128 const __m128i ba0 = _mm_packus_epi16(*B, *A);
129 #else
130 const __m128i rg0 = _mm_packus_epi16(*B, *A);
131 const __m128i ba0 = _mm_packus_epi16(*R, *G);
132 #endif
133 const __m128i mask_0xf0 = _mm_set1_epi8((char)0xf0);
134 const __m128i rb1 = _mm_unpacklo_epi8(rg0, ba0); // rbrbrbrbrb...
135 const __m128i ga1 = _mm_unpackhi_epi8(rg0, ba0); // gagagagaga...
136 const __m128i rb2 = _mm_and_si128(rb1, mask_0xf0);
137 const __m128i ga2 = _mm_srli_epi16(_mm_and_si128(ga1, mask_0xf0), 4);
138 const __m128i rgba4444 = _mm_or_si128(rb2, ga2);
139 _mm_storeu_si128((__m128i*)dst, rgba4444);
140 }
141
142 // Pack R/G/B results into 16b output.
PackAndStore565_SSE2(const __m128i * const R,const __m128i * const G,const __m128i * const B,uint8_t * WEBP_RESTRICT const dst)143 static WEBP_INLINE void PackAndStore565_SSE2(const __m128i* const R,
144 const __m128i* const G,
145 const __m128i* const B,
146 uint8_t* WEBP_RESTRICT const dst) {
147 const __m128i r0 = _mm_packus_epi16(*R, *R);
148 const __m128i g0 = _mm_packus_epi16(*G, *G);
149 const __m128i b0 = _mm_packus_epi16(*B, *B);
150 const __m128i r1 = _mm_and_si128(r0, _mm_set1_epi8((char)0xf8));
151 const __m128i b1 = _mm_and_si128(_mm_srli_epi16(b0, 3), _mm_set1_epi8(0x1f));
152 const __m128i g1 =
153 _mm_srli_epi16(_mm_and_si128(g0, _mm_set1_epi8((char)0xe0)), 5);
154 const __m128i g2 = _mm_slli_epi16(_mm_and_si128(g0, _mm_set1_epi8(0x1c)), 3);
155 const __m128i rg = _mm_or_si128(r1, g1);
156 const __m128i gb = _mm_or_si128(g2, b1);
157 #if (WEBP_SWAP_16BIT_CSP == 0)
158 const __m128i rgb565 = _mm_unpacklo_epi8(rg, gb);
159 #else
160 const __m128i rgb565 = _mm_unpacklo_epi8(gb, rg);
161 #endif
162 _mm_storeu_si128((__m128i*)dst, rgb565);
163 }
164
165 // Pack the planar buffers
166 // rrrr... rrrr... gggg... gggg... bbbb... bbbb....
167 // triplet by triplet in the output buffer rgb as rgbrgbrgbrgb ...
PlanarTo24b_SSE2(__m128i * const in0,__m128i * const in1,__m128i * const in2,__m128i * const in3,__m128i * const in4,__m128i * const in5,uint8_t * WEBP_RESTRICT const rgb)168 static WEBP_INLINE void PlanarTo24b_SSE2(__m128i* const in0, __m128i* const in1,
169 __m128i* const in2, __m128i* const in3,
170 __m128i* const in4, __m128i* const in5,
171 uint8_t* WEBP_RESTRICT const rgb) {
172 // The input is 6 registers of sixteen 8b but for the sake of explanation,
173 // let's take 6 registers of four 8b values.
174 // To pack, we will keep taking one every two 8b integer and move it
175 // around as follows:
176 // Input:
177 // r0r1r2r3 | r4r5r6r7 | g0g1g2g3 | g4g5g6g7 | b0b1b2b3 | b4b5b6b7
178 // Split the 6 registers in two sets of 3 registers: the first set as the even
179 // 8b bytes, the second the odd ones:
180 // r0r2r4r6 | g0g2g4g6 | b0b2b4b6 | r1r3r5r7 | g1g3g5g7 | b1b3b5b7
181 // Repeat the same permutations twice more:
182 // r0r4g0g4 | b0b4r1r5 | g1g5b1b5 | r2r6g2g6 | b2b6r3r7 | g3g7b3b7
183 // r0g0b0r1 | g1b1r2g2 | b2r3g3b3 | r4g4b4r5 | g5b5r6g6 | b6r7g7b7
184 VP8PlanarTo24b_SSE2(in0, in1, in2, in3, in4, in5);
185
186 _mm_storeu_si128((__m128i*)(rgb + 0), *in0);
187 _mm_storeu_si128((__m128i*)(rgb + 16), *in1);
188 _mm_storeu_si128((__m128i*)(rgb + 32), *in2);
189 _mm_storeu_si128((__m128i*)(rgb + 48), *in3);
190 _mm_storeu_si128((__m128i*)(rgb + 64), *in4);
191 _mm_storeu_si128((__m128i*)(rgb + 80), *in5);
192 }
193
VP8YuvToRgba32_SSE2(const uint8_t * WEBP_RESTRICT y,const uint8_t * WEBP_RESTRICT u,const uint8_t * WEBP_RESTRICT v,uint8_t * WEBP_RESTRICT dst)194 void VP8YuvToRgba32_SSE2(const uint8_t* WEBP_RESTRICT y,
195 const uint8_t* WEBP_RESTRICT u,
196 const uint8_t* WEBP_RESTRICT v,
197 uint8_t* WEBP_RESTRICT dst) {
198 const __m128i kAlpha = _mm_set1_epi16(255);
199 int n;
200 for (n = 0; n < 32; n += 8, dst += 32) {
201 __m128i R, G, B;
202 YUV444ToRGB_SSE2(y + n, u + n, v + n, &R, &G, &B);
203 PackAndStore4_SSE2(&R, &G, &B, &kAlpha, dst);
204 }
205 }
206
VP8YuvToBgra32_SSE2(const uint8_t * WEBP_RESTRICT y,const uint8_t * WEBP_RESTRICT u,const uint8_t * WEBP_RESTRICT v,uint8_t * WEBP_RESTRICT dst)207 void VP8YuvToBgra32_SSE2(const uint8_t* WEBP_RESTRICT y,
208 const uint8_t* WEBP_RESTRICT u,
209 const uint8_t* WEBP_RESTRICT v,
210 uint8_t* WEBP_RESTRICT dst) {
211 const __m128i kAlpha = _mm_set1_epi16(255);
212 int n;
213 for (n = 0; n < 32; n += 8, dst += 32) {
214 __m128i R, G, B;
215 YUV444ToRGB_SSE2(y + n, u + n, v + n, &R, &G, &B);
216 PackAndStore4_SSE2(&B, &G, &R, &kAlpha, dst);
217 }
218 }
219
VP8YuvToArgb32_SSE2(const uint8_t * WEBP_RESTRICT y,const uint8_t * WEBP_RESTRICT u,const uint8_t * WEBP_RESTRICT v,uint8_t * WEBP_RESTRICT dst)220 void VP8YuvToArgb32_SSE2(const uint8_t* WEBP_RESTRICT y,
221 const uint8_t* WEBP_RESTRICT u,
222 const uint8_t* WEBP_RESTRICT v,
223 uint8_t* WEBP_RESTRICT dst) {
224 const __m128i kAlpha = _mm_set1_epi16(255);
225 int n;
226 for (n = 0; n < 32; n += 8, dst += 32) {
227 __m128i R, G, B;
228 YUV444ToRGB_SSE2(y + n, u + n, v + n, &R, &G, &B);
229 PackAndStore4_SSE2(&kAlpha, &R, &G, &B, dst);
230 }
231 }
232
VP8YuvToRgba444432_SSE2(const uint8_t * WEBP_RESTRICT y,const uint8_t * WEBP_RESTRICT u,const uint8_t * WEBP_RESTRICT v,uint8_t * WEBP_RESTRICT dst)233 void VP8YuvToRgba444432_SSE2(const uint8_t* WEBP_RESTRICT y,
234 const uint8_t* WEBP_RESTRICT u,
235 const uint8_t* WEBP_RESTRICT v,
236 uint8_t* WEBP_RESTRICT dst) {
237 const __m128i kAlpha = _mm_set1_epi16(255);
238 int n;
239 for (n = 0; n < 32; n += 8, dst += 16) {
240 __m128i R, G, B;
241 YUV444ToRGB_SSE2(y + n, u + n, v + n, &R, &G, &B);
242 PackAndStore4444_SSE2(&R, &G, &B, &kAlpha, dst);
243 }
244 }
245
VP8YuvToRgb56532_SSE2(const uint8_t * WEBP_RESTRICT y,const uint8_t * WEBP_RESTRICT u,const uint8_t * WEBP_RESTRICT v,uint8_t * WEBP_RESTRICT dst)246 void VP8YuvToRgb56532_SSE2(const uint8_t* WEBP_RESTRICT y,
247 const uint8_t* WEBP_RESTRICT u,
248 const uint8_t* WEBP_RESTRICT v,
249 uint8_t* WEBP_RESTRICT dst) {
250 int n;
251 for (n = 0; n < 32; n += 8, dst += 16) {
252 __m128i R, G, B;
253 YUV444ToRGB_SSE2(y + n, u + n, v + n, &R, &G, &B);
254 PackAndStore565_SSE2(&R, &G, &B, dst);
255 }
256 }
257
VP8YuvToRgb32_SSE2(const uint8_t * WEBP_RESTRICT y,const uint8_t * WEBP_RESTRICT u,const uint8_t * WEBP_RESTRICT v,uint8_t * WEBP_RESTRICT dst)258 void VP8YuvToRgb32_SSE2(const uint8_t* WEBP_RESTRICT y,
259 const uint8_t* WEBP_RESTRICT u,
260 const uint8_t* WEBP_RESTRICT v,
261 uint8_t* WEBP_RESTRICT dst) {
262 __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
263 __m128i rgb0, rgb1, rgb2, rgb3, rgb4, rgb5;
264
265 YUV444ToRGB_SSE2(y + 0, u + 0, v + 0, &R0, &G0, &B0);
266 YUV444ToRGB_SSE2(y + 8, u + 8, v + 8, &R1, &G1, &B1);
267 YUV444ToRGB_SSE2(y + 16, u + 16, v + 16, &R2, &G2, &B2);
268 YUV444ToRGB_SSE2(y + 24, u + 24, v + 24, &R3, &G3, &B3);
269
270 // Cast to 8b and store as RRRRGGGGBBBB.
271 rgb0 = _mm_packus_epi16(R0, R1);
272 rgb1 = _mm_packus_epi16(R2, R3);
273 rgb2 = _mm_packus_epi16(G0, G1);
274 rgb3 = _mm_packus_epi16(G2, G3);
275 rgb4 = _mm_packus_epi16(B0, B1);
276 rgb5 = _mm_packus_epi16(B2, B3);
277
278 // Pack as RGBRGBRGBRGB.
279 PlanarTo24b_SSE2(&rgb0, &rgb1, &rgb2, &rgb3, &rgb4, &rgb5, dst);
280 }
281
VP8YuvToBgr32_SSE2(const uint8_t * WEBP_RESTRICT y,const uint8_t * WEBP_RESTRICT u,const uint8_t * WEBP_RESTRICT v,uint8_t * WEBP_RESTRICT dst)282 void VP8YuvToBgr32_SSE2(const uint8_t* WEBP_RESTRICT y,
283 const uint8_t* WEBP_RESTRICT u,
284 const uint8_t* WEBP_RESTRICT v,
285 uint8_t* WEBP_RESTRICT dst) {
286 __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
287 __m128i bgr0, bgr1, bgr2, bgr3, bgr4, bgr5;
288
289 YUV444ToRGB_SSE2(y + 0, u + 0, v + 0, &R0, &G0, &B0);
290 YUV444ToRGB_SSE2(y + 8, u + 8, v + 8, &R1, &G1, &B1);
291 YUV444ToRGB_SSE2(y + 16, u + 16, v + 16, &R2, &G2, &B2);
292 YUV444ToRGB_SSE2(y + 24, u + 24, v + 24, &R3, &G3, &B3);
293
294 // Cast to 8b and store as BBBBGGGGRRRR.
295 bgr0 = _mm_packus_epi16(B0, B1);
296 bgr1 = _mm_packus_epi16(B2, B3);
297 bgr2 = _mm_packus_epi16(G0, G1);
298 bgr3 = _mm_packus_epi16(G2, G3);
299 bgr4 = _mm_packus_epi16(R0, R1);
300 bgr5= _mm_packus_epi16(R2, R3);
301
302 // Pack as BGRBGRBGRBGR.
303 PlanarTo24b_SSE2(&bgr0, &bgr1, &bgr2, &bgr3, &bgr4, &bgr5, dst);
304 }
305
306 //-----------------------------------------------------------------------------
307 // Arbitrary-length row conversion functions
308
YuvToRgbaRow_SSE2(const uint8_t * WEBP_RESTRICT y,const uint8_t * WEBP_RESTRICT u,const uint8_t * WEBP_RESTRICT v,uint8_t * WEBP_RESTRICT dst,int len)309 static void YuvToRgbaRow_SSE2(const uint8_t* WEBP_RESTRICT y,
310 const uint8_t* WEBP_RESTRICT u,
311 const uint8_t* WEBP_RESTRICT v,
312 uint8_t* WEBP_RESTRICT dst, int len) {
313 const __m128i kAlpha = _mm_set1_epi16(255);
314 int n;
315 for (n = 0; n + 8 <= len; n += 8, dst += 32) {
316 __m128i R, G, B;
317 YUV420ToRGB_SSE2(y, u, v, &R, &G, &B);
318 PackAndStore4_SSE2(&R, &G, &B, &kAlpha, dst);
319 y += 8;
320 u += 4;
321 v += 4;
322 }
323 for (; n < len; ++n) { // Finish off
324 VP8YuvToRgba(y[0], u[0], v[0], dst);
325 dst += 4;
326 y += 1;
327 u += (n & 1);
328 v += (n & 1);
329 }
330 }
331
YuvToBgraRow_SSE2(const uint8_t * WEBP_RESTRICT y,const uint8_t * WEBP_RESTRICT u,const uint8_t * WEBP_RESTRICT v,uint8_t * WEBP_RESTRICT dst,int len)332 static void YuvToBgraRow_SSE2(const uint8_t* WEBP_RESTRICT y,
333 const uint8_t* WEBP_RESTRICT u,
334 const uint8_t* WEBP_RESTRICT v,
335 uint8_t* WEBP_RESTRICT dst, int len) {
336 const __m128i kAlpha = _mm_set1_epi16(255);
337 int n;
338 for (n = 0; n + 8 <= len; n += 8, dst += 32) {
339 __m128i R, G, B;
340 YUV420ToRGB_SSE2(y, u, v, &R, &G, &B);
341 PackAndStore4_SSE2(&B, &G, &R, &kAlpha, dst);
342 y += 8;
343 u += 4;
344 v += 4;
345 }
346 for (; n < len; ++n) { // Finish off
347 VP8YuvToBgra(y[0], u[0], v[0], dst);
348 dst += 4;
349 y += 1;
350 u += (n & 1);
351 v += (n & 1);
352 }
353 }
354
YuvToArgbRow_SSE2(const uint8_t * WEBP_RESTRICT y,const uint8_t * WEBP_RESTRICT u,const uint8_t * WEBP_RESTRICT v,uint8_t * WEBP_RESTRICT dst,int len)355 static void YuvToArgbRow_SSE2(const uint8_t* WEBP_RESTRICT y,
356 const uint8_t* WEBP_RESTRICT u,
357 const uint8_t* WEBP_RESTRICT v,
358 uint8_t* WEBP_RESTRICT dst, int len) {
359 const __m128i kAlpha = _mm_set1_epi16(255);
360 int n;
361 for (n = 0; n + 8 <= len; n += 8, dst += 32) {
362 __m128i R, G, B;
363 YUV420ToRGB_SSE2(y, u, v, &R, &G, &B);
364 PackAndStore4_SSE2(&kAlpha, &R, &G, &B, dst);
365 y += 8;
366 u += 4;
367 v += 4;
368 }
369 for (; n < len; ++n) { // Finish off
370 VP8YuvToArgb(y[0], u[0], v[0], dst);
371 dst += 4;
372 y += 1;
373 u += (n & 1);
374 v += (n & 1);
375 }
376 }
377
YuvToRgbRow_SSE2(const uint8_t * WEBP_RESTRICT y,const uint8_t * WEBP_RESTRICT u,const uint8_t * WEBP_RESTRICT v,uint8_t * WEBP_RESTRICT dst,int len)378 static void YuvToRgbRow_SSE2(const uint8_t* WEBP_RESTRICT y,
379 const uint8_t* WEBP_RESTRICT u,
380 const uint8_t* WEBP_RESTRICT v,
381 uint8_t* WEBP_RESTRICT dst, int len) {
382 int n;
383 for (n = 0; n + 32 <= len; n += 32, dst += 32 * 3) {
384 __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
385 __m128i rgb0, rgb1, rgb2, rgb3, rgb4, rgb5;
386
387 YUV420ToRGB_SSE2(y + 0, u + 0, v + 0, &R0, &G0, &B0);
388 YUV420ToRGB_SSE2(y + 8, u + 4, v + 4, &R1, &G1, &B1);
389 YUV420ToRGB_SSE2(y + 16, u + 8, v + 8, &R2, &G2, &B2);
390 YUV420ToRGB_SSE2(y + 24, u + 12, v + 12, &R3, &G3, &B3);
391
392 // Cast to 8b and store as RRRRGGGGBBBB.
393 rgb0 = _mm_packus_epi16(R0, R1);
394 rgb1 = _mm_packus_epi16(R2, R3);
395 rgb2 = _mm_packus_epi16(G0, G1);
396 rgb3 = _mm_packus_epi16(G2, G3);
397 rgb4 = _mm_packus_epi16(B0, B1);
398 rgb5 = _mm_packus_epi16(B2, B3);
399
400 // Pack as RGBRGBRGBRGB.
401 PlanarTo24b_SSE2(&rgb0, &rgb1, &rgb2, &rgb3, &rgb4, &rgb5, dst);
402
403 y += 32;
404 u += 16;
405 v += 16;
406 }
407 for (; n < len; ++n) { // Finish off
408 VP8YuvToRgb(y[0], u[0], v[0], dst);
409 dst += 3;
410 y += 1;
411 u += (n & 1);
412 v += (n & 1);
413 }
414 }
415
YuvToBgrRow_SSE2(const uint8_t * WEBP_RESTRICT y,const uint8_t * WEBP_RESTRICT u,const uint8_t * WEBP_RESTRICT v,uint8_t * WEBP_RESTRICT dst,int len)416 static void YuvToBgrRow_SSE2(const uint8_t* WEBP_RESTRICT y,
417 const uint8_t* WEBP_RESTRICT u,
418 const uint8_t* WEBP_RESTRICT v,
419 uint8_t* WEBP_RESTRICT dst, int len) {
420 int n;
421 for (n = 0; n + 32 <= len; n += 32, dst += 32 * 3) {
422 __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
423 __m128i bgr0, bgr1, bgr2, bgr3, bgr4, bgr5;
424
425 YUV420ToRGB_SSE2(y + 0, u + 0, v + 0, &R0, &G0, &B0);
426 YUV420ToRGB_SSE2(y + 8, u + 4, v + 4, &R1, &G1, &B1);
427 YUV420ToRGB_SSE2(y + 16, u + 8, v + 8, &R2, &G2, &B2);
428 YUV420ToRGB_SSE2(y + 24, u + 12, v + 12, &R3, &G3, &B3);
429
430 // Cast to 8b and store as BBBBGGGGRRRR.
431 bgr0 = _mm_packus_epi16(B0, B1);
432 bgr1 = _mm_packus_epi16(B2, B3);
433 bgr2 = _mm_packus_epi16(G0, G1);
434 bgr3 = _mm_packus_epi16(G2, G3);
435 bgr4 = _mm_packus_epi16(R0, R1);
436 bgr5 = _mm_packus_epi16(R2, R3);
437
438 // Pack as BGRBGRBGRBGR.
439 PlanarTo24b_SSE2(&bgr0, &bgr1, &bgr2, &bgr3, &bgr4, &bgr5, dst);
440
441 y += 32;
442 u += 16;
443 v += 16;
444 }
445 for (; n < len; ++n) { // Finish off
446 VP8YuvToBgr(y[0], u[0], v[0], dst);
447 dst += 3;
448 y += 1;
449 u += (n & 1);
450 v += (n & 1);
451 }
452 }
453
454 //------------------------------------------------------------------------------
455 // Entry point
456
457 extern void WebPInitSamplersSSE2(void);
458
WebPInitSamplersSSE2(void)459 WEBP_TSAN_IGNORE_FUNCTION void WebPInitSamplersSSE2(void) {
460 WebPSamplers[MODE_RGB] = YuvToRgbRow_SSE2;
461 WebPSamplers[MODE_RGBA] = YuvToRgbaRow_SSE2;
462 WebPSamplers[MODE_BGR] = YuvToBgrRow_SSE2;
463 WebPSamplers[MODE_BGRA] = YuvToBgraRow_SSE2;
464 WebPSamplers[MODE_ARGB] = YuvToArgbRow_SSE2;
465 }
466
467 //------------------------------------------------------------------------------
468 // RGB24/32 -> YUV converters
469
470 // Load eight 16b-words from *src.
471 #define LOAD_16(src) _mm_loadu_si128((const __m128i*)(src))
472 // Store either 16b-words into *dst
473 #define STORE_16(V, dst) _mm_storeu_si128((__m128i*)(dst), (V))
474
475 // Function that inserts a value of the second half of the in buffer in between
476 // every two char of the first half.
RGB24PackedToPlanarHelper_SSE2(const __m128i * const in,__m128i * const out)477 static WEBP_INLINE void RGB24PackedToPlanarHelper_SSE2(
478 const __m128i* const in /*in[6]*/, __m128i* const out /*out[6]*/) {
479 out[0] = _mm_unpacklo_epi8(in[0], in[3]);
480 out[1] = _mm_unpackhi_epi8(in[0], in[3]);
481 out[2] = _mm_unpacklo_epi8(in[1], in[4]);
482 out[3] = _mm_unpackhi_epi8(in[1], in[4]);
483 out[4] = _mm_unpacklo_epi8(in[2], in[5]);
484 out[5] = _mm_unpackhi_epi8(in[2], in[5]);
485 }
486
487 // Unpack the 8b input rgbrgbrgbrgb ... as contiguous registers:
488 // rrrr... rrrr... gggg... gggg... bbbb... bbbb....
489 // Similar to PlanarTo24bHelper(), but in reverse order.
RGB24PackedToPlanar_SSE2(const uint8_t * WEBP_RESTRICT const rgb,__m128i * const out)490 static WEBP_INLINE void RGB24PackedToPlanar_SSE2(
491 const uint8_t* WEBP_RESTRICT const rgb, __m128i* const out /*out[6]*/) {
492 __m128i tmp[6];
493 tmp[0] = _mm_loadu_si128((const __m128i*)(rgb + 0));
494 tmp[1] = _mm_loadu_si128((const __m128i*)(rgb + 16));
495 tmp[2] = _mm_loadu_si128((const __m128i*)(rgb + 32));
496 tmp[3] = _mm_loadu_si128((const __m128i*)(rgb + 48));
497 tmp[4] = _mm_loadu_si128((const __m128i*)(rgb + 64));
498 tmp[5] = _mm_loadu_si128((const __m128i*)(rgb + 80));
499
500 RGB24PackedToPlanarHelper_SSE2(tmp, out);
501 RGB24PackedToPlanarHelper_SSE2(out, tmp);
502 RGB24PackedToPlanarHelper_SSE2(tmp, out);
503 RGB24PackedToPlanarHelper_SSE2(out, tmp);
504 RGB24PackedToPlanarHelper_SSE2(tmp, out);
505 }
506
507 // Convert 8 packed ARGB to r[], g[], b[]
RGB32PackedToPlanar_SSE2(const uint32_t * WEBP_RESTRICT const argb,__m128i * const rgb)508 static WEBP_INLINE void RGB32PackedToPlanar_SSE2(
509 const uint32_t* WEBP_RESTRICT const argb, __m128i* const rgb /*in[6]*/) {
510 const __m128i zero = _mm_setzero_si128();
511 __m128i a0 = LOAD_16(argb + 0);
512 __m128i a1 = LOAD_16(argb + 4);
513 __m128i a2 = LOAD_16(argb + 8);
514 __m128i a3 = LOAD_16(argb + 12);
515 VP8L32bToPlanar_SSE2(&a0, &a1, &a2, &a3);
516 rgb[0] = _mm_unpacklo_epi8(a1, zero);
517 rgb[1] = _mm_unpackhi_epi8(a1, zero);
518 rgb[2] = _mm_unpacklo_epi8(a2, zero);
519 rgb[3] = _mm_unpackhi_epi8(a2, zero);
520 rgb[4] = _mm_unpacklo_epi8(a3, zero);
521 rgb[5] = _mm_unpackhi_epi8(a3, zero);
522 }
523
524 // This macro computes (RG * MULT_RG + GB * MULT_GB + ROUNDER) >> DESCALE_FIX
525 // It's a macro and not a function because we need to use immediate values with
526 // srai_epi32, e.g.
527 #define TRANSFORM(RG_LO, RG_HI, GB_LO, GB_HI, MULT_RG, MULT_GB, \
528 ROUNDER, DESCALE_FIX, OUT) do { \
529 const __m128i V0_lo = _mm_madd_epi16(RG_LO, MULT_RG); \
530 const __m128i V0_hi = _mm_madd_epi16(RG_HI, MULT_RG); \
531 const __m128i V1_lo = _mm_madd_epi16(GB_LO, MULT_GB); \
532 const __m128i V1_hi = _mm_madd_epi16(GB_HI, MULT_GB); \
533 const __m128i V2_lo = _mm_add_epi32(V0_lo, V1_lo); \
534 const __m128i V2_hi = _mm_add_epi32(V0_hi, V1_hi); \
535 const __m128i V3_lo = _mm_add_epi32(V2_lo, ROUNDER); \
536 const __m128i V3_hi = _mm_add_epi32(V2_hi, ROUNDER); \
537 const __m128i V5_lo = _mm_srai_epi32(V3_lo, DESCALE_FIX); \
538 const __m128i V5_hi = _mm_srai_epi32(V3_hi, DESCALE_FIX); \
539 (OUT) = _mm_packs_epi32(V5_lo, V5_hi); \
540 } while (0)
541
542 #define MK_CST_16(A, B) _mm_set_epi16((B), (A), (B), (A), (B), (A), (B), (A))
ConvertRGBToY_SSE2(const __m128i * const R,const __m128i * const G,const __m128i * const B,__m128i * const Y)543 static WEBP_INLINE void ConvertRGBToY_SSE2(const __m128i* const R,
544 const __m128i* const G,
545 const __m128i* const B,
546 __m128i* const Y) {
547 const __m128i kRG_y = MK_CST_16(16839, 33059 - 16384);
548 const __m128i kGB_y = MK_CST_16(16384, 6420);
549 const __m128i kHALF_Y = _mm_set1_epi32((16 << YUV_FIX) + YUV_HALF);
550
551 const __m128i RG_lo = _mm_unpacklo_epi16(*R, *G);
552 const __m128i RG_hi = _mm_unpackhi_epi16(*R, *G);
553 const __m128i GB_lo = _mm_unpacklo_epi16(*G, *B);
554 const __m128i GB_hi = _mm_unpackhi_epi16(*G, *B);
555 TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_y, kGB_y, kHALF_Y, YUV_FIX, *Y);
556 }
557
ConvertRGBToUV_SSE2(const __m128i * const R,const __m128i * const G,const __m128i * const B,__m128i * const U,__m128i * const V)558 static WEBP_INLINE void ConvertRGBToUV_SSE2(const __m128i* const R,
559 const __m128i* const G,
560 const __m128i* const B,
561 __m128i* const U,
562 __m128i* const V) {
563 const __m128i kRG_u = MK_CST_16(-9719, -19081);
564 const __m128i kGB_u = MK_CST_16(0, 28800);
565 const __m128i kRG_v = MK_CST_16(28800, 0);
566 const __m128i kGB_v = MK_CST_16(-24116, -4684);
567 const __m128i kHALF_UV = _mm_set1_epi32(((128 << YUV_FIX) + YUV_HALF) << 2);
568
569 const __m128i RG_lo = _mm_unpacklo_epi16(*R, *G);
570 const __m128i RG_hi = _mm_unpackhi_epi16(*R, *G);
571 const __m128i GB_lo = _mm_unpacklo_epi16(*G, *B);
572 const __m128i GB_hi = _mm_unpackhi_epi16(*G, *B);
573 TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_u, kGB_u,
574 kHALF_UV, YUV_FIX + 2, *U);
575 TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_v, kGB_v,
576 kHALF_UV, YUV_FIX + 2, *V);
577 }
578
579 #undef MK_CST_16
580 #undef TRANSFORM
581
ConvertRGB24ToY_SSE2(const uint8_t * WEBP_RESTRICT rgb,uint8_t * WEBP_RESTRICT y,int width)582 static void ConvertRGB24ToY_SSE2(const uint8_t* WEBP_RESTRICT rgb,
583 uint8_t* WEBP_RESTRICT y, int width) {
584 const int max_width = width & ~31;
585 int i;
586 for (i = 0; i < max_width; rgb += 3 * 16 * 2) {
587 __m128i rgb_plane[6];
588 int j;
589
590 RGB24PackedToPlanar_SSE2(rgb, rgb_plane);
591
592 for (j = 0; j < 2; ++j, i += 16) {
593 const __m128i zero = _mm_setzero_si128();
594 __m128i r, g, b, Y0, Y1;
595
596 // Convert to 16-bit Y.
597 r = _mm_unpacklo_epi8(rgb_plane[0 + j], zero);
598 g = _mm_unpacklo_epi8(rgb_plane[2 + j], zero);
599 b = _mm_unpacklo_epi8(rgb_plane[4 + j], zero);
600 ConvertRGBToY_SSE2(&r, &g, &b, &Y0);
601
602 // Convert to 16-bit Y.
603 r = _mm_unpackhi_epi8(rgb_plane[0 + j], zero);
604 g = _mm_unpackhi_epi8(rgb_plane[2 + j], zero);
605 b = _mm_unpackhi_epi8(rgb_plane[4 + j], zero);
606 ConvertRGBToY_SSE2(&r, &g, &b, &Y1);
607
608 // Cast to 8-bit and store.
609 STORE_16(_mm_packus_epi16(Y0, Y1), y + i);
610 }
611 }
612 for (; i < width; ++i, rgb += 3) { // left-over
613 y[i] = VP8RGBToY(rgb[0], rgb[1], rgb[2], YUV_HALF);
614 }
615 }
616
ConvertBGR24ToY_SSE2(const uint8_t * WEBP_RESTRICT bgr,uint8_t * WEBP_RESTRICT y,int width)617 static void ConvertBGR24ToY_SSE2(const uint8_t* WEBP_RESTRICT bgr,
618 uint8_t* WEBP_RESTRICT y, int width) {
619 const int max_width = width & ~31;
620 int i;
621 for (i = 0; i < max_width; bgr += 3 * 16 * 2) {
622 __m128i bgr_plane[6];
623 int j;
624
625 RGB24PackedToPlanar_SSE2(bgr, bgr_plane);
626
627 for (j = 0; j < 2; ++j, i += 16) {
628 const __m128i zero = _mm_setzero_si128();
629 __m128i r, g, b, Y0, Y1;
630
631 // Convert to 16-bit Y.
632 b = _mm_unpacklo_epi8(bgr_plane[0 + j], zero);
633 g = _mm_unpacklo_epi8(bgr_plane[2 + j], zero);
634 r = _mm_unpacklo_epi8(bgr_plane[4 + j], zero);
635 ConvertRGBToY_SSE2(&r, &g, &b, &Y0);
636
637 // Convert to 16-bit Y.
638 b = _mm_unpackhi_epi8(bgr_plane[0 + j], zero);
639 g = _mm_unpackhi_epi8(bgr_plane[2 + j], zero);
640 r = _mm_unpackhi_epi8(bgr_plane[4 + j], zero);
641 ConvertRGBToY_SSE2(&r, &g, &b, &Y1);
642
643 // Cast to 8-bit and store.
644 STORE_16(_mm_packus_epi16(Y0, Y1), y + i);
645 }
646 }
647 for (; i < width; ++i, bgr += 3) { // left-over
648 y[i] = VP8RGBToY(bgr[2], bgr[1], bgr[0], YUV_HALF);
649 }
650 }
651
ConvertARGBToY_SSE2(const uint32_t * WEBP_RESTRICT argb,uint8_t * WEBP_RESTRICT y,int width)652 static void ConvertARGBToY_SSE2(const uint32_t* WEBP_RESTRICT argb,
653 uint8_t* WEBP_RESTRICT y, int width) {
654 const int max_width = width & ~15;
655 int i;
656 for (i = 0; i < max_width; i += 16) {
657 __m128i Y0, Y1, rgb[6];
658 RGB32PackedToPlanar_SSE2(&argb[i], rgb);
659 ConvertRGBToY_SSE2(&rgb[0], &rgb[2], &rgb[4], &Y0);
660 ConvertRGBToY_SSE2(&rgb[1], &rgb[3], &rgb[5], &Y1);
661 STORE_16(_mm_packus_epi16(Y0, Y1), y + i);
662 }
663 for (; i < width; ++i) { // left-over
664 const uint32_t p = argb[i];
665 y[i] = VP8RGBToY((p >> 16) & 0xff, (p >> 8) & 0xff, (p >> 0) & 0xff,
666 YUV_HALF);
667 }
668 }
669
670 // Horizontal add (doubled) of two 16b values, result is 16b.
671 // in: A | B | C | D | ... -> out: 2*(A+B) | 2*(C+D) | ...
HorizontalAddPack_SSE2(const __m128i * const A,const __m128i * const B,__m128i * const out)672 static void HorizontalAddPack_SSE2(const __m128i* const A,
673 const __m128i* const B,
674 __m128i* const out) {
675 const __m128i k2 = _mm_set1_epi16(2);
676 const __m128i C = _mm_madd_epi16(*A, k2);
677 const __m128i D = _mm_madd_epi16(*B, k2);
678 *out = _mm_packs_epi32(C, D);
679 }
680
ConvertARGBToUV_SSE2(const uint32_t * WEBP_RESTRICT argb,uint8_t * WEBP_RESTRICT u,uint8_t * WEBP_RESTRICT v,int src_width,int do_store)681 static void ConvertARGBToUV_SSE2(const uint32_t* WEBP_RESTRICT argb,
682 uint8_t* WEBP_RESTRICT u,
683 uint8_t* WEBP_RESTRICT v,
684 int src_width, int do_store) {
685 const int max_width = src_width & ~31;
686 int i;
687 for (i = 0; i < max_width; i += 32, u += 16, v += 16) {
688 __m128i rgb[6], U0, V0, U1, V1;
689 RGB32PackedToPlanar_SSE2(&argb[i], rgb);
690 HorizontalAddPack_SSE2(&rgb[0], &rgb[1], &rgb[0]);
691 HorizontalAddPack_SSE2(&rgb[2], &rgb[3], &rgb[2]);
692 HorizontalAddPack_SSE2(&rgb[4], &rgb[5], &rgb[4]);
693 ConvertRGBToUV_SSE2(&rgb[0], &rgb[2], &rgb[4], &U0, &V0);
694
695 RGB32PackedToPlanar_SSE2(&argb[i + 16], rgb);
696 HorizontalAddPack_SSE2(&rgb[0], &rgb[1], &rgb[0]);
697 HorizontalAddPack_SSE2(&rgb[2], &rgb[3], &rgb[2]);
698 HorizontalAddPack_SSE2(&rgb[4], &rgb[5], &rgb[4]);
699 ConvertRGBToUV_SSE2(&rgb[0], &rgb[2], &rgb[4], &U1, &V1);
700
701 U0 = _mm_packus_epi16(U0, U1);
702 V0 = _mm_packus_epi16(V0, V1);
703 if (!do_store) {
704 const __m128i prev_u = LOAD_16(u);
705 const __m128i prev_v = LOAD_16(v);
706 U0 = _mm_avg_epu8(U0, prev_u);
707 V0 = _mm_avg_epu8(V0, prev_v);
708 }
709 STORE_16(U0, u);
710 STORE_16(V0, v);
711 }
712 if (i < src_width) { // left-over
713 WebPConvertARGBToUV_C(argb + i, u, v, src_width - i, do_store);
714 }
715 }
716
717 // Convert 16 packed ARGB 16b-values to r[], g[], b[]
RGBA32PackedToPlanar_16b_SSE2(const uint16_t * WEBP_RESTRICT const rgbx,__m128i * const r,__m128i * const g,__m128i * const b)718 static WEBP_INLINE void RGBA32PackedToPlanar_16b_SSE2(
719 const uint16_t* WEBP_RESTRICT const rgbx,
720 __m128i* const r, __m128i* const g, __m128i* const b) {
721 const __m128i in0 = LOAD_16(rgbx + 0); // r0 | g0 | b0 |x| r1 | g1 | b1 |x
722 const __m128i in1 = LOAD_16(rgbx + 8); // r2 | g2 | b2 |x| r3 | g3 | b3 |x
723 const __m128i in2 = LOAD_16(rgbx + 16); // r4 | ...
724 const __m128i in3 = LOAD_16(rgbx + 24); // r6 | ...
725 // column-wise transpose
726 const __m128i A0 = _mm_unpacklo_epi16(in0, in1);
727 const __m128i A1 = _mm_unpackhi_epi16(in0, in1);
728 const __m128i A2 = _mm_unpacklo_epi16(in2, in3);
729 const __m128i A3 = _mm_unpackhi_epi16(in2, in3);
730 const __m128i B0 = _mm_unpacklo_epi16(A0, A1); // r0 r1 r2 r3 | g0 g1 ..
731 const __m128i B1 = _mm_unpackhi_epi16(A0, A1); // b0 b1 b2 b3 | x x x x
732 const __m128i B2 = _mm_unpacklo_epi16(A2, A3); // r4 r5 r6 r7 | g4 g5 ..
733 const __m128i B3 = _mm_unpackhi_epi16(A2, A3); // b4 b5 b6 b7 | x x x x
734 *r = _mm_unpacklo_epi64(B0, B2);
735 *g = _mm_unpackhi_epi64(B0, B2);
736 *b = _mm_unpacklo_epi64(B1, B3);
737 }
738
ConvertRGBA32ToUV_SSE2(const uint16_t * WEBP_RESTRICT rgb,uint8_t * WEBP_RESTRICT u,uint8_t * WEBP_RESTRICT v,int width)739 static void ConvertRGBA32ToUV_SSE2(const uint16_t* WEBP_RESTRICT rgb,
740 uint8_t* WEBP_RESTRICT u,
741 uint8_t* WEBP_RESTRICT v, int width) {
742 const int max_width = width & ~15;
743 const uint16_t* const last_rgb = rgb + 4 * max_width;
744 while (rgb < last_rgb) {
745 __m128i r, g, b, U0, V0, U1, V1;
746 RGBA32PackedToPlanar_16b_SSE2(rgb + 0, &r, &g, &b);
747 ConvertRGBToUV_SSE2(&r, &g, &b, &U0, &V0);
748 RGBA32PackedToPlanar_16b_SSE2(rgb + 32, &r, &g, &b);
749 ConvertRGBToUV_SSE2(&r, &g, &b, &U1, &V1);
750 STORE_16(_mm_packus_epi16(U0, U1), u);
751 STORE_16(_mm_packus_epi16(V0, V1), v);
752 u += 16;
753 v += 16;
754 rgb += 2 * 32;
755 }
756 if (max_width < width) { // left-over
757 WebPConvertRGBA32ToUV_C(rgb, u, v, width - max_width);
758 }
759 }
760
761 //------------------------------------------------------------------------------
762
763 extern void WebPInitConvertARGBToYUVSSE2(void);
764
WebPInitConvertARGBToYUVSSE2(void)765 WEBP_TSAN_IGNORE_FUNCTION void WebPInitConvertARGBToYUVSSE2(void) {
766 WebPConvertARGBToY = ConvertARGBToY_SSE2;
767 WebPConvertARGBToUV = ConvertARGBToUV_SSE2;
768
769 WebPConvertRGB24ToY = ConvertRGB24ToY_SSE2;
770 WebPConvertBGR24ToY = ConvertBGR24ToY_SSE2;
771
772 WebPConvertRGBA32ToUV = ConvertRGBA32ToUV_SSE2;
773 }
774
775 #else // !WEBP_USE_SSE2
776
777 WEBP_DSP_INIT_STUB(WebPInitSamplersSSE2)
778 WEBP_DSP_INIT_STUB(WebPInitConvertARGBToYUVSSE2)
779
780 #endif // WEBP_USE_SSE2
781