Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: third_party/libwebp/dsp/enc_sse2.c

Issue 1546003002: libwebp: update to 0.5.0 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/libwebp/dsp/enc_neon.c ('k') | third_party/libwebp/dsp/enc_sse41.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 Google Inc. All Rights Reserved. 1 // Copyright 2011 Google Inc. All Rights Reserved.
2 // 2 //
3 // Use of this source code is governed by a BSD-style license 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 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 5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may 6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree. 7 // be found in the AUTHORS file in the root of the source tree.
8 // ----------------------------------------------------------------------------- 8 // -----------------------------------------------------------------------------
9 // 9 //
10 // SSE2 version of speed-critical encoding functions. 10 // SSE2 version of speed-critical encoding functions.
11 // 11 //
12 // Author: Christian Duvivier (cduvivier@google.com) 12 // Author: Christian Duvivier (cduvivier@google.com)
13 13
14 #include "./dsp.h" 14 #include "./dsp.h"
15 15
16 #if defined(WEBP_USE_SSE2) 16 #if defined(WEBP_USE_SSE2)
17 #include <stdlib.h> // for abs() 17 #include <stdlib.h> // for abs()
18 #include <emmintrin.h> 18 #include <emmintrin.h>
19 19
20 #include "../enc/cost.h" 20 #include "../enc/cost.h"
21 #include "../enc/vp8enci.h" 21 #include "../enc/vp8enci.h"
22 #include "../utils/utils.h"
23 22
24 //------------------------------------------------------------------------------ 23 //------------------------------------------------------------------------------
25 // Quite useful macro for debugging. Left here for convenience. 24 // Quite useful macro for debugging. Left here for convenience.
26 25
27 #if 0 26 #if 0
28 #include <stdio.h> 27 #include <stdio.h>
29 static void PrintReg(const __m128i r, const char* const name, int size) { 28 static void PrintReg(const __m128i r, const char* const name, int size) {
30 int n; 29 int n;
31 union { 30 union {
32 __m128i r; 31 __m128i r;
33 uint8_t i8[16]; 32 uint8_t i8[16];
34 uint16_t i16[8]; 33 uint16_t i16[8];
35 uint32_t i32[4]; 34 uint32_t i32[4];
36 uint64_t i64[2]; 35 uint64_t i64[2];
37 } tmp; 36 } tmp;
38 tmp.r = r; 37 tmp.r = r;
39 printf("%s\t: ", name); 38 fprintf(stderr, "%s\t: ", name);
40 if (size == 8) { 39 if (size == 8) {
41 for (n = 0; n < 16; ++n) printf("%.2x ", tmp.i8[n]); 40 for (n = 0; n < 16; ++n) fprintf(stderr, "%.2x ", tmp.i8[n]);
42 } else if (size == 16) { 41 } else if (size == 16) {
43 for (n = 0; n < 8; ++n) printf("%.4x ", tmp.i16[n]); 42 for (n = 0; n < 8; ++n) fprintf(stderr, "%.4x ", tmp.i16[n]);
44 } else if (size == 32) { 43 } else if (size == 32) {
45 for (n = 0; n < 4; ++n) printf("%.8x ", tmp.i32[n]); 44 for (n = 0; n < 4; ++n) fprintf(stderr, "%.8x ", tmp.i32[n]);
46 } else { 45 } else {
47 for (n = 0; n < 2; ++n) printf("%.16lx ", tmp.i64[n]); 46 for (n = 0; n < 2; ++n) fprintf(stderr, "%.16lx ", tmp.i64[n]);
48 } 47 }
49 printf("\n"); 48 fprintf(stderr, "\n");
50 } 49 }
51 #endif 50 #endif
52 51
53 //------------------------------------------------------------------------------ 52 //------------------------------------------------------------------------------
54 // Compute susceptibility based on DCT-coeff histograms:
55 // the higher, the "easier" the macroblock is to compress.
56
57 static void CollectHistogram(const uint8_t* ref, const uint8_t* pred,
58 int start_block, int end_block,
59 VP8Histogram* const histo) {
60 const __m128i max_coeff_thresh = _mm_set1_epi16(MAX_COEFF_THRESH);
61 int j;
62 for (j = start_block; j < end_block; ++j) {
63 int16_t out[16];
64 int k;
65
66 VP8FTransform(ref + VP8DspScan[j], pred + VP8DspScan[j], out);
67
68 // Convert coefficients to bin (within out[]).
69 {
70 // Load.
71 const __m128i out0 = _mm_loadu_si128((__m128i*)&out[0]);
72 const __m128i out1 = _mm_loadu_si128((__m128i*)&out[8]);
73 // sign(out) = out >> 15 (0x0000 if positive, 0xffff if negative)
74 const __m128i sign0 = _mm_srai_epi16(out0, 15);
75 const __m128i sign1 = _mm_srai_epi16(out1, 15);
76 // abs(out) = (out ^ sign) - sign
77 const __m128i xor0 = _mm_xor_si128(out0, sign0);
78 const __m128i xor1 = _mm_xor_si128(out1, sign1);
79 const __m128i abs0 = _mm_sub_epi16(xor0, sign0);
80 const __m128i abs1 = _mm_sub_epi16(xor1, sign1);
81 // v = abs(out) >> 3
82 const __m128i v0 = _mm_srai_epi16(abs0, 3);
83 const __m128i v1 = _mm_srai_epi16(abs1, 3);
84 // bin = min(v, MAX_COEFF_THRESH)
85 const __m128i bin0 = _mm_min_epi16(v0, max_coeff_thresh);
86 const __m128i bin1 = _mm_min_epi16(v1, max_coeff_thresh);
87 // Store.
88 _mm_storeu_si128((__m128i*)&out[0], bin0);
89 _mm_storeu_si128((__m128i*)&out[8], bin1);
90 }
91
92 // Convert coefficients to bin.
93 for (k = 0; k < 16; ++k) {
94 histo->distribution[out[k]]++;
95 }
96 }
97 }
98
99 //------------------------------------------------------------------------------
100 // Transforms (Paragraph 14.4) 53 // Transforms (Paragraph 14.4)
101 54
102 // Does one or two inverse transforms. 55 // Does one or two inverse transforms.
103 static void ITransform(const uint8_t* ref, const int16_t* in, uint8_t* dst, 56 static void ITransform(const uint8_t* ref, const int16_t* in, uint8_t* dst,
104 int do_two) { 57 int do_two) {
105 // This implementation makes use of 16-bit fixed point versions of two 58 // This implementation makes use of 16-bit fixed point versions of two
106 // multiply constants: 59 // multiply constants:
107 // K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^16 60 // K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^16
108 // K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^16 61 // K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^16
109 // 62 //
(...skipping 11 matching lines...) Expand all
121 const __m128i k1 = _mm_set1_epi16(20091); 74 const __m128i k1 = _mm_set1_epi16(20091);
122 const __m128i k2 = _mm_set1_epi16(-30068); 75 const __m128i k2 = _mm_set1_epi16(-30068);
123 __m128i T0, T1, T2, T3; 76 __m128i T0, T1, T2, T3;
124 77
125 // Load and concatenate the transform coefficients (we'll do two inverse 78 // Load and concatenate the transform coefficients (we'll do two inverse
126 // transforms in parallel). In the case of only one inverse transform, the 79 // transforms in parallel). In the case of only one inverse transform, the
127 // second half of the vectors will just contain random value we'll never 80 // second half of the vectors will just contain random value we'll never
128 // use nor store. 81 // use nor store.
129 __m128i in0, in1, in2, in3; 82 __m128i in0, in1, in2, in3;
130 { 83 {
131 in0 = _mm_loadl_epi64((__m128i*)&in[0]); 84 in0 = _mm_loadl_epi64((const __m128i*)&in[0]);
132 in1 = _mm_loadl_epi64((__m128i*)&in[4]); 85 in1 = _mm_loadl_epi64((const __m128i*)&in[4]);
133 in2 = _mm_loadl_epi64((__m128i*)&in[8]); 86 in2 = _mm_loadl_epi64((const __m128i*)&in[8]);
134 in3 = _mm_loadl_epi64((__m128i*)&in[12]); 87 in3 = _mm_loadl_epi64((const __m128i*)&in[12]);
135 // a00 a10 a20 a30 x x x x 88 // a00 a10 a20 a30 x x x x
136 // a01 a11 a21 a31 x x x x 89 // a01 a11 a21 a31 x x x x
137 // a02 a12 a22 a32 x x x x 90 // a02 a12 a22 a32 x x x x
138 // a03 a13 a23 a33 x x x x 91 // a03 a13 a23 a33 x x x x
139 if (do_two) { 92 if (do_two) {
140 const __m128i inB0 = _mm_loadl_epi64((__m128i*)&in[16]); 93 const __m128i inB0 = _mm_loadl_epi64((const __m128i*)&in[16]);
141 const __m128i inB1 = _mm_loadl_epi64((__m128i*)&in[20]); 94 const __m128i inB1 = _mm_loadl_epi64((const __m128i*)&in[20]);
142 const __m128i inB2 = _mm_loadl_epi64((__m128i*)&in[24]); 95 const __m128i inB2 = _mm_loadl_epi64((const __m128i*)&in[24]);
143 const __m128i inB3 = _mm_loadl_epi64((__m128i*)&in[28]); 96 const __m128i inB3 = _mm_loadl_epi64((const __m128i*)&in[28]);
144 in0 = _mm_unpacklo_epi64(in0, inB0); 97 in0 = _mm_unpacklo_epi64(in0, inB0);
145 in1 = _mm_unpacklo_epi64(in1, inB1); 98 in1 = _mm_unpacklo_epi64(in1, inB1);
146 in2 = _mm_unpacklo_epi64(in2, inB2); 99 in2 = _mm_unpacklo_epi64(in2, inB2);
147 in3 = _mm_unpacklo_epi64(in3, inB3); 100 in3 = _mm_unpacklo_epi64(in3, inB3);
148 // a00 a10 a20 a30 b00 b10 b20 b30 101 // a00 a10 a20 a30 b00 b10 b20 b30
149 // a01 a11 a21 a31 b01 b11 b21 b31 102 // a01 a11 a21 a31 b01 b11 b21 b31
150 // a02 a12 a22 a32 b02 b12 b22 b32 103 // a02 a12 a22 a32 b02 b12 b22 b32
151 // a03 a13 a23 a33 b03 b13 b23 b33 104 // a03 a13 a23 a33 b03 b13 b23 b33
152 } 105 }
153 } 106 }
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 // a03 a13 a23 a33 b03 b13 b23 b33 223 // a03 a13 a23 a33 b03 b13 b23 b33
271 } 224 }
272 225
273 // Add inverse transform to 'ref' and store. 226 // Add inverse transform to 'ref' and store.
274 { 227 {
275 const __m128i zero = _mm_setzero_si128(); 228 const __m128i zero = _mm_setzero_si128();
276 // Load the reference(s). 229 // Load the reference(s).
277 __m128i ref0, ref1, ref2, ref3; 230 __m128i ref0, ref1, ref2, ref3;
278 if (do_two) { 231 if (do_two) {
279 // Load eight bytes/pixels per line. 232 // Load eight bytes/pixels per line.
280 ref0 = _mm_loadl_epi64((__m128i*)&ref[0 * BPS]); 233 ref0 = _mm_loadl_epi64((const __m128i*)&ref[0 * BPS]);
281 ref1 = _mm_loadl_epi64((__m128i*)&ref[1 * BPS]); 234 ref1 = _mm_loadl_epi64((const __m128i*)&ref[1 * BPS]);
282 ref2 = _mm_loadl_epi64((__m128i*)&ref[2 * BPS]); 235 ref2 = _mm_loadl_epi64((const __m128i*)&ref[2 * BPS]);
283 ref3 = _mm_loadl_epi64((__m128i*)&ref[3 * BPS]); 236 ref3 = _mm_loadl_epi64((const __m128i*)&ref[3 * BPS]);
284 } else { 237 } else {
285 // Load four bytes/pixels per line. 238 // Load four bytes/pixels per line.
286 ref0 = _mm_cvtsi32_si128(*(int*)&ref[0 * BPS]); 239 ref0 = _mm_cvtsi32_si128(WebPMemToUint32(&ref[0 * BPS]));
287 ref1 = _mm_cvtsi32_si128(*(int*)&ref[1 * BPS]); 240 ref1 = _mm_cvtsi32_si128(WebPMemToUint32(&ref[1 * BPS]));
288 ref2 = _mm_cvtsi32_si128(*(int*)&ref[2 * BPS]); 241 ref2 = _mm_cvtsi32_si128(WebPMemToUint32(&ref[2 * BPS]));
289 ref3 = _mm_cvtsi32_si128(*(int*)&ref[3 * BPS]); 242 ref3 = _mm_cvtsi32_si128(WebPMemToUint32(&ref[3 * BPS]));
290 } 243 }
291 // Convert to 16b. 244 // Convert to 16b.
292 ref0 = _mm_unpacklo_epi8(ref0, zero); 245 ref0 = _mm_unpacklo_epi8(ref0, zero);
293 ref1 = _mm_unpacklo_epi8(ref1, zero); 246 ref1 = _mm_unpacklo_epi8(ref1, zero);
294 ref2 = _mm_unpacklo_epi8(ref2, zero); 247 ref2 = _mm_unpacklo_epi8(ref2, zero);
295 ref3 = _mm_unpacklo_epi8(ref3, zero); 248 ref3 = _mm_unpacklo_epi8(ref3, zero);
296 // Add the inverse transform(s). 249 // Add the inverse transform(s).
297 ref0 = _mm_add_epi16(ref0, T0); 250 ref0 = _mm_add_epi16(ref0, T0);
298 ref1 = _mm_add_epi16(ref1, T1); 251 ref1 = _mm_add_epi16(ref1, T1);
299 ref2 = _mm_add_epi16(ref2, T2); 252 ref2 = _mm_add_epi16(ref2, T2);
300 ref3 = _mm_add_epi16(ref3, T3); 253 ref3 = _mm_add_epi16(ref3, T3);
301 // Unsigned saturate to 8b. 254 // Unsigned saturate to 8b.
302 ref0 = _mm_packus_epi16(ref0, ref0); 255 ref0 = _mm_packus_epi16(ref0, ref0);
303 ref1 = _mm_packus_epi16(ref1, ref1); 256 ref1 = _mm_packus_epi16(ref1, ref1);
304 ref2 = _mm_packus_epi16(ref2, ref2); 257 ref2 = _mm_packus_epi16(ref2, ref2);
305 ref3 = _mm_packus_epi16(ref3, ref3); 258 ref3 = _mm_packus_epi16(ref3, ref3);
306 // Store the results. 259 // Store the results.
307 if (do_two) { 260 if (do_two) {
308 // Store eight bytes/pixels per line. 261 // Store eight bytes/pixels per line.
309 _mm_storel_epi64((__m128i*)&dst[0 * BPS], ref0); 262 _mm_storel_epi64((__m128i*)&dst[0 * BPS], ref0);
310 _mm_storel_epi64((__m128i*)&dst[1 * BPS], ref1); 263 _mm_storel_epi64((__m128i*)&dst[1 * BPS], ref1);
311 _mm_storel_epi64((__m128i*)&dst[2 * BPS], ref2); 264 _mm_storel_epi64((__m128i*)&dst[2 * BPS], ref2);
312 _mm_storel_epi64((__m128i*)&dst[3 * BPS], ref3); 265 _mm_storel_epi64((__m128i*)&dst[3 * BPS], ref3);
313 } else { 266 } else {
314 // Store four bytes/pixels per line. 267 // Store four bytes/pixels per line.
315 *((int32_t *)&dst[0 * BPS]) = _mm_cvtsi128_si32(ref0); 268 WebPUint32ToMem(&dst[0 * BPS], _mm_cvtsi128_si32(ref0));
316 *((int32_t *)&dst[1 * BPS]) = _mm_cvtsi128_si32(ref1); 269 WebPUint32ToMem(&dst[1 * BPS], _mm_cvtsi128_si32(ref1));
317 *((int32_t *)&dst[2 * BPS]) = _mm_cvtsi128_si32(ref2); 270 WebPUint32ToMem(&dst[2 * BPS], _mm_cvtsi128_si32(ref2));
318 *((int32_t *)&dst[3 * BPS]) = _mm_cvtsi128_si32(ref3); 271 WebPUint32ToMem(&dst[3 * BPS], _mm_cvtsi128_si32(ref3));
319 } 272 }
320 } 273 }
321 } 274 }
322 275
323 static void FTransform(const uint8_t* src, const uint8_t* ref, int16_t* out) { 276 static void FTransformPass1(const __m128i* const in01,
324 const __m128i zero = _mm_setzero_si128(); 277 const __m128i* const in23,
325 const __m128i seven = _mm_set1_epi16(7); 278 __m128i* const out01,
279 __m128i* const out32) {
326 const __m128i k937 = _mm_set1_epi32(937); 280 const __m128i k937 = _mm_set1_epi32(937);
327 const __m128i k1812 = _mm_set1_epi32(1812); 281 const __m128i k1812 = _mm_set1_epi32(1812);
328 const __m128i k51000 = _mm_set1_epi32(51000); 282
329 const __m128i k12000_plus_one = _mm_set1_epi32(12000 + (1 << 16));
330 const __m128i k5352_2217 = _mm_set_epi16(5352, 2217, 5352, 2217,
331 5352, 2217, 5352, 2217);
332 const __m128i k2217_5352 = _mm_set_epi16(2217, -5352, 2217, -5352,
333 2217, -5352, 2217, -5352);
334 const __m128i k88p = _mm_set_epi16(8, 8, 8, 8, 8, 8, 8, 8); 283 const __m128i k88p = _mm_set_epi16(8, 8, 8, 8, 8, 8, 8, 8);
335 const __m128i k88m = _mm_set_epi16(-8, 8, -8, 8, -8, 8, -8, 8); 284 const __m128i k88m = _mm_set_epi16(-8, 8, -8, 8, -8, 8, -8, 8);
336 const __m128i k5352_2217p = _mm_set_epi16(2217, 5352, 2217, 5352, 285 const __m128i k5352_2217p = _mm_set_epi16(2217, 5352, 2217, 5352,
337 2217, 5352, 2217, 5352); 286 2217, 5352, 2217, 5352);
338 const __m128i k5352_2217m = _mm_set_epi16(-5352, 2217, -5352, 2217, 287 const __m128i k5352_2217m = _mm_set_epi16(-5352, 2217, -5352, 2217,
339 -5352, 2217, -5352, 2217); 288 -5352, 2217, -5352, 2217);
289
290 // *in01 = 00 01 10 11 02 03 12 13
291 // *in23 = 20 21 30 31 22 23 32 33
292 const __m128i shuf01_p = _mm_shufflehi_epi16(*in01, _MM_SHUFFLE(2, 3, 0, 1));
293 const __m128i shuf23_p = _mm_shufflehi_epi16(*in23, _MM_SHUFFLE(2, 3, 0, 1));
294 // 00 01 10 11 03 02 13 12
295 // 20 21 30 31 23 22 33 32
296 const __m128i s01 = _mm_unpacklo_epi64(shuf01_p, shuf23_p);
297 const __m128i s32 = _mm_unpackhi_epi64(shuf01_p, shuf23_p);
298 // 00 01 10 11 20 21 30 31
299 // 03 02 13 12 23 22 33 32
300 const __m128i a01 = _mm_add_epi16(s01, s32);
301 const __m128i a32 = _mm_sub_epi16(s01, s32);
302 // [d0 + d3 | d1 + d2 | ...] = [a0 a1 | a0' a1' | ... ]
303 // [d0 - d3 | d1 - d2 | ...] = [a3 a2 | a3' a2' | ... ]
304
305 const __m128i tmp0 = _mm_madd_epi16(a01, k88p); // [ (a0 + a1) << 3, ... ]
306 const __m128i tmp2 = _mm_madd_epi16(a01, k88m); // [ (a0 - a1) << 3, ... ]
307 const __m128i tmp1_1 = _mm_madd_epi16(a32, k5352_2217p);
308 const __m128i tmp3_1 = _mm_madd_epi16(a32, k5352_2217m);
309 const __m128i tmp1_2 = _mm_add_epi32(tmp1_1, k1812);
310 const __m128i tmp3_2 = _mm_add_epi32(tmp3_1, k937);
311 const __m128i tmp1 = _mm_srai_epi32(tmp1_2, 9);
312 const __m128i tmp3 = _mm_srai_epi32(tmp3_2, 9);
313 const __m128i s03 = _mm_packs_epi32(tmp0, tmp2);
314 const __m128i s12 = _mm_packs_epi32(tmp1, tmp3);
315 const __m128i s_lo = _mm_unpacklo_epi16(s03, s12); // 0 1 0 1 0 1...
316 const __m128i s_hi = _mm_unpackhi_epi16(s03, s12); // 2 3 2 3 2 3
317 const __m128i v23 = _mm_unpackhi_epi32(s_lo, s_hi);
318 *out01 = _mm_unpacklo_epi32(s_lo, s_hi);
319 *out32 = _mm_shuffle_epi32(v23, _MM_SHUFFLE(1, 0, 3, 2)); // 3 2 3 2 3 2..
320 }
321
322 static void FTransformPass2(const __m128i* const v01, const __m128i* const v32,
323 int16_t* out) {
324 const __m128i zero = _mm_setzero_si128();
325 const __m128i seven = _mm_set1_epi16(7);
326 const __m128i k5352_2217 = _mm_set_epi16(5352, 2217, 5352, 2217,
327 5352, 2217, 5352, 2217);
328 const __m128i k2217_5352 = _mm_set_epi16(2217, -5352, 2217, -5352,
329 2217, -5352, 2217, -5352);
330 const __m128i k12000_plus_one = _mm_set1_epi32(12000 + (1 << 16));
331 const __m128i k51000 = _mm_set1_epi32(51000);
332
333 // Same operations are done on the (0,3) and (1,2) pairs.
334 // a0 = v0 + v3
335 // a1 = v1 + v2
336 // a3 = v0 - v3
337 // a2 = v1 - v2
338 const __m128i a01 = _mm_add_epi16(*v01, *v32);
339 const __m128i a32 = _mm_sub_epi16(*v01, *v32);
340 const __m128i a11 = _mm_unpackhi_epi64(a01, a01);
341 const __m128i a22 = _mm_unpackhi_epi64(a32, a32);
342 const __m128i a01_plus_7 = _mm_add_epi16(a01, seven);
343
344 // d0 = (a0 + a1 + 7) >> 4;
345 // d2 = (a0 - a1 + 7) >> 4;
346 const __m128i c0 = _mm_add_epi16(a01_plus_7, a11);
347 const __m128i c2 = _mm_sub_epi16(a01_plus_7, a11);
348 const __m128i d0 = _mm_srai_epi16(c0, 4);
349 const __m128i d2 = _mm_srai_epi16(c2, 4);
350
351 // f1 = ((b3 * 5352 + b2 * 2217 + 12000) >> 16)
352 // f3 = ((b3 * 2217 - b2 * 5352 + 51000) >> 16)
353 const __m128i b23 = _mm_unpacklo_epi16(a22, a32);
354 const __m128i c1 = _mm_madd_epi16(b23, k5352_2217);
355 const __m128i c3 = _mm_madd_epi16(b23, k2217_5352);
356 const __m128i d1 = _mm_add_epi32(c1, k12000_plus_one);
357 const __m128i d3 = _mm_add_epi32(c3, k51000);
358 const __m128i e1 = _mm_srai_epi32(d1, 16);
359 const __m128i e3 = _mm_srai_epi32(d3, 16);
360 const __m128i f1 = _mm_packs_epi32(e1, e1);
361 const __m128i f3 = _mm_packs_epi32(e3, e3);
362 // f1 = f1 + (a3 != 0);
363 // The compare will return (0xffff, 0) for (==0, !=0). To turn that into the
364 // desired (0, 1), we add one earlier through k12000_plus_one.
365 // -> f1 = f1 + 1 - (a3 == 0)
366 const __m128i g1 = _mm_add_epi16(f1, _mm_cmpeq_epi16(a32, zero));
367
368 const __m128i d0_g1 = _mm_unpacklo_epi64(d0, g1);
369 const __m128i d2_f3 = _mm_unpacklo_epi64(d2, f3);
370 _mm_storeu_si128((__m128i*)&out[0], d0_g1);
371 _mm_storeu_si128((__m128i*)&out[8], d2_f3);
372 }
373
374 static void FTransform(const uint8_t* src, const uint8_t* ref, int16_t* out) {
375 const __m128i zero = _mm_setzero_si128();
376
377 // Load src and convert to 16b.
378 const __m128i src0 = _mm_loadl_epi64((const __m128i*)&src[0 * BPS]);
379 const __m128i src1 = _mm_loadl_epi64((const __m128i*)&src[1 * BPS]);
380 const __m128i src2 = _mm_loadl_epi64((const __m128i*)&src[2 * BPS]);
381 const __m128i src3 = _mm_loadl_epi64((const __m128i*)&src[3 * BPS]);
382 const __m128i src_0 = _mm_unpacklo_epi8(src0, zero);
383 const __m128i src_1 = _mm_unpacklo_epi8(src1, zero);
384 const __m128i src_2 = _mm_unpacklo_epi8(src2, zero);
385 const __m128i src_3 = _mm_unpacklo_epi8(src3, zero);
386 // Load ref and convert to 16b.
387 const __m128i ref0 = _mm_loadl_epi64((const __m128i*)&ref[0 * BPS]);
388 const __m128i ref1 = _mm_loadl_epi64((const __m128i*)&ref[1 * BPS]);
389 const __m128i ref2 = _mm_loadl_epi64((const __m128i*)&ref[2 * BPS]);
390 const __m128i ref3 = _mm_loadl_epi64((const __m128i*)&ref[3 * BPS]);
391 const __m128i ref_0 = _mm_unpacklo_epi8(ref0, zero);
392 const __m128i ref_1 = _mm_unpacklo_epi8(ref1, zero);
393 const __m128i ref_2 = _mm_unpacklo_epi8(ref2, zero);
394 const __m128i ref_3 = _mm_unpacklo_epi8(ref3, zero);
395 // Compute difference. -> 00 01 02 03 00 00 00 00
396 const __m128i diff0 = _mm_sub_epi16(src_0, ref_0);
397 const __m128i diff1 = _mm_sub_epi16(src_1, ref_1);
398 const __m128i diff2 = _mm_sub_epi16(src_2, ref_2);
399 const __m128i diff3 = _mm_sub_epi16(src_3, ref_3);
400
401 // Unpack and shuffle
402 // 00 01 02 03 0 0 0 0
403 // 10 11 12 13 0 0 0 0
404 // 20 21 22 23 0 0 0 0
405 // 30 31 32 33 0 0 0 0
406 const __m128i shuf01 = _mm_unpacklo_epi32(diff0, diff1);
407 const __m128i shuf23 = _mm_unpacklo_epi32(diff2, diff3);
340 __m128i v01, v32; 408 __m128i v01, v32;
341 409
342 410 // First pass
343 // Difference between src and ref and initial transpose. 411 FTransformPass1(&shuf01, &shuf23, &v01, &v32);
412
413 // Second pass
414 FTransformPass2(&v01, &v32, out);
415 }
416
417 static void FTransform2(const uint8_t* src, const uint8_t* ref, int16_t* out) {
418 const __m128i zero = _mm_setzero_si128();
419
420 // Load src and convert to 16b.
421 const __m128i src0 = _mm_loadl_epi64((const __m128i*)&src[0 * BPS]);
422 const __m128i src1 = _mm_loadl_epi64((const __m128i*)&src[1 * BPS]);
423 const __m128i src2 = _mm_loadl_epi64((const __m128i*)&src[2 * BPS]);
424 const __m128i src3 = _mm_loadl_epi64((const __m128i*)&src[3 * BPS]);
425 const __m128i src_0 = _mm_unpacklo_epi8(src0, zero);
426 const __m128i src_1 = _mm_unpacklo_epi8(src1, zero);
427 const __m128i src_2 = _mm_unpacklo_epi8(src2, zero);
428 const __m128i src_3 = _mm_unpacklo_epi8(src3, zero);
429 // Load ref and convert to 16b.
430 const __m128i ref0 = _mm_loadl_epi64((const __m128i*)&ref[0 * BPS]);
431 const __m128i ref1 = _mm_loadl_epi64((const __m128i*)&ref[1 * BPS]);
432 const __m128i ref2 = _mm_loadl_epi64((const __m128i*)&ref[2 * BPS]);
433 const __m128i ref3 = _mm_loadl_epi64((const __m128i*)&ref[3 * BPS]);
434 const __m128i ref_0 = _mm_unpacklo_epi8(ref0, zero);
435 const __m128i ref_1 = _mm_unpacklo_epi8(ref1, zero);
436 const __m128i ref_2 = _mm_unpacklo_epi8(ref2, zero);
437 const __m128i ref_3 = _mm_unpacklo_epi8(ref3, zero);
438 // Compute difference. -> 00 01 02 03 00' 01' 02' 03'
439 const __m128i diff0 = _mm_sub_epi16(src_0, ref_0);
440 const __m128i diff1 = _mm_sub_epi16(src_1, ref_1);
441 const __m128i diff2 = _mm_sub_epi16(src_2, ref_2);
442 const __m128i diff3 = _mm_sub_epi16(src_3, ref_3);
443
444 // Unpack and shuffle
445 // 00 01 02 03 0 0 0 0
446 // 10 11 12 13 0 0 0 0
447 // 20 21 22 23 0 0 0 0
448 // 30 31 32 33 0 0 0 0
449 const __m128i shuf01l = _mm_unpacklo_epi32(diff0, diff1);
450 const __m128i shuf23l = _mm_unpacklo_epi32(diff2, diff3);
451 const __m128i shuf01h = _mm_unpackhi_epi32(diff0, diff1);
452 const __m128i shuf23h = _mm_unpackhi_epi32(diff2, diff3);
453 __m128i v01l, v32l;
454 __m128i v01h, v32h;
455
456 // First pass
457 FTransformPass1(&shuf01l, &shuf23l, &v01l, &v32l);
458 FTransformPass1(&shuf01h, &shuf23h, &v01h, &v32h);
459
460 // Second pass
461 FTransformPass2(&v01l, &v32l, out + 0);
462 FTransformPass2(&v01h, &v32h, out + 16);
463 }
464
465 static void FTransformWHTRow(const int16_t* const in, __m128i* const out) {
466 const __m128i kMult1 = _mm_set_epi16(0, 0, 0, 0, 1, 1, 1, 1);
467 const __m128i kMult2 = _mm_set_epi16(0, 0, 0, 0, -1, 1, -1, 1);
468 const __m128i src0 = _mm_loadl_epi64((__m128i*)&in[0 * 16]);
469 const __m128i src1 = _mm_loadl_epi64((__m128i*)&in[1 * 16]);
470 const __m128i src2 = _mm_loadl_epi64((__m128i*)&in[2 * 16]);
471 const __m128i src3 = _mm_loadl_epi64((__m128i*)&in[3 * 16]);
472 const __m128i A01 = _mm_unpacklo_epi16(src0, src1); // A0 A1 | ...
473 const __m128i A23 = _mm_unpacklo_epi16(src2, src3); // A2 A3 | ...
474 const __m128i B0 = _mm_adds_epi16(A01, A23); // a0 | a1 | ...
475 const __m128i B1 = _mm_subs_epi16(A01, A23); // a3 | a2 | ...
476 const __m128i C0 = _mm_unpacklo_epi32(B0, B1); // a0 | a1 | a3 | a2
477 const __m128i C1 = _mm_unpacklo_epi32(B1, B0); // a3 | a2 | a0 | a1
478 const __m128i D0 = _mm_madd_epi16(C0, kMult1); // out0, out1
479 const __m128i D1 = _mm_madd_epi16(C1, kMult2); // out2, out3
480 *out = _mm_unpacklo_epi64(D0, D1);
481 }
482
483 static void FTransformWHT(const int16_t* in, int16_t* out) {
484 __m128i row0, row1, row2, row3;
485 FTransformWHTRow(in + 0 * 64, &row0);
486 FTransformWHTRow(in + 1 * 64, &row1);
487 FTransformWHTRow(in + 2 * 64, &row2);
488 FTransformWHTRow(in + 3 * 64, &row3);
489
344 { 490 {
345 // Load src and convert to 16b. 491 const __m128i a0 = _mm_add_epi32(row0, row2);
346 const __m128i src0 = _mm_loadl_epi64((__m128i*)&src[0 * BPS]); 492 const __m128i a1 = _mm_add_epi32(row1, row3);
347 const __m128i src1 = _mm_loadl_epi64((__m128i*)&src[1 * BPS]); 493 const __m128i a2 = _mm_sub_epi32(row1, row3);
348 const __m128i src2 = _mm_loadl_epi64((__m128i*)&src[2 * BPS]); 494 const __m128i a3 = _mm_sub_epi32(row0, row2);
349 const __m128i src3 = _mm_loadl_epi64((__m128i*)&src[3 * BPS]);
350 const __m128i src_0 = _mm_unpacklo_epi8(src0, zero);
351 const __m128i src_1 = _mm_unpacklo_epi8(src1, zero);
352 const __m128i src_2 = _mm_unpacklo_epi8(src2, zero);
353 const __m128i src_3 = _mm_unpacklo_epi8(src3, zero);
354 // Load ref and convert to 16b.
355 const __m128i ref0 = _mm_loadl_epi64((__m128i*)&ref[0 * BPS]);
356 const __m128i ref1 = _mm_loadl_epi64((__m128i*)&ref[1 * BPS]);
357 const __m128i ref2 = _mm_loadl_epi64((__m128i*)&ref[2 * BPS]);
358 const __m128i ref3 = _mm_loadl_epi64((__m128i*)&ref[3 * BPS]);
359 const __m128i ref_0 = _mm_unpacklo_epi8(ref0, zero);
360 const __m128i ref_1 = _mm_unpacklo_epi8(ref1, zero);
361 const __m128i ref_2 = _mm_unpacklo_epi8(ref2, zero);
362 const __m128i ref_3 = _mm_unpacklo_epi8(ref3, zero);
363 // Compute difference. -> 00 01 02 03 00 00 00 00
364 const __m128i diff0 = _mm_sub_epi16(src_0, ref_0);
365 const __m128i diff1 = _mm_sub_epi16(src_1, ref_1);
366 const __m128i diff2 = _mm_sub_epi16(src_2, ref_2);
367 const __m128i diff3 = _mm_sub_epi16(src_3, ref_3);
368
369
370 // Unpack and shuffle
371 // 00 01 02 03 0 0 0 0
372 // 10 11 12 13 0 0 0 0
373 // 20 21 22 23 0 0 0 0
374 // 30 31 32 33 0 0 0 0
375 const __m128i shuf01 = _mm_unpacklo_epi32(diff0, diff1);
376 const __m128i shuf23 = _mm_unpacklo_epi32(diff2, diff3);
377 // 00 01 10 11 02 03 12 13
378 // 20 21 30 31 22 23 32 33
379 const __m128i shuf01_p =
380 _mm_shufflehi_epi16(shuf01, _MM_SHUFFLE(2, 3, 0, 1));
381 const __m128i shuf23_p =
382 _mm_shufflehi_epi16(shuf23, _MM_SHUFFLE(2, 3, 0, 1));
383 // 00 01 10 11 03 02 13 12
384 // 20 21 30 31 23 22 33 32
385 const __m128i s01 = _mm_unpacklo_epi64(shuf01_p, shuf23_p);
386 const __m128i s32 = _mm_unpackhi_epi64(shuf01_p, shuf23_p);
387 // 00 01 10 11 20 21 30 31
388 // 03 02 13 12 23 22 33 32
389 const __m128i a01 = _mm_add_epi16(s01, s32);
390 const __m128i a32 = _mm_sub_epi16(s01, s32);
391 // [d0 + d3 | d1 + d2 | ...] = [a0 a1 | a0' a1' | ... ]
392 // [d0 - d3 | d1 - d2 | ...] = [a3 a2 | a3' a2' | ... ]
393
394 const __m128i tmp0 = _mm_madd_epi16(a01, k88p); // [ (a0 + a1) << 3, ... ]
395 const __m128i tmp2 = _mm_madd_epi16(a01, k88m); // [ (a0 - a1) << 3, ... ]
396 const __m128i tmp1_1 = _mm_madd_epi16(a32, k5352_2217p);
397 const __m128i tmp3_1 = _mm_madd_epi16(a32, k5352_2217m);
398 const __m128i tmp1_2 = _mm_add_epi32(tmp1_1, k1812);
399 const __m128i tmp3_2 = _mm_add_epi32(tmp3_1, k937);
400 const __m128i tmp1 = _mm_srai_epi32(tmp1_2, 9);
401 const __m128i tmp3 = _mm_srai_epi32(tmp3_2, 9);
402 const __m128i s03 = _mm_packs_epi32(tmp0, tmp2);
403 const __m128i s12 = _mm_packs_epi32(tmp1, tmp3);
404 const __m128i s_lo = _mm_unpacklo_epi16(s03, s12); // 0 1 0 1 0 1...
405 const __m128i s_hi = _mm_unpackhi_epi16(s03, s12); // 2 3 2 3 2 3
406 const __m128i v23 = _mm_unpackhi_epi32(s_lo, s_hi);
407 v01 = _mm_unpacklo_epi32(s_lo, s_hi);
408 v32 = _mm_shuffle_epi32(v23, _MM_SHUFFLE(1, 0, 3, 2)); // 3 2 3 2 3 2..
409 }
410
411 // Second pass
412 {
413 // Same operations are done on the (0,3) and (1,2) pairs.
414 // a0 = v0 + v3
415 // a1 = v1 + v2
416 // a3 = v0 - v3
417 // a2 = v1 - v2
418 const __m128i a01 = _mm_add_epi16(v01, v32);
419 const __m128i a32 = _mm_sub_epi16(v01, v32);
420 const __m128i a11 = _mm_unpackhi_epi64(a01, a01);
421 const __m128i a22 = _mm_unpackhi_epi64(a32, a32);
422 const __m128i a01_plus_7 = _mm_add_epi16(a01, seven);
423
424 // d0 = (a0 + a1 + 7) >> 4;
425 // d2 = (a0 - a1 + 7) >> 4;
426 const __m128i c0 = _mm_add_epi16(a01_plus_7, a11);
427 const __m128i c2 = _mm_sub_epi16(a01_plus_7, a11);
428 const __m128i d0 = _mm_srai_epi16(c0, 4);
429 const __m128i d2 = _mm_srai_epi16(c2, 4);
430
431 // f1 = ((b3 * 5352 + b2 * 2217 + 12000) >> 16)
432 // f3 = ((b3 * 2217 - b2 * 5352 + 51000) >> 16)
433 const __m128i b23 = _mm_unpacklo_epi16(a22, a32);
434 const __m128i c1 = _mm_madd_epi16(b23, k5352_2217);
435 const __m128i c3 = _mm_madd_epi16(b23, k2217_5352);
436 const __m128i d1 = _mm_add_epi32(c1, k12000_plus_one);
437 const __m128i d3 = _mm_add_epi32(c3, k51000);
438 const __m128i e1 = _mm_srai_epi32(d1, 16);
439 const __m128i e3 = _mm_srai_epi32(d3, 16);
440 const __m128i f1 = _mm_packs_epi32(e1, e1);
441 const __m128i f3 = _mm_packs_epi32(e3, e3);
442 // f1 = f1 + (a3 != 0);
443 // The compare will return (0xffff, 0) for (==0, !=0). To turn that into the
444 // desired (0, 1), we add one earlier through k12000_plus_one.
445 // -> f1 = f1 + 1 - (a3 == 0)
446 const __m128i g1 = _mm_add_epi16(f1, _mm_cmpeq_epi16(a32, zero));
447
448 const __m128i d0_g1 = _mm_unpacklo_epi64(d0, g1);
449 const __m128i d2_f3 = _mm_unpacklo_epi64(d2, f3);
450 _mm_storeu_si128((__m128i*)&out[0], d0_g1);
451 _mm_storeu_si128((__m128i*)&out[8], d2_f3);
452 }
453 }
454
455 static void FTransformWHT(const int16_t* in, int16_t* out) {
456 int32_t tmp[16];
457 int i;
458 for (i = 0; i < 4; ++i, in += 64) {
459 const int a0 = (in[0 * 16] + in[2 * 16]);
460 const int a1 = (in[1 * 16] + in[3 * 16]);
461 const int a2 = (in[1 * 16] - in[3 * 16]);
462 const int a3 = (in[0 * 16] - in[2 * 16]);
463 tmp[0 + i * 4] = a0 + a1;
464 tmp[1 + i * 4] = a3 + a2;
465 tmp[2 + i * 4] = a3 - a2;
466 tmp[3 + i * 4] = a0 - a1;
467 }
468 {
469 const __m128i src0 = _mm_loadu_si128((__m128i*)&tmp[0]);
470 const __m128i src1 = _mm_loadu_si128((__m128i*)&tmp[4]);
471 const __m128i src2 = _mm_loadu_si128((__m128i*)&tmp[8]);
472 const __m128i src3 = _mm_loadu_si128((__m128i*)&tmp[12]);
473 const __m128i a0 = _mm_add_epi32(src0, src2);
474 const __m128i a1 = _mm_add_epi32(src1, src3);
475 const __m128i a2 = _mm_sub_epi32(src1, src3);
476 const __m128i a3 = _mm_sub_epi32(src0, src2);
477 const __m128i b0 = _mm_srai_epi32(_mm_add_epi32(a0, a1), 1); 495 const __m128i b0 = _mm_srai_epi32(_mm_add_epi32(a0, a1), 1);
478 const __m128i b1 = _mm_srai_epi32(_mm_add_epi32(a3, a2), 1); 496 const __m128i b1 = _mm_srai_epi32(_mm_add_epi32(a3, a2), 1);
479 const __m128i b2 = _mm_srai_epi32(_mm_sub_epi32(a3, a2), 1); 497 const __m128i b2 = _mm_srai_epi32(_mm_sub_epi32(a3, a2), 1);
480 const __m128i b3 = _mm_srai_epi32(_mm_sub_epi32(a0, a1), 1); 498 const __m128i b3 = _mm_srai_epi32(_mm_sub_epi32(a0, a1), 1);
481 const __m128i out0 = _mm_packs_epi32(b0, b1); 499 const __m128i out0 = _mm_packs_epi32(b0, b1);
482 const __m128i out1 = _mm_packs_epi32(b2, b3); 500 const __m128i out1 = _mm_packs_epi32(b2, b3);
483 _mm_storeu_si128((__m128i*)&out[0], out0); 501 _mm_storeu_si128((__m128i*)&out[0], out0);
484 _mm_storeu_si128((__m128i*)&out[8], out1); 502 _mm_storeu_si128((__m128i*)&out[8], out1);
485 } 503 }
486 } 504 }
487 505
488 //------------------------------------------------------------------------------ 506 //------------------------------------------------------------------------------
507 // Compute susceptibility based on DCT-coeff histograms:
508 // the higher, the "easier" the macroblock is to compress.
509
510 static void CollectHistogram(const uint8_t* ref, const uint8_t* pred,
511 int start_block, int end_block,
512 VP8Histogram* const histo) {
513 const __m128i zero = _mm_setzero_si128();
514 const __m128i max_coeff_thresh = _mm_set1_epi16(MAX_COEFF_THRESH);
515 int j;
516 int distribution[MAX_COEFF_THRESH + 1] = { 0 };
517 for (j = start_block; j < end_block; ++j) {
518 int16_t out[16];
519 int k;
520
521 FTransform(ref + VP8DspScan[j], pred + VP8DspScan[j], out);
522
523 // Convert coefficients to bin (within out[]).
524 {
525 // Load.
526 const __m128i out0 = _mm_loadu_si128((__m128i*)&out[0]);
527 const __m128i out1 = _mm_loadu_si128((__m128i*)&out[8]);
528 const __m128i d0 = _mm_sub_epi16(zero, out0);
529 const __m128i d1 = _mm_sub_epi16(zero, out1);
530 const __m128i abs0 = _mm_max_epi16(out0, d0); // abs(v), 16b
531 const __m128i abs1 = _mm_max_epi16(out1, d1);
532 // v = abs(out) >> 3
533 const __m128i v0 = _mm_srai_epi16(abs0, 3);
534 const __m128i v1 = _mm_srai_epi16(abs1, 3);
535 // bin = min(v, MAX_COEFF_THRESH)
536 const __m128i bin0 = _mm_min_epi16(v0, max_coeff_thresh);
537 const __m128i bin1 = _mm_min_epi16(v1, max_coeff_thresh);
538 // Store.
539 _mm_storeu_si128((__m128i*)&out[0], bin0);
540 _mm_storeu_si128((__m128i*)&out[8], bin1);
541 }
542
543 // Convert coefficients to bin.
544 for (k = 0; k < 16; ++k) {
545 ++distribution[out[k]];
546 }
547 }
548 VP8SetHistogramData(distribution, histo);
549 }
550
551 //------------------------------------------------------------------------------
552 // Intra predictions
553
554 // helper for chroma-DC predictions
555 static WEBP_INLINE void Put8x8uv(uint8_t v, uint8_t* dst) {
556 int j;
557 const __m128i values = _mm_set1_epi8(v);
558 for (j = 0; j < 8; ++j) {
559 _mm_storel_epi64((__m128i*)(dst + j * BPS), values);
560 }
561 }
562
563 static WEBP_INLINE void Put16(uint8_t v, uint8_t* dst) {
564 int j;
565 const __m128i values = _mm_set1_epi8(v);
566 for (j = 0; j < 16; ++j) {
567 _mm_store_si128((__m128i*)(dst + j * BPS), values);
568 }
569 }
570
571 static WEBP_INLINE void Fill(uint8_t* dst, int value, int size) {
572 if (size == 4) {
573 int j;
574 for (j = 0; j < 4; ++j) {
575 memset(dst + j * BPS, value, 4);
576 }
577 } else if (size == 8) {
578 Put8x8uv(value, dst);
579 } else {
580 Put16(value, dst);
581 }
582 }
583
584 static WEBP_INLINE void VE8uv(uint8_t* dst, const uint8_t* top) {
585 int j;
586 const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);
587 for (j = 0; j < 8; ++j) {
588 _mm_storel_epi64((__m128i*)(dst + j * BPS), top_values);
589 }
590 }
591
592 static WEBP_INLINE void VE16(uint8_t* dst, const uint8_t* top) {
593 const __m128i top_values = _mm_load_si128((const __m128i*)top);
594 int j;
595 for (j = 0; j < 16; ++j) {
596 _mm_store_si128((__m128i*)(dst + j * BPS), top_values);
597 }
598 }
599
600 static WEBP_INLINE void VerticalPred(uint8_t* dst,
601 const uint8_t* top, int size) {
602 if (top != NULL) {
603 if (size == 8) {
604 VE8uv(dst, top);
605 } else {
606 VE16(dst, top);
607 }
608 } else {
609 Fill(dst, 127, size);
610 }
611 }
612
613 static WEBP_INLINE void HE8uv(uint8_t* dst, const uint8_t* left) {
614 int j;
615 for (j = 0; j < 8; ++j) {
616 const __m128i values = _mm_set1_epi8(left[j]);
617 _mm_storel_epi64((__m128i*)dst, values);
618 dst += BPS;
619 }
620 }
621
622 static WEBP_INLINE void HE16(uint8_t* dst, const uint8_t* left) {
623 int j;
624 for (j = 0; j < 16; ++j) {
625 const __m128i values = _mm_set1_epi8(left[j]);
626 _mm_store_si128((__m128i*)dst, values);
627 dst += BPS;
628 }
629 }
630
631 static WEBP_INLINE void HorizontalPred(uint8_t* dst,
632 const uint8_t* left, int size) {
633 if (left != NULL) {
634 if (size == 8) {
635 HE8uv(dst, left);
636 } else {
637 HE16(dst, left);
638 }
639 } else {
640 Fill(dst, 129, size);
641 }
642 }
643
644 static WEBP_INLINE void TM(uint8_t* dst, const uint8_t* left,
645 const uint8_t* top, int size) {
646 const __m128i zero = _mm_setzero_si128();
647 int y;
648 if (size == 8) {
649 const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);
650 const __m128i top_base = _mm_unpacklo_epi8(top_values, zero);
651 for (y = 0; y < 8; ++y, dst += BPS) {
652 const int val = left[y] - left[-1];
653 const __m128i base = _mm_set1_epi16(val);
654 const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero);
655 _mm_storel_epi64((__m128i*)dst, out);
656 }
657 } else {
658 const __m128i top_values = _mm_load_si128((const __m128i*)top);
659 const __m128i top_base_0 = _mm_unpacklo_epi8(top_values, zero);
660 const __m128i top_base_1 = _mm_unpackhi_epi8(top_values, zero);
661 for (y = 0; y < 16; ++y, dst += BPS) {
662 const int val = left[y] - left[-1];
663 const __m128i base = _mm_set1_epi16(val);
664 const __m128i out_0 = _mm_add_epi16(base, top_base_0);
665 const __m128i out_1 = _mm_add_epi16(base, top_base_1);
666 const __m128i out = _mm_packus_epi16(out_0, out_1);
667 _mm_store_si128((__m128i*)dst, out);
668 }
669 }
670 }
671
672 static WEBP_INLINE void TrueMotion(uint8_t* dst, const uint8_t* left,
673 const uint8_t* top, int size) {
674 if (left != NULL) {
675 if (top != NULL) {
676 TM(dst, left, top, size);
677 } else {
678 HorizontalPred(dst, left, size);
679 }
680 } else {
681 // true motion without left samples (hence: with default 129 value)
682 // is equivalent to VE prediction where you just copy the top samples.
683 // Note that if top samples are not available, the default value is
684 // then 129, and not 127 as in the VerticalPred case.
685 if (top != NULL) {
686 VerticalPred(dst, top, size);
687 } else {
688 Fill(dst, 129, size);
689 }
690 }
691 }
692
693 static WEBP_INLINE void DC8uv(uint8_t* dst, const uint8_t* left,
694 const uint8_t* top) {
695 const __m128i zero = _mm_setzero_si128();
696 const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);
697 const __m128i left_values = _mm_loadl_epi64((const __m128i*)left);
698 const __m128i sum_top = _mm_sad_epu8(top_values, zero);
699 const __m128i sum_left = _mm_sad_epu8(left_values, zero);
700 const int DC = _mm_cvtsi128_si32(sum_top) + _mm_cvtsi128_si32(sum_left) + 8;
701 Put8x8uv(DC >> 4, dst);
702 }
703
704 static WEBP_INLINE void DC8uvNoLeft(uint8_t* dst, const uint8_t* top) {
705 const __m128i zero = _mm_setzero_si128();
706 const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);
707 const __m128i sum = _mm_sad_epu8(top_values, zero);
708 const int DC = _mm_cvtsi128_si32(sum) + 4;
709 Put8x8uv(DC >> 3, dst);
710 }
711
712 static WEBP_INLINE void DC8uvNoTop(uint8_t* dst, const uint8_t* left) {
713 // 'left' is contiguous so we can reuse the top summation.
714 DC8uvNoLeft(dst, left);
715 }
716
717 static WEBP_INLINE void DC8uvNoTopLeft(uint8_t* dst) {
718 Put8x8uv(0x80, dst);
719 }
720
721 static WEBP_INLINE void DC8uvMode(uint8_t* dst, const uint8_t* left,
722 const uint8_t* top) {
723 if (top != NULL) {
724 if (left != NULL) { // top and left present
725 DC8uv(dst, left, top);
726 } else { // top, but no left
727 DC8uvNoLeft(dst, top);
728 }
729 } else if (left != NULL) { // left but no top
730 DC8uvNoTop(dst, left);
731 } else { // no top, no left, nothing.
732 DC8uvNoTopLeft(dst);
733 }
734 }
735
736 static WEBP_INLINE void DC16(uint8_t* dst, const uint8_t* left,
737 const uint8_t* top) {
738 const __m128i zero = _mm_setzero_si128();
739 const __m128i top_row = _mm_load_si128((const __m128i*)top);
740 const __m128i left_row = _mm_load_si128((const __m128i*)left);
741 const __m128i sad8x2 = _mm_sad_epu8(top_row, zero);
742 // sum the two sads: sad8x2[0:1] + sad8x2[8:9]
743 const __m128i sum_top = _mm_add_epi16(sad8x2, _mm_shuffle_epi32(sad8x2, 2));
744 const __m128i sad8x2_left = _mm_sad_epu8(left_row, zero);
745 // sum the two sads: sad8x2[0:1] + sad8x2[8:9]
746 const __m128i sum_left =
747 _mm_add_epi16(sad8x2_left, _mm_shuffle_epi32(sad8x2_left, 2));
748 const int DC = _mm_cvtsi128_si32(sum_top) + _mm_cvtsi128_si32(sum_left) + 16;
749 Put16(DC >> 5, dst);
750 }
751
752 static WEBP_INLINE void DC16NoLeft(uint8_t* dst, const uint8_t* top) {
753 const __m128i zero = _mm_setzero_si128();
754 const __m128i top_row = _mm_load_si128((const __m128i*)top);
755 const __m128i sad8x2 = _mm_sad_epu8(top_row, zero);
756 // sum the two sads: sad8x2[0:1] + sad8x2[8:9]
757 const __m128i sum = _mm_add_epi16(sad8x2, _mm_shuffle_epi32(sad8x2, 2));
758 const int DC = _mm_cvtsi128_si32(sum) + 8;
759 Put16(DC >> 4, dst);
760 }
761
762 static WEBP_INLINE void DC16NoTop(uint8_t* dst, const uint8_t* left) {
763 // 'left' is contiguous so we can reuse the top summation.
764 DC16NoLeft(dst, left);
765 }
766
767 static WEBP_INLINE void DC16NoTopLeft(uint8_t* dst) {
768 Put16(0x80, dst);
769 }
770
771 static WEBP_INLINE void DC16Mode(uint8_t* dst, const uint8_t* left,
772 const uint8_t* top) {
773 if (top != NULL) {
774 if (left != NULL) { // top and left present
775 DC16(dst, left, top);
776 } else { // top, but no left
777 DC16NoLeft(dst, top);
778 }
779 } else if (left != NULL) { // left but no top
780 DC16NoTop(dst, left);
781 } else { // no top, no left, nothing.
782 DC16NoTopLeft(dst);
783 }
784 }
785
786 //------------------------------------------------------------------------------
787 // 4x4 predictions
788
789 #define DST(x, y) dst[(x) + (y) * BPS]
790 #define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)
791 #define AVG2(a, b) (((a) + (b) + 1) >> 1)
792
793 // We use the following 8b-arithmetic tricks:
794 // (a + 2 * b + c + 2) >> 2 = (AC + b + 1) >> 1
795 // where: AC = (a + c) >> 1 = [(a + c + 1) >> 1] - [(a^c) & 1]
796 // and:
797 // (a + 2 * b + c + 2) >> 2 = (AB + BC + 1) >> 1 - (ab|bc)&lsb
798 // where: AC = (a + b + 1) >> 1, BC = (b + c + 1) >> 1
799 // and ab = a ^ b, bc = b ^ c, lsb = (AC^BC)&1
800
801 static WEBP_INLINE void VE4(uint8_t* dst, const uint8_t* top) { // vertical
802 const __m128i one = _mm_set1_epi8(1);
803 const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(top - 1));
804 const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1);
805 const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2);
806 const __m128i a = _mm_avg_epu8(ABCDEFGH, CDEFGH00);
807 const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGH00), one);
808 const __m128i b = _mm_subs_epu8(a, lsb);
809 const __m128i avg = _mm_avg_epu8(b, BCDEFGH0);
810 const uint32_t vals = _mm_cvtsi128_si32(avg);
811 int i;
812 for (i = 0; i < 4; ++i) {
813 WebPUint32ToMem(dst + i * BPS, vals);
814 }
815 }
816
817 static WEBP_INLINE void HE4(uint8_t* dst, const uint8_t* top) { // horizontal
818 const int X = top[-1];
819 const int I = top[-2];
820 const int J = top[-3];
821 const int K = top[-4];
822 const int L = top[-5];
823 WebPUint32ToMem(dst + 0 * BPS, 0x01010101U * AVG3(X, I, J));
824 WebPUint32ToMem(dst + 1 * BPS, 0x01010101U * AVG3(I, J, K));
825 WebPUint32ToMem(dst + 2 * BPS, 0x01010101U * AVG3(J, K, L));
826 WebPUint32ToMem(dst + 3 * BPS, 0x01010101U * AVG3(K, L, L));
827 }
828
829 static WEBP_INLINE void DC4(uint8_t* dst, const uint8_t* top) {
830 uint32_t dc = 4;
831 int i;
832 for (i = 0; i < 4; ++i) dc += top[i] + top[-5 + i];
833 Fill(dst, dc >> 3, 4);
834 }
835
836 static WEBP_INLINE void LD4(uint8_t* dst, const uint8_t* top) { // Down-Left
837 const __m128i one = _mm_set1_epi8(1);
838 const __m128i ABCDEFGH = _mm_loadl_epi64((const __m128i*)top);
839 const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1);
840 const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2);
841 const __m128i CDEFGHH0 = _mm_insert_epi16(CDEFGH00, top[7], 3);
842 const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, CDEFGHH0);
843 const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGHH0), one);
844 const __m128i avg2 = _mm_subs_epu8(avg1, lsb);
845 const __m128i abcdefg = _mm_avg_epu8(avg2, BCDEFGH0);
846 WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( abcdefg ));
847 WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1)));
848 WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2)));
849 WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3)));
850 }
851
852 static WEBP_INLINE void VR4(uint8_t* dst,
853 const uint8_t* top) { // Vertical-Right
854 const __m128i one = _mm_set1_epi8(1);
855 const int I = top[-2];
856 const int J = top[-3];
857 const int K = top[-4];
858 const int X = top[-1];
859 const __m128i XABCD = _mm_loadl_epi64((const __m128i*)(top - 1));
860 const __m128i ABCD0 = _mm_srli_si128(XABCD, 1);
861 const __m128i abcd = _mm_avg_epu8(XABCD, ABCD0);
862 const __m128i _XABCD = _mm_slli_si128(XABCD, 1);
863 const __m128i IXABCD = _mm_insert_epi16(_XABCD, I | (X << 8), 0);
864 const __m128i avg1 = _mm_avg_epu8(IXABCD, ABCD0);
865 const __m128i lsb = _mm_and_si128(_mm_xor_si128(IXABCD, ABCD0), one);
866 const __m128i avg2 = _mm_subs_epu8(avg1, lsb);
867 const __m128i efgh = _mm_avg_epu8(avg2, XABCD);
868 WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( abcd ));
869 WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32( efgh ));
870 WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(abcd, 1)));
871 WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(efgh, 1)));
872
873 // these two are hard to implement in SSE2, so we keep the C-version:
874 DST(0, 2) = AVG3(J, I, X);
875 DST(0, 3) = AVG3(K, J, I);
876 }
877
878 static WEBP_INLINE void VL4(uint8_t* dst,
879 const uint8_t* top) { // Vertical-Left
880 const __m128i one = _mm_set1_epi8(1);
881 const __m128i ABCDEFGH = _mm_loadl_epi64((const __m128i*)top);
882 const __m128i BCDEFGH_ = _mm_srli_si128(ABCDEFGH, 1);
883 const __m128i CDEFGH__ = _mm_srli_si128(ABCDEFGH, 2);
884 const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, BCDEFGH_);
885 const __m128i avg2 = _mm_avg_epu8(CDEFGH__, BCDEFGH_);
886 const __m128i avg3 = _mm_avg_epu8(avg1, avg2);
887 const __m128i lsb1 = _mm_and_si128(_mm_xor_si128(avg1, avg2), one);
888 const __m128i ab = _mm_xor_si128(ABCDEFGH, BCDEFGH_);
889 const __m128i bc = _mm_xor_si128(CDEFGH__, BCDEFGH_);
890 const __m128i abbc = _mm_or_si128(ab, bc);
891 const __m128i lsb2 = _mm_and_si128(abbc, lsb1);
892 const __m128i avg4 = _mm_subs_epu8(avg3, lsb2);
893 const uint32_t extra_out = _mm_cvtsi128_si32(_mm_srli_si128(avg4, 4));
894 WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( avg1 ));
895 WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32( avg4 ));
896 WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg1, 1)));
897 WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg4, 1)));
898
899 // these two are hard to get and irregular
900 DST(3, 2) = (extra_out >> 0) & 0xff;
901 DST(3, 3) = (extra_out >> 8) & 0xff;
902 }
903
904 static WEBP_INLINE void RD4(uint8_t* dst, const uint8_t* top) { // Down-right
905 const __m128i one = _mm_set1_epi8(1);
906 const __m128i LKJIXABC = _mm_loadl_epi64((const __m128i*)(top - 5));
907 const __m128i LKJIXABCD = _mm_insert_epi16(LKJIXABC, top[3], 4);
908 const __m128i KJIXABCD_ = _mm_srli_si128(LKJIXABCD, 1);
909 const __m128i JIXABCD__ = _mm_srli_si128(LKJIXABCD, 2);
910 const __m128i avg1 = _mm_avg_epu8(JIXABCD__, LKJIXABCD);
911 const __m128i lsb = _mm_and_si128(_mm_xor_si128(JIXABCD__, LKJIXABCD), one);
912 const __m128i avg2 = _mm_subs_epu8(avg1, lsb);
913 const __m128i abcdefg = _mm_avg_epu8(avg2, KJIXABCD_);
914 WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32( abcdefg ));
915 WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1)));
916 WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2)));
917 WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3)));
918 }
919
920 static WEBP_INLINE void HU4(uint8_t* dst, const uint8_t* top) {
921 const int I = top[-2];
922 const int J = top[-3];
923 const int K = top[-4];
924 const int L = top[-5];
925 DST(0, 0) = AVG2(I, J);
926 DST(2, 0) = DST(0, 1) = AVG2(J, K);
927 DST(2, 1) = DST(0, 2) = AVG2(K, L);
928 DST(1, 0) = AVG3(I, J, K);
929 DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
930 DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
931 DST(3, 2) = DST(2, 2) =
932 DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
933 }
934
935 static WEBP_INLINE void HD4(uint8_t* dst, const uint8_t* top) {
936 const int X = top[-1];
937 const int I = top[-2];
938 const int J = top[-3];
939 const int K = top[-4];
940 const int L = top[-5];
941 const int A = top[0];
942 const int B = top[1];
943 const int C = top[2];
944
945 DST(0, 0) = DST(2, 1) = AVG2(I, X);
946 DST(0, 1) = DST(2, 2) = AVG2(J, I);
947 DST(0, 2) = DST(2, 3) = AVG2(K, J);
948 DST(0, 3) = AVG2(L, K);
949
950 DST(3, 0) = AVG3(A, B, C);
951 DST(2, 0) = AVG3(X, A, B);
952 DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
953 DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
954 DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
955 DST(1, 3) = AVG3(L, K, J);
956 }
957
958 static WEBP_INLINE void TM4(uint8_t* dst, const uint8_t* top) {
959 const __m128i zero = _mm_setzero_si128();
960 const __m128i top_values = _mm_cvtsi32_si128(WebPMemToUint32(top));
961 const __m128i top_base = _mm_unpacklo_epi8(top_values, zero);
962 int y;
963 for (y = 0; y < 4; ++y, dst += BPS) {
964 const int val = top[-2 - y] - top[-1];
965 const __m128i base = _mm_set1_epi16(val);
966 const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero);
967 WebPUint32ToMem(dst, _mm_cvtsi128_si32(out));
968 }
969 }
970
971 #undef DST
972 #undef AVG3
973 #undef AVG2
974
975 //------------------------------------------------------------------------------
976 // luma 4x4 prediction
977
978 // Left samples are top[-5 .. -2], top_left is top[-1], top are
979 // located at top[0..3], and top right is top[4..7]
980 static void Intra4Preds(uint8_t* dst, const uint8_t* top) {
981 DC4(I4DC4 + dst, top);
982 TM4(I4TM4 + dst, top);
983 VE4(I4VE4 + dst, top);
984 HE4(I4HE4 + dst, top);
985 RD4(I4RD4 + dst, top);
986 VR4(I4VR4 + dst, top);
987 LD4(I4LD4 + dst, top);
988 VL4(I4VL4 + dst, top);
989 HD4(I4HD4 + dst, top);
990 HU4(I4HU4 + dst, top);
991 }
992
993 //------------------------------------------------------------------------------
994 // Chroma 8x8 prediction (paragraph 12.2)
995
996 static void IntraChromaPreds(uint8_t* dst, const uint8_t* left,
997 const uint8_t* top) {
998 // U block
999 DC8uvMode(C8DC8 + dst, left, top);
1000 VerticalPred(C8VE8 + dst, top, 8);
1001 HorizontalPred(C8HE8 + dst, left, 8);
1002 TrueMotion(C8TM8 + dst, left, top, 8);
1003 // V block
1004 dst += 8;
1005 if (top != NULL) top += 8;
1006 if (left != NULL) left += 16;
1007 DC8uvMode(C8DC8 + dst, left, top);
1008 VerticalPred(C8VE8 + dst, top, 8);
1009 HorizontalPred(C8HE8 + dst, left, 8);
1010 TrueMotion(C8TM8 + dst, left, top, 8);
1011 }
1012
1013 //------------------------------------------------------------------------------
1014 // luma 16x16 prediction (paragraph 12.3)
1015
1016 static void Intra16Preds(uint8_t* dst,
1017 const uint8_t* left, const uint8_t* top) {
1018 DC16Mode(I16DC16 + dst, left, top);
1019 VerticalPred(I16VE16 + dst, top, 16);
1020 HorizontalPred(I16HE16 + dst, left, 16);
1021 TrueMotion(I16TM16 + dst, left, top, 16);
1022 }
1023
1024 //------------------------------------------------------------------------------
489 // Metric 1025 // Metric
490 1026
491 static int SSE_Nx4(const uint8_t* a, const uint8_t* b, 1027 static WEBP_INLINE void SubtractAndAccumulate(const __m128i a, const __m128i b,
492 int num_quads, int do_16) { 1028 __m128i* const sum) {
493 const __m128i zero = _mm_setzero_si128(); 1029 // take abs(a-b) in 8b
494 __m128i sum1 = zero; 1030 const __m128i a_b = _mm_subs_epu8(a, b);
495 __m128i sum2 = zero; 1031 const __m128i b_a = _mm_subs_epu8(b, a);
496 1032 const __m128i abs_a_b = _mm_or_si128(a_b, b_a);
497 while (num_quads-- > 0) { 1033 // zero-extend to 16b
498 // Note: for the !do_16 case, we read 16 pixels instead of 8 but that's ok, 1034 const __m128i zero = _mm_setzero_si128();
499 // thanks to buffer over-allocation to that effect. 1035 const __m128i C0 = _mm_unpacklo_epi8(abs_a_b, zero);
500 const __m128i a0 = _mm_loadu_si128((__m128i*)&a[BPS * 0]); 1036 const __m128i C1 = _mm_unpackhi_epi8(abs_a_b, zero);
501 const __m128i a1 = _mm_loadu_si128((__m128i*)&a[BPS * 1]); 1037 // multiply with self
502 const __m128i a2 = _mm_loadu_si128((__m128i*)&a[BPS * 2]); 1038 const __m128i sum1 = _mm_madd_epi16(C0, C0);
503 const __m128i a3 = _mm_loadu_si128((__m128i*)&a[BPS * 3]); 1039 const __m128i sum2 = _mm_madd_epi16(C1, C1);
504 const __m128i b0 = _mm_loadu_si128((__m128i*)&b[BPS * 0]); 1040 *sum = _mm_add_epi32(sum1, sum2);
505 const __m128i b1 = _mm_loadu_si128((__m128i*)&b[BPS * 1]); 1041 }
506 const __m128i b2 = _mm_loadu_si128((__m128i*)&b[BPS * 2]); 1042
507 const __m128i b3 = _mm_loadu_si128((__m128i*)&b[BPS * 3]); 1043 static WEBP_INLINE int SSE_16xN(const uint8_t* a, const uint8_t* b,
508 1044 int num_pairs) {
509 // compute clip0(a-b) and clip0(b-a) 1045 __m128i sum = _mm_setzero_si128();
510 const __m128i a0p = _mm_subs_epu8(a0, b0); 1046 int32_t tmp[4];
511 const __m128i a0m = _mm_subs_epu8(b0, a0); 1047 int i;
512 const __m128i a1p = _mm_subs_epu8(a1, b1); 1048
513 const __m128i a1m = _mm_subs_epu8(b1, a1); 1049 for (i = 0; i < num_pairs; ++i) {
514 const __m128i a2p = _mm_subs_epu8(a2, b2); 1050 const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[BPS * 0]);
515 const __m128i a2m = _mm_subs_epu8(b2, a2); 1051 const __m128i b0 = _mm_loadu_si128((const __m128i*)&b[BPS * 0]);
516 const __m128i a3p = _mm_subs_epu8(a3, b3); 1052 const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[BPS * 1]);
517 const __m128i a3m = _mm_subs_epu8(b3, a3); 1053 const __m128i b1 = _mm_loadu_si128((const __m128i*)&b[BPS * 1]);
518 1054 __m128i sum1, sum2;
519 // compute |a-b| with 8b arithmetic as clip0(a-b) | clip0(b-a) 1055 SubtractAndAccumulate(a0, b0, &sum1);
520 const __m128i diff0 = _mm_or_si128(a0p, a0m); 1056 SubtractAndAccumulate(a1, b1, &sum2);
521 const __m128i diff1 = _mm_or_si128(a1p, a1m); 1057 sum = _mm_add_epi32(sum, _mm_add_epi32(sum1, sum2));
522 const __m128i diff2 = _mm_or_si128(a2p, a2m); 1058 a += 2 * BPS;
523 const __m128i diff3 = _mm_or_si128(a3p, a3m); 1059 b += 2 * BPS;
524 1060 }
525 // unpack (only four operations, instead of eight) 1061 _mm_storeu_si128((__m128i*)tmp, sum);
526 const __m128i low0 = _mm_unpacklo_epi8(diff0, zero); 1062 return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);
527 const __m128i low1 = _mm_unpacklo_epi8(diff1, zero);
528 const __m128i low2 = _mm_unpacklo_epi8(diff2, zero);
529 const __m128i low3 = _mm_unpacklo_epi8(diff3, zero);
530
531 // multiply with self
532 const __m128i low_madd0 = _mm_madd_epi16(low0, low0);
533 const __m128i low_madd1 = _mm_madd_epi16(low1, low1);
534 const __m128i low_madd2 = _mm_madd_epi16(low2, low2);
535 const __m128i low_madd3 = _mm_madd_epi16(low3, low3);
536
537 // collect in a cascading way
538 const __m128i low_sum0 = _mm_add_epi32(low_madd0, low_madd1);
539 const __m128i low_sum1 = _mm_add_epi32(low_madd2, low_madd3);
540 sum1 = _mm_add_epi32(sum1, low_sum0);
541 sum2 = _mm_add_epi32(sum2, low_sum1);
542
543 if (do_16) { // if necessary, process the higher 8 bytes similarly
544 const __m128i hi0 = _mm_unpackhi_epi8(diff0, zero);
545 const __m128i hi1 = _mm_unpackhi_epi8(diff1, zero);
546 const __m128i hi2 = _mm_unpackhi_epi8(diff2, zero);
547 const __m128i hi3 = _mm_unpackhi_epi8(diff3, zero);
548
549 const __m128i hi_madd0 = _mm_madd_epi16(hi0, hi0);
550 const __m128i hi_madd1 = _mm_madd_epi16(hi1, hi1);
551 const __m128i hi_madd2 = _mm_madd_epi16(hi2, hi2);
552 const __m128i hi_madd3 = _mm_madd_epi16(hi3, hi3);
553 const __m128i hi_sum0 = _mm_add_epi32(hi_madd0, hi_madd1);
554 const __m128i hi_sum1 = _mm_add_epi32(hi_madd2, hi_madd3);
555 sum1 = _mm_add_epi32(sum1, hi_sum0);
556 sum2 = _mm_add_epi32(sum2, hi_sum1);
557 }
558 a += 4 * BPS;
559 b += 4 * BPS;
560 }
561 {
562 int32_t tmp[4];
563 const __m128i sum = _mm_add_epi32(sum1, sum2);
564 _mm_storeu_si128((__m128i*)tmp, sum);
565 return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);
566 }
567 } 1063 }
568 1064
569 static int SSE16x16(const uint8_t* a, const uint8_t* b) { 1065 static int SSE16x16(const uint8_t* a, const uint8_t* b) {
570 return SSE_Nx4(a, b, 4, 1); 1066 return SSE_16xN(a, b, 8);
571 } 1067 }
572 1068
573 static int SSE16x8(const uint8_t* a, const uint8_t* b) { 1069 static int SSE16x8(const uint8_t* a, const uint8_t* b) {
574 return SSE_Nx4(a, b, 2, 1); 1070 return SSE_16xN(a, b, 4);
575 } 1071 }
1072
1073 #define LOAD_8x16b(ptr) \
1074 _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(ptr)), zero)
576 1075
577 static int SSE8x8(const uint8_t* a, const uint8_t* b) { 1076 static int SSE8x8(const uint8_t* a, const uint8_t* b) {
578 return SSE_Nx4(a, b, 2, 0); 1077 const __m128i zero = _mm_setzero_si128();
579 } 1078 int num_pairs = 4;
1079 __m128i sum = zero;
1080 int32_t tmp[4];
1081 while (num_pairs-- > 0) {
1082 const __m128i a0 = LOAD_8x16b(&a[BPS * 0]);
1083 const __m128i a1 = LOAD_8x16b(&a[BPS * 1]);
1084 const __m128i b0 = LOAD_8x16b(&b[BPS * 0]);
1085 const __m128i b1 = LOAD_8x16b(&b[BPS * 1]);
1086 // subtract
1087 const __m128i c0 = _mm_subs_epi16(a0, b0);
1088 const __m128i c1 = _mm_subs_epi16(a1, b1);
1089 // multiply/accumulate with self
1090 const __m128i d0 = _mm_madd_epi16(c0, c0);
1091 const __m128i d1 = _mm_madd_epi16(c1, c1);
1092 // collect
1093 const __m128i sum01 = _mm_add_epi32(d0, d1);
1094 sum = _mm_add_epi32(sum, sum01);
1095 a += 2 * BPS;
1096 b += 2 * BPS;
1097 }
1098 _mm_storeu_si128((__m128i*)tmp, sum);
1099 return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);
1100 }
1101 #undef LOAD_8x16b
580 1102
581 static int SSE4x4(const uint8_t* a, const uint8_t* b) { 1103 static int SSE4x4(const uint8_t* a, const uint8_t* b) {
582 const __m128i zero = _mm_setzero_si128(); 1104 const __m128i zero = _mm_setzero_si128();
583 1105
584 // Load values. Note that we read 8 pixels instead of 4, 1106 // Load values. Note that we read 8 pixels instead of 4,
585 // but the a/b buffers are over-allocated to that effect. 1107 // but the a/b buffers are over-allocated to that effect.
586 const __m128i a0 = _mm_loadl_epi64((__m128i*)&a[BPS * 0]); 1108 const __m128i a0 = _mm_loadl_epi64((const __m128i*)&a[BPS * 0]);
587 const __m128i a1 = _mm_loadl_epi64((__m128i*)&a[BPS * 1]); 1109 const __m128i a1 = _mm_loadl_epi64((const __m128i*)&a[BPS * 1]);
588 const __m128i a2 = _mm_loadl_epi64((__m128i*)&a[BPS * 2]); 1110 const __m128i a2 = _mm_loadl_epi64((const __m128i*)&a[BPS * 2]);
589 const __m128i a3 = _mm_loadl_epi64((__m128i*)&a[BPS * 3]); 1111 const __m128i a3 = _mm_loadl_epi64((const __m128i*)&a[BPS * 3]);
590 const __m128i b0 = _mm_loadl_epi64((__m128i*)&b[BPS * 0]); 1112 const __m128i b0 = _mm_loadl_epi64((const __m128i*)&b[BPS * 0]);
591 const __m128i b1 = _mm_loadl_epi64((__m128i*)&b[BPS * 1]); 1113 const __m128i b1 = _mm_loadl_epi64((const __m128i*)&b[BPS * 1]);
592 const __m128i b2 = _mm_loadl_epi64((__m128i*)&b[BPS * 2]); 1114 const __m128i b2 = _mm_loadl_epi64((const __m128i*)&b[BPS * 2]);
593 const __m128i b3 = _mm_loadl_epi64((__m128i*)&b[BPS * 3]); 1115 const __m128i b3 = _mm_loadl_epi64((const __m128i*)&b[BPS * 3]);
594 1116 // Combine pair of lines.
595 // Combine pair of lines and convert to 16b.
596 const __m128i a01 = _mm_unpacklo_epi32(a0, a1); 1117 const __m128i a01 = _mm_unpacklo_epi32(a0, a1);
597 const __m128i a23 = _mm_unpacklo_epi32(a2, a3); 1118 const __m128i a23 = _mm_unpacklo_epi32(a2, a3);
598 const __m128i b01 = _mm_unpacklo_epi32(b0, b1); 1119 const __m128i b01 = _mm_unpacklo_epi32(b0, b1);
599 const __m128i b23 = _mm_unpacklo_epi32(b2, b3); 1120 const __m128i b23 = _mm_unpacklo_epi32(b2, b3);
1121 // Convert to 16b.
600 const __m128i a01s = _mm_unpacklo_epi8(a01, zero); 1122 const __m128i a01s = _mm_unpacklo_epi8(a01, zero);
601 const __m128i a23s = _mm_unpacklo_epi8(a23, zero); 1123 const __m128i a23s = _mm_unpacklo_epi8(a23, zero);
602 const __m128i b01s = _mm_unpacklo_epi8(b01, zero); 1124 const __m128i b01s = _mm_unpacklo_epi8(b01, zero);
603 const __m128i b23s = _mm_unpacklo_epi8(b23, zero); 1125 const __m128i b23s = _mm_unpacklo_epi8(b23, zero);
604 1126 // subtract, square and accumulate
605 // Compute differences; (a-b)^2 = (abs(a-b))^2 = (sat8(a-b) + sat8(b-a))^2 1127 const __m128i d0 = _mm_subs_epi16(a01s, b01s);
606 // TODO(cduvivier): Dissassemble and figure out why this is fastest. We don't 1128 const __m128i d1 = _mm_subs_epi16(a23s, b23s);
607 // need absolute values, there is no need to do calculation 1129 const __m128i e0 = _mm_madd_epi16(d0, d0);
608 // in 8bit as we are already in 16bit, ... Yet this is what 1130 const __m128i e1 = _mm_madd_epi16(d1, d1);
609 // benchmarks the fastest! 1131 const __m128i sum = _mm_add_epi32(e0, e1);
610 const __m128i d0 = _mm_subs_epu8(a01s, b01s);
611 const __m128i d1 = _mm_subs_epu8(b01s, a01s);
612 const __m128i d2 = _mm_subs_epu8(a23s, b23s);
613 const __m128i d3 = _mm_subs_epu8(b23s, a23s);
614
615 // Square and add them all together.
616 const __m128i madd0 = _mm_madd_epi16(d0, d0);
617 const __m128i madd1 = _mm_madd_epi16(d1, d1);
618 const __m128i madd2 = _mm_madd_epi16(d2, d2);
619 const __m128i madd3 = _mm_madd_epi16(d3, d3);
620 const __m128i sum0 = _mm_add_epi32(madd0, madd1);
621 const __m128i sum1 = _mm_add_epi32(madd2, madd3);
622 const __m128i sum2 = _mm_add_epi32(sum0, sum1);
623 1132
624 int32_t tmp[4]; 1133 int32_t tmp[4];
625 _mm_storeu_si128((__m128i*)tmp, sum2); 1134 _mm_storeu_si128((__m128i*)tmp, sum);
626 return (tmp[3] + tmp[2] + tmp[1] + tmp[0]); 1135 return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);
627 } 1136 }
628 1137
629 //------------------------------------------------------------------------------ 1138 //------------------------------------------------------------------------------
630 // Texture distortion 1139 // Texture distortion
631 // 1140 //
632 // We try to match the spectral content (weighted) between source and 1141 // We try to match the spectral content (weighted) between source and
633 // reconstructed samples. 1142 // reconstructed samples.
634 1143
635 // Hadamard transform 1144 // Hadamard transform
636 // Returns the difference between the weighted sum of the absolute value of 1145 // Returns the difference between the weighted sum of the absolute value of
637 // transformed coefficients. 1146 // transformed coefficients.
638 static int TTransform(const uint8_t* inA, const uint8_t* inB, 1147 static int TTransform(const uint8_t* inA, const uint8_t* inB,
639 const uint16_t* const w) { 1148 const uint16_t* const w) {
640 int32_t sum[4]; 1149 int32_t sum[4];
641 __m128i tmp_0, tmp_1, tmp_2, tmp_3; 1150 __m128i tmp_0, tmp_1, tmp_2, tmp_3;
642 const __m128i zero = _mm_setzero_si128(); 1151 const __m128i zero = _mm_setzero_si128();
643 1152
644 // Load, combine and transpose inputs. 1153 // Load, combine and transpose inputs.
645 { 1154 {
646 const __m128i inA_0 = _mm_loadl_epi64((__m128i*)&inA[BPS * 0]); 1155 const __m128i inA_0 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 0]);
647 const __m128i inA_1 = _mm_loadl_epi64((__m128i*)&inA[BPS * 1]); 1156 const __m128i inA_1 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 1]);
648 const __m128i inA_2 = _mm_loadl_epi64((__m128i*)&inA[BPS * 2]); 1157 const __m128i inA_2 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 2]);
649 const __m128i inA_3 = _mm_loadl_epi64((__m128i*)&inA[BPS * 3]); 1158 const __m128i inA_3 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 3]);
650 const __m128i inB_0 = _mm_loadl_epi64((__m128i*)&inB[BPS * 0]); 1159 const __m128i inB_0 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 0]);
651 const __m128i inB_1 = _mm_loadl_epi64((__m128i*)&inB[BPS * 1]); 1160 const __m128i inB_1 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 1]);
652 const __m128i inB_2 = _mm_loadl_epi64((__m128i*)&inB[BPS * 2]); 1161 const __m128i inB_2 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 2]);
653 const __m128i inB_3 = _mm_loadl_epi64((__m128i*)&inB[BPS * 3]); 1162 const __m128i inB_3 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 3]);
654 1163
655 // Combine inA and inB (we'll do two transforms in parallel). 1164 // Combine inA and inB (we'll do two transforms in parallel).
656 const __m128i inAB_0 = _mm_unpacklo_epi8(inA_0, inB_0); 1165 const __m128i inAB_0 = _mm_unpacklo_epi8(inA_0, inB_0);
657 const __m128i inAB_1 = _mm_unpacklo_epi8(inA_1, inB_1); 1166 const __m128i inAB_1 = _mm_unpacklo_epi8(inA_1, inB_1);
658 const __m128i inAB_2 = _mm_unpacklo_epi8(inA_2, inB_2); 1167 const __m128i inAB_2 = _mm_unpacklo_epi8(inA_2, inB_2);
659 const __m128i inAB_3 = _mm_unpacklo_epi8(inA_3, inB_3); 1168 const __m128i inAB_3 = _mm_unpacklo_epi8(inA_3, inB_3);
660 // a00 b00 a01 b01 a02 b03 a03 b03 0 0 0 0 0 0 0 0 1169 // a00 b00 a01 b01 a02 b03 a03 b03 0 0 0 0 0 0 0 0
661 // a10 b10 a11 b11 a12 b12 a13 b13 0 0 0 0 0 0 0 0 1170 // a10 b10 a11 b11 a12 b12 a13 b13 0 0 0 0 0 0 0 0
662 // a20 b20 a21 b21 a22 b22 a23 b23 0 0 0 0 0 0 0 0 1171 // a20 b20 a21 b21 a22 b22 a23 b23 0 0 0 0 0 0 0 0
663 // a30 b30 a31 b31 a32 b32 a33 b33 0 0 0 0 0 0 0 0 1172 // a30 b30 a31 b31 a32 b32 a33 b33 0 0 0 0 0 0 0 0
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
722 tmp_3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3); 1231 tmp_3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3);
723 // a00 a10 a20 a30 b00 b10 b20 b30 1232 // a00 a10 a20 a30 b00 b10 b20 b30
724 // a01 a11 a21 a31 b01 b11 b21 b31 1233 // a01 a11 a21 a31 b01 b11 b21 b31
725 // a02 a12 a22 a32 b02 b12 b22 b32 1234 // a02 a12 a22 a32 b02 b12 b22 b32
726 // a03 a13 a23 a33 b03 b13 b23 b33 1235 // a03 a13 a23 a33 b03 b13 b23 b33
727 } 1236 }
728 1237
729 // Vertical pass and difference of weighted sums. 1238 // Vertical pass and difference of weighted sums.
730 { 1239 {
731 // Load all inputs. 1240 // Load all inputs.
732 // TODO(cduvivier): Make variable declarations and allocations aligned so 1241 const __m128i w_0 = _mm_loadu_si128((const __m128i*)&w[0]);
733 // we can use _mm_load_si128 instead of _mm_loadu_si128. 1242 const __m128i w_8 = _mm_loadu_si128((const __m128i*)&w[8]);
734 const __m128i w_0 = _mm_loadu_si128((__m128i*)&w[0]);
735 const __m128i w_8 = _mm_loadu_si128((__m128i*)&w[8]);
736 1243
737 // Calculate a and b (two 4x4 at once). 1244 // Calculate a and b (two 4x4 at once).
738 const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2); 1245 const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2);
739 const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3); 1246 const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3);
740 const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3); 1247 const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3);
741 const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2); 1248 const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2);
742 const __m128i b0 = _mm_add_epi16(a0, a1); 1249 const __m128i b0 = _mm_add_epi16(a0, a1);
743 const __m128i b1 = _mm_add_epi16(a3, a2); 1250 const __m128i b1 = _mm_add_epi16(a3, a2);
744 const __m128i b2 = _mm_sub_epi16(a3, a2); 1251 const __m128i b2 = _mm_sub_epi16(a3, a2);
745 const __m128i b3 = _mm_sub_epi16(a0, a1); 1252 const __m128i b3 = _mm_sub_epi16(a0, a1);
746 1253
747 // Separate the transforms of inA and inB. 1254 // Separate the transforms of inA and inB.
748 __m128i A_b0 = _mm_unpacklo_epi64(b0, b1); 1255 __m128i A_b0 = _mm_unpacklo_epi64(b0, b1);
749 __m128i A_b2 = _mm_unpacklo_epi64(b2, b3); 1256 __m128i A_b2 = _mm_unpacklo_epi64(b2, b3);
750 __m128i B_b0 = _mm_unpackhi_epi64(b0, b1); 1257 __m128i B_b0 = _mm_unpackhi_epi64(b0, b1);
751 __m128i B_b2 = _mm_unpackhi_epi64(b2, b3); 1258 __m128i B_b2 = _mm_unpackhi_epi64(b2, b3);
752 1259
753 { 1260 {
754 // sign(b) = b >> 15 (0x0000 if positive, 0xffff if negative) 1261 const __m128i d0 = _mm_sub_epi16(zero, A_b0);
755 const __m128i sign_A_b0 = _mm_srai_epi16(A_b0, 15); 1262 const __m128i d1 = _mm_sub_epi16(zero, A_b2);
756 const __m128i sign_A_b2 = _mm_srai_epi16(A_b2, 15); 1263 const __m128i d2 = _mm_sub_epi16(zero, B_b0);
757 const __m128i sign_B_b0 = _mm_srai_epi16(B_b0, 15); 1264 const __m128i d3 = _mm_sub_epi16(zero, B_b2);
758 const __m128i sign_B_b2 = _mm_srai_epi16(B_b2, 15); 1265 A_b0 = _mm_max_epi16(A_b0, d0); // abs(v), 16b
759 1266 A_b2 = _mm_max_epi16(A_b2, d1);
760 // b = abs(b) = (b ^ sign) - sign 1267 B_b0 = _mm_max_epi16(B_b0, d2);
761 A_b0 = _mm_xor_si128(A_b0, sign_A_b0); 1268 B_b2 = _mm_max_epi16(B_b2, d3);
762 A_b2 = _mm_xor_si128(A_b2, sign_A_b2);
763 B_b0 = _mm_xor_si128(B_b0, sign_B_b0);
764 B_b2 = _mm_xor_si128(B_b2, sign_B_b2);
765 A_b0 = _mm_sub_epi16(A_b0, sign_A_b0);
766 A_b2 = _mm_sub_epi16(A_b2, sign_A_b2);
767 B_b0 = _mm_sub_epi16(B_b0, sign_B_b0);
768 B_b2 = _mm_sub_epi16(B_b2, sign_B_b2);
769 } 1269 }
770 1270
771 // weighted sums 1271 // weighted sums
772 A_b0 = _mm_madd_epi16(A_b0, w_0); 1272 A_b0 = _mm_madd_epi16(A_b0, w_0);
773 A_b2 = _mm_madd_epi16(A_b2, w_8); 1273 A_b2 = _mm_madd_epi16(A_b2, w_8);
774 B_b0 = _mm_madd_epi16(B_b0, w_0); 1274 B_b0 = _mm_madd_epi16(B_b0, w_0);
775 B_b2 = _mm_madd_epi16(B_b2, w_8); 1275 B_b2 = _mm_madd_epi16(B_b2, w_8);
776 A_b0 = _mm_add_epi32(A_b0, A_b2); 1276 A_b0 = _mm_add_epi32(A_b0, A_b2);
777 B_b0 = _mm_add_epi32(B_b0, B_b2); 1277 B_b0 = _mm_add_epi32(B_b0, B_b2);
778 1278
(...skipping 29 matching lines...) Expand all
808 static WEBP_INLINE int DoQuantizeBlock(int16_t in[16], int16_t out[16], 1308 static WEBP_INLINE int DoQuantizeBlock(int16_t in[16], int16_t out[16],
809 const uint16_t* const sharpen, 1309 const uint16_t* const sharpen,
810 const VP8Matrix* const mtx) { 1310 const VP8Matrix* const mtx) {
811 const __m128i max_coeff_2047 = _mm_set1_epi16(MAX_LEVEL); 1311 const __m128i max_coeff_2047 = _mm_set1_epi16(MAX_LEVEL);
812 const __m128i zero = _mm_setzero_si128(); 1312 const __m128i zero = _mm_setzero_si128();
813 __m128i coeff0, coeff8; 1313 __m128i coeff0, coeff8;
814 __m128i out0, out8; 1314 __m128i out0, out8;
815 __m128i packed_out; 1315 __m128i packed_out;
816 1316
817 // Load all inputs. 1317 // Load all inputs.
818 // TODO(cduvivier): Make variable declarations and allocations aligned so that
819 // we can use _mm_load_si128 instead of _mm_loadu_si128.
820 __m128i in0 = _mm_loadu_si128((__m128i*)&in[0]); 1318 __m128i in0 = _mm_loadu_si128((__m128i*)&in[0]);
821 __m128i in8 = _mm_loadu_si128((__m128i*)&in[8]); 1319 __m128i in8 = _mm_loadu_si128((__m128i*)&in[8]);
822 const __m128i iq0 = _mm_loadu_si128((__m128i*)&mtx->iq_[0]); 1320 const __m128i iq0 = _mm_loadu_si128((const __m128i*)&mtx->iq_[0]);
823 const __m128i iq8 = _mm_loadu_si128((__m128i*)&mtx->iq_[8]); 1321 const __m128i iq8 = _mm_loadu_si128((const __m128i*)&mtx->iq_[8]);
824 const __m128i q0 = _mm_loadu_si128((__m128i*)&mtx->q_[0]); 1322 const __m128i q0 = _mm_loadu_si128((const __m128i*)&mtx->q_[0]);
825 const __m128i q8 = _mm_loadu_si128((__m128i*)&mtx->q_[8]); 1323 const __m128i q8 = _mm_loadu_si128((const __m128i*)&mtx->q_[8]);
826 1324
827 // extract sign(in) (0x0000 if positive, 0xffff if negative) 1325 // extract sign(in) (0x0000 if positive, 0xffff if negative)
828 const __m128i sign0 = _mm_cmpgt_epi16(zero, in0); 1326 const __m128i sign0 = _mm_cmpgt_epi16(zero, in0);
829 const __m128i sign8 = _mm_cmpgt_epi16(zero, in8); 1327 const __m128i sign8 = _mm_cmpgt_epi16(zero, in8);
830 1328
831 // coeff = abs(in) = (in ^ sign) - sign 1329 // coeff = abs(in) = (in ^ sign) - sign
832 coeff0 = _mm_xor_si128(in0, sign0); 1330 coeff0 = _mm_xor_si128(in0, sign0);
833 coeff8 = _mm_xor_si128(in8, sign8); 1331 coeff8 = _mm_xor_si128(in8, sign8);
834 coeff0 = _mm_sub_epi16(coeff0, sign0); 1332 coeff0 = _mm_sub_epi16(coeff0, sign0);
835 coeff8 = _mm_sub_epi16(coeff8, sign8); 1333 coeff8 = _mm_sub_epi16(coeff8, sign8);
836 1334
837 // coeff = abs(in) + sharpen 1335 // coeff = abs(in) + sharpen
838 if (sharpen != NULL) { 1336 if (sharpen != NULL) {
839 const __m128i sharpen0 = _mm_loadu_si128((__m128i*)&sharpen[0]); 1337 const __m128i sharpen0 = _mm_loadu_si128((const __m128i*)&sharpen[0]);
840 const __m128i sharpen8 = _mm_loadu_si128((__m128i*)&sharpen[8]); 1338 const __m128i sharpen8 = _mm_loadu_si128((const __m128i*)&sharpen[8]);
841 coeff0 = _mm_add_epi16(coeff0, sharpen0); 1339 coeff0 = _mm_add_epi16(coeff0, sharpen0);
842 coeff8 = _mm_add_epi16(coeff8, sharpen8); 1340 coeff8 = _mm_add_epi16(coeff8, sharpen8);
843 } 1341 }
844 1342
845 // out = (coeff * iQ + B) >> QFIX 1343 // out = (coeff * iQ + B) >> QFIX
846 { 1344 {
847 // doing calculations with 32b precision (QFIX=17) 1345 // doing calculations with 32b precision (QFIX=17)
848 // out = (coeff * iQ) 1346 // out = (coeff * iQ)
849 const __m128i coeff_iQ0H = _mm_mulhi_epu16(coeff0, iq0); 1347 const __m128i coeff_iQ0H = _mm_mulhi_epu16(coeff0, iq0);
850 const __m128i coeff_iQ0L = _mm_mullo_epi16(coeff0, iq0); 1348 const __m128i coeff_iQ0L = _mm_mullo_epi16(coeff0, iq0);
851 const __m128i coeff_iQ8H = _mm_mulhi_epu16(coeff8, iq8); 1349 const __m128i coeff_iQ8H = _mm_mulhi_epu16(coeff8, iq8);
852 const __m128i coeff_iQ8L = _mm_mullo_epi16(coeff8, iq8); 1350 const __m128i coeff_iQ8L = _mm_mullo_epi16(coeff8, iq8);
853 __m128i out_00 = _mm_unpacklo_epi16(coeff_iQ0L, coeff_iQ0H); 1351 __m128i out_00 = _mm_unpacklo_epi16(coeff_iQ0L, coeff_iQ0H);
854 __m128i out_04 = _mm_unpackhi_epi16(coeff_iQ0L, coeff_iQ0H); 1352 __m128i out_04 = _mm_unpackhi_epi16(coeff_iQ0L, coeff_iQ0H);
855 __m128i out_08 = _mm_unpacklo_epi16(coeff_iQ8L, coeff_iQ8H); 1353 __m128i out_08 = _mm_unpacklo_epi16(coeff_iQ8L, coeff_iQ8H);
856 __m128i out_12 = _mm_unpackhi_epi16(coeff_iQ8L, coeff_iQ8H); 1354 __m128i out_12 = _mm_unpackhi_epi16(coeff_iQ8L, coeff_iQ8H);
857 // out = (coeff * iQ + B) 1355 // out = (coeff * iQ + B)
858 const __m128i bias_00 = _mm_loadu_si128((__m128i*)&mtx->bias_[0]); 1356 const __m128i bias_00 = _mm_loadu_si128((const __m128i*)&mtx->bias_[0]);
859 const __m128i bias_04 = _mm_loadu_si128((__m128i*)&mtx->bias_[4]); 1357 const __m128i bias_04 = _mm_loadu_si128((const __m128i*)&mtx->bias_[4]);
860 const __m128i bias_08 = _mm_loadu_si128((__m128i*)&mtx->bias_[8]); 1358 const __m128i bias_08 = _mm_loadu_si128((const __m128i*)&mtx->bias_[8]);
861 const __m128i bias_12 = _mm_loadu_si128((__m128i*)&mtx->bias_[12]); 1359 const __m128i bias_12 = _mm_loadu_si128((const __m128i*)&mtx->bias_[12]);
862 out_00 = _mm_add_epi32(out_00, bias_00); 1360 out_00 = _mm_add_epi32(out_00, bias_00);
863 out_04 = _mm_add_epi32(out_04, bias_04); 1361 out_04 = _mm_add_epi32(out_04, bias_04);
864 out_08 = _mm_add_epi32(out_08, bias_08); 1362 out_08 = _mm_add_epi32(out_08, bias_08);
865 out_12 = _mm_add_epi32(out_12, bias_12); 1363 out_12 = _mm_add_epi32(out_12, bias_12);
866 // out = QUANTDIV(coeff, iQ, B, QFIX) 1364 // out = QUANTDIV(coeff, iQ, B, QFIX)
867 out_00 = _mm_srai_epi32(out_00, QFIX); 1365 out_00 = _mm_srai_epi32(out_00, QFIX);
868 out_04 = _mm_srai_epi32(out_04, QFIX); 1366 out_04 = _mm_srai_epi32(out_04, QFIX);
869 out_08 = _mm_srai_epi32(out_08, QFIX); 1367 out_08 = _mm_srai_epi32(out_08, QFIX);
870 out_12 = _mm_srai_epi32(out_12, QFIX); 1368 out_12 = _mm_srai_epi32(out_12, QFIX);
871 1369
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
922 static int QuantizeBlock(int16_t in[16], int16_t out[16], 1420 static int QuantizeBlock(int16_t in[16], int16_t out[16],
923 const VP8Matrix* const mtx) { 1421 const VP8Matrix* const mtx) {
924 return DoQuantizeBlock(in, out, &mtx->sharpen_[0], mtx); 1422 return DoQuantizeBlock(in, out, &mtx->sharpen_[0], mtx);
925 } 1423 }
926 1424
927 static int QuantizeBlockWHT(int16_t in[16], int16_t out[16], 1425 static int QuantizeBlockWHT(int16_t in[16], int16_t out[16],
928 const VP8Matrix* const mtx) { 1426 const VP8Matrix* const mtx) {
929 return DoQuantizeBlock(in, out, NULL, mtx); 1427 return DoQuantizeBlock(in, out, NULL, mtx);
930 } 1428 }
931 1429
932 // Forward declaration. 1430 static int Quantize2Blocks(int16_t in[32], int16_t out[32],
933 void VP8SetResidualCoeffsSSE2(const int16_t* const coeffs, 1431 const VP8Matrix* const mtx) {
934 VP8Residual* const res); 1432 int nz;
935 1433 const uint16_t* const sharpen = &mtx->sharpen_[0];
936 void VP8SetResidualCoeffsSSE2(const int16_t* const coeffs, 1434 nz = DoQuantizeBlock(in + 0 * 16, out + 0 * 16, sharpen, mtx) << 0;
937 VP8Residual* const res) { 1435 nz |= DoQuantizeBlock(in + 1 * 16, out + 1 * 16, sharpen, mtx) << 1;
938 const __m128i c0 = _mm_loadu_si128((const __m128i*)coeffs); 1436 return nz;
939 const __m128i c1 = _mm_loadu_si128((const __m128i*)(coeffs + 8));
940 // Use SSE to compare 8 values with a single instruction.
941 const __m128i zero = _mm_setzero_si128();
942 const __m128i m0 = _mm_cmpeq_epi16(c0, zero);
943 const __m128i m1 = _mm_cmpeq_epi16(c1, zero);
944 // Get the comparison results as a bitmask, consisting of two times 16 bits:
945 // two identical bits for each result. Concatenate both bitmasks to get a
946 // single 32 bit value. Negate the mask to get the position of entries that
947 // are not equal to zero. We don't need to mask out least significant bits
948 // according to res->first, since coeffs[0] is 0 if res->first > 0
949 const uint32_t mask =
950 ~(((uint32_t)_mm_movemask_epi8(m1) << 16) | _mm_movemask_epi8(m0));
951 // The position of the most significant non-zero bit indicates the position of
952 // the last non-zero value. Divide the result by two because __movemask_epi8
953 // operates on 8 bit values instead of 16 bit values.
954 assert(res->first == 0 || coeffs[0] == 0);
955 res->last = mask ? (BitsLog2Floor(mask) >> 1) : -1;
956 res->coeffs = coeffs;
957 } 1437 }
958 1438
959 #endif // WEBP_USE_SSE2
960
961 //------------------------------------------------------------------------------ 1439 //------------------------------------------------------------------------------
962 // Entry point 1440 // Entry point
963 1441
964 extern void VP8EncDspInitSSE2(void); 1442 extern void VP8EncDspInitSSE2(void);
965 1443
966 void VP8EncDspInitSSE2(void) { 1444 WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspInitSSE2(void) {
967 #if defined(WEBP_USE_SSE2)
968 VP8CollectHistogram = CollectHistogram; 1445 VP8CollectHistogram = CollectHistogram;
1446 VP8EncPredLuma16 = Intra16Preds;
1447 VP8EncPredChroma8 = IntraChromaPreds;
1448 VP8EncPredLuma4 = Intra4Preds;
969 VP8EncQuantizeBlock = QuantizeBlock; 1449 VP8EncQuantizeBlock = QuantizeBlock;
1450 VP8EncQuantize2Blocks = Quantize2Blocks;
970 VP8EncQuantizeBlockWHT = QuantizeBlockWHT; 1451 VP8EncQuantizeBlockWHT = QuantizeBlockWHT;
971 VP8ITransform = ITransform; 1452 VP8ITransform = ITransform;
972 VP8FTransform = FTransform; 1453 VP8FTransform = FTransform;
1454 VP8FTransform2 = FTransform2;
973 VP8FTransformWHT = FTransformWHT; 1455 VP8FTransformWHT = FTransformWHT;
974 VP8SSE16x16 = SSE16x16; 1456 VP8SSE16x16 = SSE16x16;
975 VP8SSE16x8 = SSE16x8; 1457 VP8SSE16x8 = SSE16x8;
976 VP8SSE8x8 = SSE8x8; 1458 VP8SSE8x8 = SSE8x8;
977 VP8SSE4x4 = SSE4x4; 1459 VP8SSE4x4 = SSE4x4;
978 VP8TDisto4x4 = Disto4x4; 1460 VP8TDisto4x4 = Disto4x4;
979 VP8TDisto16x16 = Disto16x16; 1461 VP8TDisto16x16 = Disto16x16;
980 #endif // WEBP_USE_SSE2
981 } 1462 }
982 1463
1464 #else // !WEBP_USE_SSE2
1465
1466 WEBP_DSP_INIT_STUB(VP8EncDspInitSSE2)
1467
1468 #endif // WEBP_USE_SSE2
OLDNEW
« no previous file with comments | « third_party/libwebp/dsp/enc_neon.c ('k') | third_party/libwebp/dsp/enc_sse41.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698