1 | // Copyright 2015 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 encoder |
11 | // |
12 | // Author: Skal (pascal.massimino@gmail.com) |
13 | |
14 | #include "src/dsp/dsp.h" |
15 | |
16 | #if defined(WEBP_USE_SSE2) |
17 | #include <assert.h> |
18 | #include <emmintrin.h> |
19 | #include "src/dsp/lossless.h" |
20 | #include "src/dsp/common_sse2.h" |
21 | #include "src/dsp/lossless_common.h" |
22 | |
23 | // For sign-extended multiplying constants, pre-shifted by 5: |
24 | #define CST_5b(X) (((int16_t)((uint16_t)(X) << 8)) >> 5) |
25 | |
26 | //------------------------------------------------------------------------------ |
27 | // Subtract-Green Transform |
28 | |
29 | static void SubtractGreenFromBlueAndRed_SSE2(uint32_t* argb_data, |
30 | int num_pixels) { |
31 | int i; |
32 | for (i = 0; i + 4 <= num_pixels; i += 4) { |
33 | const __m128i in = _mm_loadu_si128((__m128i*)&argb_data[i]); // argb |
34 | const __m128i A = _mm_srli_epi16(in, 8); // 0 a 0 g |
35 | const __m128i B = _mm_shufflelo_epi16(A, _MM_SHUFFLE(2, 2, 0, 0)); |
36 | const __m128i C = _mm_shufflehi_epi16(B, _MM_SHUFFLE(2, 2, 0, 0)); // 0g0g |
37 | const __m128i out = _mm_sub_epi8(in, C); |
38 | _mm_storeu_si128((__m128i*)&argb_data[i], out); |
39 | } |
40 | // fallthrough and finish off with plain-C |
41 | if (i != num_pixels) { |
42 | VP8LSubtractGreenFromBlueAndRed_C(argb_data + i, num_pixels - i); |
43 | } |
44 | } |
45 | |
46 | //------------------------------------------------------------------------------ |
47 | // Color Transform |
48 | |
49 | #define MK_CST_16(HI, LO) \ |
50 | _mm_set1_epi32((int)(((uint32_t)(HI) << 16) | ((LO) & 0xffff))) |
51 | |
52 | static void TransformColor_SSE2(const VP8LMultipliers* const m, |
53 | uint32_t* argb_data, int num_pixels) { |
54 | const __m128i mults_rb = MK_CST_16(CST_5b(m->green_to_red_), |
55 | CST_5b(m->green_to_blue_)); |
56 | const __m128i mults_b2 = MK_CST_16(CST_5b(m->red_to_blue_), 0); |
57 | const __m128i mask_ag = _mm_set1_epi32((int)0xff00ff00); // alpha-green masks |
58 | const __m128i mask_rb = _mm_set1_epi32(0x00ff00ff); // red-blue masks |
59 | int i; |
60 | for (i = 0; i + 4 <= num_pixels; i += 4) { |
61 | const __m128i in = _mm_loadu_si128((__m128i*)&argb_data[i]); // argb |
62 | const __m128i A = _mm_and_si128(in, mask_ag); // a 0 g 0 |
63 | const __m128i B = _mm_shufflelo_epi16(A, _MM_SHUFFLE(2, 2, 0, 0)); |
64 | const __m128i C = _mm_shufflehi_epi16(B, _MM_SHUFFLE(2, 2, 0, 0)); // g0g0 |
65 | const __m128i D = _mm_mulhi_epi16(C, mults_rb); // x dr x db1 |
66 | const __m128i E = _mm_slli_epi16(in, 8); // r 0 b 0 |
67 | const __m128i F = _mm_mulhi_epi16(E, mults_b2); // x db2 0 0 |
68 | const __m128i G = _mm_srli_epi32(F, 16); // 0 0 x db2 |
69 | const __m128i H = _mm_add_epi8(G, D); // x dr x db |
70 | const __m128i I = _mm_and_si128(H, mask_rb); // 0 dr 0 db |
71 | const __m128i out = _mm_sub_epi8(in, I); |
72 | _mm_storeu_si128((__m128i*)&argb_data[i], out); |
73 | } |
74 | // fallthrough and finish off with plain-C |
75 | if (i != num_pixels) { |
76 | VP8LTransformColor_C(m, argb_data + i, num_pixels - i); |
77 | } |
78 | } |
79 | |
80 | //------------------------------------------------------------------------------ |
81 | #define SPAN 8 |
82 | static void CollectColorBlueTransforms_SSE2(const uint32_t* argb, int stride, |
83 | int tile_width, int tile_height, |
84 | int green_to_blue, int red_to_blue, |
85 | int histo[]) { |
86 | const __m128i mults_r = MK_CST_16(CST_5b(red_to_blue), 0); |
87 | const __m128i mults_g = MK_CST_16(0, CST_5b(green_to_blue)); |
88 | const __m128i mask_g = _mm_set1_epi32(0x00ff00); // green mask |
89 | const __m128i mask_b = _mm_set1_epi32(0x0000ff); // blue mask |
90 | int y; |
91 | for (y = 0; y < tile_height; ++y) { |
92 | const uint32_t* const src = argb + y * stride; |
93 | int i, x; |
94 | for (x = 0; x + SPAN <= tile_width; x += SPAN) { |
95 | uint16_t values[SPAN]; |
96 | const __m128i in0 = _mm_loadu_si128((__m128i*)&src[x + 0]); |
97 | const __m128i in1 = _mm_loadu_si128((__m128i*)&src[x + SPAN / 2]); |
98 | const __m128i A0 = _mm_slli_epi16(in0, 8); // r 0 | b 0 |
99 | const __m128i A1 = _mm_slli_epi16(in1, 8); |
100 | const __m128i B0 = _mm_and_si128(in0, mask_g); // 0 0 | g 0 |
101 | const __m128i B1 = _mm_and_si128(in1, mask_g); |
102 | const __m128i C0 = _mm_mulhi_epi16(A0, mults_r); // x db | 0 0 |
103 | const __m128i C1 = _mm_mulhi_epi16(A1, mults_r); |
104 | const __m128i D0 = _mm_mulhi_epi16(B0, mults_g); // 0 0 | x db |
105 | const __m128i D1 = _mm_mulhi_epi16(B1, mults_g); |
106 | const __m128i E0 = _mm_sub_epi8(in0, D0); // x x | x b' |
107 | const __m128i E1 = _mm_sub_epi8(in1, D1); |
108 | const __m128i F0 = _mm_srli_epi32(C0, 16); // 0 0 | x db |
109 | const __m128i F1 = _mm_srli_epi32(C1, 16); |
110 | const __m128i G0 = _mm_sub_epi8(E0, F0); // 0 0 | x b' |
111 | const __m128i G1 = _mm_sub_epi8(E1, F1); |
112 | const __m128i H0 = _mm_and_si128(G0, mask_b); // 0 0 | 0 b |
113 | const __m128i H1 = _mm_and_si128(G1, mask_b); |
114 | const __m128i I = _mm_packs_epi32(H0, H1); // 0 b' | 0 b' |
115 | _mm_storeu_si128((__m128i*)values, I); |
116 | for (i = 0; i < SPAN; ++i) ++histo[values[i]]; |
117 | } |
118 | } |
119 | { |
120 | const int left_over = tile_width & (SPAN - 1); |
121 | if (left_over > 0) { |
122 | VP8LCollectColorBlueTransforms_C(argb + tile_width - left_over, stride, |
123 | left_over, tile_height, |
124 | green_to_blue, red_to_blue, histo); |
125 | } |
126 | } |
127 | } |
128 | |
129 | static void CollectColorRedTransforms_SSE2(const uint32_t* argb, int stride, |
130 | int tile_width, int tile_height, |
131 | int green_to_red, int histo[]) { |
132 | const __m128i mults_g = MK_CST_16(0, CST_5b(green_to_red)); |
133 | const __m128i mask_g = _mm_set1_epi32(0x00ff00); // green mask |
134 | const __m128i mask = _mm_set1_epi32(0xff); |
135 | |
136 | int y; |
137 | for (y = 0; y < tile_height; ++y) { |
138 | const uint32_t* const src = argb + y * stride; |
139 | int i, x; |
140 | for (x = 0; x + SPAN <= tile_width; x += SPAN) { |
141 | uint16_t values[SPAN]; |
142 | const __m128i in0 = _mm_loadu_si128((__m128i*)&src[x + 0]); |
143 | const __m128i in1 = _mm_loadu_si128((__m128i*)&src[x + SPAN / 2]); |
144 | const __m128i A0 = _mm_and_si128(in0, mask_g); // 0 0 | g 0 |
145 | const __m128i A1 = _mm_and_si128(in1, mask_g); |
146 | const __m128i B0 = _mm_srli_epi32(in0, 16); // 0 0 | x r |
147 | const __m128i B1 = _mm_srli_epi32(in1, 16); |
148 | const __m128i C0 = _mm_mulhi_epi16(A0, mults_g); // 0 0 | x dr |
149 | const __m128i C1 = _mm_mulhi_epi16(A1, mults_g); |
150 | const __m128i E0 = _mm_sub_epi8(B0, C0); // x x | x r' |
151 | const __m128i E1 = _mm_sub_epi8(B1, C1); |
152 | const __m128i F0 = _mm_and_si128(E0, mask); // 0 0 | 0 r' |
153 | const __m128i F1 = _mm_and_si128(E1, mask); |
154 | const __m128i I = _mm_packs_epi32(F0, F1); |
155 | _mm_storeu_si128((__m128i*)values, I); |
156 | for (i = 0; i < SPAN; ++i) ++histo[values[i]]; |
157 | } |
158 | } |
159 | { |
160 | const int left_over = tile_width & (SPAN - 1); |
161 | if (left_over > 0) { |
162 | VP8LCollectColorRedTransforms_C(argb + tile_width - left_over, stride, |
163 | left_over, tile_height, |
164 | green_to_red, histo); |
165 | } |
166 | } |
167 | } |
168 | #undef SPAN |
169 | #undef MK_CST_16 |
170 | |
171 | //------------------------------------------------------------------------------ |
172 | |
173 | // Note we are adding uint32_t's as *signed* int32's (using _mm_add_epi32). But |
174 | // that's ok since the histogram values are less than 1<<28 (max picture size). |
175 | #define LINE_SIZE 16 // 8 or 16 |
176 | static void AddVector_SSE2(const uint32_t* a, const uint32_t* b, uint32_t* out, |
177 | int size) { |
178 | int i; |
179 | for (i = 0; i + LINE_SIZE <= size; i += LINE_SIZE) { |
180 | const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[i + 0]); |
181 | const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[i + 4]); |
182 | #if (LINE_SIZE == 16) |
183 | const __m128i a2 = _mm_loadu_si128((const __m128i*)&a[i + 8]); |
184 | const __m128i a3 = _mm_loadu_si128((const __m128i*)&a[i + 12]); |
185 | #endif |
186 | const __m128i b0 = _mm_loadu_si128((const __m128i*)&b[i + 0]); |
187 | const __m128i b1 = _mm_loadu_si128((const __m128i*)&b[i + 4]); |
188 | #if (LINE_SIZE == 16) |
189 | const __m128i b2 = _mm_loadu_si128((const __m128i*)&b[i + 8]); |
190 | const __m128i b3 = _mm_loadu_si128((const __m128i*)&b[i + 12]); |
191 | #endif |
192 | _mm_storeu_si128((__m128i*)&out[i + 0], _mm_add_epi32(a0, b0)); |
193 | _mm_storeu_si128((__m128i*)&out[i + 4], _mm_add_epi32(a1, b1)); |
194 | #if (LINE_SIZE == 16) |
195 | _mm_storeu_si128((__m128i*)&out[i + 8], _mm_add_epi32(a2, b2)); |
196 | _mm_storeu_si128((__m128i*)&out[i + 12], _mm_add_epi32(a3, b3)); |
197 | #endif |
198 | } |
199 | for (; i < size; ++i) { |
200 | out[i] = a[i] + b[i]; |
201 | } |
202 | } |
203 | |
204 | static void AddVectorEq_SSE2(const uint32_t* a, uint32_t* out, int size) { |
205 | int i; |
206 | for (i = 0; i + LINE_SIZE <= size; i += LINE_SIZE) { |
207 | const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[i + 0]); |
208 | const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[i + 4]); |
209 | #if (LINE_SIZE == 16) |
210 | const __m128i a2 = _mm_loadu_si128((const __m128i*)&a[i + 8]); |
211 | const __m128i a3 = _mm_loadu_si128((const __m128i*)&a[i + 12]); |
212 | #endif |
213 | const __m128i b0 = _mm_loadu_si128((const __m128i*)&out[i + 0]); |
214 | const __m128i b1 = _mm_loadu_si128((const __m128i*)&out[i + 4]); |
215 | #if (LINE_SIZE == 16) |
216 | const __m128i b2 = _mm_loadu_si128((const __m128i*)&out[i + 8]); |
217 | const __m128i b3 = _mm_loadu_si128((const __m128i*)&out[i + 12]); |
218 | #endif |
219 | _mm_storeu_si128((__m128i*)&out[i + 0], _mm_add_epi32(a0, b0)); |
220 | _mm_storeu_si128((__m128i*)&out[i + 4], _mm_add_epi32(a1, b1)); |
221 | #if (LINE_SIZE == 16) |
222 | _mm_storeu_si128((__m128i*)&out[i + 8], _mm_add_epi32(a2, b2)); |
223 | _mm_storeu_si128((__m128i*)&out[i + 12], _mm_add_epi32(a3, b3)); |
224 | #endif |
225 | } |
226 | for (; i < size; ++i) { |
227 | out[i] += a[i]; |
228 | } |
229 | } |
230 | #undef LINE_SIZE |
231 | |
232 | //------------------------------------------------------------------------------ |
233 | // Entropy |
234 | |
235 | // TODO(https://crbug.com/webp/499): this function produces different results |
236 | // from the C code due to use of double/float resulting in output differences |
237 | // when compared to -noasm. |
238 | #if !(defined(WEBP_HAVE_SLOW_CLZ_CTZ) || defined(__i386__) || defined(_M_IX86)) |
239 | |
240 | static float CombinedShannonEntropy_SSE2(const int X[256], const int Y[256]) { |
241 | int i; |
242 | float retval = 0.f; |
243 | int sumX = 0, sumXY = 0; |
244 | const __m128i zero = _mm_setzero_si128(); |
245 | |
246 | for (i = 0; i < 256; i += 16) { |
247 | const __m128i x0 = _mm_loadu_si128((const __m128i*)(X + i + 0)); |
248 | const __m128i y0 = _mm_loadu_si128((const __m128i*)(Y + i + 0)); |
249 | const __m128i x1 = _mm_loadu_si128((const __m128i*)(X + i + 4)); |
250 | const __m128i y1 = _mm_loadu_si128((const __m128i*)(Y + i + 4)); |
251 | const __m128i x2 = _mm_loadu_si128((const __m128i*)(X + i + 8)); |
252 | const __m128i y2 = _mm_loadu_si128((const __m128i*)(Y + i + 8)); |
253 | const __m128i x3 = _mm_loadu_si128((const __m128i*)(X + i + 12)); |
254 | const __m128i y3 = _mm_loadu_si128((const __m128i*)(Y + i + 12)); |
255 | const __m128i x4 = _mm_packs_epi16(_mm_packs_epi32(x0, x1), |
256 | _mm_packs_epi32(x2, x3)); |
257 | const __m128i y4 = _mm_packs_epi16(_mm_packs_epi32(y0, y1), |
258 | _mm_packs_epi32(y2, y3)); |
259 | const int32_t mx = _mm_movemask_epi8(_mm_cmpgt_epi8(x4, zero)); |
260 | int32_t my = _mm_movemask_epi8(_mm_cmpgt_epi8(y4, zero)) | mx; |
261 | while (my) { |
262 | const int32_t j = BitsCtz(my); |
263 | int xy; |
264 | if ((mx >> j) & 1) { |
265 | const int x = X[i + j]; |
266 | sumXY += x; |
267 | retval -= VP8LFastSLog2(x); |
268 | } |
269 | xy = X[i + j] + Y[i + j]; |
270 | sumX += xy; |
271 | retval -= VP8LFastSLog2(xy); |
272 | my &= my - 1; |
273 | } |
274 | } |
275 | retval += VP8LFastSLog2(sumX) + VP8LFastSLog2(sumXY); |
276 | return retval; |
277 | } |
278 | |
279 | #else |
280 | |
281 | #define DONT_USE_COMBINED_SHANNON_ENTROPY_SSE2_FUNC // won't be faster |
282 | |
283 | #endif |
284 | |
285 | //------------------------------------------------------------------------------ |
286 | |
287 | static int VectorMismatch_SSE2(const uint32_t* const array1, |
288 | const uint32_t* const array2, int length) { |
289 | int match_len; |
290 | |
291 | if (length >= 12) { |
292 | __m128i A0 = _mm_loadu_si128((const __m128i*)&array1[0]); |
293 | __m128i A1 = _mm_loadu_si128((const __m128i*)&array2[0]); |
294 | match_len = 0; |
295 | do { |
296 | // Loop unrolling and early load both provide a speedup of 10% for the |
297 | // current function. Also, max_limit can be MAX_LENGTH=4096 at most. |
298 | const __m128i cmpA = _mm_cmpeq_epi32(A0, A1); |
299 | const __m128i B0 = |
300 | _mm_loadu_si128((const __m128i*)&array1[match_len + 4]); |
301 | const __m128i B1 = |
302 | _mm_loadu_si128((const __m128i*)&array2[match_len + 4]); |
303 | if (_mm_movemask_epi8(cmpA) != 0xffff) break; |
304 | match_len += 4; |
305 | |
306 | { |
307 | const __m128i cmpB = _mm_cmpeq_epi32(B0, B1); |
308 | A0 = _mm_loadu_si128((const __m128i*)&array1[match_len + 4]); |
309 | A1 = _mm_loadu_si128((const __m128i*)&array2[match_len + 4]); |
310 | if (_mm_movemask_epi8(cmpB) != 0xffff) break; |
311 | match_len += 4; |
312 | } |
313 | } while (match_len + 12 < length); |
314 | } else { |
315 | match_len = 0; |
316 | // Unroll the potential first two loops. |
317 | if (length >= 4 && |
318 | _mm_movemask_epi8(_mm_cmpeq_epi32( |
319 | _mm_loadu_si128((const __m128i*)&array1[0]), |
320 | _mm_loadu_si128((const __m128i*)&array2[0]))) == 0xffff) { |
321 | match_len = 4; |
322 | if (length >= 8 && |
323 | _mm_movemask_epi8(_mm_cmpeq_epi32( |
324 | _mm_loadu_si128((const __m128i*)&array1[4]), |
325 | _mm_loadu_si128((const __m128i*)&array2[4]))) == 0xffff) { |
326 | match_len = 8; |
327 | } |
328 | } |
329 | } |
330 | |
331 | while (match_len < length && array1[match_len] == array2[match_len]) { |
332 | ++match_len; |
333 | } |
334 | return match_len; |
335 | } |
336 | |
337 | // Bundles multiple (1, 2, 4 or 8) pixels into a single pixel. |
338 | static void BundleColorMap_SSE2(const uint8_t* const row, int width, int xbits, |
339 | uint32_t* dst) { |
340 | int x; |
341 | assert(xbits >= 0); |
342 | assert(xbits <= 3); |
343 | switch (xbits) { |
344 | case 0: { |
345 | const __m128i ff = _mm_set1_epi16((short)0xff00); |
346 | const __m128i zero = _mm_setzero_si128(); |
347 | // Store 0xff000000 | (row[x] << 8). |
348 | for (x = 0; x + 16 <= width; x += 16, dst += 16) { |
349 | const __m128i in = _mm_loadu_si128((const __m128i*)&row[x]); |
350 | const __m128i in_lo = _mm_unpacklo_epi8(zero, in); |
351 | const __m128i dst0 = _mm_unpacklo_epi16(in_lo, ff); |
352 | const __m128i dst1 = _mm_unpackhi_epi16(in_lo, ff); |
353 | const __m128i in_hi = _mm_unpackhi_epi8(zero, in); |
354 | const __m128i dst2 = _mm_unpacklo_epi16(in_hi, ff); |
355 | const __m128i dst3 = _mm_unpackhi_epi16(in_hi, ff); |
356 | _mm_storeu_si128((__m128i*)&dst[0], dst0); |
357 | _mm_storeu_si128((__m128i*)&dst[4], dst1); |
358 | _mm_storeu_si128((__m128i*)&dst[8], dst2); |
359 | _mm_storeu_si128((__m128i*)&dst[12], dst3); |
360 | } |
361 | break; |
362 | } |
363 | case 1: { |
364 | const __m128i ff = _mm_set1_epi16((short)0xff00); |
365 | const __m128i mul = _mm_set1_epi16(0x110); |
366 | for (x = 0; x + 16 <= width; x += 16, dst += 8) { |
367 | // 0a0b | (where a/b are 4 bits). |
368 | const __m128i in = _mm_loadu_si128((const __m128i*)&row[x]); |
369 | const __m128i tmp = _mm_mullo_epi16(in, mul); // aba0 |
370 | const __m128i pack = _mm_and_si128(tmp, ff); // ab00 |
371 | const __m128i dst0 = _mm_unpacklo_epi16(pack, ff); |
372 | const __m128i dst1 = _mm_unpackhi_epi16(pack, ff); |
373 | _mm_storeu_si128((__m128i*)&dst[0], dst0); |
374 | _mm_storeu_si128((__m128i*)&dst[4], dst1); |
375 | } |
376 | break; |
377 | } |
378 | case 2: { |
379 | const __m128i mask_or = _mm_set1_epi32((int)0xff000000); |
380 | const __m128i mul_cst = _mm_set1_epi16(0x0104); |
381 | const __m128i mask_mul = _mm_set1_epi16(0x0f00); |
382 | for (x = 0; x + 16 <= width; x += 16, dst += 4) { |
383 | // 000a000b000c000d | (where a/b/c/d are 2 bits). |
384 | const __m128i in = _mm_loadu_si128((const __m128i*)&row[x]); |
385 | const __m128i mul = _mm_mullo_epi16(in, mul_cst); // 00ab00b000cd00d0 |
386 | const __m128i tmp = _mm_and_si128(mul, mask_mul); // 00ab000000cd0000 |
387 | const __m128i shift = _mm_srli_epi32(tmp, 12); // 00000000ab000000 |
388 | const __m128i pack = _mm_or_si128(shift, tmp); // 00000000abcd0000 |
389 | // Convert to 0xff00**00. |
390 | const __m128i res = _mm_or_si128(pack, mask_or); |
391 | _mm_storeu_si128((__m128i*)dst, res); |
392 | } |
393 | break; |
394 | } |
395 | default: { |
396 | assert(xbits == 3); |
397 | for (x = 0; x + 16 <= width; x += 16, dst += 2) { |
398 | // 0000000a00000000b... | (where a/b are 1 bit). |
399 | const __m128i in = _mm_loadu_si128((const __m128i*)&row[x]); |
400 | const __m128i shift = _mm_slli_epi64(in, 7); |
401 | const uint32_t move = _mm_movemask_epi8(shift); |
402 | dst[0] = 0xff000000 | ((move & 0xff) << 8); |
403 | dst[1] = 0xff000000 | (move & 0xff00); |
404 | } |
405 | break; |
406 | } |
407 | } |
408 | if (x != width) { |
409 | VP8LBundleColorMap_C(row + x, width - x, xbits, dst); |
410 | } |
411 | } |
412 | |
413 | //------------------------------------------------------------------------------ |
414 | // Batch version of Predictor Transform subtraction |
415 | |
416 | static WEBP_INLINE void Average2_m128i(const __m128i* const a0, |
417 | const __m128i* const a1, |
418 | __m128i* const avg) { |
419 | // (a + b) >> 1 = ((a + b + 1) >> 1) - ((a ^ b) & 1) |
420 | const __m128i ones = _mm_set1_epi8(1); |
421 | const __m128i avg1 = _mm_avg_epu8(*a0, *a1); |
422 | const __m128i one = _mm_and_si128(_mm_xor_si128(*a0, *a1), ones); |
423 | *avg = _mm_sub_epi8(avg1, one); |
424 | } |
425 | |
426 | // Predictor0: ARGB_BLACK. |
427 | static void PredictorSub0_SSE2(const uint32_t* in, const uint32_t* upper, |
428 | int num_pixels, uint32_t* out) { |
429 | int i; |
430 | const __m128i black = _mm_set1_epi32((int)ARGB_BLACK); |
431 | for (i = 0; i + 4 <= num_pixels; i += 4) { |
432 | const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); |
433 | const __m128i res = _mm_sub_epi8(src, black); |
434 | _mm_storeu_si128((__m128i*)&out[i], res); |
435 | } |
436 | if (i != num_pixels) { |
437 | VP8LPredictorsSub_C[0](in + i, NULL, num_pixels - i, out + i); |
438 | } |
439 | (void)upper; |
440 | } |
441 | |
442 | #define GENERATE_PREDICTOR_1(X, IN) \ |
443 | static void PredictorSub##X##_SSE2(const uint32_t* const in, \ |
444 | const uint32_t* const upper, \ |
445 | int num_pixels, uint32_t* const out) { \ |
446 | int i; \ |
447 | for (i = 0; i + 4 <= num_pixels; i += 4) { \ |
448 | const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); \ |
449 | const __m128i pred = _mm_loadu_si128((const __m128i*)&(IN)); \ |
450 | const __m128i res = _mm_sub_epi8(src, pred); \ |
451 | _mm_storeu_si128((__m128i*)&out[i], res); \ |
452 | } \ |
453 | if (i != num_pixels) { \ |
454 | VP8LPredictorsSub_C[(X)](in + i, WEBP_OFFSET_PTR(upper, i), \ |
455 | num_pixels - i, out + i); \ |
456 | } \ |
457 | } |
458 | |
459 | GENERATE_PREDICTOR_1(1, in[i - 1]) // Predictor1: L |
460 | GENERATE_PREDICTOR_1(2, upper[i]) // Predictor2: T |
461 | GENERATE_PREDICTOR_1(3, upper[i + 1]) // Predictor3: TR |
462 | GENERATE_PREDICTOR_1(4, upper[i - 1]) // Predictor4: TL |
463 | #undef GENERATE_PREDICTOR_1 |
464 | |
465 | // Predictor5: avg2(avg2(L, TR), T) |
466 | static void PredictorSub5_SSE2(const uint32_t* in, const uint32_t* upper, |
467 | int num_pixels, uint32_t* out) { |
468 | int i; |
469 | for (i = 0; i + 4 <= num_pixels; i += 4) { |
470 | const __m128i L = _mm_loadu_si128((const __m128i*)&in[i - 1]); |
471 | const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]); |
472 | const __m128i TR = _mm_loadu_si128((const __m128i*)&upper[i + 1]); |
473 | const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); |
474 | __m128i avg, pred, res; |
475 | Average2_m128i(&L, &TR, &avg); |
476 | Average2_m128i(&avg, &T, &pred); |
477 | res = _mm_sub_epi8(src, pred); |
478 | _mm_storeu_si128((__m128i*)&out[i], res); |
479 | } |
480 | if (i != num_pixels) { |
481 | VP8LPredictorsSub_C[5](in + i, upper + i, num_pixels - i, out + i); |
482 | } |
483 | } |
484 | |
485 | #define GENERATE_PREDICTOR_2(X, A, B) \ |
486 | static void PredictorSub##X##_SSE2(const uint32_t* in, const uint32_t* upper, \ |
487 | int num_pixels, uint32_t* out) { \ |
488 | int i; \ |
489 | for (i = 0; i + 4 <= num_pixels; i += 4) { \ |
490 | const __m128i tA = _mm_loadu_si128((const __m128i*)&(A)); \ |
491 | const __m128i tB = _mm_loadu_si128((const __m128i*)&(B)); \ |
492 | const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); \ |
493 | __m128i pred, res; \ |
494 | Average2_m128i(&tA, &tB, &pred); \ |
495 | res = _mm_sub_epi8(src, pred); \ |
496 | _mm_storeu_si128((__m128i*)&out[i], res); \ |
497 | } \ |
498 | if (i != num_pixels) { \ |
499 | VP8LPredictorsSub_C[(X)](in + i, upper + i, num_pixels - i, out + i); \ |
500 | } \ |
501 | } |
502 | |
503 | GENERATE_PREDICTOR_2(6, in[i - 1], upper[i - 1]) // Predictor6: avg(L, TL) |
504 | GENERATE_PREDICTOR_2(7, in[i - 1], upper[i]) // Predictor7: avg(L, T) |
505 | GENERATE_PREDICTOR_2(8, upper[i - 1], upper[i]) // Predictor8: avg(TL, T) |
506 | GENERATE_PREDICTOR_2(9, upper[i], upper[i + 1]) // Predictor9: average(T, TR) |
507 | #undef GENERATE_PREDICTOR_2 |
508 | |
509 | // Predictor10: avg(avg(L,TL), avg(T, TR)). |
510 | static void PredictorSub10_SSE2(const uint32_t* in, const uint32_t* upper, |
511 | int num_pixels, uint32_t* out) { |
512 | int i; |
513 | for (i = 0; i + 4 <= num_pixels; i += 4) { |
514 | const __m128i L = _mm_loadu_si128((const __m128i*)&in[i - 1]); |
515 | const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); |
516 | const __m128i TL = _mm_loadu_si128((const __m128i*)&upper[i - 1]); |
517 | const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]); |
518 | const __m128i TR = _mm_loadu_si128((const __m128i*)&upper[i + 1]); |
519 | __m128i avgTTR, avgLTL, avg, res; |
520 | Average2_m128i(&T, &TR, &avgTTR); |
521 | Average2_m128i(&L, &TL, &avgLTL); |
522 | Average2_m128i(&avgTTR, &avgLTL, &avg); |
523 | res = _mm_sub_epi8(src, avg); |
524 | _mm_storeu_si128((__m128i*)&out[i], res); |
525 | } |
526 | if (i != num_pixels) { |
527 | VP8LPredictorsSub_C[10](in + i, upper + i, num_pixels - i, out + i); |
528 | } |
529 | } |
530 | |
531 | // Predictor11: select. |
532 | static void GetSumAbsDiff32_SSE2(const __m128i* const A, const __m128i* const B, |
533 | __m128i* const out) { |
534 | // We can unpack with any value on the upper 32 bits, provided it's the same |
535 | // on both operands (to that their sum of abs diff is zero). Here we use *A. |
536 | const __m128i A_lo = _mm_unpacklo_epi32(*A, *A); |
537 | const __m128i B_lo = _mm_unpacklo_epi32(*B, *A); |
538 | const __m128i A_hi = _mm_unpackhi_epi32(*A, *A); |
539 | const __m128i B_hi = _mm_unpackhi_epi32(*B, *A); |
540 | const __m128i s_lo = _mm_sad_epu8(A_lo, B_lo); |
541 | const __m128i s_hi = _mm_sad_epu8(A_hi, B_hi); |
542 | *out = _mm_packs_epi32(s_lo, s_hi); |
543 | } |
544 | |
545 | static void PredictorSub11_SSE2(const uint32_t* in, const uint32_t* upper, |
546 | int num_pixels, uint32_t* out) { |
547 | int i; |
548 | for (i = 0; i + 4 <= num_pixels; i += 4) { |
549 | const __m128i L = _mm_loadu_si128((const __m128i*)&in[i - 1]); |
550 | const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]); |
551 | const __m128i TL = _mm_loadu_si128((const __m128i*)&upper[i - 1]); |
552 | const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); |
553 | __m128i pa, pb; |
554 | GetSumAbsDiff32_SSE2(&T, &TL, &pa); // pa = sum |T-TL| |
555 | GetSumAbsDiff32_SSE2(&L, &TL, &pb); // pb = sum |L-TL| |
556 | { |
557 | const __m128i mask = _mm_cmpgt_epi32(pb, pa); |
558 | const __m128i A = _mm_and_si128(mask, L); |
559 | const __m128i B = _mm_andnot_si128(mask, T); |
560 | const __m128i pred = _mm_or_si128(A, B); // pred = (L > T)? L : T |
561 | const __m128i res = _mm_sub_epi8(src, pred); |
562 | _mm_storeu_si128((__m128i*)&out[i], res); |
563 | } |
564 | } |
565 | if (i != num_pixels) { |
566 | VP8LPredictorsSub_C[11](in + i, upper + i, num_pixels - i, out + i); |
567 | } |
568 | } |
569 | |
570 | // Predictor12: ClampedSubSubtractFull. |
571 | static void PredictorSub12_SSE2(const uint32_t* in, const uint32_t* upper, |
572 | int num_pixels, uint32_t* out) { |
573 | int i; |
574 | const __m128i zero = _mm_setzero_si128(); |
575 | for (i = 0; i + 4 <= num_pixels; i += 4) { |
576 | const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); |
577 | const __m128i L = _mm_loadu_si128((const __m128i*)&in[i - 1]); |
578 | const __m128i L_lo = _mm_unpacklo_epi8(L, zero); |
579 | const __m128i L_hi = _mm_unpackhi_epi8(L, zero); |
580 | const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]); |
581 | const __m128i T_lo = _mm_unpacklo_epi8(T, zero); |
582 | const __m128i T_hi = _mm_unpackhi_epi8(T, zero); |
583 | const __m128i TL = _mm_loadu_si128((const __m128i*)&upper[i - 1]); |
584 | const __m128i TL_lo = _mm_unpacklo_epi8(TL, zero); |
585 | const __m128i TL_hi = _mm_unpackhi_epi8(TL, zero); |
586 | const __m128i diff_lo = _mm_sub_epi16(T_lo, TL_lo); |
587 | const __m128i diff_hi = _mm_sub_epi16(T_hi, TL_hi); |
588 | const __m128i pred_lo = _mm_add_epi16(L_lo, diff_lo); |
589 | const __m128i pred_hi = _mm_add_epi16(L_hi, diff_hi); |
590 | const __m128i pred = _mm_packus_epi16(pred_lo, pred_hi); |
591 | const __m128i res = _mm_sub_epi8(src, pred); |
592 | _mm_storeu_si128((__m128i*)&out[i], res); |
593 | } |
594 | if (i != num_pixels) { |
595 | VP8LPredictorsSub_C[12](in + i, upper + i, num_pixels - i, out + i); |
596 | } |
597 | } |
598 | |
599 | // Predictors13: ClampedAddSubtractHalf |
600 | static void PredictorSub13_SSE2(const uint32_t* in, const uint32_t* upper, |
601 | int num_pixels, uint32_t* out) { |
602 | int i; |
603 | const __m128i zero = _mm_setzero_si128(); |
604 | for (i = 0; i + 2 <= num_pixels; i += 2) { |
605 | // we can only process two pixels at a time |
606 | const __m128i L = _mm_loadl_epi64((const __m128i*)&in[i - 1]); |
607 | const __m128i src = _mm_loadl_epi64((const __m128i*)&in[i]); |
608 | const __m128i T = _mm_loadl_epi64((const __m128i*)&upper[i]); |
609 | const __m128i TL = _mm_loadl_epi64((const __m128i*)&upper[i - 1]); |
610 | const __m128i L_lo = _mm_unpacklo_epi8(L, zero); |
611 | const __m128i T_lo = _mm_unpacklo_epi8(T, zero); |
612 | const __m128i TL_lo = _mm_unpacklo_epi8(TL, zero); |
613 | const __m128i sum = _mm_add_epi16(T_lo, L_lo); |
614 | const __m128i avg = _mm_srli_epi16(sum, 1); |
615 | const __m128i A1 = _mm_sub_epi16(avg, TL_lo); |
616 | const __m128i bit_fix = _mm_cmpgt_epi16(TL_lo, avg); |
617 | const __m128i A2 = _mm_sub_epi16(A1, bit_fix); |
618 | const __m128i A3 = _mm_srai_epi16(A2, 1); |
619 | const __m128i A4 = _mm_add_epi16(avg, A3); |
620 | const __m128i pred = _mm_packus_epi16(A4, A4); |
621 | const __m128i res = _mm_sub_epi8(src, pred); |
622 | _mm_storel_epi64((__m128i*)&out[i], res); |
623 | } |
624 | if (i != num_pixels) { |
625 | VP8LPredictorsSub_C[13](in + i, upper + i, num_pixels - i, out + i); |
626 | } |
627 | } |
628 | |
629 | //------------------------------------------------------------------------------ |
630 | // Entry point |
631 | |
632 | extern void VP8LEncDspInitSSE2(void); |
633 | |
634 | WEBP_TSAN_IGNORE_FUNCTION void VP8LEncDspInitSSE2(void) { |
635 | VP8LSubtractGreenFromBlueAndRed = SubtractGreenFromBlueAndRed_SSE2; |
636 | VP8LTransformColor = TransformColor_SSE2; |
637 | VP8LCollectColorBlueTransforms = CollectColorBlueTransforms_SSE2; |
638 | VP8LCollectColorRedTransforms = CollectColorRedTransforms_SSE2; |
639 | VP8LAddVector = AddVector_SSE2; |
640 | VP8LAddVectorEq = AddVectorEq_SSE2; |
641 | #if !defined(DONT_USE_COMBINED_SHANNON_ENTROPY_SSE2_FUNC) |
642 | VP8LCombinedShannonEntropy = CombinedShannonEntropy_SSE2; |
643 | #endif |
644 | VP8LVectorMismatch = VectorMismatch_SSE2; |
645 | VP8LBundleColorMap = BundleColorMap_SSE2; |
646 | |
647 | VP8LPredictorsSub[0] = PredictorSub0_SSE2; |
648 | VP8LPredictorsSub[1] = PredictorSub1_SSE2; |
649 | VP8LPredictorsSub[2] = PredictorSub2_SSE2; |
650 | VP8LPredictorsSub[3] = PredictorSub3_SSE2; |
651 | VP8LPredictorsSub[4] = PredictorSub4_SSE2; |
652 | VP8LPredictorsSub[5] = PredictorSub5_SSE2; |
653 | VP8LPredictorsSub[6] = PredictorSub6_SSE2; |
654 | VP8LPredictorsSub[7] = PredictorSub7_SSE2; |
655 | VP8LPredictorsSub[8] = PredictorSub8_SSE2; |
656 | VP8LPredictorsSub[9] = PredictorSub9_SSE2; |
657 | VP8LPredictorsSub[10] = PredictorSub10_SSE2; |
658 | VP8LPredictorsSub[11] = PredictorSub11_SSE2; |
659 | VP8LPredictorsSub[12] = PredictorSub12_SSE2; |
660 | VP8LPredictorsSub[13] = PredictorSub13_SSE2; |
661 | VP8LPredictorsSub[14] = PredictorSub0_SSE2; // <- padding security sentinels |
662 | VP8LPredictorsSub[15] = PredictorSub0_SSE2; |
663 | } |
664 | |
665 | #else // !WEBP_USE_SSE2 |
666 | |
667 | WEBP_DSP_INIT_STUB(VP8LEncDspInitSSE2) |
668 | |
669 | #endif // WEBP_USE_SSE2 |
670 | |