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