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_SSE41)
17
18 #include <stdlib.h>
19 #include <smmintrin.h>
20
21 #include "src/dsp/common_sse41.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_SSE41(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_SSE41(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_SSE41(const uint8_t * src)71 static WEBP_INLINE __m128i Load_HI_16_SSE41(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_SSE41(const uint8_t * src)77 static WEBP_INLINE __m128i Load_UV_HI_8_SSE41(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_SSE41(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_SSE41(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_SSE41(y), U0 = Load_HI_16_SSE41(u),
91 V0 = Load_HI_16_SSE41(v);
92 ConvertYUV444ToRGB_SSE41(&Y0, &U0, &V0, R, G, B);
93 }
94
95 // Convert 32 samples of YUV420 to R/G/B
YUV420ToRGB_SSE41(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_SSE41(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_SSE41(y), U0 = Load_UV_HI_8_SSE41(u),
102 V0 = Load_UV_HI_8_SSE41(v);
103 ConvertYUV444ToRGB_SSE41(&Y0, &U0, &V0, R, G, B);
104 }
105
106 // Pack the planar buffers
107 // rrrr... rrrr... gggg... gggg... bbbb... bbbb....
108 // triplet by triplet in the output buffer rgb as rgbrgbrgbrgb ...
PlanarTo24b_SSE41(__m128i * const in0,__m128i * const in1,__m128i * const in2,__m128i * const in3,__m128i * const in4,__m128i * const in5,uint8_t * WEBP_RESTRICT const rgb)109 static WEBP_INLINE void PlanarTo24b_SSE41(
110 __m128i* const in0, __m128i* const in1, __m128i* const in2,
111 __m128i* const in3, __m128i* const in4, __m128i* const in5,
112 uint8_t* WEBP_RESTRICT const rgb) {
113 // The input is 6 registers of sixteen 8b but for the sake of explanation,
114 // let's take 6 registers of four 8b values.
115 // To pack, we will keep taking one every two 8b integer and move it
116 // around as follows:
117 // Input:
118 // r0r1r2r3 | r4r5r6r7 | g0g1g2g3 | g4g5g6g7 | b0b1b2b3 | b4b5b6b7
119 // Split the 6 registers in two sets of 3 registers: the first set as the even
120 // 8b bytes, the second the odd ones:
121 // r0r2r4r6 | g0g2g4g6 | b0b2b4b6 | r1r3r5r7 | g1g3g5g7 | b1b3b5b7
122 // Repeat the same permutations twice more:
123 // r0r4g0g4 | b0b4r1r5 | g1g5b1b5 | r2r6g2g6 | b2b6r3r7 | g3g7b3b7
124 // r0g0b0r1 | g1b1r2g2 | b2r3g3b3 | r4g4b4r5 | g5b5r6g6 | b6r7g7b7
125 VP8PlanarTo24b_SSE41(in0, in1, in2, in3, in4, in5);
126
127 _mm_storeu_si128((__m128i*)(rgb + 0), *in0);
128 _mm_storeu_si128((__m128i*)(rgb + 16), *in1);
129 _mm_storeu_si128((__m128i*)(rgb + 32), *in2);
130 _mm_storeu_si128((__m128i*)(rgb + 48), *in3);
131 _mm_storeu_si128((__m128i*)(rgb + 64), *in4);
132 _mm_storeu_si128((__m128i*)(rgb + 80), *in5);
133 }
134
VP8YuvToRgb32_SSE41(const uint8_t * WEBP_RESTRICT y,const uint8_t * WEBP_RESTRICT u,const uint8_t * WEBP_RESTRICT v,uint8_t * WEBP_RESTRICT dst)135 void VP8YuvToRgb32_SSE41(const uint8_t* WEBP_RESTRICT y,
136 const uint8_t* WEBP_RESTRICT u,
137 const uint8_t* WEBP_RESTRICT v,
138 uint8_t* WEBP_RESTRICT dst) {
139 __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
140 __m128i rgb0, rgb1, rgb2, rgb3, rgb4, rgb5;
141
142 YUV444ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0);
143 YUV444ToRGB_SSE41(y + 8, u + 8, v + 8, &R1, &G1, &B1);
144 YUV444ToRGB_SSE41(y + 16, u + 16, v + 16, &R2, &G2, &B2);
145 YUV444ToRGB_SSE41(y + 24, u + 24, v + 24, &R3, &G3, &B3);
146
147 // Cast to 8b and store as RRRRGGGGBBBB.
148 rgb0 = _mm_packus_epi16(R0, R1);
149 rgb1 = _mm_packus_epi16(R2, R3);
150 rgb2 = _mm_packus_epi16(G0, G1);
151 rgb3 = _mm_packus_epi16(G2, G3);
152 rgb4 = _mm_packus_epi16(B0, B1);
153 rgb5 = _mm_packus_epi16(B2, B3);
154
155 // Pack as RGBRGBRGBRGB.
156 PlanarTo24b_SSE41(&rgb0, &rgb1, &rgb2, &rgb3, &rgb4, &rgb5, dst);
157 }
158
VP8YuvToBgr32_SSE41(const uint8_t * WEBP_RESTRICT y,const uint8_t * WEBP_RESTRICT u,const uint8_t * WEBP_RESTRICT v,uint8_t * WEBP_RESTRICT dst)159 void VP8YuvToBgr32_SSE41(const uint8_t* WEBP_RESTRICT y,
160 const uint8_t* WEBP_RESTRICT u,
161 const uint8_t* WEBP_RESTRICT v,
162 uint8_t* WEBP_RESTRICT dst) {
163 __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
164 __m128i bgr0, bgr1, bgr2, bgr3, bgr4, bgr5;
165
166 YUV444ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0);
167 YUV444ToRGB_SSE41(y + 8, u + 8, v + 8, &R1, &G1, &B1);
168 YUV444ToRGB_SSE41(y + 16, u + 16, v + 16, &R2, &G2, &B2);
169 YUV444ToRGB_SSE41(y + 24, u + 24, v + 24, &R3, &G3, &B3);
170
171 // Cast to 8b and store as BBBBGGGGRRRR.
172 bgr0 = _mm_packus_epi16(B0, B1);
173 bgr1 = _mm_packus_epi16(B2, B3);
174 bgr2 = _mm_packus_epi16(G0, G1);
175 bgr3 = _mm_packus_epi16(G2, G3);
176 bgr4 = _mm_packus_epi16(R0, R1);
177 bgr5= _mm_packus_epi16(R2, R3);
178
179 // Pack as BGRBGRBGRBGR.
180 PlanarTo24b_SSE41(&bgr0, &bgr1, &bgr2, &bgr3, &bgr4, &bgr5, dst);
181 }
182
183 //-----------------------------------------------------------------------------
184 // Arbitrary-length row conversion functions
185
YuvToRgbRow_SSE41(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)186 static void YuvToRgbRow_SSE41(const uint8_t* WEBP_RESTRICT y,
187 const uint8_t* WEBP_RESTRICT u,
188 const uint8_t* WEBP_RESTRICT v,
189 uint8_t* WEBP_RESTRICT dst, int len) {
190 int n;
191 for (n = 0; n + 32 <= len; n += 32, dst += 32 * 3) {
192 __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
193 __m128i rgb0, rgb1, rgb2, rgb3, rgb4, rgb5;
194
195 YUV420ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0);
196 YUV420ToRGB_SSE41(y + 8, u + 4, v + 4, &R1, &G1, &B1);
197 YUV420ToRGB_SSE41(y + 16, u + 8, v + 8, &R2, &G2, &B2);
198 YUV420ToRGB_SSE41(y + 24, u + 12, v + 12, &R3, &G3, &B3);
199
200 // Cast to 8b and store as RRRRGGGGBBBB.
201 rgb0 = _mm_packus_epi16(R0, R1);
202 rgb1 = _mm_packus_epi16(R2, R3);
203 rgb2 = _mm_packus_epi16(G0, G1);
204 rgb3 = _mm_packus_epi16(G2, G3);
205 rgb4 = _mm_packus_epi16(B0, B1);
206 rgb5 = _mm_packus_epi16(B2, B3);
207
208 // Pack as RGBRGBRGBRGB.
209 PlanarTo24b_SSE41(&rgb0, &rgb1, &rgb2, &rgb3, &rgb4, &rgb5, dst);
210
211 y += 32;
212 u += 16;
213 v += 16;
214 }
215 for (; n < len; ++n) { // Finish off
216 VP8YuvToRgb(y[0], u[0], v[0], dst);
217 dst += 3;
218 y += 1;
219 u += (n & 1);
220 v += (n & 1);
221 }
222 }
223
YuvToBgrRow_SSE41(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)224 static void YuvToBgrRow_SSE41(const uint8_t* WEBP_RESTRICT y,
225 const uint8_t* WEBP_RESTRICT u,
226 const uint8_t* WEBP_RESTRICT v,
227 uint8_t* WEBP_RESTRICT dst, int len) {
228 int n;
229 for (n = 0; n + 32 <= len; n += 32, dst += 32 * 3) {
230 __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
231 __m128i bgr0, bgr1, bgr2, bgr3, bgr4, bgr5;
232
233 YUV420ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0);
234 YUV420ToRGB_SSE41(y + 8, u + 4, v + 4, &R1, &G1, &B1);
235 YUV420ToRGB_SSE41(y + 16, u + 8, v + 8, &R2, &G2, &B2);
236 YUV420ToRGB_SSE41(y + 24, u + 12, v + 12, &R3, &G3, &B3);
237
238 // Cast to 8b and store as BBBBGGGGRRRR.
239 bgr0 = _mm_packus_epi16(B0, B1);
240 bgr1 = _mm_packus_epi16(B2, B3);
241 bgr2 = _mm_packus_epi16(G0, G1);
242 bgr3 = _mm_packus_epi16(G2, G3);
243 bgr4 = _mm_packus_epi16(R0, R1);
244 bgr5 = _mm_packus_epi16(R2, R3);
245
246 // Pack as BGRBGRBGRBGR.
247 PlanarTo24b_SSE41(&bgr0, &bgr1, &bgr2, &bgr3, &bgr4, &bgr5, dst);
248
249 y += 32;
250 u += 16;
251 v += 16;
252 }
253 for (; n < len; ++n) { // Finish off
254 VP8YuvToBgr(y[0], u[0], v[0], dst);
255 dst += 3;
256 y += 1;
257 u += (n & 1);
258 v += (n & 1);
259 }
260 }
261
262 //------------------------------------------------------------------------------
263 // Entry point
264
265 extern void WebPInitSamplersSSE41(void);
266
WebPInitSamplersSSE41(void)267 WEBP_TSAN_IGNORE_FUNCTION void WebPInitSamplersSSE41(void) {
268 WebPSamplers[MODE_RGB] = YuvToRgbRow_SSE41;
269 WebPSamplers[MODE_BGR] = YuvToBgrRow_SSE41;
270 }
271
272 //------------------------------------------------------------------------------
273 // RGB24/32 -> YUV converters
274
275 // Load eight 16b-words from *src.
276 #define LOAD_16(src) _mm_loadu_si128((const __m128i*)(src))
277 // Store either 16b-words into *dst
278 #define STORE_16(V, dst) _mm_storeu_si128((__m128i*)(dst), (V))
279
280 #define WEBP_SSE41_SHUFF(OUT) do { \
281 const __m128i tmp0 = _mm_shuffle_epi8(A0, shuff0); \
282 const __m128i tmp1 = _mm_shuffle_epi8(A1, shuff1); \
283 const __m128i tmp2 = _mm_shuffle_epi8(A2, shuff2); \
284 const __m128i tmp3 = _mm_shuffle_epi8(A3, shuff0); \
285 const __m128i tmp4 = _mm_shuffle_epi8(A4, shuff1); \
286 const __m128i tmp5 = _mm_shuffle_epi8(A5, shuff2); \
287 \
288 /* OR everything to get one channel */ \
289 const __m128i tmp6 = _mm_or_si128(tmp0, tmp1); \
290 const __m128i tmp7 = _mm_or_si128(tmp3, tmp4); \
291 out[OUT + 0] = _mm_or_si128(tmp6, tmp2); \
292 out[OUT + 1] = _mm_or_si128(tmp7, tmp5); \
293 } while (0);
294
295 // Unpack the 8b input rgbrgbrgbrgb ... as contiguous registers:
296 // rrrr... rrrr... gggg... gggg... bbbb... bbbb....
297 // Similar to PlanarTo24bHelper(), but in reverse order.
RGB24PackedToPlanar_SSE41(const uint8_t * WEBP_RESTRICT const rgb,__m128i * const out)298 static WEBP_INLINE void RGB24PackedToPlanar_SSE41(
299 const uint8_t* WEBP_RESTRICT const rgb, __m128i* const out /*out[6]*/) {
300 const __m128i A0 = _mm_loadu_si128((const __m128i*)(rgb + 0));
301 const __m128i A1 = _mm_loadu_si128((const __m128i*)(rgb + 16));
302 const __m128i A2 = _mm_loadu_si128((const __m128i*)(rgb + 32));
303 const __m128i A3 = _mm_loadu_si128((const __m128i*)(rgb + 48));
304 const __m128i A4 = _mm_loadu_si128((const __m128i*)(rgb + 64));
305 const __m128i A5 = _mm_loadu_si128((const __m128i*)(rgb + 80));
306
307 // Compute RR.
308 {
309 const __m128i shuff0 = _mm_set_epi8(
310 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 15, 12, 9, 6, 3, 0);
311 const __m128i shuff1 = _mm_set_epi8(
312 -1, -1, -1, -1, -1, 14, 11, 8, 5, 2, -1, -1, -1, -1, -1, -1);
313 const __m128i shuff2 = _mm_set_epi8(
314 13, 10, 7, 4, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
315 WEBP_SSE41_SHUFF(0)
316 }
317 // Compute GG.
318 {
319 const __m128i shuff0 = _mm_set_epi8(
320 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 13, 10, 7, 4, 1);
321 const __m128i shuff1 = _mm_set_epi8(
322 -1, -1, -1, -1, -1, 15, 12, 9, 6, 3, 0, -1, -1, -1, -1, -1);
323 const __m128i shuff2 = _mm_set_epi8(
324 14, 11, 8, 5, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
325 WEBP_SSE41_SHUFF(2)
326 }
327 // Compute BB.
328 {
329 const __m128i shuff0 = _mm_set_epi8(
330 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 14, 11, 8, 5, 2);
331 const __m128i shuff1 = _mm_set_epi8(
332 -1, -1, -1, -1, -1, -1, 13, 10, 7, 4, 1, -1, -1, -1, -1, -1);
333 const __m128i shuff2 = _mm_set_epi8(
334 15, 12, 9, 6, 3, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
335 WEBP_SSE41_SHUFF(4)
336 }
337 }
338
339 #undef WEBP_SSE41_SHUFF
340
341 // Convert 8 packed ARGB to r[], g[], b[]
RGB32PackedToPlanar_SSE41(const uint32_t * WEBP_RESTRICT const argb,__m128i * const rgb)342 static WEBP_INLINE void RGB32PackedToPlanar_SSE41(
343 const uint32_t* WEBP_RESTRICT const argb, __m128i* const rgb /*in[6]*/) {
344 const __m128i zero = _mm_setzero_si128();
345 __m128i a0 = LOAD_16(argb + 0);
346 __m128i a1 = LOAD_16(argb + 4);
347 __m128i a2 = LOAD_16(argb + 8);
348 __m128i a3 = LOAD_16(argb + 12);
349 VP8L32bToPlanar_SSE41(&a0, &a1, &a2, &a3);
350 rgb[0] = _mm_unpacklo_epi8(a1, zero);
351 rgb[1] = _mm_unpackhi_epi8(a1, zero);
352 rgb[2] = _mm_unpacklo_epi8(a2, zero);
353 rgb[3] = _mm_unpackhi_epi8(a2, zero);
354 rgb[4] = _mm_unpacklo_epi8(a3, zero);
355 rgb[5] = _mm_unpackhi_epi8(a3, zero);
356 }
357
358 // This macro computes (RG * MULT_RG + GB * MULT_GB + ROUNDER) >> DESCALE_FIX
359 // It's a macro and not a function because we need to use immediate values with
360 // srai_epi32, e.g.
361 #define TRANSFORM(RG_LO, RG_HI, GB_LO, GB_HI, MULT_RG, MULT_GB, \
362 ROUNDER, DESCALE_FIX, OUT) do { \
363 const __m128i V0_lo = _mm_madd_epi16(RG_LO, MULT_RG); \
364 const __m128i V0_hi = _mm_madd_epi16(RG_HI, MULT_RG); \
365 const __m128i V1_lo = _mm_madd_epi16(GB_LO, MULT_GB); \
366 const __m128i V1_hi = _mm_madd_epi16(GB_HI, MULT_GB); \
367 const __m128i V2_lo = _mm_add_epi32(V0_lo, V1_lo); \
368 const __m128i V2_hi = _mm_add_epi32(V0_hi, V1_hi); \
369 const __m128i V3_lo = _mm_add_epi32(V2_lo, ROUNDER); \
370 const __m128i V3_hi = _mm_add_epi32(V2_hi, ROUNDER); \
371 const __m128i V5_lo = _mm_srai_epi32(V3_lo, DESCALE_FIX); \
372 const __m128i V5_hi = _mm_srai_epi32(V3_hi, DESCALE_FIX); \
373 (OUT) = _mm_packs_epi32(V5_lo, V5_hi); \
374 } while (0)
375
376 #define MK_CST_16(A, B) _mm_set_epi16((B), (A), (B), (A), (B), (A), (B), (A))
ConvertRGBToY_SSE41(const __m128i * const R,const __m128i * const G,const __m128i * const B,__m128i * const Y)377 static WEBP_INLINE void ConvertRGBToY_SSE41(const __m128i* const R,
378 const __m128i* const G,
379 const __m128i* const B,
380 __m128i* const Y) {
381 const __m128i kRG_y = MK_CST_16(16839, 33059 - 16384);
382 const __m128i kGB_y = MK_CST_16(16384, 6420);
383 const __m128i kHALF_Y = _mm_set1_epi32((16 << YUV_FIX) + YUV_HALF);
384
385 const __m128i RG_lo = _mm_unpacklo_epi16(*R, *G);
386 const __m128i RG_hi = _mm_unpackhi_epi16(*R, *G);
387 const __m128i GB_lo = _mm_unpacklo_epi16(*G, *B);
388 const __m128i GB_hi = _mm_unpackhi_epi16(*G, *B);
389 TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_y, kGB_y, kHALF_Y, YUV_FIX, *Y);
390 }
391
ConvertRGBToUV_SSE41(const __m128i * const R,const __m128i * const G,const __m128i * const B,__m128i * const U,__m128i * const V)392 static WEBP_INLINE void ConvertRGBToUV_SSE41(const __m128i* const R,
393 const __m128i* const G,
394 const __m128i* const B,
395 __m128i* const U,
396 __m128i* const V) {
397 const __m128i kRG_u = MK_CST_16(-9719, -19081);
398 const __m128i kGB_u = MK_CST_16(0, 28800);
399 const __m128i kRG_v = MK_CST_16(28800, 0);
400 const __m128i kGB_v = MK_CST_16(-24116, -4684);
401 const __m128i kHALF_UV = _mm_set1_epi32(((128 << YUV_FIX) + YUV_HALF) << 2);
402
403 const __m128i RG_lo = _mm_unpacklo_epi16(*R, *G);
404 const __m128i RG_hi = _mm_unpackhi_epi16(*R, *G);
405 const __m128i GB_lo = _mm_unpacklo_epi16(*G, *B);
406 const __m128i GB_hi = _mm_unpackhi_epi16(*G, *B);
407 TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_u, kGB_u,
408 kHALF_UV, YUV_FIX + 2, *U);
409 TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_v, kGB_v,
410 kHALF_UV, YUV_FIX + 2, *V);
411 }
412
413 #undef MK_CST_16
414 #undef TRANSFORM
415
ConvertRGB24ToY_SSE41(const uint8_t * WEBP_RESTRICT rgb,uint8_t * WEBP_RESTRICT y,int width)416 static void ConvertRGB24ToY_SSE41(const uint8_t* WEBP_RESTRICT rgb,
417 uint8_t* WEBP_RESTRICT y, int width) {
418 const int max_width = width & ~31;
419 int i;
420 for (i = 0; i < max_width; rgb += 3 * 16 * 2) {
421 __m128i rgb_plane[6];
422 int j;
423
424 RGB24PackedToPlanar_SSE41(rgb, rgb_plane);
425
426 for (j = 0; j < 2; ++j, i += 16) {
427 const __m128i zero = _mm_setzero_si128();
428 __m128i r, g, b, Y0, Y1;
429
430 // Convert to 16-bit Y.
431 r = _mm_unpacklo_epi8(rgb_plane[0 + j], zero);
432 g = _mm_unpacklo_epi8(rgb_plane[2 + j], zero);
433 b = _mm_unpacklo_epi8(rgb_plane[4 + j], zero);
434 ConvertRGBToY_SSE41(&r, &g, &b, &Y0);
435
436 // Convert to 16-bit Y.
437 r = _mm_unpackhi_epi8(rgb_plane[0 + j], zero);
438 g = _mm_unpackhi_epi8(rgb_plane[2 + j], zero);
439 b = _mm_unpackhi_epi8(rgb_plane[4 + j], zero);
440 ConvertRGBToY_SSE41(&r, &g, &b, &Y1);
441
442 // Cast to 8-bit and store.
443 STORE_16(_mm_packus_epi16(Y0, Y1), y + i);
444 }
445 }
446 for (; i < width; ++i, rgb += 3) { // left-over
447 y[i] = VP8RGBToY(rgb[0], rgb[1], rgb[2], YUV_HALF);
448 }
449 }
450
ConvertBGR24ToY_SSE41(const uint8_t * WEBP_RESTRICT bgr,uint8_t * WEBP_RESTRICT y,int width)451 static void ConvertBGR24ToY_SSE41(const uint8_t* WEBP_RESTRICT bgr,
452 uint8_t* WEBP_RESTRICT y, int width) {
453 const int max_width = width & ~31;
454 int i;
455 for (i = 0; i < max_width; bgr += 3 * 16 * 2) {
456 __m128i bgr_plane[6];
457 int j;
458
459 RGB24PackedToPlanar_SSE41(bgr, bgr_plane);
460
461 for (j = 0; j < 2; ++j, i += 16) {
462 const __m128i zero = _mm_setzero_si128();
463 __m128i r, g, b, Y0, Y1;
464
465 // Convert to 16-bit Y.
466 b = _mm_unpacklo_epi8(bgr_plane[0 + j], zero);
467 g = _mm_unpacklo_epi8(bgr_plane[2 + j], zero);
468 r = _mm_unpacklo_epi8(bgr_plane[4 + j], zero);
469 ConvertRGBToY_SSE41(&r, &g, &b, &Y0);
470
471 // Convert to 16-bit Y.
472 b = _mm_unpackhi_epi8(bgr_plane[0 + j], zero);
473 g = _mm_unpackhi_epi8(bgr_plane[2 + j], zero);
474 r = _mm_unpackhi_epi8(bgr_plane[4 + j], zero);
475 ConvertRGBToY_SSE41(&r, &g, &b, &Y1);
476
477 // Cast to 8-bit and store.
478 STORE_16(_mm_packus_epi16(Y0, Y1), y + i);
479 }
480 }
481 for (; i < width; ++i, bgr += 3) { // left-over
482 y[i] = VP8RGBToY(bgr[2], bgr[1], bgr[0], YUV_HALF);
483 }
484 }
485
ConvertARGBToY_SSE41(const uint32_t * WEBP_RESTRICT argb,uint8_t * WEBP_RESTRICT y,int width)486 static void ConvertARGBToY_SSE41(const uint32_t* WEBP_RESTRICT argb,
487 uint8_t* WEBP_RESTRICT y, int width) {
488 const int max_width = width & ~15;
489 int i;
490 for (i = 0; i < max_width; i += 16) {
491 __m128i Y0, Y1, rgb[6];
492 RGB32PackedToPlanar_SSE41(&argb[i], rgb);
493 ConvertRGBToY_SSE41(&rgb[0], &rgb[2], &rgb[4], &Y0);
494 ConvertRGBToY_SSE41(&rgb[1], &rgb[3], &rgb[5], &Y1);
495 STORE_16(_mm_packus_epi16(Y0, Y1), y + i);
496 }
497 for (; i < width; ++i) { // left-over
498 const uint32_t p = argb[i];
499 y[i] = VP8RGBToY((p >> 16) & 0xff, (p >> 8) & 0xff, (p >> 0) & 0xff,
500 YUV_HALF);
501 }
502 }
503
504 // Horizontal add (doubled) of two 16b values, result is 16b.
505 // in: A | B | C | D | ... -> out: 2*(A+B) | 2*(C+D) | ...
HorizontalAddPack_SSE41(const __m128i * const A,const __m128i * const B,__m128i * const out)506 static void HorizontalAddPack_SSE41(const __m128i* const A,
507 const __m128i* const B,
508 __m128i* const out) {
509 const __m128i k2 = _mm_set1_epi16(2);
510 const __m128i C = _mm_madd_epi16(*A, k2);
511 const __m128i D = _mm_madd_epi16(*B, k2);
512 *out = _mm_packs_epi32(C, D);
513 }
514
ConvertARGBToUV_SSE41(const uint32_t * WEBP_RESTRICT argb,uint8_t * WEBP_RESTRICT u,uint8_t * WEBP_RESTRICT v,int src_width,int do_store)515 static void ConvertARGBToUV_SSE41(const uint32_t* WEBP_RESTRICT argb,
516 uint8_t* WEBP_RESTRICT u,
517 uint8_t* WEBP_RESTRICT v,
518 int src_width, int do_store) {
519 const int max_width = src_width & ~31;
520 int i;
521 for (i = 0; i < max_width; i += 32, u += 16, v += 16) {
522 __m128i rgb[6], U0, V0, U1, V1;
523 RGB32PackedToPlanar_SSE41(&argb[i], rgb);
524 HorizontalAddPack_SSE41(&rgb[0], &rgb[1], &rgb[0]);
525 HorizontalAddPack_SSE41(&rgb[2], &rgb[3], &rgb[2]);
526 HorizontalAddPack_SSE41(&rgb[4], &rgb[5], &rgb[4]);
527 ConvertRGBToUV_SSE41(&rgb[0], &rgb[2], &rgb[4], &U0, &V0);
528
529 RGB32PackedToPlanar_SSE41(&argb[i + 16], rgb);
530 HorizontalAddPack_SSE41(&rgb[0], &rgb[1], &rgb[0]);
531 HorizontalAddPack_SSE41(&rgb[2], &rgb[3], &rgb[2]);
532 HorizontalAddPack_SSE41(&rgb[4], &rgb[5], &rgb[4]);
533 ConvertRGBToUV_SSE41(&rgb[0], &rgb[2], &rgb[4], &U1, &V1);
534
535 U0 = _mm_packus_epi16(U0, U1);
536 V0 = _mm_packus_epi16(V0, V1);
537 if (!do_store) {
538 const __m128i prev_u = LOAD_16(u);
539 const __m128i prev_v = LOAD_16(v);
540 U0 = _mm_avg_epu8(U0, prev_u);
541 V0 = _mm_avg_epu8(V0, prev_v);
542 }
543 STORE_16(U0, u);
544 STORE_16(V0, v);
545 }
546 if (i < src_width) { // left-over
547 WebPConvertARGBToUV_C(argb + i, u, v, src_width - i, do_store);
548 }
549 }
550
551 // Convert 16 packed ARGB 16b-values to r[], g[], b[]
RGBA32PackedToPlanar_16b_SSE41(const uint16_t * WEBP_RESTRICT const rgbx,__m128i * const r,__m128i * const g,__m128i * const b)552 static WEBP_INLINE void RGBA32PackedToPlanar_16b_SSE41(
553 const uint16_t* WEBP_RESTRICT const rgbx,
554 __m128i* const r, __m128i* const g, __m128i* const b) {
555 const __m128i in0 = LOAD_16(rgbx + 0); // r0 | g0 | b0 |x| r1 | g1 | b1 |x
556 const __m128i in1 = LOAD_16(rgbx + 8); // r2 | g2 | b2 |x| r3 | g3 | b3 |x
557 const __m128i in2 = LOAD_16(rgbx + 16); // r4 | ...
558 const __m128i in3 = LOAD_16(rgbx + 24); // r6 | ...
559 // aarrggbb as 16-bit.
560 const __m128i shuff0 =
561 _mm_set_epi8(-1, -1, -1, -1, 13, 12, 5, 4, 11, 10, 3, 2, 9, 8, 1, 0);
562 const __m128i shuff1 =
563 _mm_set_epi8(13, 12, 5, 4, -1, -1, -1, -1, 11, 10, 3, 2, 9, 8, 1, 0);
564 const __m128i A0 = _mm_shuffle_epi8(in0, shuff0);
565 const __m128i A1 = _mm_shuffle_epi8(in1, shuff1);
566 const __m128i A2 = _mm_shuffle_epi8(in2, shuff0);
567 const __m128i A3 = _mm_shuffle_epi8(in3, shuff1);
568 // R0R1G0G1
569 // B0B1****
570 // R2R3G2G3
571 // B2B3****
572 // (OR is used to free port 5 for the unpack)
573 const __m128i B0 = _mm_unpacklo_epi32(A0, A1);
574 const __m128i B1 = _mm_or_si128(A0, A1);
575 const __m128i B2 = _mm_unpacklo_epi32(A2, A3);
576 const __m128i B3 = _mm_or_si128(A2, A3);
577 // Gather the channels.
578 *r = _mm_unpacklo_epi64(B0, B2);
579 *g = _mm_unpackhi_epi64(B0, B2);
580 *b = _mm_unpackhi_epi64(B1, B3);
581 }
582
ConvertRGBA32ToUV_SSE41(const uint16_t * WEBP_RESTRICT rgb,uint8_t * WEBP_RESTRICT u,uint8_t * WEBP_RESTRICT v,int width)583 static void ConvertRGBA32ToUV_SSE41(const uint16_t* WEBP_RESTRICT rgb,
584 uint8_t* WEBP_RESTRICT u,
585 uint8_t* WEBP_RESTRICT v, int width) {
586 const int max_width = width & ~15;
587 const uint16_t* const last_rgb = rgb + 4 * max_width;
588 while (rgb < last_rgb) {
589 __m128i r, g, b, U0, V0, U1, V1;
590 RGBA32PackedToPlanar_16b_SSE41(rgb + 0, &r, &g, &b);
591 ConvertRGBToUV_SSE41(&r, &g, &b, &U0, &V0);
592 RGBA32PackedToPlanar_16b_SSE41(rgb + 32, &r, &g, &b);
593 ConvertRGBToUV_SSE41(&r, &g, &b, &U1, &V1);
594 STORE_16(_mm_packus_epi16(U0, U1), u);
595 STORE_16(_mm_packus_epi16(V0, V1), v);
596 u += 16;
597 v += 16;
598 rgb += 2 * 32;
599 }
600 if (max_width < width) { // left-over
601 WebPConvertRGBA32ToUV_C(rgb, u, v, width - max_width);
602 }
603 }
604
605 //------------------------------------------------------------------------------
606
607 extern void WebPInitConvertARGBToYUVSSE41(void);
608
WebPInitConvertARGBToYUVSSE41(void)609 WEBP_TSAN_IGNORE_FUNCTION void WebPInitConvertARGBToYUVSSE41(void) {
610 WebPConvertARGBToY = ConvertARGBToY_SSE41;
611 WebPConvertARGBToUV = ConvertARGBToUV_SSE41;
612
613 WebPConvertRGB24ToY = ConvertRGB24ToY_SSE41;
614 WebPConvertBGR24ToY = ConvertBGR24ToY_SSE41;
615
616 WebPConvertRGBA32ToUV = ConvertRGBA32ToUV_SSE41;
617 }
618
619 //------------------------------------------------------------------------------
620
621 #else // !WEBP_USE_SSE41
622
623 WEBP_DSP_INIT_STUB(WebPInitSamplersSSE41)
624 WEBP_DSP_INIT_STUB(WebPInitConvertARGBToYUVSSE41)
625
626 #endif // WEBP_USE_SSE41
627