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(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 | // Checks whether the X or Y contribution is worth computing and adding. |
236 | // Used in loop unrolling. |
237 | #define ANALYZE_X_OR_Y(x_or_y, j) \ |
238 | do { \ |
239 | if ((x_or_y)[i + (j)] != 0) retval -= VP8LFastSLog2((x_or_y)[i + (j)]); \ |
240 | } while (0) |
241 | |
242 | // Checks whether the X + Y contribution is worth computing and adding. |
243 | // Used in loop unrolling. |
244 | #define ANALYZE_XY(j) \ |
245 | do { \ |
246 | if (tmp[j] != 0) { \ |
247 | retval -= VP8LFastSLog2(tmp[j]); \ |
248 | ANALYZE_X_OR_Y(X, j); \ |
249 | } \ |
250 | } while (0) |
251 | |
252 | static float CombinedShannonEntropy_SSE2(const int X[256], const int Y[256]) { |
253 | int i; |
254 | double retval = 0.; |
255 | int sumX, sumXY; |
256 | int32_t tmp[4]; |
257 | __m128i zero = _mm_setzero_si128(); |
258 | // Sums up X + Y, 4 ints at a time (and will merge it at the end for sumXY). |
259 | __m128i sumXY_128 = zero; |
260 | __m128i sumX_128 = zero; |
261 | |
262 | for (i = 0; i < 256; i += 4) { |
263 | const __m128i x = _mm_loadu_si128((const __m128i*)(X + i)); |
264 | const __m128i y = _mm_loadu_si128((const __m128i*)(Y + i)); |
265 | |
266 | // Check if any X is non-zero: this actually provides a speedup as X is |
267 | // usually sparse. |
268 | if (_mm_movemask_epi8(_mm_cmpeq_epi32(x, zero)) != 0xFFFF) { |
269 | const __m128i xy_128 = _mm_add_epi32(x, y); |
270 | sumXY_128 = _mm_add_epi32(sumXY_128, xy_128); |
271 | |
272 | sumX_128 = _mm_add_epi32(sumX_128, x); |
273 | |
274 | // Analyze the different X + Y. |
275 | _mm_storeu_si128((__m128i*)tmp, xy_128); |
276 | |
277 | ANALYZE_XY(0); |
278 | ANALYZE_XY(1); |
279 | ANALYZE_XY(2); |
280 | ANALYZE_XY(3); |
281 | } else { |
282 | // X is fully 0, so only deal with Y. |
283 | sumXY_128 = _mm_add_epi32(sumXY_128, y); |
284 | |
285 | ANALYZE_X_OR_Y(Y, 0); |
286 | ANALYZE_X_OR_Y(Y, 1); |
287 | ANALYZE_X_OR_Y(Y, 2); |
288 | ANALYZE_X_OR_Y(Y, 3); |
289 | } |
290 | } |
291 | |
292 | // Sum up sumX_128 to get sumX. |
293 | _mm_storeu_si128((__m128i*)tmp, sumX_128); |
294 | sumX = tmp[3] + tmp[2] + tmp[1] + tmp[0]; |
295 | |
296 | // Sum up sumXY_128 to get sumXY. |
297 | _mm_storeu_si128((__m128i*)tmp, sumXY_128); |
298 | sumXY = tmp[3] + tmp[2] + tmp[1] + tmp[0]; |
299 | |
300 | retval += VP8LFastSLog2(sumX) + VP8LFastSLog2(sumXY); |
301 | return (float)retval; |
302 | } |
303 | #undef ANALYZE_X_OR_Y |
304 | #undef ANALYZE_XY |
305 | |
306 | //------------------------------------------------------------------------------ |
307 | |
308 | static int VectorMismatch_SSE2(const uint32_t* const array1, |
309 | const uint32_t* const array2, int length) { |
310 | int match_len; |
311 | |
312 | if (length >= 12) { |
313 | __m128i A0 = _mm_loadu_si128((const __m128i*)&array1[0]); |
314 | __m128i A1 = _mm_loadu_si128((const __m128i*)&array2[0]); |
315 | match_len = 0; |
316 | do { |
317 | // Loop unrolling and early load both provide a speedup of 10% for the |
318 | // current function. Also, max_limit can be MAX_LENGTH=4096 at most. |
319 | const __m128i cmpA = _mm_cmpeq_epi32(A0, A1); |
320 | const __m128i B0 = |
321 | _mm_loadu_si128((const __m128i*)&array1[match_len + 4]); |
322 | const __m128i B1 = |
323 | _mm_loadu_si128((const __m128i*)&array2[match_len + 4]); |
324 | if (_mm_movemask_epi8(cmpA) != 0xffff) break; |
325 | match_len += 4; |
326 | |
327 | { |
328 | const __m128i cmpB = _mm_cmpeq_epi32(B0, B1); |
329 | A0 = _mm_loadu_si128((const __m128i*)&array1[match_len + 4]); |
330 | A1 = _mm_loadu_si128((const __m128i*)&array2[match_len + 4]); |
331 | if (_mm_movemask_epi8(cmpB) != 0xffff) break; |
332 | match_len += 4; |
333 | } |
334 | } while (match_len + 12 < length); |
335 | } else { |
336 | match_len = 0; |
337 | // Unroll the potential first two loops. |
338 | if (length >= 4 && |
339 | _mm_movemask_epi8(_mm_cmpeq_epi32( |
340 | _mm_loadu_si128((const __m128i*)&array1[0]), |
341 | _mm_loadu_si128((const __m128i*)&array2[0]))) == 0xffff) { |
342 | match_len = 4; |
343 | if (length >= 8 && |
344 | _mm_movemask_epi8(_mm_cmpeq_epi32( |
345 | _mm_loadu_si128((const __m128i*)&array1[4]), |
346 | _mm_loadu_si128((const __m128i*)&array2[4]))) == 0xffff) { |
347 | match_len = 8; |
348 | } |
349 | } |
350 | } |
351 | |
352 | while (match_len < length && array1[match_len] == array2[match_len]) { |
353 | ++match_len; |
354 | } |
355 | return match_len; |
356 | } |
357 | |
358 | // Bundles multiple (1, 2, 4 or 8) pixels into a single pixel. |
359 | static void BundleColorMap_SSE2(const uint8_t* const row, int width, int xbits, |
360 | uint32_t* dst) { |
361 | int x; |
362 | assert(xbits >= 0); |
363 | assert(xbits <= 3); |
364 | switch (xbits) { |
365 | case 0: { |
366 | const __m128i ff = _mm_set1_epi16((short)0xff00); |
367 | const __m128i zero = _mm_setzero_si128(); |
368 | // Store 0xff000000 | (row[x] << 8). |
369 | for (x = 0; x + 16 <= width; x += 16, dst += 16) { |
370 | const __m128i in = _mm_loadu_si128((const __m128i*)&row[x]); |
371 | const __m128i in_lo = _mm_unpacklo_epi8(zero, in); |
372 | const __m128i dst0 = _mm_unpacklo_epi16(in_lo, ff); |
373 | const __m128i dst1 = _mm_unpackhi_epi16(in_lo, ff); |
374 | const __m128i in_hi = _mm_unpackhi_epi8(zero, in); |
375 | const __m128i dst2 = _mm_unpacklo_epi16(in_hi, ff); |
376 | const __m128i dst3 = _mm_unpackhi_epi16(in_hi, ff); |
377 | _mm_storeu_si128((__m128i*)&dst[0], dst0); |
378 | _mm_storeu_si128((__m128i*)&dst[4], dst1); |
379 | _mm_storeu_si128((__m128i*)&dst[8], dst2); |
380 | _mm_storeu_si128((__m128i*)&dst[12], dst3); |
381 | } |
382 | break; |
383 | } |
384 | case 1: { |
385 | const __m128i ff = _mm_set1_epi16((short)0xff00); |
386 | const __m128i mul = _mm_set1_epi16(0x110); |
387 | for (x = 0; x + 16 <= width; x += 16, dst += 8) { |
388 | // 0a0b | (where a/b are 4 bits). |
389 | const __m128i in = _mm_loadu_si128((const __m128i*)&row[x]); |
390 | const __m128i tmp = _mm_mullo_epi16(in, mul); // aba0 |
391 | const __m128i pack = _mm_and_si128(tmp, ff); // ab00 |
392 | const __m128i dst0 = _mm_unpacklo_epi16(pack, ff); |
393 | const __m128i dst1 = _mm_unpackhi_epi16(pack, ff); |
394 | _mm_storeu_si128((__m128i*)&dst[0], dst0); |
395 | _mm_storeu_si128((__m128i*)&dst[4], dst1); |
396 | } |
397 | break; |
398 | } |
399 | case 2: { |
400 | const __m128i mask_or = _mm_set1_epi32(0xff000000); |
401 | const __m128i mul_cst = _mm_set1_epi16(0x0104); |
402 | const __m128i mask_mul = _mm_set1_epi16(0x0f00); |
403 | for (x = 0; x + 16 <= width; x += 16, dst += 4) { |
404 | // 000a000b000c000d | (where a/b/c/d are 2 bits). |
405 | const __m128i in = _mm_loadu_si128((const __m128i*)&row[x]); |
406 | const __m128i mul = _mm_mullo_epi16(in, mul_cst); // 00ab00b000cd00d0 |
407 | const __m128i tmp = _mm_and_si128(mul, mask_mul); // 00ab000000cd0000 |
408 | const __m128i shift = _mm_srli_epi32(tmp, 12); // 00000000ab000000 |
409 | const __m128i pack = _mm_or_si128(shift, tmp); // 00000000abcd0000 |
410 | // Convert to 0xff00**00. |
411 | const __m128i res = _mm_or_si128(pack, mask_or); |
412 | _mm_storeu_si128((__m128i*)dst, res); |
413 | } |
414 | break; |
415 | } |
416 | default: { |
417 | assert(xbits == 3); |
418 | for (x = 0; x + 16 <= width; x += 16, dst += 2) { |
419 | // 0000000a00000000b... | (where a/b are 1 bit). |
420 | const __m128i in = _mm_loadu_si128((const __m128i*)&row[x]); |
421 | const __m128i shift = _mm_slli_epi64(in, 7); |
422 | const uint32_t move = _mm_movemask_epi8(shift); |
423 | dst[0] = 0xff000000 | ((move & 0xff) << 8); |
424 | dst[1] = 0xff000000 | (move & 0xff00); |
425 | } |
426 | break; |
427 | } |
428 | } |
429 | if (x != width) { |
430 | VP8LBundleColorMap_C(row + x, width - x, xbits, dst); |
431 | } |
432 | } |
433 | |
434 | //------------------------------------------------------------------------------ |
435 | // Batch version of Predictor Transform subtraction |
436 | |
437 | static WEBP_INLINE void Average2_m128i(const __m128i* const a0, |
438 | const __m128i* const a1, |
439 | __m128i* const avg) { |
440 | // (a + b) >> 1 = ((a + b + 1) >> 1) - ((a ^ b) & 1) |
441 | const __m128i ones = _mm_set1_epi8(1); |
442 | const __m128i avg1 = _mm_avg_epu8(*a0, *a1); |
443 | const __m128i one = _mm_and_si128(_mm_xor_si128(*a0, *a1), ones); |
444 | *avg = _mm_sub_epi8(avg1, one); |
445 | } |
446 | |
447 | // Predictor0: ARGB_BLACK. |
448 | static void PredictorSub0_SSE2(const uint32_t* in, const uint32_t* upper, |
449 | int num_pixels, uint32_t* out) { |
450 | int i; |
451 | const __m128i black = _mm_set1_epi32(ARGB_BLACK); |
452 | for (i = 0; i + 4 <= num_pixels; i += 4) { |
453 | const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); |
454 | const __m128i res = _mm_sub_epi8(src, black); |
455 | _mm_storeu_si128((__m128i*)&out[i], res); |
456 | } |
457 | if (i != num_pixels) { |
458 | VP8LPredictorsSub_C[0](in + i, NULL, num_pixels - i, out + i); |
459 | } |
460 | (void)upper; |
461 | } |
462 | |
463 | #define GENERATE_PREDICTOR_1(X, IN) \ |
464 | static void PredictorSub##X##_SSE2(const uint32_t* in, const uint32_t* upper, \ |
465 | int num_pixels, uint32_t* out) { \ |
466 | int i; \ |
467 | for (i = 0; i + 4 <= num_pixels; i += 4) { \ |
468 | const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); \ |
469 | const __m128i pred = _mm_loadu_si128((const __m128i*)&(IN)); \ |
470 | const __m128i res = _mm_sub_epi8(src, pred); \ |
471 | _mm_storeu_si128((__m128i*)&out[i], res); \ |
472 | } \ |
473 | if (i != num_pixels) { \ |
474 | VP8LPredictorsSub_C[(X)](in + i, upper + i, num_pixels - i, out + i); \ |
475 | } \ |
476 | } |
477 | |
478 | GENERATE_PREDICTOR_1(1, in[i - 1]) // Predictor1: L |
479 | GENERATE_PREDICTOR_1(2, upper[i]) // Predictor2: T |
480 | GENERATE_PREDICTOR_1(3, upper[i + 1]) // Predictor3: TR |
481 | GENERATE_PREDICTOR_1(4, upper[i - 1]) // Predictor4: TL |
482 | #undef GENERATE_PREDICTOR_1 |
483 | |
484 | // Predictor5: avg2(avg2(L, TR), T) |
485 | static void PredictorSub5_SSE2(const uint32_t* in, const uint32_t* upper, |
486 | int num_pixels, uint32_t* out) { |
487 | int i; |
488 | for (i = 0; i + 4 <= num_pixels; i += 4) { |
489 | const __m128i L = _mm_loadu_si128((const __m128i*)&in[i - 1]); |
490 | const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]); |
491 | const __m128i TR = _mm_loadu_si128((const __m128i*)&upper[i + 1]); |
492 | const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); |
493 | __m128i avg, pred, res; |
494 | Average2_m128i(&L, &TR, &avg); |
495 | Average2_m128i(&avg, &T, &pred); |
496 | res = _mm_sub_epi8(src, pred); |
497 | _mm_storeu_si128((__m128i*)&out[i], res); |
498 | } |
499 | if (i != num_pixels) { |
500 | VP8LPredictorsSub_C[5](in + i, upper + i, num_pixels - i, out + i); |
501 | } |
502 | } |
503 | |
504 | #define GENERATE_PREDICTOR_2(X, A, B) \ |
505 | static void PredictorSub##X##_SSE2(const uint32_t* in, const uint32_t* upper, \ |
506 | int num_pixels, uint32_t* out) { \ |
507 | int i; \ |
508 | for (i = 0; i + 4 <= num_pixels; i += 4) { \ |
509 | const __m128i tA = _mm_loadu_si128((const __m128i*)&(A)); \ |
510 | const __m128i tB = _mm_loadu_si128((const __m128i*)&(B)); \ |
511 | const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); \ |
512 | __m128i pred, res; \ |
513 | Average2_m128i(&tA, &tB, &pred); \ |
514 | res = _mm_sub_epi8(src, pred); \ |
515 | _mm_storeu_si128((__m128i*)&out[i], res); \ |
516 | } \ |
517 | if (i != num_pixels) { \ |
518 | VP8LPredictorsSub_C[(X)](in + i, upper + i, num_pixels - i, out + i); \ |
519 | } \ |
520 | } |
521 | |
522 | GENERATE_PREDICTOR_2(6, in[i - 1], upper[i - 1]) // Predictor6: avg(L, TL) |
523 | GENERATE_PREDICTOR_2(7, in[i - 1], upper[i]) // Predictor7: avg(L, T) |
524 | GENERATE_PREDICTOR_2(8, upper[i - 1], upper[i]) // Predictor8: avg(TL, T) |
525 | GENERATE_PREDICTOR_2(9, upper[i], upper[i + 1]) // Predictor9: average(T, TR) |
526 | #undef GENERATE_PREDICTOR_2 |
527 | |
528 | // Predictor10: avg(avg(L,TL), avg(T, TR)). |
529 | static void PredictorSub10_SSE2(const uint32_t* in, const uint32_t* upper, |
530 | int num_pixels, uint32_t* out) { |
531 | int i; |
532 | for (i = 0; i + 4 <= num_pixels; i += 4) { |
533 | const __m128i L = _mm_loadu_si128((const __m128i*)&in[i - 1]); |
534 | const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); |
535 | const __m128i TL = _mm_loadu_si128((const __m128i*)&upper[i - 1]); |
536 | const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]); |
537 | const __m128i TR = _mm_loadu_si128((const __m128i*)&upper[i + 1]); |
538 | __m128i avgTTR, avgLTL, avg, res; |
539 | Average2_m128i(&T, &TR, &avgTTR); |
540 | Average2_m128i(&L, &TL, &avgLTL); |
541 | Average2_m128i(&avgTTR, &avgLTL, &avg); |
542 | res = _mm_sub_epi8(src, avg); |
543 | _mm_storeu_si128((__m128i*)&out[i], res); |
544 | } |
545 | if (i != num_pixels) { |
546 | VP8LPredictorsSub_C[10](in + i, upper + i, num_pixels - i, out + i); |
547 | } |
548 | } |
549 | |
550 | // Predictor11: select. |
551 | static void GetSumAbsDiff32_SSE2(const __m128i* const A, const __m128i* const B, |
552 | __m128i* const out) { |
553 | // We can unpack with any value on the upper 32 bits, provided it's the same |
554 | // on both operands (to that their sum of abs diff is zero). Here we use *A. |
555 | const __m128i A_lo = _mm_unpacklo_epi32(*A, *A); |
556 | const __m128i B_lo = _mm_unpacklo_epi32(*B, *A); |
557 | const __m128i A_hi = _mm_unpackhi_epi32(*A, *A); |
558 | const __m128i B_hi = _mm_unpackhi_epi32(*B, *A); |
559 | const __m128i s_lo = _mm_sad_epu8(A_lo, B_lo); |
560 | const __m128i s_hi = _mm_sad_epu8(A_hi, B_hi); |
561 | *out = _mm_packs_epi32(s_lo, s_hi); |
562 | } |
563 | |
564 | static void PredictorSub11_SSE2(const uint32_t* in, const uint32_t* upper, |
565 | int num_pixels, uint32_t* out) { |
566 | int i; |
567 | for (i = 0; i + 4 <= num_pixels; i += 4) { |
568 | const __m128i L = _mm_loadu_si128((const __m128i*)&in[i - 1]); |
569 | const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]); |
570 | const __m128i TL = _mm_loadu_si128((const __m128i*)&upper[i - 1]); |
571 | const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); |
572 | __m128i pa, pb; |
573 | GetSumAbsDiff32_SSE2(&T, &TL, &pa); // pa = sum |T-TL| |
574 | GetSumAbsDiff32_SSE2(&L, &TL, &pb); // pb = sum |L-TL| |
575 | { |
576 | const __m128i mask = _mm_cmpgt_epi32(pb, pa); |
577 | const __m128i A = _mm_and_si128(mask, L); |
578 | const __m128i B = _mm_andnot_si128(mask, T); |
579 | const __m128i pred = _mm_or_si128(A, B); // pred = (L > T)? L : T |
580 | const __m128i res = _mm_sub_epi8(src, pred); |
581 | _mm_storeu_si128((__m128i*)&out[i], res); |
582 | } |
583 | } |
584 | if (i != num_pixels) { |
585 | VP8LPredictorsSub_C[11](in + i, upper + i, num_pixels - i, out + i); |
586 | } |
587 | } |
588 | |
589 | // Predictor12: ClampedSubSubtractFull. |
590 | static void PredictorSub12_SSE2(const uint32_t* in, const uint32_t* upper, |
591 | int num_pixels, uint32_t* out) { |
592 | int i; |
593 | const __m128i zero = _mm_setzero_si128(); |
594 | for (i = 0; i + 4 <= num_pixels; i += 4) { |
595 | const __m128i src = _mm_loadu_si128((const __m128i*)&in[i]); |
596 | const __m128i L = _mm_loadu_si128((const __m128i*)&in[i - 1]); |
597 | const __m128i L_lo = _mm_unpacklo_epi8(L, zero); |
598 | const __m128i L_hi = _mm_unpackhi_epi8(L, zero); |
599 | const __m128i T = _mm_loadu_si128((const __m128i*)&upper[i]); |
600 | const __m128i T_lo = _mm_unpacklo_epi8(T, zero); |
601 | const __m128i T_hi = _mm_unpackhi_epi8(T, zero); |
602 | const __m128i TL = _mm_loadu_si128((const __m128i*)&upper[i - 1]); |
603 | const __m128i TL_lo = _mm_unpacklo_epi8(TL, zero); |
604 | const __m128i TL_hi = _mm_unpackhi_epi8(TL, zero); |
605 | const __m128i diff_lo = _mm_sub_epi16(T_lo, TL_lo); |
606 | const __m128i diff_hi = _mm_sub_epi16(T_hi, TL_hi); |
607 | const __m128i pred_lo = _mm_add_epi16(L_lo, diff_lo); |
608 | const __m128i pred_hi = _mm_add_epi16(L_hi, diff_hi); |
609 | const __m128i pred = _mm_packus_epi16(pred_lo, pred_hi); |
610 | const __m128i res = _mm_sub_epi8(src, pred); |
611 | _mm_storeu_si128((__m128i*)&out[i], res); |
612 | } |
613 | if (i != num_pixels) { |
614 | VP8LPredictorsSub_C[12](in + i, upper + i, num_pixels - i, out + i); |
615 | } |
616 | } |
617 | |
618 | // Predictors13: ClampedAddSubtractHalf |
619 | static void PredictorSub13_SSE2(const uint32_t* in, const uint32_t* upper, |
620 | int num_pixels, uint32_t* out) { |
621 | int i; |
622 | const __m128i zero = _mm_setzero_si128(); |
623 | for (i = 0; i + 2 <= num_pixels; i += 2) { |
624 | // we can only process two pixels at a time |
625 | const __m128i L = _mm_loadl_epi64((const __m128i*)&in[i - 1]); |
626 | const __m128i src = _mm_loadl_epi64((const __m128i*)&in[i]); |
627 | const __m128i T = _mm_loadl_epi64((const __m128i*)&upper[i]); |
628 | const __m128i TL = _mm_loadl_epi64((const __m128i*)&upper[i - 1]); |
629 | const __m128i L_lo = _mm_unpacklo_epi8(L, zero); |
630 | const __m128i T_lo = _mm_unpacklo_epi8(T, zero); |
631 | const __m128i TL_lo = _mm_unpacklo_epi8(TL, zero); |
632 | const __m128i sum = _mm_add_epi16(T_lo, L_lo); |
633 | const __m128i avg = _mm_srli_epi16(sum, 1); |
634 | const __m128i A1 = _mm_sub_epi16(avg, TL_lo); |
635 | const __m128i bit_fix = _mm_cmpgt_epi16(TL_lo, avg); |
636 | const __m128i A2 = _mm_sub_epi16(A1, bit_fix); |
637 | const __m128i A3 = _mm_srai_epi16(A2, 1); |
638 | const __m128i A4 = _mm_add_epi16(avg, A3); |
639 | const __m128i pred = _mm_packus_epi16(A4, A4); |
640 | const __m128i res = _mm_sub_epi8(src, pred); |
641 | _mm_storel_epi64((__m128i*)&out[i], res); |
642 | } |
643 | if (i != num_pixels) { |
644 | VP8LPredictorsSub_C[13](in + i, upper + i, num_pixels - i, out + i); |
645 | } |
646 | } |
647 | |
648 | //------------------------------------------------------------------------------ |
649 | // Entry point |
650 | |
651 | extern void VP8LEncDspInitSSE2(void); |
652 | |
653 | WEBP_TSAN_IGNORE_FUNCTION void VP8LEncDspInitSSE2(void) { |
654 | VP8LSubtractGreenFromBlueAndRed = SubtractGreenFromBlueAndRed_SSE2; |
655 | VP8LTransformColor = TransformColor_SSE2; |
656 | VP8LCollectColorBlueTransforms = CollectColorBlueTransforms_SSE2; |
657 | VP8LCollectColorRedTransforms = CollectColorRedTransforms_SSE2; |
658 | VP8LAddVector = AddVector_SSE2; |
659 | VP8LAddVectorEq = AddVectorEq_SSE2; |
660 | VP8LCombinedShannonEntropy = CombinedShannonEntropy_SSE2; |
661 | VP8LVectorMismatch = VectorMismatch_SSE2; |
662 | VP8LBundleColorMap = BundleColorMap_SSE2; |
663 | |
664 | VP8LPredictorsSub[0] = PredictorSub0_SSE2; |
665 | VP8LPredictorsSub[1] = PredictorSub1_SSE2; |
666 | VP8LPredictorsSub[2] = PredictorSub2_SSE2; |
667 | VP8LPredictorsSub[3] = PredictorSub3_SSE2; |
668 | VP8LPredictorsSub[4] = PredictorSub4_SSE2; |
669 | VP8LPredictorsSub[5] = PredictorSub5_SSE2; |
670 | VP8LPredictorsSub[6] = PredictorSub6_SSE2; |
671 | VP8LPredictorsSub[7] = PredictorSub7_SSE2; |
672 | VP8LPredictorsSub[8] = PredictorSub8_SSE2; |
673 | VP8LPredictorsSub[9] = PredictorSub9_SSE2; |
674 | VP8LPredictorsSub[10] = PredictorSub10_SSE2; |
675 | VP8LPredictorsSub[11] = PredictorSub11_SSE2; |
676 | VP8LPredictorsSub[12] = PredictorSub12_SSE2; |
677 | VP8LPredictorsSub[13] = PredictorSub13_SSE2; |
678 | VP8LPredictorsSub[14] = PredictorSub0_SSE2; // <- padding security sentinels |
679 | VP8LPredictorsSub[15] = PredictorSub0_SSE2; |
680 | } |
681 | |
682 | #else // !WEBP_USE_SSE2 |
683 | |
684 | WEBP_DSP_INIT_STUB(VP8LEncDspInitSSE2) |
685 | |
686 | #endif // WEBP_USE_SSE2 |
687 | |