Path: blob/master/thirdparty/libwebp/src/dsp/dec_sse2.c
21407 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>2526#include "src/dec/vp8i_dec.h"27#include "src/dsp/common_sse2.h"28#include "src/dsp/cpu.h"29#include "src/utils/utils.h"30#include "src/webp/types.h"3132//------------------------------------------------------------------------------33// Transforms (Paragraph 14.4)3435static void Transform_SSE2(const int16_t* WEBP_RESTRICT in,36uint8_t* WEBP_RESTRICT dst, int do_two) {37// This implementation makes use of 16-bit fixed point versions of two38// multiply constants:39// K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^1640// K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^1641//42// To be able to use signed 16-bit integers, we use the following trick to43// have constants within range:44// - Associated constants are obtained by subtracting the 16-bit fixed point45// version of one:46// k = K - (1 << 16) => K = k + (1 << 16)47// K1 = 85267 => k1 = 2009148// K2 = 35468 => k2 = -3006849// - The multiplication of a variable by a constant become the sum of the50// variable and the multiplication of that variable by the associated51// constant:52// (x * K) >> 16 = (x * (k + (1 << 16))) >> 16 = ((x * k ) >> 16) + x53const __m128i k1 = _mm_set1_epi16(20091);54const __m128i k2 = _mm_set1_epi16(-30068);55__m128i T0, T1, T2, T3;5657// Load and concatenate the transform coefficients (we'll do two transforms58// in parallel). In the case of only one transform, the second half of the59// vectors will just contain random value we'll never use nor store.60__m128i in0, in1, in2, in3;61{62in0 = _mm_loadl_epi64((const __m128i*)&in[0]);63in1 = _mm_loadl_epi64((const __m128i*)&in[4]);64in2 = _mm_loadl_epi64((const __m128i*)&in[8]);65in3 = _mm_loadl_epi64((const __m128i*)&in[12]);66// a00 a10 a20 a30 x x x x67// a01 a11 a21 a31 x x x x68// a02 a12 a22 a32 x x x x69// a03 a13 a23 a33 x x x x70if (do_two) {71const __m128i inB0 = _mm_loadl_epi64((const __m128i*)&in[16]);72const __m128i inB1 = _mm_loadl_epi64((const __m128i*)&in[20]);73const __m128i inB2 = _mm_loadl_epi64((const __m128i*)&in[24]);74const __m128i inB3 = _mm_loadl_epi64((const __m128i*)&in[28]);75in0 = _mm_unpacklo_epi64(in0, inB0);76in1 = _mm_unpacklo_epi64(in1, inB1);77in2 = _mm_unpacklo_epi64(in2, inB2);78in3 = _mm_unpacklo_epi64(in3, inB3);79// a00 a10 a20 a30 b00 b10 b20 b3080// a01 a11 a21 a31 b01 b11 b21 b3181// a02 a12 a22 a32 b02 b12 b22 b3282// a03 a13 a23 a33 b03 b13 b23 b3383}84}8586// Vertical pass and subsequent transpose.87{88// First pass, c and d calculations are longer because of the "trick"89// multiplications.90const __m128i a = _mm_add_epi16(in0, in2);91const __m128i b = _mm_sub_epi16(in0, in2);92// c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in393const __m128i c1 = _mm_mulhi_epi16(in1, k2);94const __m128i c2 = _mm_mulhi_epi16(in3, k1);95const __m128i c3 = _mm_sub_epi16(in1, in3);96const __m128i c4 = _mm_sub_epi16(c1, c2);97const __m128i c = _mm_add_epi16(c3, c4);98// d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in399const __m128i d1 = _mm_mulhi_epi16(in1, k1);100const __m128i d2 = _mm_mulhi_epi16(in3, k2);101const __m128i d3 = _mm_add_epi16(in1, in3);102const __m128i d4 = _mm_add_epi16(d1, d2);103const __m128i d = _mm_add_epi16(d3, d4);104105// Second pass.106const __m128i tmp0 = _mm_add_epi16(a, d);107const __m128i tmp1 = _mm_add_epi16(b, c);108const __m128i tmp2 = _mm_sub_epi16(b, c);109const __m128i tmp3 = _mm_sub_epi16(a, d);110111// Transpose the two 4x4.112VP8Transpose_2_4x4_16b(&tmp0, &tmp1, &tmp2, &tmp3, &T0, &T1, &T2, &T3);113}114115// Horizontal pass and subsequent transpose.116{117// First pass, c and d calculations are longer because of the "trick"118// multiplications.119const __m128i four = _mm_set1_epi16(4);120const __m128i dc = _mm_add_epi16(T0, four);121const __m128i a = _mm_add_epi16(dc, T2);122const __m128i b = _mm_sub_epi16(dc, T2);123// c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3124const __m128i c1 = _mm_mulhi_epi16(T1, k2);125const __m128i c2 = _mm_mulhi_epi16(T3, k1);126const __m128i c3 = _mm_sub_epi16(T1, T3);127const __m128i c4 = _mm_sub_epi16(c1, c2);128const __m128i c = _mm_add_epi16(c3, c4);129// d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3130const __m128i d1 = _mm_mulhi_epi16(T1, k1);131const __m128i d2 = _mm_mulhi_epi16(T3, k2);132const __m128i d3 = _mm_add_epi16(T1, T3);133const __m128i d4 = _mm_add_epi16(d1, d2);134const __m128i d = _mm_add_epi16(d3, d4);135136// Second pass.137const __m128i tmp0 = _mm_add_epi16(a, d);138const __m128i tmp1 = _mm_add_epi16(b, c);139const __m128i tmp2 = _mm_sub_epi16(b, c);140const __m128i tmp3 = _mm_sub_epi16(a, d);141const __m128i shifted0 = _mm_srai_epi16(tmp0, 3);142const __m128i shifted1 = _mm_srai_epi16(tmp1, 3);143const __m128i shifted2 = _mm_srai_epi16(tmp2, 3);144const __m128i shifted3 = _mm_srai_epi16(tmp3, 3);145146// Transpose the two 4x4.147VP8Transpose_2_4x4_16b(&shifted0, &shifted1, &shifted2, &shifted3, &T0, &T1,148&T2, &T3);149}150151// Add inverse transform to 'dst' and store.152{153const __m128i zero = _mm_setzero_si128();154// Load the reference(s).155__m128i dst0, dst1, dst2, dst3;156if (do_two) {157// Load eight bytes/pixels per line.158dst0 = _mm_loadl_epi64((__m128i*)(dst + 0 * BPS));159dst1 = _mm_loadl_epi64((__m128i*)(dst + 1 * BPS));160dst2 = _mm_loadl_epi64((__m128i*)(dst + 2 * BPS));161dst3 = _mm_loadl_epi64((__m128i*)(dst + 3 * BPS));162} else {163// Load four bytes/pixels per line.164dst0 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 0 * BPS));165dst1 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 1 * BPS));166dst2 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 2 * BPS));167dst3 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 3 * BPS));168}169// Convert to 16b.170dst0 = _mm_unpacklo_epi8(dst0, zero);171dst1 = _mm_unpacklo_epi8(dst1, zero);172dst2 = _mm_unpacklo_epi8(dst2, zero);173dst3 = _mm_unpacklo_epi8(dst3, zero);174// Add the inverse transform(s).175dst0 = _mm_add_epi16(dst0, T0);176dst1 = _mm_add_epi16(dst1, T1);177dst2 = _mm_add_epi16(dst2, T2);178dst3 = _mm_add_epi16(dst3, T3);179// Unsigned saturate to 8b.180dst0 = _mm_packus_epi16(dst0, dst0);181dst1 = _mm_packus_epi16(dst1, dst1);182dst2 = _mm_packus_epi16(dst2, dst2);183dst3 = _mm_packus_epi16(dst3, dst3);184// Store the results.185if (do_two) {186// Store eight bytes/pixels per line.187_mm_storel_epi64((__m128i*)(dst + 0 * BPS), dst0);188_mm_storel_epi64((__m128i*)(dst + 1 * BPS), dst1);189_mm_storel_epi64((__m128i*)(dst + 2 * BPS), dst2);190_mm_storel_epi64((__m128i*)(dst + 3 * BPS), dst3);191} else {192// Store four bytes/pixels per line.193WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(dst0));194WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(dst1));195WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(dst2));196WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(dst3));197}198}199}200201#if (USE_TRANSFORM_AC3 == 1)202203static void TransformAC3_SSE2(const int16_t* WEBP_RESTRICT in,204uint8_t* WEBP_RESTRICT dst) {205const __m128i A = _mm_set1_epi16(in[0] + 4);206const __m128i c4 = _mm_set1_epi16(WEBP_TRANSFORM_AC3_MUL2(in[4]));207const __m128i d4 = _mm_set1_epi16(WEBP_TRANSFORM_AC3_MUL1(in[4]));208const int c1 = WEBP_TRANSFORM_AC3_MUL2(in[1]);209const int d1 = WEBP_TRANSFORM_AC3_MUL1(in[1]);210const __m128i CD = _mm_set_epi16(0, 0, 0, 0, -d1, -c1, c1, d1);211const __m128i B = _mm_adds_epi16(A, CD);212const __m128i m0 = _mm_adds_epi16(B, d4);213const __m128i m1 = _mm_adds_epi16(B, c4);214const __m128i m2 = _mm_subs_epi16(B, c4);215const __m128i m3 = _mm_subs_epi16(B, d4);216const __m128i zero = _mm_setzero_si128();217// Load the source pixels.218__m128i dst0 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 0 * BPS));219__m128i dst1 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 1 * BPS));220__m128i dst2 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 2 * BPS));221__m128i dst3 = _mm_cvtsi32_si128(WebPMemToInt32(dst + 3 * BPS));222// Convert to 16b.223dst0 = _mm_unpacklo_epi8(dst0, zero);224dst1 = _mm_unpacklo_epi8(dst1, zero);225dst2 = _mm_unpacklo_epi8(dst2, zero);226dst3 = _mm_unpacklo_epi8(dst3, zero);227// Add the inverse transform.228dst0 = _mm_adds_epi16(dst0, _mm_srai_epi16(m0, 3));229dst1 = _mm_adds_epi16(dst1, _mm_srai_epi16(m1, 3));230dst2 = _mm_adds_epi16(dst2, _mm_srai_epi16(m2, 3));231dst3 = _mm_adds_epi16(dst3, _mm_srai_epi16(m3, 3));232// Unsigned saturate to 8b.233dst0 = _mm_packus_epi16(dst0, dst0);234dst1 = _mm_packus_epi16(dst1, dst1);235dst2 = _mm_packus_epi16(dst2, dst2);236dst3 = _mm_packus_epi16(dst3, dst3);237// Store the results.238WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(dst0));239WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(dst1));240WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(dst2));241WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(dst3));242}243244#endif // USE_TRANSFORM_AC3245246//------------------------------------------------------------------------------247// Loop Filter (Paragraph 15)248249// Compute abs(p - q) = subs(p - q) OR subs(q - p)250#define MM_ABS(p, q) _mm_or_si128( \251_mm_subs_epu8((q), (p)), \252_mm_subs_epu8((p), (q)))253254// Shift each byte of "x" by 3 bits while preserving by the sign bit.255static WEBP_INLINE void SignedShift8b_SSE2(__m128i* const x) {256const __m128i zero = _mm_setzero_si128();257const __m128i lo_0 = _mm_unpacklo_epi8(zero, *x);258const __m128i hi_0 = _mm_unpackhi_epi8(zero, *x);259const __m128i lo_1 = _mm_srai_epi16(lo_0, 3 + 8);260const __m128i hi_1 = _mm_srai_epi16(hi_0, 3 + 8);261*x = _mm_packs_epi16(lo_1, hi_1);262}263264#define FLIP_SIGN_BIT2(a, b) do { \265(a) = _mm_xor_si128(a, sign_bit); \266(b) = _mm_xor_si128(b, sign_bit); \267} while (0)268269#define FLIP_SIGN_BIT4(a, b, c, d) do { \270FLIP_SIGN_BIT2(a, b); \271FLIP_SIGN_BIT2(c, d); \272} while (0)273274// input/output is uint8_t275static WEBP_INLINE void GetNotHEV_SSE2(const __m128i* const p1,276const __m128i* const p0,277const __m128i* const q0,278const __m128i* const q1,279int hev_thresh, __m128i* const not_hev) {280const __m128i zero = _mm_setzero_si128();281const __m128i t_1 = MM_ABS(*p1, *p0);282const __m128i t_2 = MM_ABS(*q1, *q0);283284const __m128i h = _mm_set1_epi8(hev_thresh);285const __m128i t_max = _mm_max_epu8(t_1, t_2);286287const __m128i t_max_h = _mm_subs_epu8(t_max, h);288*not_hev = _mm_cmpeq_epi8(t_max_h, zero); // not_hev <= t1 && not_hev <= t2289}290291// input pixels are int8_t292static WEBP_INLINE void GetBaseDelta_SSE2(const __m128i* const p1,293const __m128i* const p0,294const __m128i* const q0,295const __m128i* const q1,296__m128i* const delta) {297// beware of addition order, for saturation!298const __m128i p1_q1 = _mm_subs_epi8(*p1, *q1); // p1 - q1299const __m128i q0_p0 = _mm_subs_epi8(*q0, *p0); // q0 - p0300const __m128i s1 = _mm_adds_epi8(p1_q1, q0_p0); // p1 - q1 + 1 * (q0 - p0)301const __m128i s2 = _mm_adds_epi8(q0_p0, s1); // p1 - q1 + 2 * (q0 - p0)302const __m128i s3 = _mm_adds_epi8(q0_p0, s2); // p1 - q1 + 3 * (q0 - p0)303*delta = s3;304}305306// input and output are int8_t307static WEBP_INLINE void DoSimpleFilter_SSE2(__m128i* const p0,308__m128i* const q0,309const __m128i* const fl) {310const __m128i k3 = _mm_set1_epi8(3);311const __m128i k4 = _mm_set1_epi8(4);312__m128i v3 = _mm_adds_epi8(*fl, k3);313__m128i v4 = _mm_adds_epi8(*fl, k4);314315SignedShift8b_SSE2(&v4); // v4 >> 3316SignedShift8b_SSE2(&v3); // v3 >> 3317*q0 = _mm_subs_epi8(*q0, v4); // q0 -= v4318*p0 = _mm_adds_epi8(*p0, v3); // p0 += v3319}320321// Updates values of 2 pixels at MB edge during complex filtering.322// Update operations:323// q = q - delta and p = p + delta; where delta = [(a_hi >> 7), (a_lo >> 7)]324// Pixels 'pi' and 'qi' are int8_t on input, uint8_t on output (sign flip).325static WEBP_INLINE void Update2Pixels_SSE2(__m128i* const pi, __m128i* const qi,326const __m128i* const a0_lo,327const __m128i* const a0_hi) {328const __m128i a1_lo = _mm_srai_epi16(*a0_lo, 7);329const __m128i a1_hi = _mm_srai_epi16(*a0_hi, 7);330const __m128i delta = _mm_packs_epi16(a1_lo, a1_hi);331const __m128i sign_bit = _mm_set1_epi8((char)0x80);332*pi = _mm_adds_epi8(*pi, delta);333*qi = _mm_subs_epi8(*qi, delta);334FLIP_SIGN_BIT2(*pi, *qi);335}336337// input pixels are uint8_t338static WEBP_INLINE void NeedsFilter_SSE2(const __m128i* const p1,339const __m128i* const p0,340const __m128i* const q0,341const __m128i* const q1,342int thresh, __m128i* const mask) {343const __m128i m_thresh = _mm_set1_epi8((char)thresh);344const __m128i t1 = MM_ABS(*p1, *q1); // abs(p1 - q1)345const __m128i kFE = _mm_set1_epi8((char)0xFE);346const __m128i t2 = _mm_and_si128(t1, kFE); // set lsb of each byte to zero347const __m128i t3 = _mm_srli_epi16(t2, 1); // abs(p1 - q1) / 2348349const __m128i t4 = MM_ABS(*p0, *q0); // abs(p0 - q0)350const __m128i t5 = _mm_adds_epu8(t4, t4); // abs(p0 - q0) * 2351const __m128i t6 = _mm_adds_epu8(t5, t3); // abs(p0-q0)*2 + abs(p1-q1)/2352353const __m128i t7 = _mm_subs_epu8(t6, m_thresh); // mask <= m_thresh354*mask = _mm_cmpeq_epi8(t7, _mm_setzero_si128());355}356357//------------------------------------------------------------------------------358// Edge filtering functions359360// Applies filter on 2 pixels (p0 and q0)361static WEBP_INLINE void DoFilter2_SSE2(__m128i* const p1, __m128i* const p0,362__m128i* const q0, __m128i* const q1,363int thresh) {364__m128i a, mask;365const __m128i sign_bit = _mm_set1_epi8((char)0x80);366// convert p1/q1 to int8_t (for GetBaseDelta_SSE2)367const __m128i p1s = _mm_xor_si128(*p1, sign_bit);368const __m128i q1s = _mm_xor_si128(*q1, sign_bit);369370NeedsFilter_SSE2(p1, p0, q0, q1, thresh, &mask);371372FLIP_SIGN_BIT2(*p0, *q0);373GetBaseDelta_SSE2(&p1s, p0, q0, &q1s, &a);374a = _mm_and_si128(a, mask); // mask filter values we don't care about375DoSimpleFilter_SSE2(p0, q0, &a);376FLIP_SIGN_BIT2(*p0, *q0);377}378379// Applies filter on 4 pixels (p1, p0, q0 and q1)380static WEBP_INLINE void DoFilter4_SSE2(__m128i* const p1, __m128i* const p0,381__m128i* const q0, __m128i* const q1,382const __m128i* const mask,383int hev_thresh) {384const __m128i zero = _mm_setzero_si128();385const __m128i sign_bit = _mm_set1_epi8((char)0x80);386const __m128i k64 = _mm_set1_epi8(64);387const __m128i k3 = _mm_set1_epi8(3);388const __m128i k4 = _mm_set1_epi8(4);389__m128i not_hev;390__m128i t1, t2, t3;391392// compute hev mask393GetNotHEV_SSE2(p1, p0, q0, q1, hev_thresh, ¬_hev);394395// convert to signed values396FLIP_SIGN_BIT4(*p1, *p0, *q0, *q1);397398t1 = _mm_subs_epi8(*p1, *q1); // p1 - q1399t1 = _mm_andnot_si128(not_hev, t1); // hev(p1 - q1)400t2 = _mm_subs_epi8(*q0, *p0); // q0 - p0401t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 1 * (q0 - p0)402t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 2 * (q0 - p0)403t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 3 * (q0 - p0)404t1 = _mm_and_si128(t1, *mask); // mask filter values we don't care about405406t2 = _mm_adds_epi8(t1, k3); // 3 * (q0 - p0) + hev(p1 - q1) + 3407t3 = _mm_adds_epi8(t1, k4); // 3 * (q0 - p0) + hev(p1 - q1) + 4408SignedShift8b_SSE2(&t2); // (3 * (q0 - p0) + hev(p1 - q1) + 3) >> 3409SignedShift8b_SSE2(&t3); // (3 * (q0 - p0) + hev(p1 - q1) + 4) >> 3410*p0 = _mm_adds_epi8(*p0, t2); // p0 += t2411*q0 = _mm_subs_epi8(*q0, t3); // q0 -= t3412FLIP_SIGN_BIT2(*p0, *q0);413414// this is equivalent to signed (a + 1) >> 1 calculation415t2 = _mm_add_epi8(t3, sign_bit);416t3 = _mm_avg_epu8(t2, zero);417t3 = _mm_sub_epi8(t3, k64);418419t3 = _mm_and_si128(not_hev, t3); // if !hev420*q1 = _mm_subs_epi8(*q1, t3); // q1 -= t3421*p1 = _mm_adds_epi8(*p1, t3); // p1 += t3422FLIP_SIGN_BIT2(*p1, *q1);423}424425// Applies filter on 6 pixels (p2, p1, p0, q0, q1 and q2)426static WEBP_INLINE void DoFilter6_SSE2(__m128i* const p2, __m128i* const p1,427__m128i* const p0, __m128i* const q0,428__m128i* const q1, __m128i* const q2,429const __m128i* const mask,430int hev_thresh) {431const __m128i zero = _mm_setzero_si128();432const __m128i sign_bit = _mm_set1_epi8((char)0x80);433__m128i a, not_hev;434435// compute hev mask436GetNotHEV_SSE2(p1, p0, q0, q1, hev_thresh, ¬_hev);437438FLIP_SIGN_BIT4(*p1, *p0, *q0, *q1);439FLIP_SIGN_BIT2(*p2, *q2);440GetBaseDelta_SSE2(p1, p0, q0, q1, &a);441442{ // do simple filter on pixels with hev443const __m128i m = _mm_andnot_si128(not_hev, *mask);444const __m128i f = _mm_and_si128(a, m);445DoSimpleFilter_SSE2(p0, q0, &f);446}447448{ // do strong filter on pixels with not hev449const __m128i k9 = _mm_set1_epi16(0x0900);450const __m128i k63 = _mm_set1_epi16(63);451452const __m128i m = _mm_and_si128(not_hev, *mask);453const __m128i f = _mm_and_si128(a, m);454455const __m128i f_lo = _mm_unpacklo_epi8(zero, f);456const __m128i f_hi = _mm_unpackhi_epi8(zero, f);457458const __m128i f9_lo = _mm_mulhi_epi16(f_lo, k9); // Filter (lo) * 9459const __m128i f9_hi = _mm_mulhi_epi16(f_hi, k9); // Filter (hi) * 9460461const __m128i a2_lo = _mm_add_epi16(f9_lo, k63); // Filter * 9 + 63462const __m128i a2_hi = _mm_add_epi16(f9_hi, k63); // Filter * 9 + 63463464const __m128i a1_lo = _mm_add_epi16(a2_lo, f9_lo); // Filter * 18 + 63465const __m128i a1_hi = _mm_add_epi16(a2_hi, f9_hi); // Filter * 18 + 63466467const __m128i a0_lo = _mm_add_epi16(a1_lo, f9_lo); // Filter * 27 + 63468const __m128i a0_hi = _mm_add_epi16(a1_hi, f9_hi); // Filter * 27 + 63469470Update2Pixels_SSE2(p2, q2, &a2_lo, &a2_hi);471Update2Pixels_SSE2(p1, q1, &a1_lo, &a1_hi);472Update2Pixels_SSE2(p0, q0, &a0_lo, &a0_hi);473}474}475476// reads 8 rows across a vertical edge.477static WEBP_INLINE void Load8x4_SSE2(const uint8_t* const b, int stride,478__m128i* const p, __m128i* const q) {479// A0 = 63 62 61 60 23 22 21 20 43 42 41 40 03 02 01 00480// A1 = 73 72 71 70 33 32 31 30 53 52 51 50 13 12 11 10481const __m128i A0 = _mm_set_epi32(482WebPMemToInt32(&b[6 * stride]), WebPMemToInt32(&b[2 * stride]),483WebPMemToInt32(&b[4 * stride]), WebPMemToInt32(&b[0 * stride]));484const __m128i A1 = _mm_set_epi32(485WebPMemToInt32(&b[7 * stride]), WebPMemToInt32(&b[3 * stride]),486WebPMemToInt32(&b[5 * stride]), WebPMemToInt32(&b[1 * stride]));487488// B0 = 53 43 52 42 51 41 50 40 13 03 12 02 11 01 10 00489// B1 = 73 63 72 62 71 61 70 60 33 23 32 22 31 21 30 20490const __m128i B0 = _mm_unpacklo_epi8(A0, A1);491const __m128i B1 = _mm_unpackhi_epi8(A0, A1);492493// C0 = 33 23 13 03 32 22 12 02 31 21 11 01 30 20 10 00494// C1 = 73 63 53 43 72 62 52 42 71 61 51 41 70 60 50 40495const __m128i C0 = _mm_unpacklo_epi16(B0, B1);496const __m128i C1 = _mm_unpackhi_epi16(B0, B1);497498// *p = 71 61 51 41 31 21 11 01 70 60 50 40 30 20 10 00499// *q = 73 63 53 43 33 23 13 03 72 62 52 42 32 22 12 02500*p = _mm_unpacklo_epi32(C0, C1);501*q = _mm_unpackhi_epi32(C0, C1);502}503504static WEBP_INLINE void Load16x4_SSE2(const uint8_t* const r0,505const uint8_t* const r8,506int stride,507__m128i* const p1, __m128i* const p0,508__m128i* const q0, __m128i* const q1) {509// Assume the pixels around the edge (|) are numbered as follows510// 00 01 | 02 03511// 10 11 | 12 13512// ... | ...513// e0 e1 | e2 e3514// f0 f1 | f2 f3515//516// r0 is pointing to the 0th row (00)517// r8 is pointing to the 8th row (80)518519// Load520// p1 = 71 61 51 41 31 21 11 01 70 60 50 40 30 20 10 00521// q0 = 73 63 53 43 33 23 13 03 72 62 52 42 32 22 12 02522// p0 = f1 e1 d1 c1 b1 a1 91 81 f0 e0 d0 c0 b0 a0 90 80523// q1 = f3 e3 d3 c3 b3 a3 93 83 f2 e2 d2 c2 b2 a2 92 82524Load8x4_SSE2(r0, stride, p1, q0);525Load8x4_SSE2(r8, stride, p0, q1);526527{528// p1 = f0 e0 d0 c0 b0 a0 90 80 70 60 50 40 30 20 10 00529// p0 = f1 e1 d1 c1 b1 a1 91 81 71 61 51 41 31 21 11 01530// q0 = f2 e2 d2 c2 b2 a2 92 82 72 62 52 42 32 22 12 02531// q1 = f3 e3 d3 c3 b3 a3 93 83 73 63 53 43 33 23 13 03532const __m128i t1 = *p1;533const __m128i t2 = *q0;534*p1 = _mm_unpacklo_epi64(t1, *p0);535*p0 = _mm_unpackhi_epi64(t1, *p0);536*q0 = _mm_unpacklo_epi64(t2, *q1);537*q1 = _mm_unpackhi_epi64(t2, *q1);538}539}540541static WEBP_INLINE void Store4x4_SSE2(__m128i* const x,542uint8_t* dst, int stride) {543int i;544for (i = 0; i < 4; ++i, dst += stride) {545WebPInt32ToMem(dst, _mm_cvtsi128_si32(*x));546*x = _mm_srli_si128(*x, 4);547}548}549550// Transpose back and store551static WEBP_INLINE void Store16x4_SSE2(const __m128i* const p1,552const __m128i* const p0,553const __m128i* const q0,554const __m128i* const q1,555uint8_t* r0, uint8_t* r8,556int stride) {557__m128i t1, p1_s, p0_s, q0_s, q1_s;558559// p0 = 71 70 61 60 51 50 41 40 31 30 21 20 11 10 01 00560// p1 = f1 f0 e1 e0 d1 d0 c1 c0 b1 b0 a1 a0 91 90 81 80561t1 = *p0;562p0_s = _mm_unpacklo_epi8(*p1, t1);563p1_s = _mm_unpackhi_epi8(*p1, t1);564565// q0 = 73 72 63 62 53 52 43 42 33 32 23 22 13 12 03 02566// q1 = f3 f2 e3 e2 d3 d2 c3 c2 b3 b2 a3 a2 93 92 83 82567t1 = *q0;568q0_s = _mm_unpacklo_epi8(t1, *q1);569q1_s = _mm_unpackhi_epi8(t1, *q1);570571// p0 = 33 32 31 30 23 22 21 20 13 12 11 10 03 02 01 00572// q0 = 73 72 71 70 63 62 61 60 53 52 51 50 43 42 41 40573t1 = p0_s;574p0_s = _mm_unpacklo_epi16(t1, q0_s);575q0_s = _mm_unpackhi_epi16(t1, q0_s);576577// p1 = b3 b2 b1 b0 a3 a2 a1 a0 93 92 91 90 83 82 81 80578// q1 = f3 f2 f1 f0 e3 e2 e1 e0 d3 d2 d1 d0 c3 c2 c1 c0579t1 = p1_s;580p1_s = _mm_unpacklo_epi16(t1, q1_s);581q1_s = _mm_unpackhi_epi16(t1, q1_s);582583Store4x4_SSE2(&p0_s, r0, stride);584r0 += 4 * stride;585Store4x4_SSE2(&q0_s, r0, stride);586587Store4x4_SSE2(&p1_s, r8, stride);588r8 += 4 * stride;589Store4x4_SSE2(&q1_s, r8, stride);590}591592//------------------------------------------------------------------------------593// Simple In-loop filtering (Paragraph 15.2)594595static void SimpleVFilter16_SSE2(uint8_t* p, int stride, int thresh) {596// Load597__m128i p1 = _mm_loadu_si128((__m128i*)&p[-2 * stride]);598__m128i p0 = _mm_loadu_si128((__m128i*)&p[-stride]);599__m128i q0 = _mm_loadu_si128((__m128i*)&p[0]);600__m128i q1 = _mm_loadu_si128((__m128i*)&p[stride]);601602DoFilter2_SSE2(&p1, &p0, &q0, &q1, thresh);603604// Store605_mm_storeu_si128((__m128i*)&p[-stride], p0);606_mm_storeu_si128((__m128i*)&p[0], q0);607}608609static void SimpleHFilter16_SSE2(uint8_t* p, int stride, int thresh) {610__m128i p1, p0, q0, q1;611612p -= 2; // beginning of p1613614Load16x4_SSE2(p, p + 8 * stride, stride, &p1, &p0, &q0, &q1);615DoFilter2_SSE2(&p1, &p0, &q0, &q1, thresh);616Store16x4_SSE2(&p1, &p0, &q0, &q1, p, p + 8 * stride, stride);617}618619static void SimpleVFilter16i_SSE2(uint8_t* p, int stride, int thresh) {620int k;621for (k = 3; k > 0; --k) {622p += 4 * stride;623SimpleVFilter16_SSE2(p, stride, thresh);624}625}626627static void SimpleHFilter16i_SSE2(uint8_t* p, int stride, int thresh) {628int k;629for (k = 3; k > 0; --k) {630p += 4;631SimpleHFilter16_SSE2(p, stride, thresh);632}633}634635//------------------------------------------------------------------------------636// Complex In-loop filtering (Paragraph 15.3)637638#define MAX_DIFF1(p3, p2, p1, p0, m) do { \639(m) = MM_ABS(p1, p0); \640(m) = _mm_max_epu8(m, MM_ABS(p3, p2)); \641(m) = _mm_max_epu8(m, MM_ABS(p2, p1)); \642} while (0)643644#define MAX_DIFF2(p3, p2, p1, p0, m) do { \645(m) = _mm_max_epu8(m, MM_ABS(p1, p0)); \646(m) = _mm_max_epu8(m, MM_ABS(p3, p2)); \647(m) = _mm_max_epu8(m, MM_ABS(p2, p1)); \648} while (0)649650#define LOAD_H_EDGES4(p, stride, e1, e2, e3, e4) do { \651(e1) = _mm_loadu_si128((__m128i*)&(p)[0 * (stride)]); \652(e2) = _mm_loadu_si128((__m128i*)&(p)[1 * (stride)]); \653(e3) = _mm_loadu_si128((__m128i*)&(p)[2 * (stride)]); \654(e4) = _mm_loadu_si128((__m128i*)&(p)[3 * (stride)]); \655} while (0)656657#define LOADUV_H_EDGE(p, u, v, stride) do { \658const __m128i U = _mm_loadl_epi64((__m128i*)&(u)[(stride)]); \659const __m128i V = _mm_loadl_epi64((__m128i*)&(v)[(stride)]); \660(p) = _mm_unpacklo_epi64(U, V); \661} while (0)662663#define LOADUV_H_EDGES4(u, v, stride, e1, e2, e3, e4) do { \664LOADUV_H_EDGE(e1, u, v, 0 * (stride)); \665LOADUV_H_EDGE(e2, u, v, 1 * (stride)); \666LOADUV_H_EDGE(e3, u, v, 2 * (stride)); \667LOADUV_H_EDGE(e4, u, v, 3 * (stride)); \668} while (0)669670#define STOREUV(p, u, v, stride) do { \671_mm_storel_epi64((__m128i*)&(u)[(stride)], p); \672(p) = _mm_srli_si128(p, 8); \673_mm_storel_epi64((__m128i*)&(v)[(stride)], p); \674} while (0)675676static WEBP_INLINE void ComplexMask_SSE2(const __m128i* const p1,677const __m128i* const p0,678const __m128i* const q0,679const __m128i* const q1,680int thresh, int ithresh,681__m128i* const mask) {682const __m128i it = _mm_set1_epi8(ithresh);683const __m128i diff = _mm_subs_epu8(*mask, it);684const __m128i thresh_mask = _mm_cmpeq_epi8(diff, _mm_setzero_si128());685__m128i filter_mask;686NeedsFilter_SSE2(p1, p0, q0, q1, thresh, &filter_mask);687*mask = _mm_and_si128(thresh_mask, filter_mask);688}689690// on macroblock edges691static void VFilter16_SSE2(uint8_t* p, int stride,692int thresh, int ithresh, int hev_thresh) {693__m128i t1;694__m128i mask;695__m128i p2, p1, p0, q0, q1, q2;696697// Load p3, p2, p1, p0698LOAD_H_EDGES4(p - 4 * stride, stride, t1, p2, p1, p0);699MAX_DIFF1(t1, p2, p1, p0, mask);700701// Load q0, q1, q2, q3702LOAD_H_EDGES4(p, stride, q0, q1, q2, t1);703MAX_DIFF2(t1, q2, q1, q0, mask);704705ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);706DoFilter6_SSE2(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);707708// Store709_mm_storeu_si128((__m128i*)&p[-3 * stride], p2);710_mm_storeu_si128((__m128i*)&p[-2 * stride], p1);711_mm_storeu_si128((__m128i*)&p[-1 * stride], p0);712_mm_storeu_si128((__m128i*)&p[+0 * stride], q0);713_mm_storeu_si128((__m128i*)&p[+1 * stride], q1);714_mm_storeu_si128((__m128i*)&p[+2 * stride], q2);715}716717static void HFilter16_SSE2(uint8_t* p, int stride,718int thresh, int ithresh, int hev_thresh) {719__m128i mask;720__m128i p3, p2, p1, p0, q0, q1, q2, q3;721722uint8_t* const b = p - 4;723Load16x4_SSE2(b, b + 8 * stride, stride, &p3, &p2, &p1, &p0);724MAX_DIFF1(p3, p2, p1, p0, mask);725726Load16x4_SSE2(p, p + 8 * stride, stride, &q0, &q1, &q2, &q3);727MAX_DIFF2(q3, q2, q1, q0, mask);728729ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);730DoFilter6_SSE2(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);731732Store16x4_SSE2(&p3, &p2, &p1, &p0, b, b + 8 * stride, stride);733Store16x4_SSE2(&q0, &q1, &q2, &q3, p, p + 8 * stride, stride);734}735736// on three inner edges737static void VFilter16i_SSE2(uint8_t* p, int stride,738int thresh, int ithresh, int hev_thresh) {739int k;740__m128i p3, p2, p1, p0; // loop invariants741742LOAD_H_EDGES4(p, stride, p3, p2, p1, p0); // prologue743744for (k = 3; k > 0; --k) {745__m128i mask, tmp1, tmp2;746uint8_t* const b = p + 2 * stride; // beginning of p1747p += 4 * stride;748749MAX_DIFF1(p3, p2, p1, p0, mask); // compute partial mask750LOAD_H_EDGES4(p, stride, p3, p2, tmp1, tmp2);751MAX_DIFF2(p3, p2, tmp1, tmp2, mask);752753// p3 and p2 are not just temporary variables here: they will be754// re-used for next span. And q2/q3 will become p1/p0 accordingly.755ComplexMask_SSE2(&p1, &p0, &p3, &p2, thresh, ithresh, &mask);756DoFilter4_SSE2(&p1, &p0, &p3, &p2, &mask, hev_thresh);757758// Store759_mm_storeu_si128((__m128i*)&b[0 * stride], p1);760_mm_storeu_si128((__m128i*)&b[1 * stride], p0);761_mm_storeu_si128((__m128i*)&b[2 * stride], p3);762_mm_storeu_si128((__m128i*)&b[3 * stride], p2);763764// rotate samples765p1 = tmp1;766p0 = tmp2;767}768}769770static void HFilter16i_SSE2(uint8_t* p, int stride,771int thresh, int ithresh, int hev_thresh) {772int k;773__m128i p3, p2, p1, p0; // loop invariants774775Load16x4_SSE2(p, p + 8 * stride, stride, &p3, &p2, &p1, &p0); // prologue776777for (k = 3; k > 0; --k) {778__m128i mask, tmp1, tmp2;779uint8_t* const b = p + 2; // beginning of p1780781p += 4; // beginning of q0 (and next span)782783MAX_DIFF1(p3, p2, p1, p0, mask); // compute partial mask784Load16x4_SSE2(p, p + 8 * stride, stride, &p3, &p2, &tmp1, &tmp2);785MAX_DIFF2(p3, p2, tmp1, tmp2, mask);786787ComplexMask_SSE2(&p1, &p0, &p3, &p2, thresh, ithresh, &mask);788DoFilter4_SSE2(&p1, &p0, &p3, &p2, &mask, hev_thresh);789790Store16x4_SSE2(&p1, &p0, &p3, &p2, b, b + 8 * stride, stride);791792// rotate samples793p1 = tmp1;794p0 = tmp2;795}796}797798// 8-pixels wide variant, for chroma filtering799static void VFilter8_SSE2(uint8_t* WEBP_RESTRICT u, uint8_t* WEBP_RESTRICT v,800int stride, int thresh, int ithresh, int hev_thresh) {801__m128i mask;802__m128i t1, p2, p1, p0, q0, q1, q2;803804// Load p3, p2, p1, p0805LOADUV_H_EDGES4(u - 4 * stride, v - 4 * stride, stride, t1, p2, p1, p0);806MAX_DIFF1(t1, p2, p1, p0, mask);807808// Load q0, q1, q2, q3809LOADUV_H_EDGES4(u, v, stride, q0, q1, q2, t1);810MAX_DIFF2(t1, q2, q1, q0, mask);811812ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);813DoFilter6_SSE2(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);814815// Store816STOREUV(p2, u, v, -3 * stride);817STOREUV(p1, u, v, -2 * stride);818STOREUV(p0, u, v, -1 * stride);819STOREUV(q0, u, v, 0 * stride);820STOREUV(q1, u, v, 1 * stride);821STOREUV(q2, u, v, 2 * stride);822}823824static void HFilter8_SSE2(uint8_t* WEBP_RESTRICT u, uint8_t* WEBP_RESTRICT v,825int stride, int thresh, int ithresh, int hev_thresh) {826__m128i mask;827__m128i p3, p2, p1, p0, q0, q1, q2, q3;828829uint8_t* const tu = u - 4;830uint8_t* const tv = v - 4;831Load16x4_SSE2(tu, tv, stride, &p3, &p2, &p1, &p0);832MAX_DIFF1(p3, p2, p1, p0, mask);833834Load16x4_SSE2(u, v, stride, &q0, &q1, &q2, &q3);835MAX_DIFF2(q3, q2, q1, q0, mask);836837ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);838DoFilter6_SSE2(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);839840Store16x4_SSE2(&p3, &p2, &p1, &p0, tu, tv, stride);841Store16x4_SSE2(&q0, &q1, &q2, &q3, u, v, stride);842}843844static void VFilter8i_SSE2(uint8_t* WEBP_RESTRICT u, uint8_t* WEBP_RESTRICT v,845int stride,846int thresh, int ithresh, int hev_thresh) {847__m128i mask;848__m128i t1, t2, p1, p0, q0, q1;849850// Load p3, p2, p1, p0851LOADUV_H_EDGES4(u, v, stride, t2, t1, p1, p0);852MAX_DIFF1(t2, t1, p1, p0, mask);853854u += 4 * stride;855v += 4 * stride;856857// Load q0, q1, q2, q3858LOADUV_H_EDGES4(u, v, stride, q0, q1, t1, t2);859MAX_DIFF2(t2, t1, q1, q0, mask);860861ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);862DoFilter4_SSE2(&p1, &p0, &q0, &q1, &mask, hev_thresh);863864// Store865STOREUV(p1, u, v, -2 * stride);866STOREUV(p0, u, v, -1 * stride);867STOREUV(q0, u, v, 0 * stride);868STOREUV(q1, u, v, 1 * stride);869}870871static void HFilter8i_SSE2(uint8_t* WEBP_RESTRICT u, uint8_t* WEBP_RESTRICT v,872int stride,873int thresh, int ithresh, int hev_thresh) {874__m128i mask;875__m128i t1, t2, p1, p0, q0, q1;876Load16x4_SSE2(u, v, stride, &t2, &t1, &p1, &p0); // p3, p2, p1, p0877MAX_DIFF1(t2, t1, p1, p0, mask);878879u += 4; // beginning of q0880v += 4;881Load16x4_SSE2(u, v, stride, &q0, &q1, &t1, &t2); // q0, q1, q2, q3882MAX_DIFF2(t2, t1, q1, q0, mask);883884ComplexMask_SSE2(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);885DoFilter4_SSE2(&p1, &p0, &q0, &q1, &mask, hev_thresh);886887u -= 2; // beginning of p1888v -= 2;889Store16x4_SSE2(&p1, &p0, &q0, &q1, u, v, stride);890}891892//------------------------------------------------------------------------------893// 4x4 predictions894895#define DST(x, y) dst[(x) + (y) * BPS]896#define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)897898// We use the following 8b-arithmetic tricks:899// (a + 2 * b + c + 2) >> 2 = (AC + b + 1) >> 1900// where: AC = (a + c) >> 1 = [(a + c + 1) >> 1] - [(a^c) & 1]901// and:902// (a + 2 * b + c + 2) >> 2 = (AB + BC + 1) >> 1 - (ab|bc)&lsb903// where: AC = (a + b + 1) >> 1, BC = (b + c + 1) >> 1904// and ab = a ^ b, bc = b ^ c, lsb = (AC^BC)&1905906static void VE4_SSE2(uint8_t* dst) { // vertical907const __m128i one = _mm_set1_epi8(1);908const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(dst - BPS - 1));909const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1);910const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2);911const __m128i a = _mm_avg_epu8(ABCDEFGH, CDEFGH00);912const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGH00), one);913const __m128i b = _mm_subs_epu8(a, lsb);914const __m128i avg = _mm_avg_epu8(b, BCDEFGH0);915const int vals = _mm_cvtsi128_si32(avg);916int i;917for (i = 0; i < 4; ++i) {918WebPInt32ToMem(dst + i * BPS, vals);919}920}921922static void LD4_SSE2(uint8_t* dst) { // Down-Left923const __m128i one = _mm_set1_epi8(1);924const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(dst - BPS));925const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1);926const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2);927const __m128i CDEFGHH0 = _mm_insert_epi16(CDEFGH00, dst[-BPS + 7], 3);928const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, CDEFGHH0);929const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGHH0), one);930const __m128i avg2 = _mm_subs_epu8(avg1, lsb);931const __m128i abcdefg = _mm_avg_epu8(avg2, BCDEFGH0);932WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( abcdefg ));933WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1)));934WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2)));935WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3)));936}937938static void VR4_SSE2(uint8_t* dst) { // Vertical-Right939const __m128i one = _mm_set1_epi8(1);940const int I = dst[-1 + 0 * BPS];941const int J = dst[-1 + 1 * BPS];942const int K = dst[-1 + 2 * BPS];943const int X = dst[-1 - BPS];944const __m128i XABCD = _mm_loadl_epi64((__m128i*)(dst - BPS - 1));945const __m128i ABCD0 = _mm_srli_si128(XABCD, 1);946const __m128i abcd = _mm_avg_epu8(XABCD, ABCD0);947const __m128i _XABCD = _mm_slli_si128(XABCD, 1);948const __m128i IXABCD = _mm_insert_epi16(_XABCD, (short)(I | (X << 8)), 0);949const __m128i avg1 = _mm_avg_epu8(IXABCD, ABCD0);950const __m128i lsb = _mm_and_si128(_mm_xor_si128(IXABCD, ABCD0), one);951const __m128i avg2 = _mm_subs_epu8(avg1, lsb);952const __m128i efgh = _mm_avg_epu8(avg2, XABCD);953WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( abcd ));954WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32( efgh ));955WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(abcd, 1)));956WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(efgh, 1)));957958// these two are hard to implement in SSE2, so we keep the C-version:959DST(0, 2) = AVG3(J, I, X);960DST(0, 3) = AVG3(K, J, I);961}962963static void VL4_SSE2(uint8_t* dst) { // Vertical-Left964const __m128i one = _mm_set1_epi8(1);965const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(dst - BPS));966const __m128i BCDEFGH_ = _mm_srli_si128(ABCDEFGH, 1);967const __m128i CDEFGH__ = _mm_srli_si128(ABCDEFGH, 2);968const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, BCDEFGH_);969const __m128i avg2 = _mm_avg_epu8(CDEFGH__, BCDEFGH_);970const __m128i avg3 = _mm_avg_epu8(avg1, avg2);971const __m128i lsb1 = _mm_and_si128(_mm_xor_si128(avg1, avg2), one);972const __m128i ab = _mm_xor_si128(ABCDEFGH, BCDEFGH_);973const __m128i bc = _mm_xor_si128(CDEFGH__, BCDEFGH_);974const __m128i abbc = _mm_or_si128(ab, bc);975const __m128i lsb2 = _mm_and_si128(abbc, lsb1);976const __m128i avg4 = _mm_subs_epu8(avg3, lsb2);977const uint32_t extra_out =978(uint32_t)_mm_cvtsi128_si32(_mm_srli_si128(avg4, 4));979WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( avg1 ));980WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32( avg4 ));981WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg1, 1)));982WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg4, 1)));983984// these two are hard to get and irregular985DST(3, 2) = (extra_out >> 0) & 0xff;986DST(3, 3) = (extra_out >> 8) & 0xff;987}988989static void RD4_SSE2(uint8_t* dst) { // Down-right990const __m128i one = _mm_set1_epi8(1);991const __m128i XABCD = _mm_loadl_epi64((__m128i*)(dst - BPS - 1));992const __m128i ____XABCD = _mm_slli_si128(XABCD, 4);993const uint32_t I = dst[-1 + 0 * BPS];994const uint32_t J = dst[-1 + 1 * BPS];995const uint32_t K = dst[-1 + 2 * BPS];996const uint32_t L = dst[-1 + 3 * BPS];997const __m128i LKJI_____ =998_mm_cvtsi32_si128((int)(L | (K << 8) | (J << 16) | (I << 24)));999const __m128i LKJIXABCD = _mm_or_si128(LKJI_____, ____XABCD);1000const __m128i KJIXABCD_ = _mm_srli_si128(LKJIXABCD, 1);1001const __m128i JIXABCD__ = _mm_srli_si128(LKJIXABCD, 2);1002const __m128i avg1 = _mm_avg_epu8(JIXABCD__, LKJIXABCD);1003const __m128i lsb = _mm_and_si128(_mm_xor_si128(JIXABCD__, LKJIXABCD), one);1004const __m128i avg2 = _mm_subs_epu8(avg1, lsb);1005const __m128i abcdefg = _mm_avg_epu8(avg2, KJIXABCD_);1006WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32( abcdefg ));1007WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1)));1008WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2)));1009WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3)));1010}10111012#undef DST1013#undef AVG310141015//------------------------------------------------------------------------------1016// Luma 16x1610171018static WEBP_INLINE void TrueMotion_SSE2(uint8_t* dst, int size) {1019const uint8_t* top = dst - BPS;1020const __m128i zero = _mm_setzero_si128();1021int y;1022if (size == 4) {1023const __m128i top_values = _mm_cvtsi32_si128(WebPMemToInt32(top));1024const __m128i top_base = _mm_unpacklo_epi8(top_values, zero);1025for (y = 0; y < 4; ++y, dst += BPS) {1026const int val = dst[-1] - top[-1];1027const __m128i base = _mm_set1_epi16(val);1028const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero);1029WebPInt32ToMem(dst, _mm_cvtsi128_si32(out));1030}1031} else if (size == 8) {1032const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);1033const __m128i top_base = _mm_unpacklo_epi8(top_values, zero);1034for (y = 0; y < 8; ++y, dst += BPS) {1035const int val = dst[-1] - top[-1];1036const __m128i base = _mm_set1_epi16(val);1037const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero);1038_mm_storel_epi64((__m128i*)dst, out);1039}1040} else {1041const __m128i top_values = _mm_loadu_si128((const __m128i*)top);1042const __m128i top_base_0 = _mm_unpacklo_epi8(top_values, zero);1043const __m128i top_base_1 = _mm_unpackhi_epi8(top_values, zero);1044for (y = 0; y < 16; ++y, dst += BPS) {1045const int val = dst[-1] - top[-1];1046const __m128i base = _mm_set1_epi16(val);1047const __m128i out_0 = _mm_add_epi16(base, top_base_0);1048const __m128i out_1 = _mm_add_epi16(base, top_base_1);1049const __m128i out = _mm_packus_epi16(out_0, out_1);1050_mm_storeu_si128((__m128i*)dst, out);1051}1052}1053}10541055static void TM4_SSE2(uint8_t* dst) { TrueMotion_SSE2(dst, 4); }1056static void TM8uv_SSE2(uint8_t* dst) { TrueMotion_SSE2(dst, 8); }1057static void TM16_SSE2(uint8_t* dst) { TrueMotion_SSE2(dst, 16); }10581059static void VE16_SSE2(uint8_t* dst) {1060const __m128i top = _mm_loadu_si128((const __m128i*)(dst - BPS));1061int j;1062for (j = 0; j < 16; ++j) {1063_mm_storeu_si128((__m128i*)(dst + j * BPS), top);1064}1065}10661067static void HE16_SSE2(uint8_t* dst) { // horizontal1068int j;1069for (j = 16; j > 0; --j) {1070const __m128i values = _mm_set1_epi8((char)dst[-1]);1071_mm_storeu_si128((__m128i*)dst, values);1072dst += BPS;1073}1074}10751076static WEBP_INLINE void Put16_SSE2(uint8_t v, uint8_t* dst) {1077int j;1078const __m128i values = _mm_set1_epi8((char)v);1079for (j = 0; j < 16; ++j) {1080_mm_storeu_si128((__m128i*)(dst + j * BPS), values);1081}1082}10831084static void DC16_SSE2(uint8_t* dst) { // DC1085const __m128i zero = _mm_setzero_si128();1086const __m128i top = _mm_loadu_si128((const __m128i*)(dst - BPS));1087const __m128i sad8x2 = _mm_sad_epu8(top, zero);1088// sum the two sads: sad8x2[0:1] + sad8x2[8:9]1089const __m128i sum = _mm_add_epi16(sad8x2, _mm_shuffle_epi32(sad8x2, 2));1090int left = 0;1091int j;1092for (j = 0; j < 16; ++j) {1093left += dst[-1 + j * BPS];1094}1095{1096const int DC = _mm_cvtsi128_si32(sum) + left + 16;1097Put16_SSE2(DC >> 5, dst);1098}1099}11001101static void DC16NoTop_SSE2(uint8_t* dst) { // DC with top samples unavailable1102int DC = 8;1103int j;1104for (j = 0; j < 16; ++j) {1105DC += dst[-1 + j * BPS];1106}1107Put16_SSE2(DC >> 4, dst);1108}11091110static void DC16NoLeft_SSE2(uint8_t* dst) { // DC with left samples unavailable1111const __m128i zero = _mm_setzero_si128();1112const __m128i top = _mm_loadu_si128((const __m128i*)(dst - BPS));1113const __m128i sad8x2 = _mm_sad_epu8(top, zero);1114// sum the two sads: sad8x2[0:1] + sad8x2[8:9]1115const __m128i sum = _mm_add_epi16(sad8x2, _mm_shuffle_epi32(sad8x2, 2));1116const int DC = _mm_cvtsi128_si32(sum) + 8;1117Put16_SSE2(DC >> 4, dst);1118}11191120static void DC16NoTopLeft_SSE2(uint8_t* dst) { // DC with no top & left samples1121Put16_SSE2(0x80, dst);1122}11231124//------------------------------------------------------------------------------1125// Chroma11261127static void VE8uv_SSE2(uint8_t* dst) { // vertical1128int j;1129const __m128i top = _mm_loadl_epi64((const __m128i*)(dst - BPS));1130for (j = 0; j < 8; ++j) {1131_mm_storel_epi64((__m128i*)(dst + j * BPS), top);1132}1133}11341135// helper for chroma-DC predictions1136static WEBP_INLINE void Put8x8uv_SSE2(uint8_t v, uint8_t* dst) {1137int j;1138const __m128i values = _mm_set1_epi8((char)v);1139for (j = 0; j < 8; ++j) {1140_mm_storel_epi64((__m128i*)(dst + j * BPS), values);1141}1142}11431144static void DC8uv_SSE2(uint8_t* dst) { // DC1145const __m128i zero = _mm_setzero_si128();1146const __m128i top = _mm_loadl_epi64((const __m128i*)(dst - BPS));1147const __m128i sum = _mm_sad_epu8(top, zero);1148int left = 0;1149int j;1150for (j = 0; j < 8; ++j) {1151left += dst[-1 + j * BPS];1152}1153{1154const int DC = _mm_cvtsi128_si32(sum) + left + 8;1155Put8x8uv_SSE2(DC >> 4, dst);1156}1157}11581159static void DC8uvNoLeft_SSE2(uint8_t* dst) { // DC with no left samples1160const __m128i zero = _mm_setzero_si128();1161const __m128i top = _mm_loadl_epi64((const __m128i*)(dst - BPS));1162const __m128i sum = _mm_sad_epu8(top, zero);1163const int DC = _mm_cvtsi128_si32(sum) + 4;1164Put8x8uv_SSE2(DC >> 3, dst);1165}11661167static void DC8uvNoTop_SSE2(uint8_t* dst) { // DC with no top samples1168int dc0 = 4;1169int i;1170for (i = 0; i < 8; ++i) {1171dc0 += dst[-1 + i * BPS];1172}1173Put8x8uv_SSE2(dc0 >> 3, dst);1174}11751176static void DC8uvNoTopLeft_SSE2(uint8_t* dst) { // DC with nothing1177Put8x8uv_SSE2(0x80, dst);1178}11791180//------------------------------------------------------------------------------1181// Entry point11821183extern void VP8DspInitSSE2(void);11841185WEBP_TSAN_IGNORE_FUNCTION void VP8DspInitSSE2(void) {1186VP8Transform = Transform_SSE2;1187#if (USE_TRANSFORM_AC3 == 1)1188VP8TransformAC3 = TransformAC3_SSE2;1189#endif11901191VP8VFilter16 = VFilter16_SSE2;1192VP8HFilter16 = HFilter16_SSE2;1193VP8VFilter8 = VFilter8_SSE2;1194VP8HFilter8 = HFilter8_SSE2;1195VP8VFilter16i = VFilter16i_SSE2;1196VP8HFilter16i = HFilter16i_SSE2;1197VP8VFilter8i = VFilter8i_SSE2;1198VP8HFilter8i = HFilter8i_SSE2;11991200VP8SimpleVFilter16 = SimpleVFilter16_SSE2;1201VP8SimpleHFilter16 = SimpleHFilter16_SSE2;1202VP8SimpleVFilter16i = SimpleVFilter16i_SSE2;1203VP8SimpleHFilter16i = SimpleHFilter16i_SSE2;12041205VP8PredLuma4[1] = TM4_SSE2;1206VP8PredLuma4[2] = VE4_SSE2;1207VP8PredLuma4[4] = RD4_SSE2;1208VP8PredLuma4[5] = VR4_SSE2;1209VP8PredLuma4[6] = LD4_SSE2;1210VP8PredLuma4[7] = VL4_SSE2;12111212VP8PredLuma16[0] = DC16_SSE2;1213VP8PredLuma16[1] = TM16_SSE2;1214VP8PredLuma16[2] = VE16_SSE2;1215VP8PredLuma16[3] = HE16_SSE2;1216VP8PredLuma16[4] = DC16NoTop_SSE2;1217VP8PredLuma16[5] = DC16NoLeft_SSE2;1218VP8PredLuma16[6] = DC16NoTopLeft_SSE2;12191220VP8PredChroma8[0] = DC8uv_SSE2;1221VP8PredChroma8[1] = TM8uv_SSE2;1222VP8PredChroma8[2] = VE8uv_SSE2;1223VP8PredChroma8[4] = DC8uvNoTop_SSE2;1224VP8PredChroma8[5] = DC8uvNoLeft_SSE2;1225VP8PredChroma8[6] = DC8uvNoTopLeft_SSE2;1226}12271228#else // !WEBP_USE_SSE212291230WEBP_DSP_INIT_STUB(VP8DspInitSSE2)12311232#endif // WEBP_USE_SSE2123312341235