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 // SSE2 variant of methods for lossless decoder
11 //
12 // Author: Skal (pascal.massimino@gmail.com)
13
14 #include "./dsp.h"
15
16 #if defined(WEBP_USE_SSE2)
17 #include <assert.h>
18 #include <emmintrin.h>
19 #include "./lossless.h"
20
21 //------------------------------------------------------------------------------
22 // Predictor Transform
23
ClampedAddSubtractFull(uint32_t c0,uint32_t c1,uint32_t c2)24 static WEBP_INLINE uint32_t ClampedAddSubtractFull(uint32_t c0, uint32_t c1,
25 uint32_t c2) {
26 const __m128i zero = _mm_setzero_si128();
27 const __m128i C0 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(c0), zero);
28 const __m128i C1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(c1), zero);
29 const __m128i C2 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(c2), zero);
30 const __m128i V1 = _mm_add_epi16(C0, C1);
31 const __m128i V2 = _mm_sub_epi16(V1, C2);
32 const __m128i b = _mm_packus_epi16(V2, V2);
33 const uint32_t output = _mm_cvtsi128_si32(b);
34 return output;
35 }
36
ClampedAddSubtractHalf(uint32_t c0,uint32_t c1,uint32_t c2)37 static WEBP_INLINE uint32_t ClampedAddSubtractHalf(uint32_t c0, uint32_t c1,
38 uint32_t c2) {
39 const __m128i zero = _mm_setzero_si128();
40 const __m128i C0 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(c0), zero);
41 const __m128i C1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(c1), zero);
42 const __m128i B0 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(c2), zero);
43 const __m128i avg = _mm_add_epi16(C1, C0);
44 const __m128i A0 = _mm_srli_epi16(avg, 1);
45 const __m128i A1 = _mm_sub_epi16(A0, B0);
46 const __m128i BgtA = _mm_cmpgt_epi16(B0, A0);
47 const __m128i A2 = _mm_sub_epi16(A1, BgtA);
48 const __m128i A3 = _mm_srai_epi16(A2, 1);
49 const __m128i A4 = _mm_add_epi16(A0, A3);
50 const __m128i A5 = _mm_packus_epi16(A4, A4);
51 const uint32_t output = _mm_cvtsi128_si32(A5);
52 return output;
53 }
54
Select(uint32_t a,uint32_t b,uint32_t c)55 static WEBP_INLINE uint32_t Select(uint32_t a, uint32_t b, uint32_t c) {
56 int pa_minus_pb;
57 const __m128i zero = _mm_setzero_si128();
58 const __m128i A0 = _mm_cvtsi32_si128(a);
59 const __m128i B0 = _mm_cvtsi32_si128(b);
60 const __m128i C0 = _mm_cvtsi32_si128(c);
61 const __m128i AC0 = _mm_subs_epu8(A0, C0);
62 const __m128i CA0 = _mm_subs_epu8(C0, A0);
63 const __m128i BC0 = _mm_subs_epu8(B0, C0);
64 const __m128i CB0 = _mm_subs_epu8(C0, B0);
65 const __m128i AC = _mm_or_si128(AC0, CA0);
66 const __m128i BC = _mm_or_si128(BC0, CB0);
67 const __m128i pa = _mm_unpacklo_epi8(AC, zero); // |a - c|
68 const __m128i pb = _mm_unpacklo_epi8(BC, zero); // |b - c|
69 const __m128i diff = _mm_sub_epi16(pb, pa);
70 {
71 int16_t out[8];
72 _mm_storeu_si128((__m128i*)out, diff);
73 pa_minus_pb = out[0] + out[1] + out[2] + out[3];
74 }
75 return (pa_minus_pb <= 0) ? a : b;
76 }
77
Average2_128i(uint32_t a0,uint32_t a1)78 static WEBP_INLINE __m128i Average2_128i(uint32_t a0, uint32_t a1) {
79 const __m128i zero = _mm_setzero_si128();
80 const __m128i A0 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(a0), zero);
81 const __m128i A1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(a1), zero);
82 const __m128i sum = _mm_add_epi16(A1, A0);
83 const __m128i avg = _mm_srli_epi16(sum, 1);
84 return avg;
85 }
86
Average2(uint32_t a0,uint32_t a1)87 static WEBP_INLINE uint32_t Average2(uint32_t a0, uint32_t a1) {
88 const __m128i avg = Average2_128i(a0, a1);
89 const __m128i A2 = _mm_packus_epi16(avg, avg);
90 const uint32_t output = _mm_cvtsi128_si32(A2);
91 return output;
92 }
93
Average3(uint32_t a0,uint32_t a1,uint32_t a2)94 static WEBP_INLINE uint32_t Average3(uint32_t a0, uint32_t a1, uint32_t a2) {
95 const __m128i zero = _mm_setzero_si128();
96 const __m128i avg1 = Average2_128i(a0, a2);
97 const __m128i A1 = _mm_unpacklo_epi8(_mm_cvtsi32_si128(a1), zero);
98 const __m128i sum = _mm_add_epi16(avg1, A1);
99 const __m128i avg2 = _mm_srli_epi16(sum, 1);
100 const __m128i A2 = _mm_packus_epi16(avg2, avg2);
101 const uint32_t output = _mm_cvtsi128_si32(A2);
102 return output;
103 }
104
Average4(uint32_t a0,uint32_t a1,uint32_t a2,uint32_t a3)105 static WEBP_INLINE uint32_t Average4(uint32_t a0, uint32_t a1,
106 uint32_t a2, uint32_t a3) {
107 const __m128i avg1 = Average2_128i(a0, a1);
108 const __m128i avg2 = Average2_128i(a2, a3);
109 const __m128i sum = _mm_add_epi16(avg2, avg1);
110 const __m128i avg3 = _mm_srli_epi16(sum, 1);
111 const __m128i A0 = _mm_packus_epi16(avg3, avg3);
112 const uint32_t output = _mm_cvtsi128_si32(A0);
113 return output;
114 }
115
Predictor5(uint32_t left,const uint32_t * const top)116 static uint32_t Predictor5(uint32_t left, const uint32_t* const top) {
117 const uint32_t pred = Average3(left, top[0], top[1]);
118 return pred;
119 }
Predictor6(uint32_t left,const uint32_t * const top)120 static uint32_t Predictor6(uint32_t left, const uint32_t* const top) {
121 const uint32_t pred = Average2(left, top[-1]);
122 return pred;
123 }
Predictor7(uint32_t left,const uint32_t * const top)124 static uint32_t Predictor7(uint32_t left, const uint32_t* const top) {
125 const uint32_t pred = Average2(left, top[0]);
126 return pred;
127 }
Predictor8(uint32_t left,const uint32_t * const top)128 static uint32_t Predictor8(uint32_t left, const uint32_t* const top) {
129 const uint32_t pred = Average2(top[-1], top[0]);
130 (void)left;
131 return pred;
132 }
Predictor9(uint32_t left,const uint32_t * const top)133 static uint32_t Predictor9(uint32_t left, const uint32_t* const top) {
134 const uint32_t pred = Average2(top[0], top[1]);
135 (void)left;
136 return pred;
137 }
Predictor10(uint32_t left,const uint32_t * const top)138 static uint32_t Predictor10(uint32_t left, const uint32_t* const top) {
139 const uint32_t pred = Average4(left, top[-1], top[0], top[1]);
140 return pred;
141 }
Predictor11(uint32_t left,const uint32_t * const top)142 static uint32_t Predictor11(uint32_t left, const uint32_t* const top) {
143 const uint32_t pred = Select(top[0], left, top[-1]);
144 return pred;
145 }
Predictor12(uint32_t left,const uint32_t * const top)146 static uint32_t Predictor12(uint32_t left, const uint32_t* const top) {
147 const uint32_t pred = ClampedAddSubtractFull(left, top[0], top[-1]);
148 return pred;
149 }
Predictor13(uint32_t left,const uint32_t * const top)150 static uint32_t Predictor13(uint32_t left, const uint32_t* const top) {
151 const uint32_t pred = ClampedAddSubtractHalf(left, top[0], top[-1]);
152 return pred;
153 }
154
155 //------------------------------------------------------------------------------
156 // Subtract-Green Transform
157
AddGreenToBlueAndRed(uint32_t * argb_data,int num_pixels)158 static void AddGreenToBlueAndRed(uint32_t* argb_data, int num_pixels) {
159 int i;
160 for (i = 0; i + 4 <= num_pixels; i += 4) {
161 const __m128i in = _mm_loadu_si128((__m128i*)&argb_data[i]); // argb
162 const __m128i A = _mm_srli_epi16(in, 8); // 0 a 0 g
163 const __m128i B = _mm_shufflelo_epi16(A, _MM_SHUFFLE(2, 2, 0, 0));
164 const __m128i C = _mm_shufflehi_epi16(B, _MM_SHUFFLE(2, 2, 0, 0)); // 0g0g
165 const __m128i out = _mm_add_epi8(in, C);
166 _mm_storeu_si128((__m128i*)&argb_data[i], out);
167 }
168 // fallthrough and finish off with plain-C
169 VP8LAddGreenToBlueAndRed_C(argb_data + i, num_pixels - i);
170 }
171
172 //------------------------------------------------------------------------------
173 // Color Transform
174
TransformColorInverse(const VP8LMultipliers * const m,uint32_t * argb_data,int num_pixels)175 static void TransformColorInverse(const VP8LMultipliers* const m,
176 uint32_t* argb_data, int num_pixels) {
177 // sign-extended multiplying constants, pre-shifted by 5.
178 #define CST(X) (((int16_t)(m->X << 8)) >> 5) // sign-extend
179 const __m128i mults_rb = _mm_set_epi16(
180 CST(green_to_red_), CST(green_to_blue_),
181 CST(green_to_red_), CST(green_to_blue_),
182 CST(green_to_red_), CST(green_to_blue_),
183 CST(green_to_red_), CST(green_to_blue_));
184 const __m128i mults_b2 = _mm_set_epi16(
185 CST(red_to_blue_), 0, CST(red_to_blue_), 0,
186 CST(red_to_blue_), 0, CST(red_to_blue_), 0);
187 #undef CST
188 const __m128i mask_ag = _mm_set1_epi32(0xff00ff00); // alpha-green masks
189 int i;
190 for (i = 0; i + 4 <= num_pixels; i += 4) {
191 const __m128i in = _mm_loadu_si128((__m128i*)&argb_data[i]); // argb
192 const __m128i A = _mm_and_si128(in, mask_ag); // a 0 g 0
193 const __m128i B = _mm_shufflelo_epi16(A, _MM_SHUFFLE(2, 2, 0, 0));
194 const __m128i C = _mm_shufflehi_epi16(B, _MM_SHUFFLE(2, 2, 0, 0)); // g0g0
195 const __m128i D = _mm_mulhi_epi16(C, mults_rb); // x dr x db1
196 const __m128i E = _mm_add_epi8(in, D); // x r' x b'
197 const __m128i F = _mm_slli_epi16(E, 8); // r' 0 b' 0
198 const __m128i G = _mm_mulhi_epi16(F, mults_b2); // x db2 0 0
199 const __m128i H = _mm_srli_epi32(G, 8); // 0 x db2 0
200 const __m128i I = _mm_add_epi8(H, F); // r' x b'' 0
201 const __m128i J = _mm_srli_epi16(I, 8); // 0 r' 0 b''
202 const __m128i out = _mm_or_si128(J, A);
203 _mm_storeu_si128((__m128i*)&argb_data[i], out);
204 }
205 // Fall-back to C-version for left-overs.
206 VP8LTransformColorInverse_C(m, argb_data + i, num_pixels - i);
207 }
208
209 //------------------------------------------------------------------------------
210 // Color-space conversion functions
211
ConvertBGRAToRGBA(const uint32_t * src,int num_pixels,uint8_t * dst)212 static void ConvertBGRAToRGBA(const uint32_t* src,
213 int num_pixels, uint8_t* dst) {
214 const __m128i* in = (const __m128i*)src;
215 __m128i* out = (__m128i*)dst;
216 while (num_pixels >= 8) {
217 const __m128i bgra0 = _mm_loadu_si128(in++); // bgra0|bgra1|bgra2|bgra3
218 const __m128i bgra4 = _mm_loadu_si128(in++); // bgra4|bgra5|bgra6|bgra7
219 const __m128i v0l = _mm_unpacklo_epi8(bgra0, bgra4); // b0b4g0g4r0r4a0a4...
220 const __m128i v0h = _mm_unpackhi_epi8(bgra0, bgra4); // b2b6g2g6r2r6a2a6...
221 const __m128i v1l = _mm_unpacklo_epi8(v0l, v0h); // b0b2b4b6g0g2g4g6...
222 const __m128i v1h = _mm_unpackhi_epi8(v0l, v0h); // b1b3b5b7g1g3g5g7...
223 const __m128i v2l = _mm_unpacklo_epi8(v1l, v1h); // b0...b7 | g0...g7
224 const __m128i v2h = _mm_unpackhi_epi8(v1l, v1h); // r0...r7 | a0...a7
225 const __m128i ga0 = _mm_unpackhi_epi64(v2l, v2h); // g0...g7 | a0...a7
226 const __m128i rb0 = _mm_unpacklo_epi64(v2h, v2l); // r0...r7 | b0...b7
227 const __m128i rg0 = _mm_unpacklo_epi8(rb0, ga0); // r0g0r1g1 ... r6g6r7g7
228 const __m128i ba0 = _mm_unpackhi_epi8(rb0, ga0); // b0a0b1a1 ... b6a6b7a7
229 const __m128i rgba0 = _mm_unpacklo_epi16(rg0, ba0); // rgba0|rgba1...
230 const __m128i rgba4 = _mm_unpackhi_epi16(rg0, ba0); // rgba4|rgba5...
231 _mm_storeu_si128(out++, rgba0);
232 _mm_storeu_si128(out++, rgba4);
233 num_pixels -= 8;
234 }
235 // left-overs
236 VP8LConvertBGRAToRGBA_C((const uint32_t*)in, num_pixels, (uint8_t*)out);
237 }
238
ConvertBGRAToRGBA4444(const uint32_t * src,int num_pixels,uint8_t * dst)239 static void ConvertBGRAToRGBA4444(const uint32_t* src,
240 int num_pixels, uint8_t* dst) {
241 const __m128i mask_0x0f = _mm_set1_epi8(0x0f);
242 const __m128i mask_0xf0 = _mm_set1_epi8(0xf0);
243 const __m128i* in = (const __m128i*)src;
244 __m128i* out = (__m128i*)dst;
245 while (num_pixels >= 8) {
246 const __m128i bgra0 = _mm_loadu_si128(in++); // bgra0|bgra1|bgra2|bgra3
247 const __m128i bgra4 = _mm_loadu_si128(in++); // bgra4|bgra5|bgra6|bgra7
248 const __m128i v0l = _mm_unpacklo_epi8(bgra0, bgra4); // b0b4g0g4r0r4a0a4...
249 const __m128i v0h = _mm_unpackhi_epi8(bgra0, bgra4); // b2b6g2g6r2r6a2a6...
250 const __m128i v1l = _mm_unpacklo_epi8(v0l, v0h); // b0b2b4b6g0g2g4g6...
251 const __m128i v1h = _mm_unpackhi_epi8(v0l, v0h); // b1b3b5b7g1g3g5g7...
252 const __m128i v2l = _mm_unpacklo_epi8(v1l, v1h); // b0...b7 | g0...g7
253 const __m128i v2h = _mm_unpackhi_epi8(v1l, v1h); // r0...r7 | a0...a7
254 const __m128i ga0 = _mm_unpackhi_epi64(v2l, v2h); // g0...g7 | a0...a7
255 const __m128i rb0 = _mm_unpacklo_epi64(v2h, v2l); // r0...r7 | b0...b7
256 const __m128i ga1 = _mm_srli_epi16(ga0, 4); // g0-|g1-|...|a6-|a7-
257 const __m128i rb1 = _mm_and_si128(rb0, mask_0xf0); // -r0|-r1|...|-b6|-a7
258 const __m128i ga2 = _mm_and_si128(ga1, mask_0x0f); // g0-|g1-|...|a6-|a7-
259 const __m128i rgba0 = _mm_or_si128(ga2, rb1); // rg0..rg7 | ba0..ba7
260 const __m128i rgba1 = _mm_srli_si128(rgba0, 8); // ba0..ba7 | 0
261 #ifdef WEBP_SWAP_16BIT_CSP
262 const __m128i rgba = _mm_unpacklo_epi8(rgba1, rgba0); // barg0...barg7
263 #else
264 const __m128i rgba = _mm_unpacklo_epi8(rgba0, rgba1); // rgba0...rgba7
265 #endif
266 _mm_storeu_si128(out++, rgba);
267 num_pixels -= 8;
268 }
269 // left-overs
270 VP8LConvertBGRAToRGBA4444_C((const uint32_t*)in, num_pixels, (uint8_t*)out);
271 }
272
ConvertBGRAToRGB565(const uint32_t * src,int num_pixels,uint8_t * dst)273 static void ConvertBGRAToRGB565(const uint32_t* src,
274 int num_pixels, uint8_t* dst) {
275 const __m128i mask_0xe0 = _mm_set1_epi8(0xe0);
276 const __m128i mask_0xf8 = _mm_set1_epi8(0xf8);
277 const __m128i mask_0x07 = _mm_set1_epi8(0x07);
278 const __m128i* in = (const __m128i*)src;
279 __m128i* out = (__m128i*)dst;
280 while (num_pixels >= 8) {
281 const __m128i bgra0 = _mm_loadu_si128(in++); // bgra0|bgra1|bgra2|bgra3
282 const __m128i bgra4 = _mm_loadu_si128(in++); // bgra4|bgra5|bgra6|bgra7
283 const __m128i v0l = _mm_unpacklo_epi8(bgra0, bgra4); // b0b4g0g4r0r4a0a4...
284 const __m128i v0h = _mm_unpackhi_epi8(bgra0, bgra4); // b2b6g2g6r2r6a2a6...
285 const __m128i v1l = _mm_unpacklo_epi8(v0l, v0h); // b0b2b4b6g0g2g4g6...
286 const __m128i v1h = _mm_unpackhi_epi8(v0l, v0h); // b1b3b5b7g1g3g5g7...
287 const __m128i v2l = _mm_unpacklo_epi8(v1l, v1h); // b0...b7 | g0...g7
288 const __m128i v2h = _mm_unpackhi_epi8(v1l, v1h); // r0...r7 | a0...a7
289 const __m128i ga0 = _mm_unpackhi_epi64(v2l, v2h); // g0...g7 | a0...a7
290 const __m128i rb0 = _mm_unpacklo_epi64(v2h, v2l); // r0...r7 | b0...b7
291 const __m128i rb1 = _mm_and_si128(rb0, mask_0xf8); // -r0..-r7|-b0..-b7
292 const __m128i g_lo1 = _mm_srli_epi16(ga0, 5);
293 const __m128i g_lo2 = _mm_and_si128(g_lo1, mask_0x07); // g0-...g7-|xx (3b)
294 const __m128i g_hi1 = _mm_slli_epi16(ga0, 3);
295 const __m128i g_hi2 = _mm_and_si128(g_hi1, mask_0xe0); // -g0...-g7|xx (3b)
296 const __m128i b0 = _mm_srli_si128(rb1, 8); // -b0...-b7|0
297 const __m128i rg1 = _mm_or_si128(rb1, g_lo2); // gr0...gr7|xx
298 const __m128i b1 = _mm_srli_epi16(b0, 3);
299 const __m128i gb1 = _mm_or_si128(b1, g_hi2); // bg0...bg7|xx
300 #ifdef WEBP_SWAP_16BIT_CSP
301 const __m128i rgba = _mm_unpacklo_epi8(gb1, rg1); // rggb0...rggb7
302 #else
303 const __m128i rgba = _mm_unpacklo_epi8(rg1, gb1); // bgrb0...bgrb7
304 #endif
305 _mm_storeu_si128(out++, rgba);
306 num_pixels -= 8;
307 }
308 // left-overs
309 VP8LConvertBGRAToRGB565_C((const uint32_t*)in, num_pixels, (uint8_t*)out);
310 }
311
ConvertBGRAToBGR(const uint32_t * src,int num_pixels,uint8_t * dst)312 static void ConvertBGRAToBGR(const uint32_t* src,
313 int num_pixels, uint8_t* dst) {
314 const __m128i mask_l = _mm_set_epi32(0, 0x00ffffff, 0, 0x00ffffff);
315 const __m128i mask_h = _mm_set_epi32(0x00ffffff, 0, 0x00ffffff, 0);
316 const __m128i* in = (const __m128i*)src;
317 const uint8_t* const end = dst + num_pixels * 3;
318 // the last storel_epi64 below writes 8 bytes starting at offset 18
319 while (dst + 26 <= end) {
320 const __m128i bgra0 = _mm_loadu_si128(in++); // bgra0|bgra1|bgra2|bgra3
321 const __m128i bgra4 = _mm_loadu_si128(in++); // bgra4|bgra5|bgra6|bgra7
322 const __m128i a0l = _mm_and_si128(bgra0, mask_l); // bgr0|0|bgr0|0
323 const __m128i a4l = _mm_and_si128(bgra4, mask_l); // bgr0|0|bgr0|0
324 const __m128i a0h = _mm_and_si128(bgra0, mask_h); // 0|bgr0|0|bgr0
325 const __m128i a4h = _mm_and_si128(bgra4, mask_h); // 0|bgr0|0|bgr0
326 const __m128i b0h = _mm_srli_epi64(a0h, 8); // 000b|gr00|000b|gr00
327 const __m128i b4h = _mm_srli_epi64(a4h, 8); // 000b|gr00|000b|gr00
328 const __m128i c0 = _mm_or_si128(a0l, b0h); // rgbrgb00|rgbrgb00
329 const __m128i c4 = _mm_or_si128(a4l, b4h); // rgbrgb00|rgbrgb00
330 const __m128i c2 = _mm_srli_si128(c0, 8);
331 const __m128i c6 = _mm_srli_si128(c4, 8);
332 _mm_storel_epi64((__m128i*)(dst + 0), c0);
333 _mm_storel_epi64((__m128i*)(dst + 6), c2);
334 _mm_storel_epi64((__m128i*)(dst + 12), c4);
335 _mm_storel_epi64((__m128i*)(dst + 18), c6);
336 dst += 24;
337 num_pixels -= 8;
338 }
339 // left-overs
340 VP8LConvertBGRAToBGR_C((const uint32_t*)in, num_pixels, dst);
341 }
342
343 //------------------------------------------------------------------------------
344 // Entry point
345
346 extern void VP8LDspInitSSE2(void);
347
VP8LDspInitSSE2(void)348 WEBP_TSAN_IGNORE_FUNCTION void VP8LDspInitSSE2(void) {
349 VP8LPredictors[5] = Predictor5;
350 VP8LPredictors[6] = Predictor6;
351 VP8LPredictors[7] = Predictor7;
352 VP8LPredictors[8] = Predictor8;
353 VP8LPredictors[9] = Predictor9;
354 VP8LPredictors[10] = Predictor10;
355 VP8LPredictors[11] = Predictor11;
356 VP8LPredictors[12] = Predictor12;
357 VP8LPredictors[13] = Predictor13;
358
359 VP8LAddGreenToBlueAndRed = AddGreenToBlueAndRed;
360 VP8LTransformColorInverse = TransformColorInverse;
361
362 VP8LConvertBGRAToRGBA = ConvertBGRAToRGBA;
363 VP8LConvertBGRAToRGBA4444 = ConvertBGRAToRGBA4444;
364 VP8LConvertBGRAToRGB565 = ConvertBGRAToRGB565;
365 VP8LConvertBGRAToBGR = ConvertBGRAToBGR;
366 }
367
368 #else // !WEBP_USE_SSE2
369
370 WEBP_DSP_INIT_STUB(VP8LDspInitSSE2)
371
372 #endif // WEBP_USE_SSE2
373