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