Path: blob/master/3rdparty/libwebp/src/dsp/dec_sse2.c
16348 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* in, uint8_t* dst, int do_two) {33// This implementation makes use of 16-bit fixed point versions of two34// multiply constants:35// K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^1636// K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^1637//38// To be able to use signed 16-bit integers, we use the following trick to39// have constants within range:40// - Associated constants are obtained by subtracting the 16-bit fixed point41// version of one:42// k = K - (1 << 16) => K = k + (1 << 16)43// K1 = 85267 => k1 = 2009144// K2 = 35468 => k2 = -3006845// - The multiplication of a variable by a constant become the sum of the46// variable and the multiplication of that variable by the associated47// constant:48// (x * K) >> 16 = (x * (k + (1 << 16))) >> 16 = ((x * k ) >> 16) + x49const __m128i k1 = _mm_set1_epi16(20091);50const __m128i k2 = _mm_set1_epi16(-30068);51__m128i T0, T1, T2, T3;5253// Load and concatenate the transform coefficients (we'll do two transforms54// in parallel). In the case of only one transform, the second half of the55// vectors will just contain random value we'll never use nor store.56__m128i in0, in1, in2, in3;57{58in0 = _mm_loadl_epi64((const __m128i*)&in[0]);59in1 = _mm_loadl_epi64((const __m128i*)&in[4]);60in2 = _mm_loadl_epi64((const __m128i*)&in[8]);61in3 = _mm_loadl_epi64((const __m128i*)&in[12]);62// a00 a10 a20 a30 x x x x63// a01 a11 a21 a31 x x x x64// a02 a12 a22 a32 x x x x65// a03 a13 a23 a33 x x x x66if (do_two) {67const __m128i inB0 = _mm_loadl_epi64((const __m128i*)&in[16]);68const __m128i inB1 = _mm_loadl_epi64((const __m128i*)&in[20]);69const __m128i inB2 = _mm_loadl_epi64((const __m128i*)&in[24]);70const __m128i inB3 = _mm_loadl_epi64((const __m128i*)&in[28]);71in0 = _mm_unpacklo_epi64(in0, inB0);72in1 = _mm_unpacklo_epi64(in1, inB1);73in2 = _mm_unpacklo_epi64(in2, inB2);74in3 = _mm_unpacklo_epi64(in3, inB3);75// a00 a10 a20 a30 b00 b10 b20 b3076// a01 a11 a21 a31 b01 b11 b21 b3177// a02 a12 a22 a32 b02 b12 b22 b3278// a03 a13 a23 a33 b03 b13 b23 b3379}80}8182// Vertical pass and subsequent transpose.83{84// First pass, c and d calculations are longer because of the "trick"85// multiplications.86const __m128i a = _mm_add_epi16(in0, in2);87const __m128i b = _mm_sub_epi16(in0, in2);88// c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in389const __m128i c1 = _mm_mulhi_epi16(in1, k2);90const __m128i c2 = _mm_mulhi_epi16(in3, k1);91const __m128i c3 = _mm_sub_epi16(in1, in3);92const __m128i c4 = _mm_sub_epi16(c1, c2);93const __m128i c = _mm_add_epi16(c3, c4);94// d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in395const __m128i d1 = _mm_mulhi_epi16(in1, k1);96const __m128i d2 = _mm_mulhi_epi16(in3, k2);97const __m128i d3 = _mm_add_epi16(in1, in3);98const __m128i d4 = _mm_add_epi16(d1, d2);99const __m128i d = _mm_add_epi16(d3, d4);100101// Second pass.102const __m128i tmp0 = _mm_add_epi16(a, d);103const __m128i tmp1 = _mm_add_epi16(b, c);104const __m128i tmp2 = _mm_sub_epi16(b, c);105const __m128i tmp3 = _mm_sub_epi16(a, d);106107// Transpose the two 4x4.108VP8Transpose_2_4x4_16b(&tmp0, &tmp1, &tmp2, &tmp3, &T0, &T1, &T2, &T3);109}110111// Horizontal pass and subsequent transpose.112{113// First pass, c and d calculations are longer because of the "trick"114// multiplications.115const __m128i four = _mm_set1_epi16(4);116const __m128i dc = _mm_add_epi16(T0, four);117const __m128i a = _mm_add_epi16(dc, T2);118const __m128i b = _mm_sub_epi16(dc, T2);119// c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3120const __m128i c1 = _mm_mulhi_epi16(T1, k2);121const __m128i c2 = _mm_mulhi_epi16(T3, k1);122const __m128i c3 = _mm_sub_epi16(T1, T3);123const __m128i c4 = _mm_sub_epi16(c1, c2);124const __m128i c = _mm_add_epi16(c3, c4);125// d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3126const __m128i d1 = _mm_mulhi_epi16(T1, k1);127const __m128i d2 = _mm_mulhi_epi16(T3, k2);128const __m128i d3 = _mm_add_epi16(T1, T3);129const __m128i d4 = _mm_add_epi16(d1, d2);130const __m128i d = _mm_add_epi16(d3, d4);131132// Second pass.133const __m128i tmp0 = _mm_add_epi16(a, d);134const __m128i tmp1 = _mm_add_epi16(b, c);135const __m128i tmp2 = _mm_sub_epi16(b, c);136const __m128i tmp3 = _mm_sub_epi16(a, d);137const __m128i shifted0 = _mm_srai_epi16(tmp0, 3);138const __m128i shifted1 = _mm_srai_epi16(tmp1, 3);139const __m128i shifted2 = _mm_srai_epi16(tmp2, 3);140const __m128i shifted3 = _mm_srai_epi16(tmp3, 3);141142// Transpose the two 4x4.143VP8Transpose_2_4x4_16b(&shifted0, &shifted1, &shifted2, &shifted3, &T0, &T1,144&T2, &T3);145}146147// Add inverse transform to 'dst' and store.148{149const __m128i zero = _mm_setzero_si128();150// Load the reference(s).151__m128i dst0, dst1, dst2, dst3;152if (do_two) {153// Load eight bytes/pixels per line.154dst0 = _mm_loadl_epi64((__m128i*)(dst + 0 * BPS));155dst1 = _mm_loadl_epi64((__m128i*)(dst + 1 * BPS));156dst2 = _mm_loadl_epi64((__m128i*)(dst + 2 * BPS));157dst3 = _mm_loadl_epi64((__m128i*)(dst + 3 * BPS));158} else {159// Load four bytes/pixels per line.160dst0 = _mm_cvtsi32_si128(WebPMemToUint32(dst + 0 * BPS));161dst1 = _mm_cvtsi32_si128(WebPMemToUint32(dst + 1 * BPS));162dst2 = _mm_cvtsi32_si128(WebPMemToUint32(dst + 2 * BPS));163dst3 = _mm_cvtsi32_si128(WebPMemToUint32(dst + 3 * BPS));164}165// Convert to 16b.166dst0 = _mm_unpacklo_epi8(dst0, zero);167dst1 = _mm_unpacklo_epi8(dst1, zero);168dst2 = _mm_unpacklo_epi8(dst2, zero);169dst3 = _mm_unpacklo_epi8(dst3, zero);170// Add the inverse transform(s).171dst0 = _mm_add_epi16(dst0, T0);172dst1 = _mm_add_epi16(dst1, T1);173dst2 = _mm_add_epi16(dst2, T2);174dst3 = _mm_add_epi16(dst3, T3);175// Unsigned saturate to 8b.176dst0 = _mm_packus_epi16(dst0, dst0);177dst1 = _mm_packus_epi16(dst1, dst1);178dst2 = _mm_packus_epi16(dst2, dst2);179dst3 = _mm_packus_epi16(dst3, dst3);180// Store the results.181if (do_two) {182// Store eight bytes/pixels per line.183_mm_storel_epi64((__m128i*)(dst + 0 * BPS), dst0);184_mm_storel_epi64((__m128i*)(dst + 1 * BPS), dst1);185_mm_storel_epi64((__m128i*)(dst + 2 * BPS), dst2);186_mm_storel_epi64((__m128i*)(dst + 3 * BPS), dst3);187} else {188// Store four bytes/pixels per line.189WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(dst0));190WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(dst1));191WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(dst2));192WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(dst3));193}194}195}196197#if (USE_TRANSFORM_AC3 == 1)198#define MUL(a, b) (((a) * (b)) >> 16)199static void TransformAC3(const int16_t* in, uint8_t* dst) {200static const int kC1 = 20091 + (1 << 16);201static const int kC2 = 35468;202const __m128i A = _mm_set1_epi16(in[0] + 4);203const __m128i c4 = _mm_set1_epi16(MUL(in[4], kC2));204const __m128i d4 = _mm_set1_epi16(MUL(in[4], kC1));205const int c1 = MUL(in[1], kC2);206const int d1 = MUL(in[1], kC1);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(WebPMemToUint32(dst + 0 * BPS));216__m128i dst1 = _mm_cvtsi32_si128(WebPMemToUint32(dst + 1 * BPS));217__m128i dst2 = _mm_cvtsi32_si128(WebPMemToUint32(dst + 2 * BPS));218__m128i dst3 = _mm_cvtsi32_si128(WebPMemToUint32(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.235WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(dst0));236WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(dst1));237WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(dst2));238WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(dst3));239}240#undef MUL241#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) { \262(a) = _mm_xor_si128(a, sign_bit); \263(b) = _mm_xor_si128(b, sign_bit); \264}265266#define FLIP_SIGN_BIT4(a, b, c, d) { \267FLIP_SIGN_BIT2(a, b); \268FLIP_SIGN_BIT2(c, d); \269}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(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(thresh);341const __m128i t1 = MM_ABS(*p1, *q1); // abs(p1 - q1)342const __m128i kFE = _mm_set1_epi8(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(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(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(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(479WebPMemToUint32(&b[6 * stride]), WebPMemToUint32(&b[2 * stride]),480WebPMemToUint32(&b[4 * stride]), WebPMemToUint32(&b[0 * stride]));481const __m128i A1 = _mm_set_epi32(482WebPMemToUint32(&b[7 * stride]), WebPMemToUint32(&b[3 * stride]),483WebPMemToUint32(&b[5 * stride]), WebPMemToUint32(&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) {542WebPUint32ToMem(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) { \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}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) { \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}666667#define STOREUV(p, u, v, stride) { \668_mm_storel_epi64((__m128i*)&(u)[(stride)], p); \669(p) = _mm_srli_si128(p, 8); \670_mm_storel_epi64((__m128i*)&(v)[(stride)], p); \671}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* u, uint8_t* v, int stride,797int 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* u, uint8_t* v, int stride,822int 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* u, uint8_t* v, int stride,842int thresh, int ithresh, int hev_thresh) {843__m128i mask;844__m128i t1, t2, p1, p0, q0, q1;845846// Load p3, p2, p1, p0847LOADUV_H_EDGES4(u, v, stride, t2, t1, p1, p0);848MAX_DIFF1(t2, t1, p1, p0, mask);849850u += 4 * stride;851v += 4 * stride;852853// Load q0, q1, q2, q3854LOADUV_H_EDGES4(u, v, stride, q0, q1, t1, t2);855MAX_DIFF2(t2, t1, q1, q0, mask);856857ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);858DoFilter4_SSE2(&p1, &p0, &q0, &q1, &mask, hev_thresh);859860// Store861STOREUV(p1, u, v, -2 * stride);862STOREUV(p0, u, v, -1 * stride);863STOREUV(q0, u, v, 0 * stride);864STOREUV(q1, u, v, 1 * stride);865}866867static void HFilter8i_SSE2(uint8_t* u, uint8_t* v, int stride,868int thresh, int ithresh, int hev_thresh) {869__m128i mask;870__m128i t1, t2, p1, p0, q0, q1;871Load16x4_SSE2(u, v, stride, &t2, &t1, &p1, &p0); // p3, p2, p1, p0872MAX_DIFF1(t2, t1, p1, p0, mask);873874u += 4; // beginning of q0875v += 4;876Load16x4_SSE2(u, v, stride, &q0, &q1, &t1, &t2); // q0, q1, q2, q3877MAX_DIFF2(t2, t1, q1, q0, mask);878879ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);880DoFilter4_SSE2(&p1, &p0, &q0, &q1, &mask, hev_thresh);881882u -= 2; // beginning of p1883v -= 2;884Store16x4_SSE2(&p1, &p0, &q0, &q1, u, v, stride);885}886887//------------------------------------------------------------------------------888// 4x4 predictions889890#define DST(x, y) dst[(x) + (y) * BPS]891#define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)892893// We use the following 8b-arithmetic tricks:894// (a + 2 * b + c + 2) >> 2 = (AC + b + 1) >> 1895// where: AC = (a + c) >> 1 = [(a + c + 1) >> 1] - [(a^c) & 1]896// and:897// (a + 2 * b + c + 2) >> 2 = (AB + BC + 1) >> 1 - (ab|bc)&lsb898// where: AC = (a + b + 1) >> 1, BC = (b + c + 1) >> 1899// and ab = a ^ b, bc = b ^ c, lsb = (AC^BC)&1900901static void VE4_SSE2(uint8_t* dst) { // vertical902const __m128i one = _mm_set1_epi8(1);903const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(dst - BPS - 1));904const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1);905const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2);906const __m128i a = _mm_avg_epu8(ABCDEFGH, CDEFGH00);907const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGH00), one);908const __m128i b = _mm_subs_epu8(a, lsb);909const __m128i avg = _mm_avg_epu8(b, BCDEFGH0);910const uint32_t vals = _mm_cvtsi128_si32(avg);911int i;912for (i = 0; i < 4; ++i) {913WebPUint32ToMem(dst + i * BPS, vals);914}915}916917static void LD4_SSE2(uint8_t* dst) { // Down-Left918const __m128i one = _mm_set1_epi8(1);919const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(dst - BPS));920const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1);921const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2);922const __m128i CDEFGHH0 = _mm_insert_epi16(CDEFGH00, dst[-BPS + 7], 3);923const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, CDEFGHH0);924const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGHH0), one);925const __m128i avg2 = _mm_subs_epu8(avg1, lsb);926const __m128i abcdefg = _mm_avg_epu8(avg2, BCDEFGH0);927WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( abcdefg ));928WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1)));929WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2)));930WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3)));931}932933static void VR4_SSE2(uint8_t* dst) { // Vertical-Right934const __m128i one = _mm_set1_epi8(1);935const int I = dst[-1 + 0 * BPS];936const int J = dst[-1 + 1 * BPS];937const int K = dst[-1 + 2 * BPS];938const int X = dst[-1 - BPS];939const __m128i XABCD = _mm_loadl_epi64((__m128i*)(dst - BPS - 1));940const __m128i ABCD0 = _mm_srli_si128(XABCD, 1);941const __m128i abcd = _mm_avg_epu8(XABCD, ABCD0);942const __m128i _XABCD = _mm_slli_si128(XABCD, 1);943const __m128i IXABCD = _mm_insert_epi16(_XABCD, I | (X << 8), 0);944const __m128i avg1 = _mm_avg_epu8(IXABCD, ABCD0);945const __m128i lsb = _mm_and_si128(_mm_xor_si128(IXABCD, ABCD0), one);946const __m128i avg2 = _mm_subs_epu8(avg1, lsb);947const __m128i efgh = _mm_avg_epu8(avg2, XABCD);948WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( abcd ));949WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32( efgh ));950WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(abcd, 1)));951WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(efgh, 1)));952953// these two are hard to implement in SSE2, so we keep the C-version:954DST(0, 2) = AVG3(J, I, X);955DST(0, 3) = AVG3(K, J, I);956}957958static void VL4_SSE2(uint8_t* dst) { // Vertical-Left959const __m128i one = _mm_set1_epi8(1);960const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(dst - BPS));961const __m128i BCDEFGH_ = _mm_srli_si128(ABCDEFGH, 1);962const __m128i CDEFGH__ = _mm_srli_si128(ABCDEFGH, 2);963const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, BCDEFGH_);964const __m128i avg2 = _mm_avg_epu8(CDEFGH__, BCDEFGH_);965const __m128i avg3 = _mm_avg_epu8(avg1, avg2);966const __m128i lsb1 = _mm_and_si128(_mm_xor_si128(avg1, avg2), one);967const __m128i ab = _mm_xor_si128(ABCDEFGH, BCDEFGH_);968const __m128i bc = _mm_xor_si128(CDEFGH__, BCDEFGH_);969const __m128i abbc = _mm_or_si128(ab, bc);970const __m128i lsb2 = _mm_and_si128(abbc, lsb1);971const __m128i avg4 = _mm_subs_epu8(avg3, lsb2);972const uint32_t extra_out = _mm_cvtsi128_si32(_mm_srli_si128(avg4, 4));973WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( avg1 ));974WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32( avg4 ));975WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg1, 1)));976WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg4, 1)));977978// these two are hard to get and irregular979DST(3, 2) = (extra_out >> 0) & 0xff;980DST(3, 3) = (extra_out >> 8) & 0xff;981}982983static void RD4_SSE2(uint8_t* dst) { // Down-right984const __m128i one = _mm_set1_epi8(1);985const __m128i XABCD = _mm_loadl_epi64((__m128i*)(dst - BPS - 1));986const __m128i ____XABCD = _mm_slli_si128(XABCD, 4);987const uint32_t I = dst[-1 + 0 * BPS];988const uint32_t J = dst[-1 + 1 * BPS];989const uint32_t K = dst[-1 + 2 * BPS];990const uint32_t L = dst[-1 + 3 * BPS];991const __m128i LKJI_____ =992_mm_cvtsi32_si128(L | (K << 8) | (J << 16) | (I << 24));993const __m128i LKJIXABCD = _mm_or_si128(LKJI_____, ____XABCD);994const __m128i KJIXABCD_ = _mm_srli_si128(LKJIXABCD, 1);995const __m128i JIXABCD__ = _mm_srli_si128(LKJIXABCD, 2);996const __m128i avg1 = _mm_avg_epu8(JIXABCD__, LKJIXABCD);997const __m128i lsb = _mm_and_si128(_mm_xor_si128(JIXABCD__, LKJIXABCD), one);998const __m128i avg2 = _mm_subs_epu8(avg1, lsb);999const __m128i abcdefg = _mm_avg_epu8(avg2, KJIXABCD_);1000WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32( abcdefg ));1001WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1)));1002WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2)));1003WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3)));1004}10051006#undef DST1007#undef AVG310081009//------------------------------------------------------------------------------1010// Luma 16x1610111012static WEBP_INLINE void TrueMotion_SSE2(uint8_t* dst, int size) {1013const uint8_t* top = dst - BPS;1014const __m128i zero = _mm_setzero_si128();1015int y;1016if (size == 4) {1017const __m128i top_values = _mm_cvtsi32_si128(WebPMemToUint32(top));1018const __m128i top_base = _mm_unpacklo_epi8(top_values, zero);1019for (y = 0; y < 4; ++y, dst += BPS) {1020const int val = dst[-1] - top[-1];1021const __m128i base = _mm_set1_epi16(val);1022const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero);1023WebPUint32ToMem(dst, _mm_cvtsi128_si32(out));1024}1025} else if (size == 8) {1026const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);1027const __m128i top_base = _mm_unpacklo_epi8(top_values, zero);1028for (y = 0; y < 8; ++y, dst += BPS) {1029const int val = dst[-1] - top[-1];1030const __m128i base = _mm_set1_epi16(val);1031const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero);1032_mm_storel_epi64((__m128i*)dst, out);1033}1034} else {1035const __m128i top_values = _mm_loadu_si128((const __m128i*)top);1036const __m128i top_base_0 = _mm_unpacklo_epi8(top_values, zero);1037const __m128i top_base_1 = _mm_unpackhi_epi8(top_values, zero);1038for (y = 0; y < 16; ++y, dst += BPS) {1039const int val = dst[-1] - top[-1];1040const __m128i base = _mm_set1_epi16(val);1041const __m128i out_0 = _mm_add_epi16(base, top_base_0);1042const __m128i out_1 = _mm_add_epi16(base, top_base_1);1043const __m128i out = _mm_packus_epi16(out_0, out_1);1044_mm_storeu_si128((__m128i*)dst, out);1045}1046}1047}10481049static void TM4_SSE2(uint8_t* dst) { TrueMotion_SSE2(dst, 4); }1050static void TM8uv_SSE2(uint8_t* dst) { TrueMotion_SSE2(dst, 8); }1051static void TM16_SSE2(uint8_t* dst) { TrueMotion_SSE2(dst, 16); }10521053static void VE16_SSE2(uint8_t* dst) {1054const __m128i top = _mm_loadu_si128((const __m128i*)(dst - BPS));1055int j;1056for (j = 0; j < 16; ++j) {1057_mm_storeu_si128((__m128i*)(dst + j * BPS), top);1058}1059}10601061static void HE16_SSE2(uint8_t* dst) { // horizontal1062int j;1063for (j = 16; j > 0; --j) {1064const __m128i values = _mm_set1_epi8(dst[-1]);1065_mm_storeu_si128((__m128i*)dst, values);1066dst += BPS;1067}1068}10691070static WEBP_INLINE void Put16_SSE2(uint8_t v, uint8_t* dst) {1071int j;1072const __m128i values = _mm_set1_epi8(v);1073for (j = 0; j < 16; ++j) {1074_mm_storeu_si128((__m128i*)(dst + j * BPS), values);1075}1076}10771078static void DC16_SSE2(uint8_t* dst) { // DC1079const __m128i zero = _mm_setzero_si128();1080const __m128i top = _mm_loadu_si128((const __m128i*)(dst - BPS));1081const __m128i sad8x2 = _mm_sad_epu8(top, zero);1082// sum the two sads: sad8x2[0:1] + sad8x2[8:9]1083const __m128i sum = _mm_add_epi16(sad8x2, _mm_shuffle_epi32(sad8x2, 2));1084int left = 0;1085int j;1086for (j = 0; j < 16; ++j) {1087left += dst[-1 + j * BPS];1088}1089{1090const int DC = _mm_cvtsi128_si32(sum) + left + 16;1091Put16_SSE2(DC >> 5, dst);1092}1093}10941095static void DC16NoTop_SSE2(uint8_t* dst) { // DC with top samples unavailable1096int DC = 8;1097int j;1098for (j = 0; j < 16; ++j) {1099DC += dst[-1 + j * BPS];1100}1101Put16_SSE2(DC >> 4, dst);1102}11031104static void DC16NoLeft_SSE2(uint8_t* dst) { // DC with left samples unavailable1105const __m128i zero = _mm_setzero_si128();1106const __m128i top = _mm_loadu_si128((const __m128i*)(dst - BPS));1107const __m128i sad8x2 = _mm_sad_epu8(top, zero);1108// sum the two sads: sad8x2[0:1] + sad8x2[8:9]1109const __m128i sum = _mm_add_epi16(sad8x2, _mm_shuffle_epi32(sad8x2, 2));1110const int DC = _mm_cvtsi128_si32(sum) + 8;1111Put16_SSE2(DC >> 4, dst);1112}11131114static void DC16NoTopLeft_SSE2(uint8_t* dst) { // DC with no top & left samples1115Put16_SSE2(0x80, dst);1116}11171118//------------------------------------------------------------------------------1119// Chroma11201121static void VE8uv_SSE2(uint8_t* dst) { // vertical1122int j;1123const __m128i top = _mm_loadl_epi64((const __m128i*)(dst - BPS));1124for (j = 0; j < 8; ++j) {1125_mm_storel_epi64((__m128i*)(dst + j * BPS), top);1126}1127}11281129// helper for chroma-DC predictions1130static WEBP_INLINE void Put8x8uv_SSE2(uint8_t v, uint8_t* dst) {1131int j;1132const __m128i values = _mm_set1_epi8(v);1133for (j = 0; j < 8; ++j) {1134_mm_storel_epi64((__m128i*)(dst + j * BPS), values);1135}1136}11371138static void DC8uv_SSE2(uint8_t* dst) { // DC1139const __m128i zero = _mm_setzero_si128();1140const __m128i top = _mm_loadl_epi64((const __m128i*)(dst - BPS));1141const __m128i sum = _mm_sad_epu8(top, zero);1142int left = 0;1143int j;1144for (j = 0; j < 8; ++j) {1145left += dst[-1 + j * BPS];1146}1147{1148const int DC = _mm_cvtsi128_si32(sum) + left + 8;1149Put8x8uv_SSE2(DC >> 4, dst);1150}1151}11521153static void DC8uvNoLeft_SSE2(uint8_t* dst) { // DC with no left samples1154const __m128i zero = _mm_setzero_si128();1155const __m128i top = _mm_loadl_epi64((const __m128i*)(dst - BPS));1156const __m128i sum = _mm_sad_epu8(top, zero);1157const int DC = _mm_cvtsi128_si32(sum) + 4;1158Put8x8uv_SSE2(DC >> 3, dst);1159}11601161static void DC8uvNoTop_SSE2(uint8_t* dst) { // DC with no top samples1162int dc0 = 4;1163int i;1164for (i = 0; i < 8; ++i) {1165dc0 += dst[-1 + i * BPS];1166}1167Put8x8uv_SSE2(dc0 >> 3, dst);1168}11691170static void DC8uvNoTopLeft_SSE2(uint8_t* dst) { // DC with nothing1171Put8x8uv_SSE2(0x80, dst);1172}11731174//------------------------------------------------------------------------------1175// Entry point11761177extern void VP8DspInitSSE2(void);11781179WEBP_TSAN_IGNORE_FUNCTION void VP8DspInitSSE2(void) {1180VP8Transform = Transform_SSE2;1181#if (USE_TRANSFORM_AC3 == 1)1182VP8TransformAC3 = TransformAC3_SSE2;1183#endif11841185VP8VFilter16 = VFilter16_SSE2;1186VP8HFilter16 = HFilter16_SSE2;1187VP8VFilter8 = VFilter8_SSE2;1188VP8HFilter8 = HFilter8_SSE2;1189VP8VFilter16i = VFilter16i_SSE2;1190VP8HFilter16i = HFilter16i_SSE2;1191VP8VFilter8i = VFilter8i_SSE2;1192VP8HFilter8i = HFilter8i_SSE2;11931194VP8SimpleVFilter16 = SimpleVFilter16_SSE2;1195VP8SimpleHFilter16 = SimpleHFilter16_SSE2;1196VP8SimpleVFilter16i = SimpleVFilter16i_SSE2;1197VP8SimpleHFilter16i = SimpleHFilter16i_SSE2;11981199VP8PredLuma4[1] = TM4_SSE2;1200VP8PredLuma4[2] = VE4_SSE2;1201VP8PredLuma4[4] = RD4_SSE2;1202VP8PredLuma4[5] = VR4_SSE2;1203VP8PredLuma4[6] = LD4_SSE2;1204VP8PredLuma4[7] = VL4_SSE2;12051206VP8PredLuma16[0] = DC16_SSE2;1207VP8PredLuma16[1] = TM16_SSE2;1208VP8PredLuma16[2] = VE16_SSE2;1209VP8PredLuma16[3] = HE16_SSE2;1210VP8PredLuma16[4] = DC16NoTop_SSE2;1211VP8PredLuma16[5] = DC16NoLeft_SSE2;1212VP8PredLuma16[6] = DC16NoTopLeft_SSE2;12131214VP8PredChroma8[0] = DC8uv_SSE2;1215VP8PredChroma8[1] = TM8uv_SSE2;1216VP8PredChroma8[2] = VE8uv_SSE2;1217VP8PredChroma8[4] = DC8uvNoTop_SSE2;1218VP8PredChroma8[5] = DC8uvNoLeft_SSE2;1219VP8PredChroma8[6] = DC8uvNoTopLeft_SSE2;1220}12211222#else // !WEBP_USE_SSE212231224WEBP_DSP_INIT_STUB(VP8DspInitSSE2)12251226#endif // WEBP_USE_SSE2122712281229