Coverage Report

Created: 2025-06-13 06:49

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