Path: blob/master/thirdparty/libwebp/src/dsp/dec_sse2.c
9913 views
// Copyright 2011 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// SSE2 version of some decoding functions (idct, loop filtering).10//11// Author: [email protected] (Somnath Banerjee)12// [email protected] (Christian Duvivier)1314#include "src/dsp/dsp.h"1516#if defined(WEBP_USE_SSE2)1718// The 3-coeff sparse transform in SSE2 is not really faster than the plain-C19// one it seems => disable it by default. Uncomment the following to enable:20#if !defined(USE_TRANSFORM_AC3)21#define USE_TRANSFORM_AC3 0 // ALTERNATE_CODE22#endif2324#include <emmintrin.h>25#include "src/dsp/common_sse2.h"26#include "src/dec/vp8i_dec.h"27#include "src/utils/utils.h"2829//------------------------------------------------------------------------------30// Transforms (Paragraph 14.4)3132static void Transform_SSE2(const int16_t* WEBP_RESTRICT in,33uint8_t* WEBP_RESTRICT dst, int do_two) {34// This implementation makes use of 16-bit fixed point versions of two35// multiply constants:36// K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^1637// K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^1638//39// To be able to use signed 16-bit integers, we use the following trick to40// have constants within range:41// - Associated constants are obtained by subtracting the 16-bit fixed point42// version of one:43// k = K - (1 << 16) => K = k + (1 << 16)44// K1 = 85267 => k1 = 2009145// K2 = 35468 => k2 = -3006846// - The multiplication of a variable by a constant become the sum of the47// variable and the multiplication of that variable by the associated48// constant:49// (x * K) >> 16 = (x * (k + (1 << 16))) >> 16 = ((x * k ) >> 16) + x50const __m128i k1 = _mm_set1_epi16(20091);51const __m128i k2 = _mm_set1_epi16(-30068);52__m128i T0, T1, T2, T3;5354// Load and concatenate the transform coefficients (we'll do two transforms55// in parallel). In the case of only one transform, the second half of the56// vectors will just contain random value we'll never use nor store.57__m128i in0, in1, in2, in3;58{59in0 = _mm_loadl_epi64((const __m128i*)&in[0]);60in1 = _mm_loadl_epi64((const __m128i*)&in[4]);61in2 = _mm_loadl_epi64((const __m128i*)&in[8]);62in3 = _mm_loadl_epi64((const __m128i*)&in[12]);63// a00 a10 a20 a30 x x x x64// a01 a11 a21 a31 x x x x65// a02 a12 a22 a32 x x x x66// a03 a13 a23 a33 x x x x67if (do_two) {68const __m128i inB0 = _mm_loadl_epi64((const __m128i*)&in[16]);69const __m128i inB1 = _mm_loadl_epi64((const __m128i*)&in[20]);70const __m128i inB2 = _mm_loadl_epi64((const __m128i*)&in[24]);71const __m128i inB3 = _mm_loadl_epi64((const __m128i*)&in[28]);72in0 = _mm_unpacklo_epi64(in0, inB0);73in1 = _mm_unpacklo_epi64(in1, inB1);74in2 = _mm_unpacklo_epi64(in2, inB2);75in3 = _mm_unpacklo_epi64(in3, inB3);76// a00 a10 a20 a30 b00 b10 b20 b3077// a01 a11 a21 a31 b01 b11 b21 b3178// a02 a12 a22 a32 b02 b12 b22 b3279// a03 a13 a23 a33 b03 b13 b23 b3380}81}8283// Vertical pass and subsequent transpose.84{85// First pass, c and d calculations are longer because of the "trick"86// multiplications.87const __m128i a = _mm_add_epi16(in0, in2);88const __m128i b = _mm_sub_epi16(in0, in2);89// c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in390const __m128i c1 = _mm_mulhi_epi16(in1, k2);91const __m128i c2 = _mm_mulhi_epi16(in3, k1);92const __m128i c3 = _mm_sub_epi16(in1, in3);93const __m128i c4 = _mm_sub_epi16(c1, c2);94const __m128i c = _mm_add_epi16(c3, c4);95// d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in396const __m128i d1 = _mm_mulhi_epi16(in1, k1);97const __m128i d2 = _mm_mulhi_epi16(in3, k2);98const __m128i d3 = _mm_add_epi16(in1, in3);99const __m128i d4 = _mm_add_epi16(d1, d2);100const __m128i d = _mm_add_epi16(d3, d4);101102// Second pass.103const __m128i tmp0 = _mm_add_epi16(a, d);104const __m128i tmp1 = _mm_add_epi16(b, c);105const __m128i tmp2 = _mm_sub_epi16(b, c);106const __m128i tmp3 = _mm_sub_epi16(a, d);107108// Transpose the two 4x4.109VP8Transpose_2_4x4_16b(&tmp0, &tmp1, &tmp2, &tmp3, &T0, &T1, &T2, &T3);110}111112// Horizontal pass and subsequent transpose.113{114// First pass, c and d calculations are longer because of the "trick"115// multiplications.116const __m128i four = _mm_set1_epi16(4);117const __m128i dc = _mm_add_epi16(T0, four);118const __m128i a = _mm_add_epi16(dc, T2);119const __m128i b = _mm_sub_epi16(dc, T2);120// c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3121const __m128i c1 = _mm_mulhi_epi16(T1, k2);122const __m128i c2 = _mm_mulhi_epi16(T3, k1);123const __m128i c3 = _mm_sub_epi16(T1, T3);124const __m128i c4 = _mm_sub_epi16(c1, c2);125const __m128i c = _mm_add_epi16(c3, c4);126// d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3127const __m128i d1 = _mm_mulhi_epi16(T1, k1);128const __m128i d2 = _mm_mulhi_epi16(T3, k2);129const __m128i d3 = _mm_add_epi16(T1, T3);130const __m128i d4 = _mm_add_epi16(d1, d2);131const __m128i d = _mm_add_epi16(d3, d4);132133// Second pass.134const __m128i tmp0 = _mm_add_epi16(a, d);135const __m128i tmp1 = _mm_add_epi16(b, c);136const __m128i tmp2 = _mm_sub_epi16(b, c);137const __m128i tmp3 = _mm_sub_epi16(a, d);138const __m128i shifted0 = _mm_srai_epi16(tmp0, 3);139const __m128i shifted1 = _mm_srai_epi16(tmp1, 3);140const __m128i shifted2 = _mm_srai_epi16(tmp2, 3);141const __m128i shifted3 = _mm_srai_epi16(tmp3, 3);142143// Transpose the two 4x4.144VP8Transpose_2_4x4_16b(&shifted0, &shifted1, &shifted2, &shifted3, &T0, &T1,145&T2, &T3);146}147148// Add inverse transform to 'dst' and store.149{150const __m128i zero = _mm_setzero_si128();151// Load the reference(s).152__m128i dst0, dst1, dst2, dst3;153if (do_two) {154// Load eight bytes/pixels per line.155dst0 = _mm_loadl_epi64((__m128i*)(dst + 0 * BPS));156dst1 = _mm_loadl_epi64((__m128i*)(dst + 1 * BPS));157dst2 = _mm_loadl_epi64((__m128i*)(dst + 2 * BPS));158dst3 = _mm_loadl_epi64((__m128i*)(dst + 3 * BPS));159} else {160// Load four bytes/pixels per line.161dst0 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 0 * BPS));162dst1 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 1 * BPS));163dst2 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 2 * BPS));164dst3 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 3 * BPS));165}166// Convert to 16b.167dst0 = _mm_unpacklo_epi8(dst0, zero);168dst1 = _mm_unpacklo_epi8(dst1, zero);169dst2 = _mm_unpacklo_epi8(dst2, zero);170dst3 = _mm_unpacklo_epi8(dst3, zero);171// Add the inverse transform(s).172dst0 = _mm_add_epi16(dst0, T0);173dst1 = _mm_add_epi16(dst1, T1);174dst2 = _mm_add_epi16(dst2, T2);175dst3 = _mm_add_epi16(dst3, T3);176// Unsigned saturate to 8b.177dst0 = _mm_packus_epi16(dst0, dst0);178dst1 = _mm_packus_epi16(dst1, dst1);179dst2 = _mm_packus_epi16(dst2, dst2);180dst3 = _mm_packus_epi16(dst3, dst3);181// Store the results.182if (do_two) {183// Store eight bytes/pixels per line.184_mm_storel_epi64((__m128i*)(dst + 0 * BPS), dst0);185_mm_storel_epi64((__m128i*)(dst + 1 * BPS), dst1);186_mm_storel_epi64((__m128i*)(dst + 2 * BPS), dst2);187_mm_storel_epi64((__m128i*)(dst + 3 * BPS), dst3);188} else {189// Store four bytes/pixels per line.190WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(dst0));191WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(dst1));192WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(dst2));193WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(dst3));194}195}196}197198#if (USE_TRANSFORM_AC3 == 1)199200static void TransformAC3_SSE2(const int16_t* WEBP_RESTRICT in,201uint8_t* WEBP_RESTRICT dst) {202const __m128i A = _mm_set1_epi16(in[0] + 4);203const __m128i c4 = _mm_set1_epi16(WEBP_TRANSFORM_AC3_MUL2(in[4]));204const __m128i d4 = _mm_set1_epi16(WEBP_TRANSFORM_AC3_MUL1(in[4]));205const int c1 = WEBP_TRANSFORM_AC3_MUL2(in[1]);206const int d1 = WEBP_TRANSFORM_AC3_MUL1(in[1]);207const __m128i CD = _mm_set_epi16(0, 0, 0, 0, -d1, -c1, c1, d1);208const __m128i B = _mm_adds_epi16(A, CD);209const __m128i m0 = _mm_adds_epi16(B, d4);210const __m128i m1 = _mm_adds_epi16(B, c4);211const __m128i m2 = _mm_subs_epi16(B, c4);212const __m128i m3 = _mm_subs_epi16(B, d4);213const __m128i zero = _mm_setzero_si128();214// Load the source pixels.215__m128i dst0 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 0 * BPS));216__m128i dst1 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 1 * BPS));217__m128i dst2 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 2 * BPS));218__m128i dst3 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 3 * BPS));219// Convert to 16b.220dst0 = _mm_unpacklo_epi8(dst0, zero);221dst1 = _mm_unpacklo_epi8(dst1, zero);222dst2 = _mm_unpacklo_epi8(dst2, zero);223dst3 = _mm_unpacklo_epi8(dst3, zero);224// Add the inverse transform.225dst0 = _mm_adds_epi16(dst0, _mm_srai_epi16(m0, 3));226dst1 = _mm_adds_epi16(dst1, _mm_srai_epi16(m1, 3));227dst2 = _mm_adds_epi16(dst2, _mm_srai_epi16(m2, 3));228dst3 = _mm_adds_epi16(dst3, _mm_srai_epi16(m3, 3));229// Unsigned saturate to 8b.230dst0 = _mm_packus_epi16(dst0, dst0);231dst1 = _mm_packus_epi16(dst1, dst1);232dst2 = _mm_packus_epi16(dst2, dst2);233dst3 = _mm_packus_epi16(dst3, dst3);234// Store the results.235WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(dst0));236WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(dst1));237WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(dst2));238WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(dst3));239}240241#endif // USE_TRANSFORM_AC3242243//------------------------------------------------------------------------------244// Loop Filter (Paragraph 15)245246// Compute abs(p - q) = subs(p - q) OR subs(q - p)247#define MM_ABS(p, q) _mm_or_si128( \248_mm_subs_epu8((q), (p)), \249_mm_subs_epu8((p), (q)))250251// Shift each byte of "x" by 3 bits while preserving by the sign bit.252static WEBP_INLINE void SignedShift8b_SSE2(__m128i* const x) {253const __m128i zero = _mm_setzero_si128();254const __m128i lo_0 = _mm_unpacklo_epi8(zero, *x);255const __m128i hi_0 = _mm_unpackhi_epi8(zero, *x);256const __m128i lo_1 = _mm_srai_epi16(lo_0, 3 + 8);257const __m128i hi_1 = _mm_srai_epi16(hi_0, 3 + 8);258*x = _mm_packs_epi16(lo_1, hi_1);259}260261#define FLIP_SIGN_BIT2(a, b) do { \262(a) = _mm_xor_si128(a, sign_bit); \263(b) = _mm_xor_si128(b, sign_bit); \264} while (0)265266#define FLIP_SIGN_BIT4(a, b, c, d) do { \267FLIP_SIGN_BIT2(a, b); \268FLIP_SIGN_BIT2(c, d); \269} while (0)270271// input/output is uint8_t272static WEBP_INLINE void GetNotHEV_SSE2(const __m128i* const p1,273const __m128i* const p0,274const __m128i* const q0,275const __m128i* const q1,276int hev_thresh, __m128i* const not_hev) {277const __m128i zero = _mm_setzero_si128();278const __m128i t_1 = MM_ABS(*p1, *p0);279const __m128i t_2 = MM_ABS(*q1, *q0);280281const __m128i h = _mm_set1_epi8(hev_thresh);282const __m128i t_max = _mm_max_epu8(t_1, t_2);283284const __m128i t_max_h = _mm_subs_epu8(t_max, h);285*not_hev = _mm_cmpeq_epi8(t_max_h, zero); // not_hev <= t1 && not_hev <= t2286}287288// input pixels are int8_t289static WEBP_INLINE void GetBaseDelta_SSE2(const __m128i* const p1,290const __m128i* const p0,291const __m128i* const q0,292const __m128i* const q1,293__m128i* const delta) {294// beware of addition order, for saturation!295const __m128i p1_q1 = _mm_subs_epi8(*p1, *q1); // p1 - q1296const __m128i q0_p0 = _mm_subs_epi8(*q0, *p0); // q0 - p0297const __m128i s1 = _mm_adds_epi8(p1_q1, q0_p0); // p1 - q1 + 1 * (q0 - p0)298const __m128i s2 = _mm_adds_epi8(q0_p0, s1); // p1 - q1 + 2 * (q0 - p0)299const __m128i s3 = _mm_adds_epi8(q0_p0, s2); // p1 - q1 + 3 * (q0 - p0)300*delta = s3;301}302303// input and output are int8_t304static WEBP_INLINE void DoSimpleFilter_SSE2(__m128i* const p0,305__m128i* const q0,306const __m128i* const fl) {307const __m128i k3 = _mm_set1_epi8(3);308const __m128i k4 = _mm_set1_epi8(4);309__m128i v3 = _mm_adds_epi8(*fl, k3);310__m128i v4 = _mm_adds_epi8(*fl, k4);311312SignedShift8b_SSE2(&v4); // v4 >> 3313SignedShift8b_SSE2(&v3); // v3 >> 3314*q0 = _mm_subs_epi8(*q0, v4); // q0 -= v4315*p0 = _mm_adds_epi8(*p0, v3); // p0 += v3316}317318// Updates values of 2 pixels at MB edge during complex filtering.319// Update operations:320// q = q - delta and p = p + delta; where delta = [(a_hi >> 7), (a_lo >> 7)]321// Pixels 'pi' and 'qi' are int8_t on input, uint8_t on output (sign flip).322static WEBP_INLINE void Update2Pixels_SSE2(__m128i* const pi, __m128i* const qi,323const __m128i* const a0_lo,324const __m128i* const a0_hi) {325const __m128i a1_lo = _mm_srai_epi16(*a0_lo, 7);326const __m128i a1_hi = _mm_srai_epi16(*a0_hi, 7);327const __m128i delta = _mm_packs_epi16(a1_lo, a1_hi);328const __m128i sign_bit = _mm_set1_epi8((char)0x80);329*pi = _mm_adds_epi8(*pi, delta);330*qi = _mm_subs_epi8(*qi, delta);331FLIP_SIGN_BIT2(*pi, *qi);332}333334// input pixels are uint8_t335static WEBP_INLINE void NeedsFilter_SSE2(const __m128i* const p1,336const __m128i* const p0,337const __m128i* const q0,338const __m128i* const q1,339int thresh, __m128i* const mask) {340const __m128i m_thresh = _mm_set1_epi8((char)thresh);341const __m128i t1 = MM_ABS(*p1, *q1); // abs(p1 - q1)342const __m128i kFE = _mm_set1_epi8((char)0xFE);343const __m128i t2 = _mm_and_si128(t1, kFE); // set lsb of each byte to zero344const __m128i t3 = _mm_srli_epi16(t2, 1); // abs(p1 - q1) / 2345346const __m128i t4 = MM_ABS(*p0, *q0); // abs(p0 - q0)347const __m128i t5 = _mm_adds_epu8(t4, t4); // abs(p0 - q0) * 2348const __m128i t6 = _mm_adds_epu8(t5, t3); // abs(p0-q0)*2 + abs(p1-q1)/2349350const __m128i t7 = _mm_subs_epu8(t6, m_thresh); // mask <= m_thresh351*mask = _mm_cmpeq_epi8(t7, _mm_setzero_si128());352}353354//------------------------------------------------------------------------------355// Edge filtering functions356357// Applies filter on 2 pixels (p0 and q0)358static WEBP_INLINE void DoFilter2_SSE2(__m128i* const p1, __m128i* const p0,359__m128i* const q0, __m128i* const q1,360int thresh) {361__m128i a, mask;362const __m128i sign_bit = _mm_set1_epi8((char)0x80);363// convert p1/q1 to int8_t (for GetBaseDelta_SSE2)364const __m128i p1s = _mm_xor_si128(*p1, sign_bit);365const __m128i q1s = _mm_xor_si128(*q1, sign_bit);366367NeedsFilter_SSE2(p1, p0, q0, q1, thresh, &mask);368369FLIP_SIGN_BIT2(*p0, *q0);370GetBaseDelta_SSE2(&p1s, p0, q0, &q1s, &a);371a = _mm_and_si128(a, mask); // mask filter values we don't care about372DoSimpleFilter_SSE2(p0, q0, &a);373FLIP_SIGN_BIT2(*p0, *q0);374}375376// Applies filter on 4 pixels (p1, p0, q0 and q1)377static WEBP_INLINE void DoFilter4_SSE2(__m128i* const p1, __m128i* const p0,378__m128i* const q0, __m128i* const q1,379const __m128i* const mask,380int hev_thresh) {381const __m128i zero = _mm_setzero_si128();382const __m128i sign_bit = _mm_set1_epi8((char)0x80);383const __m128i k64 = _mm_set1_epi8(64);384const __m128i k3 = _mm_set1_epi8(3);385const __m128i k4 = _mm_set1_epi8(4);386__m128i not_hev;387__m128i t1, t2, t3;388389// compute hev mask390GetNotHEV_SSE2(p1, p0, q0, q1, hev_thresh, ¬_hev);391392// convert to signed values393FLIP_SIGN_BIT4(*p1, *p0, *q0, *q1);394395t1 = _mm_subs_epi8(*p1, *q1); // p1 - q1396t1 = _mm_andnot_si128(not_hev, t1); // hev(p1 - q1)397t2 = _mm_subs_epi8(*q0, *p0); // q0 - p0398t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 1 * (q0 - p0)399t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 2 * (q0 - p0)400t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 3 * (q0 - p0)401t1 = _mm_and_si128(t1, *mask); // mask filter values we don't care about402403t2 = _mm_adds_epi8(t1, k3); // 3 * (q0 - p0) + hev(p1 - q1) + 3404t3 = _mm_adds_epi8(t1, k4); // 3 * (q0 - p0) + hev(p1 - q1) + 4405SignedShift8b_SSE2(&t2); // (3 * (q0 - p0) + hev(p1 - q1) + 3) >> 3406SignedShift8b_SSE2(&t3); // (3 * (q0 - p0) + hev(p1 - q1) + 4) >> 3407*p0 = _mm_adds_epi8(*p0, t2); // p0 += t2408*q0 = _mm_subs_epi8(*q0, t3); // q0 -= t3409FLIP_SIGN_BIT2(*p0, *q0);410411// this is equivalent to signed (a + 1) >> 1 calculation412t2 = _mm_add_epi8(t3, sign_bit);413t3 = _mm_avg_epu8(t2, zero);414t3 = _mm_sub_epi8(t3, k64);415416t3 = _mm_and_si128(not_hev, t3); // if !hev417*q1 = _mm_subs_epi8(*q1, t3); // q1 -= t3418*p1 = _mm_adds_epi8(*p1, t3); // p1 += t3419FLIP_SIGN_BIT2(*p1, *q1);420}421422// Applies filter on 6 pixels (p2, p1, p0, q0, q1 and q2)423static WEBP_INLINE void DoFilter6_SSE2(__m128i* const p2, __m128i* const p1,424__m128i* const p0, __m128i* const q0,425__m128i* const q1, __m128i* const q2,426const __m128i* const mask,427int hev_thresh) {428const __m128i zero = _mm_setzero_si128();429const __m128i sign_bit = _mm_set1_epi8((char)0x80);430__m128i a, not_hev;431432// compute hev mask433GetNotHEV_SSE2(p1, p0, q0, q1, hev_thresh, ¬_hev);434435FLIP_SIGN_BIT4(*p1, *p0, *q0, *q1);436FLIP_SIGN_BIT2(*p2, *q2);437GetBaseDelta_SSE2(p1, p0, q0, q1, &a);438439{ // do simple filter on pixels with hev440const __m128i m = _mm_andnot_si128(not_hev, *mask);441const __m128i f = _mm_and_si128(a, m);442DoSimpleFilter_SSE2(p0, q0, &f);443}444445{ // do strong filter on pixels with not hev446const __m128i k9 = _mm_set1_epi16(0x0900);447const __m128i k63 = _mm_set1_epi16(63);448449const __m128i m = _mm_and_si128(not_hev, *mask);450const __m128i f = _mm_and_si128(a, m);451452const __m128i f_lo = _mm_unpacklo_epi8(zero, f);453const __m128i f_hi = _mm_unpackhi_epi8(zero, f);454455const __m128i f9_lo = _mm_mulhi_epi16(f_lo, k9); // Filter (lo) * 9456const __m128i f9_hi = _mm_mulhi_epi16(f_hi, k9); // Filter (hi) * 9457458const __m128i a2_lo = _mm_add_epi16(f9_lo, k63); // Filter * 9 + 63459const __m128i a2_hi = _mm_add_epi16(f9_hi, k63); // Filter * 9 + 63460461const __m128i a1_lo = _mm_add_epi16(a2_lo, f9_lo); // Filter * 18 + 63462const __m128i a1_hi = _mm_add_epi16(a2_hi, f9_hi); // Filter * 18 + 63463464const __m128i a0_lo = _mm_add_epi16(a1_lo, f9_lo); // Filter * 27 + 63465const __m128i a0_hi = _mm_add_epi16(a1_hi, f9_hi); // Filter * 27 + 63466467Update2Pixels_SSE2(p2, q2, &a2_lo, &a2_hi);468Update2Pixels_SSE2(p1, q1, &a1_lo, &a1_hi);469Update2Pixels_SSE2(p0, q0, &a0_lo, &a0_hi);470}471}472473// reads 8 rows across a vertical edge.474static WEBP_INLINE void Load8x4_SSE2(const uint8_t* const b, int stride,475__m128i* const p, __m128i* const q) {476// A0 = 63 62 61 60 23 22 21 20 43 42 41 40 03 02 01 00477// A1 = 73 72 71 70 33 32 31 30 53 52 51 50 13 12 11 10478const __m128i A0 = _mm_set_epi32(479WebPMemToInt32(&b[6 * stride]), WebPMemToInt32(&b[2 * stride]),480WebPMemToInt32(&b[4 * stride]), WebPMemToInt32(&b[0 * stride]));481const __m128i A1 = _mm_set_epi32(482WebPMemToInt32(&b[7 * stride]), WebPMemToInt32(&b[3 * stride]),483WebPMemToInt32(&b[5 * stride]), WebPMemToInt32(&b[1 * stride]));484485// B0 = 53 43 52 42 51 41 50 40 13 03 12 02 11 01 10 00486// B1 = 73 63 72 62 71 61 70 60 33 23 32 22 31 21 30 20487const __m128i B0 = _mm_unpacklo_epi8(A0, A1);488const __m128i B1 = _mm_unpackhi_epi8(A0, A1);489490// C0 = 33 23 13 03 32 22 12 02 31 21 11 01 30 20 10 00491// C1 = 73 63 53 43 72 62 52 42 71 61 51 41 70 60 50 40492const __m128i C0 = _mm_unpacklo_epi16(B0, B1);493const __m128i C1 = _mm_unpackhi_epi16(B0, B1);494495// *p = 71 61 51 41 31 21 11 01 70 60 50 40 30 20 10 00496// *q = 73 63 53 43 33 23 13 03 72 62 52 42 32 22 12 02497*p = _mm_unpacklo_epi32(C0, C1);498*q = _mm_unpackhi_epi32(C0, C1);499}500501static WEBP_INLINE void Load16x4_SSE2(const uint8_t* const r0,502const uint8_t* const r8,503int stride,504__m128i* const p1, __m128i* const p0,505__m128i* const q0, __m128i* const q1) {506// Assume the pixels around the edge (|) are numbered as follows507// 00 01 | 02 03508// 10 11 | 12 13509// ... | ...510// e0 e1 | e2 e3511// f0 f1 | f2 f3512//513// r0 is pointing to the 0th row (00)514// r8 is pointing to the 8th row (80)515516// Load517// p1 = 71 61 51 41 31 21 11 01 70 60 50 40 30 20 10 00518// q0 = 73 63 53 43 33 23 13 03 72 62 52 42 32 22 12 02519// p0 = f1 e1 d1 c1 b1 a1 91 81 f0 e0 d0 c0 b0 a0 90 80520// q1 = f3 e3 d3 c3 b3 a3 93 83 f2 e2 d2 c2 b2 a2 92 82521Load8x4_SSE2(r0, stride, p1, q0);522Load8x4_SSE2(r8, stride, p0, q1);523524{525// p1 = f0 e0 d0 c0 b0 a0 90 80 70 60 50 40 30 20 10 00526// p0 = f1 e1 d1 c1 b1 a1 91 81 71 61 51 41 31 21 11 01527// q0 = f2 e2 d2 c2 b2 a2 92 82 72 62 52 42 32 22 12 02528// q1 = f3 e3 d3 c3 b3 a3 93 83 73 63 53 43 33 23 13 03529const __m128i t1 = *p1;530const __m128i t2 = *q0;531*p1 = _mm_unpacklo_epi64(t1, *p0);532*p0 = _mm_unpackhi_epi64(t1, *p0);533*q0 = _mm_unpacklo_epi64(t2, *q1);534*q1 = _mm_unpackhi_epi64(t2, *q1);535}536}537538static WEBP_INLINE void Store4x4_SSE2(__m128i* const x,539uint8_t* dst, int stride) {540int i;541for (i = 0; i < 4; ++i, dst += stride) {542WebPInt32ToMem(dst, _mm_cvtsi128_si32(*x));543*x = _mm_srli_si128(*x, 4);544}545}546547// Transpose back and store548static WEBP_INLINE void Store16x4_SSE2(const __m128i* const p1,549const __m128i* const p0,550const __m128i* const q0,551const __m128i* const q1,552uint8_t* r0, uint8_t* r8,553int stride) {554__m128i t1, p1_s, p0_s, q0_s, q1_s;555556// p0 = 71 70 61 60 51 50 41 40 31 30 21 20 11 10 01 00557// p1 = f1 f0 e1 e0 d1 d0 c1 c0 b1 b0 a1 a0 91 90 81 80558t1 = *p0;559p0_s = _mm_unpacklo_epi8(*p1, t1);560p1_s = _mm_unpackhi_epi8(*p1, t1);561562// q0 = 73 72 63 62 53 52 43 42 33 32 23 22 13 12 03 02563// q1 = f3 f2 e3 e2 d3 d2 c3 c2 b3 b2 a3 a2 93 92 83 82564t1 = *q0;565q0_s = _mm_unpacklo_epi8(t1, *q1);566q1_s = _mm_unpackhi_epi8(t1, *q1);567568// p0 = 33 32 31 30 23 22 21 20 13 12 11 10 03 02 01 00569// q0 = 73 72 71 70 63 62 61 60 53 52 51 50 43 42 41 40570t1 = p0_s;571p0_s = _mm_unpacklo_epi16(t1, q0_s);572q0_s = _mm_unpackhi_epi16(t1, q0_s);573574// p1 = b3 b2 b1 b0 a3 a2 a1 a0 93 92 91 90 83 82 81 80575// q1 = f3 f2 f1 f0 e3 e2 e1 e0 d3 d2 d1 d0 c3 c2 c1 c0576t1 = p1_s;577p1_s = _mm_unpacklo_epi16(t1, q1_s);578q1_s = _mm_unpackhi_epi16(t1, q1_s);579580Store4x4_SSE2(&p0_s, r0, stride);581r0 += 4 * stride;582Store4x4_SSE2(&q0_s, r0, stride);583584Store4x4_SSE2(&p1_s, r8, stride);585r8 += 4 * stride;586Store4x4_SSE2(&q1_s, r8, stride);587}588589//------------------------------------------------------------------------------590// Simple In-loop filtering (Paragraph 15.2)591592static void SimpleVFilter16_SSE2(uint8_t* p, int stride, int thresh) {593// Load594__m128i p1 = _mm_loadu_si128((__m128i*)&p[-2 * stride]);595__m128i p0 = _mm_loadu_si128((__m128i*)&p[-stride]);596__m128i q0 = _mm_loadu_si128((__m128i*)&p[0]);597__m128i q1 = _mm_loadu_si128((__m128i*)&p[stride]);598599DoFilter2_SSE2(&p1, &p0, &q0, &q1, thresh);600601// Store602_mm_storeu_si128((__m128i*)&p[-stride], p0);603_mm_storeu_si128((__m128i*)&p[0], q0);604}605606static void SimpleHFilter16_SSE2(uint8_t* p, int stride, int thresh) {607__m128i p1, p0, q0, q1;608609p -= 2; // beginning of p1610611Load16x4_SSE2(p, p + 8 * stride, stride, &p1, &p0, &q0, &q1);612DoFilter2_SSE2(&p1, &p0, &q0, &q1, thresh);613Store16x4_SSE2(&p1, &p0, &q0, &q1, p, p + 8 * stride, stride);614}615616static void SimpleVFilter16i_SSE2(uint8_t* p, int stride, int thresh) {617int k;618for (k = 3; k > 0; --k) {619p += 4 * stride;620SimpleVFilter16_SSE2(p, stride, thresh);621}622}623624static void SimpleHFilter16i_SSE2(uint8_t* p, int stride, int thresh) {625int k;626for (k = 3; k > 0; --k) {627p += 4;628SimpleHFilter16_SSE2(p, stride, thresh);629}630}631632//------------------------------------------------------------------------------633// Complex In-loop filtering (Paragraph 15.3)634635#define MAX_DIFF1(p3, p2, p1, p0, m) do { \636(m) = MM_ABS(p1, p0); \637(m) = _mm_max_epu8(m, MM_ABS(p3, p2)); \638(m) = _mm_max_epu8(m, MM_ABS(p2, p1)); \639} while (0)640641#define MAX_DIFF2(p3, p2, p1, p0, m) do { \642(m) = _mm_max_epu8(m, MM_ABS(p1, p0)); \643(m) = _mm_max_epu8(m, MM_ABS(p3, p2)); \644(m) = _mm_max_epu8(m, MM_ABS(p2, p1)); \645} while (0)646647#define LOAD_H_EDGES4(p, stride, e1, e2, e3, e4) do { \648(e1) = _mm_loadu_si128((__m128i*)&(p)[0 * (stride)]); \649(e2) = _mm_loadu_si128((__m128i*)&(p)[1 * (stride)]); \650(e3) = _mm_loadu_si128((__m128i*)&(p)[2 * (stride)]); \651(e4) = _mm_loadu_si128((__m128i*)&(p)[3 * (stride)]); \652} while (0)653654#define LOADUV_H_EDGE(p, u, v, stride) do { \655const __m128i U = _mm_loadl_epi64((__m128i*)&(u)[(stride)]); \656const __m128i V = _mm_loadl_epi64((__m128i*)&(v)[(stride)]); \657(p) = _mm_unpacklo_epi64(U, V); \658} while (0)659660#define LOADUV_H_EDGES4(u, v, stride, e1, e2, e3, e4) do { \661LOADUV_H_EDGE(e1, u, v, 0 * (stride)); \662LOADUV_H_EDGE(e2, u, v, 1 * (stride)); \663LOADUV_H_EDGE(e3, u, v, 2 * (stride)); \664LOADUV_H_EDGE(e4, u, v, 3 * (stride)); \665} while (0)666667#define STOREUV(p, u, v, stride) do { \668_mm_storel_epi64((__m128i*)&(u)[(stride)], p); \669(p) = _mm_srli_si128(p, 8); \670_mm_storel_epi64((__m128i*)&(v)[(stride)], p); \671} while (0)672673static WEBP_INLINE void ComplexMask_SSE2(const __m128i* const p1,674const __m128i* const p0,675const __m128i* const q0,676const __m128i* const q1,677int thresh, int ithresh,678__m128i* const mask) {679const __m128i it = _mm_set1_epi8(ithresh);680const __m128i diff = _mm_subs_epu8(*mask, it);681const __m128i thresh_mask = _mm_cmpeq_epi8(diff, _mm_setzero_si128());682__m128i filter_mask;683NeedsFilter_SSE2(p1, p0, q0, q1, thresh, &filter_mask);684*mask = _mm_and_si128(thresh_mask, filter_mask);685}686687// on macroblock edges688static void VFilter16_SSE2(uint8_t* p, int stride,689int thresh, int ithresh, int hev_thresh) {690__m128i t1;691__m128i mask;692__m128i p2, p1, p0, q0, q1, q2;693694// Load p3, p2, p1, p0695LOAD_H_EDGES4(p - 4 * stride, stride, t1, p2, p1, p0);696MAX_DIFF1(t1, p2, p1, p0, mask);697698// Load q0, q1, q2, q3699LOAD_H_EDGES4(p, stride, q0, q1, q2, t1);700MAX_DIFF2(t1, q2, q1, q0, mask);701702ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);703DoFilter6_SSE2(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);704705// Store706_mm_storeu_si128((__m128i*)&p[-3 * stride], p2);707_mm_storeu_si128((__m128i*)&p[-2 * stride], p1);708_mm_storeu_si128((__m128i*)&p[-1 * stride], p0);709_mm_storeu_si128((__m128i*)&p[+0 * stride], q0);710_mm_storeu_si128((__m128i*)&p[+1 * stride], q1);711_mm_storeu_si128((__m128i*)&p[+2 * stride], q2);712}713714static void HFilter16_SSE2(uint8_t* p, int stride,715int thresh, int ithresh, int hev_thresh) {716__m128i mask;717__m128i p3, p2, p1, p0, q0, q1, q2, q3;718719uint8_t* const b = p - 4;720Load16x4_SSE2(b, b + 8 * stride, stride, &p3, &p2, &p1, &p0);721MAX_DIFF1(p3, p2, p1, p0, mask);722723Load16x4_SSE2(p, p + 8 * stride, stride, &q0, &q1, &q2, &q3);724MAX_DIFF2(q3, q2, q1, q0, mask);725726ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);727DoFilter6_SSE2(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);728729Store16x4_SSE2(&p3, &p2, &p1, &p0, b, b + 8 * stride, stride);730Store16x4_SSE2(&q0, &q1, &q2, &q3, p, p + 8 * stride, stride);731}732733// on three inner edges734static void VFilter16i_SSE2(uint8_t* p, int stride,735int thresh, int ithresh, int hev_thresh) {736int k;737__m128i p3, p2, p1, p0; // loop invariants738739LOAD_H_EDGES4(p, stride, p3, p2, p1, p0); // prologue740741for (k = 3; k > 0; --k) {742__m128i mask, tmp1, tmp2;743uint8_t* const b = p + 2 * stride; // beginning of p1744p += 4 * stride;745746MAX_DIFF1(p3, p2, p1, p0, mask); // compute partial mask747LOAD_H_EDGES4(p, stride, p3, p2, tmp1, tmp2);748MAX_DIFF2(p3, p2, tmp1, tmp2, mask);749750// p3 and p2 are not just temporary variables here: they will be751// re-used for next span. And q2/q3 will become p1/p0 accordingly.752ComplexMask_SSE2(&p1, &p0, &p3, &p2, thresh, ithresh, &mask);753DoFilter4_SSE2(&p1, &p0, &p3, &p2, &mask, hev_thresh);754755// Store756_mm_storeu_si128((__m128i*)&b[0 * stride], p1);757_mm_storeu_si128((__m128i*)&b[1 * stride], p0);758_mm_storeu_si128((__m128i*)&b[2 * stride], p3);759_mm_storeu_si128((__m128i*)&b[3 * stride], p2);760761// rotate samples762p1 = tmp1;763p0 = tmp2;764}765}766767static void HFilter16i_SSE2(uint8_t* p, int stride,768int thresh, int ithresh, int hev_thresh) {769int k;770__m128i p3, p2, p1, p0; // loop invariants771772Load16x4_SSE2(p, p + 8 * stride, stride, &p3, &p2, &p1, &p0); // prologue773774for (k = 3; k > 0; --k) {775__m128i mask, tmp1, tmp2;776uint8_t* const b = p + 2; // beginning of p1777778p += 4; // beginning of q0 (and next span)779780MAX_DIFF1(p3, p2, p1, p0, mask); // compute partial mask781Load16x4_SSE2(p, p + 8 * stride, stride, &p3, &p2, &tmp1, &tmp2);782MAX_DIFF2(p3, p2, tmp1, tmp2, mask);783784ComplexMask_SSE2(&p1, &p0, &p3, &p2, thresh, ithresh, &mask);785DoFilter4_SSE2(&p1, &p0, &p3, &p2, &mask, hev_thresh);786787Store16x4_SSE2(&p1, &p0, &p3, &p2, b, b + 8 * stride, stride);788789// rotate samples790p1 = tmp1;791p0 = tmp2;792}793}794795// 8-pixels wide variant, for chroma filtering796static void VFilter8_SSE2(uint8_t* WEBP_RESTRICT u, uint8_t* WEBP_RESTRICT v,797int stride, int thresh, int ithresh, int hev_thresh) {798__m128i mask;799__m128i t1, p2, p1, p0, q0, q1, q2;800801// Load p3, p2, p1, p0802LOADUV_H_EDGES4(u - 4 * stride, v - 4 * stride, stride, t1, p2, p1, p0);803MAX_DIFF1(t1, p2, p1, p0, mask);804805// Load q0, q1, q2, q3806LOADUV_H_EDGES4(u, v, stride, q0, q1, q2, t1);807MAX_DIFF2(t1, q2, q1, q0, mask);808809ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);810DoFilter6_SSE2(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);811812// Store813STOREUV(p2, u, v, -3 * stride);814STOREUV(p1, u, v, -2 * stride);815STOREUV(p0, u, v, -1 * stride);816STOREUV(q0, u, v, 0 * stride);817STOREUV(q1, u, v, 1 * stride);818STOREUV(q2, u, v, 2 * stride);819}820821static void HFilter8_SSE2(uint8_t* WEBP_RESTRICT u, uint8_t* WEBP_RESTRICT v,822int stride, int thresh, int ithresh, int hev_thresh) {823__m128i mask;824__m128i p3, p2, p1, p0, q0, q1, q2, q3;825826uint8_t* const tu = u - 4;827uint8_t* const tv = v - 4;828Load16x4_SSE2(tu, tv, stride, &p3, &p2, &p1, &p0);829MAX_DIFF1(p3, p2, p1, p0, mask);830831Load16x4_SSE2(u, v, stride, &q0, &q1, &q2, &q3);832MAX_DIFF2(q3, q2, q1, q0, mask);833834ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);835DoFilter6_SSE2(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);836837Store16x4_SSE2(&p3, &p2, &p1, &p0, tu, tv, stride);838Store16x4_SSE2(&q0, &q1, &q2, &q3, u, v, stride);839}840841static void VFilter8i_SSE2(uint8_t* WEBP_RESTRICT u, uint8_t* WEBP_RESTRICT v,842int stride,843int thresh, int ithresh, int hev_thresh) {844__m128i mask;845__m128i t1, t2, p1, p0, q0, q1;846847// Load p3, p2, p1, p0848LOADUV_H_EDGES4(u, v, stride, t2, t1, p1, p0);849MAX_DIFF1(t2, t1, p1, p0, mask);850851u += 4 * stride;852v += 4 * stride;853854// Load q0, q1, q2, q3855LOADUV_H_EDGES4(u, v, stride, q0, q1, t1, t2);856MAX_DIFF2(t2, t1, q1, q0, mask);857858ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);859DoFilter4_SSE2(&p1, &p0, &q0, &q1, &mask, hev_thresh);860861// Store862STOREUV(p1, u, v, -2 * stride);863STOREUV(p0, u, v, -1 * stride);864STOREUV(q0, u, v, 0 * stride);865STOREUV(q1, u, v, 1 * stride);866}867868static void HFilter8i_SSE2(uint8_t* WEBP_RESTRICT u, uint8_t* WEBP_RESTRICT v,869int stride,870int thresh, int ithresh, int hev_thresh) {871__m128i mask;872__m128i t1, t2, p1, p0, q0, q1;873Load16x4_SSE2(u, v, stride, &t2, &t1, &p1, &p0); // p3, p2, p1, p0874MAX_DIFF1(t2, t1, p1, p0, mask);875876u += 4; // beginning of q0877v += 4;878Load16x4_SSE2(u, v, stride, &q0, &q1, &t1, &t2); // q0, q1, q2, q3879MAX_DIFF2(t2, t1, q1, q0, mask);880881ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);882DoFilter4_SSE2(&p1, &p0, &q0, &q1, &mask, hev_thresh);883884u -= 2; // beginning of p1885v -= 2;886Store16x4_SSE2(&p1, &p0, &q0, &q1, u, v, stride);887}888889//------------------------------------------------------------------------------890// 4x4 predictions891892#define DST(x, y) dst[(x) + (y) * BPS]893#define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)894895// We use the following 8b-arithmetic tricks:896// (a + 2 * b + c + 2) >> 2 = (AC + b + 1) >> 1897// where: AC = (a + c) >> 1 = [(a + c + 1) >> 1] - [(a^c) & 1]898// and:899// (a + 2 * b + c + 2) >> 2 = (AB + BC + 1) >> 1 - (ab|bc)&lsb900// where: AC = (a + b + 1) >> 1, BC = (b + c + 1) >> 1901// and ab = a ^ b, bc = b ^ c, lsb = (AC^BC)&1902903static void VE4_SSE2(uint8_t* dst) { // vertical904const __m128i one = _mm_set1_epi8(1);905const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(dst - BPS - 1));906const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1);907const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2);908const __m128i a = _mm_avg_epu8(ABCDEFGH, CDEFGH00);909const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGH00), one);910const __m128i b = _mm_subs_epu8(a, lsb);911const __m128i avg = _mm_avg_epu8(b, BCDEFGH0);912const int vals = _mm_cvtsi128_si32(avg);913int i;914for (i = 0; i < 4; ++i) {915WebPInt32ToMem(dst + i * BPS, vals);916}917}918919static void LD4_SSE2(uint8_t* dst) { // Down-Left920const __m128i one = _mm_set1_epi8(1);921const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(dst - BPS));922const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1);923const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2);924const __m128i CDEFGHH0 = _mm_insert_epi16(CDEFGH00, dst[-BPS + 7], 3);925const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, CDEFGHH0);926const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGHH0), one);927const __m128i avg2 = _mm_subs_epu8(avg1, lsb);928const __m128i abcdefg = _mm_avg_epu8(avg2, BCDEFGH0);929WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( abcdefg ));930WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1)));931WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2)));932WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3)));933}934935static void VR4_SSE2(uint8_t* dst) { // Vertical-Right936const __m128i one = _mm_set1_epi8(1);937const int I = dst[-1 + 0 * BPS];938const int J = dst[-1 + 1 * BPS];939const int K = dst[-1 + 2 * BPS];940const int X = dst[-1 - BPS];941const __m128i XABCD = _mm_loadl_epi64((__m128i*)(dst - BPS - 1));942const __m128i ABCD0 = _mm_srli_si128(XABCD, 1);943const __m128i abcd = _mm_avg_epu8(XABCD, ABCD0);944const __m128i _XABCD = _mm_slli_si128(XABCD, 1);945const __m128i IXABCD = _mm_insert_epi16(_XABCD, (short)(I | (X << 8)), 0);946const __m128i avg1 = _mm_avg_epu8(IXABCD, ABCD0);947const __m128i lsb = _mm_and_si128(_mm_xor_si128(IXABCD, ABCD0), one);948const __m128i avg2 = _mm_subs_epu8(avg1, lsb);949const __m128i efgh = _mm_avg_epu8(avg2, XABCD);950WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( abcd ));951WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32( efgh ));952WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(abcd, 1)));953WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(efgh, 1)));954955// these two are hard to implement in SSE2, so we keep the C-version:956DST(0, 2) = AVG3(J, I, X);957DST(0, 3) = AVG3(K, J, I);958}959960static void VL4_SSE2(uint8_t* dst) { // Vertical-Left961const __m128i one = _mm_set1_epi8(1);962const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(dst - BPS));963const __m128i BCDEFGH_ = _mm_srli_si128(ABCDEFGH, 1);964const __m128i CDEFGH__ = _mm_srli_si128(ABCDEFGH, 2);965const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, BCDEFGH_);966const __m128i avg2 = _mm_avg_epu8(CDEFGH__, BCDEFGH_);967const __m128i avg3 = _mm_avg_epu8(avg1, avg2);968const __m128i lsb1 = _mm_and_si128(_mm_xor_si128(avg1, avg2), one);969const __m128i ab = _mm_xor_si128(ABCDEFGH, BCDEFGH_);970const __m128i bc = _mm_xor_si128(CDEFGH__, BCDEFGH_);971const __m128i abbc = _mm_or_si128(ab, bc);972const __m128i lsb2 = _mm_and_si128(abbc, lsb1);973const __m128i avg4 = _mm_subs_epu8(avg3, lsb2);974const uint32_t extra_out =975(uint32_t)_mm_cvtsi128_si32(_mm_srli_si128(avg4, 4));976WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( avg1 ));977WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32( avg4 ));978WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg1, 1)));979WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg4, 1)));980981// these two are hard to get and irregular982DST(3, 2) = (extra_out >> 0) & 0xff;983DST(3, 3) = (extra_out >> 8) & 0xff;984}985986static void RD4_SSE2(uint8_t* dst) { // Down-right987const __m128i one = _mm_set1_epi8(1);988const __m128i XABCD = _mm_loadl_epi64((__m128i*)(dst - BPS - 1));989const __m128i ____XABCD = _mm_slli_si128(XABCD, 4);990const uint32_t I = dst[-1 + 0 * BPS];991const uint32_t J = dst[-1 + 1 * BPS];992const uint32_t K = dst[-1 + 2 * BPS];993const uint32_t L = dst[-1 + 3 * BPS];994const __m128i LKJI_____ =995_mm_cvtsi32_si128((int)(L | (K << 8) | (J << 16) | (I << 24)));996const __m128i LKJIXABCD = _mm_or_si128(LKJI_____, ____XABCD);997const __m128i KJIXABCD_ = _mm_srli_si128(LKJIXABCD, 1);998const __m128i JIXABCD__ = _mm_srli_si128(LKJIXABCD, 2);999const __m128i avg1 = _mm_avg_epu8(JIXABCD__, LKJIXABCD);1000const __m128i lsb = _mm_and_si128(_mm_xor_si128(JIXABCD__, LKJIXABCD), one);1001const __m128i avg2 = _mm_subs_epu8(avg1, lsb);1002const __m128i abcdefg = _mm_avg_epu8(avg2, KJIXABCD_);1003WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32( abcdefg ));1004WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1)));1005WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2)));1006WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3)));1007}10081009#undef DST1010#undef AVG310111012//------------------------------------------------------------------------------1013// Luma 16x1610141015static WEBP_INLINE void TrueMotion_SSE2(uint8_t* dst, int size) {1016const uint8_t* top = dst - BPS;1017const __m128i zero = _mm_setzero_si128();1018int y;1019if (size == 4) {1020const __m128i top_values = _mm_cvtsi32_si128(WebPMemToInt32(top));1021const __m128i top_base = _mm_unpacklo_epi8(top_values, zero);1022for (y = 0; y < 4; ++y, dst += BPS) {1023const int val = dst[-1] - top[-1];1024const __m128i base = _mm_set1_epi16(val);1025const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero);1026WebPInt32ToMem(dst, _mm_cvtsi128_si32(out));1027}1028} else if (size == 8) {1029const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);1030const __m128i top_base = _mm_unpacklo_epi8(top_values, zero);1031for (y = 0; y < 8; ++y, dst += BPS) {1032const int val = dst[-1] - top[-1];1033const __m128i base = _mm_set1_epi16(val);1034const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero);1035_mm_storel_epi64((__m128i*)dst, out);1036}1037} else {1038const __m128i top_values = _mm_loadu_si128((const __m128i*)top);1039const __m128i top_base_0 = _mm_unpacklo_epi8(top_values, zero);1040const __m128i top_base_1 = _mm_unpackhi_epi8(top_values, zero);1041for (y = 0; y < 16; ++y, dst += BPS) {1042const int val = dst[-1] - top[-1];1043const __m128i base = _mm_set1_epi16(val);1044const __m128i out_0 = _mm_add_epi16(base, top_base_0);1045const __m128i out_1 = _mm_add_epi16(base, top_base_1);1046const __m128i out = _mm_packus_epi16(out_0, out_1);1047_mm_storeu_si128((__m128i*)dst, out);1048}1049}1050}10511052static void TM4_SSE2(uint8_t* dst) { TrueMotion_SSE2(dst, 4); }1053static void TM8uv_SSE2(uint8_t* dst) { TrueMotion_SSE2(dst, 8); }1054static void TM16_SSE2(uint8_t* dst) { TrueMotion_SSE2(dst, 16); }10551056static void VE16_SSE2(uint8_t* dst) {1057const __m128i top = _mm_loadu_si128((const __m128i*)(dst - BPS));1058int j;1059for (j = 0; j < 16; ++j) {1060_mm_storeu_si128((__m128i*)(dst + j * BPS), top);1061}1062}10631064static void HE16_SSE2(uint8_t* dst) { // horizontal1065int j;1066for (j = 16; j > 0; --j) {1067const __m128i values = _mm_set1_epi8((char)dst[-1]);1068_mm_storeu_si128((__m128i*)dst, values);1069dst += BPS;1070}1071}10721073static WEBP_INLINE void Put16_SSE2(uint8_t v, uint8_t* dst) {1074int j;1075const __m128i values = _mm_set1_epi8((char)v);1076for (j = 0; j < 16; ++j) {1077_mm_storeu_si128((__m128i*)(dst + j * BPS), values);1078}1079}10801081static void DC16_SSE2(uint8_t* dst) { // DC1082const __m128i zero = _mm_setzero_si128();1083const __m128i top = _mm_loadu_si128((const __m128i*)(dst - BPS));1084const __m128i sad8x2 = _mm_sad_epu8(top, zero);1085// sum the two sads: sad8x2[0:1] + sad8x2[8:9]1086const __m128i sum = _mm_add_epi16(sad8x2, _mm_shuffle_epi32(sad8x2, 2));1087int left = 0;1088int j;1089for (j = 0; j < 16; ++j) {1090left += dst[-1 + j * BPS];1091}1092{1093const int DC = _mm_cvtsi128_si32(sum) + left + 16;1094Put16_SSE2(DC >> 5, dst);1095}1096}10971098static void DC16NoTop_SSE2(uint8_t* dst) { // DC with top samples unavailable1099int DC = 8;1100int j;1101for (j = 0; j < 16; ++j) {1102DC += dst[-1 + j * BPS];1103}1104Put16_SSE2(DC >> 4, dst);1105}11061107static void DC16NoLeft_SSE2(uint8_t* dst) { // DC with left samples unavailable1108const __m128i zero = _mm_setzero_si128();1109const __m128i top = _mm_loadu_si128((const __m128i*)(dst - BPS));1110const __m128i sad8x2 = _mm_sad_epu8(top, zero);1111// sum the two sads: sad8x2[0:1] + sad8x2[8:9]1112const __m128i sum = _mm_add_epi16(sad8x2, _mm_shuffle_epi32(sad8x2, 2));1113const int DC = _mm_cvtsi128_si32(sum) + 8;1114Put16_SSE2(DC >> 4, dst);1115}11161117static void DC16NoTopLeft_SSE2(uint8_t* dst) { // DC with no top & left samples1118Put16_SSE2(0x80, dst);1119}11201121//------------------------------------------------------------------------------1122// Chroma11231124static void VE8uv_SSE2(uint8_t* dst) { // vertical1125int j;1126const __m128i top = _mm_loadl_epi64((const __m128i*)(dst - BPS));1127for (j = 0; j < 8; ++j) {1128_mm_storel_epi64((__m128i*)(dst + j * BPS), top);1129}1130}11311132// helper for chroma-DC predictions1133static WEBP_INLINE void Put8x8uv_SSE2(uint8_t v, uint8_t* dst) {1134int j;1135const __m128i values = _mm_set1_epi8((char)v);1136for (j = 0; j < 8; ++j) {1137_mm_storel_epi64((__m128i*)(dst + j * BPS), values);1138}1139}11401141static void DC8uv_SSE2(uint8_t* dst) { // DC1142const __m128i zero = _mm_setzero_si128();1143const __m128i top = _mm_loadl_epi64((const __m128i*)(dst - BPS));1144const __m128i sum = _mm_sad_epu8(top, zero);1145int left = 0;1146int j;1147for (j = 0; j < 8; ++j) {1148left += dst[-1 + j * BPS];1149}1150{1151const int DC = _mm_cvtsi128_si32(sum) + left + 8;1152Put8x8uv_SSE2(DC >> 4, dst);1153}1154}11551156static void DC8uvNoLeft_SSE2(uint8_t* dst) { // DC with no left samples1157const __m128i zero = _mm_setzero_si128();1158const __m128i top = _mm_loadl_epi64((const __m128i*)(dst - BPS));1159const __m128i sum = _mm_sad_epu8(top, zero);1160const int DC = _mm_cvtsi128_si32(sum) + 4;1161Put8x8uv_SSE2(DC >> 3, dst);1162}11631164static void DC8uvNoTop_SSE2(uint8_t* dst) { // DC with no top samples1165int dc0 = 4;1166int i;1167for (i = 0; i < 8; ++i) {1168dc0 += dst[-1 + i * BPS];1169}1170Put8x8uv_SSE2(dc0 >> 3, dst);1171}11721173static void DC8uvNoTopLeft_SSE2(uint8_t* dst) { // DC with nothing1174Put8x8uv_SSE2(0x80, dst);1175}11761177//------------------------------------------------------------------------------1178// Entry point11791180extern void VP8DspInitSSE2(void);11811182WEBP_TSAN_IGNORE_FUNCTION void VP8DspInitSSE2(void) {1183VP8Transform = Transform_SSE2;1184#if (USE_TRANSFORM_AC3 == 1)1185VP8TransformAC3 = TransformAC3_SSE2;1186#endif11871188VP8VFilter16 = VFilter16_SSE2;1189VP8HFilter16 = HFilter16_SSE2;1190VP8VFilter8 = VFilter8_SSE2;1191VP8HFilter8 = HFilter8_SSE2;1192VP8VFilter16i = VFilter16i_SSE2;1193VP8HFilter16i = HFilter16i_SSE2;1194VP8VFilter8i = VFilter8i_SSE2;1195VP8HFilter8i = HFilter8i_SSE2;11961197VP8SimpleVFilter16 = SimpleVFilter16_SSE2;1198VP8SimpleHFilter16 = SimpleHFilter16_SSE2;1199VP8SimpleVFilter16i = SimpleVFilter16i_SSE2;1200VP8SimpleHFilter16i = SimpleHFilter16i_SSE2;12011202VP8PredLuma4[1] = TM4_SSE2;1203VP8PredLuma4[2] = VE4_SSE2;1204VP8PredLuma4[4] = RD4_SSE2;1205VP8PredLuma4[5] = VR4_SSE2;1206VP8PredLuma4[6] = LD4_SSE2;1207VP8PredLuma4[7] = VL4_SSE2;12081209VP8PredLuma16[0] = DC16_SSE2;1210VP8PredLuma16[1] = TM16_SSE2;1211VP8PredLuma16[2] = VE16_SSE2;1212VP8PredLuma16[3] = HE16_SSE2;1213VP8PredLuma16[4] = DC16NoTop_SSE2;1214VP8PredLuma16[5] = DC16NoLeft_SSE2;1215VP8PredLuma16[6] = DC16NoTopLeft_SSE2;12161217VP8PredChroma8[0] = DC8uv_SSE2;1218VP8PredChroma8[1] = TM8uv_SSE2;1219VP8PredChroma8[2] = VE8uv_SSE2;1220VP8PredChroma8[4] = DC8uvNoTop_SSE2;1221VP8PredChroma8[5] = DC8uvNoLeft_SSE2;1222VP8PredChroma8[6] = DC8uvNoTopLeft_SSE2;1223}12241225#else // !WEBP_USE_SSE212261227WEBP_DSP_INIT_STUB(VP8DspInitSSE2)12281229#endif // WEBP_USE_SSE2123012311232