Path: blob/master/3rdparty/libwebp/src/dsp/enc_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 speed-critical encoding functions.10//11// Author: Christian Duvivier ([email protected])1213#include "src/dsp/dsp.h"1415#if defined(WEBP_USE_SSE2)16#include <assert.h>17#include <stdlib.h> // for abs()18#include <emmintrin.h>1920#include "src/dsp/common_sse2.h"21#include "src/enc/cost_enc.h"22#include "src/enc/vp8i_enc.h"2324//------------------------------------------------------------------------------25// Transforms (Paragraph 14.4)2627// Does one or two inverse transforms.28static void ITransform_SSE2(const uint8_t* ref, const int16_t* in, uint8_t* dst,29int do_two) {30// This implementation makes use of 16-bit fixed point versions of two31// multiply constants:32// K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^1633// K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^1634//35// To be able to use signed 16-bit integers, we use the following trick to36// have constants within range:37// - Associated constants are obtained by subtracting the 16-bit fixed point38// version of one:39// k = K - (1 << 16) => K = k + (1 << 16)40// K1 = 85267 => k1 = 2009141// K2 = 35468 => k2 = -3006842// - The multiplication of a variable by a constant become the sum of the43// variable and the multiplication of that variable by the associated44// constant:45// (x * K) >> 16 = (x * (k + (1 << 16))) >> 16 = ((x * k ) >> 16) + x46const __m128i k1 = _mm_set1_epi16(20091);47const __m128i k2 = _mm_set1_epi16(-30068);48__m128i T0, T1, T2, T3;4950// Load and concatenate the transform coefficients (we'll do two inverse51// transforms in parallel). In the case of only one inverse transform, the52// second half of the vectors will just contain random value we'll never53// use nor store.54__m128i in0, in1, in2, in3;55{56in0 = _mm_loadl_epi64((const __m128i*)&in[0]);57in1 = _mm_loadl_epi64((const __m128i*)&in[4]);58in2 = _mm_loadl_epi64((const __m128i*)&in[8]);59in3 = _mm_loadl_epi64((const __m128i*)&in[12]);60// a00 a10 a20 a30 x x x x61// a01 a11 a21 a31 x x x x62// a02 a12 a22 a32 x x x x63// a03 a13 a23 a33 x x x x64if (do_two) {65const __m128i inB0 = _mm_loadl_epi64((const __m128i*)&in[16]);66const __m128i inB1 = _mm_loadl_epi64((const __m128i*)&in[20]);67const __m128i inB2 = _mm_loadl_epi64((const __m128i*)&in[24]);68const __m128i inB3 = _mm_loadl_epi64((const __m128i*)&in[28]);69in0 = _mm_unpacklo_epi64(in0, inB0);70in1 = _mm_unpacklo_epi64(in1, inB1);71in2 = _mm_unpacklo_epi64(in2, inB2);72in3 = _mm_unpacklo_epi64(in3, inB3);73// a00 a10 a20 a30 b00 b10 b20 b3074// a01 a11 a21 a31 b01 b11 b21 b3175// a02 a12 a22 a32 b02 b12 b22 b3276// a03 a13 a23 a33 b03 b13 b23 b3377}78}7980// Vertical pass and subsequent transpose.81{82// First pass, c and d calculations are longer because of the "trick"83// multiplications.84const __m128i a = _mm_add_epi16(in0, in2);85const __m128i b = _mm_sub_epi16(in0, in2);86// c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in387const __m128i c1 = _mm_mulhi_epi16(in1, k2);88const __m128i c2 = _mm_mulhi_epi16(in3, k1);89const __m128i c3 = _mm_sub_epi16(in1, in3);90const __m128i c4 = _mm_sub_epi16(c1, c2);91const __m128i c = _mm_add_epi16(c3, c4);92// d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in393const __m128i d1 = _mm_mulhi_epi16(in1, k1);94const __m128i d2 = _mm_mulhi_epi16(in3, k2);95const __m128i d3 = _mm_add_epi16(in1, in3);96const __m128i d4 = _mm_add_epi16(d1, d2);97const __m128i d = _mm_add_epi16(d3, d4);9899// Second pass.100const __m128i tmp0 = _mm_add_epi16(a, d);101const __m128i tmp1 = _mm_add_epi16(b, c);102const __m128i tmp2 = _mm_sub_epi16(b, c);103const __m128i tmp3 = _mm_sub_epi16(a, d);104105// Transpose the two 4x4.106VP8Transpose_2_4x4_16b(&tmp0, &tmp1, &tmp2, &tmp3, &T0, &T1, &T2, &T3);107}108109// Horizontal pass and subsequent transpose.110{111// First pass, c and d calculations are longer because of the "trick"112// multiplications.113const __m128i four = _mm_set1_epi16(4);114const __m128i dc = _mm_add_epi16(T0, four);115const __m128i a = _mm_add_epi16(dc, T2);116const __m128i b = _mm_sub_epi16(dc, T2);117// c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3118const __m128i c1 = _mm_mulhi_epi16(T1, k2);119const __m128i c2 = _mm_mulhi_epi16(T3, k1);120const __m128i c3 = _mm_sub_epi16(T1, T3);121const __m128i c4 = _mm_sub_epi16(c1, c2);122const __m128i c = _mm_add_epi16(c3, c4);123// d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3124const __m128i d1 = _mm_mulhi_epi16(T1, k1);125const __m128i d2 = _mm_mulhi_epi16(T3, k2);126const __m128i d3 = _mm_add_epi16(T1, T3);127const __m128i d4 = _mm_add_epi16(d1, d2);128const __m128i d = _mm_add_epi16(d3, d4);129130// Second pass.131const __m128i tmp0 = _mm_add_epi16(a, d);132const __m128i tmp1 = _mm_add_epi16(b, c);133const __m128i tmp2 = _mm_sub_epi16(b, c);134const __m128i tmp3 = _mm_sub_epi16(a, d);135const __m128i shifted0 = _mm_srai_epi16(tmp0, 3);136const __m128i shifted1 = _mm_srai_epi16(tmp1, 3);137const __m128i shifted2 = _mm_srai_epi16(tmp2, 3);138const __m128i shifted3 = _mm_srai_epi16(tmp3, 3);139140// Transpose the two 4x4.141VP8Transpose_2_4x4_16b(&shifted0, &shifted1, &shifted2, &shifted3, &T0, &T1,142&T2, &T3);143}144145// Add inverse transform to 'ref' and store.146{147const __m128i zero = _mm_setzero_si128();148// Load the reference(s).149__m128i ref0, ref1, ref2, ref3;150if (do_two) {151// Load eight bytes/pixels per line.152ref0 = _mm_loadl_epi64((const __m128i*)&ref[0 * BPS]);153ref1 = _mm_loadl_epi64((const __m128i*)&ref[1 * BPS]);154ref2 = _mm_loadl_epi64((const __m128i*)&ref[2 * BPS]);155ref3 = _mm_loadl_epi64((const __m128i*)&ref[3 * BPS]);156} else {157// Load four bytes/pixels per line.158ref0 = _mm_cvtsi32_si128(WebPMemToUint32(&ref[0 * BPS]));159ref1 = _mm_cvtsi32_si128(WebPMemToUint32(&ref[1 * BPS]));160ref2 = _mm_cvtsi32_si128(WebPMemToUint32(&ref[2 * BPS]));161ref3 = _mm_cvtsi32_si128(WebPMemToUint32(&ref[3 * BPS]));162}163// Convert to 16b.164ref0 = _mm_unpacklo_epi8(ref0, zero);165ref1 = _mm_unpacklo_epi8(ref1, zero);166ref2 = _mm_unpacklo_epi8(ref2, zero);167ref3 = _mm_unpacklo_epi8(ref3, zero);168// Add the inverse transform(s).169ref0 = _mm_add_epi16(ref0, T0);170ref1 = _mm_add_epi16(ref1, T1);171ref2 = _mm_add_epi16(ref2, T2);172ref3 = _mm_add_epi16(ref3, T3);173// Unsigned saturate to 8b.174ref0 = _mm_packus_epi16(ref0, ref0);175ref1 = _mm_packus_epi16(ref1, ref1);176ref2 = _mm_packus_epi16(ref2, ref2);177ref3 = _mm_packus_epi16(ref3, ref3);178// Store the results.179if (do_two) {180// Store eight bytes/pixels per line.181_mm_storel_epi64((__m128i*)&dst[0 * BPS], ref0);182_mm_storel_epi64((__m128i*)&dst[1 * BPS], ref1);183_mm_storel_epi64((__m128i*)&dst[2 * BPS], ref2);184_mm_storel_epi64((__m128i*)&dst[3 * BPS], ref3);185} else {186// Store four bytes/pixels per line.187WebPUint32ToMem(&dst[0 * BPS], _mm_cvtsi128_si32(ref0));188WebPUint32ToMem(&dst[1 * BPS], _mm_cvtsi128_si32(ref1));189WebPUint32ToMem(&dst[2 * BPS], _mm_cvtsi128_si32(ref2));190WebPUint32ToMem(&dst[3 * BPS], _mm_cvtsi128_si32(ref3));191}192}193}194195static void FTransformPass1_SSE2(const __m128i* const in01,196const __m128i* const in23,197__m128i* const out01,198__m128i* const out32) {199const __m128i k937 = _mm_set1_epi32(937);200const __m128i k1812 = _mm_set1_epi32(1812);201202const __m128i k88p = _mm_set_epi16(8, 8, 8, 8, 8, 8, 8, 8);203const __m128i k88m = _mm_set_epi16(-8, 8, -8, 8, -8, 8, -8, 8);204const __m128i k5352_2217p = _mm_set_epi16(2217, 5352, 2217, 5352,2052217, 5352, 2217, 5352);206const __m128i k5352_2217m = _mm_set_epi16(-5352, 2217, -5352, 2217,207-5352, 2217, -5352, 2217);208209// *in01 = 00 01 10 11 02 03 12 13210// *in23 = 20 21 30 31 22 23 32 33211const __m128i shuf01_p = _mm_shufflehi_epi16(*in01, _MM_SHUFFLE(2, 3, 0, 1));212const __m128i shuf23_p = _mm_shufflehi_epi16(*in23, _MM_SHUFFLE(2, 3, 0, 1));213// 00 01 10 11 03 02 13 12214// 20 21 30 31 23 22 33 32215const __m128i s01 = _mm_unpacklo_epi64(shuf01_p, shuf23_p);216const __m128i s32 = _mm_unpackhi_epi64(shuf01_p, shuf23_p);217// 00 01 10 11 20 21 30 31218// 03 02 13 12 23 22 33 32219const __m128i a01 = _mm_add_epi16(s01, s32);220const __m128i a32 = _mm_sub_epi16(s01, s32);221// [d0 + d3 | d1 + d2 | ...] = [a0 a1 | a0' a1' | ... ]222// [d0 - d3 | d1 - d2 | ...] = [a3 a2 | a3' a2' | ... ]223224const __m128i tmp0 = _mm_madd_epi16(a01, k88p); // [ (a0 + a1) << 3, ... ]225const __m128i tmp2 = _mm_madd_epi16(a01, k88m); // [ (a0 - a1) << 3, ... ]226const __m128i tmp1_1 = _mm_madd_epi16(a32, k5352_2217p);227const __m128i tmp3_1 = _mm_madd_epi16(a32, k5352_2217m);228const __m128i tmp1_2 = _mm_add_epi32(tmp1_1, k1812);229const __m128i tmp3_2 = _mm_add_epi32(tmp3_1, k937);230const __m128i tmp1 = _mm_srai_epi32(tmp1_2, 9);231const __m128i tmp3 = _mm_srai_epi32(tmp3_2, 9);232const __m128i s03 = _mm_packs_epi32(tmp0, tmp2);233const __m128i s12 = _mm_packs_epi32(tmp1, tmp3);234const __m128i s_lo = _mm_unpacklo_epi16(s03, s12); // 0 1 0 1 0 1...235const __m128i s_hi = _mm_unpackhi_epi16(s03, s12); // 2 3 2 3 2 3236const __m128i v23 = _mm_unpackhi_epi32(s_lo, s_hi);237*out01 = _mm_unpacklo_epi32(s_lo, s_hi);238*out32 = _mm_shuffle_epi32(v23, _MM_SHUFFLE(1, 0, 3, 2)); // 3 2 3 2 3 2..239}240241static void FTransformPass2_SSE2(const __m128i* const v01,242const __m128i* const v32,243int16_t* out) {244const __m128i zero = _mm_setzero_si128();245const __m128i seven = _mm_set1_epi16(7);246const __m128i k5352_2217 = _mm_set_epi16(5352, 2217, 5352, 2217,2475352, 2217, 5352, 2217);248const __m128i k2217_5352 = _mm_set_epi16(2217, -5352, 2217, -5352,2492217, -5352, 2217, -5352);250const __m128i k12000_plus_one = _mm_set1_epi32(12000 + (1 << 16));251const __m128i k51000 = _mm_set1_epi32(51000);252253// Same operations are done on the (0,3) and (1,2) pairs.254// a3 = v0 - v3255// a2 = v1 - v2256const __m128i a32 = _mm_sub_epi16(*v01, *v32);257const __m128i a22 = _mm_unpackhi_epi64(a32, a32);258259const __m128i b23 = _mm_unpacklo_epi16(a22, a32);260const __m128i c1 = _mm_madd_epi16(b23, k5352_2217);261const __m128i c3 = _mm_madd_epi16(b23, k2217_5352);262const __m128i d1 = _mm_add_epi32(c1, k12000_plus_one);263const __m128i d3 = _mm_add_epi32(c3, k51000);264const __m128i e1 = _mm_srai_epi32(d1, 16);265const __m128i e3 = _mm_srai_epi32(d3, 16);266// f1 = ((b3 * 5352 + b2 * 2217 + 12000) >> 16)267// f3 = ((b3 * 2217 - b2 * 5352 + 51000) >> 16)268const __m128i f1 = _mm_packs_epi32(e1, e1);269const __m128i f3 = _mm_packs_epi32(e3, e3);270// g1 = f1 + (a3 != 0);271// The compare will return (0xffff, 0) for (==0, !=0). To turn that into the272// desired (0, 1), we add one earlier through k12000_plus_one.273// -> g1 = f1 + 1 - (a3 == 0)274const __m128i g1 = _mm_add_epi16(f1, _mm_cmpeq_epi16(a32, zero));275276// a0 = v0 + v3277// a1 = v1 + v2278const __m128i a01 = _mm_add_epi16(*v01, *v32);279const __m128i a01_plus_7 = _mm_add_epi16(a01, seven);280const __m128i a11 = _mm_unpackhi_epi64(a01, a01);281const __m128i c0 = _mm_add_epi16(a01_plus_7, a11);282const __m128i c2 = _mm_sub_epi16(a01_plus_7, a11);283// d0 = (a0 + a1 + 7) >> 4;284// d2 = (a0 - a1 + 7) >> 4;285const __m128i d0 = _mm_srai_epi16(c0, 4);286const __m128i d2 = _mm_srai_epi16(c2, 4);287288const __m128i d0_g1 = _mm_unpacklo_epi64(d0, g1);289const __m128i d2_f3 = _mm_unpacklo_epi64(d2, f3);290_mm_storeu_si128((__m128i*)&out[0], d0_g1);291_mm_storeu_si128((__m128i*)&out[8], d2_f3);292}293294static void FTransform_SSE2(const uint8_t* src, const uint8_t* ref,295int16_t* out) {296const __m128i zero = _mm_setzero_si128();297// Load src.298const __m128i src0 = _mm_loadl_epi64((const __m128i*)&src[0 * BPS]);299const __m128i src1 = _mm_loadl_epi64((const __m128i*)&src[1 * BPS]);300const __m128i src2 = _mm_loadl_epi64((const __m128i*)&src[2 * BPS]);301const __m128i src3 = _mm_loadl_epi64((const __m128i*)&src[3 * BPS]);302// 00 01 02 03 *303// 10 11 12 13 *304// 20 21 22 23 *305// 30 31 32 33 *306// Shuffle.307const __m128i src_0 = _mm_unpacklo_epi16(src0, src1);308const __m128i src_1 = _mm_unpacklo_epi16(src2, src3);309// 00 01 10 11 02 03 12 13 * * ...310// 20 21 30 31 22 22 32 33 * * ...311312// Load ref.313const __m128i ref0 = _mm_loadl_epi64((const __m128i*)&ref[0 * BPS]);314const __m128i ref1 = _mm_loadl_epi64((const __m128i*)&ref[1 * BPS]);315const __m128i ref2 = _mm_loadl_epi64((const __m128i*)&ref[2 * BPS]);316const __m128i ref3 = _mm_loadl_epi64((const __m128i*)&ref[3 * BPS]);317const __m128i ref_0 = _mm_unpacklo_epi16(ref0, ref1);318const __m128i ref_1 = _mm_unpacklo_epi16(ref2, ref3);319320// Convert both to 16 bit.321const __m128i src_0_16b = _mm_unpacklo_epi8(src_0, zero);322const __m128i src_1_16b = _mm_unpacklo_epi8(src_1, zero);323const __m128i ref_0_16b = _mm_unpacklo_epi8(ref_0, zero);324const __m128i ref_1_16b = _mm_unpacklo_epi8(ref_1, zero);325326// Compute the difference.327const __m128i row01 = _mm_sub_epi16(src_0_16b, ref_0_16b);328const __m128i row23 = _mm_sub_epi16(src_1_16b, ref_1_16b);329__m128i v01, v32;330331// First pass332FTransformPass1_SSE2(&row01, &row23, &v01, &v32);333334// Second pass335FTransformPass2_SSE2(&v01, &v32, out);336}337338static void FTransform2_SSE2(const uint8_t* src, const uint8_t* ref,339int16_t* out) {340const __m128i zero = _mm_setzero_si128();341342// Load src and convert to 16b.343const __m128i src0 = _mm_loadl_epi64((const __m128i*)&src[0 * BPS]);344const __m128i src1 = _mm_loadl_epi64((const __m128i*)&src[1 * BPS]);345const __m128i src2 = _mm_loadl_epi64((const __m128i*)&src[2 * BPS]);346const __m128i src3 = _mm_loadl_epi64((const __m128i*)&src[3 * BPS]);347const __m128i src_0 = _mm_unpacklo_epi8(src0, zero);348const __m128i src_1 = _mm_unpacklo_epi8(src1, zero);349const __m128i src_2 = _mm_unpacklo_epi8(src2, zero);350const __m128i src_3 = _mm_unpacklo_epi8(src3, zero);351// Load ref and convert to 16b.352const __m128i ref0 = _mm_loadl_epi64((const __m128i*)&ref[0 * BPS]);353const __m128i ref1 = _mm_loadl_epi64((const __m128i*)&ref[1 * BPS]);354const __m128i ref2 = _mm_loadl_epi64((const __m128i*)&ref[2 * BPS]);355const __m128i ref3 = _mm_loadl_epi64((const __m128i*)&ref[3 * BPS]);356const __m128i ref_0 = _mm_unpacklo_epi8(ref0, zero);357const __m128i ref_1 = _mm_unpacklo_epi8(ref1, zero);358const __m128i ref_2 = _mm_unpacklo_epi8(ref2, zero);359const __m128i ref_3 = _mm_unpacklo_epi8(ref3, zero);360// Compute difference. -> 00 01 02 03 00' 01' 02' 03'361const __m128i diff0 = _mm_sub_epi16(src_0, ref_0);362const __m128i diff1 = _mm_sub_epi16(src_1, ref_1);363const __m128i diff2 = _mm_sub_epi16(src_2, ref_2);364const __m128i diff3 = _mm_sub_epi16(src_3, ref_3);365366// Unpack and shuffle367// 00 01 02 03 0 0 0 0368// 10 11 12 13 0 0 0 0369// 20 21 22 23 0 0 0 0370// 30 31 32 33 0 0 0 0371const __m128i shuf01l = _mm_unpacklo_epi32(diff0, diff1);372const __m128i shuf23l = _mm_unpacklo_epi32(diff2, diff3);373const __m128i shuf01h = _mm_unpackhi_epi32(diff0, diff1);374const __m128i shuf23h = _mm_unpackhi_epi32(diff2, diff3);375__m128i v01l, v32l;376__m128i v01h, v32h;377378// First pass379FTransformPass1_SSE2(&shuf01l, &shuf23l, &v01l, &v32l);380FTransformPass1_SSE2(&shuf01h, &shuf23h, &v01h, &v32h);381382// Second pass383FTransformPass2_SSE2(&v01l, &v32l, out + 0);384FTransformPass2_SSE2(&v01h, &v32h, out + 16);385}386387static void FTransformWHTRow_SSE2(const int16_t* const in, __m128i* const out) {388const __m128i kMult = _mm_set_epi16(-1, 1, -1, 1, 1, 1, 1, 1);389const __m128i src0 = _mm_loadl_epi64((__m128i*)&in[0 * 16]);390const __m128i src1 = _mm_loadl_epi64((__m128i*)&in[1 * 16]);391const __m128i src2 = _mm_loadl_epi64((__m128i*)&in[2 * 16]);392const __m128i src3 = _mm_loadl_epi64((__m128i*)&in[3 * 16]);393const __m128i A01 = _mm_unpacklo_epi16(src0, src1); // A0 A1 | ...394const __m128i A23 = _mm_unpacklo_epi16(src2, src3); // A2 A3 | ...395const __m128i B0 = _mm_adds_epi16(A01, A23); // a0 | a1 | ...396const __m128i B1 = _mm_subs_epi16(A01, A23); // a3 | a2 | ...397const __m128i C0 = _mm_unpacklo_epi32(B0, B1); // a0 | a1 | a3 | a2 | ...398const __m128i C1 = _mm_unpacklo_epi32(B1, B0); // a3 | a2 | a0 | a1 | ...399const __m128i D = _mm_unpacklo_epi64(C0, C1); // a0 a1 a3 a2 a3 a2 a0 a1400*out = _mm_madd_epi16(D, kMult);401}402403static void FTransformWHT_SSE2(const int16_t* in, int16_t* out) {404// Input is 12b signed.405__m128i row0, row1, row2, row3;406// Rows are 14b signed.407FTransformWHTRow_SSE2(in + 0 * 64, &row0);408FTransformWHTRow_SSE2(in + 1 * 64, &row1);409FTransformWHTRow_SSE2(in + 2 * 64, &row2);410FTransformWHTRow_SSE2(in + 3 * 64, &row3);411412{413// The a* are 15b signed.414const __m128i a0 = _mm_add_epi32(row0, row2);415const __m128i a1 = _mm_add_epi32(row1, row3);416const __m128i a2 = _mm_sub_epi32(row1, row3);417const __m128i a3 = _mm_sub_epi32(row0, row2);418const __m128i a0a3 = _mm_packs_epi32(a0, a3);419const __m128i a1a2 = _mm_packs_epi32(a1, a2);420421// The b* are 16b signed.422const __m128i b0b1 = _mm_add_epi16(a0a3, a1a2);423const __m128i b3b2 = _mm_sub_epi16(a0a3, a1a2);424const __m128i tmp_b2b3 = _mm_unpackhi_epi64(b3b2, b3b2);425const __m128i b2b3 = _mm_unpacklo_epi64(tmp_b2b3, b3b2);426427_mm_storeu_si128((__m128i*)&out[0], _mm_srai_epi16(b0b1, 1));428_mm_storeu_si128((__m128i*)&out[8], _mm_srai_epi16(b2b3, 1));429}430}431432//------------------------------------------------------------------------------433// Compute susceptibility based on DCT-coeff histograms:434// the higher, the "easier" the macroblock is to compress.435436static void CollectHistogram_SSE2(const uint8_t* ref, const uint8_t* pred,437int start_block, int end_block,438VP8Histogram* const histo) {439const __m128i zero = _mm_setzero_si128();440const __m128i max_coeff_thresh = _mm_set1_epi16(MAX_COEFF_THRESH);441int j;442int distribution[MAX_COEFF_THRESH + 1] = { 0 };443for (j = start_block; j < end_block; ++j) {444int16_t out[16];445int k;446447FTransform_SSE2(ref + VP8DspScan[j], pred + VP8DspScan[j], out);448449// Convert coefficients to bin (within out[]).450{451// Load.452const __m128i out0 = _mm_loadu_si128((__m128i*)&out[0]);453const __m128i out1 = _mm_loadu_si128((__m128i*)&out[8]);454const __m128i d0 = _mm_sub_epi16(zero, out0);455const __m128i d1 = _mm_sub_epi16(zero, out1);456const __m128i abs0 = _mm_max_epi16(out0, d0); // abs(v), 16b457const __m128i abs1 = _mm_max_epi16(out1, d1);458// v = abs(out) >> 3459const __m128i v0 = _mm_srai_epi16(abs0, 3);460const __m128i v1 = _mm_srai_epi16(abs1, 3);461// bin = min(v, MAX_COEFF_THRESH)462const __m128i bin0 = _mm_min_epi16(v0, max_coeff_thresh);463const __m128i bin1 = _mm_min_epi16(v1, max_coeff_thresh);464// Store.465_mm_storeu_si128((__m128i*)&out[0], bin0);466_mm_storeu_si128((__m128i*)&out[8], bin1);467}468469// Convert coefficients to bin.470for (k = 0; k < 16; ++k) {471++distribution[out[k]];472}473}474VP8SetHistogramData(distribution, histo);475}476477//------------------------------------------------------------------------------478// Intra predictions479480// helper for chroma-DC predictions481static WEBP_INLINE void Put8x8uv_SSE2(uint8_t v, uint8_t* dst) {482int j;483const __m128i values = _mm_set1_epi8(v);484for (j = 0; j < 8; ++j) {485_mm_storel_epi64((__m128i*)(dst + j * BPS), values);486}487}488489static WEBP_INLINE void Put16_SSE2(uint8_t v, uint8_t* dst) {490int j;491const __m128i values = _mm_set1_epi8(v);492for (j = 0; j < 16; ++j) {493_mm_store_si128((__m128i*)(dst + j * BPS), values);494}495}496497static WEBP_INLINE void Fill_SSE2(uint8_t* dst, int value, int size) {498if (size == 4) {499int j;500for (j = 0; j < 4; ++j) {501memset(dst + j * BPS, value, 4);502}503} else if (size == 8) {504Put8x8uv_SSE2(value, dst);505} else {506Put16_SSE2(value, dst);507}508}509510static WEBP_INLINE void VE8uv_SSE2(uint8_t* dst, const uint8_t* top) {511int j;512const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);513for (j = 0; j < 8; ++j) {514_mm_storel_epi64((__m128i*)(dst + j * BPS), top_values);515}516}517518static WEBP_INLINE void VE16_SSE2(uint8_t* dst, const uint8_t* top) {519const __m128i top_values = _mm_load_si128((const __m128i*)top);520int j;521for (j = 0; j < 16; ++j) {522_mm_store_si128((__m128i*)(dst + j * BPS), top_values);523}524}525526static WEBP_INLINE void VerticalPred_SSE2(uint8_t* dst,527const uint8_t* top, int size) {528if (top != NULL) {529if (size == 8) {530VE8uv_SSE2(dst, top);531} else {532VE16_SSE2(dst, top);533}534} else {535Fill_SSE2(dst, 127, size);536}537}538539static WEBP_INLINE void HE8uv_SSE2(uint8_t* dst, const uint8_t* left) {540int j;541for (j = 0; j < 8; ++j) {542const __m128i values = _mm_set1_epi8(left[j]);543_mm_storel_epi64((__m128i*)dst, values);544dst += BPS;545}546}547548static WEBP_INLINE void HE16_SSE2(uint8_t* dst, const uint8_t* left) {549int j;550for (j = 0; j < 16; ++j) {551const __m128i values = _mm_set1_epi8(left[j]);552_mm_store_si128((__m128i*)dst, values);553dst += BPS;554}555}556557static WEBP_INLINE void HorizontalPred_SSE2(uint8_t* dst,558const uint8_t* left, int size) {559if (left != NULL) {560if (size == 8) {561HE8uv_SSE2(dst, left);562} else {563HE16_SSE2(dst, left);564}565} else {566Fill_SSE2(dst, 129, size);567}568}569570static WEBP_INLINE void TM_SSE2(uint8_t* dst, const uint8_t* left,571const uint8_t* top, int size) {572const __m128i zero = _mm_setzero_si128();573int y;574if (size == 8) {575const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);576const __m128i top_base = _mm_unpacklo_epi8(top_values, zero);577for (y = 0; y < 8; ++y, dst += BPS) {578const int val = left[y] - left[-1];579const __m128i base = _mm_set1_epi16(val);580const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero);581_mm_storel_epi64((__m128i*)dst, out);582}583} else {584const __m128i top_values = _mm_load_si128((const __m128i*)top);585const __m128i top_base_0 = _mm_unpacklo_epi8(top_values, zero);586const __m128i top_base_1 = _mm_unpackhi_epi8(top_values, zero);587for (y = 0; y < 16; ++y, dst += BPS) {588const int val = left[y] - left[-1];589const __m128i base = _mm_set1_epi16(val);590const __m128i out_0 = _mm_add_epi16(base, top_base_0);591const __m128i out_1 = _mm_add_epi16(base, top_base_1);592const __m128i out = _mm_packus_epi16(out_0, out_1);593_mm_store_si128((__m128i*)dst, out);594}595}596}597598static WEBP_INLINE void TrueMotion_SSE2(uint8_t* dst, const uint8_t* left,599const uint8_t* top, int size) {600if (left != NULL) {601if (top != NULL) {602TM_SSE2(dst, left, top, size);603} else {604HorizontalPred_SSE2(dst, left, size);605}606} else {607// true motion without left samples (hence: with default 129 value)608// is equivalent to VE prediction where you just copy the top samples.609// Note that if top samples are not available, the default value is610// then 129, and not 127 as in the VerticalPred case.611if (top != NULL) {612VerticalPred_SSE2(dst, top, size);613} else {614Fill_SSE2(dst, 129, size);615}616}617}618619static WEBP_INLINE void DC8uv_SSE2(uint8_t* dst, const uint8_t* left,620const uint8_t* top) {621const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);622const __m128i left_values = _mm_loadl_epi64((const __m128i*)left);623const __m128i combined = _mm_unpacklo_epi64(top_values, left_values);624const int DC = VP8HorizontalAdd8b(&combined) + 8;625Put8x8uv_SSE2(DC >> 4, dst);626}627628static WEBP_INLINE void DC8uvNoLeft_SSE2(uint8_t* dst, const uint8_t* top) {629const __m128i zero = _mm_setzero_si128();630const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);631const __m128i sum = _mm_sad_epu8(top_values, zero);632const int DC = _mm_cvtsi128_si32(sum) + 4;633Put8x8uv_SSE2(DC >> 3, dst);634}635636static WEBP_INLINE void DC8uvNoTop_SSE2(uint8_t* dst, const uint8_t* left) {637// 'left' is contiguous so we can reuse the top summation.638DC8uvNoLeft_SSE2(dst, left);639}640641static WEBP_INLINE void DC8uvNoTopLeft_SSE2(uint8_t* dst) {642Put8x8uv_SSE2(0x80, dst);643}644645static WEBP_INLINE void DC8uvMode_SSE2(uint8_t* dst, const uint8_t* left,646const uint8_t* top) {647if (top != NULL) {648if (left != NULL) { // top and left present649DC8uv_SSE2(dst, left, top);650} else { // top, but no left651DC8uvNoLeft_SSE2(dst, top);652}653} else if (left != NULL) { // left but no top654DC8uvNoTop_SSE2(dst, left);655} else { // no top, no left, nothing.656DC8uvNoTopLeft_SSE2(dst);657}658}659660static WEBP_INLINE void DC16_SSE2(uint8_t* dst, const uint8_t* left,661const uint8_t* top) {662const __m128i top_row = _mm_load_si128((const __m128i*)top);663const __m128i left_row = _mm_load_si128((const __m128i*)left);664const int DC =665VP8HorizontalAdd8b(&top_row) + VP8HorizontalAdd8b(&left_row) + 16;666Put16_SSE2(DC >> 5, dst);667}668669static WEBP_INLINE void DC16NoLeft_SSE2(uint8_t* dst, const uint8_t* top) {670const __m128i top_row = _mm_load_si128((const __m128i*)top);671const int DC = VP8HorizontalAdd8b(&top_row) + 8;672Put16_SSE2(DC >> 4, dst);673}674675static WEBP_INLINE void DC16NoTop_SSE2(uint8_t* dst, const uint8_t* left) {676// 'left' is contiguous so we can reuse the top summation.677DC16NoLeft_SSE2(dst, left);678}679680static WEBP_INLINE void DC16NoTopLeft_SSE2(uint8_t* dst) {681Put16_SSE2(0x80, dst);682}683684static WEBP_INLINE void DC16Mode_SSE2(uint8_t* dst, const uint8_t* left,685const uint8_t* top) {686if (top != NULL) {687if (left != NULL) { // top and left present688DC16_SSE2(dst, left, top);689} else { // top, but no left690DC16NoLeft_SSE2(dst, top);691}692} else if (left != NULL) { // left but no top693DC16NoTop_SSE2(dst, left);694} else { // no top, no left, nothing.695DC16NoTopLeft_SSE2(dst);696}697}698699//------------------------------------------------------------------------------700// 4x4 predictions701702#define DST(x, y) dst[(x) + (y) * BPS]703#define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)704#define AVG2(a, b) (((a) + (b) + 1) >> 1)705706// We use the following 8b-arithmetic tricks:707// (a + 2 * b + c + 2) >> 2 = (AC + b + 1) >> 1708// where: AC = (a + c) >> 1 = [(a + c + 1) >> 1] - [(a^c) & 1]709// and:710// (a + 2 * b + c + 2) >> 2 = (AB + BC + 1) >> 1 - (ab|bc)&lsb711// where: AC = (a + b + 1) >> 1, BC = (b + c + 1) >> 1712// and ab = a ^ b, bc = b ^ c, lsb = (AC^BC)&1713714static WEBP_INLINE void VE4_SSE2(uint8_t* dst,715const uint8_t* top) { // vertical716const __m128i one = _mm_set1_epi8(1);717const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(top - 1));718const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1);719const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2);720const __m128i a = _mm_avg_epu8(ABCDEFGH, CDEFGH00);721const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGH00), one);722const __m128i b = _mm_subs_epu8(a, lsb);723const __m128i avg = _mm_avg_epu8(b, BCDEFGH0);724const uint32_t vals = _mm_cvtsi128_si32(avg);725int i;726for (i = 0; i < 4; ++i) {727WebPUint32ToMem(dst + i * BPS, vals);728}729}730731static WEBP_INLINE void HE4_SSE2(uint8_t* dst,732const uint8_t* top) { // horizontal733const int X = top[-1];734const int I = top[-2];735const int J = top[-3];736const int K = top[-4];737const int L = top[-5];738WebPUint32ToMem(dst + 0 * BPS, 0x01010101U * AVG3(X, I, J));739WebPUint32ToMem(dst + 1 * BPS, 0x01010101U * AVG3(I, J, K));740WebPUint32ToMem(dst + 2 * BPS, 0x01010101U * AVG3(J, K, L));741WebPUint32ToMem(dst + 3 * BPS, 0x01010101U * AVG3(K, L, L));742}743744static WEBP_INLINE void DC4_SSE2(uint8_t* dst, const uint8_t* top) {745uint32_t dc = 4;746int i;747for (i = 0; i < 4; ++i) dc += top[i] + top[-5 + i];748Fill_SSE2(dst, dc >> 3, 4);749}750751static WEBP_INLINE void LD4_SSE2(uint8_t* dst,752const uint8_t* top) { // Down-Left753const __m128i one = _mm_set1_epi8(1);754const __m128i ABCDEFGH = _mm_loadl_epi64((const __m128i*)top);755const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1);756const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2);757const __m128i CDEFGHH0 = _mm_insert_epi16(CDEFGH00, top[7], 3);758const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, CDEFGHH0);759const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGHH0), one);760const __m128i avg2 = _mm_subs_epu8(avg1, lsb);761const __m128i abcdefg = _mm_avg_epu8(avg2, BCDEFGH0);762WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( abcdefg ));763WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1)));764WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2)));765WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3)));766}767768static WEBP_INLINE void VR4_SSE2(uint8_t* dst,769const uint8_t* top) { // Vertical-Right770const __m128i one = _mm_set1_epi8(1);771const int I = top[-2];772const int J = top[-3];773const int K = top[-4];774const int X = top[-1];775const __m128i XABCD = _mm_loadl_epi64((const __m128i*)(top - 1));776const __m128i ABCD0 = _mm_srli_si128(XABCD, 1);777const __m128i abcd = _mm_avg_epu8(XABCD, ABCD0);778const __m128i _XABCD = _mm_slli_si128(XABCD, 1);779const __m128i IXABCD = _mm_insert_epi16(_XABCD, I | (X << 8), 0);780const __m128i avg1 = _mm_avg_epu8(IXABCD, ABCD0);781const __m128i lsb = _mm_and_si128(_mm_xor_si128(IXABCD, ABCD0), one);782const __m128i avg2 = _mm_subs_epu8(avg1, lsb);783const __m128i efgh = _mm_avg_epu8(avg2, XABCD);784WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( abcd ));785WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32( efgh ));786WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(abcd, 1)));787WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(efgh, 1)));788789// these two are hard to implement in SSE2, so we keep the C-version:790DST(0, 2) = AVG3(J, I, X);791DST(0, 3) = AVG3(K, J, I);792}793794static WEBP_INLINE void VL4_SSE2(uint8_t* dst,795const uint8_t* top) { // Vertical-Left796const __m128i one = _mm_set1_epi8(1);797const __m128i ABCDEFGH = _mm_loadl_epi64((const __m128i*)top);798const __m128i BCDEFGH_ = _mm_srli_si128(ABCDEFGH, 1);799const __m128i CDEFGH__ = _mm_srli_si128(ABCDEFGH, 2);800const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, BCDEFGH_);801const __m128i avg2 = _mm_avg_epu8(CDEFGH__, BCDEFGH_);802const __m128i avg3 = _mm_avg_epu8(avg1, avg2);803const __m128i lsb1 = _mm_and_si128(_mm_xor_si128(avg1, avg2), one);804const __m128i ab = _mm_xor_si128(ABCDEFGH, BCDEFGH_);805const __m128i bc = _mm_xor_si128(CDEFGH__, BCDEFGH_);806const __m128i abbc = _mm_or_si128(ab, bc);807const __m128i lsb2 = _mm_and_si128(abbc, lsb1);808const __m128i avg4 = _mm_subs_epu8(avg3, lsb2);809const uint32_t extra_out = _mm_cvtsi128_si32(_mm_srli_si128(avg4, 4));810WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( avg1 ));811WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32( avg4 ));812WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg1, 1)));813WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg4, 1)));814815// these two are hard to get and irregular816DST(3, 2) = (extra_out >> 0) & 0xff;817DST(3, 3) = (extra_out >> 8) & 0xff;818}819820static WEBP_INLINE void RD4_SSE2(uint8_t* dst,821const uint8_t* top) { // Down-right822const __m128i one = _mm_set1_epi8(1);823const __m128i LKJIXABC = _mm_loadl_epi64((const __m128i*)(top - 5));824const __m128i LKJIXABCD = _mm_insert_epi16(LKJIXABC, top[3], 4);825const __m128i KJIXABCD_ = _mm_srli_si128(LKJIXABCD, 1);826const __m128i JIXABCD__ = _mm_srli_si128(LKJIXABCD, 2);827const __m128i avg1 = _mm_avg_epu8(JIXABCD__, LKJIXABCD);828const __m128i lsb = _mm_and_si128(_mm_xor_si128(JIXABCD__, LKJIXABCD), one);829const __m128i avg2 = _mm_subs_epu8(avg1, lsb);830const __m128i abcdefg = _mm_avg_epu8(avg2, KJIXABCD_);831WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32( abcdefg ));832WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1)));833WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2)));834WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3)));835}836837static WEBP_INLINE void HU4_SSE2(uint8_t* dst, const uint8_t* top) {838const int I = top[-2];839const int J = top[-3];840const int K = top[-4];841const int L = top[-5];842DST(0, 0) = AVG2(I, J);843DST(2, 0) = DST(0, 1) = AVG2(J, K);844DST(2, 1) = DST(0, 2) = AVG2(K, L);845DST(1, 0) = AVG3(I, J, K);846DST(3, 0) = DST(1, 1) = AVG3(J, K, L);847DST(3, 1) = DST(1, 2) = AVG3(K, L, L);848DST(3, 2) = DST(2, 2) =849DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;850}851852static WEBP_INLINE void HD4_SSE2(uint8_t* dst, const uint8_t* top) {853const int X = top[-1];854const int I = top[-2];855const int J = top[-3];856const int K = top[-4];857const int L = top[-5];858const int A = top[0];859const int B = top[1];860const int C = top[2];861862DST(0, 0) = DST(2, 1) = AVG2(I, X);863DST(0, 1) = DST(2, 2) = AVG2(J, I);864DST(0, 2) = DST(2, 3) = AVG2(K, J);865DST(0, 3) = AVG2(L, K);866867DST(3, 0) = AVG3(A, B, C);868DST(2, 0) = AVG3(X, A, B);869DST(1, 0) = DST(3, 1) = AVG3(I, X, A);870DST(1, 1) = DST(3, 2) = AVG3(J, I, X);871DST(1, 2) = DST(3, 3) = AVG3(K, J, I);872DST(1, 3) = AVG3(L, K, J);873}874875static WEBP_INLINE void TM4_SSE2(uint8_t* dst, const uint8_t* top) {876const __m128i zero = _mm_setzero_si128();877const __m128i top_values = _mm_cvtsi32_si128(WebPMemToUint32(top));878const __m128i top_base = _mm_unpacklo_epi8(top_values, zero);879int y;880for (y = 0; y < 4; ++y, dst += BPS) {881const int val = top[-2 - y] - top[-1];882const __m128i base = _mm_set1_epi16(val);883const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero);884WebPUint32ToMem(dst, _mm_cvtsi128_si32(out));885}886}887888#undef DST889#undef AVG3890#undef AVG2891892//------------------------------------------------------------------------------893// luma 4x4 prediction894895// Left samples are top[-5 .. -2], top_left is top[-1], top are896// located at top[0..3], and top right is top[4..7]897static void Intra4Preds_SSE2(uint8_t* dst, const uint8_t* top) {898DC4_SSE2(I4DC4 + dst, top);899TM4_SSE2(I4TM4 + dst, top);900VE4_SSE2(I4VE4 + dst, top);901HE4_SSE2(I4HE4 + dst, top);902RD4_SSE2(I4RD4 + dst, top);903VR4_SSE2(I4VR4 + dst, top);904LD4_SSE2(I4LD4 + dst, top);905VL4_SSE2(I4VL4 + dst, top);906HD4_SSE2(I4HD4 + dst, top);907HU4_SSE2(I4HU4 + dst, top);908}909910//------------------------------------------------------------------------------911// Chroma 8x8 prediction (paragraph 12.2)912913static void IntraChromaPreds_SSE2(uint8_t* dst, const uint8_t* left,914const uint8_t* top) {915// U block916DC8uvMode_SSE2(C8DC8 + dst, left, top);917VerticalPred_SSE2(C8VE8 + dst, top, 8);918HorizontalPred_SSE2(C8HE8 + dst, left, 8);919TrueMotion_SSE2(C8TM8 + dst, left, top, 8);920// V block921dst += 8;922if (top != NULL) top += 8;923if (left != NULL) left += 16;924DC8uvMode_SSE2(C8DC8 + dst, left, top);925VerticalPred_SSE2(C8VE8 + dst, top, 8);926HorizontalPred_SSE2(C8HE8 + dst, left, 8);927TrueMotion_SSE2(C8TM8 + dst, left, top, 8);928}929930//------------------------------------------------------------------------------931// luma 16x16 prediction (paragraph 12.3)932933static void Intra16Preds_SSE2(uint8_t* dst,934const uint8_t* left, const uint8_t* top) {935DC16Mode_SSE2(I16DC16 + dst, left, top);936VerticalPred_SSE2(I16VE16 + dst, top, 16);937HorizontalPred_SSE2(I16HE16 + dst, left, 16);938TrueMotion_SSE2(I16TM16 + dst, left, top, 16);939}940941//------------------------------------------------------------------------------942// Metric943944static WEBP_INLINE void SubtractAndAccumulate_SSE2(const __m128i a,945const __m128i b,946__m128i* const sum) {947// take abs(a-b) in 8b948const __m128i a_b = _mm_subs_epu8(a, b);949const __m128i b_a = _mm_subs_epu8(b, a);950const __m128i abs_a_b = _mm_or_si128(a_b, b_a);951// zero-extend to 16b952const __m128i zero = _mm_setzero_si128();953const __m128i C0 = _mm_unpacklo_epi8(abs_a_b, zero);954const __m128i C1 = _mm_unpackhi_epi8(abs_a_b, zero);955// multiply with self956const __m128i sum1 = _mm_madd_epi16(C0, C0);957const __m128i sum2 = _mm_madd_epi16(C1, C1);958*sum = _mm_add_epi32(sum1, sum2);959}960961static WEBP_INLINE int SSE_16xN_SSE2(const uint8_t* a, const uint8_t* b,962int num_pairs) {963__m128i sum = _mm_setzero_si128();964int32_t tmp[4];965int i;966967for (i = 0; i < num_pairs; ++i) {968const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[BPS * 0]);969const __m128i b0 = _mm_loadu_si128((const __m128i*)&b[BPS * 0]);970const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[BPS * 1]);971const __m128i b1 = _mm_loadu_si128((const __m128i*)&b[BPS * 1]);972__m128i sum1, sum2;973SubtractAndAccumulate_SSE2(a0, b0, &sum1);974SubtractAndAccumulate_SSE2(a1, b1, &sum2);975sum = _mm_add_epi32(sum, _mm_add_epi32(sum1, sum2));976a += 2 * BPS;977b += 2 * BPS;978}979_mm_storeu_si128((__m128i*)tmp, sum);980return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);981}982983static int SSE16x16_SSE2(const uint8_t* a, const uint8_t* b) {984return SSE_16xN_SSE2(a, b, 8);985}986987static int SSE16x8_SSE2(const uint8_t* a, const uint8_t* b) {988return SSE_16xN_SSE2(a, b, 4);989}990991#define LOAD_8x16b(ptr) \992_mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(ptr)), zero)993994static int SSE8x8_SSE2(const uint8_t* a, const uint8_t* b) {995const __m128i zero = _mm_setzero_si128();996int num_pairs = 4;997__m128i sum = zero;998int32_t tmp[4];999while (num_pairs-- > 0) {1000const __m128i a0 = LOAD_8x16b(&a[BPS * 0]);1001const __m128i a1 = LOAD_8x16b(&a[BPS * 1]);1002const __m128i b0 = LOAD_8x16b(&b[BPS * 0]);1003const __m128i b1 = LOAD_8x16b(&b[BPS * 1]);1004// subtract1005const __m128i c0 = _mm_subs_epi16(a0, b0);1006const __m128i c1 = _mm_subs_epi16(a1, b1);1007// multiply/accumulate with self1008const __m128i d0 = _mm_madd_epi16(c0, c0);1009const __m128i d1 = _mm_madd_epi16(c1, c1);1010// collect1011const __m128i sum01 = _mm_add_epi32(d0, d1);1012sum = _mm_add_epi32(sum, sum01);1013a += 2 * BPS;1014b += 2 * BPS;1015}1016_mm_storeu_si128((__m128i*)tmp, sum);1017return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);1018}1019#undef LOAD_8x16b10201021static int SSE4x4_SSE2(const uint8_t* a, const uint8_t* b) {1022const __m128i zero = _mm_setzero_si128();10231024// Load values. Note that we read 8 pixels instead of 4,1025// but the a/b buffers are over-allocated to that effect.1026const __m128i a0 = _mm_loadl_epi64((const __m128i*)&a[BPS * 0]);1027const __m128i a1 = _mm_loadl_epi64((const __m128i*)&a[BPS * 1]);1028const __m128i a2 = _mm_loadl_epi64((const __m128i*)&a[BPS * 2]);1029const __m128i a3 = _mm_loadl_epi64((const __m128i*)&a[BPS * 3]);1030const __m128i b0 = _mm_loadl_epi64((const __m128i*)&b[BPS * 0]);1031const __m128i b1 = _mm_loadl_epi64((const __m128i*)&b[BPS * 1]);1032const __m128i b2 = _mm_loadl_epi64((const __m128i*)&b[BPS * 2]);1033const __m128i b3 = _mm_loadl_epi64((const __m128i*)&b[BPS * 3]);1034// Combine pair of lines.1035const __m128i a01 = _mm_unpacklo_epi32(a0, a1);1036const __m128i a23 = _mm_unpacklo_epi32(a2, a3);1037const __m128i b01 = _mm_unpacklo_epi32(b0, b1);1038const __m128i b23 = _mm_unpacklo_epi32(b2, b3);1039// Convert to 16b.1040const __m128i a01s = _mm_unpacklo_epi8(a01, zero);1041const __m128i a23s = _mm_unpacklo_epi8(a23, zero);1042const __m128i b01s = _mm_unpacklo_epi8(b01, zero);1043const __m128i b23s = _mm_unpacklo_epi8(b23, zero);1044// subtract, square and accumulate1045const __m128i d0 = _mm_subs_epi16(a01s, b01s);1046const __m128i d1 = _mm_subs_epi16(a23s, b23s);1047const __m128i e0 = _mm_madd_epi16(d0, d0);1048const __m128i e1 = _mm_madd_epi16(d1, d1);1049const __m128i sum = _mm_add_epi32(e0, e1);10501051int32_t tmp[4];1052_mm_storeu_si128((__m128i*)tmp, sum);1053return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);1054}10551056//------------------------------------------------------------------------------10571058static void Mean16x4_SSE2(const uint8_t* ref, uint32_t dc[4]) {1059const __m128i mask = _mm_set1_epi16(0x00ff);1060const __m128i a0 = _mm_loadu_si128((const __m128i*)&ref[BPS * 0]);1061const __m128i a1 = _mm_loadu_si128((const __m128i*)&ref[BPS * 1]);1062const __m128i a2 = _mm_loadu_si128((const __m128i*)&ref[BPS * 2]);1063const __m128i a3 = _mm_loadu_si128((const __m128i*)&ref[BPS * 3]);1064const __m128i b0 = _mm_srli_epi16(a0, 8); // hi byte1065const __m128i b1 = _mm_srli_epi16(a1, 8);1066const __m128i b2 = _mm_srli_epi16(a2, 8);1067const __m128i b3 = _mm_srli_epi16(a3, 8);1068const __m128i c0 = _mm_and_si128(a0, mask); // lo byte1069const __m128i c1 = _mm_and_si128(a1, mask);1070const __m128i c2 = _mm_and_si128(a2, mask);1071const __m128i c3 = _mm_and_si128(a3, mask);1072const __m128i d0 = _mm_add_epi32(b0, c0);1073const __m128i d1 = _mm_add_epi32(b1, c1);1074const __m128i d2 = _mm_add_epi32(b2, c2);1075const __m128i d3 = _mm_add_epi32(b3, c3);1076const __m128i e0 = _mm_add_epi32(d0, d1);1077const __m128i e1 = _mm_add_epi32(d2, d3);1078const __m128i f0 = _mm_add_epi32(e0, e1);1079uint16_t tmp[8];1080_mm_storeu_si128((__m128i*)tmp, f0);1081dc[0] = tmp[0] + tmp[1];1082dc[1] = tmp[2] + tmp[3];1083dc[2] = tmp[4] + tmp[5];1084dc[3] = tmp[6] + tmp[7];1085}10861087//------------------------------------------------------------------------------1088// Texture distortion1089//1090// We try to match the spectral content (weighted) between source and1091// reconstructed samples.10921093// Hadamard transform1094// Returns the weighted sum of the absolute value of transformed coefficients.1095// w[] contains a row-major 4 by 4 symmetric matrix.1096static int TTransform_SSE2(const uint8_t* inA, const uint8_t* inB,1097const uint16_t* const w) {1098int32_t sum[4];1099__m128i tmp_0, tmp_1, tmp_2, tmp_3;1100const __m128i zero = _mm_setzero_si128();11011102// Load and combine inputs.1103{1104const __m128i inA_0 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 0]);1105const __m128i inA_1 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 1]);1106const __m128i inA_2 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 2]);1107const __m128i inA_3 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 3]);1108const __m128i inB_0 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 0]);1109const __m128i inB_1 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 1]);1110const __m128i inB_2 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 2]);1111const __m128i inB_3 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 3]);11121113// Combine inA and inB (we'll do two transforms in parallel).1114const __m128i inAB_0 = _mm_unpacklo_epi32(inA_0, inB_0);1115const __m128i inAB_1 = _mm_unpacklo_epi32(inA_1, inB_1);1116const __m128i inAB_2 = _mm_unpacklo_epi32(inA_2, inB_2);1117const __m128i inAB_3 = _mm_unpacklo_epi32(inA_3, inB_3);1118tmp_0 = _mm_unpacklo_epi8(inAB_0, zero);1119tmp_1 = _mm_unpacklo_epi8(inAB_1, zero);1120tmp_2 = _mm_unpacklo_epi8(inAB_2, zero);1121tmp_3 = _mm_unpacklo_epi8(inAB_3, zero);1122// a00 a01 a02 a03 b00 b01 b02 b031123// a10 a11 a12 a13 b10 b11 b12 b131124// a20 a21 a22 a23 b20 b21 b22 b231125// a30 a31 a32 a33 b30 b31 b32 b331126}11271128// Vertical pass first to avoid a transpose (vertical and horizontal passes1129// are commutative because w/kWeightY is symmetric) and subsequent transpose.1130{1131// Calculate a and b (two 4x4 at once).1132const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2);1133const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3);1134const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3);1135const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2);1136const __m128i b0 = _mm_add_epi16(a0, a1);1137const __m128i b1 = _mm_add_epi16(a3, a2);1138const __m128i b2 = _mm_sub_epi16(a3, a2);1139const __m128i b3 = _mm_sub_epi16(a0, a1);1140// a00 a01 a02 a03 b00 b01 b02 b031141// a10 a11 a12 a13 b10 b11 b12 b131142// a20 a21 a22 a23 b20 b21 b22 b231143// a30 a31 a32 a33 b30 b31 b32 b3311441145// Transpose the two 4x4.1146VP8Transpose_2_4x4_16b(&b0, &b1, &b2, &b3, &tmp_0, &tmp_1, &tmp_2, &tmp_3);1147}11481149// Horizontal pass and difference of weighted sums.1150{1151// Load all inputs.1152const __m128i w_0 = _mm_loadu_si128((const __m128i*)&w[0]);1153const __m128i w_8 = _mm_loadu_si128((const __m128i*)&w[8]);11541155// Calculate a and b (two 4x4 at once).1156const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2);1157const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3);1158const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3);1159const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2);1160const __m128i b0 = _mm_add_epi16(a0, a1);1161const __m128i b1 = _mm_add_epi16(a3, a2);1162const __m128i b2 = _mm_sub_epi16(a3, a2);1163const __m128i b3 = _mm_sub_epi16(a0, a1);11641165// Separate the transforms of inA and inB.1166__m128i A_b0 = _mm_unpacklo_epi64(b0, b1);1167__m128i A_b2 = _mm_unpacklo_epi64(b2, b3);1168__m128i B_b0 = _mm_unpackhi_epi64(b0, b1);1169__m128i B_b2 = _mm_unpackhi_epi64(b2, b3);11701171{1172const __m128i d0 = _mm_sub_epi16(zero, A_b0);1173const __m128i d1 = _mm_sub_epi16(zero, A_b2);1174const __m128i d2 = _mm_sub_epi16(zero, B_b0);1175const __m128i d3 = _mm_sub_epi16(zero, B_b2);1176A_b0 = _mm_max_epi16(A_b0, d0); // abs(v), 16b1177A_b2 = _mm_max_epi16(A_b2, d1);1178B_b0 = _mm_max_epi16(B_b0, d2);1179B_b2 = _mm_max_epi16(B_b2, d3);1180}11811182// weighted sums1183A_b0 = _mm_madd_epi16(A_b0, w_0);1184A_b2 = _mm_madd_epi16(A_b2, w_8);1185B_b0 = _mm_madd_epi16(B_b0, w_0);1186B_b2 = _mm_madd_epi16(B_b2, w_8);1187A_b0 = _mm_add_epi32(A_b0, A_b2);1188B_b0 = _mm_add_epi32(B_b0, B_b2);11891190// difference of weighted sums1191A_b0 = _mm_sub_epi32(A_b0, B_b0);1192_mm_storeu_si128((__m128i*)&sum[0], A_b0);1193}1194return sum[0] + sum[1] + sum[2] + sum[3];1195}11961197static int Disto4x4_SSE2(const uint8_t* const a, const uint8_t* const b,1198const uint16_t* const w) {1199const int diff_sum = TTransform_SSE2(a, b, w);1200return abs(diff_sum) >> 5;1201}12021203static int Disto16x16_SSE2(const uint8_t* const a, const uint8_t* const b,1204const uint16_t* const w) {1205int D = 0;1206int x, y;1207for (y = 0; y < 16 * BPS; y += 4 * BPS) {1208for (x = 0; x < 16; x += 4) {1209D += Disto4x4_SSE2(a + x + y, b + x + y, w);1210}1211}1212return D;1213}12141215//------------------------------------------------------------------------------1216// Quantization1217//12181219static WEBP_INLINE int DoQuantizeBlock_SSE2(int16_t in[16], int16_t out[16],1220const uint16_t* const sharpen,1221const VP8Matrix* const mtx) {1222const __m128i max_coeff_2047 = _mm_set1_epi16(MAX_LEVEL);1223const __m128i zero = _mm_setzero_si128();1224__m128i coeff0, coeff8;1225__m128i out0, out8;1226__m128i packed_out;12271228// Load all inputs.1229__m128i in0 = _mm_loadu_si128((__m128i*)&in[0]);1230__m128i in8 = _mm_loadu_si128((__m128i*)&in[8]);1231const __m128i iq0 = _mm_loadu_si128((const __m128i*)&mtx->iq_[0]);1232const __m128i iq8 = _mm_loadu_si128((const __m128i*)&mtx->iq_[8]);1233const __m128i q0 = _mm_loadu_si128((const __m128i*)&mtx->q_[0]);1234const __m128i q8 = _mm_loadu_si128((const __m128i*)&mtx->q_[8]);12351236// extract sign(in) (0x0000 if positive, 0xffff if negative)1237const __m128i sign0 = _mm_cmpgt_epi16(zero, in0);1238const __m128i sign8 = _mm_cmpgt_epi16(zero, in8);12391240// coeff = abs(in) = (in ^ sign) - sign1241coeff0 = _mm_xor_si128(in0, sign0);1242coeff8 = _mm_xor_si128(in8, sign8);1243coeff0 = _mm_sub_epi16(coeff0, sign0);1244coeff8 = _mm_sub_epi16(coeff8, sign8);12451246// coeff = abs(in) + sharpen1247if (sharpen != NULL) {1248const __m128i sharpen0 = _mm_loadu_si128((const __m128i*)&sharpen[0]);1249const __m128i sharpen8 = _mm_loadu_si128((const __m128i*)&sharpen[8]);1250coeff0 = _mm_add_epi16(coeff0, sharpen0);1251coeff8 = _mm_add_epi16(coeff8, sharpen8);1252}12531254// out = (coeff * iQ + B) >> QFIX1255{1256// doing calculations with 32b precision (QFIX=17)1257// out = (coeff * iQ)1258const __m128i coeff_iQ0H = _mm_mulhi_epu16(coeff0, iq0);1259const __m128i coeff_iQ0L = _mm_mullo_epi16(coeff0, iq0);1260const __m128i coeff_iQ8H = _mm_mulhi_epu16(coeff8, iq8);1261const __m128i coeff_iQ8L = _mm_mullo_epi16(coeff8, iq8);1262__m128i out_00 = _mm_unpacklo_epi16(coeff_iQ0L, coeff_iQ0H);1263__m128i out_04 = _mm_unpackhi_epi16(coeff_iQ0L, coeff_iQ0H);1264__m128i out_08 = _mm_unpacklo_epi16(coeff_iQ8L, coeff_iQ8H);1265__m128i out_12 = _mm_unpackhi_epi16(coeff_iQ8L, coeff_iQ8H);1266// out = (coeff * iQ + B)1267const __m128i bias_00 = _mm_loadu_si128((const __m128i*)&mtx->bias_[0]);1268const __m128i bias_04 = _mm_loadu_si128((const __m128i*)&mtx->bias_[4]);1269const __m128i bias_08 = _mm_loadu_si128((const __m128i*)&mtx->bias_[8]);1270const __m128i bias_12 = _mm_loadu_si128((const __m128i*)&mtx->bias_[12]);1271out_00 = _mm_add_epi32(out_00, bias_00);1272out_04 = _mm_add_epi32(out_04, bias_04);1273out_08 = _mm_add_epi32(out_08, bias_08);1274out_12 = _mm_add_epi32(out_12, bias_12);1275// out = QUANTDIV(coeff, iQ, B, QFIX)1276out_00 = _mm_srai_epi32(out_00, QFIX);1277out_04 = _mm_srai_epi32(out_04, QFIX);1278out_08 = _mm_srai_epi32(out_08, QFIX);1279out_12 = _mm_srai_epi32(out_12, QFIX);12801281// pack result as 16b1282out0 = _mm_packs_epi32(out_00, out_04);1283out8 = _mm_packs_epi32(out_08, out_12);12841285// if (coeff > 2047) coeff = 20471286out0 = _mm_min_epi16(out0, max_coeff_2047);1287out8 = _mm_min_epi16(out8, max_coeff_2047);1288}12891290// get sign back (if (sign[j]) out_n = -out_n)1291out0 = _mm_xor_si128(out0, sign0);1292out8 = _mm_xor_si128(out8, sign8);1293out0 = _mm_sub_epi16(out0, sign0);1294out8 = _mm_sub_epi16(out8, sign8);12951296// in = out * Q1297in0 = _mm_mullo_epi16(out0, q0);1298in8 = _mm_mullo_epi16(out8, q8);12991300_mm_storeu_si128((__m128i*)&in[0], in0);1301_mm_storeu_si128((__m128i*)&in[8], in8);13021303// zigzag the output before storing it.1304//1305// The zigzag pattern can almost be reproduced with a small sequence of1306// shuffles. After it, we only need to swap the 7th (ending up in third1307// position instead of twelfth) and 8th values.1308{1309__m128i outZ0, outZ8;1310outZ0 = _mm_shufflehi_epi16(out0, _MM_SHUFFLE(2, 1, 3, 0));1311outZ0 = _mm_shuffle_epi32 (outZ0, _MM_SHUFFLE(3, 1, 2, 0));1312outZ0 = _mm_shufflehi_epi16(outZ0, _MM_SHUFFLE(3, 1, 0, 2));1313outZ8 = _mm_shufflelo_epi16(out8, _MM_SHUFFLE(3, 0, 2, 1));1314outZ8 = _mm_shuffle_epi32 (outZ8, _MM_SHUFFLE(3, 1, 2, 0));1315outZ8 = _mm_shufflelo_epi16(outZ8, _MM_SHUFFLE(1, 3, 2, 0));1316_mm_storeu_si128((__m128i*)&out[0], outZ0);1317_mm_storeu_si128((__m128i*)&out[8], outZ8);1318packed_out = _mm_packs_epi16(outZ0, outZ8);1319}1320{1321const int16_t outZ_12 = out[12];1322const int16_t outZ_3 = out[3];1323out[3] = outZ_12;1324out[12] = outZ_3;1325}13261327// detect if all 'out' values are zeroes or not1328return (_mm_movemask_epi8(_mm_cmpeq_epi8(packed_out, zero)) != 0xffff);1329}13301331static int QuantizeBlock_SSE2(int16_t in[16], int16_t out[16],1332const VP8Matrix* const mtx) {1333return DoQuantizeBlock_SSE2(in, out, &mtx->sharpen_[0], mtx);1334}13351336static int QuantizeBlockWHT_SSE2(int16_t in[16], int16_t out[16],1337const VP8Matrix* const mtx) {1338return DoQuantizeBlock_SSE2(in, out, NULL, mtx);1339}13401341static int Quantize2Blocks_SSE2(int16_t in[32], int16_t out[32],1342const VP8Matrix* const mtx) {1343int nz;1344const uint16_t* const sharpen = &mtx->sharpen_[0];1345nz = DoQuantizeBlock_SSE2(in + 0 * 16, out + 0 * 16, sharpen, mtx) << 0;1346nz |= DoQuantizeBlock_SSE2(in + 1 * 16, out + 1 * 16, sharpen, mtx) << 1;1347return nz;1348}13491350//------------------------------------------------------------------------------1351// Entry point13521353extern void VP8EncDspInitSSE2(void);13541355WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspInitSSE2(void) {1356VP8CollectHistogram = CollectHistogram_SSE2;1357VP8EncPredLuma16 = Intra16Preds_SSE2;1358VP8EncPredChroma8 = IntraChromaPreds_SSE2;1359VP8EncPredLuma4 = Intra4Preds_SSE2;1360VP8EncQuantizeBlock = QuantizeBlock_SSE2;1361VP8EncQuantize2Blocks = Quantize2Blocks_SSE2;1362VP8EncQuantizeBlockWHT = QuantizeBlockWHT_SSE2;1363VP8ITransform = ITransform_SSE2;1364VP8FTransform = FTransform_SSE2;1365VP8FTransform2 = FTransform2_SSE2;1366VP8FTransformWHT = FTransformWHT_SSE2;1367VP8SSE16x16 = SSE16x16_SSE2;1368VP8SSE16x8 = SSE16x8_SSE2;1369VP8SSE8x8 = SSE8x8_SSE2;1370VP8SSE4x4 = SSE4x4_SSE2;1371VP8TDisto4x4 = Disto4x4_SSE2;1372VP8TDisto16x16 = Disto16x16_SSE2;1373VP8Mean16x4 = Mean16x4_SSE2;1374}13751376#else // !WEBP_USE_SSE213771378WEBP_DSP_INIT_STUB(VP8EncDspInitSSE2)13791380#endif // WEBP_USE_SSE2138113821383