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 (at) google.com) 13 14 #include "./dsp.h" 15 16 #if defined(WEBP_USE_SSE2) 17 #include <stdlib.h> // for abs() 18 #include <emmintrin.h> 19 20 #include "../enc/cost.h" 21 #include "../enc/vp8enci.h" 22 23 //------------------------------------------------------------------------------ 24 // Quite useful macro for debugging. Left here for convenience. 25 26 #if 0 27 #include <stdio.h> 28 static void PrintReg(const __m128i r, const char* const name, int size) { 29 int n; 30 union { 31 __m128i r; 32 uint8_t i8[16]; 33 uint16_t i16[8]; 34 uint32_t i32[4]; 35 uint64_t i64[2]; 36 } tmp; 37 tmp.r = r; 38 fprintf(stderr, "%s\t: ", name); 39 if (size == 8) { 40 for (n = 0; n < 16; ++n) fprintf(stderr, "%.2x ", tmp.i8[n]); 41 } else if (size == 16) { 42 for (n = 0; n < 8; ++n) fprintf(stderr, "%.4x ", tmp.i16[n]); 43 } else if (size == 32) { 44 for (n = 0; n < 4; ++n) fprintf(stderr, "%.8x ", tmp.i32[n]); 45 } else { 46 for (n = 0; n < 2; ++n) fprintf(stderr, "%.16lx ", tmp.i64[n]); 47 } 48 fprintf(stderr, "\n"); 49 } 50 #endif 51 52 //------------------------------------------------------------------------------ 53 // Transforms (Paragraph 14.4) 54 55 // Does one or two inverse transforms. 56 static void ITransform(const uint8_t* ref, const int16_t* in, uint8_t* dst, 57 int do_two) { 58 // This implementation makes use of 16-bit fixed point versions of two 59 // multiply constants: 60 // K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^16 61 // K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^16 62 // 63 // To be able to use signed 16-bit integers, we use the following trick to 64 // have constants within range: 65 // - Associated constants are obtained by subtracting the 16-bit fixed point 66 // version of one: 67 // k = K - (1 << 16) => K = k + (1 << 16) 68 // K1 = 85267 => k1 = 20091 69 // K2 = 35468 => k2 = -30068 70 // - The multiplication of a variable by a constant become the sum of the 71 // variable and the multiplication of that variable by the associated 72 // constant: 73 // (x * K) >> 16 = (x * (k + (1 << 16))) >> 16 = ((x * k ) >> 16) + x 74 const __m128i k1 = _mm_set1_epi16(20091); 75 const __m128i k2 = _mm_set1_epi16(-30068); 76 __m128i T0, T1, T2, T3; 77 78 // Load and concatenate the transform coefficients (we'll do two inverse 79 // transforms in parallel). In the case of only one inverse transform, the 80 // second half of the vectors will just contain random value we'll never 81 // use nor store. 82 __m128i in0, in1, in2, in3; 83 { 84 in0 = _mm_loadl_epi64((const __m128i*)&in[0]); 85 in1 = _mm_loadl_epi64((const __m128i*)&in[4]); 86 in2 = _mm_loadl_epi64((const __m128i*)&in[8]); 87 in3 = _mm_loadl_epi64((const __m128i*)&in[12]); 88 // a00 a10 a20 a30 x x x x 89 // a01 a11 a21 a31 x x x x 90 // a02 a12 a22 a32 x x x x 91 // a03 a13 a23 a33 x x x x 92 if (do_two) { 93 const __m128i inB0 = _mm_loadl_epi64((const __m128i*)&in[16]); 94 const __m128i inB1 = _mm_loadl_epi64((const __m128i*)&in[20]); 95 const __m128i inB2 = _mm_loadl_epi64((const __m128i*)&in[24]); 96 const __m128i inB3 = _mm_loadl_epi64((const __m128i*)&in[28]); 97 in0 = _mm_unpacklo_epi64(in0, inB0); 98 in1 = _mm_unpacklo_epi64(in1, inB1); 99 in2 = _mm_unpacklo_epi64(in2, inB2); 100 in3 = _mm_unpacklo_epi64(in3, inB3); 101 // a00 a10 a20 a30 b00 b10 b20 b30 102 // a01 a11 a21 a31 b01 b11 b21 b31 103 // a02 a12 a22 a32 b02 b12 b22 b32 104 // a03 a13 a23 a33 b03 b13 b23 b33 105 } 106 } 107 108 // Vertical pass and subsequent transpose. 109 { 110 // First pass, c and d calculations are longer because of the "trick" 111 // multiplications. 112 const __m128i a = _mm_add_epi16(in0, in2); 113 const __m128i b = _mm_sub_epi16(in0, in2); 114 // c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in3 115 const __m128i c1 = _mm_mulhi_epi16(in1, k2); 116 const __m128i c2 = _mm_mulhi_epi16(in3, k1); 117 const __m128i c3 = _mm_sub_epi16(in1, in3); 118 const __m128i c4 = _mm_sub_epi16(c1, c2); 119 const __m128i c = _mm_add_epi16(c3, c4); 120 // d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in3 121 const __m128i d1 = _mm_mulhi_epi16(in1, k1); 122 const __m128i d2 = _mm_mulhi_epi16(in3, k2); 123 const __m128i d3 = _mm_add_epi16(in1, in3); 124 const __m128i d4 = _mm_add_epi16(d1, d2); 125 const __m128i d = _mm_add_epi16(d3, d4); 126 127 // Second pass. 128 const __m128i tmp0 = _mm_add_epi16(a, d); 129 const __m128i tmp1 = _mm_add_epi16(b, c); 130 const __m128i tmp2 = _mm_sub_epi16(b, c); 131 const __m128i tmp3 = _mm_sub_epi16(a, d); 132 133 // Transpose the two 4x4. 134 // a00 a01 a02 a03 b00 b01 b02 b03 135 // a10 a11 a12 a13 b10 b11 b12 b13 136 // a20 a21 a22 a23 b20 b21 b22 b23 137 // a30 a31 a32 a33 b30 b31 b32 b33 138 const __m128i transpose0_0 = _mm_unpacklo_epi16(tmp0, tmp1); 139 const __m128i transpose0_1 = _mm_unpacklo_epi16(tmp2, tmp3); 140 const __m128i transpose0_2 = _mm_unpackhi_epi16(tmp0, tmp1); 141 const __m128i transpose0_3 = _mm_unpackhi_epi16(tmp2, tmp3); 142 // a00 a10 a01 a11 a02 a12 a03 a13 143 // a20 a30 a21 a31 a22 a32 a23 a33 144 // b00 b10 b01 b11 b02 b12 b03 b13 145 // b20 b30 b21 b31 b22 b32 b23 b33 146 const __m128i transpose1_0 = _mm_unpacklo_epi32(transpose0_0, transpose0_1); 147 const __m128i transpose1_1 = _mm_unpacklo_epi32(transpose0_2, transpose0_3); 148 const __m128i transpose1_2 = _mm_unpackhi_epi32(transpose0_0, transpose0_1); 149 const __m128i transpose1_3 = _mm_unpackhi_epi32(transpose0_2, transpose0_3); 150 // a00 a10 a20 a30 a01 a11 a21 a31 151 // b00 b10 b20 b30 b01 b11 b21 b31 152 // a02 a12 a22 a32 a03 a13 a23 a33 153 // b02 b12 a22 b32 b03 b13 b23 b33 154 T0 = _mm_unpacklo_epi64(transpose1_0, transpose1_1); 155 T1 = _mm_unpackhi_epi64(transpose1_0, transpose1_1); 156 T2 = _mm_unpacklo_epi64(transpose1_2, transpose1_3); 157 T3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3); 158 // a00 a10 a20 a30 b00 b10 b20 b30 159 // a01 a11 a21 a31 b01 b11 b21 b31 160 // a02 a12 a22 a32 b02 b12 b22 b32 161 // a03 a13 a23 a33 b03 b13 b23 b33 162 } 163 164 // Horizontal pass and subsequent transpose. 165 { 166 // First pass, c and d calculations are longer because of the "trick" 167 // multiplications. 168 const __m128i four = _mm_set1_epi16(4); 169 const __m128i dc = _mm_add_epi16(T0, four); 170 const __m128i a = _mm_add_epi16(dc, T2); 171 const __m128i b = _mm_sub_epi16(dc, T2); 172 // c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3 173 const __m128i c1 = _mm_mulhi_epi16(T1, k2); 174 const __m128i c2 = _mm_mulhi_epi16(T3, k1); 175 const __m128i c3 = _mm_sub_epi16(T1, T3); 176 const __m128i c4 = _mm_sub_epi16(c1, c2); 177 const __m128i c = _mm_add_epi16(c3, c4); 178 // d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3 179 const __m128i d1 = _mm_mulhi_epi16(T1, k1); 180 const __m128i d2 = _mm_mulhi_epi16(T3, k2); 181 const __m128i d3 = _mm_add_epi16(T1, T3); 182 const __m128i d4 = _mm_add_epi16(d1, d2); 183 const __m128i d = _mm_add_epi16(d3, d4); 184 185 // Second pass. 186 const __m128i tmp0 = _mm_add_epi16(a, d); 187 const __m128i tmp1 = _mm_add_epi16(b, c); 188 const __m128i tmp2 = _mm_sub_epi16(b, c); 189 const __m128i tmp3 = _mm_sub_epi16(a, d); 190 const __m128i shifted0 = _mm_srai_epi16(tmp0, 3); 191 const __m128i shifted1 = _mm_srai_epi16(tmp1, 3); 192 const __m128i shifted2 = _mm_srai_epi16(tmp2, 3); 193 const __m128i shifted3 = _mm_srai_epi16(tmp3, 3); 194 195 // Transpose the two 4x4. 196 // a00 a01 a02 a03 b00 b01 b02 b03 197 // a10 a11 a12 a13 b10 b11 b12 b13 198 // a20 a21 a22 a23 b20 b21 b22 b23 199 // a30 a31 a32 a33 b30 b31 b32 b33 200 const __m128i transpose0_0 = _mm_unpacklo_epi16(shifted0, shifted1); 201 const __m128i transpose0_1 = _mm_unpacklo_epi16(shifted2, shifted3); 202 const __m128i transpose0_2 = _mm_unpackhi_epi16(shifted0, shifted1); 203 const __m128i transpose0_3 = _mm_unpackhi_epi16(shifted2, shifted3); 204 // a00 a10 a01 a11 a02 a12 a03 a13 205 // a20 a30 a21 a31 a22 a32 a23 a33 206 // b00 b10 b01 b11 b02 b12 b03 b13 207 // b20 b30 b21 b31 b22 b32 b23 b33 208 const __m128i transpose1_0 = _mm_unpacklo_epi32(transpose0_0, transpose0_1); 209 const __m128i transpose1_1 = _mm_unpacklo_epi32(transpose0_2, transpose0_3); 210 const __m128i transpose1_2 = _mm_unpackhi_epi32(transpose0_0, transpose0_1); 211 const __m128i transpose1_3 = _mm_unpackhi_epi32(transpose0_2, transpose0_3); 212 // a00 a10 a20 a30 a01 a11 a21 a31 213 // b00 b10 b20 b30 b01 b11 b21 b31 214 // a02 a12 a22 a32 a03 a13 a23 a33 215 // b02 b12 a22 b32 b03 b13 b23 b33 216 T0 = _mm_unpacklo_epi64(transpose1_0, transpose1_1); 217 T1 = _mm_unpackhi_epi64(transpose1_0, transpose1_1); 218 T2 = _mm_unpacklo_epi64(transpose1_2, transpose1_3); 219 T3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3); 220 // a00 a10 a20 a30 b00 b10 b20 b30 221 // a01 a11 a21 a31 b01 b11 b21 b31 222 // a02 a12 a22 a32 b02 b12 b22 b32 223 // a03 a13 a23 a33 b03 b13 b23 b33 224 } 225 226 // Add inverse transform to 'ref' and store. 227 { 228 const __m128i zero = _mm_setzero_si128(); 229 // Load the reference(s). 230 __m128i ref0, ref1, ref2, ref3; 231 if (do_two) { 232 // Load eight bytes/pixels per line. 233 ref0 = _mm_loadl_epi64((const __m128i*)&ref[0 * BPS]); 234 ref1 = _mm_loadl_epi64((const __m128i*)&ref[1 * BPS]); 235 ref2 = _mm_loadl_epi64((const __m128i*)&ref[2 * BPS]); 236 ref3 = _mm_loadl_epi64((const __m128i*)&ref[3 * BPS]); 237 } else { 238 // Load four bytes/pixels per line. 239 ref0 = _mm_cvtsi32_si128(WebPMemToUint32(&ref[0 * BPS])); 240 ref1 = _mm_cvtsi32_si128(WebPMemToUint32(&ref[1 * BPS])); 241 ref2 = _mm_cvtsi32_si128(WebPMemToUint32(&ref[2 * BPS])); 242 ref3 = _mm_cvtsi32_si128(WebPMemToUint32(&ref[3 * BPS])); 243 } 244 // Convert to 16b. 245 ref0 = _mm_unpacklo_epi8(ref0, zero); 246 ref1 = _mm_unpacklo_epi8(ref1, zero); 247 ref2 = _mm_unpacklo_epi8(ref2, zero); 248 ref3 = _mm_unpacklo_epi8(ref3, zero); 249 // Add the inverse transform(s). 250 ref0 = _mm_add_epi16(ref0, T0); 251 ref1 = _mm_add_epi16(ref1, T1); 252 ref2 = _mm_add_epi16(ref2, T2); 253 ref3 = _mm_add_epi16(ref3, T3); 254 // Unsigned saturate to 8b. 255 ref0 = _mm_packus_epi16(ref0, ref0); 256 ref1 = _mm_packus_epi16(ref1, ref1); 257 ref2 = _mm_packus_epi16(ref2, ref2); 258 ref3 = _mm_packus_epi16(ref3, ref3); 259 // Store the results. 260 if (do_two) { 261 // Store eight bytes/pixels per line. 262 _mm_storel_epi64((__m128i*)&dst[0 * BPS], ref0); 263 _mm_storel_epi64((__m128i*)&dst[1 * BPS], ref1); 264 _mm_storel_epi64((__m128i*)&dst[2 * BPS], ref2); 265 _mm_storel_epi64((__m128i*)&dst[3 * BPS], ref3); 266 } else { 267 // Store four bytes/pixels per line. 268 WebPUint32ToMem(&dst[0 * BPS], _mm_cvtsi128_si32(ref0)); 269 WebPUint32ToMem(&dst[1 * BPS], _mm_cvtsi128_si32(ref1)); 270 WebPUint32ToMem(&dst[2 * BPS], _mm_cvtsi128_si32(ref2)); 271 WebPUint32ToMem(&dst[3 * BPS], _mm_cvtsi128_si32(ref3)); 272 } 273 } 274 } 275 276 static void FTransformPass1(const __m128i* const in01, 277 const __m128i* const in23, 278 __m128i* const out01, 279 __m128i* const out32) { 280 const __m128i k937 = _mm_set1_epi32(937); 281 const __m128i k1812 = _mm_set1_epi32(1812); 282 283 const __m128i k88p = _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); 285 const __m128i k5352_2217p = _mm_set_epi16(2217, 5352, 2217, 5352, 286 2217, 5352, 2217, 5352); 287 const __m128i k5352_2217m = _mm_set_epi16(-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); 408 __m128i v01, v32; 409 410 // First pass 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 490 { 491 const __m128i a0 = _mm_add_epi32(row0, row2); 492 const __m128i a1 = _mm_add_epi32(row1, row3); 493 const __m128i a2 = _mm_sub_epi32(row1, row3); 494 const __m128i a3 = _mm_sub_epi32(row0, row2); 495 const __m128i b0 = _mm_srai_epi32(_mm_add_epi32(a0, a1), 1); 496 const __m128i b1 = _mm_srai_epi32(_mm_add_epi32(a3, a2), 1); 497 const __m128i b2 = _mm_srai_epi32(_mm_sub_epi32(a3, a2), 1); 498 const __m128i b3 = _mm_srai_epi32(_mm_sub_epi32(a0, a1), 1); 499 const __m128i out0 = _mm_packs_epi32(b0, b1); 500 const __m128i out1 = _mm_packs_epi32(b2, b3); 501 _mm_storeu_si128((__m128i*)&out[0], out0); 502 _mm_storeu_si128((__m128i*)&out[8], out1); 503 } 504 } 505 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 //------------------------------------------------------------------------------ 1025 // Metric 1026 1027 static WEBP_INLINE void SubtractAndAccumulate(const __m128i a, const __m128i b, 1028 __m128i* const sum) { 1029 // take abs(a-b) in 8b 1030 const __m128i a_b = _mm_subs_epu8(a, b); 1031 const __m128i b_a = _mm_subs_epu8(b, a); 1032 const __m128i abs_a_b = _mm_or_si128(a_b, b_a); 1033 // zero-extend to 16b 1034 const __m128i zero = _mm_setzero_si128(); 1035 const __m128i C0 = _mm_unpacklo_epi8(abs_a_b, zero); 1036 const __m128i C1 = _mm_unpackhi_epi8(abs_a_b, zero); 1037 // multiply with self 1038 const __m128i sum1 = _mm_madd_epi16(C0, C0); 1039 const __m128i sum2 = _mm_madd_epi16(C1, C1); 1040 *sum = _mm_add_epi32(sum1, sum2); 1041 } 1042 1043 static WEBP_INLINE int SSE_16xN(const uint8_t* a, const uint8_t* b, 1044 int num_pairs) { 1045 __m128i sum = _mm_setzero_si128(); 1046 int32_t tmp[4]; 1047 int i; 1048 1049 for (i = 0; i < num_pairs; ++i) { 1050 const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[BPS * 0]); 1051 const __m128i b0 = _mm_loadu_si128((const __m128i*)&b[BPS * 0]); 1052 const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[BPS * 1]); 1053 const __m128i b1 = _mm_loadu_si128((const __m128i*)&b[BPS * 1]); 1054 __m128i sum1, sum2; 1055 SubtractAndAccumulate(a0, b0, &sum1); 1056 SubtractAndAccumulate(a1, b1, &sum2); 1057 sum = _mm_add_epi32(sum, _mm_add_epi32(sum1, sum2)); 1058 a += 2 * BPS; 1059 b += 2 * BPS; 1060 } 1061 _mm_storeu_si128((__m128i*)tmp, sum); 1062 return (tmp[3] + tmp[2] + tmp[1] + tmp[0]); 1063 } 1064 1065 static int SSE16x16(const uint8_t* a, const uint8_t* b) { 1066 return SSE_16xN(a, b, 8); 1067 } 1068 1069 static int SSE16x8(const uint8_t* a, const uint8_t* b) { 1070 return SSE_16xN(a, b, 4); 1071 } 1072 1073 #define LOAD_8x16b(ptr) \ 1074 _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(ptr)), zero) 1075 1076 static int SSE8x8(const uint8_t* a, const uint8_t* b) { 1077 const __m128i zero = _mm_setzero_si128(); 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 1102 1103 static int SSE4x4(const uint8_t* a, const uint8_t* b) { 1104 const __m128i zero = _mm_setzero_si128(); 1105 1106 // Load values. Note that we read 8 pixels instead of 4, 1107 // but the a/b buffers are over-allocated to that effect. 1108 const __m128i a0 = _mm_loadl_epi64((const __m128i*)&a[BPS * 0]); 1109 const __m128i a1 = _mm_loadl_epi64((const __m128i*)&a[BPS * 1]); 1110 const __m128i a2 = _mm_loadl_epi64((const __m128i*)&a[BPS * 2]); 1111 const __m128i a3 = _mm_loadl_epi64((const __m128i*)&a[BPS * 3]); 1112 const __m128i b0 = _mm_loadl_epi64((const __m128i*)&b[BPS * 0]); 1113 const __m128i b1 = _mm_loadl_epi64((const __m128i*)&b[BPS * 1]); 1114 const __m128i b2 = _mm_loadl_epi64((const __m128i*)&b[BPS * 2]); 1115 const __m128i b3 = _mm_loadl_epi64((const __m128i*)&b[BPS * 3]); 1116 // Combine pair of lines. 1117 const __m128i a01 = _mm_unpacklo_epi32(a0, a1); 1118 const __m128i a23 = _mm_unpacklo_epi32(a2, a3); 1119 const __m128i b01 = _mm_unpacklo_epi32(b0, b1); 1120 const __m128i b23 = _mm_unpacklo_epi32(b2, b3); 1121 // Convert to 16b. 1122 const __m128i a01s = _mm_unpacklo_epi8(a01, zero); 1123 const __m128i a23s = _mm_unpacklo_epi8(a23, zero); 1124 const __m128i b01s = _mm_unpacklo_epi8(b01, zero); 1125 const __m128i b23s = _mm_unpacklo_epi8(b23, zero); 1126 // subtract, square and accumulate 1127 const __m128i d0 = _mm_subs_epi16(a01s, b01s); 1128 const __m128i d1 = _mm_subs_epi16(a23s, b23s); 1129 const __m128i e0 = _mm_madd_epi16(d0, d0); 1130 const __m128i e1 = _mm_madd_epi16(d1, d1); 1131 const __m128i sum = _mm_add_epi32(e0, e1); 1132 1133 int32_t tmp[4]; 1134 _mm_storeu_si128((__m128i*)tmp, sum); 1135 return (tmp[3] + tmp[2] + tmp[1] + tmp[0]); 1136 } 1137 1138 //------------------------------------------------------------------------------ 1139 // Texture distortion 1140 // 1141 // We try to match the spectral content (weighted) between source and 1142 // reconstructed samples. 1143 1144 // Hadamard transform 1145 // Returns the difference between the weighted sum of the absolute value of 1146 // transformed coefficients. 1147 static int TTransform(const uint8_t* inA, const uint8_t* inB, 1148 const uint16_t* const w) { 1149 int32_t sum[4]; 1150 __m128i tmp_0, tmp_1, tmp_2, tmp_3; 1151 const __m128i zero = _mm_setzero_si128(); 1152 1153 // Load, combine and transpose inputs. 1154 { 1155 const __m128i inA_0 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 0]); 1156 const __m128i inA_1 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 1]); 1157 const __m128i inA_2 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 2]); 1158 const __m128i inA_3 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 3]); 1159 const __m128i inB_0 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 0]); 1160 const __m128i inB_1 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 1]); 1161 const __m128i inB_2 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 2]); 1162 const __m128i inB_3 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 3]); 1163 1164 // Combine inA and inB (we'll do two transforms in parallel). 1165 const __m128i inAB_0 = _mm_unpacklo_epi8(inA_0, inB_0); 1166 const __m128i inAB_1 = _mm_unpacklo_epi8(inA_1, inB_1); 1167 const __m128i inAB_2 = _mm_unpacklo_epi8(inA_2, inB_2); 1168 const __m128i inAB_3 = _mm_unpacklo_epi8(inA_3, inB_3); 1169 // a00 b00 a01 b01 a02 b03 a03 b03 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 1171 // a20 b20 a21 b21 a22 b22 a23 b23 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 1173 1174 // Transpose the two 4x4, discarding the filling zeroes. 1175 const __m128i transpose0_0 = _mm_unpacklo_epi8(inAB_0, inAB_2); 1176 const __m128i transpose0_1 = _mm_unpacklo_epi8(inAB_1, inAB_3); 1177 // a00 a20 b00 b20 a01 a21 b01 b21 a02 a22 b02 b22 a03 a23 b03 b23 1178 // a10 a30 b10 b30 a11 a31 b11 b31 a12 a32 b12 b32 a13 a33 b13 b33 1179 const __m128i transpose1_0 = _mm_unpacklo_epi8(transpose0_0, transpose0_1); 1180 const __m128i transpose1_1 = _mm_unpackhi_epi8(transpose0_0, transpose0_1); 1181 // a00 a10 a20 a30 b00 b10 b20 b30 a01 a11 a21 a31 b01 b11 b21 b31 1182 // a02 a12 a22 a32 b02 b12 b22 b32 a03 a13 a23 a33 b03 b13 b23 b33 1183 1184 // Convert to 16b. 1185 tmp_0 = _mm_unpacklo_epi8(transpose1_0, zero); 1186 tmp_1 = _mm_unpackhi_epi8(transpose1_0, zero); 1187 tmp_2 = _mm_unpacklo_epi8(transpose1_1, zero); 1188 tmp_3 = _mm_unpackhi_epi8(transpose1_1, zero); 1189 // a00 a10 a20 a30 b00 b10 b20 b30 1190 // a01 a11 a21 a31 b01 b11 b21 b31 1191 // a02 a12 a22 a32 b02 b12 b22 b32 1192 // a03 a13 a23 a33 b03 b13 b23 b33 1193 } 1194 1195 // Horizontal pass and subsequent transpose. 1196 { 1197 // Calculate a and b (two 4x4 at once). 1198 const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2); 1199 const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3); 1200 const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3); 1201 const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2); 1202 const __m128i b0 = _mm_add_epi16(a0, a1); 1203 const __m128i b1 = _mm_add_epi16(a3, a2); 1204 const __m128i b2 = _mm_sub_epi16(a3, a2); 1205 const __m128i b3 = _mm_sub_epi16(a0, a1); 1206 // a00 a01 a02 a03 b00 b01 b02 b03 1207 // a10 a11 a12 a13 b10 b11 b12 b13 1208 // a20 a21 a22 a23 b20 b21 b22 b23 1209 // a30 a31 a32 a33 b30 b31 b32 b33 1210 1211 // Transpose the two 4x4. 1212 const __m128i transpose0_0 = _mm_unpacklo_epi16(b0, b1); 1213 const __m128i transpose0_1 = _mm_unpacklo_epi16(b2, b3); 1214 const __m128i transpose0_2 = _mm_unpackhi_epi16(b0, b1); 1215 const __m128i transpose0_3 = _mm_unpackhi_epi16(b2, b3); 1216 // a00 a10 a01 a11 a02 a12 a03 a13 1217 // a20 a30 a21 a31 a22 a32 a23 a33 1218 // b00 b10 b01 b11 b02 b12 b03 b13 1219 // b20 b30 b21 b31 b22 b32 b23 b33 1220 const __m128i transpose1_0 = _mm_unpacklo_epi32(transpose0_0, transpose0_1); 1221 const __m128i transpose1_1 = _mm_unpacklo_epi32(transpose0_2, transpose0_3); 1222 const __m128i transpose1_2 = _mm_unpackhi_epi32(transpose0_0, transpose0_1); 1223 const __m128i transpose1_3 = _mm_unpackhi_epi32(transpose0_2, transpose0_3); 1224 // a00 a10 a20 a30 a01 a11 a21 a31 1225 // b00 b10 b20 b30 b01 b11 b21 b31 1226 // a02 a12 a22 a32 a03 a13 a23 a33 1227 // b02 b12 a22 b32 b03 b13 b23 b33 1228 tmp_0 = _mm_unpacklo_epi64(transpose1_0, transpose1_1); 1229 tmp_1 = _mm_unpackhi_epi64(transpose1_0, transpose1_1); 1230 tmp_2 = _mm_unpacklo_epi64(transpose1_2, transpose1_3); 1231 tmp_3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3); 1232 // a00 a10 a20 a30 b00 b10 b20 b30 1233 // a01 a11 a21 a31 b01 b11 b21 b31 1234 // a02 a12 a22 a32 b02 b12 b22 b32 1235 // a03 a13 a23 a33 b03 b13 b23 b33 1236 } 1237 1238 // Vertical pass and difference of weighted sums. 1239 { 1240 // Load all inputs. 1241 const __m128i w_0 = _mm_loadu_si128((const __m128i*)&w[0]); 1242 const __m128i w_8 = _mm_loadu_si128((const __m128i*)&w[8]); 1243 1244 // Calculate a and b (two 4x4 at once). 1245 const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2); 1246 const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3); 1247 const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3); 1248 const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2); 1249 const __m128i b0 = _mm_add_epi16(a0, a1); 1250 const __m128i b1 = _mm_add_epi16(a3, a2); 1251 const __m128i b2 = _mm_sub_epi16(a3, a2); 1252 const __m128i b3 = _mm_sub_epi16(a0, a1); 1253 1254 // Separate the transforms of inA and inB. 1255 __m128i A_b0 = _mm_unpacklo_epi64(b0, b1); 1256 __m128i A_b2 = _mm_unpacklo_epi64(b2, b3); 1257 __m128i B_b0 = _mm_unpackhi_epi64(b0, b1); 1258 __m128i B_b2 = _mm_unpackhi_epi64(b2, b3); 1259 1260 { 1261 const __m128i d0 = _mm_sub_epi16(zero, A_b0); 1262 const __m128i d1 = _mm_sub_epi16(zero, A_b2); 1263 const __m128i d2 = _mm_sub_epi16(zero, B_b0); 1264 const __m128i d3 = _mm_sub_epi16(zero, B_b2); 1265 A_b0 = _mm_max_epi16(A_b0, d0); // abs(v), 16b 1266 A_b2 = _mm_max_epi16(A_b2, d1); 1267 B_b0 = _mm_max_epi16(B_b0, d2); 1268 B_b2 = _mm_max_epi16(B_b2, d3); 1269 } 1270 1271 // weighted sums 1272 A_b0 = _mm_madd_epi16(A_b0, w_0); 1273 A_b2 = _mm_madd_epi16(A_b2, w_8); 1274 B_b0 = _mm_madd_epi16(B_b0, w_0); 1275 B_b2 = _mm_madd_epi16(B_b2, w_8); 1276 A_b0 = _mm_add_epi32(A_b0, A_b2); 1277 B_b0 = _mm_add_epi32(B_b0, B_b2); 1278 1279 // difference of weighted sums 1280 A_b0 = _mm_sub_epi32(A_b0, B_b0); 1281 _mm_storeu_si128((__m128i*)&sum[0], A_b0); 1282 } 1283 return sum[0] + sum[1] + sum[2] + sum[3]; 1284 } 1285 1286 static int Disto4x4(const uint8_t* const a, const uint8_t* const b, 1287 const uint16_t* const w) { 1288 const int diff_sum = TTransform(a, b, w); 1289 return abs(diff_sum) >> 5; 1290 } 1291 1292 static int Disto16x16(const uint8_t* const a, const uint8_t* const b, 1293 const uint16_t* const w) { 1294 int D = 0; 1295 int x, y; 1296 for (y = 0; y < 16 * BPS; y += 4 * BPS) { 1297 for (x = 0; x < 16; x += 4) { 1298 D += Disto4x4(a + x + y, b + x + y, w); 1299 } 1300 } 1301 return D; 1302 } 1303 1304 //------------------------------------------------------------------------------ 1305 // Quantization 1306 // 1307 1308 static WEBP_INLINE int DoQuantizeBlock(int16_t in[16], int16_t out[16], 1309 const uint16_t* const sharpen, 1310 const VP8Matrix* const mtx) { 1311 const __m128i max_coeff_2047 = _mm_set1_epi16(MAX_LEVEL); 1312 const __m128i zero = _mm_setzero_si128(); 1313 __m128i coeff0, coeff8; 1314 __m128i out0, out8; 1315 __m128i packed_out; 1316 1317 // Load all inputs. 1318 __m128i in0 = _mm_loadu_si128((__m128i*)&in[0]); 1319 __m128i in8 = _mm_loadu_si128((__m128i*)&in[8]); 1320 const __m128i iq0 = _mm_loadu_si128((const __m128i*)&mtx->iq_[0]); 1321 const __m128i iq8 = _mm_loadu_si128((const __m128i*)&mtx->iq_[8]); 1322 const __m128i q0 = _mm_loadu_si128((const __m128i*)&mtx->q_[0]); 1323 const __m128i q8 = _mm_loadu_si128((const __m128i*)&mtx->q_[8]); 1324 1325 // extract sign(in) (0x0000 if positive, 0xffff if negative) 1326 const __m128i sign0 = _mm_cmpgt_epi16(zero, in0); 1327 const __m128i sign8 = _mm_cmpgt_epi16(zero, in8); 1328 1329 // coeff = abs(in) = (in ^ sign) - sign 1330 coeff0 = _mm_xor_si128(in0, sign0); 1331 coeff8 = _mm_xor_si128(in8, sign8); 1332 coeff0 = _mm_sub_epi16(coeff0, sign0); 1333 coeff8 = _mm_sub_epi16(coeff8, sign8); 1334 1335 // coeff = abs(in) + sharpen 1336 if (sharpen != NULL) { 1337 const __m128i sharpen0 = _mm_loadu_si128((const __m128i*)&sharpen[0]); 1338 const __m128i sharpen8 = _mm_loadu_si128((const __m128i*)&sharpen[8]); 1339 coeff0 = _mm_add_epi16(coeff0, sharpen0); 1340 coeff8 = _mm_add_epi16(coeff8, sharpen8); 1341 } 1342 1343 // out = (coeff * iQ + B) >> QFIX 1344 { 1345 // doing calculations with 32b precision (QFIX=17) 1346 // out = (coeff * iQ) 1347 const __m128i coeff_iQ0H = _mm_mulhi_epu16(coeff0, iq0); 1348 const __m128i coeff_iQ0L = _mm_mullo_epi16(coeff0, iq0); 1349 const __m128i coeff_iQ8H = _mm_mulhi_epu16(coeff8, iq8); 1350 const __m128i coeff_iQ8L = _mm_mullo_epi16(coeff8, iq8); 1351 __m128i out_00 = _mm_unpacklo_epi16(coeff_iQ0L, coeff_iQ0H); 1352 __m128i out_04 = _mm_unpackhi_epi16(coeff_iQ0L, coeff_iQ0H); 1353 __m128i out_08 = _mm_unpacklo_epi16(coeff_iQ8L, coeff_iQ8H); 1354 __m128i out_12 = _mm_unpackhi_epi16(coeff_iQ8L, coeff_iQ8H); 1355 // out = (coeff * iQ + B) 1356 const __m128i bias_00 = _mm_loadu_si128((const __m128i*)&mtx->bias_[0]); 1357 const __m128i bias_04 = _mm_loadu_si128((const __m128i*)&mtx->bias_[4]); 1358 const __m128i bias_08 = _mm_loadu_si128((const __m128i*)&mtx->bias_[8]); 1359 const __m128i bias_12 = _mm_loadu_si128((const __m128i*)&mtx->bias_[12]); 1360 out_00 = _mm_add_epi32(out_00, bias_00); 1361 out_04 = _mm_add_epi32(out_04, bias_04); 1362 out_08 = _mm_add_epi32(out_08, bias_08); 1363 out_12 = _mm_add_epi32(out_12, bias_12); 1364 // out = QUANTDIV(coeff, iQ, B, QFIX) 1365 out_00 = _mm_srai_epi32(out_00, QFIX); 1366 out_04 = _mm_srai_epi32(out_04, QFIX); 1367 out_08 = _mm_srai_epi32(out_08, QFIX); 1368 out_12 = _mm_srai_epi32(out_12, QFIX); 1369 1370 // pack result as 16b 1371 out0 = _mm_packs_epi32(out_00, out_04); 1372 out8 = _mm_packs_epi32(out_08, out_12); 1373 1374 // if (coeff > 2047) coeff = 2047 1375 out0 = _mm_min_epi16(out0, max_coeff_2047); 1376 out8 = _mm_min_epi16(out8, max_coeff_2047); 1377 } 1378 1379 // get sign back (if (sign[j]) out_n = -out_n) 1380 out0 = _mm_xor_si128(out0, sign0); 1381 out8 = _mm_xor_si128(out8, sign8); 1382 out0 = _mm_sub_epi16(out0, sign0); 1383 out8 = _mm_sub_epi16(out8, sign8); 1384 1385 // in = out * Q 1386 in0 = _mm_mullo_epi16(out0, q0); 1387 in8 = _mm_mullo_epi16(out8, q8); 1388 1389 _mm_storeu_si128((__m128i*)&in[0], in0); 1390 _mm_storeu_si128((__m128i*)&in[8], in8); 1391 1392 // zigzag the output before storing it. 1393 // 1394 // The zigzag pattern can almost be reproduced with a small sequence of 1395 // shuffles. After it, we only need to swap the 7th (ending up in third 1396 // position instead of twelfth) and 8th values. 1397 { 1398 __m128i outZ0, outZ8; 1399 outZ0 = _mm_shufflehi_epi16(out0, _MM_SHUFFLE(2, 1, 3, 0)); 1400 outZ0 = _mm_shuffle_epi32 (outZ0, _MM_SHUFFLE(3, 1, 2, 0)); 1401 outZ0 = _mm_shufflehi_epi16(outZ0, _MM_SHUFFLE(3, 1, 0, 2)); 1402 outZ8 = _mm_shufflelo_epi16(out8, _MM_SHUFFLE(3, 0, 2, 1)); 1403 outZ8 = _mm_shuffle_epi32 (outZ8, _MM_SHUFFLE(3, 1, 2, 0)); 1404 outZ8 = _mm_shufflelo_epi16(outZ8, _MM_SHUFFLE(1, 3, 2, 0)); 1405 _mm_storeu_si128((__m128i*)&out[0], outZ0); 1406 _mm_storeu_si128((__m128i*)&out[8], outZ8); 1407 packed_out = _mm_packs_epi16(outZ0, outZ8); 1408 } 1409 { 1410 const int16_t outZ_12 = out[12]; 1411 const int16_t outZ_3 = out[3]; 1412 out[3] = outZ_12; 1413 out[12] = outZ_3; 1414 } 1415 1416 // detect if all 'out' values are zeroes or not 1417 return (_mm_movemask_epi8(_mm_cmpeq_epi8(packed_out, zero)) != 0xffff); 1418 } 1419 1420 static int QuantizeBlock(int16_t in[16], int16_t out[16], 1421 const VP8Matrix* const mtx) { 1422 return DoQuantizeBlock(in, out, &mtx->sharpen_[0], mtx); 1423 } 1424 1425 static int QuantizeBlockWHT(int16_t in[16], int16_t out[16], 1426 const VP8Matrix* const mtx) { 1427 return DoQuantizeBlock(in, out, NULL, mtx); 1428 } 1429 1430 static int Quantize2Blocks(int16_t in[32], int16_t out[32], 1431 const VP8Matrix* const mtx) { 1432 int nz; 1433 const uint16_t* const sharpen = &mtx->sharpen_[0]; 1434 nz = DoQuantizeBlock(in + 0 * 16, out + 0 * 16, sharpen, mtx) << 0; 1435 nz |= DoQuantizeBlock(in + 1 * 16, out + 1 * 16, sharpen, mtx) << 1; 1436 return nz; 1437 } 1438 1439 //------------------------------------------------------------------------------ 1440 // Entry point 1441 1442 extern void VP8EncDspInitSSE2(void); 1443 1444 WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspInitSSE2(void) { 1445 VP8CollectHistogram = CollectHistogram; 1446 VP8EncPredLuma16 = Intra16Preds; 1447 VP8EncPredChroma8 = IntraChromaPreds; 1448 VP8EncPredLuma4 = Intra4Preds; 1449 VP8EncQuantizeBlock = QuantizeBlock; 1450 VP8EncQuantize2Blocks = Quantize2Blocks; 1451 VP8EncQuantizeBlockWHT = QuantizeBlockWHT; 1452 VP8ITransform = ITransform; 1453 VP8FTransform = FTransform; 1454 VP8FTransform2 = FTransform2; 1455 VP8FTransformWHT = FTransformWHT; 1456 VP8SSE16x16 = SSE16x16; 1457 VP8SSE16x8 = SSE16x8; 1458 VP8SSE8x8 = SSE8x8; 1459 VP8SSE4x4 = SSE4x4; 1460 VP8TDisto4x4 = Disto4x4; 1461 VP8TDisto16x16 = Disto16x16; 1462 } 1463 1464 #else // !WEBP_USE_SSE2 1465 1466 WEBP_DSP_INIT_STUB(VP8EncDspInitSSE2) 1467 1468 #endif // WEBP_USE_SSE2 1469