/src/libwebp/src/dsp/enc_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 speed-critical encoding functions. |
11 | | // |
12 | | // Author: Christian Duvivier (cduvivier@google.com) |
13 | | |
14 | | #include "src/dsp/dsp.h" |
15 | | |
16 | | #if defined(WEBP_USE_SSE2) |
17 | | #include <assert.h> |
18 | | #include <emmintrin.h> |
19 | | #include <stdlib.h> // for abs() |
20 | | #include <string.h> |
21 | | |
22 | | #include "src/dsp/common_sse2.h" |
23 | | #include "src/dsp/cpu.h" |
24 | | #include "src/enc/cost_enc.h" |
25 | | #include "src/enc/vp8i_enc.h" |
26 | | #include "src/utils/utils.h" |
27 | | #include "src/webp/types.h" |
28 | | |
29 | | //------------------------------------------------------------------------------ |
30 | | // Transforms (Paragraph 14.4) |
31 | | |
32 | | // Does one inverse transform. |
33 | | static void ITransform_One_SSE2(const uint8_t* WEBP_RESTRICT ref, |
34 | | const int16_t* WEBP_RESTRICT in, |
35 | 252M | uint8_t* WEBP_RESTRICT dst) { |
36 | | // This implementation makes use of 16-bit fixed point versions of two |
37 | | // multiply constants: |
38 | | // K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^16 |
39 | | // K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^16 |
40 | | // |
41 | | // To be able to use signed 16-bit integers, we use the following trick to |
42 | | // have constants within range: |
43 | | // - Associated constants are obtained by subtracting the 16-bit fixed point |
44 | | // version of one: |
45 | | // k = K - (1 << 16) => K = k + (1 << 16) |
46 | | // K1 = 85267 => k1 = 20091 |
47 | | // K2 = 35468 => k2 = -30068 |
48 | | // - The multiplication of a variable by a constant become the sum of the |
49 | | // variable and the multiplication of that variable by the associated |
50 | | // constant: |
51 | | // (x * K) >> 16 = (x * (k + (1 << 16))) >> 16 = ((x * k ) >> 16) + x |
52 | 252M | const __m128i k1k2 = |
53 | 252M | _mm_set_epi16(-30068, -30068, -30068, -30068, 20091, 20091, 20091, 20091); |
54 | 252M | const __m128i k2k1 = |
55 | 252M | _mm_set_epi16(20091, 20091, 20091, 20091, -30068, -30068, -30068, -30068); |
56 | 252M | const __m128i zero = _mm_setzero_si128(); |
57 | 252M | const __m128i zero_four = _mm_set_epi16(0, 0, 0, 0, 4, 4, 4, 4); |
58 | 252M | __m128i T01, T23; |
59 | | |
60 | | // Load and concatenate the transform coefficients. |
61 | 252M | const __m128i in01 = _mm_loadu_si128((const __m128i*)&in[0]); |
62 | 252M | const __m128i in23 = _mm_loadu_si128((const __m128i*)&in[8]); |
63 | | // a00 a10 a20 a30 a01 a11 a21 a31 |
64 | | // a02 a12 a22 a32 a03 a13 a23 a33 |
65 | | |
66 | | // Vertical pass and subsequent transpose. |
67 | 252M | { |
68 | 252M | const __m128i in1 = _mm_unpackhi_epi64(in01, in01); |
69 | 252M | const __m128i in3 = _mm_unpackhi_epi64(in23, in23); |
70 | | |
71 | | // First pass, c and d calculations are longer because of the "trick" |
72 | | // multiplications. |
73 | | // c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in3 |
74 | | // d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in3 |
75 | 252M | const __m128i a_d3 = _mm_add_epi16(in01, in23); |
76 | 252M | const __m128i b_c3 = _mm_sub_epi16(in01, in23); |
77 | 252M | const __m128i c1d1 = _mm_mulhi_epi16(in1, k2k1); |
78 | 252M | const __m128i c2d2 = _mm_mulhi_epi16(in3, k1k2); |
79 | 252M | const __m128i c3 = _mm_unpackhi_epi64(b_c3, b_c3); |
80 | 252M | const __m128i c4 = _mm_sub_epi16(c1d1, c2d2); |
81 | 252M | const __m128i c = _mm_add_epi16(c3, c4); |
82 | 252M | const __m128i d4u = _mm_add_epi16(c1d1, c2d2); |
83 | 252M | const __m128i du = _mm_add_epi16(a_d3, d4u); |
84 | 252M | const __m128i d = _mm_unpackhi_epi64(du, du); |
85 | | |
86 | | // Second pass. |
87 | 252M | const __m128i comb_ab = _mm_unpacklo_epi64(a_d3, b_c3); |
88 | 252M | const __m128i comb_dc = _mm_unpacklo_epi64(d, c); |
89 | | |
90 | 252M | const __m128i tmp01 = _mm_add_epi16(comb_ab, comb_dc); |
91 | 252M | const __m128i tmp32 = _mm_sub_epi16(comb_ab, comb_dc); |
92 | 252M | const __m128i tmp23 = _mm_shuffle_epi32(tmp32, _MM_SHUFFLE(1, 0, 3, 2)); |
93 | | |
94 | 252M | const __m128i transpose_0 = _mm_unpacklo_epi16(tmp01, tmp23); |
95 | 252M | const __m128i transpose_1 = _mm_unpackhi_epi16(tmp01, tmp23); |
96 | | // a00 a20 a01 a21 a02 a22 a03 a23 |
97 | | // a10 a30 a11 a31 a12 a32 a13 a33 |
98 | | |
99 | 252M | T01 = _mm_unpacklo_epi16(transpose_0, transpose_1); |
100 | 252M | T23 = _mm_unpackhi_epi16(transpose_0, transpose_1); |
101 | | // a00 a10 a20 a30 a01 a11 a21 a31 |
102 | | // a02 a12 a22 a32 a03 a13 a23 a33 |
103 | 252M | } |
104 | | |
105 | | // Horizontal pass and subsequent transpose. |
106 | 252M | { |
107 | 252M | const __m128i T1 = _mm_unpackhi_epi64(T01, T01); |
108 | 252M | const __m128i T3 = _mm_unpackhi_epi64(T23, T23); |
109 | | |
110 | | // First pass, c and d calculations are longer because of the "trick" |
111 | | // multiplications. |
112 | 252M | const __m128i dc = _mm_add_epi16(T01, zero_four); |
113 | | |
114 | | // c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3 |
115 | | // d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3 |
116 | 252M | const __m128i a_d3 = _mm_add_epi16(dc, T23); |
117 | 252M | const __m128i b_c3 = _mm_sub_epi16(dc, T23); |
118 | 252M | const __m128i c1d1 = _mm_mulhi_epi16(T1, k2k1); |
119 | 252M | const __m128i c2d2 = _mm_mulhi_epi16(T3, k1k2); |
120 | 252M | const __m128i c3 = _mm_unpackhi_epi64(b_c3, b_c3); |
121 | 252M | const __m128i c4 = _mm_sub_epi16(c1d1, c2d2); |
122 | 252M | const __m128i c = _mm_add_epi16(c3, c4); |
123 | 252M | const __m128i d4u = _mm_add_epi16(c1d1, c2d2); |
124 | 252M | const __m128i du = _mm_add_epi16(a_d3, d4u); |
125 | 252M | const __m128i d = _mm_unpackhi_epi64(du, du); |
126 | | |
127 | | // Second pass. |
128 | 252M | const __m128i comb_ab = _mm_unpacklo_epi64(a_d3, b_c3); |
129 | 252M | const __m128i comb_dc = _mm_unpacklo_epi64(d, c); |
130 | | |
131 | 252M | const __m128i tmp01 = _mm_add_epi16(comb_ab, comb_dc); |
132 | 252M | const __m128i tmp32 = _mm_sub_epi16(comb_ab, comb_dc); |
133 | 252M | const __m128i tmp23 = _mm_shuffle_epi32(tmp32, _MM_SHUFFLE(1, 0, 3, 2)); |
134 | | |
135 | 252M | const __m128i shifted01 = _mm_srai_epi16(tmp01, 3); |
136 | 252M | const __m128i shifted23 = _mm_srai_epi16(tmp23, 3); |
137 | | // a00 a01 a02 a03 a10 a11 a12 a13 |
138 | | // a20 a21 a22 a23 a30 a31 a32 a33 |
139 | | |
140 | 252M | const __m128i transpose_0 = _mm_unpacklo_epi16(shifted01, shifted23); |
141 | 252M | const __m128i transpose_1 = _mm_unpackhi_epi16(shifted01, shifted23); |
142 | | // a00 a20 a01 a21 a02 a22 a03 a23 |
143 | | // a10 a30 a11 a31 a12 a32 a13 a33 |
144 | | |
145 | 252M | T01 = _mm_unpacklo_epi16(transpose_0, transpose_1); |
146 | 252M | T23 = _mm_unpackhi_epi16(transpose_0, transpose_1); |
147 | | // a00 a10 a20 a30 a01 a11 a21 a31 |
148 | | // a02 a12 a22 a32 a03 a13 a23 a33 |
149 | 252M | } |
150 | | |
151 | | // Add inverse transform to 'ref' and store. |
152 | 252M | { |
153 | | // Load the reference(s). |
154 | 252M | __m128i ref01, ref23, ref0123; |
155 | 252M | int32_t buf[4]; |
156 | | |
157 | | // Load four bytes/pixels per line. |
158 | 252M | const __m128i ref0 = _mm_cvtsi32_si128(WebPMemToInt32(&ref[0 * BPS])); |
159 | 252M | const __m128i ref1 = _mm_cvtsi32_si128(WebPMemToInt32(&ref[1 * BPS])); |
160 | 252M | const __m128i ref2 = _mm_cvtsi32_si128(WebPMemToInt32(&ref[2 * BPS])); |
161 | 252M | const __m128i ref3 = _mm_cvtsi32_si128(WebPMemToInt32(&ref[3 * BPS])); |
162 | 252M | ref01 = _mm_unpacklo_epi32(ref0, ref1); |
163 | 252M | ref23 = _mm_unpacklo_epi32(ref2, ref3); |
164 | | |
165 | | // Convert to 16b. |
166 | 252M | ref01 = _mm_unpacklo_epi8(ref01, zero); |
167 | 252M | ref23 = _mm_unpacklo_epi8(ref23, zero); |
168 | | // Add the inverse transform(s). |
169 | 252M | ref01 = _mm_add_epi16(ref01, T01); |
170 | 252M | ref23 = _mm_add_epi16(ref23, T23); |
171 | | // Unsigned saturate to 8b. |
172 | 252M | ref0123 = _mm_packus_epi16(ref01, ref23); |
173 | | |
174 | 252M | _mm_storeu_si128((__m128i*)buf, ref0123); |
175 | | |
176 | | // Store four bytes/pixels per line. |
177 | 252M | WebPInt32ToMem(&dst[0 * BPS], buf[0]); |
178 | 252M | WebPInt32ToMem(&dst[1 * BPS], buf[1]); |
179 | 252M | WebPInt32ToMem(&dst[2 * BPS], buf[2]); |
180 | 252M | WebPInt32ToMem(&dst[3 * BPS], buf[3]); |
181 | 252M | } |
182 | 252M | } |
183 | | |
184 | | // Does two inverse transforms. |
185 | | static void ITransform_Two_SSE2(const uint8_t* WEBP_RESTRICT ref, |
186 | | const int16_t* WEBP_RESTRICT in, |
187 | 157M | uint8_t* WEBP_RESTRICT dst) { |
188 | | // This implementation makes use of 16-bit fixed point versions of two |
189 | | // multiply constants: |
190 | | // K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^16 |
191 | | // K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^16 |
192 | | // |
193 | | // To be able to use signed 16-bit integers, we use the following trick to |
194 | | // have constants within range: |
195 | | // - Associated constants are obtained by subtracting the 16-bit fixed point |
196 | | // version of one: |
197 | | // k = K - (1 << 16) => K = k + (1 << 16) |
198 | | // K1 = 85267 => k1 = 20091 |
199 | | // K2 = 35468 => k2 = -30068 |
200 | | // - The multiplication of a variable by a constant become the sum of the |
201 | | // variable and the multiplication of that variable by the associated |
202 | | // constant: |
203 | | // (x * K) >> 16 = (x * (k + (1 << 16))) >> 16 = ((x * k ) >> 16) + x |
204 | 157M | const __m128i k1 = _mm_set1_epi16(20091); |
205 | 157M | const __m128i k2 = _mm_set1_epi16(-30068); |
206 | 157M | __m128i T0, T1, T2, T3; |
207 | | |
208 | | // Load and concatenate the transform coefficients (we'll do two inverse |
209 | | // transforms in parallel). |
210 | 157M | __m128i in0, in1, in2, in3; |
211 | 157M | { |
212 | 157M | const __m128i tmp0 = _mm_loadu_si128((const __m128i*)&in[0]); |
213 | 157M | const __m128i tmp1 = _mm_loadu_si128((const __m128i*)&in[8]); |
214 | 157M | const __m128i tmp2 = _mm_loadu_si128((const __m128i*)&in[16]); |
215 | 157M | const __m128i tmp3 = _mm_loadu_si128((const __m128i*)&in[24]); |
216 | 157M | in0 = _mm_unpacklo_epi64(tmp0, tmp2); |
217 | 157M | in1 = _mm_unpackhi_epi64(tmp0, tmp2); |
218 | 157M | in2 = _mm_unpacklo_epi64(tmp1, tmp3); |
219 | 157M | in3 = _mm_unpackhi_epi64(tmp1, tmp3); |
220 | | // a00 a10 a20 a30 b00 b10 b20 b30 |
221 | | // a01 a11 a21 a31 b01 b11 b21 b31 |
222 | | // a02 a12 a22 a32 b02 b12 b22 b32 |
223 | | // a03 a13 a23 a33 b03 b13 b23 b33 |
224 | 157M | } |
225 | | |
226 | | // Vertical pass and subsequent transpose. |
227 | 157M | { |
228 | | // First pass, c and d calculations are longer because of the "trick" |
229 | | // multiplications. |
230 | 157M | const __m128i a = _mm_add_epi16(in0, in2); |
231 | 157M | const __m128i b = _mm_sub_epi16(in0, in2); |
232 | | // c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in3 |
233 | 157M | const __m128i c1 = _mm_mulhi_epi16(in1, k2); |
234 | 157M | const __m128i c2 = _mm_mulhi_epi16(in3, k1); |
235 | 157M | const __m128i c3 = _mm_sub_epi16(in1, in3); |
236 | 157M | const __m128i c4 = _mm_sub_epi16(c1, c2); |
237 | 157M | const __m128i c = _mm_add_epi16(c3, c4); |
238 | | // d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in3 |
239 | 157M | const __m128i d1 = _mm_mulhi_epi16(in1, k1); |
240 | 157M | const __m128i d2 = _mm_mulhi_epi16(in3, k2); |
241 | 157M | const __m128i d3 = _mm_add_epi16(in1, in3); |
242 | 157M | const __m128i d4 = _mm_add_epi16(d1, d2); |
243 | 157M | const __m128i d = _mm_add_epi16(d3, d4); |
244 | | |
245 | | // Second pass. |
246 | 157M | const __m128i tmp0 = _mm_add_epi16(a, d); |
247 | 157M | const __m128i tmp1 = _mm_add_epi16(b, c); |
248 | 157M | const __m128i tmp2 = _mm_sub_epi16(b, c); |
249 | 157M | const __m128i tmp3 = _mm_sub_epi16(a, d); |
250 | | |
251 | | // Transpose the two 4x4. |
252 | 157M | VP8Transpose_2_4x4_16b(&tmp0, &tmp1, &tmp2, &tmp3, &T0, &T1, &T2, &T3); |
253 | 157M | } |
254 | | |
255 | | // Horizontal pass and subsequent transpose. |
256 | 157M | { |
257 | | // First pass, c and d calculations are longer because of the "trick" |
258 | | // multiplications. |
259 | 157M | const __m128i four = _mm_set1_epi16(4); |
260 | 157M | const __m128i dc = _mm_add_epi16(T0, four); |
261 | 157M | const __m128i a = _mm_add_epi16(dc, T2); |
262 | 157M | const __m128i b = _mm_sub_epi16(dc, T2); |
263 | | // c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3 |
264 | 157M | const __m128i c1 = _mm_mulhi_epi16(T1, k2); |
265 | 157M | const __m128i c2 = _mm_mulhi_epi16(T3, k1); |
266 | 157M | const __m128i c3 = _mm_sub_epi16(T1, T3); |
267 | 157M | const __m128i c4 = _mm_sub_epi16(c1, c2); |
268 | 157M | const __m128i c = _mm_add_epi16(c3, c4); |
269 | | // d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3 |
270 | 157M | const __m128i d1 = _mm_mulhi_epi16(T1, k1); |
271 | 157M | const __m128i d2 = _mm_mulhi_epi16(T3, k2); |
272 | 157M | const __m128i d3 = _mm_add_epi16(T1, T3); |
273 | 157M | const __m128i d4 = _mm_add_epi16(d1, d2); |
274 | 157M | const __m128i d = _mm_add_epi16(d3, d4); |
275 | | |
276 | | // Second pass. |
277 | 157M | const __m128i tmp0 = _mm_add_epi16(a, d); |
278 | 157M | const __m128i tmp1 = _mm_add_epi16(b, c); |
279 | 157M | const __m128i tmp2 = _mm_sub_epi16(b, c); |
280 | 157M | const __m128i tmp3 = _mm_sub_epi16(a, d); |
281 | 157M | const __m128i shifted0 = _mm_srai_epi16(tmp0, 3); |
282 | 157M | const __m128i shifted1 = _mm_srai_epi16(tmp1, 3); |
283 | 157M | const __m128i shifted2 = _mm_srai_epi16(tmp2, 3); |
284 | 157M | const __m128i shifted3 = _mm_srai_epi16(tmp3, 3); |
285 | | |
286 | | // Transpose the two 4x4. |
287 | 157M | VP8Transpose_2_4x4_16b(&shifted0, &shifted1, &shifted2, &shifted3, &T0, &T1, |
288 | 157M | &T2, &T3); |
289 | 157M | } |
290 | | |
291 | | // Add inverse transform to 'ref' and store. |
292 | 157M | { |
293 | 157M | const __m128i zero = _mm_setzero_si128(); |
294 | | // Load the reference(s). |
295 | 157M | __m128i ref0, ref1, ref2, ref3; |
296 | | // Load eight bytes/pixels per line. |
297 | 157M | ref0 = _mm_loadl_epi64((const __m128i*)&ref[0 * BPS]); |
298 | 157M | ref1 = _mm_loadl_epi64((const __m128i*)&ref[1 * BPS]); |
299 | 157M | ref2 = _mm_loadl_epi64((const __m128i*)&ref[2 * BPS]); |
300 | 157M | ref3 = _mm_loadl_epi64((const __m128i*)&ref[3 * BPS]); |
301 | | // Convert to 16b. |
302 | 157M | ref0 = _mm_unpacklo_epi8(ref0, zero); |
303 | 157M | ref1 = _mm_unpacklo_epi8(ref1, zero); |
304 | 157M | ref2 = _mm_unpacklo_epi8(ref2, zero); |
305 | 157M | ref3 = _mm_unpacklo_epi8(ref3, zero); |
306 | | // Add the inverse transform(s). |
307 | 157M | ref0 = _mm_add_epi16(ref0, T0); |
308 | 157M | ref1 = _mm_add_epi16(ref1, T1); |
309 | 157M | ref2 = _mm_add_epi16(ref2, T2); |
310 | 157M | ref3 = _mm_add_epi16(ref3, T3); |
311 | | // Unsigned saturate to 8b. |
312 | 157M | ref0 = _mm_packus_epi16(ref0, ref0); |
313 | 157M | ref1 = _mm_packus_epi16(ref1, ref1); |
314 | 157M | ref2 = _mm_packus_epi16(ref2, ref2); |
315 | 157M | ref3 = _mm_packus_epi16(ref3, ref3); |
316 | | // Store eight bytes/pixels per line. |
317 | 157M | _mm_storel_epi64((__m128i*)&dst[0 * BPS], ref0); |
318 | 157M | _mm_storel_epi64((__m128i*)&dst[1 * BPS], ref1); |
319 | 157M | _mm_storel_epi64((__m128i*)&dst[2 * BPS], ref2); |
320 | 157M | _mm_storel_epi64((__m128i*)&dst[3 * BPS], ref3); |
321 | 157M | } |
322 | 157M | } |
323 | | |
324 | | // Does one or two inverse transforms. |
325 | | static void ITransform_SSE2(const uint8_t* WEBP_RESTRICT ref, |
326 | | const int16_t* WEBP_RESTRICT in, |
327 | 409M | uint8_t* WEBP_RESTRICT dst, int do_two) { |
328 | 409M | if (do_two) { |
329 | 157M | ITransform_Two_SSE2(ref, in, dst); |
330 | 252M | } else { |
331 | 252M | ITransform_One_SSE2(ref, in, dst); |
332 | 252M | } |
333 | 409M | } |
334 | | |
335 | | static void FTransformPass1_SSE2(const __m128i* const in01, |
336 | | const __m128i* const in23, |
337 | 724M | __m128i* const out01, __m128i* const out32) { |
338 | 724M | const __m128i k937 = _mm_set1_epi32(937); |
339 | 724M | const __m128i k1812 = _mm_set1_epi32(1812); |
340 | | |
341 | 724M | const __m128i k88p = _mm_set_epi16(8, 8, 8, 8, 8, 8, 8, 8); |
342 | 724M | const __m128i k88m = _mm_set_epi16(-8, 8, -8, 8, -8, 8, -8, 8); |
343 | 724M | const __m128i k5352_2217p = |
344 | 724M | _mm_set_epi16(2217, 5352, 2217, 5352, 2217, 5352, 2217, 5352); |
345 | 724M | const __m128i k5352_2217m = |
346 | 724M | _mm_set_epi16(-5352, 2217, -5352, 2217, -5352, 2217, -5352, 2217); |
347 | | |
348 | | // *in01 = 00 01 10 11 02 03 12 13 |
349 | | // *in23 = 20 21 30 31 22 23 32 33 |
350 | 724M | const __m128i shuf01_p = _mm_shufflehi_epi16(*in01, _MM_SHUFFLE(2, 3, 0, 1)); |
351 | 724M | const __m128i shuf23_p = _mm_shufflehi_epi16(*in23, _MM_SHUFFLE(2, 3, 0, 1)); |
352 | | // 00 01 10 11 03 02 13 12 |
353 | | // 20 21 30 31 23 22 33 32 |
354 | 724M | const __m128i s01 = _mm_unpacklo_epi64(shuf01_p, shuf23_p); |
355 | 724M | const __m128i s32 = _mm_unpackhi_epi64(shuf01_p, shuf23_p); |
356 | | // 00 01 10 11 20 21 30 31 |
357 | | // 03 02 13 12 23 22 33 32 |
358 | 724M | const __m128i a01 = _mm_add_epi16(s01, s32); |
359 | 724M | const __m128i a32 = _mm_sub_epi16(s01, s32); |
360 | | // [d0 + d3 | d1 + d2 | ...] = [a0 a1 | a0' a1' | ... ] |
361 | | // [d0 - d3 | d1 - d2 | ...] = [a3 a2 | a3' a2' | ... ] |
362 | | |
363 | 724M | const __m128i tmp0 = _mm_madd_epi16(a01, k88p); // [ (a0 + a1) << 3, ... ] |
364 | 724M | const __m128i tmp2 = _mm_madd_epi16(a01, k88m); // [ (a0 - a1) << 3, ... ] |
365 | 724M | const __m128i tmp1_1 = _mm_madd_epi16(a32, k5352_2217p); |
366 | 724M | const __m128i tmp3_1 = _mm_madd_epi16(a32, k5352_2217m); |
367 | 724M | const __m128i tmp1_2 = _mm_add_epi32(tmp1_1, k1812); |
368 | 724M | const __m128i tmp3_2 = _mm_add_epi32(tmp3_1, k937); |
369 | 724M | const __m128i tmp1 = _mm_srai_epi32(tmp1_2, 9); |
370 | 724M | const __m128i tmp3 = _mm_srai_epi32(tmp3_2, 9); |
371 | 724M | const __m128i s03 = _mm_packs_epi32(tmp0, tmp2); |
372 | 724M | const __m128i s12 = _mm_packs_epi32(tmp1, tmp3); |
373 | 724M | const __m128i s_lo = _mm_unpacklo_epi16(s03, s12); // 0 1 0 1 0 1... |
374 | 724M | const __m128i s_hi = _mm_unpackhi_epi16(s03, s12); // 2 3 2 3 2 3 |
375 | 724M | const __m128i v23 = _mm_unpackhi_epi32(s_lo, s_hi); |
376 | 724M | *out01 = _mm_unpacklo_epi32(s_lo, s_hi); |
377 | 724M | *out32 = _mm_shuffle_epi32(v23, _MM_SHUFFLE(1, 0, 3, 2)); // 3 2 3 2 3 2.. |
378 | 724M | } |
379 | | |
380 | | static void FTransformPass2_SSE2(const __m128i* const v01, |
381 | | const __m128i* const v32, |
382 | 724M | int16_t* WEBP_RESTRICT out) { |
383 | 724M | const __m128i zero = _mm_setzero_si128(); |
384 | 724M | const __m128i seven = _mm_set1_epi16(7); |
385 | 724M | const __m128i k5352_2217 = |
386 | 724M | _mm_set_epi16(5352, 2217, 5352, 2217, 5352, 2217, 5352, 2217); |
387 | 724M | const __m128i k2217_5352 = |
388 | 724M | _mm_set_epi16(2217, -5352, 2217, -5352, 2217, -5352, 2217, -5352); |
389 | 724M | const __m128i k12000_plus_one = _mm_set1_epi32(12000 + (1 << 16)); |
390 | 724M | const __m128i k51000 = _mm_set1_epi32(51000); |
391 | | |
392 | | // Same operations are done on the (0,3) and (1,2) pairs. |
393 | | // a3 = v0 - v3 |
394 | | // a2 = v1 - v2 |
395 | 724M | const __m128i a32 = _mm_sub_epi16(*v01, *v32); |
396 | 724M | const __m128i a22 = _mm_unpackhi_epi64(a32, a32); |
397 | | |
398 | 724M | const __m128i b23 = _mm_unpacklo_epi16(a22, a32); |
399 | 724M | const __m128i c1 = _mm_madd_epi16(b23, k5352_2217); |
400 | 724M | const __m128i c3 = _mm_madd_epi16(b23, k2217_5352); |
401 | 724M | const __m128i d1 = _mm_add_epi32(c1, k12000_plus_one); |
402 | 724M | const __m128i d3 = _mm_add_epi32(c3, k51000); |
403 | 724M | const __m128i e1 = _mm_srai_epi32(d1, 16); |
404 | 724M | const __m128i e3 = _mm_srai_epi32(d3, 16); |
405 | | // f1 = ((b3 * 5352 + b2 * 2217 + 12000) >> 16) |
406 | | // f3 = ((b3 * 2217 - b2 * 5352 + 51000) >> 16) |
407 | 724M | const __m128i f1 = _mm_packs_epi32(e1, e1); |
408 | 724M | const __m128i f3 = _mm_packs_epi32(e3, e3); |
409 | | // g1 = f1 + (a3 != 0); |
410 | | // The compare will return (0xffff, 0) for (==0, !=0). To turn that into the |
411 | | // desired (0, 1), we add one earlier through k12000_plus_one. |
412 | | // -> g1 = f1 + 1 - (a3 == 0) |
413 | 724M | const __m128i g1 = _mm_add_epi16(f1, _mm_cmpeq_epi16(a32, zero)); |
414 | | |
415 | | // a0 = v0 + v3 |
416 | | // a1 = v1 + v2 |
417 | 724M | const __m128i a01 = _mm_add_epi16(*v01, *v32); |
418 | 724M | const __m128i a01_plus_7 = _mm_add_epi16(a01, seven); |
419 | 724M | const __m128i a11 = _mm_unpackhi_epi64(a01, a01); |
420 | 724M | const __m128i c0 = _mm_add_epi16(a01_plus_7, a11); |
421 | 724M | const __m128i c2 = _mm_sub_epi16(a01_plus_7, a11); |
422 | | // d0 = (a0 + a1 + 7) >> 4; |
423 | | // d2 = (a0 - a1 + 7) >> 4; |
424 | 724M | const __m128i d0 = _mm_srai_epi16(c0, 4); |
425 | 724M | const __m128i d2 = _mm_srai_epi16(c2, 4); |
426 | | |
427 | 724M | const __m128i d0_g1 = _mm_unpacklo_epi64(d0, g1); |
428 | 724M | const __m128i d2_f3 = _mm_unpacklo_epi64(d2, f3); |
429 | 724M | _mm_storeu_si128((__m128i*)&out[0], d0_g1); |
430 | 724M | _mm_storeu_si128((__m128i*)&out[8], d2_f3); |
431 | 724M | } |
432 | | |
433 | | static void FTransform_SSE2(const uint8_t* WEBP_RESTRICT src, |
434 | | const uint8_t* WEBP_RESTRICT ref, |
435 | 409M | int16_t* WEBP_RESTRICT out) { |
436 | 409M | const __m128i zero = _mm_setzero_si128(); |
437 | | // Load src. |
438 | 409M | const __m128i src0 = _mm_loadl_epi64((const __m128i*)&src[0 * BPS]); |
439 | 409M | const __m128i src1 = _mm_loadl_epi64((const __m128i*)&src[1 * BPS]); |
440 | 409M | const __m128i src2 = _mm_loadl_epi64((const __m128i*)&src[2 * BPS]); |
441 | 409M | const __m128i src3 = _mm_loadl_epi64((const __m128i*)&src[3 * BPS]); |
442 | | // 00 01 02 03 * |
443 | | // 10 11 12 13 * |
444 | | // 20 21 22 23 * |
445 | | // 30 31 32 33 * |
446 | | // Shuffle. |
447 | 409M | const __m128i src_0 = _mm_unpacklo_epi16(src0, src1); |
448 | 409M | const __m128i src_1 = _mm_unpacklo_epi16(src2, src3); |
449 | | // 00 01 10 11 02 03 12 13 * * ... |
450 | | // 20 21 30 31 22 22 32 33 * * ... |
451 | | |
452 | | // Load ref. |
453 | 409M | const __m128i ref0 = _mm_loadl_epi64((const __m128i*)&ref[0 * BPS]); |
454 | 409M | const __m128i ref1 = _mm_loadl_epi64((const __m128i*)&ref[1 * BPS]); |
455 | 409M | const __m128i ref2 = _mm_loadl_epi64((const __m128i*)&ref[2 * BPS]); |
456 | 409M | const __m128i ref3 = _mm_loadl_epi64((const __m128i*)&ref[3 * BPS]); |
457 | 409M | const __m128i ref_0 = _mm_unpacklo_epi16(ref0, ref1); |
458 | 409M | const __m128i ref_1 = _mm_unpacklo_epi16(ref2, ref3); |
459 | | |
460 | | // Convert both to 16 bit. |
461 | 409M | const __m128i src_0_16b = _mm_unpacklo_epi8(src_0, zero); |
462 | 409M | const __m128i src_1_16b = _mm_unpacklo_epi8(src_1, zero); |
463 | 409M | const __m128i ref_0_16b = _mm_unpacklo_epi8(ref_0, zero); |
464 | 409M | const __m128i ref_1_16b = _mm_unpacklo_epi8(ref_1, zero); |
465 | | |
466 | | // Compute the difference. |
467 | 409M | const __m128i row01 = _mm_sub_epi16(src_0_16b, ref_0_16b); |
468 | 409M | const __m128i row23 = _mm_sub_epi16(src_1_16b, ref_1_16b); |
469 | 409M | __m128i v01, v32; |
470 | | |
471 | | // First pass |
472 | 409M | FTransformPass1_SSE2(&row01, &row23, &v01, &v32); |
473 | | |
474 | | // Second pass |
475 | 409M | FTransformPass2_SSE2(&v01, &v32, out); |
476 | 409M | } |
477 | | |
478 | | static void FTransform2_SSE2(const uint8_t* WEBP_RESTRICT src, |
479 | | const uint8_t* WEBP_RESTRICT ref, |
480 | 157M | int16_t* WEBP_RESTRICT out) { |
481 | 157M | const __m128i zero = _mm_setzero_si128(); |
482 | | |
483 | | // Load src and convert to 16b. |
484 | 157M | const __m128i src0 = _mm_loadl_epi64((const __m128i*)&src[0 * BPS]); |
485 | 157M | const __m128i src1 = _mm_loadl_epi64((const __m128i*)&src[1 * BPS]); |
486 | 157M | const __m128i src2 = _mm_loadl_epi64((const __m128i*)&src[2 * BPS]); |
487 | 157M | const __m128i src3 = _mm_loadl_epi64((const __m128i*)&src[3 * BPS]); |
488 | 157M | const __m128i src_0 = _mm_unpacklo_epi8(src0, zero); |
489 | 157M | const __m128i src_1 = _mm_unpacklo_epi8(src1, zero); |
490 | 157M | const __m128i src_2 = _mm_unpacklo_epi8(src2, zero); |
491 | 157M | const __m128i src_3 = _mm_unpacklo_epi8(src3, zero); |
492 | | // Load ref and convert to 16b. |
493 | 157M | const __m128i ref0 = _mm_loadl_epi64((const __m128i*)&ref[0 * BPS]); |
494 | 157M | const __m128i ref1 = _mm_loadl_epi64((const __m128i*)&ref[1 * BPS]); |
495 | 157M | const __m128i ref2 = _mm_loadl_epi64((const __m128i*)&ref[2 * BPS]); |
496 | 157M | const __m128i ref3 = _mm_loadl_epi64((const __m128i*)&ref[3 * BPS]); |
497 | 157M | const __m128i ref_0 = _mm_unpacklo_epi8(ref0, zero); |
498 | 157M | const __m128i ref_1 = _mm_unpacklo_epi8(ref1, zero); |
499 | 157M | const __m128i ref_2 = _mm_unpacklo_epi8(ref2, zero); |
500 | 157M | const __m128i ref_3 = _mm_unpacklo_epi8(ref3, zero); |
501 | | // Compute difference. -> 00 01 02 03 00' 01' 02' 03' |
502 | 157M | const __m128i diff0 = _mm_sub_epi16(src_0, ref_0); |
503 | 157M | const __m128i diff1 = _mm_sub_epi16(src_1, ref_1); |
504 | 157M | const __m128i diff2 = _mm_sub_epi16(src_2, ref_2); |
505 | 157M | const __m128i diff3 = _mm_sub_epi16(src_3, ref_3); |
506 | | |
507 | | // Unpack and shuffle |
508 | | // 00 01 02 03 0 0 0 0 |
509 | | // 10 11 12 13 0 0 0 0 |
510 | | // 20 21 22 23 0 0 0 0 |
511 | | // 30 31 32 33 0 0 0 0 |
512 | 157M | const __m128i shuf01l = _mm_unpacklo_epi32(diff0, diff1); |
513 | 157M | const __m128i shuf23l = _mm_unpacklo_epi32(diff2, diff3); |
514 | 157M | const __m128i shuf01h = _mm_unpackhi_epi32(diff0, diff1); |
515 | 157M | const __m128i shuf23h = _mm_unpackhi_epi32(diff2, diff3); |
516 | 157M | __m128i v01l, v32l; |
517 | 157M | __m128i v01h, v32h; |
518 | | |
519 | | // First pass |
520 | 157M | FTransformPass1_SSE2(&shuf01l, &shuf23l, &v01l, &v32l); |
521 | 157M | FTransformPass1_SSE2(&shuf01h, &shuf23h, &v01h, &v32h); |
522 | | |
523 | | // Second pass |
524 | 157M | FTransformPass2_SSE2(&v01l, &v32l, out + 0); |
525 | 157M | FTransformPass2_SSE2(&v01h, &v32h, out + 16); |
526 | 157M | } |
527 | | |
528 | | static void FTransformWHTRow_SSE2(const int16_t* WEBP_RESTRICT const in, |
529 | 52.4M | __m128i* const out) { |
530 | 52.4M | const __m128i kMult = _mm_set_epi16(-1, 1, -1, 1, 1, 1, 1, 1); |
531 | 52.4M | const __m128i src0 = _mm_loadl_epi64((__m128i*)&in[0 * 16]); |
532 | 52.4M | const __m128i src1 = _mm_loadl_epi64((__m128i*)&in[1 * 16]); |
533 | 52.4M | const __m128i src2 = _mm_loadl_epi64((__m128i*)&in[2 * 16]); |
534 | 52.4M | const __m128i src3 = _mm_loadl_epi64((__m128i*)&in[3 * 16]); |
535 | 52.4M | const __m128i A01 = _mm_unpacklo_epi16(src0, src1); // A0 A1 | ... |
536 | 52.4M | const __m128i A23 = _mm_unpacklo_epi16(src2, src3); // A2 A3 | ... |
537 | 52.4M | const __m128i B0 = _mm_adds_epi16(A01, A23); // a0 | a1 | ... |
538 | 52.4M | const __m128i B1 = _mm_subs_epi16(A01, A23); // a3 | a2 | ... |
539 | 52.4M | const __m128i C0 = _mm_unpacklo_epi32(B0, B1); // a0 | a1 | a3 | a2 | ... |
540 | 52.4M | const __m128i C1 = _mm_unpacklo_epi32(B1, B0); // a3 | a2 | a0 | a1 | ... |
541 | 52.4M | const __m128i D = _mm_unpacklo_epi64(C0, C1); // a0 a1 a3 a2 a3 a2 a0 a1 |
542 | 52.4M | *out = _mm_madd_epi16(D, kMult); |
543 | 52.4M | } |
544 | | |
545 | | static void FTransformWHT_SSE2(const int16_t* WEBP_RESTRICT in, |
546 | 13.1M | int16_t* WEBP_RESTRICT out) { |
547 | | // Input is 12b signed. |
548 | 13.1M | __m128i row0, row1, row2, row3; |
549 | | // Rows are 14b signed. |
550 | 13.1M | FTransformWHTRow_SSE2(in + 0 * 64, &row0); |
551 | 13.1M | FTransformWHTRow_SSE2(in + 1 * 64, &row1); |
552 | 13.1M | FTransformWHTRow_SSE2(in + 2 * 64, &row2); |
553 | 13.1M | FTransformWHTRow_SSE2(in + 3 * 64, &row3); |
554 | | |
555 | 13.1M | { |
556 | | // The a* are 15b signed. |
557 | 13.1M | const __m128i a0 = _mm_add_epi32(row0, row2); |
558 | 13.1M | const __m128i a1 = _mm_add_epi32(row1, row3); |
559 | 13.1M | const __m128i a2 = _mm_sub_epi32(row1, row3); |
560 | 13.1M | const __m128i a3 = _mm_sub_epi32(row0, row2); |
561 | 13.1M | const __m128i a0a3 = _mm_packs_epi32(a0, a3); |
562 | 13.1M | const __m128i a1a2 = _mm_packs_epi32(a1, a2); |
563 | | |
564 | | // The b* are 16b signed. |
565 | 13.1M | const __m128i b0b1 = _mm_add_epi16(a0a3, a1a2); |
566 | 13.1M | const __m128i b3b2 = _mm_sub_epi16(a0a3, a1a2); |
567 | 13.1M | const __m128i tmp_b2b3 = _mm_unpackhi_epi64(b3b2, b3b2); |
568 | 13.1M | const __m128i b2b3 = _mm_unpacklo_epi64(tmp_b2b3, b3b2); |
569 | | |
570 | 13.1M | _mm_storeu_si128((__m128i*)&out[0], _mm_srai_epi16(b0b1, 1)); |
571 | 13.1M | _mm_storeu_si128((__m128i*)&out[8], _mm_srai_epi16(b2b3, 1)); |
572 | 13.1M | } |
573 | 13.1M | } |
574 | | |
575 | | //------------------------------------------------------------------------------ |
576 | | // Compute susceptibility based on DCT-coeff histograms: |
577 | | // the higher, the "easier" the macroblock is to compress. |
578 | | |
579 | | static void CollectHistogram_SSE2(const uint8_t* WEBP_RESTRICT ref, |
580 | | const uint8_t* WEBP_RESTRICT pred, |
581 | | int start_block, int end_block, |
582 | 0 | VP8Histogram* WEBP_RESTRICT const histo) { |
583 | 0 | const __m128i zero = _mm_setzero_si128(); |
584 | 0 | const __m128i max_coeff_thresh = _mm_set1_epi16(MAX_COEFF_THRESH); |
585 | 0 | int j; |
586 | 0 | int distribution[MAX_COEFF_THRESH + 1] = {0}; |
587 | 0 | for (j = start_block; j < end_block; ++j) { |
588 | 0 | int16_t out[16]; |
589 | 0 | int k; |
590 | |
|
591 | 0 | FTransform_SSE2(ref + VP8DspScan[j], pred + VP8DspScan[j], out); |
592 | | |
593 | | // Convert coefficients to bin (within out[]). |
594 | 0 | { |
595 | | // Load. |
596 | 0 | const __m128i out0 = _mm_loadu_si128((__m128i*)&out[0]); |
597 | 0 | const __m128i out1 = _mm_loadu_si128((__m128i*)&out[8]); |
598 | 0 | const __m128i d0 = _mm_sub_epi16(zero, out0); |
599 | 0 | const __m128i d1 = _mm_sub_epi16(zero, out1); |
600 | 0 | const __m128i abs0 = _mm_max_epi16(out0, d0); // abs(v), 16b |
601 | 0 | const __m128i abs1 = _mm_max_epi16(out1, d1); |
602 | | // v = abs(out) >> 3 |
603 | 0 | const __m128i v0 = _mm_srai_epi16(abs0, 3); |
604 | 0 | const __m128i v1 = _mm_srai_epi16(abs1, 3); |
605 | | // bin = min(v, MAX_COEFF_THRESH) |
606 | 0 | const __m128i bin0 = _mm_min_epi16(v0, max_coeff_thresh); |
607 | 0 | const __m128i bin1 = _mm_min_epi16(v1, max_coeff_thresh); |
608 | | // Store. |
609 | 0 | _mm_storeu_si128((__m128i*)&out[0], bin0); |
610 | 0 | _mm_storeu_si128((__m128i*)&out[8], bin1); |
611 | 0 | } |
612 | | |
613 | | // Convert coefficients to bin. |
614 | 0 | for (k = 0; k < 16; ++k) { |
615 | 0 | ++distribution[out[k]]; |
616 | 0 | } |
617 | 0 | } |
618 | 0 | VP8SetHistogramData(distribution, histo); |
619 | 0 | } |
620 | | |
621 | | //------------------------------------------------------------------------------ |
622 | | // Intra predictions |
623 | | |
624 | | // helper for chroma-DC predictions |
625 | 13.5M | static WEBP_INLINE void Put8x8uv_SSE2(uint8_t v, uint8_t* dst) { |
626 | 13.5M | int j; |
627 | 13.5M | const __m128i values = _mm_set1_epi8((char)v); |
628 | 122M | for (j = 0; j < 8; ++j) { |
629 | 108M | _mm_storel_epi64((__m128i*)(dst + j * BPS), values); |
630 | 108M | } |
631 | 13.5M | } |
632 | | |
633 | 6.79M | static WEBP_INLINE void Put16_SSE2(uint8_t v, uint8_t* dst) { |
634 | 6.79M | int j; |
635 | 6.79M | const __m128i values = _mm_set1_epi8((char)v); |
636 | 115M | for (j = 0; j < 16; ++j) { |
637 | 108M | _mm_store_si128((__m128i*)(dst + j * BPS), values); |
638 | 108M | } |
639 | 6.79M | } |
640 | | |
641 | 25.9M | static WEBP_INLINE void Fill_SSE2(uint8_t* dst, int value, int size) { |
642 | 25.9M | if (size == 4) { |
643 | 25.2M | int j; |
644 | 126M | for (j = 0; j < 4; ++j) { |
645 | 100M | memset(dst + j * BPS, value, 4); |
646 | 100M | } |
647 | 25.2M | } else if (size == 8) { |
648 | 473k | Put8x8uv_SSE2(value, dst); |
649 | 473k | } else { |
650 | 236k | Put16_SSE2(value, dst); |
651 | 236k | } |
652 | 25.9M | } |
653 | | |
654 | | static WEBP_INLINE void VE8uv_SSE2(uint8_t* WEBP_RESTRICT dst, |
655 | 13.1M | const uint8_t* WEBP_RESTRICT top) { |
656 | 13.1M | int j; |
657 | 13.1M | const __m128i top_values = _mm_loadl_epi64((const __m128i*)top); |
658 | 117M | for (j = 0; j < 8; ++j) { |
659 | 104M | _mm_storel_epi64((__m128i*)(dst + j * BPS), top_values); |
660 | 104M | } |
661 | 13.1M | } |
662 | | |
663 | | static WEBP_INLINE void VE16_SSE2(uint8_t* WEBP_RESTRICT dst, |
664 | 6.55M | const uint8_t* WEBP_RESTRICT top) { |
665 | 6.55M | const __m128i top_values = _mm_load_si128((const __m128i*)top); |
666 | 6.55M | int j; |
667 | 111M | for (j = 0; j < 16; ++j) { |
668 | 104M | _mm_store_si128((__m128i*)(dst + j * BPS), top_values); |
669 | 104M | } |
670 | 6.55M | } |
671 | | |
672 | | static WEBP_INLINE void VerticalPred_SSE2(uint8_t* WEBP_RESTRICT dst, |
673 | | const uint8_t* WEBP_RESTRICT top, |
674 | 20.0M | int size) { |
675 | 20.0M | if (top != NULL) { |
676 | 19.6M | if (size == 8) { |
677 | 13.1M | VE8uv_SSE2(dst, top); |
678 | 13.1M | } else { |
679 | 6.55M | VE16_SSE2(dst, top); |
680 | 6.55M | } |
681 | 19.6M | } else { |
682 | 345k | Fill_SSE2(dst, 127, size); |
683 | 345k | } |
684 | 20.0M | } |
685 | | |
686 | | static WEBP_INLINE void HE8uv_SSE2(uint8_t* WEBP_RESTRICT dst, |
687 | 13.0M | const uint8_t* WEBP_RESTRICT left) { |
688 | 13.0M | int j; |
689 | 117M | for (j = 0; j < 8; ++j) { |
690 | 104M | const __m128i values = _mm_set1_epi8((char)left[j]); |
691 | 104M | _mm_storel_epi64((__m128i*)dst, values); |
692 | 104M | dst += BPS; |
693 | 104M | } |
694 | 13.0M | } |
695 | | |
696 | | static WEBP_INLINE void HE16_SSE2(uint8_t* WEBP_RESTRICT dst, |
697 | 6.54M | const uint8_t* WEBP_RESTRICT left) { |
698 | 6.54M | int j; |
699 | 111M | for (j = 0; j < 16; ++j) { |
700 | 104M | const __m128i values = _mm_set1_epi8((char)left[j]); |
701 | 104M | _mm_store_si128((__m128i*)dst, values); |
702 | 104M | dst += BPS; |
703 | 104M | } |
704 | 6.54M | } |
705 | | |
706 | | static WEBP_INLINE void HorizontalPred_SSE2(uint8_t* WEBP_RESTRICT dst, |
707 | | const uint8_t* WEBP_RESTRICT left, |
708 | 19.9M | int size) { |
709 | 19.9M | if (left != NULL) { |
710 | 19.6M | if (size == 8) { |
711 | 13.0M | HE8uv_SSE2(dst, left); |
712 | 13.0M | } else { |
713 | 6.54M | HE16_SSE2(dst, left); |
714 | 6.54M | } |
715 | 19.6M | } else { |
716 | 354k | Fill_SSE2(dst, 129, size); |
717 | 354k | } |
718 | 19.9M | } |
719 | | |
720 | | static WEBP_INLINE void TM_SSE2(uint8_t* WEBP_RESTRICT dst, |
721 | | const uint8_t* WEBP_RESTRICT left, |
722 | 18.9M | const uint8_t* WEBP_RESTRICT top, int size) { |
723 | 18.9M | const __m128i zero = _mm_setzero_si128(); |
724 | 18.9M | int y; |
725 | 18.9M | if (size == 8) { |
726 | 12.6M | const __m128i top_values = _mm_loadl_epi64((const __m128i*)top); |
727 | 12.6M | const __m128i top_base = _mm_unpacklo_epi8(top_values, zero); |
728 | 113M | for (y = 0; y < 8; ++y, dst += BPS) { |
729 | 101M | const int val = left[y] - left[-1]; |
730 | 101M | const __m128i base = _mm_set1_epi16(val); |
731 | 101M | const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero); |
732 | 101M | _mm_storel_epi64((__m128i*)dst, out); |
733 | 101M | } |
734 | 12.6M | } else { |
735 | 6.32M | const __m128i top_values = _mm_load_si128((const __m128i*)top); |
736 | 6.32M | const __m128i top_base_0 = _mm_unpacklo_epi8(top_values, zero); |
737 | 6.32M | const __m128i top_base_1 = _mm_unpackhi_epi8(top_values, zero); |
738 | 107M | for (y = 0; y < 16; ++y, dst += BPS) { |
739 | 101M | const int val = left[y] - left[-1]; |
740 | 101M | const __m128i base = _mm_set1_epi16(val); |
741 | 101M | const __m128i out_0 = _mm_add_epi16(base, top_base_0); |
742 | 101M | const __m128i out_1 = _mm_add_epi16(base, top_base_1); |
743 | 101M | const __m128i out = _mm_packus_epi16(out_0, out_1); |
744 | 101M | _mm_store_si128((__m128i*)dst, out); |
745 | 101M | } |
746 | 6.32M | } |
747 | 18.9M | } |
748 | | |
749 | | static WEBP_INLINE void TrueMotion_SSE2(uint8_t* WEBP_RESTRICT dst, |
750 | | const uint8_t* WEBP_RESTRICT left, |
751 | | const uint8_t* WEBP_RESTRICT top, |
752 | 19.6M | int size) { |
753 | 19.6M | if (left != NULL) { |
754 | 19.3M | if (top != NULL) { |
755 | 18.9M | TM_SSE2(dst, left, top, size); |
756 | 18.9M | } else { |
757 | 335k | HorizontalPred_SSE2(dst, left, size); |
758 | 335k | } |
759 | 19.3M | } else { |
760 | | // true motion without left samples (hence: with default 129 value) |
761 | | // is equivalent to VE prediction where you just copy the top samples. |
762 | | // Note that if top samples are not available, the default value is |
763 | | // then 129, and not 127 as in the VerticalPred case. |
764 | 354k | if (top != NULL) { |
765 | 344k | VerticalPred_SSE2(dst, top, size); |
766 | 344k | } else { |
767 | 10.0k | Fill_SSE2(dst, 129, size); |
768 | 10.0k | } |
769 | 354k | } |
770 | 19.6M | } |
771 | | |
772 | | static WEBP_INLINE void DC8uv_SSE2(uint8_t* WEBP_RESTRICT dst, |
773 | | const uint8_t* WEBP_RESTRICT left, |
774 | 12.6M | const uint8_t* WEBP_RESTRICT top) { |
775 | 12.6M | const __m128i top_values = _mm_loadl_epi64((const __m128i*)top); |
776 | 12.6M | const __m128i left_values = _mm_loadl_epi64((const __m128i*)left); |
777 | 12.6M | const __m128i combined = _mm_unpacklo_epi64(top_values, left_values); |
778 | 12.6M | const int DC = VP8HorizontalAdd8b(&combined) + 8; |
779 | 12.6M | Put8x8uv_SSE2(DC >> 4, dst); |
780 | 12.6M | } |
781 | | |
782 | | static WEBP_INLINE void DC8uvNoLeft_SSE2(uint8_t* WEBP_RESTRICT dst, |
783 | 453k | const uint8_t* WEBP_RESTRICT top) { |
784 | 453k | const __m128i zero = _mm_setzero_si128(); |
785 | 453k | const __m128i top_values = _mm_loadl_epi64((const __m128i*)top); |
786 | 453k | const __m128i sum = _mm_sad_epu8(top_values, zero); |
787 | 453k | const int DC = _mm_cvtsi128_si32(sum) + 4; |
788 | 453k | Put8x8uv_SSE2(DC >> 3, dst); |
789 | 453k | } |
790 | | |
791 | | static WEBP_INLINE void DC8uvNoTop_SSE2(uint8_t* WEBP_RESTRICT dst, |
792 | 223k | const uint8_t* WEBP_RESTRICT left) { |
793 | | // 'left' is contiguous so we can reuse the top summation. |
794 | 223k | DC8uvNoLeft_SSE2(dst, left); |
795 | 223k | } |
796 | | |
797 | 6.72k | static WEBP_INLINE void DC8uvNoTopLeft_SSE2(uint8_t* dst) { |
798 | 6.72k | Put8x8uv_SSE2(0x80, dst); |
799 | 6.72k | } |
800 | | |
801 | | static WEBP_INLINE void DC8uvMode_SSE2(uint8_t* WEBP_RESTRICT dst, |
802 | | const uint8_t* WEBP_RESTRICT left, |
803 | 13.1M | const uint8_t* WEBP_RESTRICT top) { |
804 | 13.1M | if (top != NULL) { |
805 | 12.8M | if (left != NULL) { // top and left present |
806 | 12.6M | DC8uv_SSE2(dst, left, top); |
807 | 12.6M | } else { // top, but no left |
808 | 229k | DC8uvNoLeft_SSE2(dst, top); |
809 | 229k | } |
810 | 12.8M | } else if (left != NULL) { // left but no top |
811 | 223k | DC8uvNoTop_SSE2(dst, left); |
812 | 223k | } else { // no top, no left, nothing. |
813 | 6.72k | DC8uvNoTopLeft_SSE2(dst); |
814 | 6.72k | } |
815 | 13.1M | } |
816 | | |
817 | | static WEBP_INLINE void DC16_SSE2(uint8_t* WEBP_RESTRICT dst, |
818 | | const uint8_t* WEBP_RESTRICT left, |
819 | 6.32M | const uint8_t* WEBP_RESTRICT top) { |
820 | 6.32M | const __m128i top_row = _mm_load_si128((const __m128i*)top); |
821 | 6.32M | const __m128i left_row = _mm_load_si128((const __m128i*)left); |
822 | 6.32M | const int DC = |
823 | 6.32M | VP8HorizontalAdd8b(&top_row) + VP8HorizontalAdd8b(&left_row) + 16; |
824 | 6.32M | Put16_SSE2(DC >> 5, dst); |
825 | 6.32M | } |
826 | | |
827 | | static WEBP_INLINE void DC16NoLeft_SSE2(uint8_t* WEBP_RESTRICT dst, |
828 | 226k | const uint8_t* WEBP_RESTRICT top) { |
829 | 226k | const __m128i top_row = _mm_load_si128((const __m128i*)top); |
830 | 226k | const int DC = VP8HorizontalAdd8b(&top_row) + 8; |
831 | 226k | Put16_SSE2(DC >> 4, dst); |
832 | 226k | } |
833 | | |
834 | | static WEBP_INLINE void DC16NoTop_SSE2(uint8_t* WEBP_RESTRICT dst, |
835 | 111k | const uint8_t* WEBP_RESTRICT left) { |
836 | | // 'left' is contiguous so we can reuse the top summation. |
837 | 111k | DC16NoLeft_SSE2(dst, left); |
838 | 111k | } |
839 | | |
840 | 3.36k | static WEBP_INLINE void DC16NoTopLeft_SSE2(uint8_t* dst) { |
841 | 3.36k | Put16_SSE2(0x80, dst); |
842 | 3.36k | } |
843 | | |
844 | | static WEBP_INLINE void DC16Mode_SSE2(uint8_t* WEBP_RESTRICT dst, |
845 | | const uint8_t* WEBP_RESTRICT left, |
846 | 6.55M | const uint8_t* WEBP_RESTRICT top) { |
847 | 6.55M | if (top != NULL) { |
848 | 6.43M | if (left != NULL) { // top and left present |
849 | 6.32M | DC16_SSE2(dst, left, top); |
850 | 6.32M | } else { // top, but no left |
851 | 114k | DC16NoLeft_SSE2(dst, top); |
852 | 114k | } |
853 | 6.43M | } else if (left != NULL) { // left but no top |
854 | 111k | DC16NoTop_SSE2(dst, left); |
855 | 111k | } else { // no top, no left, nothing. |
856 | 3.36k | DC16NoTopLeft_SSE2(dst); |
857 | 3.36k | } |
858 | 6.55M | } |
859 | | |
860 | | //------------------------------------------------------------------------------ |
861 | | // 4x4 predictions |
862 | | |
863 | 908M | #define DST(x, y) dst[(x) + (y) * BPS] |
864 | 378M | #define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2) |
865 | 176M | #define AVG2(a, b) (((a) + (b) + 1) >> 1) |
866 | | |
867 | | // We use the following 8b-arithmetic tricks: |
868 | | // (a + 2 * b + c + 2) >> 2 = (AC + b + 1) >> 1 |
869 | | // where: AC = (a + c) >> 1 = [(a + c + 1) >> 1] - [(a^c) & 1] |
870 | | // and: |
871 | | // (a + 2 * b + c + 2) >> 2 = (AB + BC + 1) >> 1 - (ab|bc)&lsb |
872 | | // where: AC = (a + b + 1) >> 1, BC = (b + c + 1) >> 1 |
873 | | // and ab = a ^ b, bc = b ^ c, lsb = (AC^BC)&1 |
874 | | |
875 | | // vertical |
876 | | static WEBP_INLINE void VE4_SSE2(uint8_t* WEBP_RESTRICT dst, |
877 | 25.2M | const uint8_t* WEBP_RESTRICT top) { |
878 | 25.2M | const __m128i one = _mm_set1_epi8(1); |
879 | 25.2M | const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(top - 1)); |
880 | 25.2M | const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1); |
881 | 25.2M | const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2); |
882 | 25.2M | const __m128i a = _mm_avg_epu8(ABCDEFGH, CDEFGH00); |
883 | 25.2M | const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGH00), one); |
884 | 25.2M | const __m128i b = _mm_subs_epu8(a, lsb); |
885 | 25.2M | const __m128i avg = _mm_avg_epu8(b, BCDEFGH0); |
886 | 25.2M | const int vals = _mm_cvtsi128_si32(avg); |
887 | 25.2M | int i; |
888 | 126M | for (i = 0; i < 4; ++i) { |
889 | 100M | WebPInt32ToMem(dst + i * BPS, vals); |
890 | 100M | } |
891 | 25.2M | } |
892 | | |
893 | | // horizontal |
894 | | static WEBP_INLINE void HE4_SSE2(uint8_t* WEBP_RESTRICT dst, |
895 | 25.2M | const uint8_t* WEBP_RESTRICT top) { |
896 | 25.2M | const int X = top[-1]; |
897 | 25.2M | const int I = top[-2]; |
898 | 25.2M | const int J = top[-3]; |
899 | 25.2M | const int K = top[-4]; |
900 | 25.2M | const int L = top[-5]; |
901 | 25.2M | WebPUint32ToMem(dst + 0 * BPS, 0x01010101U * AVG3(X, I, J)); |
902 | 25.2M | WebPUint32ToMem(dst + 1 * BPS, 0x01010101U * AVG3(I, J, K)); |
903 | 25.2M | WebPUint32ToMem(dst + 2 * BPS, 0x01010101U * AVG3(J, K, L)); |
904 | 25.2M | WebPUint32ToMem(dst + 3 * BPS, 0x01010101U * AVG3(K, L, L)); |
905 | 25.2M | } |
906 | | |
907 | | static WEBP_INLINE void DC4_SSE2(uint8_t* WEBP_RESTRICT dst, |
908 | 25.2M | const uint8_t* WEBP_RESTRICT top) { |
909 | 25.2M | uint32_t dc = 4; |
910 | 25.2M | int i; |
911 | 126M | for (i = 0; i < 4; ++i) dc += top[i] + top[-5 + i]; |
912 | 25.2M | Fill_SSE2(dst, dc >> 3, 4); |
913 | 25.2M | } |
914 | | |
915 | | // Down-Left |
916 | | static WEBP_INLINE void LD4_SSE2(uint8_t* WEBP_RESTRICT dst, |
917 | 25.2M | const uint8_t* WEBP_RESTRICT top) { |
918 | 25.2M | const __m128i one = _mm_set1_epi8(1); |
919 | 25.2M | const __m128i ABCDEFGH = _mm_loadl_epi64((const __m128i*)top); |
920 | 25.2M | const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1); |
921 | 25.2M | const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2); |
922 | 25.2M | const __m128i CDEFGHH0 = _mm_insert_epi16(CDEFGH00, top[7], 3); |
923 | 25.2M | const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, CDEFGHH0); |
924 | 25.2M | const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGHH0), one); |
925 | 25.2M | const __m128i avg2 = _mm_subs_epu8(avg1, lsb); |
926 | 25.2M | const __m128i abcdefg = _mm_avg_epu8(avg2, BCDEFGH0); |
927 | 25.2M | WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(abcdefg)); |
928 | 25.2M | WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1))); |
929 | 25.2M | WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2))); |
930 | 25.2M | WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3))); |
931 | 25.2M | } |
932 | | |
933 | | // Vertical-Right |
934 | | static WEBP_INLINE void VR4_SSE2(uint8_t* WEBP_RESTRICT dst, |
935 | 25.2M | const uint8_t* WEBP_RESTRICT top) { |
936 | 25.2M | const __m128i one = _mm_set1_epi8(1); |
937 | 25.2M | const int I = top[-2]; |
938 | 25.2M | const int J = top[-3]; |
939 | 25.2M | const int K = top[-4]; |
940 | 25.2M | const int X = top[-1]; |
941 | 25.2M | const __m128i XABCD = _mm_loadl_epi64((const __m128i*)(top - 1)); |
942 | 25.2M | const __m128i ABCD0 = _mm_srli_si128(XABCD, 1); |
943 | 25.2M | const __m128i abcd = _mm_avg_epu8(XABCD, ABCD0); |
944 | 25.2M | const __m128i _XABCD = _mm_slli_si128(XABCD, 1); |
945 | 25.2M | const __m128i IXABCD = _mm_insert_epi16(_XABCD, (short)(I | (X << 8)), 0); |
946 | 25.2M | const __m128i avg1 = _mm_avg_epu8(IXABCD, ABCD0); |
947 | 25.2M | const __m128i lsb = _mm_and_si128(_mm_xor_si128(IXABCD, ABCD0), one); |
948 | 25.2M | const __m128i avg2 = _mm_subs_epu8(avg1, lsb); |
949 | 25.2M | const __m128i efgh = _mm_avg_epu8(avg2, XABCD); |
950 | 25.2M | WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(abcd)); |
951 | 25.2M | WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(efgh)); |
952 | 25.2M | WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(abcd, 1))); |
953 | 25.2M | WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(efgh, 1))); |
954 | | |
955 | | // these two are hard to implement in SSE2, so we keep the C-version: |
956 | 25.2M | DST(0, 2) = AVG3(J, I, X); |
957 | 25.2M | DST(0, 3) = AVG3(K, J, I); |
958 | 25.2M | } |
959 | | |
960 | | // Vertical-Left |
961 | | static WEBP_INLINE void VL4_SSE2(uint8_t* WEBP_RESTRICT dst, |
962 | 25.2M | const uint8_t* WEBP_RESTRICT top) { |
963 | 25.2M | const __m128i one = _mm_set1_epi8(1); |
964 | 25.2M | const __m128i ABCDEFGH = _mm_loadl_epi64((const __m128i*)top); |
965 | 25.2M | const __m128i BCDEFGH_ = _mm_srli_si128(ABCDEFGH, 1); |
966 | 25.2M | const __m128i CDEFGH__ = _mm_srli_si128(ABCDEFGH, 2); |
967 | 25.2M | const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, BCDEFGH_); |
968 | 25.2M | const __m128i avg2 = _mm_avg_epu8(CDEFGH__, BCDEFGH_); |
969 | 25.2M | const __m128i avg3 = _mm_avg_epu8(avg1, avg2); |
970 | 25.2M | const __m128i lsb1 = _mm_and_si128(_mm_xor_si128(avg1, avg2), one); |
971 | 25.2M | const __m128i ab = _mm_xor_si128(ABCDEFGH, BCDEFGH_); |
972 | 25.2M | const __m128i bc = _mm_xor_si128(CDEFGH__, BCDEFGH_); |
973 | 25.2M | const __m128i abbc = _mm_or_si128(ab, bc); |
974 | 25.2M | const __m128i lsb2 = _mm_and_si128(abbc, lsb1); |
975 | 25.2M | const __m128i avg4 = _mm_subs_epu8(avg3, lsb2); |
976 | 25.2M | const uint32_t extra_out = |
977 | 25.2M | (uint32_t)_mm_cvtsi128_si32(_mm_srli_si128(avg4, 4)); |
978 | 25.2M | WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(avg1)); |
979 | 25.2M | WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(avg4)); |
980 | 25.2M | WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg1, 1))); |
981 | 25.2M | WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg4, 1))); |
982 | | |
983 | | // these two are hard to get and irregular |
984 | 25.2M | DST(3, 2) = (extra_out >> 0) & 0xff; |
985 | 25.2M | DST(3, 3) = (extra_out >> 8) & 0xff; |
986 | 25.2M | } |
987 | | |
988 | | // Down-right |
989 | | static WEBP_INLINE void RD4_SSE2(uint8_t* WEBP_RESTRICT dst, |
990 | 25.2M | const uint8_t* WEBP_RESTRICT top) { |
991 | 25.2M | const __m128i one = _mm_set1_epi8(1); |
992 | 25.2M | const __m128i LKJIXABC = _mm_loadl_epi64((const __m128i*)(top - 5)); |
993 | 25.2M | const __m128i LKJIXABCD = _mm_insert_epi16(LKJIXABC, top[3], 4); |
994 | 25.2M | const __m128i KJIXABCD_ = _mm_srli_si128(LKJIXABCD, 1); |
995 | 25.2M | const __m128i JIXABCD__ = _mm_srli_si128(LKJIXABCD, 2); |
996 | 25.2M | const __m128i avg1 = _mm_avg_epu8(JIXABCD__, LKJIXABCD); |
997 | 25.2M | const __m128i lsb = _mm_and_si128(_mm_xor_si128(JIXABCD__, LKJIXABCD), one); |
998 | 25.2M | const __m128i avg2 = _mm_subs_epu8(avg1, lsb); |
999 | 25.2M | const __m128i abcdefg = _mm_avg_epu8(avg2, KJIXABCD_); |
1000 | 25.2M | WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(abcdefg)); |
1001 | 25.2M | WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1))); |
1002 | 25.2M | WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2))); |
1003 | 25.2M | WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3))); |
1004 | 25.2M | } |
1005 | | |
1006 | | static WEBP_INLINE void HU4_SSE2(uint8_t* WEBP_RESTRICT dst, |
1007 | 25.2M | const uint8_t* WEBP_RESTRICT top) { |
1008 | 25.2M | const int I = top[-2]; |
1009 | 25.2M | const int J = top[-3]; |
1010 | 25.2M | const int K = top[-4]; |
1011 | 25.2M | const int L = top[-5]; |
1012 | 25.2M | DST(0, 0) = AVG2(I, J); |
1013 | 25.2M | DST(2, 0) = DST(0, 1) = AVG2(J, K); |
1014 | 25.2M | DST(2, 1) = DST(0, 2) = AVG2(K, L); |
1015 | 25.2M | DST(1, 0) = AVG3(I, J, K); |
1016 | 25.2M | DST(3, 0) = DST(1, 1) = AVG3(J, K, L); |
1017 | 25.2M | DST(3, 1) = DST(1, 2) = AVG3(K, L, L); |
1018 | 25.2M | DST(3, 2) = DST(2, 2) = DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L; |
1019 | 25.2M | } |
1020 | | |
1021 | | static WEBP_INLINE void HD4_SSE2(uint8_t* WEBP_RESTRICT dst, |
1022 | 25.2M | const uint8_t* WEBP_RESTRICT top) { |
1023 | 25.2M | const int X = top[-1]; |
1024 | 25.2M | const int I = top[-2]; |
1025 | 25.2M | const int J = top[-3]; |
1026 | 25.2M | const int K = top[-4]; |
1027 | 25.2M | const int L = top[-5]; |
1028 | 25.2M | const int A = top[0]; |
1029 | 25.2M | const int B = top[1]; |
1030 | 25.2M | const int C = top[2]; |
1031 | | |
1032 | 25.2M | DST(0, 0) = DST(2, 1) = AVG2(I, X); |
1033 | 25.2M | DST(0, 1) = DST(2, 2) = AVG2(J, I); |
1034 | 25.2M | DST(0, 2) = DST(2, 3) = AVG2(K, J); |
1035 | 25.2M | DST(0, 3) = AVG2(L, K); |
1036 | | |
1037 | 25.2M | DST(3, 0) = AVG3(A, B, C); |
1038 | 25.2M | DST(2, 0) = AVG3(X, A, B); |
1039 | 25.2M | DST(1, 0) = DST(3, 1) = AVG3(I, X, A); |
1040 | 25.2M | DST(1, 1) = DST(3, 2) = AVG3(J, I, X); |
1041 | 25.2M | DST(1, 2) = DST(3, 3) = AVG3(K, J, I); |
1042 | 25.2M | DST(1, 3) = AVG3(L, K, J); |
1043 | 25.2M | } |
1044 | | |
1045 | | static WEBP_INLINE void TM4_SSE2(uint8_t* WEBP_RESTRICT dst, |
1046 | 25.2M | const uint8_t* WEBP_RESTRICT top) { |
1047 | 25.2M | const __m128i zero = _mm_setzero_si128(); |
1048 | 25.2M | const __m128i top_values = _mm_cvtsi32_si128(WebPMemToInt32(top)); |
1049 | 25.2M | const __m128i top_base = _mm_unpacklo_epi8(top_values, zero); |
1050 | 25.2M | int y; |
1051 | 126M | for (y = 0; y < 4; ++y, dst += BPS) { |
1052 | 100M | const int val = top[-2 - y] - top[-1]; |
1053 | 100M | const __m128i base = _mm_set1_epi16(val); |
1054 | 100M | const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero); |
1055 | 100M | WebPInt32ToMem(dst, _mm_cvtsi128_si32(out)); |
1056 | 100M | } |
1057 | 25.2M | } |
1058 | | |
1059 | | #undef DST |
1060 | | #undef AVG3 |
1061 | | #undef AVG2 |
1062 | | |
1063 | | //------------------------------------------------------------------------------ |
1064 | | // luma 4x4 prediction |
1065 | | |
1066 | | // Left samples are top[-5 .. -2], top_left is top[-1], top are |
1067 | | // located at top[0..3], and top right is top[4..7] |
1068 | | static void Intra4Preds_SSE2(uint8_t* WEBP_RESTRICT dst, |
1069 | 25.2M | const uint8_t* WEBP_RESTRICT top) { |
1070 | 25.2M | DC4_SSE2(I4DC4 + dst, top); |
1071 | 25.2M | TM4_SSE2(I4TM4 + dst, top); |
1072 | 25.2M | VE4_SSE2(I4VE4 + dst, top); |
1073 | 25.2M | HE4_SSE2(I4HE4 + dst, top); |
1074 | 25.2M | RD4_SSE2(I4RD4 + dst, top); |
1075 | 25.2M | VR4_SSE2(I4VR4 + dst, top); |
1076 | 25.2M | LD4_SSE2(I4LD4 + dst, top); |
1077 | 25.2M | VL4_SSE2(I4VL4 + dst, top); |
1078 | 25.2M | HD4_SSE2(I4HD4 + dst, top); |
1079 | 25.2M | HU4_SSE2(I4HU4 + dst, top); |
1080 | 25.2M | } |
1081 | | |
1082 | | //------------------------------------------------------------------------------ |
1083 | | // Chroma 8x8 prediction (paragraph 12.2) |
1084 | | |
1085 | | static void IntraChromaPreds_SSE2(uint8_t* WEBP_RESTRICT dst, |
1086 | | const uint8_t* WEBP_RESTRICT left, |
1087 | 6.55M | const uint8_t* WEBP_RESTRICT top) { |
1088 | | // U block |
1089 | 6.55M | DC8uvMode_SSE2(C8DC8 + dst, left, top); |
1090 | 6.55M | VerticalPred_SSE2(C8VE8 + dst, top, 8); |
1091 | 6.55M | HorizontalPred_SSE2(C8HE8 + dst, left, 8); |
1092 | 6.55M | TrueMotion_SSE2(C8TM8 + dst, left, top, 8); |
1093 | | // V block |
1094 | 6.55M | dst += 8; |
1095 | 6.55M | if (top != NULL) top += 8; |
1096 | 6.55M | if (left != NULL) left += 16; |
1097 | 6.55M | DC8uvMode_SSE2(C8DC8 + dst, left, top); |
1098 | 6.55M | VerticalPred_SSE2(C8VE8 + dst, top, 8); |
1099 | 6.55M | HorizontalPred_SSE2(C8HE8 + dst, left, 8); |
1100 | 6.55M | TrueMotion_SSE2(C8TM8 + dst, left, top, 8); |
1101 | 6.55M | } |
1102 | | |
1103 | | //------------------------------------------------------------------------------ |
1104 | | // luma 16x16 prediction (paragraph 12.3) |
1105 | | |
1106 | | static void Intra16Preds_SSE2(uint8_t* WEBP_RESTRICT dst, |
1107 | | const uint8_t* WEBP_RESTRICT left, |
1108 | 6.55M | const uint8_t* WEBP_RESTRICT top) { |
1109 | 6.55M | DC16Mode_SSE2(I16DC16 + dst, left, top); |
1110 | 6.55M | VerticalPred_SSE2(I16VE16 + dst, top, 16); |
1111 | 6.55M | HorizontalPred_SSE2(I16HE16 + dst, left, 16); |
1112 | 6.55M | TrueMotion_SSE2(I16TM16 + dst, left, top, 16); |
1113 | 6.55M | } |
1114 | | |
1115 | | //------------------------------------------------------------------------------ |
1116 | | // Metric |
1117 | | |
1118 | | static WEBP_INLINE void SubtractAndAccumulate_SSE2(const __m128i a, |
1119 | | const __m128i b, |
1120 | 314M | __m128i* const sum) { |
1121 | | // take abs(a-b) in 8b |
1122 | 314M | const __m128i a_b = _mm_subs_epu8(a, b); |
1123 | 314M | const __m128i b_a = _mm_subs_epu8(b, a); |
1124 | 314M | const __m128i abs_a_b = _mm_or_si128(a_b, b_a); |
1125 | | // zero-extend to 16b |
1126 | 314M | const __m128i zero = _mm_setzero_si128(); |
1127 | 314M | const __m128i C0 = _mm_unpacklo_epi8(abs_a_b, zero); |
1128 | 314M | const __m128i C1 = _mm_unpackhi_epi8(abs_a_b, zero); |
1129 | | // multiply with self |
1130 | 314M | const __m128i sum1 = _mm_madd_epi16(C0, C0); |
1131 | 314M | const __m128i sum2 = _mm_madd_epi16(C1, C1); |
1132 | 314M | *sum = _mm_add_epi32(sum1, sum2); |
1133 | 314M | } |
1134 | | |
1135 | | static WEBP_INLINE int SSE_16xN_SSE2(const uint8_t* WEBP_RESTRICT a, |
1136 | | const uint8_t* WEBP_RESTRICT b, |
1137 | 26.2M | int num_pairs) { |
1138 | 26.2M | __m128i sum = _mm_setzero_si128(); |
1139 | 26.2M | int32_t tmp[4]; |
1140 | 26.2M | int i; |
1141 | | |
1142 | 183M | for (i = 0; i < num_pairs; ++i) { |
1143 | 157M | const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[BPS * 0]); |
1144 | 157M | const __m128i b0 = _mm_loadu_si128((const __m128i*)&b[BPS * 0]); |
1145 | 157M | const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[BPS * 1]); |
1146 | 157M | const __m128i b1 = _mm_loadu_si128((const __m128i*)&b[BPS * 1]); |
1147 | 157M | __m128i sum1, sum2; |
1148 | 157M | SubtractAndAccumulate_SSE2(a0, b0, &sum1); |
1149 | 157M | SubtractAndAccumulate_SSE2(a1, b1, &sum2); |
1150 | 157M | sum = _mm_add_epi32(sum, _mm_add_epi32(sum1, sum2)); |
1151 | 157M | a += 2 * BPS; |
1152 | 157M | b += 2 * BPS; |
1153 | 157M | } |
1154 | 26.2M | _mm_storeu_si128((__m128i*)tmp, sum); |
1155 | 26.2M | return (tmp[3] + tmp[2] + tmp[1] + tmp[0]); |
1156 | 26.2M | } |
1157 | | |
1158 | | static int SSE16x16_SSE2(const uint8_t* WEBP_RESTRICT a, |
1159 | 13.1M | const uint8_t* WEBP_RESTRICT b) { |
1160 | 13.1M | return SSE_16xN_SSE2(a, b, 8); |
1161 | 13.1M | } |
1162 | | |
1163 | | static int SSE16x8_SSE2(const uint8_t* WEBP_RESTRICT a, |
1164 | 13.1M | const uint8_t* WEBP_RESTRICT b) { |
1165 | 13.1M | return SSE_16xN_SSE2(a, b, 4); |
1166 | 13.1M | } |
1167 | | |
1168 | | #define LOAD_8x16b(ptr) \ |
1169 | 0 | _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(ptr)), zero) |
1170 | | |
1171 | | static int SSE8x8_SSE2(const uint8_t* WEBP_RESTRICT a, |
1172 | 0 | const uint8_t* WEBP_RESTRICT b) { |
1173 | 0 | const __m128i zero = _mm_setzero_si128(); |
1174 | 0 | int num_pairs = 4; |
1175 | 0 | __m128i sum = zero; |
1176 | 0 | int32_t tmp[4]; |
1177 | 0 | while (num_pairs-- > 0) { |
1178 | 0 | const __m128i a0 = LOAD_8x16b(&a[BPS * 0]); |
1179 | 0 | const __m128i a1 = LOAD_8x16b(&a[BPS * 1]); |
1180 | 0 | const __m128i b0 = LOAD_8x16b(&b[BPS * 0]); |
1181 | 0 | const __m128i b1 = LOAD_8x16b(&b[BPS * 1]); |
1182 | | // subtract |
1183 | 0 | const __m128i c0 = _mm_subs_epi16(a0, b0); |
1184 | 0 | const __m128i c1 = _mm_subs_epi16(a1, b1); |
1185 | | // multiply/accumulate with self |
1186 | 0 | const __m128i d0 = _mm_madd_epi16(c0, c0); |
1187 | 0 | const __m128i d1 = _mm_madd_epi16(c1, c1); |
1188 | | // collect |
1189 | 0 | const __m128i sum01 = _mm_add_epi32(d0, d1); |
1190 | 0 | sum = _mm_add_epi32(sum, sum01); |
1191 | 0 | a += 2 * BPS; |
1192 | 0 | b += 2 * BPS; |
1193 | 0 | } |
1194 | 0 | _mm_storeu_si128((__m128i*)tmp, sum); |
1195 | 0 | return (tmp[3] + tmp[2] + tmp[1] + tmp[0]); |
1196 | 0 | } |
1197 | | #undef LOAD_8x16b |
1198 | | |
1199 | | static int SSE4x4_SSE2(const uint8_t* WEBP_RESTRICT a, |
1200 | 252M | const uint8_t* WEBP_RESTRICT b) { |
1201 | 252M | const __m128i zero = _mm_setzero_si128(); |
1202 | | |
1203 | | // Load values. Note that we read 8 pixels instead of 4, |
1204 | | // but the a/b buffers are over-allocated to that effect. |
1205 | 252M | const __m128i a0 = _mm_loadl_epi64((const __m128i*)&a[BPS * 0]); |
1206 | 252M | const __m128i a1 = _mm_loadl_epi64((const __m128i*)&a[BPS * 1]); |
1207 | 252M | const __m128i a2 = _mm_loadl_epi64((const __m128i*)&a[BPS * 2]); |
1208 | 252M | const __m128i a3 = _mm_loadl_epi64((const __m128i*)&a[BPS * 3]); |
1209 | 252M | const __m128i b0 = _mm_loadl_epi64((const __m128i*)&b[BPS * 0]); |
1210 | 252M | const __m128i b1 = _mm_loadl_epi64((const __m128i*)&b[BPS * 1]); |
1211 | 252M | const __m128i b2 = _mm_loadl_epi64((const __m128i*)&b[BPS * 2]); |
1212 | 252M | const __m128i b3 = _mm_loadl_epi64((const __m128i*)&b[BPS * 3]); |
1213 | | // Combine pair of lines. |
1214 | 252M | const __m128i a01 = _mm_unpacklo_epi32(a0, a1); |
1215 | 252M | const __m128i a23 = _mm_unpacklo_epi32(a2, a3); |
1216 | 252M | const __m128i b01 = _mm_unpacklo_epi32(b0, b1); |
1217 | 252M | const __m128i b23 = _mm_unpacklo_epi32(b2, b3); |
1218 | | // Convert to 16b. |
1219 | 252M | const __m128i a01s = _mm_unpacklo_epi8(a01, zero); |
1220 | 252M | const __m128i a23s = _mm_unpacklo_epi8(a23, zero); |
1221 | 252M | const __m128i b01s = _mm_unpacklo_epi8(b01, zero); |
1222 | 252M | const __m128i b23s = _mm_unpacklo_epi8(b23, zero); |
1223 | | // subtract, square and accumulate |
1224 | 252M | const __m128i d0 = _mm_subs_epi16(a01s, b01s); |
1225 | 252M | const __m128i d1 = _mm_subs_epi16(a23s, b23s); |
1226 | 252M | const __m128i e0 = _mm_madd_epi16(d0, d0); |
1227 | 252M | const __m128i e1 = _mm_madd_epi16(d1, d1); |
1228 | 252M | const __m128i sum = _mm_add_epi32(e0, e1); |
1229 | | |
1230 | 252M | int32_t tmp[4]; |
1231 | 252M | _mm_storeu_si128((__m128i*)tmp, sum); |
1232 | 252M | return (tmp[3] + tmp[2] + tmp[1] + tmp[0]); |
1233 | 252M | } |
1234 | | |
1235 | | //------------------------------------------------------------------------------ |
1236 | | |
1237 | 0 | static void Mean16x4_SSE2(const uint8_t* WEBP_RESTRICT ref, uint32_t dc[4]) { |
1238 | 0 | const __m128i mask = _mm_set1_epi16(0x00ff); |
1239 | 0 | const __m128i a0 = _mm_loadu_si128((const __m128i*)&ref[BPS * 0]); |
1240 | 0 | const __m128i a1 = _mm_loadu_si128((const __m128i*)&ref[BPS * 1]); |
1241 | 0 | const __m128i a2 = _mm_loadu_si128((const __m128i*)&ref[BPS * 2]); |
1242 | 0 | const __m128i a3 = _mm_loadu_si128((const __m128i*)&ref[BPS * 3]); |
1243 | 0 | const __m128i b0 = _mm_srli_epi16(a0, 8); // hi byte |
1244 | 0 | const __m128i b1 = _mm_srli_epi16(a1, 8); |
1245 | 0 | const __m128i b2 = _mm_srli_epi16(a2, 8); |
1246 | 0 | const __m128i b3 = _mm_srli_epi16(a3, 8); |
1247 | 0 | const __m128i c0 = _mm_and_si128(a0, mask); // lo byte |
1248 | 0 | const __m128i c1 = _mm_and_si128(a1, mask); |
1249 | 0 | const __m128i c2 = _mm_and_si128(a2, mask); |
1250 | 0 | const __m128i c3 = _mm_and_si128(a3, mask); |
1251 | 0 | const __m128i d0 = _mm_add_epi32(b0, c0); |
1252 | 0 | const __m128i d1 = _mm_add_epi32(b1, c1); |
1253 | 0 | const __m128i d2 = _mm_add_epi32(b2, c2); |
1254 | 0 | const __m128i d3 = _mm_add_epi32(b3, c3); |
1255 | 0 | const __m128i e0 = _mm_add_epi32(d0, d1); |
1256 | 0 | const __m128i e1 = _mm_add_epi32(d2, d3); |
1257 | 0 | const __m128i f0 = _mm_add_epi32(e0, e1); |
1258 | 0 | uint16_t tmp[8]; |
1259 | 0 | _mm_storeu_si128((__m128i*)tmp, f0); |
1260 | 0 | dc[0] = tmp[0] + tmp[1]; |
1261 | 0 | dc[1] = tmp[2] + tmp[3]; |
1262 | 0 | dc[2] = tmp[4] + tmp[5]; |
1263 | 0 | dc[3] = tmp[6] + tmp[7]; |
1264 | 0 | } |
1265 | | |
1266 | | //------------------------------------------------------------------------------ |
1267 | | // Texture distortion |
1268 | | // |
1269 | | // We try to match the spectral content (weighted) between source and |
1270 | | // reconstructed samples. |
1271 | | |
1272 | | // Hadamard transform |
1273 | | // Returns the weighted sum of the absolute value of transformed coefficients. |
1274 | | // w[] contains a row-major 4 by 4 symmetric matrix. |
1275 | | static int TTransform_SSE2(const uint8_t* WEBP_RESTRICT inA, |
1276 | | const uint8_t* WEBP_RESTRICT inB, |
1277 | 0 | const uint16_t* WEBP_RESTRICT const w) { |
1278 | 0 | int32_t sum[4]; |
1279 | 0 | __m128i tmp_0, tmp_1, tmp_2, tmp_3; |
1280 | 0 | const __m128i zero = _mm_setzero_si128(); |
1281 | | |
1282 | | // Load and combine inputs. |
1283 | 0 | { |
1284 | 0 | const __m128i inA_0 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 0]); |
1285 | 0 | const __m128i inA_1 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 1]); |
1286 | 0 | const __m128i inA_2 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 2]); |
1287 | 0 | const __m128i inA_3 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 3]); |
1288 | 0 | const __m128i inB_0 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 0]); |
1289 | 0 | const __m128i inB_1 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 1]); |
1290 | 0 | const __m128i inB_2 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 2]); |
1291 | 0 | const __m128i inB_3 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 3]); |
1292 | | |
1293 | | // Combine inA and inB (we'll do two transforms in parallel). |
1294 | 0 | const __m128i inAB_0 = _mm_unpacklo_epi32(inA_0, inB_0); |
1295 | 0 | const __m128i inAB_1 = _mm_unpacklo_epi32(inA_1, inB_1); |
1296 | 0 | const __m128i inAB_2 = _mm_unpacklo_epi32(inA_2, inB_2); |
1297 | 0 | const __m128i inAB_3 = _mm_unpacklo_epi32(inA_3, inB_3); |
1298 | 0 | tmp_0 = _mm_unpacklo_epi8(inAB_0, zero); |
1299 | 0 | tmp_1 = _mm_unpacklo_epi8(inAB_1, zero); |
1300 | 0 | tmp_2 = _mm_unpacklo_epi8(inAB_2, zero); |
1301 | 0 | tmp_3 = _mm_unpacklo_epi8(inAB_3, zero); |
1302 | | // a00 a01 a02 a03 b00 b01 b02 b03 |
1303 | | // a10 a11 a12 a13 b10 b11 b12 b13 |
1304 | | // a20 a21 a22 a23 b20 b21 b22 b23 |
1305 | | // a30 a31 a32 a33 b30 b31 b32 b33 |
1306 | 0 | } |
1307 | | |
1308 | | // Vertical pass first to avoid a transpose (vertical and horizontal passes |
1309 | | // are commutative because w/kWeightY is symmetric) and subsequent transpose. |
1310 | 0 | { |
1311 | | // Calculate a and b (two 4x4 at once). |
1312 | 0 | const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2); |
1313 | 0 | const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3); |
1314 | 0 | const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3); |
1315 | 0 | const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2); |
1316 | 0 | const __m128i b0 = _mm_add_epi16(a0, a1); |
1317 | 0 | const __m128i b1 = _mm_add_epi16(a3, a2); |
1318 | 0 | const __m128i b2 = _mm_sub_epi16(a3, a2); |
1319 | 0 | const __m128i b3 = _mm_sub_epi16(a0, a1); |
1320 | | // a00 a01 a02 a03 b00 b01 b02 b03 |
1321 | | // a10 a11 a12 a13 b10 b11 b12 b13 |
1322 | | // a20 a21 a22 a23 b20 b21 b22 b23 |
1323 | | // a30 a31 a32 a33 b30 b31 b32 b33 |
1324 | | |
1325 | | // Transpose the two 4x4. |
1326 | 0 | VP8Transpose_2_4x4_16b(&b0, &b1, &b2, &b3, &tmp_0, &tmp_1, &tmp_2, &tmp_3); |
1327 | 0 | } |
1328 | | |
1329 | | // Horizontal pass and difference of weighted sums. |
1330 | 0 | { |
1331 | | // Load all inputs. |
1332 | 0 | const __m128i w_0 = _mm_loadu_si128((const __m128i*)&w[0]); |
1333 | 0 | const __m128i w_8 = _mm_loadu_si128((const __m128i*)&w[8]); |
1334 | | |
1335 | | // Calculate a and b (two 4x4 at once). |
1336 | 0 | const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2); |
1337 | 0 | const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3); |
1338 | 0 | const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3); |
1339 | 0 | const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2); |
1340 | 0 | const __m128i b0 = _mm_add_epi16(a0, a1); |
1341 | 0 | const __m128i b1 = _mm_add_epi16(a3, a2); |
1342 | 0 | const __m128i b2 = _mm_sub_epi16(a3, a2); |
1343 | 0 | const __m128i b3 = _mm_sub_epi16(a0, a1); |
1344 | | |
1345 | | // Separate the transforms of inA and inB. |
1346 | 0 | __m128i A_b0 = _mm_unpacklo_epi64(b0, b1); |
1347 | 0 | __m128i A_b2 = _mm_unpacklo_epi64(b2, b3); |
1348 | 0 | __m128i B_b0 = _mm_unpackhi_epi64(b0, b1); |
1349 | 0 | __m128i B_b2 = _mm_unpackhi_epi64(b2, b3); |
1350 | |
|
1351 | 0 | { |
1352 | 0 | const __m128i d0 = _mm_sub_epi16(zero, A_b0); |
1353 | 0 | const __m128i d1 = _mm_sub_epi16(zero, A_b2); |
1354 | 0 | const __m128i d2 = _mm_sub_epi16(zero, B_b0); |
1355 | 0 | const __m128i d3 = _mm_sub_epi16(zero, B_b2); |
1356 | 0 | A_b0 = _mm_max_epi16(A_b0, d0); // abs(v), 16b |
1357 | 0 | A_b2 = _mm_max_epi16(A_b2, d1); |
1358 | 0 | B_b0 = _mm_max_epi16(B_b0, d2); |
1359 | 0 | B_b2 = _mm_max_epi16(B_b2, d3); |
1360 | 0 | } |
1361 | | |
1362 | | // weighted sums |
1363 | 0 | A_b0 = _mm_madd_epi16(A_b0, w_0); |
1364 | 0 | A_b2 = _mm_madd_epi16(A_b2, w_8); |
1365 | 0 | B_b0 = _mm_madd_epi16(B_b0, w_0); |
1366 | 0 | B_b2 = _mm_madd_epi16(B_b2, w_8); |
1367 | 0 | A_b0 = _mm_add_epi32(A_b0, A_b2); |
1368 | 0 | B_b0 = _mm_add_epi32(B_b0, B_b2); |
1369 | | |
1370 | | // difference of weighted sums |
1371 | 0 | A_b0 = _mm_sub_epi32(A_b0, B_b0); |
1372 | 0 | _mm_storeu_si128((__m128i*)&sum[0], A_b0); |
1373 | 0 | } |
1374 | 0 | return sum[0] + sum[1] + sum[2] + sum[3]; |
1375 | 0 | } |
1376 | | |
1377 | | static int Disto4x4_SSE2(const uint8_t* WEBP_RESTRICT const a, |
1378 | | const uint8_t* WEBP_RESTRICT const b, |
1379 | 0 | const uint16_t* WEBP_RESTRICT const w) { |
1380 | 0 | const int diff_sum = TTransform_SSE2(a, b, w); |
1381 | 0 | return abs(diff_sum) >> 5; |
1382 | 0 | } |
1383 | | |
1384 | | static int Disto16x16_SSE2(const uint8_t* WEBP_RESTRICT const a, |
1385 | | const uint8_t* WEBP_RESTRICT const b, |
1386 | 0 | const uint16_t* WEBP_RESTRICT const w) { |
1387 | 0 | int D = 0; |
1388 | 0 | int x, y; |
1389 | 0 | for (y = 0; y < 16 * BPS; y += 4 * BPS) { |
1390 | 0 | for (x = 0; x < 16; x += 4) { |
1391 | 0 | D += Disto4x4_SSE2(a + x + y, b + x + y, w); |
1392 | 0 | } |
1393 | 0 | } |
1394 | 0 | return D; |
1395 | 0 | } |
1396 | | |
1397 | | //------------------------------------------------------------------------------ |
1398 | | // Quantization |
1399 | | // |
1400 | | |
1401 | | static WEBP_INLINE int DoQuantizeBlock_SSE2( |
1402 | | int16_t in[16], int16_t out[16], |
1403 | | const uint16_t* WEBP_RESTRICT const sharpen, |
1404 | 0 | const VP8Matrix* WEBP_RESTRICT const mtx) { |
1405 | 0 | const __m128i max_coeff_2047 = _mm_set1_epi16(MAX_LEVEL); |
1406 | 0 | const __m128i zero = _mm_setzero_si128(); |
1407 | 0 | __m128i coeff0, coeff8; |
1408 | 0 | __m128i out0, out8; |
1409 | 0 | __m128i packed_out; |
1410 | | |
1411 | | // Load all inputs. |
1412 | 0 | __m128i in0 = _mm_loadu_si128((__m128i*)&in[0]); |
1413 | 0 | __m128i in8 = _mm_loadu_si128((__m128i*)&in[8]); |
1414 | 0 | const __m128i iq0 = _mm_loadu_si128((const __m128i*)&mtx->iq[0]); |
1415 | 0 | const __m128i iq8 = _mm_loadu_si128((const __m128i*)&mtx->iq[8]); |
1416 | 0 | const __m128i q0 = _mm_loadu_si128((const __m128i*)&mtx->q[0]); |
1417 | 0 | const __m128i q8 = _mm_loadu_si128((const __m128i*)&mtx->q[8]); |
1418 | | |
1419 | | // extract sign(in) (0x0000 if positive, 0xffff if negative) |
1420 | 0 | const __m128i sign0 = _mm_cmpgt_epi16(zero, in0); |
1421 | 0 | const __m128i sign8 = _mm_cmpgt_epi16(zero, in8); |
1422 | | |
1423 | | // coeff = abs(in) = (in ^ sign) - sign |
1424 | 0 | coeff0 = _mm_xor_si128(in0, sign0); |
1425 | 0 | coeff8 = _mm_xor_si128(in8, sign8); |
1426 | 0 | coeff0 = _mm_sub_epi16(coeff0, sign0); |
1427 | 0 | coeff8 = _mm_sub_epi16(coeff8, sign8); |
1428 | | |
1429 | | // coeff = abs(in) + sharpen |
1430 | 0 | if (sharpen != NULL) { |
1431 | 0 | const __m128i sharpen0 = _mm_loadu_si128((const __m128i*)&sharpen[0]); |
1432 | 0 | const __m128i sharpen8 = _mm_loadu_si128((const __m128i*)&sharpen[8]); |
1433 | 0 | coeff0 = _mm_add_epi16(coeff0, sharpen0); |
1434 | 0 | coeff8 = _mm_add_epi16(coeff8, sharpen8); |
1435 | 0 | } |
1436 | | |
1437 | | // out = (coeff * iQ + B) >> QFIX |
1438 | 0 | { |
1439 | | // doing calculations with 32b precision (QFIX=17) |
1440 | | // out = (coeff * iQ) |
1441 | 0 | const __m128i coeff_iQ0H = _mm_mulhi_epu16(coeff0, iq0); |
1442 | 0 | const __m128i coeff_iQ0L = _mm_mullo_epi16(coeff0, iq0); |
1443 | 0 | const __m128i coeff_iQ8H = _mm_mulhi_epu16(coeff8, iq8); |
1444 | 0 | const __m128i coeff_iQ8L = _mm_mullo_epi16(coeff8, iq8); |
1445 | 0 | __m128i out_00 = _mm_unpacklo_epi16(coeff_iQ0L, coeff_iQ0H); |
1446 | 0 | __m128i out_04 = _mm_unpackhi_epi16(coeff_iQ0L, coeff_iQ0H); |
1447 | 0 | __m128i out_08 = _mm_unpacklo_epi16(coeff_iQ8L, coeff_iQ8H); |
1448 | 0 | __m128i out_12 = _mm_unpackhi_epi16(coeff_iQ8L, coeff_iQ8H); |
1449 | | // out = (coeff * iQ + B) |
1450 | 0 | const __m128i bias_00 = _mm_loadu_si128((const __m128i*)&mtx->bias[0]); |
1451 | 0 | const __m128i bias_04 = _mm_loadu_si128((const __m128i*)&mtx->bias[4]); |
1452 | 0 | const __m128i bias_08 = _mm_loadu_si128((const __m128i*)&mtx->bias[8]); |
1453 | 0 | const __m128i bias_12 = _mm_loadu_si128((const __m128i*)&mtx->bias[12]); |
1454 | 0 | out_00 = _mm_add_epi32(out_00, bias_00); |
1455 | 0 | out_04 = _mm_add_epi32(out_04, bias_04); |
1456 | 0 | out_08 = _mm_add_epi32(out_08, bias_08); |
1457 | 0 | out_12 = _mm_add_epi32(out_12, bias_12); |
1458 | | // out = QUANTDIV(coeff, iQ, B, QFIX) |
1459 | 0 | out_00 = _mm_srai_epi32(out_00, QFIX); |
1460 | 0 | out_04 = _mm_srai_epi32(out_04, QFIX); |
1461 | 0 | out_08 = _mm_srai_epi32(out_08, QFIX); |
1462 | 0 | out_12 = _mm_srai_epi32(out_12, QFIX); |
1463 | | |
1464 | | // pack result as 16b |
1465 | 0 | out0 = _mm_packs_epi32(out_00, out_04); |
1466 | 0 | out8 = _mm_packs_epi32(out_08, out_12); |
1467 | | |
1468 | | // if (coeff > 2047) coeff = 2047 |
1469 | 0 | out0 = _mm_min_epi16(out0, max_coeff_2047); |
1470 | 0 | out8 = _mm_min_epi16(out8, max_coeff_2047); |
1471 | 0 | } |
1472 | | |
1473 | | // get sign back (if (sign[j]) out_n = -out_n) |
1474 | 0 | out0 = _mm_xor_si128(out0, sign0); |
1475 | 0 | out8 = _mm_xor_si128(out8, sign8); |
1476 | 0 | out0 = _mm_sub_epi16(out0, sign0); |
1477 | 0 | out8 = _mm_sub_epi16(out8, sign8); |
1478 | | |
1479 | | // in = out * Q |
1480 | 0 | in0 = _mm_mullo_epi16(out0, q0); |
1481 | 0 | in8 = _mm_mullo_epi16(out8, q8); |
1482 | |
|
1483 | 0 | _mm_storeu_si128((__m128i*)&in[0], in0); |
1484 | 0 | _mm_storeu_si128((__m128i*)&in[8], in8); |
1485 | | |
1486 | | // zigzag the output before storing it. |
1487 | | // |
1488 | | // The zigzag pattern can almost be reproduced with a small sequence of |
1489 | | // shuffles. After it, we only need to swap the 7th (ending up in third |
1490 | | // position instead of twelfth) and 8th values. |
1491 | 0 | { |
1492 | 0 | __m128i outZ0, outZ8; |
1493 | 0 | outZ0 = _mm_shufflehi_epi16(out0, _MM_SHUFFLE(2, 1, 3, 0)); |
1494 | 0 | outZ0 = _mm_shuffle_epi32(outZ0, _MM_SHUFFLE(3, 1, 2, 0)); |
1495 | 0 | outZ0 = _mm_shufflehi_epi16(outZ0, _MM_SHUFFLE(3, 1, 0, 2)); |
1496 | 0 | outZ8 = _mm_shufflelo_epi16(out8, _MM_SHUFFLE(3, 0, 2, 1)); |
1497 | 0 | outZ8 = _mm_shuffle_epi32(outZ8, _MM_SHUFFLE(3, 1, 2, 0)); |
1498 | 0 | outZ8 = _mm_shufflelo_epi16(outZ8, _MM_SHUFFLE(1, 3, 2, 0)); |
1499 | 0 | _mm_storeu_si128((__m128i*)&out[0], outZ0); |
1500 | 0 | _mm_storeu_si128((__m128i*)&out[8], outZ8); |
1501 | 0 | packed_out = _mm_packs_epi16(outZ0, outZ8); |
1502 | 0 | } |
1503 | 0 | { |
1504 | 0 | const int16_t outZ_12 = out[12]; |
1505 | 0 | const int16_t outZ_3 = out[3]; |
1506 | 0 | out[3] = outZ_12; |
1507 | 0 | out[12] = outZ_3; |
1508 | 0 | } |
1509 | | |
1510 | | // detect if all 'out' values are zeroes or not |
1511 | 0 | return (_mm_movemask_epi8(_mm_cmpeq_epi8(packed_out, zero)) != 0xffff); |
1512 | 0 | } |
1513 | | |
1514 | | static int QuantizeBlock_SSE2(int16_t in[16], int16_t out[16], |
1515 | 0 | const VP8Matrix* WEBP_RESTRICT const mtx) { |
1516 | 0 | return DoQuantizeBlock_SSE2(in, out, &mtx->sharpen[0], mtx); |
1517 | 0 | } |
1518 | | |
1519 | | static int QuantizeBlockWHT_SSE2(int16_t in[16], int16_t out[16], |
1520 | 0 | const VP8Matrix* WEBP_RESTRICT const mtx) { |
1521 | 0 | return DoQuantizeBlock_SSE2(in, out, NULL, mtx); |
1522 | 0 | } |
1523 | | |
1524 | | static int Quantize2Blocks_SSE2(int16_t in[32], int16_t out[32], |
1525 | 0 | const VP8Matrix* WEBP_RESTRICT const mtx) { |
1526 | 0 | int nz; |
1527 | 0 | const uint16_t* const sharpen = &mtx->sharpen[0]; |
1528 | 0 | nz = DoQuantizeBlock_SSE2(in + 0 * 16, out + 0 * 16, sharpen, mtx) << 0; |
1529 | 0 | nz |= DoQuantizeBlock_SSE2(in + 1 * 16, out + 1 * 16, sharpen, mtx) << 1; |
1530 | 0 | return nz; |
1531 | 0 | } |
1532 | | |
1533 | | //------------------------------------------------------------------------------ |
1534 | | // Entry point |
1535 | | |
1536 | | extern void VP8EncDspInitSSE2(void); |
1537 | | |
1538 | 1 | WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspInitSSE2(void) { |
1539 | 1 | VP8CollectHistogram = CollectHistogram_SSE2; |
1540 | 1 | VP8EncPredLuma16 = Intra16Preds_SSE2; |
1541 | 1 | VP8EncPredChroma8 = IntraChromaPreds_SSE2; |
1542 | 1 | VP8EncPredLuma4 = Intra4Preds_SSE2; |
1543 | 1 | VP8EncQuantizeBlock = QuantizeBlock_SSE2; |
1544 | 1 | VP8EncQuantize2Blocks = Quantize2Blocks_SSE2; |
1545 | 1 | VP8EncQuantizeBlockWHT = QuantizeBlockWHT_SSE2; |
1546 | 1 | VP8ITransform = ITransform_SSE2; |
1547 | 1 | VP8FTransform = FTransform_SSE2; |
1548 | 1 | VP8FTransform2 = FTransform2_SSE2; |
1549 | 1 | VP8FTransformWHT = FTransformWHT_SSE2; |
1550 | 1 | VP8SSE16x16 = SSE16x16_SSE2; |
1551 | 1 | VP8SSE16x8 = SSE16x8_SSE2; |
1552 | 1 | VP8SSE8x8 = SSE8x8_SSE2; |
1553 | 1 | VP8SSE4x4 = SSE4x4_SSE2; |
1554 | 1 | VP8TDisto4x4 = Disto4x4_SSE2; |
1555 | 1 | VP8TDisto16x16 = Disto16x16_SSE2; |
1556 | 1 | VP8Mean16x4 = Mean16x4_SSE2; |
1557 | 1 | } |
1558 | | |
1559 | | #else // !WEBP_USE_SSE2 |
1560 | | |
1561 | | WEBP_DSP_INIT_STUB(VP8EncDspInitSSE2) |
1562 | | |
1563 | | #endif // WEBP_USE_SSE2 |