/src/libwebp/src/dsp/dec_sse2.c
Line | Count | Source |
1 | | // Copyright 2011 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 version of some decoding functions (idct, loop filtering). |
11 | | // |
12 | | // Author: somnath@google.com (Somnath Banerjee) |
13 | | // cduvivier@google.com (Christian Duvivier) |
14 | | |
15 | | #include "src/dsp/dsp.h" |
16 | | |
17 | | #if defined(WEBP_USE_SSE2) |
18 | | |
19 | | // The 3-coeff sparse transform in SSE2 is not really faster than the plain-C |
20 | | // one it seems => disable it by default. Uncomment the following to enable: |
21 | | #if !defined(USE_TRANSFORM_AC3) |
22 | | #define USE_TRANSFORM_AC3 0 // ALTERNATE_CODE |
23 | | #endif |
24 | | |
25 | | #include <emmintrin.h> |
26 | | |
27 | | #include "src/dec/vp8i_dec.h" |
28 | | #include "src/dsp/common_sse2.h" |
29 | | #include "src/dsp/cpu.h" |
30 | | #include "src/utils/utils.h" |
31 | | #include "src/webp/types.h" |
32 | | |
33 | | //------------------------------------------------------------------------------ |
34 | | // Transforms (Paragraph 14.4) |
35 | | |
36 | | static void Transform_SSE2(const int16_t* WEBP_RESTRICT in, |
37 | 2.47M | uint8_t* WEBP_RESTRICT dst, int do_two) { |
38 | | // This implementation makes use of 16-bit fixed point versions of two |
39 | | // multiply constants: |
40 | | // K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^16 |
41 | | // K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^16 |
42 | | // |
43 | | // To be able to use signed 16-bit integers, we use the following trick to |
44 | | // have constants within range: |
45 | | // - Associated constants are obtained by subtracting the 16-bit fixed point |
46 | | // version of one: |
47 | | // k = K - (1 << 16) => K = k + (1 << 16) |
48 | | // K1 = 85267 => k1 = 20091 |
49 | | // K2 = 35468 => k2 = -30068 |
50 | | // - The multiplication of a variable by a constant become the sum of the |
51 | | // variable and the multiplication of that variable by the associated |
52 | | // constant: |
53 | | // (x * K) >> 16 = (x * (k + (1 << 16))) >> 16 = ((x * k ) >> 16) + x |
54 | 2.47M | const __m128i k1 = _mm_set1_epi16(20091); |
55 | 2.47M | const __m128i k2 = _mm_set1_epi16(-30068); |
56 | 2.47M | __m128i T0, T1, T2, T3; |
57 | | |
58 | | // Load and concatenate the transform coefficients (we'll do two transforms |
59 | | // in parallel). In the case of only one transform, the second half of the |
60 | | // vectors will just contain random value we'll never use nor store. |
61 | 2.47M | __m128i in0, in1, in2, in3; |
62 | 2.47M | { |
63 | 2.47M | in0 = _mm_loadl_epi64((const __m128i*)&in[0]); |
64 | 2.47M | in1 = _mm_loadl_epi64((const __m128i*)&in[4]); |
65 | 2.47M | in2 = _mm_loadl_epi64((const __m128i*)&in[8]); |
66 | 2.47M | in3 = _mm_loadl_epi64((const __m128i*)&in[12]); |
67 | | // a00 a10 a20 a30 x x x x |
68 | | // a01 a11 a21 a31 x x x x |
69 | | // a02 a12 a22 a32 x x x x |
70 | | // a03 a13 a23 a33 x x x x |
71 | 2.47M | if (do_two) { |
72 | 664k | const __m128i inB0 = _mm_loadl_epi64((const __m128i*)&in[16]); |
73 | 664k | const __m128i inB1 = _mm_loadl_epi64((const __m128i*)&in[20]); |
74 | 664k | const __m128i inB2 = _mm_loadl_epi64((const __m128i*)&in[24]); |
75 | 664k | const __m128i inB3 = _mm_loadl_epi64((const __m128i*)&in[28]); |
76 | 664k | in0 = _mm_unpacklo_epi64(in0, inB0); |
77 | 664k | in1 = _mm_unpacklo_epi64(in1, inB1); |
78 | 664k | in2 = _mm_unpacklo_epi64(in2, inB2); |
79 | 664k | in3 = _mm_unpacklo_epi64(in3, inB3); |
80 | | // a00 a10 a20 a30 b00 b10 b20 b30 |
81 | | // a01 a11 a21 a31 b01 b11 b21 b31 |
82 | | // a02 a12 a22 a32 b02 b12 b22 b32 |
83 | | // a03 a13 a23 a33 b03 b13 b23 b33 |
84 | 664k | } |
85 | 2.47M | } |
86 | | |
87 | | // Vertical pass and subsequent transpose. |
88 | 2.47M | { |
89 | | // First pass, c and d calculations are longer because of the "trick" |
90 | | // multiplications. |
91 | 2.47M | const __m128i a = _mm_add_epi16(in0, in2); |
92 | 2.47M | const __m128i b = _mm_sub_epi16(in0, in2); |
93 | | // c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in3 |
94 | 2.47M | const __m128i c1 = _mm_mulhi_epi16(in1, k2); |
95 | 2.47M | const __m128i c2 = _mm_mulhi_epi16(in3, k1); |
96 | 2.47M | const __m128i c3 = _mm_sub_epi16(in1, in3); |
97 | 2.47M | const __m128i c4 = _mm_sub_epi16(c1, c2); |
98 | 2.47M | const __m128i c = _mm_add_epi16(c3, c4); |
99 | | // d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in3 |
100 | 2.47M | const __m128i d1 = _mm_mulhi_epi16(in1, k1); |
101 | 2.47M | const __m128i d2 = _mm_mulhi_epi16(in3, k2); |
102 | 2.47M | const __m128i d3 = _mm_add_epi16(in1, in3); |
103 | 2.47M | const __m128i d4 = _mm_add_epi16(d1, d2); |
104 | 2.47M | const __m128i d = _mm_add_epi16(d3, d4); |
105 | | |
106 | | // Second pass. |
107 | 2.47M | const __m128i tmp0 = _mm_add_epi16(a, d); |
108 | 2.47M | const __m128i tmp1 = _mm_add_epi16(b, c); |
109 | 2.47M | const __m128i tmp2 = _mm_sub_epi16(b, c); |
110 | 2.47M | const __m128i tmp3 = _mm_sub_epi16(a, d); |
111 | | |
112 | | // Transpose the two 4x4. |
113 | 2.47M | VP8Transpose_2_4x4_16b(&tmp0, &tmp1, &tmp2, &tmp3, &T0, &T1, &T2, &T3); |
114 | 2.47M | } |
115 | | |
116 | | // Horizontal pass and subsequent transpose. |
117 | 2.47M | { |
118 | | // First pass, c and d calculations are longer because of the "trick" |
119 | | // multiplications. |
120 | 2.47M | const __m128i four = _mm_set1_epi16(4); |
121 | 2.47M | const __m128i dc = _mm_add_epi16(T0, four); |
122 | 2.47M | const __m128i a = _mm_add_epi16(dc, T2); |
123 | 2.47M | const __m128i b = _mm_sub_epi16(dc, T2); |
124 | | // c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3 |
125 | 2.47M | const __m128i c1 = _mm_mulhi_epi16(T1, k2); |
126 | 2.47M | const __m128i c2 = _mm_mulhi_epi16(T3, k1); |
127 | 2.47M | const __m128i c3 = _mm_sub_epi16(T1, T3); |
128 | 2.47M | const __m128i c4 = _mm_sub_epi16(c1, c2); |
129 | 2.47M | const __m128i c = _mm_add_epi16(c3, c4); |
130 | | // d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3 |
131 | 2.47M | const __m128i d1 = _mm_mulhi_epi16(T1, k1); |
132 | 2.47M | const __m128i d2 = _mm_mulhi_epi16(T3, k2); |
133 | 2.47M | const __m128i d3 = _mm_add_epi16(T1, T3); |
134 | 2.47M | const __m128i d4 = _mm_add_epi16(d1, d2); |
135 | 2.47M | const __m128i d = _mm_add_epi16(d3, d4); |
136 | | |
137 | | // Second pass. |
138 | 2.47M | const __m128i tmp0 = _mm_add_epi16(a, d); |
139 | 2.47M | const __m128i tmp1 = _mm_add_epi16(b, c); |
140 | 2.47M | const __m128i tmp2 = _mm_sub_epi16(b, c); |
141 | 2.47M | const __m128i tmp3 = _mm_sub_epi16(a, d); |
142 | 2.47M | const __m128i shifted0 = _mm_srai_epi16(tmp0, 3); |
143 | 2.47M | const __m128i shifted1 = _mm_srai_epi16(tmp1, 3); |
144 | 2.47M | const __m128i shifted2 = _mm_srai_epi16(tmp2, 3); |
145 | 2.47M | const __m128i shifted3 = _mm_srai_epi16(tmp3, 3); |
146 | | |
147 | | // Transpose the two 4x4. |
148 | 2.47M | VP8Transpose_2_4x4_16b(&shifted0, &shifted1, &shifted2, &shifted3, &T0, &T1, |
149 | 2.47M | &T2, &T3); |
150 | 2.47M | } |
151 | | |
152 | | // Add inverse transform to 'dst' and store. |
153 | 2.47M | { |
154 | 2.47M | const __m128i zero = _mm_setzero_si128(); |
155 | | // Load the reference(s). |
156 | 2.47M | __m128i dst0, dst1, dst2, dst3; |
157 | 2.47M | if (do_two) { |
158 | | // Load eight bytes/pixels per line. |
159 | 664k | dst0 = _mm_loadl_epi64((__m128i*)(dst + 0 * BPS)); |
160 | 664k | dst1 = _mm_loadl_epi64((__m128i*)(dst + 1 * BPS)); |
161 | 664k | dst2 = _mm_loadl_epi64((__m128i*)(dst + 2 * BPS)); |
162 | 664k | dst3 = _mm_loadl_epi64((__m128i*)(dst + 3 * BPS)); |
163 | 1.80M | } else { |
164 | | // Load four bytes/pixels per line. |
165 | 1.80M | dst0 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 0 * BPS)); |
166 | 1.80M | dst1 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 1 * BPS)); |
167 | 1.80M | dst2 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 2 * BPS)); |
168 | 1.80M | dst3 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 3 * BPS)); |
169 | 1.80M | } |
170 | | // Convert to 16b. |
171 | 2.47M | dst0 = _mm_unpacklo_epi8(dst0, zero); |
172 | 2.47M | dst1 = _mm_unpacklo_epi8(dst1, zero); |
173 | 2.47M | dst2 = _mm_unpacklo_epi8(dst2, zero); |
174 | 2.47M | dst3 = _mm_unpacklo_epi8(dst3, zero); |
175 | | // Add the inverse transform(s). |
176 | 2.47M | dst0 = _mm_add_epi16(dst0, T0); |
177 | 2.47M | dst1 = _mm_add_epi16(dst1, T1); |
178 | 2.47M | dst2 = _mm_add_epi16(dst2, T2); |
179 | 2.47M | dst3 = _mm_add_epi16(dst3, T3); |
180 | | // Unsigned saturate to 8b. |
181 | 2.47M | dst0 = _mm_packus_epi16(dst0, dst0); |
182 | 2.47M | dst1 = _mm_packus_epi16(dst1, dst1); |
183 | 2.47M | dst2 = _mm_packus_epi16(dst2, dst2); |
184 | 2.47M | dst3 = _mm_packus_epi16(dst3, dst3); |
185 | | // Store the results. |
186 | 2.47M | if (do_two) { |
187 | | // Store eight bytes/pixels per line. |
188 | 664k | _mm_storel_epi64((__m128i*)(dst + 0 * BPS), dst0); |
189 | 664k | _mm_storel_epi64((__m128i*)(dst + 1 * BPS), dst1); |
190 | 664k | _mm_storel_epi64((__m128i*)(dst + 2 * BPS), dst2); |
191 | 664k | _mm_storel_epi64((__m128i*)(dst + 3 * BPS), dst3); |
192 | 1.80M | } else { |
193 | | // Store four bytes/pixels per line. |
194 | 1.80M | WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(dst0)); |
195 | 1.80M | WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(dst1)); |
196 | 1.80M | WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(dst2)); |
197 | 1.80M | WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(dst3)); |
198 | 1.80M | } |
199 | 2.47M | } |
200 | 2.47M | } |
201 | | |
202 | | #if (USE_TRANSFORM_AC3 == 1) |
203 | | |
204 | | static void TransformAC3_SSE2(const int16_t* WEBP_RESTRICT in, |
205 | | uint8_t* WEBP_RESTRICT dst) { |
206 | | const __m128i A = _mm_set1_epi16(in[0] + 4); |
207 | | const __m128i c4 = _mm_set1_epi16(WEBP_TRANSFORM_AC3_MUL2(in[4])); |
208 | | const __m128i d4 = _mm_set1_epi16(WEBP_TRANSFORM_AC3_MUL1(in[4])); |
209 | | const int c1 = WEBP_TRANSFORM_AC3_MUL2(in[1]); |
210 | | const int d1 = WEBP_TRANSFORM_AC3_MUL1(in[1]); |
211 | | const __m128i CD = _mm_set_epi16(0, 0, 0, 0, -d1, -c1, c1, d1); |
212 | | const __m128i B = _mm_adds_epi16(A, CD); |
213 | | const __m128i m0 = _mm_adds_epi16(B, d4); |
214 | | const __m128i m1 = _mm_adds_epi16(B, c4); |
215 | | const __m128i m2 = _mm_subs_epi16(B, c4); |
216 | | const __m128i m3 = _mm_subs_epi16(B, d4); |
217 | | const __m128i zero = _mm_setzero_si128(); |
218 | | // Load the source pixels. |
219 | | __m128i dst0 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 0 * BPS)); |
220 | | __m128i dst1 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 1 * BPS)); |
221 | | __m128i dst2 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 2 * BPS)); |
222 | | __m128i dst3 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 3 * BPS)); |
223 | | // Convert to 16b. |
224 | | dst0 = _mm_unpacklo_epi8(dst0, zero); |
225 | | dst1 = _mm_unpacklo_epi8(dst1, zero); |
226 | | dst2 = _mm_unpacklo_epi8(dst2, zero); |
227 | | dst3 = _mm_unpacklo_epi8(dst3, zero); |
228 | | // Add the inverse transform. |
229 | | dst0 = _mm_adds_epi16(dst0, _mm_srai_epi16(m0, 3)); |
230 | | dst1 = _mm_adds_epi16(dst1, _mm_srai_epi16(m1, 3)); |
231 | | dst2 = _mm_adds_epi16(dst2, _mm_srai_epi16(m2, 3)); |
232 | | dst3 = _mm_adds_epi16(dst3, _mm_srai_epi16(m3, 3)); |
233 | | // Unsigned saturate to 8b. |
234 | | dst0 = _mm_packus_epi16(dst0, dst0); |
235 | | dst1 = _mm_packus_epi16(dst1, dst1); |
236 | | dst2 = _mm_packus_epi16(dst2, dst2); |
237 | | dst3 = _mm_packus_epi16(dst3, dst3); |
238 | | // Store the results. |
239 | | WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(dst0)); |
240 | | WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(dst1)); |
241 | | WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(dst2)); |
242 | | WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(dst3)); |
243 | | } |
244 | | |
245 | | #endif // USE_TRANSFORM_AC3 |
246 | | |
247 | | //------------------------------------------------------------------------------ |
248 | | // Loop Filter (Paragraph 15) |
249 | | |
250 | | // Compute abs(p - q) = subs(p - q) OR subs(q - p) |
251 | | #define MM_ABS(p, q) \ |
252 | 117M | _mm_or_si128(_mm_subs_epu8((q), (p)), _mm_subs_epu8((p), (q))) |
253 | | |
254 | | // Shift each byte of "x" by 3 bits while preserving by the sign bit. |
255 | 30.6M | static WEBP_INLINE void SignedShift8b_SSE2(__m128i* const x) { |
256 | 30.6M | const __m128i zero = _mm_setzero_si128(); |
257 | 30.6M | const __m128i lo_0 = _mm_unpacklo_epi8(zero, *x); |
258 | 30.6M | const __m128i hi_0 = _mm_unpackhi_epi8(zero, *x); |
259 | 30.6M | const __m128i lo_1 = _mm_srai_epi16(lo_0, 3 + 8); |
260 | 30.6M | const __m128i hi_1 = _mm_srai_epi16(hi_0, 3 + 8); |
261 | 30.6M | *x = _mm_packs_epi16(lo_1, hi_1); |
262 | 30.6M | } |
263 | | |
264 | | #define FLIP_SIGN_BIT2(a, b) \ |
265 | 55.8M | do { \ |
266 | 55.8M | (a) = _mm_xor_si128(a, sign_bit); \ |
267 | 55.8M | (b) = _mm_xor_si128(b, sign_bit); \ |
268 | 55.8M | } while (0) |
269 | | |
270 | | #define FLIP_SIGN_BIT4(a, b, c, d) \ |
271 | 10.9M | do { \ |
272 | 10.9M | FLIP_SIGN_BIT2(a, b); \ |
273 | 10.9M | FLIP_SIGN_BIT2(c, d); \ |
274 | 10.9M | } while (0) |
275 | | |
276 | | // input/output is uint8_t |
277 | | static WEBP_INLINE void GetNotHEV_SSE2(const __m128i* const p1, |
278 | | const __m128i* const p0, |
279 | | const __m128i* const q0, |
280 | | const __m128i* const q1, int hev_thresh, |
281 | 10.9M | __m128i* const not_hev) { |
282 | 10.9M | const __m128i zero = _mm_setzero_si128(); |
283 | 10.9M | const __m128i t_1 = MM_ABS(*p1, *p0); |
284 | 10.9M | const __m128i t_2 = MM_ABS(*q1, *q0); |
285 | | |
286 | 10.9M | const __m128i h = _mm_set1_epi8(hev_thresh); |
287 | 10.9M | const __m128i t_max = _mm_max_epu8(t_1, t_2); |
288 | | |
289 | 10.9M | const __m128i t_max_h = _mm_subs_epu8(t_max, h); |
290 | 10.9M | *not_hev = _mm_cmpeq_epi8(t_max_h, zero); // not_hev <= t1 && not_hev <= t2 |
291 | 10.9M | } |
292 | | |
293 | | // input pixels are int8_t |
294 | | static WEBP_INLINE void GetBaseDelta_SSE2(const __m128i* const p1, |
295 | | const __m128i* const p0, |
296 | | const __m128i* const q0, |
297 | | const __m128i* const q1, |
298 | 6.10M | __m128i* const delta) { |
299 | | // beware of addition order, for saturation! |
300 | 6.10M | const __m128i p1_q1 = _mm_subs_epi8(*p1, *q1); // p1 - q1 |
301 | 6.10M | const __m128i q0_p0 = _mm_subs_epi8(*q0, *p0); // q0 - p0 |
302 | 6.10M | const __m128i s1 = _mm_adds_epi8(p1_q1, q0_p0); // p1 - q1 + 1 * (q0 - p0) |
303 | 6.10M | const __m128i s2 = _mm_adds_epi8(q0_p0, s1); // p1 - q1 + 2 * (q0 - p0) |
304 | 6.10M | const __m128i s3 = _mm_adds_epi8(q0_p0, s2); // p1 - q1 + 3 * (q0 - p0) |
305 | 6.10M | *delta = s3; |
306 | 6.10M | } |
307 | | |
308 | | // input and output are int8_t |
309 | | static WEBP_INLINE void DoSimpleFilter_SSE2(__m128i* const p0, |
310 | | __m128i* const q0, |
311 | 6.10M | const __m128i* const fl) { |
312 | 6.10M | const __m128i k3 = _mm_set1_epi8(3); |
313 | 6.10M | const __m128i k4 = _mm_set1_epi8(4); |
314 | 6.10M | __m128i v3 = _mm_adds_epi8(*fl, k3); |
315 | 6.10M | __m128i v4 = _mm_adds_epi8(*fl, k4); |
316 | | |
317 | 6.10M | SignedShift8b_SSE2(&v4); // v4 >> 3 |
318 | 6.10M | SignedShift8b_SSE2(&v3); // v3 >> 3 |
319 | 6.10M | *q0 = _mm_subs_epi8(*q0, v4); // q0 -= v4 |
320 | 6.10M | *p0 = _mm_adds_epi8(*p0, v3); // p0 += v3 |
321 | 6.10M | } |
322 | | |
323 | | // Updates values of 2 pixels at MB edge during complex filtering. |
324 | | // Update operations: |
325 | | // q = q - delta and p = p + delta; where delta = [(a_hi >> 7), (a_lo >> 7)] |
326 | | // Pixels 'pi' and 'qi' are int8_t on input, uint8_t on output (sign flip). |
327 | | static WEBP_INLINE void Update2Pixels_SSE2(__m128i* const pi, __m128i* const qi, |
328 | | const __m128i* const a0_lo, |
329 | 5.03M | const __m128i* const a0_hi) { |
330 | 5.03M | const __m128i a1_lo = _mm_srai_epi16(*a0_lo, 7); |
331 | 5.03M | const __m128i a1_hi = _mm_srai_epi16(*a0_hi, 7); |
332 | 5.03M | const __m128i delta = _mm_packs_epi16(a1_lo, a1_hi); |
333 | 5.03M | const __m128i sign_bit = _mm_set1_epi8((char)0x80); |
334 | 5.03M | *pi = _mm_adds_epi8(*pi, delta); |
335 | 5.03M | *qi = _mm_subs_epi8(*qi, delta); |
336 | 5.03M | FLIP_SIGN_BIT2(*pi, *qi); |
337 | 5.03M | } |
338 | | |
339 | | // input pixels are uint8_t |
340 | | static WEBP_INLINE void NeedsFilter_SSE2(const __m128i* const p1, |
341 | | const __m128i* const p0, |
342 | | const __m128i* const q0, |
343 | | const __m128i* const q1, int thresh, |
344 | 15.3M | __m128i* const mask) { |
345 | 15.3M | const __m128i m_thresh = _mm_set1_epi8((char)thresh); |
346 | 15.3M | const __m128i t1 = MM_ABS(*p1, *q1); // abs(p1 - q1) |
347 | 15.3M | const __m128i kFE = _mm_set1_epi8((char)0xFE); |
348 | 15.3M | const __m128i t2 = _mm_and_si128(t1, kFE); // set lsb of each byte to zero |
349 | 15.3M | const __m128i t3 = _mm_srli_epi16(t2, 1); // abs(p1 - q1) / 2 |
350 | | |
351 | 15.3M | const __m128i t4 = MM_ABS(*p0, *q0); // abs(p0 - q0) |
352 | 15.3M | const __m128i t5 = _mm_adds_epu8(t4, t4); // abs(p0 - q0) * 2 |
353 | 15.3M | const __m128i t6 = _mm_adds_epu8(t5, t3); // abs(p0-q0)*2 + abs(p1-q1)/2 |
354 | | |
355 | 15.3M | const __m128i t7 = _mm_subs_epu8(t6, m_thresh); // mask <= m_thresh |
356 | 15.3M | *mask = _mm_cmpeq_epi8(t7, _mm_setzero_si128()); |
357 | 15.3M | } |
358 | | |
359 | | //------------------------------------------------------------------------------ |
360 | | // Edge filtering functions |
361 | | |
362 | | // Applies filter on 2 pixels (p0 and q0) |
363 | | static WEBP_INLINE void DoFilter2_SSE2(__m128i* const p1, __m128i* const p0, |
364 | | __m128i* const q0, __m128i* const q1, |
365 | 4.42M | int thresh) { |
366 | 4.42M | __m128i a, mask; |
367 | 4.42M | const __m128i sign_bit = _mm_set1_epi8((char)0x80); |
368 | | // convert p1/q1 to int8_t (for GetBaseDelta_SSE2) |
369 | 4.42M | const __m128i p1s = _mm_xor_si128(*p1, sign_bit); |
370 | 4.42M | const __m128i q1s = _mm_xor_si128(*q1, sign_bit); |
371 | | |
372 | 4.42M | NeedsFilter_SSE2(p1, p0, q0, q1, thresh, &mask); |
373 | | |
374 | 4.42M | FLIP_SIGN_BIT2(*p0, *q0); |
375 | 4.42M | GetBaseDelta_SSE2(&p1s, p0, q0, &q1s, &a); |
376 | 4.42M | a = _mm_and_si128(a, mask); // mask filter values we don't care about |
377 | 4.42M | DoSimpleFilter_SSE2(p0, q0, &a); |
378 | 4.42M | FLIP_SIGN_BIT2(*p0, *q0); |
379 | 4.42M | } |
380 | | |
381 | | // Applies filter on 4 pixels (p1, p0, q0 and q1) |
382 | | static WEBP_INLINE void DoFilter4_SSE2(__m128i* const p1, __m128i* const p0, |
383 | | __m128i* const q0, __m128i* const q1, |
384 | | const __m128i* const mask, |
385 | 9.22M | int hev_thresh) { |
386 | 9.22M | const __m128i zero = _mm_setzero_si128(); |
387 | 9.22M | const __m128i sign_bit = _mm_set1_epi8((char)0x80); |
388 | 9.22M | const __m128i k64 = _mm_set1_epi8(64); |
389 | 9.22M | const __m128i k3 = _mm_set1_epi8(3); |
390 | 9.22M | const __m128i k4 = _mm_set1_epi8(4); |
391 | 9.22M | __m128i not_hev; |
392 | 9.22M | __m128i t1, t2, t3; |
393 | | |
394 | | // compute hev mask |
395 | 9.22M | GetNotHEV_SSE2(p1, p0, q0, q1, hev_thresh, ¬_hev); |
396 | | |
397 | | // convert to signed values |
398 | 9.22M | FLIP_SIGN_BIT4(*p1, *p0, *q0, *q1); |
399 | | |
400 | 9.22M | t1 = _mm_subs_epi8(*p1, *q1); // p1 - q1 |
401 | 9.22M | t1 = _mm_andnot_si128(not_hev, t1); // hev(p1 - q1) |
402 | 9.22M | t2 = _mm_subs_epi8(*q0, *p0); // q0 - p0 |
403 | 9.22M | t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 1 * (q0 - p0) |
404 | 9.22M | t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 2 * (q0 - p0) |
405 | 9.22M | t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 3 * (q0 - p0) |
406 | 9.22M | t1 = _mm_and_si128(t1, *mask); // mask filter values we don't care about |
407 | | |
408 | 9.22M | t2 = _mm_adds_epi8(t1, k3); // 3 * (q0 - p0) + hev(p1 - q1) + 3 |
409 | 9.22M | t3 = _mm_adds_epi8(t1, k4); // 3 * (q0 - p0) + hev(p1 - q1) + 4 |
410 | 9.22M | SignedShift8b_SSE2(&t2); // (3 * (q0 - p0) + hev(p1 - q1) + 3) >> 3 |
411 | 9.22M | SignedShift8b_SSE2(&t3); // (3 * (q0 - p0) + hev(p1 - q1) + 4) >> 3 |
412 | 9.22M | *p0 = _mm_adds_epi8(*p0, t2); // p0 += t2 |
413 | 9.22M | *q0 = _mm_subs_epi8(*q0, t3); // q0 -= t3 |
414 | 9.22M | FLIP_SIGN_BIT2(*p0, *q0); |
415 | | |
416 | | // this is equivalent to signed (a + 1) >> 1 calculation |
417 | 9.22M | t2 = _mm_add_epi8(t3, sign_bit); |
418 | 9.22M | t3 = _mm_avg_epu8(t2, zero); |
419 | 9.22M | t3 = _mm_sub_epi8(t3, k64); |
420 | | |
421 | 9.22M | t3 = _mm_and_si128(not_hev, t3); // if !hev |
422 | 9.22M | *q1 = _mm_subs_epi8(*q1, t3); // q1 -= t3 |
423 | 9.22M | *p1 = _mm_adds_epi8(*p1, t3); // p1 += t3 |
424 | 9.22M | FLIP_SIGN_BIT2(*p1, *q1); |
425 | 9.22M | } |
426 | | |
427 | | // Applies filter on 6 pixels (p2, p1, p0, q0, q1 and q2) |
428 | | static WEBP_INLINE void DoFilter6_SSE2(__m128i* const p2, __m128i* const p1, |
429 | | __m128i* const p0, __m128i* const q0, |
430 | | __m128i* const q1, __m128i* const q2, |
431 | | const __m128i* const mask, |
432 | 1.67M | int hev_thresh) { |
433 | 1.67M | const __m128i zero = _mm_setzero_si128(); |
434 | 1.67M | const __m128i sign_bit = _mm_set1_epi8((char)0x80); |
435 | 1.67M | __m128i a, not_hev; |
436 | | |
437 | | // compute hev mask |
438 | 1.67M | GetNotHEV_SSE2(p1, p0, q0, q1, hev_thresh, ¬_hev); |
439 | | |
440 | 1.67M | FLIP_SIGN_BIT4(*p1, *p0, *q0, *q1); |
441 | 1.67M | FLIP_SIGN_BIT2(*p2, *q2); |
442 | 1.67M | GetBaseDelta_SSE2(p1, p0, q0, q1, &a); |
443 | | |
444 | 1.67M | { // do simple filter on pixels with hev |
445 | 1.67M | const __m128i m = _mm_andnot_si128(not_hev, *mask); |
446 | 1.67M | const __m128i f = _mm_and_si128(a, m); |
447 | 1.67M | DoSimpleFilter_SSE2(p0, q0, &f); |
448 | 1.67M | } |
449 | | |
450 | 1.67M | { // do strong filter on pixels with not hev |
451 | 1.67M | const __m128i k9 = _mm_set1_epi16(0x0900); |
452 | 1.67M | const __m128i k63 = _mm_set1_epi16(63); |
453 | | |
454 | 1.67M | const __m128i m = _mm_and_si128(not_hev, *mask); |
455 | 1.67M | const __m128i f = _mm_and_si128(a, m); |
456 | | |
457 | 1.67M | const __m128i f_lo = _mm_unpacklo_epi8(zero, f); |
458 | 1.67M | const __m128i f_hi = _mm_unpackhi_epi8(zero, f); |
459 | | |
460 | 1.67M | const __m128i f9_lo = _mm_mulhi_epi16(f_lo, k9); // Filter (lo) * 9 |
461 | 1.67M | const __m128i f9_hi = _mm_mulhi_epi16(f_hi, k9); // Filter (hi) * 9 |
462 | | |
463 | 1.67M | const __m128i a2_lo = _mm_add_epi16(f9_lo, k63); // Filter * 9 + 63 |
464 | 1.67M | const __m128i a2_hi = _mm_add_epi16(f9_hi, k63); // Filter * 9 + 63 |
465 | | |
466 | 1.67M | const __m128i a1_lo = _mm_add_epi16(a2_lo, f9_lo); // Filter * 18 + 63 |
467 | 1.67M | const __m128i a1_hi = _mm_add_epi16(a2_hi, f9_hi); // Filter * 18 + 63 |
468 | | |
469 | 1.67M | const __m128i a0_lo = _mm_add_epi16(a1_lo, f9_lo); // Filter * 27 + 63 |
470 | 1.67M | const __m128i a0_hi = _mm_add_epi16(a1_hi, f9_hi); // Filter * 27 + 63 |
471 | | |
472 | 1.67M | Update2Pixels_SSE2(p2, q2, &a2_lo, &a2_hi); |
473 | 1.67M | Update2Pixels_SSE2(p1, q1, &a1_lo, &a1_hi); |
474 | 1.67M | Update2Pixels_SSE2(p0, q0, &a0_lo, &a0_hi); |
475 | 1.67M | } |
476 | 1.67M | } |
477 | | |
478 | | // reads 8 rows across a vertical edge. |
479 | | static WEBP_INLINE void Load8x4_SSE2(const uint8_t* const b, int stride, |
480 | 21.6M | __m128i* const p, __m128i* const q) { |
481 | | // A0 = 63 62 61 60 23 22 21 20 43 42 41 40 03 02 01 00 |
482 | | // A1 = 73 72 71 70 33 32 31 30 53 52 51 50 13 12 11 10 |
483 | 21.6M | const __m128i A0 = _mm_set_epi32( |
484 | 21.6M | WebPMemToInt32(&b[6 * stride]), WebPMemToInt32(&b[2 * stride]), |
485 | 21.6M | WebPMemToInt32(&b[4 * stride]), WebPMemToInt32(&b[0 * stride])); |
486 | 21.6M | const __m128i A1 = _mm_set_epi32( |
487 | 21.6M | WebPMemToInt32(&b[7 * stride]), WebPMemToInt32(&b[3 * stride]), |
488 | 21.6M | WebPMemToInt32(&b[5 * stride]), WebPMemToInt32(&b[1 * stride])); |
489 | | |
490 | | // B0 = 53 43 52 42 51 41 50 40 13 03 12 02 11 01 10 00 |
491 | | // B1 = 73 63 72 62 71 61 70 60 33 23 32 22 31 21 30 20 |
492 | 21.6M | const __m128i B0 = _mm_unpacklo_epi8(A0, A1); |
493 | 21.6M | const __m128i B1 = _mm_unpackhi_epi8(A0, A1); |
494 | | |
495 | | // C0 = 33 23 13 03 32 22 12 02 31 21 11 01 30 20 10 00 |
496 | | // C1 = 73 63 53 43 72 62 52 42 71 61 51 41 70 60 50 40 |
497 | 21.6M | const __m128i C0 = _mm_unpacklo_epi16(B0, B1); |
498 | 21.6M | const __m128i C1 = _mm_unpackhi_epi16(B0, B1); |
499 | | |
500 | | // *p = 71 61 51 41 31 21 11 01 70 60 50 40 30 20 10 00 |
501 | | // *q = 73 63 53 43 33 23 13 03 72 62 52 42 32 22 12 02 |
502 | 21.6M | *p = _mm_unpacklo_epi32(C0, C1); |
503 | 21.6M | *q = _mm_unpackhi_epi32(C0, C1); |
504 | 21.6M | } |
505 | | |
506 | | static WEBP_INLINE void Load16x4_SSE2(const uint8_t* const r0, |
507 | | const uint8_t* const r8, int stride, |
508 | | __m128i* const p1, __m128i* const p0, |
509 | 10.8M | __m128i* const q0, __m128i* const q1) { |
510 | | // Assume the pixels around the edge (|) are numbered as follows |
511 | | // 00 01 | 02 03 |
512 | | // 10 11 | 12 13 |
513 | | // ... | ... |
514 | | // e0 e1 | e2 e3 |
515 | | // f0 f1 | f2 f3 |
516 | | // |
517 | | // r0 is pointing to the 0th row (00) |
518 | | // r8 is pointing to the 8th row (80) |
519 | | |
520 | | // Load |
521 | | // p1 = 71 61 51 41 31 21 11 01 70 60 50 40 30 20 10 00 |
522 | | // q0 = 73 63 53 43 33 23 13 03 72 62 52 42 32 22 12 02 |
523 | | // p0 = f1 e1 d1 c1 b1 a1 91 81 f0 e0 d0 c0 b0 a0 90 80 |
524 | | // q1 = f3 e3 d3 c3 b3 a3 93 83 f2 e2 d2 c2 b2 a2 92 82 |
525 | 10.8M | Load8x4_SSE2(r0, stride, p1, q0); |
526 | 10.8M | Load8x4_SSE2(r8, stride, p0, q1); |
527 | | |
528 | 10.8M | { |
529 | | // p1 = f0 e0 d0 c0 b0 a0 90 80 70 60 50 40 30 20 10 00 |
530 | | // p0 = f1 e1 d1 c1 b1 a1 91 81 71 61 51 41 31 21 11 01 |
531 | | // q0 = f2 e2 d2 c2 b2 a2 92 82 72 62 52 42 32 22 12 02 |
532 | | // q1 = f3 e3 d3 c3 b3 a3 93 83 73 63 53 43 33 23 13 03 |
533 | 10.8M | const __m128i t1 = *p1; |
534 | 10.8M | const __m128i t2 = *q0; |
535 | 10.8M | *p1 = _mm_unpacklo_epi64(t1, *p0); |
536 | 10.8M | *p0 = _mm_unpackhi_epi64(t1, *p0); |
537 | 10.8M | *q0 = _mm_unpacklo_epi64(t2, *q1); |
538 | 10.8M | *q1 = _mm_unpackhi_epi64(t2, *q1); |
539 | 10.8M | } |
540 | 10.8M | } |
541 | | |
542 | | static WEBP_INLINE void Store4x4_SSE2(__m128i* const x, uint8_t* dst, |
543 | 34.0M | int stride) { |
544 | 34.0M | int i; |
545 | 170M | for (i = 0; i < 4; ++i, dst += stride) { |
546 | 136M | WebPInt32ToMem(dst, _mm_cvtsi128_si32(*x)); |
547 | 136M | *x = _mm_srli_si128(*x, 4); |
548 | 136M | } |
549 | 34.0M | } |
550 | | |
551 | | // Transpose back and store |
552 | | static WEBP_INLINE void Store16x4_SSE2(const __m128i* const p1, |
553 | | const __m128i* const p0, |
554 | | const __m128i* const q0, |
555 | | const __m128i* const q1, uint8_t* r0, |
556 | 8.51M | uint8_t* r8, int stride) { |
557 | 8.51M | __m128i t1, p1_s, p0_s, q0_s, q1_s; |
558 | | |
559 | | // p0 = 71 70 61 60 51 50 41 40 31 30 21 20 11 10 01 00 |
560 | | // p1 = f1 f0 e1 e0 d1 d0 c1 c0 b1 b0 a1 a0 91 90 81 80 |
561 | 8.51M | t1 = *p0; |
562 | 8.51M | p0_s = _mm_unpacklo_epi8(*p1, t1); |
563 | 8.51M | p1_s = _mm_unpackhi_epi8(*p1, t1); |
564 | | |
565 | | // q0 = 73 72 63 62 53 52 43 42 33 32 23 22 13 12 03 02 |
566 | | // q1 = f3 f2 e3 e2 d3 d2 c3 c2 b3 b2 a3 a2 93 92 83 82 |
567 | 8.51M | t1 = *q0; |
568 | 8.51M | q0_s = _mm_unpacklo_epi8(t1, *q1); |
569 | 8.51M | q1_s = _mm_unpackhi_epi8(t1, *q1); |
570 | | |
571 | | // p0 = 33 32 31 30 23 22 21 20 13 12 11 10 03 02 01 00 |
572 | | // q0 = 73 72 71 70 63 62 61 60 53 52 51 50 43 42 41 40 |
573 | 8.51M | t1 = p0_s; |
574 | 8.51M | p0_s = _mm_unpacklo_epi16(t1, q0_s); |
575 | 8.51M | q0_s = _mm_unpackhi_epi16(t1, q0_s); |
576 | | |
577 | | // p1 = b3 b2 b1 b0 a3 a2 a1 a0 93 92 91 90 83 82 81 80 |
578 | | // q1 = f3 f2 f1 f0 e3 e2 e1 e0 d3 d2 d1 d0 c3 c2 c1 c0 |
579 | 8.51M | t1 = p1_s; |
580 | 8.51M | p1_s = _mm_unpacklo_epi16(t1, q1_s); |
581 | 8.51M | q1_s = _mm_unpackhi_epi16(t1, q1_s); |
582 | | |
583 | 8.51M | Store4x4_SSE2(&p0_s, r0, stride); |
584 | 8.51M | r0 += 4 * stride; |
585 | 8.51M | Store4x4_SSE2(&q0_s, r0, stride); |
586 | | |
587 | 8.51M | Store4x4_SSE2(&p1_s, r8, stride); |
588 | 8.51M | r8 += 4 * stride; |
589 | 8.51M | Store4x4_SSE2(&q1_s, r8, stride); |
590 | 8.51M | } |
591 | | |
592 | | //------------------------------------------------------------------------------ |
593 | | // Simple In-loop filtering (Paragraph 15.2) |
594 | | |
595 | 2.22M | static void SimpleVFilter16_SSE2(uint8_t* p, int stride, int thresh) { |
596 | | // Load |
597 | 2.22M | __m128i p1 = _mm_loadu_si128((__m128i*)&p[-2 * stride]); |
598 | 2.22M | __m128i p0 = _mm_loadu_si128((__m128i*)&p[-stride]); |
599 | 2.22M | __m128i q0 = _mm_loadu_si128((__m128i*)&p[0]); |
600 | 2.22M | __m128i q1 = _mm_loadu_si128((__m128i*)&p[stride]); |
601 | | |
602 | 2.22M | DoFilter2_SSE2(&p1, &p0, &q0, &q1, thresh); |
603 | | |
604 | | // Store |
605 | 2.22M | _mm_storeu_si128((__m128i*)&p[-stride], p0); |
606 | 2.22M | _mm_storeu_si128((__m128i*)&p[0], q0); |
607 | 2.22M | } |
608 | | |
609 | 2.20M | static void SimpleHFilter16_SSE2(uint8_t* p, int stride, int thresh) { |
610 | 2.20M | __m128i p1, p0, q0, q1; |
611 | | |
612 | 2.20M | p -= 2; // beginning of p1 |
613 | | |
614 | 2.20M | Load16x4_SSE2(p, p + 8 * stride, stride, &p1, &p0, &q0, &q1); |
615 | 2.20M | DoFilter2_SSE2(&p1, &p0, &q0, &q1, thresh); |
616 | 2.20M | Store16x4_SSE2(&p1, &p0, &q0, &q1, p, p + 8 * stride, stride); |
617 | 2.20M | } |
618 | | |
619 | 694k | static void SimpleVFilter16i_SSE2(uint8_t* p, int stride, int thresh) { |
620 | 694k | int k; |
621 | 2.77M | for (k = 3; k > 0; --k) { |
622 | 2.08M | p += 4 * stride; |
623 | 2.08M | SimpleVFilter16_SSE2(p, stride, thresh); |
624 | 2.08M | } |
625 | 694k | } |
626 | | |
627 | 694k | static void SimpleHFilter16i_SSE2(uint8_t* p, int stride, int thresh) { |
628 | 694k | int k; |
629 | 2.77M | for (k = 3; k > 0; --k) { |
630 | 2.08M | p += 4; |
631 | 2.08M | SimpleHFilter16_SSE2(p, stride, thresh); |
632 | 2.08M | } |
633 | 694k | } |
634 | | |
635 | | //------------------------------------------------------------------------------ |
636 | | // Complex In-loop filtering (Paragraph 15.3) |
637 | | |
638 | | #define MAX_DIFF1(p3, p2, p1, p0, m) \ |
639 | 10.9M | do { \ |
640 | 10.9M | (m) = MM_ABS(p1, p0); \ |
641 | 10.9M | (m) = _mm_max_epu8(m, MM_ABS(p3, p2)); \ |
642 | 10.9M | (m) = _mm_max_epu8(m, MM_ABS(p2, p1)); \ |
643 | 10.9M | } while (0) |
644 | | |
645 | | #define MAX_DIFF2(p3, p2, p1, p0, m) \ |
646 | 10.9M | do { \ |
647 | 10.9M | (m) = _mm_max_epu8(m, MM_ABS(p1, p0)); \ |
648 | 10.9M | (m) = _mm_max_epu8(m, MM_ABS(p3, p2)); \ |
649 | 10.9M | (m) = _mm_max_epu8(m, MM_ABS(p2, p1)); \ |
650 | 10.9M | } while (0) |
651 | | |
652 | | #define LOAD_H_EDGES4(p, stride, e1, e2, e3, e4) \ |
653 | 5.44M | do { \ |
654 | 5.44M | (e1) = _mm_loadu_si128((__m128i*)&(p)[0 * (stride)]); \ |
655 | 5.44M | (e2) = _mm_loadu_si128((__m128i*)&(p)[1 * (stride)]); \ |
656 | 5.44M | (e3) = _mm_loadu_si128((__m128i*)&(p)[2 * (stride)]); \ |
657 | 5.44M | (e4) = _mm_loadu_si128((__m128i*)&(p)[3 * (stride)]); \ |
658 | 5.44M | } while (0) |
659 | | |
660 | | #define LOADUV_H_EDGE(p, u, v, stride) \ |
661 | 12.5M | do { \ |
662 | 12.5M | const __m128i U = _mm_loadl_epi64((__m128i*)&(u)[(stride)]); \ |
663 | 12.5M | const __m128i V = _mm_loadl_epi64((__m128i*)&(v)[(stride)]); \ |
664 | 12.5M | (p) = _mm_unpacklo_epi64(U, V); \ |
665 | 12.5M | } while (0) |
666 | | |
667 | | #define LOADUV_H_EDGES4(u, v, stride, e1, e2, e3, e4) \ |
668 | 3.13M | do { \ |
669 | 3.13M | LOADUV_H_EDGE(e1, u, v, 0 * (stride)); \ |
670 | 3.13M | LOADUV_H_EDGE(e2, u, v, 1 * (stride)); \ |
671 | 3.13M | LOADUV_H_EDGE(e3, u, v, 2 * (stride)); \ |
672 | 3.13M | LOADUV_H_EDGE(e4, u, v, 3 * (stride)); \ |
673 | 3.13M | } while (0) |
674 | | |
675 | | #define STOREUV(p, u, v, stride) \ |
676 | 7.10M | do { \ |
677 | 7.10M | _mm_storel_epi64((__m128i*)&(u)[(stride)], p); \ |
678 | 7.10M | (p) = _mm_srli_si128(p, 8); \ |
679 | 7.10M | _mm_storel_epi64((__m128i*)&(v)[(stride)], p); \ |
680 | 7.10M | } while (0) |
681 | | |
682 | | static WEBP_INLINE void ComplexMask_SSE2(const __m128i* const p1, |
683 | | const __m128i* const p0, |
684 | | const __m128i* const q0, |
685 | | const __m128i* const q1, int thresh, |
686 | 10.9M | int ithresh, __m128i* const mask) { |
687 | 10.9M | const __m128i it = _mm_set1_epi8(ithresh); |
688 | 10.9M | const __m128i diff = _mm_subs_epu8(*mask, it); |
689 | 10.9M | const __m128i thresh_mask = _mm_cmpeq_epi8(diff, _mm_setzero_si128()); |
690 | 10.9M | __m128i filter_mask; |
691 | 10.9M | NeedsFilter_SSE2(p1, p0, q0, q1, thresh, &filter_mask); |
692 | 10.9M | *mask = _mm_and_si128(thresh_mask, filter_mask); |
693 | 10.9M | } |
694 | | |
695 | | // on macroblock edges |
696 | | static void VFilter16_SSE2(uint8_t* p, int stride, int thresh, int ithresh, |
697 | 414k | int hev_thresh) { |
698 | 414k | __m128i t1; |
699 | 414k | __m128i mask; |
700 | 414k | __m128i p2, p1, p0, q0, q1, q2; |
701 | | |
702 | | // Load p3, p2, p1, p0 |
703 | 414k | LOAD_H_EDGES4(p - 4 * stride, stride, t1, p2, p1, p0); |
704 | 414k | MAX_DIFF1(t1, p2, p1, p0, mask); |
705 | | |
706 | | // Load q0, q1, q2, q3 |
707 | 414k | LOAD_H_EDGES4(p, stride, q0, q1, q2, t1); |
708 | 414k | MAX_DIFF2(t1, q2, q1, q0, mask); |
709 | | |
710 | 414k | ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask); |
711 | 414k | DoFilter6_SSE2(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh); |
712 | | |
713 | | // Store |
714 | 414k | _mm_storeu_si128((__m128i*)&p[-3 * stride], p2); |
715 | 414k | _mm_storeu_si128((__m128i*)&p[-2 * stride], p1); |
716 | 414k | _mm_storeu_si128((__m128i*)&p[-1 * stride], p0); |
717 | 414k | _mm_storeu_si128((__m128i*)&p[+0 * stride], q0); |
718 | 414k | _mm_storeu_si128((__m128i*)&p[+1 * stride], q1); |
719 | 414k | _mm_storeu_si128((__m128i*)&p[+2 * stride], q2); |
720 | 414k | } |
721 | | |
722 | | static void HFilter16_SSE2(uint8_t* p, int stride, int thresh, int ithresh, |
723 | 425k | int hev_thresh) { |
724 | 425k | __m128i mask; |
725 | 425k | __m128i p3, p2, p1, p0, q0, q1, q2, q3; |
726 | | |
727 | 425k | uint8_t* const b = p - 4; |
728 | 425k | Load16x4_SSE2(b, b + 8 * stride, stride, &p3, &p2, &p1, &p0); |
729 | 425k | MAX_DIFF1(p3, p2, p1, p0, mask); |
730 | | |
731 | 425k | Load16x4_SSE2(p, p + 8 * stride, stride, &q0, &q1, &q2, &q3); |
732 | 425k | MAX_DIFF2(q3, q2, q1, q0, mask); |
733 | | |
734 | 425k | ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask); |
735 | 425k | DoFilter6_SSE2(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh); |
736 | | |
737 | 425k | Store16x4_SSE2(&p3, &p2, &p1, &p0, b, b + 8 * stride, stride); |
738 | 425k | Store16x4_SSE2(&q0, &q1, &q2, &q3, p, p + 8 * stride, stride); |
739 | 425k | } |
740 | | |
741 | | // on three inner edges |
742 | | static void VFilter16i_SSE2(uint8_t* p, int stride, int thresh, int ithresh, |
743 | 1.15M | int hev_thresh) { |
744 | 1.15M | int k; |
745 | 1.15M | __m128i p3, p2, p1, p0; // loop invariants |
746 | | |
747 | 1.15M | LOAD_H_EDGES4(p, stride, p3, p2, p1, p0); // prologue |
748 | | |
749 | 4.61M | for (k = 3; k > 0; --k) { |
750 | 3.46M | __m128i mask, tmp1, tmp2; |
751 | 3.46M | uint8_t* const b = p + 2 * stride; // beginning of p1 |
752 | 3.46M | p += 4 * stride; |
753 | | |
754 | 3.46M | MAX_DIFF1(p3, p2, p1, p0, mask); // compute partial mask |
755 | 3.46M | LOAD_H_EDGES4(p, stride, p3, p2, tmp1, tmp2); |
756 | 3.46M | MAX_DIFF2(p3, p2, tmp1, tmp2, mask); |
757 | | |
758 | | // p3 and p2 are not just temporary variables here: they will be |
759 | | // re-used for next span. And q2/q3 will become p1/p0 accordingly. |
760 | 3.46M | ComplexMask_SSE2(&p1, &p0, &p3, &p2, thresh, ithresh, &mask); |
761 | 3.46M | DoFilter4_SSE2(&p1, &p0, &p3, &p2, &mask, hev_thresh); |
762 | | |
763 | | // Store |
764 | 3.46M | _mm_storeu_si128((__m128i*)&b[0 * stride], p1); |
765 | 3.46M | _mm_storeu_si128((__m128i*)&b[1 * stride], p0); |
766 | 3.46M | _mm_storeu_si128((__m128i*)&b[2 * stride], p3); |
767 | 3.46M | _mm_storeu_si128((__m128i*)&b[3 * stride], p2); |
768 | | |
769 | | // rotate samples |
770 | 3.46M | p1 = tmp1; |
771 | 3.46M | p0 = tmp2; |
772 | 3.46M | } |
773 | 1.15M | } |
774 | | |
775 | | static void HFilter16i_SSE2(uint8_t* p, int stride, int thresh, int ithresh, |
776 | 1.15M | int hev_thresh) { |
777 | 1.15M | int k; |
778 | 1.15M | __m128i p3, p2, p1, p0; // loop invariants |
779 | | |
780 | 1.15M | Load16x4_SSE2(p, p + 8 * stride, stride, &p3, &p2, &p1, &p0); // prologue |
781 | | |
782 | 4.61M | for (k = 3; k > 0; --k) { |
783 | 3.46M | __m128i mask, tmp1, tmp2; |
784 | 3.46M | uint8_t* const b = p + 2; // beginning of p1 |
785 | | |
786 | 3.46M | p += 4; // beginning of q0 (and next span) |
787 | | |
788 | 3.46M | MAX_DIFF1(p3, p2, p1, p0, mask); // compute partial mask |
789 | 3.46M | Load16x4_SSE2(p, p + 8 * stride, stride, &p3, &p2, &tmp1, &tmp2); |
790 | 3.46M | MAX_DIFF2(p3, p2, tmp1, tmp2, mask); |
791 | | |
792 | 3.46M | ComplexMask_SSE2(&p1, &p0, &p3, &p2, thresh, ithresh, &mask); |
793 | 3.46M | DoFilter4_SSE2(&p1, &p0, &p3, &p2, &mask, hev_thresh); |
794 | | |
795 | 3.46M | Store16x4_SSE2(&p1, &p0, &p3, &p2, b, b + 8 * stride, stride); |
796 | | |
797 | | // rotate samples |
798 | 3.46M | p1 = tmp1; |
799 | 3.46M | p0 = tmp2; |
800 | 3.46M | } |
801 | 1.15M | } |
802 | | |
803 | | // 8-pixels wide variant, for chroma filtering |
804 | | static void VFilter8_SSE2(uint8_t* WEBP_RESTRICT u, uint8_t* WEBP_RESTRICT v, |
805 | 414k | int stride, int thresh, int ithresh, int hev_thresh) { |
806 | 414k | __m128i mask; |
807 | 414k | __m128i t1, p2, p1, p0, q0, q1, q2; |
808 | | |
809 | | // Load p3, p2, p1, p0 |
810 | 414k | LOADUV_H_EDGES4(u - 4 * stride, v - 4 * stride, stride, t1, p2, p1, p0); |
811 | 414k | MAX_DIFF1(t1, p2, p1, p0, mask); |
812 | | |
813 | | // Load q0, q1, q2, q3 |
814 | 414k | LOADUV_H_EDGES4(u, v, stride, q0, q1, q2, t1); |
815 | 414k | MAX_DIFF2(t1, q2, q1, q0, mask); |
816 | | |
817 | 414k | ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask); |
818 | 414k | DoFilter6_SSE2(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh); |
819 | | |
820 | | // Store |
821 | 414k | STOREUV(p2, u, v, -3 * stride); |
822 | 414k | STOREUV(p1, u, v, -2 * stride); |
823 | 414k | STOREUV(p0, u, v, -1 * stride); |
824 | 414k | STOREUV(q0, u, v, 0 * stride); |
825 | 414k | STOREUV(q1, u, v, 1 * stride); |
826 | 414k | STOREUV(q2, u, v, 2 * stride); |
827 | 414k | } |
828 | | |
829 | | static void HFilter8_SSE2(uint8_t* WEBP_RESTRICT u, uint8_t* WEBP_RESTRICT v, |
830 | 425k | int stride, int thresh, int ithresh, int hev_thresh) { |
831 | 425k | __m128i mask; |
832 | 425k | __m128i p3, p2, p1, p0, q0, q1, q2, q3; |
833 | | |
834 | 425k | uint8_t* const tu = u - 4; |
835 | 425k | uint8_t* const tv = v - 4; |
836 | 425k | Load16x4_SSE2(tu, tv, stride, &p3, &p2, &p1, &p0); |
837 | 425k | MAX_DIFF1(p3, p2, p1, p0, mask); |
838 | | |
839 | 425k | Load16x4_SSE2(u, v, stride, &q0, &q1, &q2, &q3); |
840 | 425k | MAX_DIFF2(q3, q2, q1, q0, mask); |
841 | | |
842 | 425k | ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask); |
843 | 425k | DoFilter6_SSE2(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh); |
844 | | |
845 | 425k | Store16x4_SSE2(&p3, &p2, &p1, &p0, tu, tv, stride); |
846 | 425k | Store16x4_SSE2(&q0, &q1, &q2, &q3, u, v, stride); |
847 | 425k | } |
848 | | |
849 | | static void VFilter8i_SSE2(uint8_t* WEBP_RESTRICT u, uint8_t* WEBP_RESTRICT v, |
850 | | int stride, int thresh, int ithresh, |
851 | 1.15M | int hev_thresh) { |
852 | 1.15M | __m128i mask; |
853 | 1.15M | __m128i t1, t2, p1, p0, q0, q1; |
854 | | |
855 | | // Load p3, p2, p1, p0 |
856 | 1.15M | LOADUV_H_EDGES4(u, v, stride, t2, t1, p1, p0); |
857 | 1.15M | MAX_DIFF1(t2, t1, p1, p0, mask); |
858 | | |
859 | 1.15M | u += 4 * stride; |
860 | 1.15M | v += 4 * stride; |
861 | | |
862 | | // Load q0, q1, q2, q3 |
863 | 1.15M | LOADUV_H_EDGES4(u, v, stride, q0, q1, t1, t2); |
864 | 1.15M | MAX_DIFF2(t2, t1, q1, q0, mask); |
865 | | |
866 | 1.15M | ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask); |
867 | 1.15M | DoFilter4_SSE2(&p1, &p0, &q0, &q1, &mask, hev_thresh); |
868 | | |
869 | | // Store |
870 | 1.15M | STOREUV(p1, u, v, -2 * stride); |
871 | 1.15M | STOREUV(p0, u, v, -1 * stride); |
872 | 1.15M | STOREUV(q0, u, v, 0 * stride); |
873 | 1.15M | STOREUV(q1, u, v, 1 * stride); |
874 | 1.15M | } |
875 | | |
876 | | static void HFilter8i_SSE2(uint8_t* WEBP_RESTRICT u, uint8_t* WEBP_RESTRICT v, |
877 | | int stride, int thresh, int ithresh, |
878 | 1.15M | int hev_thresh) { |
879 | 1.15M | __m128i mask; |
880 | 1.15M | __m128i t1, t2, p1, p0, q0, q1; |
881 | 1.15M | Load16x4_SSE2(u, v, stride, &t2, &t1, &p1, &p0); // p3, p2, p1, p0 |
882 | 1.15M | MAX_DIFF1(t2, t1, p1, p0, mask); |
883 | | |
884 | 1.15M | u += 4; // beginning of q0 |
885 | 1.15M | v += 4; |
886 | 1.15M | Load16x4_SSE2(u, v, stride, &q0, &q1, &t1, &t2); // q0, q1, q2, q3 |
887 | 1.15M | MAX_DIFF2(t2, t1, q1, q0, mask); |
888 | | |
889 | 1.15M | ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask); |
890 | 1.15M | DoFilter4_SSE2(&p1, &p0, &q0, &q1, &mask, hev_thresh); |
891 | | |
892 | 1.15M | u -= 2; // beginning of p1 |
893 | 1.15M | v -= 2; |
894 | 1.15M | Store16x4_SSE2(&p1, &p0, &q0, &q1, u, v, stride); |
895 | 1.15M | } |
896 | | |
897 | | //------------------------------------------------------------------------------ |
898 | | // 4x4 predictions |
899 | | |
900 | 631k | #define DST(x, y) dst[(x) + (y) * BPS] |
901 | 326k | #define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2) |
902 | | |
903 | | // We use the following 8b-arithmetic tricks: |
904 | | // (a + 2 * b + c + 2) >> 2 = (AC + b + 1) >> 1 |
905 | | // where: AC = (a + c) >> 1 = [(a + c + 1) >> 1] - [(a^c) & 1] |
906 | | // and: |
907 | | // (a + 2 * b + c + 2) >> 2 = (AB + BC + 1) >> 1 - (ab|bc)&lsb |
908 | | // where: AC = (a + b + 1) >> 1, BC = (b + c + 1) >> 1 |
909 | | // and ab = a ^ b, bc = b ^ c, lsb = (AC^BC)&1 |
910 | | |
911 | 375k | static void VE4_SSE2(uint8_t* dst) { // vertical |
912 | 375k | const __m128i one = _mm_set1_epi8(1); |
913 | 375k | const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(dst - BPS - 1)); |
914 | 375k | const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1); |
915 | 375k | const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2); |
916 | 375k | const __m128i a = _mm_avg_epu8(ABCDEFGH, CDEFGH00); |
917 | 375k | const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGH00), one); |
918 | 375k | const __m128i b = _mm_subs_epu8(a, lsb); |
919 | 375k | const __m128i avg = _mm_avg_epu8(b, BCDEFGH0); |
920 | 375k | const int vals = _mm_cvtsi128_si32(avg); |
921 | 375k | int i; |
922 | 1.87M | for (i = 0; i < 4; ++i) { |
923 | 1.50M | WebPInt32ToMem(dst + i * BPS, vals); |
924 | 1.50M | } |
925 | 375k | } |
926 | | |
927 | 169k | static void LD4_SSE2(uint8_t* dst) { // Down-Left |
928 | 169k | const __m128i one = _mm_set1_epi8(1); |
929 | 169k | const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(dst - BPS)); |
930 | 169k | const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1); |
931 | 169k | const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2); |
932 | 169k | const __m128i CDEFGHH0 = _mm_insert_epi16(CDEFGH00, dst[-BPS + 7], 3); |
933 | 169k | const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, CDEFGHH0); |
934 | 169k | const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGHH0), one); |
935 | 169k | const __m128i avg2 = _mm_subs_epu8(avg1, lsb); |
936 | 169k | const __m128i abcdefg = _mm_avg_epu8(avg2, BCDEFGH0); |
937 | 169k | WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(abcdefg)); |
938 | 169k | WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1))); |
939 | 169k | WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2))); |
940 | 169k | WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3))); |
941 | 169k | } |
942 | | |
943 | 163k | static void VR4_SSE2(uint8_t* dst) { // Vertical-Right |
944 | 163k | const __m128i one = _mm_set1_epi8(1); |
945 | 163k | const int I = dst[-1 + 0 * BPS]; |
946 | 163k | const int J = dst[-1 + 1 * BPS]; |
947 | 163k | const int K = dst[-1 + 2 * BPS]; |
948 | 163k | const int X = dst[-1 - BPS]; |
949 | 163k | const __m128i XABCD = _mm_loadl_epi64((__m128i*)(dst - BPS - 1)); |
950 | 163k | const __m128i ABCD0 = _mm_srli_si128(XABCD, 1); |
951 | 163k | const __m128i abcd = _mm_avg_epu8(XABCD, ABCD0); |
952 | 163k | const __m128i _XABCD = _mm_slli_si128(XABCD, 1); |
953 | 163k | const __m128i IXABCD = _mm_insert_epi16(_XABCD, (short)(I | (X << 8)), 0); |
954 | 163k | const __m128i avg1 = _mm_avg_epu8(IXABCD, ABCD0); |
955 | 163k | const __m128i lsb = _mm_and_si128(_mm_xor_si128(IXABCD, ABCD0), one); |
956 | 163k | const __m128i avg2 = _mm_subs_epu8(avg1, lsb); |
957 | 163k | const __m128i efgh = _mm_avg_epu8(avg2, XABCD); |
958 | 163k | WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(abcd)); |
959 | 163k | WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(efgh)); |
960 | 163k | WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(abcd, 1))); |
961 | 163k | WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(efgh, 1))); |
962 | | |
963 | | // these two are hard to implement in SSE2, so we keep the C-version: |
964 | 163k | DST(0, 2) = AVG3(J, I, X); |
965 | 163k | DST(0, 3) = AVG3(K, J, I); |
966 | 163k | } |
967 | | |
968 | 152k | static void VL4_SSE2(uint8_t* dst) { // Vertical-Left |
969 | 152k | const __m128i one = _mm_set1_epi8(1); |
970 | 152k | const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(dst - BPS)); |
971 | 152k | const __m128i BCDEFGH_ = _mm_srli_si128(ABCDEFGH, 1); |
972 | 152k | const __m128i CDEFGH__ = _mm_srli_si128(ABCDEFGH, 2); |
973 | 152k | const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, BCDEFGH_); |
974 | 152k | const __m128i avg2 = _mm_avg_epu8(CDEFGH__, BCDEFGH_); |
975 | 152k | const __m128i avg3 = _mm_avg_epu8(avg1, avg2); |
976 | 152k | const __m128i lsb1 = _mm_and_si128(_mm_xor_si128(avg1, avg2), one); |
977 | 152k | const __m128i ab = _mm_xor_si128(ABCDEFGH, BCDEFGH_); |
978 | 152k | const __m128i bc = _mm_xor_si128(CDEFGH__, BCDEFGH_); |
979 | 152k | const __m128i abbc = _mm_or_si128(ab, bc); |
980 | 152k | const __m128i lsb2 = _mm_and_si128(abbc, lsb1); |
981 | 152k | const __m128i avg4 = _mm_subs_epu8(avg3, lsb2); |
982 | 152k | const uint32_t extra_out = |
983 | 152k | (uint32_t)_mm_cvtsi128_si32(_mm_srli_si128(avg4, 4)); |
984 | 152k | WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(avg1)); |
985 | 152k | WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(avg4)); |
986 | 152k | WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg1, 1))); |
987 | 152k | WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg4, 1))); |
988 | | |
989 | | // these two are hard to get and irregular |
990 | 152k | DST(3, 2) = (extra_out >> 0) & 0xff; |
991 | 152k | DST(3, 3) = (extra_out >> 8) & 0xff; |
992 | 152k | } |
993 | | |
994 | 126k | static void RD4_SSE2(uint8_t* dst) { // Down-right |
995 | 126k | const __m128i one = _mm_set1_epi8(1); |
996 | 126k | const __m128i XABCD = _mm_loadl_epi64((__m128i*)(dst - BPS - 1)); |
997 | 126k | const __m128i ____XABCD = _mm_slli_si128(XABCD, 4); |
998 | 126k | const uint32_t I = dst[-1 + 0 * BPS]; |
999 | 126k | const uint32_t J = dst[-1 + 1 * BPS]; |
1000 | 126k | const uint32_t K = dst[-1 + 2 * BPS]; |
1001 | 126k | const uint32_t L = dst[-1 + 3 * BPS]; |
1002 | 126k | const __m128i LKJI_____ = |
1003 | 126k | _mm_cvtsi32_si128((int)(L | (K << 8) | (J << 16) | (I << 24))); |
1004 | 126k | const __m128i LKJIXABCD = _mm_or_si128(LKJI_____, ____XABCD); |
1005 | 126k | const __m128i KJIXABCD_ = _mm_srli_si128(LKJIXABCD, 1); |
1006 | 126k | const __m128i JIXABCD__ = _mm_srli_si128(LKJIXABCD, 2); |
1007 | 126k | const __m128i avg1 = _mm_avg_epu8(JIXABCD__, LKJIXABCD); |
1008 | 126k | const __m128i lsb = _mm_and_si128(_mm_xor_si128(JIXABCD__, LKJIXABCD), one); |
1009 | 126k | const __m128i avg2 = _mm_subs_epu8(avg1, lsb); |
1010 | 126k | const __m128i abcdefg = _mm_avg_epu8(avg2, KJIXABCD_); |
1011 | 126k | WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(abcdefg)); |
1012 | 126k | WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1))); |
1013 | 126k | WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2))); |
1014 | 126k | WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3))); |
1015 | 126k | } |
1016 | | |
1017 | | #undef DST |
1018 | | #undef AVG3 |
1019 | | |
1020 | | //------------------------------------------------------------------------------ |
1021 | | // Luma 16x16 |
1022 | | |
1023 | 1.48M | static WEBP_INLINE void TrueMotion_SSE2(uint8_t* dst, int size) { |
1024 | 1.48M | const uint8_t* top = dst - BPS; |
1025 | 1.48M | const __m128i zero = _mm_setzero_si128(); |
1026 | 1.48M | int y; |
1027 | 1.48M | if (size == 4) { |
1028 | 1.17M | const __m128i top_values = _mm_cvtsi32_si128(WebPMemToInt32(top)); |
1029 | 1.17M | const __m128i top_base = _mm_unpacklo_epi8(top_values, zero); |
1030 | 5.85M | for (y = 0; y < 4; ++y, dst += BPS) { |
1031 | 4.68M | const int val = dst[-1] - top[-1]; |
1032 | 4.68M | const __m128i base = _mm_set1_epi16(val); |
1033 | 4.68M | const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero); |
1034 | 4.68M | WebPInt32ToMem(dst, _mm_cvtsi128_si32(out)); |
1035 | 4.68M | } |
1036 | 1.17M | } else if (size == 8) { |
1037 | 184k | const __m128i top_values = _mm_loadl_epi64((const __m128i*)top); |
1038 | 184k | const __m128i top_base = _mm_unpacklo_epi8(top_values, zero); |
1039 | 1.66M | for (y = 0; y < 8; ++y, dst += BPS) { |
1040 | 1.47M | const int val = dst[-1] - top[-1]; |
1041 | 1.47M | const __m128i base = _mm_set1_epi16(val); |
1042 | 1.47M | const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero); |
1043 | 1.47M | _mm_storel_epi64((__m128i*)dst, out); |
1044 | 1.47M | } |
1045 | 184k | } else { |
1046 | 128k | const __m128i top_values = _mm_loadu_si128((const __m128i*)top); |
1047 | 128k | const __m128i top_base_0 = _mm_unpacklo_epi8(top_values, zero); |
1048 | 128k | const __m128i top_base_1 = _mm_unpackhi_epi8(top_values, zero); |
1049 | 2.18M | for (y = 0; y < 16; ++y, dst += BPS) { |
1050 | 2.05M | const int val = dst[-1] - top[-1]; |
1051 | 2.05M | const __m128i base = _mm_set1_epi16(val); |
1052 | 2.05M | const __m128i out_0 = _mm_add_epi16(base, top_base_0); |
1053 | 2.05M | const __m128i out_1 = _mm_add_epi16(base, top_base_1); |
1054 | 2.05M | const __m128i out = _mm_packus_epi16(out_0, out_1); |
1055 | 2.05M | _mm_storeu_si128((__m128i*)dst, out); |
1056 | 2.05M | } |
1057 | 128k | } |
1058 | 1.48M | } |
1059 | | |
1060 | 1.17M | static void TM4_SSE2(uint8_t* dst) { TrueMotion_SSE2(dst, 4); } |
1061 | 184k | static void TM8uv_SSE2(uint8_t* dst) { TrueMotion_SSE2(dst, 8); } |
1062 | 128k | static void TM16_SSE2(uint8_t* dst) { TrueMotion_SSE2(dst, 16); } |
1063 | | |
1064 | 90.0k | static void VE16_SSE2(uint8_t* dst) { |
1065 | 90.0k | const __m128i top = _mm_loadu_si128((const __m128i*)(dst - BPS)); |
1066 | 90.0k | int j; |
1067 | 1.53M | for (j = 0; j < 16; ++j) { |
1068 | 1.44M | _mm_storeu_si128((__m128i*)(dst + j * BPS), top); |
1069 | 1.44M | } |
1070 | 90.0k | } |
1071 | | |
1072 | 3.07k | static void HE16_SSE2(uint8_t* dst) { // horizontal |
1073 | 3.07k | int j; |
1074 | 52.3k | for (j = 16; j > 0; --j) { |
1075 | 49.2k | const __m128i values = _mm_set1_epi8((char)dst[-1]); |
1076 | 49.2k | _mm_storeu_si128((__m128i*)dst, values); |
1077 | 49.2k | dst += BPS; |
1078 | 49.2k | } |
1079 | 3.07k | } |
1080 | | |
1081 | 247k | static WEBP_INLINE void Put16_SSE2(uint8_t v, uint8_t* dst) { |
1082 | 247k | int j; |
1083 | 247k | const __m128i values = _mm_set1_epi8((char)v); |
1084 | 4.21M | for (j = 0; j < 16; ++j) { |
1085 | 3.96M | _mm_storeu_si128((__m128i*)(dst + j * BPS), values); |
1086 | 3.96M | } |
1087 | 247k | } |
1088 | | |
1089 | 120k | static void DC16_SSE2(uint8_t* dst) { // DC |
1090 | 120k | const __m128i zero = _mm_setzero_si128(); |
1091 | 120k | const __m128i top = _mm_loadu_si128((const __m128i*)(dst - BPS)); |
1092 | 120k | const __m128i sad8x2 = _mm_sad_epu8(top, zero); |
1093 | | // sum the two sads: sad8x2[0:1] + sad8x2[8:9] |
1094 | 120k | const __m128i sum = _mm_add_epi16(sad8x2, _mm_shuffle_epi32(sad8x2, 2)); |
1095 | 120k | int left = 0; |
1096 | 120k | int j; |
1097 | 2.05M | for (j = 0; j < 16; ++j) { |
1098 | 1.93M | left += dst[-1 + j * BPS]; |
1099 | 1.93M | } |
1100 | 120k | { |
1101 | 120k | const int DC = _mm_cvtsi128_si32(sum) + left + 16; |
1102 | 120k | Put16_SSE2(DC >> 5, dst); |
1103 | 120k | } |
1104 | 120k | } |
1105 | | |
1106 | 57.7k | static void DC16NoTop_SSE2(uint8_t* dst) { // DC with top samples unavailable |
1107 | 57.7k | int DC = 8; |
1108 | 57.7k | int j; |
1109 | 981k | for (j = 0; j < 16; ++j) { |
1110 | 923k | DC += dst[-1 + j * BPS]; |
1111 | 923k | } |
1112 | 57.7k | Put16_SSE2(DC >> 4, dst); |
1113 | 57.7k | } |
1114 | | |
1115 | 63.1k | static void DC16NoLeft_SSE2(uint8_t* dst) { // DC with left samples unavailable |
1116 | 63.1k | const __m128i zero = _mm_setzero_si128(); |
1117 | 63.1k | const __m128i top = _mm_loadu_si128((const __m128i*)(dst - BPS)); |
1118 | 63.1k | const __m128i sad8x2 = _mm_sad_epu8(top, zero); |
1119 | | // sum the two sads: sad8x2[0:1] + sad8x2[8:9] |
1120 | 63.1k | const __m128i sum = _mm_add_epi16(sad8x2, _mm_shuffle_epi32(sad8x2, 2)); |
1121 | 63.1k | const int DC = _mm_cvtsi128_si32(sum) + 8; |
1122 | 63.1k | Put16_SSE2(DC >> 4, dst); |
1123 | 63.1k | } |
1124 | | |
1125 | 5.91k | static void DC16NoTopLeft_SSE2(uint8_t* dst) { // DC with no top & left samples |
1126 | 5.91k | Put16_SSE2(0x80, dst); |
1127 | 5.91k | } |
1128 | | |
1129 | | //------------------------------------------------------------------------------ |
1130 | | // Chroma |
1131 | | |
1132 | 288k | static void VE8uv_SSE2(uint8_t* dst) { // vertical |
1133 | 288k | int j; |
1134 | 288k | const __m128i top = _mm_loadl_epi64((const __m128i*)(dst - BPS)); |
1135 | 2.59M | for (j = 0; j < 8; ++j) { |
1136 | 2.30M | _mm_storel_epi64((__m128i*)(dst + j * BPS), top); |
1137 | 2.30M | } |
1138 | 288k | } |
1139 | | |
1140 | | // helper for chroma-DC predictions |
1141 | 1.15M | static WEBP_INLINE void Put8x8uv_SSE2(uint8_t v, uint8_t* dst) { |
1142 | 1.15M | int j; |
1143 | 1.15M | const __m128i values = _mm_set1_epi8((char)v); |
1144 | 10.3M | for (j = 0; j < 8; ++j) { |
1145 | 9.20M | _mm_storel_epi64((__m128i*)(dst + j * BPS), values); |
1146 | 9.20M | } |
1147 | 1.15M | } |
1148 | | |
1149 | 617k | static void DC8uv_SSE2(uint8_t* dst) { // DC |
1150 | 617k | const __m128i zero = _mm_setzero_si128(); |
1151 | 617k | const __m128i top = _mm_loadl_epi64((const __m128i*)(dst - BPS)); |
1152 | 617k | const __m128i sum = _mm_sad_epu8(top, zero); |
1153 | 617k | int left = 0; |
1154 | 617k | int j; |
1155 | 5.56M | for (j = 0; j < 8; ++j) { |
1156 | 4.94M | left += dst[-1 + j * BPS]; |
1157 | 4.94M | } |
1158 | 617k | { |
1159 | 617k | const int DC = _mm_cvtsi128_si32(sum) + left + 8; |
1160 | 617k | Put8x8uv_SSE2(DC >> 4, dst); |
1161 | 617k | } |
1162 | 617k | } |
1163 | | |
1164 | 268k | static void DC8uvNoLeft_SSE2(uint8_t* dst) { // DC with no left samples |
1165 | 268k | const __m128i zero = _mm_setzero_si128(); |
1166 | 268k | const __m128i top = _mm_loadl_epi64((const __m128i*)(dst - BPS)); |
1167 | 268k | const __m128i sum = _mm_sad_epu8(top, zero); |
1168 | 268k | const int DC = _mm_cvtsi128_si32(sum) + 4; |
1169 | 268k | Put8x8uv_SSE2(DC >> 3, dst); |
1170 | 268k | } |
1171 | | |
1172 | 236k | static void DC8uvNoTop_SSE2(uint8_t* dst) { // DC with no top samples |
1173 | 236k | int dc0 = 4; |
1174 | 236k | int i; |
1175 | 2.13M | for (i = 0; i < 8; ++i) { |
1176 | 1.89M | dc0 += dst[-1 + i * BPS]; |
1177 | 1.89M | } |
1178 | 236k | Put8x8uv_SSE2(dc0 >> 3, dst); |
1179 | 236k | } |
1180 | | |
1181 | 27.6k | static void DC8uvNoTopLeft_SSE2(uint8_t* dst) { // DC with nothing |
1182 | 27.6k | Put8x8uv_SSE2(0x80, dst); |
1183 | 27.6k | } |
1184 | | |
1185 | | //------------------------------------------------------------------------------ |
1186 | | // Entry point |
1187 | | |
1188 | | extern void VP8DspInitSSE2(void); |
1189 | | |
1190 | 8.29k | WEBP_TSAN_IGNORE_FUNCTION void VP8DspInitSSE2(void) { |
1191 | 8.29k | VP8Transform = Transform_SSE2; |
1192 | | #if (USE_TRANSFORM_AC3 == 1) |
1193 | | VP8TransformAC3 = TransformAC3_SSE2; |
1194 | | #endif |
1195 | | |
1196 | 8.29k | VP8VFilter16 = VFilter16_SSE2; |
1197 | 8.29k | VP8HFilter16 = HFilter16_SSE2; |
1198 | 8.29k | VP8VFilter8 = VFilter8_SSE2; |
1199 | 8.29k | VP8HFilter8 = HFilter8_SSE2; |
1200 | 8.29k | VP8VFilter16i = VFilter16i_SSE2; |
1201 | 8.29k | VP8HFilter16i = HFilter16i_SSE2; |
1202 | 8.29k | VP8VFilter8i = VFilter8i_SSE2; |
1203 | 8.29k | VP8HFilter8i = HFilter8i_SSE2; |
1204 | | |
1205 | 8.29k | VP8SimpleVFilter16 = SimpleVFilter16_SSE2; |
1206 | 8.29k | VP8SimpleHFilter16 = SimpleHFilter16_SSE2; |
1207 | 8.29k | VP8SimpleVFilter16i = SimpleVFilter16i_SSE2; |
1208 | 8.29k | VP8SimpleHFilter16i = SimpleHFilter16i_SSE2; |
1209 | | |
1210 | 8.29k | VP8PredLuma4[1] = TM4_SSE2; |
1211 | 8.29k | VP8PredLuma4[2] = VE4_SSE2; |
1212 | 8.29k | VP8PredLuma4[4] = RD4_SSE2; |
1213 | 8.29k | VP8PredLuma4[5] = VR4_SSE2; |
1214 | 8.29k | VP8PredLuma4[6] = LD4_SSE2; |
1215 | 8.29k | VP8PredLuma4[7] = VL4_SSE2; |
1216 | | |
1217 | 8.29k | VP8PredLuma16[0] = DC16_SSE2; |
1218 | 8.29k | VP8PredLuma16[1] = TM16_SSE2; |
1219 | 8.29k | VP8PredLuma16[2] = VE16_SSE2; |
1220 | 8.29k | VP8PredLuma16[3] = HE16_SSE2; |
1221 | 8.29k | VP8PredLuma16[4] = DC16NoTop_SSE2; |
1222 | 8.29k | VP8PredLuma16[5] = DC16NoLeft_SSE2; |
1223 | 8.29k | VP8PredLuma16[6] = DC16NoTopLeft_SSE2; |
1224 | | |
1225 | 8.29k | VP8PredChroma8[0] = DC8uv_SSE2; |
1226 | 8.29k | VP8PredChroma8[1] = TM8uv_SSE2; |
1227 | 8.29k | VP8PredChroma8[2] = VE8uv_SSE2; |
1228 | 8.29k | VP8PredChroma8[4] = DC8uvNoTop_SSE2; |
1229 | 8.29k | VP8PredChroma8[5] = DC8uvNoLeft_SSE2; |
1230 | 8.29k | VP8PredChroma8[6] = DC8uvNoTopLeft_SSE2; |
1231 | 8.29k | } |
1232 | | |
1233 | | #else // !WEBP_USE_SSE2 |
1234 | | |
1235 | | WEBP_DSP_INIT_STUB(VP8DspInitSSE2) |
1236 | | |
1237 | | #endif // WEBP_USE_SSE2 |