Path: blob/master/thirdparty/libwebp/src/dsp/enc_sse2.c
21521 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 <emmintrin.h>1718#include <assert.h>19#include <stdlib.h> // for abs()20#include <string.h>2122#include "src/dsp/common_sse2.h"23#include "src/dsp/cpu.h"24#include "src/enc/cost_enc.h"25#include "src/enc/vp8i_enc.h"26#include "src/utils/utils.h"27#include "src/webp/types.h"2829//------------------------------------------------------------------------------30// Transforms (Paragraph 14.4)3132// Does one inverse transform.33static void ITransform_One_SSE2(const uint8_t* WEBP_RESTRICT ref,34const int16_t* WEBP_RESTRICT in,35uint8_t* WEBP_RESTRICT dst) {36// This implementation makes use of 16-bit fixed point versions of two37// multiply constants:38// K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^1639// K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^1640//41// To be able to use signed 16-bit integers, we use the following trick to42// have constants within range:43// - Associated constants are obtained by subtracting the 16-bit fixed point44// version of one:45// k = K - (1 << 16) => K = k + (1 << 16)46// K1 = 85267 => k1 = 2009147// K2 = 35468 => k2 = -3006848// - The multiplication of a variable by a constant become the sum of the49// variable and the multiplication of that variable by the associated50// constant:51// (x * K) >> 16 = (x * (k + (1 << 16))) >> 16 = ((x * k ) >> 16) + x52const __m128i k1k2 = _mm_set_epi16(-30068, -30068, -30068, -30068,5320091, 20091, 20091, 20091);54const __m128i k2k1 = _mm_set_epi16(20091, 20091, 20091, 20091,55-30068, -30068, -30068, -30068);56const __m128i zero = _mm_setzero_si128();57const __m128i zero_four = _mm_set_epi16(0, 0, 0, 0, 4, 4, 4, 4);58__m128i T01, T23;5960// Load and concatenate the transform coefficients.61const __m128i in01 = _mm_loadu_si128((const __m128i*)&in[0]);62const __m128i in23 = _mm_loadu_si128((const __m128i*)&in[8]);63// a00 a10 a20 a30 a01 a11 a21 a3164// a02 a12 a22 a32 a03 a13 a23 a336566// Vertical pass and subsequent transpose.67{68const __m128i in1 = _mm_unpackhi_epi64(in01, in01);69const __m128i in3 = _mm_unpackhi_epi64(in23, in23);7071// First pass, c and d calculations are longer because of the "trick"72// multiplications.73// c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in374// d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in375const __m128i a_d3 = _mm_add_epi16(in01, in23);76const __m128i b_c3 = _mm_sub_epi16(in01, in23);77const __m128i c1d1 = _mm_mulhi_epi16(in1, k2k1);78const __m128i c2d2 = _mm_mulhi_epi16(in3, k1k2);79const __m128i c3 = _mm_unpackhi_epi64(b_c3, b_c3);80const __m128i c4 = _mm_sub_epi16(c1d1, c2d2);81const __m128i c = _mm_add_epi16(c3, c4);82const __m128i d4u = _mm_add_epi16(c1d1, c2d2);83const __m128i du = _mm_add_epi16(a_d3, d4u);84const __m128i d = _mm_unpackhi_epi64(du, du);8586// Second pass.87const __m128i comb_ab = _mm_unpacklo_epi64(a_d3, b_c3);88const __m128i comb_dc = _mm_unpacklo_epi64(d, c);8990const __m128i tmp01 = _mm_add_epi16(comb_ab, comb_dc);91const __m128i tmp32 = _mm_sub_epi16(comb_ab, comb_dc);92const __m128i tmp23 = _mm_shuffle_epi32(tmp32, _MM_SHUFFLE(1, 0, 3, 2));9394const __m128i transpose_0 = _mm_unpacklo_epi16(tmp01, tmp23);95const __m128i transpose_1 = _mm_unpackhi_epi16(tmp01, tmp23);96// a00 a20 a01 a21 a02 a22 a03 a2397// a10 a30 a11 a31 a12 a32 a13 a339899T01 = _mm_unpacklo_epi16(transpose_0, transpose_1);100T23 = _mm_unpackhi_epi16(transpose_0, transpose_1);101// a00 a10 a20 a30 a01 a11 a21 a31102// a02 a12 a22 a32 a03 a13 a23 a33103}104105// Horizontal pass and subsequent transpose.106{107const __m128i T1 = _mm_unpackhi_epi64(T01, T01);108const __m128i T3 = _mm_unpackhi_epi64(T23, T23);109110// First pass, c and d calculations are longer because of the "trick"111// multiplications.112const __m128i dc = _mm_add_epi16(T01, zero_four);113114// c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3115// d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3116const __m128i a_d3 = _mm_add_epi16(dc, T23);117const __m128i b_c3 = _mm_sub_epi16(dc, T23);118const __m128i c1d1 = _mm_mulhi_epi16(T1, k2k1);119const __m128i c2d2 = _mm_mulhi_epi16(T3, k1k2);120const __m128i c3 = _mm_unpackhi_epi64(b_c3, b_c3);121const __m128i c4 = _mm_sub_epi16(c1d1, c2d2);122const __m128i c = _mm_add_epi16(c3, c4);123const __m128i d4u = _mm_add_epi16(c1d1, c2d2);124const __m128i du = _mm_add_epi16(a_d3, d4u);125const __m128i d = _mm_unpackhi_epi64(du, du);126127// Second pass.128const __m128i comb_ab = _mm_unpacklo_epi64(a_d3, b_c3);129const __m128i comb_dc = _mm_unpacklo_epi64(d, c);130131const __m128i tmp01 = _mm_add_epi16(comb_ab, comb_dc);132const __m128i tmp32 = _mm_sub_epi16(comb_ab, comb_dc);133const __m128i tmp23 = _mm_shuffle_epi32(tmp32, _MM_SHUFFLE(1, 0, 3, 2));134135const __m128i shifted01 = _mm_srai_epi16(tmp01, 3);136const __m128i shifted23 = _mm_srai_epi16(tmp23, 3);137// a00 a01 a02 a03 a10 a11 a12 a13138// a20 a21 a22 a23 a30 a31 a32 a33139140const __m128i transpose_0 = _mm_unpacklo_epi16(shifted01, shifted23);141const __m128i transpose_1 = _mm_unpackhi_epi16(shifted01, shifted23);142// a00 a20 a01 a21 a02 a22 a03 a23143// a10 a30 a11 a31 a12 a32 a13 a33144145T01 = _mm_unpacklo_epi16(transpose_0, transpose_1);146T23 = _mm_unpackhi_epi16(transpose_0, transpose_1);147// a00 a10 a20 a30 a01 a11 a21 a31148// a02 a12 a22 a32 a03 a13 a23 a33149}150151// Add inverse transform to 'ref' and store.152{153// Load the reference(s).154__m128i ref01, ref23, ref0123;155int32_t buf[4];156157// Load four bytes/pixels per line.158const __m128i ref0 = _mm_cvtsi32_si128(WebPMemToInt32(&ref[0 * BPS]));159const __m128i ref1 = _mm_cvtsi32_si128(WebPMemToInt32(&ref[1 * BPS]));160const __m128i ref2 = _mm_cvtsi32_si128(WebPMemToInt32(&ref[2 * BPS]));161const __m128i ref3 = _mm_cvtsi32_si128(WebPMemToInt32(&ref[3 * BPS]));162ref01 = _mm_unpacklo_epi32(ref0, ref1);163ref23 = _mm_unpacklo_epi32(ref2, ref3);164165// Convert to 16b.166ref01 = _mm_unpacklo_epi8(ref01, zero);167ref23 = _mm_unpacklo_epi8(ref23, zero);168// Add the inverse transform(s).169ref01 = _mm_add_epi16(ref01, T01);170ref23 = _mm_add_epi16(ref23, T23);171// Unsigned saturate to 8b.172ref0123 = _mm_packus_epi16(ref01, ref23);173174_mm_storeu_si128((__m128i *)buf, ref0123);175176// Store four bytes/pixels per line.177WebPInt32ToMem(&dst[0 * BPS], buf[0]);178WebPInt32ToMem(&dst[1 * BPS], buf[1]);179WebPInt32ToMem(&dst[2 * BPS], buf[2]);180WebPInt32ToMem(&dst[3 * BPS], buf[3]);181}182}183184// Does two inverse transforms.185static void ITransform_Two_SSE2(const uint8_t* WEBP_RESTRICT ref,186const int16_t* WEBP_RESTRICT in,187uint8_t* WEBP_RESTRICT dst) {188// This implementation makes use of 16-bit fixed point versions of two189// multiply constants:190// K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^16191// K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^16192//193// To be able to use signed 16-bit integers, we use the following trick to194// have constants within range:195// - Associated constants are obtained by subtracting the 16-bit fixed point196// version of one:197// k = K - (1 << 16) => K = k + (1 << 16)198// K1 = 85267 => k1 = 20091199// K2 = 35468 => k2 = -30068200// - The multiplication of a variable by a constant become the sum of the201// variable and the multiplication of that variable by the associated202// constant:203// (x * K) >> 16 = (x * (k + (1 << 16))) >> 16 = ((x * k ) >> 16) + x204const __m128i k1 = _mm_set1_epi16(20091);205const __m128i k2 = _mm_set1_epi16(-30068);206__m128i T0, T1, T2, T3;207208// Load and concatenate the transform coefficients (we'll do two inverse209// transforms in parallel).210__m128i in0, in1, in2, in3;211{212const __m128i tmp0 = _mm_loadu_si128((const __m128i*)&in[0]);213const __m128i tmp1 = _mm_loadu_si128((const __m128i*)&in[8]);214const __m128i tmp2 = _mm_loadu_si128((const __m128i*)&in[16]);215const __m128i tmp3 = _mm_loadu_si128((const __m128i*)&in[24]);216in0 = _mm_unpacklo_epi64(tmp0, tmp2);217in1 = _mm_unpackhi_epi64(tmp0, tmp2);218in2 = _mm_unpacklo_epi64(tmp1, tmp3);219in3 = _mm_unpackhi_epi64(tmp1, tmp3);220// a00 a10 a20 a30 b00 b10 b20 b30221// a01 a11 a21 a31 b01 b11 b21 b31222// a02 a12 a22 a32 b02 b12 b22 b32223// a03 a13 a23 a33 b03 b13 b23 b33224}225226// Vertical pass and subsequent transpose.227{228// First pass, c and d calculations are longer because of the "trick"229// multiplications.230const __m128i a = _mm_add_epi16(in0, in2);231const __m128i b = _mm_sub_epi16(in0, in2);232// c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in3233const __m128i c1 = _mm_mulhi_epi16(in1, k2);234const __m128i c2 = _mm_mulhi_epi16(in3, k1);235const __m128i c3 = _mm_sub_epi16(in1, in3);236const __m128i c4 = _mm_sub_epi16(c1, c2);237const __m128i c = _mm_add_epi16(c3, c4);238// d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in3239const __m128i d1 = _mm_mulhi_epi16(in1, k1);240const __m128i d2 = _mm_mulhi_epi16(in3, k2);241const __m128i d3 = _mm_add_epi16(in1, in3);242const __m128i d4 = _mm_add_epi16(d1, d2);243const __m128i d = _mm_add_epi16(d3, d4);244245// Second pass.246const __m128i tmp0 = _mm_add_epi16(a, d);247const __m128i tmp1 = _mm_add_epi16(b, c);248const __m128i tmp2 = _mm_sub_epi16(b, c);249const __m128i tmp3 = _mm_sub_epi16(a, d);250251// Transpose the two 4x4.252VP8Transpose_2_4x4_16b(&tmp0, &tmp1, &tmp2, &tmp3, &T0, &T1, &T2, &T3);253}254255// Horizontal pass and subsequent transpose.256{257// First pass, c and d calculations are longer because of the "trick"258// multiplications.259const __m128i four = _mm_set1_epi16(4);260const __m128i dc = _mm_add_epi16(T0, four);261const __m128i a = _mm_add_epi16(dc, T2);262const __m128i b = _mm_sub_epi16(dc, T2);263// c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3264const __m128i c1 = _mm_mulhi_epi16(T1, k2);265const __m128i c2 = _mm_mulhi_epi16(T3, k1);266const __m128i c3 = _mm_sub_epi16(T1, T3);267const __m128i c4 = _mm_sub_epi16(c1, c2);268const __m128i c = _mm_add_epi16(c3, c4);269// d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3270const __m128i d1 = _mm_mulhi_epi16(T1, k1);271const __m128i d2 = _mm_mulhi_epi16(T3, k2);272const __m128i d3 = _mm_add_epi16(T1, T3);273const __m128i d4 = _mm_add_epi16(d1, d2);274const __m128i d = _mm_add_epi16(d3, d4);275276// Second pass.277const __m128i tmp0 = _mm_add_epi16(a, d);278const __m128i tmp1 = _mm_add_epi16(b, c);279const __m128i tmp2 = _mm_sub_epi16(b, c);280const __m128i tmp3 = _mm_sub_epi16(a, d);281const __m128i shifted0 = _mm_srai_epi16(tmp0, 3);282const __m128i shifted1 = _mm_srai_epi16(tmp1, 3);283const __m128i shifted2 = _mm_srai_epi16(tmp2, 3);284const __m128i shifted3 = _mm_srai_epi16(tmp3, 3);285286// Transpose the two 4x4.287VP8Transpose_2_4x4_16b(&shifted0, &shifted1, &shifted2, &shifted3, &T0, &T1,288&T2, &T3);289}290291// Add inverse transform to 'ref' and store.292{293const __m128i zero = _mm_setzero_si128();294// Load the reference(s).295__m128i ref0, ref1, ref2, ref3;296// Load eight bytes/pixels per line.297ref0 = _mm_loadl_epi64((const __m128i*)&ref[0 * BPS]);298ref1 = _mm_loadl_epi64((const __m128i*)&ref[1 * BPS]);299ref2 = _mm_loadl_epi64((const __m128i*)&ref[2 * BPS]);300ref3 = _mm_loadl_epi64((const __m128i*)&ref[3 * BPS]);301// Convert to 16b.302ref0 = _mm_unpacklo_epi8(ref0, zero);303ref1 = _mm_unpacklo_epi8(ref1, zero);304ref2 = _mm_unpacklo_epi8(ref2, zero);305ref3 = _mm_unpacklo_epi8(ref3, zero);306// Add the inverse transform(s).307ref0 = _mm_add_epi16(ref0, T0);308ref1 = _mm_add_epi16(ref1, T1);309ref2 = _mm_add_epi16(ref2, T2);310ref3 = _mm_add_epi16(ref3, T3);311// Unsigned saturate to 8b.312ref0 = _mm_packus_epi16(ref0, ref0);313ref1 = _mm_packus_epi16(ref1, ref1);314ref2 = _mm_packus_epi16(ref2, ref2);315ref3 = _mm_packus_epi16(ref3, ref3);316// Store eight bytes/pixels per line.317_mm_storel_epi64((__m128i*)&dst[0 * BPS], ref0);318_mm_storel_epi64((__m128i*)&dst[1 * BPS], ref1);319_mm_storel_epi64((__m128i*)&dst[2 * BPS], ref2);320_mm_storel_epi64((__m128i*)&dst[3 * BPS], ref3);321}322}323324// Does one or two inverse transforms.325static void ITransform_SSE2(const uint8_t* WEBP_RESTRICT ref,326const int16_t* WEBP_RESTRICT in,327uint8_t* WEBP_RESTRICT dst,328int do_two) {329if (do_two) {330ITransform_Two_SSE2(ref, in, dst);331} else {332ITransform_One_SSE2(ref, in, dst);333}334}335336static void FTransformPass1_SSE2(const __m128i* const in01,337const __m128i* const in23,338__m128i* const out01,339__m128i* const out32) {340const __m128i k937 = _mm_set1_epi32(937);341const __m128i k1812 = _mm_set1_epi32(1812);342343const __m128i k88p = _mm_set_epi16(8, 8, 8, 8, 8, 8, 8, 8);344const __m128i k88m = _mm_set_epi16(-8, 8, -8, 8, -8, 8, -8, 8);345const __m128i k5352_2217p = _mm_set_epi16(2217, 5352, 2217, 5352,3462217, 5352, 2217, 5352);347const __m128i k5352_2217m = _mm_set_epi16(-5352, 2217, -5352, 2217,348-5352, 2217, -5352, 2217);349350// *in01 = 00 01 10 11 02 03 12 13351// *in23 = 20 21 30 31 22 23 32 33352const __m128i shuf01_p = _mm_shufflehi_epi16(*in01, _MM_SHUFFLE(2, 3, 0, 1));353const __m128i shuf23_p = _mm_shufflehi_epi16(*in23, _MM_SHUFFLE(2, 3, 0, 1));354// 00 01 10 11 03 02 13 12355// 20 21 30 31 23 22 33 32356const __m128i s01 = _mm_unpacklo_epi64(shuf01_p, shuf23_p);357const __m128i s32 = _mm_unpackhi_epi64(shuf01_p, shuf23_p);358// 00 01 10 11 20 21 30 31359// 03 02 13 12 23 22 33 32360const __m128i a01 = _mm_add_epi16(s01, s32);361const __m128i a32 = _mm_sub_epi16(s01, s32);362// [d0 + d3 | d1 + d2 | ...] = [a0 a1 | a0' a1' | ... ]363// [d0 - d3 | d1 - d2 | ...] = [a3 a2 | a3' a2' | ... ]364365const __m128i tmp0 = _mm_madd_epi16(a01, k88p); // [ (a0 + a1) << 3, ... ]366const __m128i tmp2 = _mm_madd_epi16(a01, k88m); // [ (a0 - a1) << 3, ... ]367const __m128i tmp1_1 = _mm_madd_epi16(a32, k5352_2217p);368const __m128i tmp3_1 = _mm_madd_epi16(a32, k5352_2217m);369const __m128i tmp1_2 = _mm_add_epi32(tmp1_1, k1812);370const __m128i tmp3_2 = _mm_add_epi32(tmp3_1, k937);371const __m128i tmp1 = _mm_srai_epi32(tmp1_2, 9);372const __m128i tmp3 = _mm_srai_epi32(tmp3_2, 9);373const __m128i s03 = _mm_packs_epi32(tmp0, tmp2);374const __m128i s12 = _mm_packs_epi32(tmp1, tmp3);375const __m128i s_lo = _mm_unpacklo_epi16(s03, s12); // 0 1 0 1 0 1...376const __m128i s_hi = _mm_unpackhi_epi16(s03, s12); // 2 3 2 3 2 3377const __m128i v23 = _mm_unpackhi_epi32(s_lo, s_hi);378*out01 = _mm_unpacklo_epi32(s_lo, s_hi);379*out32 = _mm_shuffle_epi32(v23, _MM_SHUFFLE(1, 0, 3, 2)); // 3 2 3 2 3 2..380}381382static void FTransformPass2_SSE2(const __m128i* const v01,383const __m128i* const v32,384int16_t* WEBP_RESTRICT out) {385const __m128i zero = _mm_setzero_si128();386const __m128i seven = _mm_set1_epi16(7);387const __m128i k5352_2217 = _mm_set_epi16(5352, 2217, 5352, 2217,3885352, 2217, 5352, 2217);389const __m128i k2217_5352 = _mm_set_epi16(2217, -5352, 2217, -5352,3902217, -5352, 2217, -5352);391const __m128i k12000_plus_one = _mm_set1_epi32(12000 + (1 << 16));392const __m128i k51000 = _mm_set1_epi32(51000);393394// Same operations are done on the (0,3) and (1,2) pairs.395// a3 = v0 - v3396// a2 = v1 - v2397const __m128i a32 = _mm_sub_epi16(*v01, *v32);398const __m128i a22 = _mm_unpackhi_epi64(a32, a32);399400const __m128i b23 = _mm_unpacklo_epi16(a22, a32);401const __m128i c1 = _mm_madd_epi16(b23, k5352_2217);402const __m128i c3 = _mm_madd_epi16(b23, k2217_5352);403const __m128i d1 = _mm_add_epi32(c1, k12000_plus_one);404const __m128i d3 = _mm_add_epi32(c3, k51000);405const __m128i e1 = _mm_srai_epi32(d1, 16);406const __m128i e3 = _mm_srai_epi32(d3, 16);407// f1 = ((b3 * 5352 + b2 * 2217 + 12000) >> 16)408// f3 = ((b3 * 2217 - b2 * 5352 + 51000) >> 16)409const __m128i f1 = _mm_packs_epi32(e1, e1);410const __m128i f3 = _mm_packs_epi32(e3, e3);411// g1 = f1 + (a3 != 0);412// The compare will return (0xffff, 0) for (==0, !=0). To turn that into the413// desired (0, 1), we add one earlier through k12000_plus_one.414// -> g1 = f1 + 1 - (a3 == 0)415const __m128i g1 = _mm_add_epi16(f1, _mm_cmpeq_epi16(a32, zero));416417// a0 = v0 + v3418// a1 = v1 + v2419const __m128i a01 = _mm_add_epi16(*v01, *v32);420const __m128i a01_plus_7 = _mm_add_epi16(a01, seven);421const __m128i a11 = _mm_unpackhi_epi64(a01, a01);422const __m128i c0 = _mm_add_epi16(a01_plus_7, a11);423const __m128i c2 = _mm_sub_epi16(a01_plus_7, a11);424// d0 = (a0 + a1 + 7) >> 4;425// d2 = (a0 - a1 + 7) >> 4;426const __m128i d0 = _mm_srai_epi16(c0, 4);427const __m128i d2 = _mm_srai_epi16(c2, 4);428429const __m128i d0_g1 = _mm_unpacklo_epi64(d0, g1);430const __m128i d2_f3 = _mm_unpacklo_epi64(d2, f3);431_mm_storeu_si128((__m128i*)&out[0], d0_g1);432_mm_storeu_si128((__m128i*)&out[8], d2_f3);433}434435static void FTransform_SSE2(const uint8_t* WEBP_RESTRICT src,436const uint8_t* WEBP_RESTRICT ref,437int16_t* WEBP_RESTRICT out) {438const __m128i zero = _mm_setzero_si128();439// Load src.440const __m128i src0 = _mm_loadl_epi64((const __m128i*)&src[0 * BPS]);441const __m128i src1 = _mm_loadl_epi64((const __m128i*)&src[1 * BPS]);442const __m128i src2 = _mm_loadl_epi64((const __m128i*)&src[2 * BPS]);443const __m128i src3 = _mm_loadl_epi64((const __m128i*)&src[3 * BPS]);444// 00 01 02 03 *445// 10 11 12 13 *446// 20 21 22 23 *447// 30 31 32 33 *448// Shuffle.449const __m128i src_0 = _mm_unpacklo_epi16(src0, src1);450const __m128i src_1 = _mm_unpacklo_epi16(src2, src3);451// 00 01 10 11 02 03 12 13 * * ...452// 20 21 30 31 22 22 32 33 * * ...453454// Load ref.455const __m128i ref0 = _mm_loadl_epi64((const __m128i*)&ref[0 * BPS]);456const __m128i ref1 = _mm_loadl_epi64((const __m128i*)&ref[1 * BPS]);457const __m128i ref2 = _mm_loadl_epi64((const __m128i*)&ref[2 * BPS]);458const __m128i ref3 = _mm_loadl_epi64((const __m128i*)&ref[3 * BPS]);459const __m128i ref_0 = _mm_unpacklo_epi16(ref0, ref1);460const __m128i ref_1 = _mm_unpacklo_epi16(ref2, ref3);461462// Convert both to 16 bit.463const __m128i src_0_16b = _mm_unpacklo_epi8(src_0, zero);464const __m128i src_1_16b = _mm_unpacklo_epi8(src_1, zero);465const __m128i ref_0_16b = _mm_unpacklo_epi8(ref_0, zero);466const __m128i ref_1_16b = _mm_unpacklo_epi8(ref_1, zero);467468// Compute the difference.469const __m128i row01 = _mm_sub_epi16(src_0_16b, ref_0_16b);470const __m128i row23 = _mm_sub_epi16(src_1_16b, ref_1_16b);471__m128i v01, v32;472473// First pass474FTransformPass1_SSE2(&row01, &row23, &v01, &v32);475476// Second pass477FTransformPass2_SSE2(&v01, &v32, out);478}479480static void FTransform2_SSE2(const uint8_t* WEBP_RESTRICT src,481const uint8_t* WEBP_RESTRICT ref,482int16_t* WEBP_RESTRICT out) {483const __m128i zero = _mm_setzero_si128();484485// Load src and convert to 16b.486const __m128i src0 = _mm_loadl_epi64((const __m128i*)&src[0 * BPS]);487const __m128i src1 = _mm_loadl_epi64((const __m128i*)&src[1 * BPS]);488const __m128i src2 = _mm_loadl_epi64((const __m128i*)&src[2 * BPS]);489const __m128i src3 = _mm_loadl_epi64((const __m128i*)&src[3 * BPS]);490const __m128i src_0 = _mm_unpacklo_epi8(src0, zero);491const __m128i src_1 = _mm_unpacklo_epi8(src1, zero);492const __m128i src_2 = _mm_unpacklo_epi8(src2, zero);493const __m128i src_3 = _mm_unpacklo_epi8(src3, zero);494// Load ref and convert to 16b.495const __m128i ref0 = _mm_loadl_epi64((const __m128i*)&ref[0 * BPS]);496const __m128i ref1 = _mm_loadl_epi64((const __m128i*)&ref[1 * BPS]);497const __m128i ref2 = _mm_loadl_epi64((const __m128i*)&ref[2 * BPS]);498const __m128i ref3 = _mm_loadl_epi64((const __m128i*)&ref[3 * BPS]);499const __m128i ref_0 = _mm_unpacklo_epi8(ref0, zero);500const __m128i ref_1 = _mm_unpacklo_epi8(ref1, zero);501const __m128i ref_2 = _mm_unpacklo_epi8(ref2, zero);502const __m128i ref_3 = _mm_unpacklo_epi8(ref3, zero);503// Compute difference. -> 00 01 02 03 00' 01' 02' 03'504const __m128i diff0 = _mm_sub_epi16(src_0, ref_0);505const __m128i diff1 = _mm_sub_epi16(src_1, ref_1);506const __m128i diff2 = _mm_sub_epi16(src_2, ref_2);507const __m128i diff3 = _mm_sub_epi16(src_3, ref_3);508509// Unpack and shuffle510// 00 01 02 03 0 0 0 0511// 10 11 12 13 0 0 0 0512// 20 21 22 23 0 0 0 0513// 30 31 32 33 0 0 0 0514const __m128i shuf01l = _mm_unpacklo_epi32(diff0, diff1);515const __m128i shuf23l = _mm_unpacklo_epi32(diff2, diff3);516const __m128i shuf01h = _mm_unpackhi_epi32(diff0, diff1);517const __m128i shuf23h = _mm_unpackhi_epi32(diff2, diff3);518__m128i v01l, v32l;519__m128i v01h, v32h;520521// First pass522FTransformPass1_SSE2(&shuf01l, &shuf23l, &v01l, &v32l);523FTransformPass1_SSE2(&shuf01h, &shuf23h, &v01h, &v32h);524525// Second pass526FTransformPass2_SSE2(&v01l, &v32l, out + 0);527FTransformPass2_SSE2(&v01h, &v32h, out + 16);528}529530static void FTransformWHTRow_SSE2(const int16_t* WEBP_RESTRICT const in,531__m128i* const out) {532const __m128i kMult = _mm_set_epi16(-1, 1, -1, 1, 1, 1, 1, 1);533const __m128i src0 = _mm_loadl_epi64((__m128i*)&in[0 * 16]);534const __m128i src1 = _mm_loadl_epi64((__m128i*)&in[1 * 16]);535const __m128i src2 = _mm_loadl_epi64((__m128i*)&in[2 * 16]);536const __m128i src3 = _mm_loadl_epi64((__m128i*)&in[3 * 16]);537const __m128i A01 = _mm_unpacklo_epi16(src0, src1); // A0 A1 | ...538const __m128i A23 = _mm_unpacklo_epi16(src2, src3); // A2 A3 | ...539const __m128i B0 = _mm_adds_epi16(A01, A23); // a0 | a1 | ...540const __m128i B1 = _mm_subs_epi16(A01, A23); // a3 | a2 | ...541const __m128i C0 = _mm_unpacklo_epi32(B0, B1); // a0 | a1 | a3 | a2 | ...542const __m128i C1 = _mm_unpacklo_epi32(B1, B0); // a3 | a2 | a0 | a1 | ...543const __m128i D = _mm_unpacklo_epi64(C0, C1); // a0 a1 a3 a2 a3 a2 a0 a1544*out = _mm_madd_epi16(D, kMult);545}546547static void FTransformWHT_SSE2(const int16_t* WEBP_RESTRICT in,548int16_t* WEBP_RESTRICT out) {549// Input is 12b signed.550__m128i row0, row1, row2, row3;551// Rows are 14b signed.552FTransformWHTRow_SSE2(in + 0 * 64, &row0);553FTransformWHTRow_SSE2(in + 1 * 64, &row1);554FTransformWHTRow_SSE2(in + 2 * 64, &row2);555FTransformWHTRow_SSE2(in + 3 * 64, &row3);556557{558// The a* are 15b signed.559const __m128i a0 = _mm_add_epi32(row0, row2);560const __m128i a1 = _mm_add_epi32(row1, row3);561const __m128i a2 = _mm_sub_epi32(row1, row3);562const __m128i a3 = _mm_sub_epi32(row0, row2);563const __m128i a0a3 = _mm_packs_epi32(a0, a3);564const __m128i a1a2 = _mm_packs_epi32(a1, a2);565566// The b* are 16b signed.567const __m128i b0b1 = _mm_add_epi16(a0a3, a1a2);568const __m128i b3b2 = _mm_sub_epi16(a0a3, a1a2);569const __m128i tmp_b2b3 = _mm_unpackhi_epi64(b3b2, b3b2);570const __m128i b2b3 = _mm_unpacklo_epi64(tmp_b2b3, b3b2);571572_mm_storeu_si128((__m128i*)&out[0], _mm_srai_epi16(b0b1, 1));573_mm_storeu_si128((__m128i*)&out[8], _mm_srai_epi16(b2b3, 1));574}575}576577//------------------------------------------------------------------------------578// Compute susceptibility based on DCT-coeff histograms:579// the higher, the "easier" the macroblock is to compress.580581static void CollectHistogram_SSE2(const uint8_t* WEBP_RESTRICT ref,582const uint8_t* WEBP_RESTRICT pred,583int start_block, int end_block,584VP8Histogram* WEBP_RESTRICT const histo) {585const __m128i zero = _mm_setzero_si128();586const __m128i max_coeff_thresh = _mm_set1_epi16(MAX_COEFF_THRESH);587int j;588int distribution[MAX_COEFF_THRESH + 1] = { 0 };589for (j = start_block; j < end_block; ++j) {590int16_t out[16];591int k;592593FTransform_SSE2(ref + VP8DspScan[j], pred + VP8DspScan[j], out);594595// Convert coefficients to bin (within out[]).596{597// Load.598const __m128i out0 = _mm_loadu_si128((__m128i*)&out[0]);599const __m128i out1 = _mm_loadu_si128((__m128i*)&out[8]);600const __m128i d0 = _mm_sub_epi16(zero, out0);601const __m128i d1 = _mm_sub_epi16(zero, out1);602const __m128i abs0 = _mm_max_epi16(out0, d0); // abs(v), 16b603const __m128i abs1 = _mm_max_epi16(out1, d1);604// v = abs(out) >> 3605const __m128i v0 = _mm_srai_epi16(abs0, 3);606const __m128i v1 = _mm_srai_epi16(abs1, 3);607// bin = min(v, MAX_COEFF_THRESH)608const __m128i bin0 = _mm_min_epi16(v0, max_coeff_thresh);609const __m128i bin1 = _mm_min_epi16(v1, max_coeff_thresh);610// Store.611_mm_storeu_si128((__m128i*)&out[0], bin0);612_mm_storeu_si128((__m128i*)&out[8], bin1);613}614615// Convert coefficients to bin.616for (k = 0; k < 16; ++k) {617++distribution[out[k]];618}619}620VP8SetHistogramData(distribution, histo);621}622623//------------------------------------------------------------------------------624// Intra predictions625626// helper for chroma-DC predictions627static WEBP_INLINE void Put8x8uv_SSE2(uint8_t v, uint8_t* dst) {628int j;629const __m128i values = _mm_set1_epi8((char)v);630for (j = 0; j < 8; ++j) {631_mm_storel_epi64((__m128i*)(dst + j * BPS), values);632}633}634635static WEBP_INLINE void Put16_SSE2(uint8_t v, uint8_t* dst) {636int j;637const __m128i values = _mm_set1_epi8((char)v);638for (j = 0; j < 16; ++j) {639_mm_store_si128((__m128i*)(dst + j * BPS), values);640}641}642643static WEBP_INLINE void Fill_SSE2(uint8_t* dst, int value, int size) {644if (size == 4) {645int j;646for (j = 0; j < 4; ++j) {647memset(dst + j * BPS, value, 4);648}649} else if (size == 8) {650Put8x8uv_SSE2(value, dst);651} else {652Put16_SSE2(value, dst);653}654}655656static WEBP_INLINE void VE8uv_SSE2(uint8_t* WEBP_RESTRICT dst,657const uint8_t* WEBP_RESTRICT top) {658int j;659const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);660for (j = 0; j < 8; ++j) {661_mm_storel_epi64((__m128i*)(dst + j * BPS), top_values);662}663}664665static WEBP_INLINE void VE16_SSE2(uint8_t* WEBP_RESTRICT dst,666const uint8_t* WEBP_RESTRICT top) {667const __m128i top_values = _mm_load_si128((const __m128i*)top);668int j;669for (j = 0; j < 16; ++j) {670_mm_store_si128((__m128i*)(dst + j * BPS), top_values);671}672}673674static WEBP_INLINE void VerticalPred_SSE2(uint8_t* WEBP_RESTRICT dst,675const uint8_t* WEBP_RESTRICT top,676int size) {677if (top != NULL) {678if (size == 8) {679VE8uv_SSE2(dst, top);680} else {681VE16_SSE2(dst, top);682}683} else {684Fill_SSE2(dst, 127, size);685}686}687688static WEBP_INLINE void HE8uv_SSE2(uint8_t* WEBP_RESTRICT dst,689const uint8_t* WEBP_RESTRICT left) {690int j;691for (j = 0; j < 8; ++j) {692const __m128i values = _mm_set1_epi8((char)left[j]);693_mm_storel_epi64((__m128i*)dst, values);694dst += BPS;695}696}697698static WEBP_INLINE void HE16_SSE2(uint8_t* WEBP_RESTRICT dst,699const uint8_t* WEBP_RESTRICT left) {700int j;701for (j = 0; j < 16; ++j) {702const __m128i values = _mm_set1_epi8((char)left[j]);703_mm_store_si128((__m128i*)dst, values);704dst += BPS;705}706}707708static WEBP_INLINE void HorizontalPred_SSE2(uint8_t* WEBP_RESTRICT dst,709const uint8_t* WEBP_RESTRICT left,710int size) {711if (left != NULL) {712if (size == 8) {713HE8uv_SSE2(dst, left);714} else {715HE16_SSE2(dst, left);716}717} else {718Fill_SSE2(dst, 129, size);719}720}721722static WEBP_INLINE void TM_SSE2(uint8_t* WEBP_RESTRICT dst,723const uint8_t* WEBP_RESTRICT left,724const uint8_t* WEBP_RESTRICT top, int size) {725const __m128i zero = _mm_setzero_si128();726int y;727if (size == 8) {728const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);729const __m128i top_base = _mm_unpacklo_epi8(top_values, zero);730for (y = 0; y < 8; ++y, dst += BPS) {731const int val = left[y] - left[-1];732const __m128i base = _mm_set1_epi16(val);733const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero);734_mm_storel_epi64((__m128i*)dst, out);735}736} else {737const __m128i top_values = _mm_load_si128((const __m128i*)top);738const __m128i top_base_0 = _mm_unpacklo_epi8(top_values, zero);739const __m128i top_base_1 = _mm_unpackhi_epi8(top_values, zero);740for (y = 0; y < 16; ++y, dst += BPS) {741const int val = left[y] - left[-1];742const __m128i base = _mm_set1_epi16(val);743const __m128i out_0 = _mm_add_epi16(base, top_base_0);744const __m128i out_1 = _mm_add_epi16(base, top_base_1);745const __m128i out = _mm_packus_epi16(out_0, out_1);746_mm_store_si128((__m128i*)dst, out);747}748}749}750751static WEBP_INLINE void TrueMotion_SSE2(uint8_t* WEBP_RESTRICT dst,752const uint8_t* WEBP_RESTRICT left,753const uint8_t* WEBP_RESTRICT top,754int size) {755if (left != NULL) {756if (top != NULL) {757TM_SSE2(dst, left, top, size);758} else {759HorizontalPred_SSE2(dst, left, size);760}761} else {762// true motion without left samples (hence: with default 129 value)763// is equivalent to VE prediction where you just copy the top samples.764// Note that if top samples are not available, the default value is765// then 129, and not 127 as in the VerticalPred case.766if (top != NULL) {767VerticalPred_SSE2(dst, top, size);768} else {769Fill_SSE2(dst, 129, size);770}771}772}773774static WEBP_INLINE void DC8uv_SSE2(uint8_t* WEBP_RESTRICT dst,775const uint8_t* WEBP_RESTRICT left,776const uint8_t* WEBP_RESTRICT top) {777const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);778const __m128i left_values = _mm_loadl_epi64((const __m128i*)left);779const __m128i combined = _mm_unpacklo_epi64(top_values, left_values);780const int DC = VP8HorizontalAdd8b(&combined) + 8;781Put8x8uv_SSE2(DC >> 4, dst);782}783784static WEBP_INLINE void DC8uvNoLeft_SSE2(uint8_t* WEBP_RESTRICT dst,785const uint8_t* WEBP_RESTRICT top) {786const __m128i zero = _mm_setzero_si128();787const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);788const __m128i sum = _mm_sad_epu8(top_values, zero);789const int DC = _mm_cvtsi128_si32(sum) + 4;790Put8x8uv_SSE2(DC >> 3, dst);791}792793static WEBP_INLINE void DC8uvNoTop_SSE2(uint8_t* WEBP_RESTRICT dst,794const uint8_t* WEBP_RESTRICT left) {795// 'left' is contiguous so we can reuse the top summation.796DC8uvNoLeft_SSE2(dst, left);797}798799static WEBP_INLINE void DC8uvNoTopLeft_SSE2(uint8_t* dst) {800Put8x8uv_SSE2(0x80, dst);801}802803static WEBP_INLINE void DC8uvMode_SSE2(uint8_t* WEBP_RESTRICT dst,804const uint8_t* WEBP_RESTRICT left,805const uint8_t* WEBP_RESTRICT top) {806if (top != NULL) {807if (left != NULL) { // top and left present808DC8uv_SSE2(dst, left, top);809} else { // top, but no left810DC8uvNoLeft_SSE2(dst, top);811}812} else if (left != NULL) { // left but no top813DC8uvNoTop_SSE2(dst, left);814} else { // no top, no left, nothing.815DC8uvNoTopLeft_SSE2(dst);816}817}818819static WEBP_INLINE void DC16_SSE2(uint8_t* WEBP_RESTRICT dst,820const uint8_t* WEBP_RESTRICT left,821const uint8_t* WEBP_RESTRICT top) {822const __m128i top_row = _mm_load_si128((const __m128i*)top);823const __m128i left_row = _mm_load_si128((const __m128i*)left);824const int DC =825VP8HorizontalAdd8b(&top_row) + VP8HorizontalAdd8b(&left_row) + 16;826Put16_SSE2(DC >> 5, dst);827}828829static WEBP_INLINE void DC16NoLeft_SSE2(uint8_t* WEBP_RESTRICT dst,830const uint8_t* WEBP_RESTRICT top) {831const __m128i top_row = _mm_load_si128((const __m128i*)top);832const int DC = VP8HorizontalAdd8b(&top_row) + 8;833Put16_SSE2(DC >> 4, dst);834}835836static WEBP_INLINE void DC16NoTop_SSE2(uint8_t* WEBP_RESTRICT dst,837const uint8_t* WEBP_RESTRICT left) {838// 'left' is contiguous so we can reuse the top summation.839DC16NoLeft_SSE2(dst, left);840}841842static WEBP_INLINE void DC16NoTopLeft_SSE2(uint8_t* dst) {843Put16_SSE2(0x80, dst);844}845846static WEBP_INLINE void DC16Mode_SSE2(uint8_t* WEBP_RESTRICT dst,847const uint8_t* WEBP_RESTRICT left,848const uint8_t* WEBP_RESTRICT top) {849if (top != NULL) {850if (left != NULL) { // top and left present851DC16_SSE2(dst, left, top);852} else { // top, but no left853DC16NoLeft_SSE2(dst, top);854}855} else if (left != NULL) { // left but no top856DC16NoTop_SSE2(dst, left);857} else { // no top, no left, nothing.858DC16NoTopLeft_SSE2(dst);859}860}861862//------------------------------------------------------------------------------863// 4x4 predictions864865#define DST(x, y) dst[(x) + (y) * BPS]866#define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)867#define AVG2(a, b) (((a) + (b) + 1) >> 1)868869// We use the following 8b-arithmetic tricks:870// (a + 2 * b + c + 2) >> 2 = (AC + b + 1) >> 1871// where: AC = (a + c) >> 1 = [(a + c + 1) >> 1] - [(a^c) & 1]872// and:873// (a + 2 * b + c + 2) >> 2 = (AB + BC + 1) >> 1 - (ab|bc)&lsb874// where: AC = (a + b + 1) >> 1, BC = (b + c + 1) >> 1875// and ab = a ^ b, bc = b ^ c, lsb = (AC^BC)&1876877// vertical878static WEBP_INLINE void VE4_SSE2(uint8_t* WEBP_RESTRICT dst,879const uint8_t* WEBP_RESTRICT top) {880const __m128i one = _mm_set1_epi8(1);881const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(top - 1));882const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1);883const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2);884const __m128i a = _mm_avg_epu8(ABCDEFGH, CDEFGH00);885const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGH00), one);886const __m128i b = _mm_subs_epu8(a, lsb);887const __m128i avg = _mm_avg_epu8(b, BCDEFGH0);888const int vals = _mm_cvtsi128_si32(avg);889int i;890for (i = 0; i < 4; ++i) {891WebPInt32ToMem(dst + i * BPS, vals);892}893}894895// horizontal896static WEBP_INLINE void HE4_SSE2(uint8_t* WEBP_RESTRICT dst,897const uint8_t* WEBP_RESTRICT top) {898const int X = top[-1];899const int I = top[-2];900const int J = top[-3];901const int K = top[-4];902const int L = top[-5];903WebPUint32ToMem(dst + 0 * BPS, 0x01010101U * AVG3(X, I, J));904WebPUint32ToMem(dst + 1 * BPS, 0x01010101U * AVG3(I, J, K));905WebPUint32ToMem(dst + 2 * BPS, 0x01010101U * AVG3(J, K, L));906WebPUint32ToMem(dst + 3 * BPS, 0x01010101U * AVG3(K, L, L));907}908909static WEBP_INLINE void DC4_SSE2(uint8_t* WEBP_RESTRICT dst,910const uint8_t* WEBP_RESTRICT top) {911uint32_t dc = 4;912int i;913for (i = 0; i < 4; ++i) dc += top[i] + top[-5 + i];914Fill_SSE2(dst, dc >> 3, 4);915}916917// Down-Left918static WEBP_INLINE void LD4_SSE2(uint8_t* WEBP_RESTRICT dst,919const uint8_t* WEBP_RESTRICT top) {920const __m128i one = _mm_set1_epi8(1);921const __m128i ABCDEFGH = _mm_loadl_epi64((const __m128i*)top);922const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1);923const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2);924const __m128i CDEFGHH0 = _mm_insert_epi16(CDEFGH00, top[7], 3);925const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, CDEFGHH0);926const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGHH0), one);927const __m128i avg2 = _mm_subs_epu8(avg1, lsb);928const __m128i abcdefg = _mm_avg_epu8(avg2, BCDEFGH0);929WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( abcdefg ));930WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1)));931WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2)));932WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3)));933}934935// Vertical-Right936static WEBP_INLINE void VR4_SSE2(uint8_t* WEBP_RESTRICT dst,937const uint8_t* WEBP_RESTRICT top) {938const __m128i one = _mm_set1_epi8(1);939const int I = top[-2];940const int J = top[-3];941const int K = top[-4];942const int X = top[-1];943const __m128i XABCD = _mm_loadl_epi64((const __m128i*)(top - 1));944const __m128i ABCD0 = _mm_srli_si128(XABCD, 1);945const __m128i abcd = _mm_avg_epu8(XABCD, ABCD0);946const __m128i _XABCD = _mm_slli_si128(XABCD, 1);947const __m128i IXABCD = _mm_insert_epi16(_XABCD, (short)(I | (X << 8)), 0);948const __m128i avg1 = _mm_avg_epu8(IXABCD, ABCD0);949const __m128i lsb = _mm_and_si128(_mm_xor_si128(IXABCD, ABCD0), one);950const __m128i avg2 = _mm_subs_epu8(avg1, lsb);951const __m128i efgh = _mm_avg_epu8(avg2, XABCD);952WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( abcd ));953WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32( efgh ));954WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(abcd, 1)));955WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(efgh, 1)));956957// these two are hard to implement in SSE2, so we keep the C-version:958DST(0, 2) = AVG3(J, I, X);959DST(0, 3) = AVG3(K, J, I);960}961962// Vertical-Left963static WEBP_INLINE void VL4_SSE2(uint8_t* WEBP_RESTRICT dst,964const uint8_t* WEBP_RESTRICT top) {965const __m128i one = _mm_set1_epi8(1);966const __m128i ABCDEFGH = _mm_loadl_epi64((const __m128i*)top);967const __m128i BCDEFGH_ = _mm_srli_si128(ABCDEFGH, 1);968const __m128i CDEFGH__ = _mm_srli_si128(ABCDEFGH, 2);969const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, BCDEFGH_);970const __m128i avg2 = _mm_avg_epu8(CDEFGH__, BCDEFGH_);971const __m128i avg3 = _mm_avg_epu8(avg1, avg2);972const __m128i lsb1 = _mm_and_si128(_mm_xor_si128(avg1, avg2), one);973const __m128i ab = _mm_xor_si128(ABCDEFGH, BCDEFGH_);974const __m128i bc = _mm_xor_si128(CDEFGH__, BCDEFGH_);975const __m128i abbc = _mm_or_si128(ab, bc);976const __m128i lsb2 = _mm_and_si128(abbc, lsb1);977const __m128i avg4 = _mm_subs_epu8(avg3, lsb2);978const uint32_t extra_out =979(uint32_t)_mm_cvtsi128_si32(_mm_srli_si128(avg4, 4));980WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( avg1 ));981WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32( avg4 ));982WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg1, 1)));983WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg4, 1)));984985// these two are hard to get and irregular986DST(3, 2) = (extra_out >> 0) & 0xff;987DST(3, 3) = (extra_out >> 8) & 0xff;988}989990// Down-right991static WEBP_INLINE void RD4_SSE2(uint8_t* WEBP_RESTRICT dst,992const uint8_t* WEBP_RESTRICT top) {993const __m128i one = _mm_set1_epi8(1);994const __m128i LKJIXABC = _mm_loadl_epi64((const __m128i*)(top - 5));995const __m128i LKJIXABCD = _mm_insert_epi16(LKJIXABC, top[3], 4);996const __m128i KJIXABCD_ = _mm_srli_si128(LKJIXABCD, 1);997const __m128i JIXABCD__ = _mm_srli_si128(LKJIXABCD, 2);998const __m128i avg1 = _mm_avg_epu8(JIXABCD__, LKJIXABCD);999const __m128i lsb = _mm_and_si128(_mm_xor_si128(JIXABCD__, LKJIXABCD), one);1000const __m128i avg2 = _mm_subs_epu8(avg1, lsb);1001const __m128i abcdefg = _mm_avg_epu8(avg2, KJIXABCD_);1002WebPInt32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32( abcdefg ));1003WebPInt32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1)));1004WebPInt32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2)));1005WebPInt32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3)));1006}10071008static WEBP_INLINE void HU4_SSE2(uint8_t* WEBP_RESTRICT dst,1009const uint8_t* WEBP_RESTRICT top) {1010const int I = top[-2];1011const int J = top[-3];1012const int K = top[-4];1013const int L = top[-5];1014DST(0, 0) = AVG2(I, J);1015DST(2, 0) = DST(0, 1) = AVG2(J, K);1016DST(2, 1) = DST(0, 2) = AVG2(K, L);1017DST(1, 0) = AVG3(I, J, K);1018DST(3, 0) = DST(1, 1) = AVG3(J, K, L);1019DST(3, 1) = DST(1, 2) = AVG3(K, L, L);1020DST(3, 2) = DST(2, 2) =1021DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;1022}10231024static WEBP_INLINE void HD4_SSE2(uint8_t* WEBP_RESTRICT dst,1025const uint8_t* WEBP_RESTRICT top) {1026const int X = top[-1];1027const int I = top[-2];1028const int J = top[-3];1029const int K = top[-4];1030const int L = top[-5];1031const int A = top[0];1032const int B = top[1];1033const int C = top[2];10341035DST(0, 0) = DST(2, 1) = AVG2(I, X);1036DST(0, 1) = DST(2, 2) = AVG2(J, I);1037DST(0, 2) = DST(2, 3) = AVG2(K, J);1038DST(0, 3) = AVG2(L, K);10391040DST(3, 0) = AVG3(A, B, C);1041DST(2, 0) = AVG3(X, A, B);1042DST(1, 0) = DST(3, 1) = AVG3(I, X, A);1043DST(1, 1) = DST(3, 2) = AVG3(J, I, X);1044DST(1, 2) = DST(3, 3) = AVG3(K, J, I);1045DST(1, 3) = AVG3(L, K, J);1046}10471048static WEBP_INLINE void TM4_SSE2(uint8_t* WEBP_RESTRICT dst,1049const uint8_t* WEBP_RESTRICT top) {1050const __m128i zero = _mm_setzero_si128();1051const __m128i top_values = _mm_cvtsi32_si128(WebPMemToInt32(top));1052const __m128i top_base = _mm_unpacklo_epi8(top_values, zero);1053int y;1054for (y = 0; y < 4; ++y, dst += BPS) {1055const int val = top[-2 - y] - top[-1];1056const __m128i base = _mm_set1_epi16(val);1057const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero);1058WebPInt32ToMem(dst, _mm_cvtsi128_si32(out));1059}1060}10611062#undef DST1063#undef AVG31064#undef AVG210651066//------------------------------------------------------------------------------1067// luma 4x4 prediction10681069// Left samples are top[-5 .. -2], top_left is top[-1], top are1070// located at top[0..3], and top right is top[4..7]1071static void Intra4Preds_SSE2(uint8_t* WEBP_RESTRICT dst,1072const uint8_t* WEBP_RESTRICT top) {1073DC4_SSE2(I4DC4 + dst, top);1074TM4_SSE2(I4TM4 + dst, top);1075VE4_SSE2(I4VE4 + dst, top);1076HE4_SSE2(I4HE4 + dst, top);1077RD4_SSE2(I4RD4 + dst, top);1078VR4_SSE2(I4VR4 + dst, top);1079LD4_SSE2(I4LD4 + dst, top);1080VL4_SSE2(I4VL4 + dst, top);1081HD4_SSE2(I4HD4 + dst, top);1082HU4_SSE2(I4HU4 + dst, top);1083}10841085//------------------------------------------------------------------------------1086// Chroma 8x8 prediction (paragraph 12.2)10871088static void IntraChromaPreds_SSE2(uint8_t* WEBP_RESTRICT dst,1089const uint8_t* WEBP_RESTRICT left,1090const uint8_t* WEBP_RESTRICT top) {1091// U block1092DC8uvMode_SSE2(C8DC8 + dst, left, top);1093VerticalPred_SSE2(C8VE8 + dst, top, 8);1094HorizontalPred_SSE2(C8HE8 + dst, left, 8);1095TrueMotion_SSE2(C8TM8 + dst, left, top, 8);1096// V block1097dst += 8;1098if (top != NULL) top += 8;1099if (left != NULL) left += 16;1100DC8uvMode_SSE2(C8DC8 + dst, left, top);1101VerticalPred_SSE2(C8VE8 + dst, top, 8);1102HorizontalPred_SSE2(C8HE8 + dst, left, 8);1103TrueMotion_SSE2(C8TM8 + dst, left, top, 8);1104}11051106//------------------------------------------------------------------------------1107// luma 16x16 prediction (paragraph 12.3)11081109static void Intra16Preds_SSE2(uint8_t* WEBP_RESTRICT dst,1110const uint8_t* WEBP_RESTRICT left,1111const uint8_t* WEBP_RESTRICT top) {1112DC16Mode_SSE2(I16DC16 + dst, left, top);1113VerticalPred_SSE2(I16VE16 + dst, top, 16);1114HorizontalPred_SSE2(I16HE16 + dst, left, 16);1115TrueMotion_SSE2(I16TM16 + dst, left, top, 16);1116}11171118//------------------------------------------------------------------------------1119// Metric11201121static WEBP_INLINE void SubtractAndAccumulate_SSE2(const __m128i a,1122const __m128i b,1123__m128i* const sum) {1124// take abs(a-b) in 8b1125const __m128i a_b = _mm_subs_epu8(a, b);1126const __m128i b_a = _mm_subs_epu8(b, a);1127const __m128i abs_a_b = _mm_or_si128(a_b, b_a);1128// zero-extend to 16b1129const __m128i zero = _mm_setzero_si128();1130const __m128i C0 = _mm_unpacklo_epi8(abs_a_b, zero);1131const __m128i C1 = _mm_unpackhi_epi8(abs_a_b, zero);1132// multiply with self1133const __m128i sum1 = _mm_madd_epi16(C0, C0);1134const __m128i sum2 = _mm_madd_epi16(C1, C1);1135*sum = _mm_add_epi32(sum1, sum2);1136}11371138static WEBP_INLINE int SSE_16xN_SSE2(const uint8_t* WEBP_RESTRICT a,1139const uint8_t* WEBP_RESTRICT b,1140int num_pairs) {1141__m128i sum = _mm_setzero_si128();1142int32_t tmp[4];1143int i;11441145for (i = 0; i < num_pairs; ++i) {1146const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[BPS * 0]);1147const __m128i b0 = _mm_loadu_si128((const __m128i*)&b[BPS * 0]);1148const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[BPS * 1]);1149const __m128i b1 = _mm_loadu_si128((const __m128i*)&b[BPS * 1]);1150__m128i sum1, sum2;1151SubtractAndAccumulate_SSE2(a0, b0, &sum1);1152SubtractAndAccumulate_SSE2(a1, b1, &sum2);1153sum = _mm_add_epi32(sum, _mm_add_epi32(sum1, sum2));1154a += 2 * BPS;1155b += 2 * BPS;1156}1157_mm_storeu_si128((__m128i*)tmp, sum);1158return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);1159}11601161static int SSE16x16_SSE2(const uint8_t* WEBP_RESTRICT a,1162const uint8_t* WEBP_RESTRICT b) {1163return SSE_16xN_SSE2(a, b, 8);1164}11651166static int SSE16x8_SSE2(const uint8_t* WEBP_RESTRICT a,1167const uint8_t* WEBP_RESTRICT b) {1168return SSE_16xN_SSE2(a, b, 4);1169}11701171#define LOAD_8x16b(ptr) \1172_mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(ptr)), zero)11731174static int SSE8x8_SSE2(const uint8_t* WEBP_RESTRICT a,1175const uint8_t* WEBP_RESTRICT b) {1176const __m128i zero = _mm_setzero_si128();1177int num_pairs = 4;1178__m128i sum = zero;1179int32_t tmp[4];1180while (num_pairs-- > 0) {1181const __m128i a0 = LOAD_8x16b(&a[BPS * 0]);1182const __m128i a1 = LOAD_8x16b(&a[BPS * 1]);1183const __m128i b0 = LOAD_8x16b(&b[BPS * 0]);1184const __m128i b1 = LOAD_8x16b(&b[BPS * 1]);1185// subtract1186const __m128i c0 = _mm_subs_epi16(a0, b0);1187const __m128i c1 = _mm_subs_epi16(a1, b1);1188// multiply/accumulate with self1189const __m128i d0 = _mm_madd_epi16(c0, c0);1190const __m128i d1 = _mm_madd_epi16(c1, c1);1191// collect1192const __m128i sum01 = _mm_add_epi32(d0, d1);1193sum = _mm_add_epi32(sum, sum01);1194a += 2 * BPS;1195b += 2 * BPS;1196}1197_mm_storeu_si128((__m128i*)tmp, sum);1198return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);1199}1200#undef LOAD_8x16b12011202static int SSE4x4_SSE2(const uint8_t* WEBP_RESTRICT a,1203const uint8_t* WEBP_RESTRICT b) {1204const __m128i zero = _mm_setzero_si128();12051206// Load values. Note that we read 8 pixels instead of 4,1207// but the a/b buffers are over-allocated to that effect.1208const __m128i a0 = _mm_loadl_epi64((const __m128i*)&a[BPS * 0]);1209const __m128i a1 = _mm_loadl_epi64((const __m128i*)&a[BPS * 1]);1210const __m128i a2 = _mm_loadl_epi64((const __m128i*)&a[BPS * 2]);1211const __m128i a3 = _mm_loadl_epi64((const __m128i*)&a[BPS * 3]);1212const __m128i b0 = _mm_loadl_epi64((const __m128i*)&b[BPS * 0]);1213const __m128i b1 = _mm_loadl_epi64((const __m128i*)&b[BPS * 1]);1214const __m128i b2 = _mm_loadl_epi64((const __m128i*)&b[BPS * 2]);1215const __m128i b3 = _mm_loadl_epi64((const __m128i*)&b[BPS * 3]);1216// Combine pair of lines.1217const __m128i a01 = _mm_unpacklo_epi32(a0, a1);1218const __m128i a23 = _mm_unpacklo_epi32(a2, a3);1219const __m128i b01 = _mm_unpacklo_epi32(b0, b1);1220const __m128i b23 = _mm_unpacklo_epi32(b2, b3);1221// Convert to 16b.1222const __m128i a01s = _mm_unpacklo_epi8(a01, zero);1223const __m128i a23s = _mm_unpacklo_epi8(a23, zero);1224const __m128i b01s = _mm_unpacklo_epi8(b01, zero);1225const __m128i b23s = _mm_unpacklo_epi8(b23, zero);1226// subtract, square and accumulate1227const __m128i d0 = _mm_subs_epi16(a01s, b01s);1228const __m128i d1 = _mm_subs_epi16(a23s, b23s);1229const __m128i e0 = _mm_madd_epi16(d0, d0);1230const __m128i e1 = _mm_madd_epi16(d1, d1);1231const __m128i sum = _mm_add_epi32(e0, e1);12321233int32_t tmp[4];1234_mm_storeu_si128((__m128i*)tmp, sum);1235return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);1236}12371238//------------------------------------------------------------------------------12391240static void Mean16x4_SSE2(const uint8_t* WEBP_RESTRICT ref, uint32_t dc[4]) {1241const __m128i mask = _mm_set1_epi16(0x00ff);1242const __m128i a0 = _mm_loadu_si128((const __m128i*)&ref[BPS * 0]);1243const __m128i a1 = _mm_loadu_si128((const __m128i*)&ref[BPS * 1]);1244const __m128i a2 = _mm_loadu_si128((const __m128i*)&ref[BPS * 2]);1245const __m128i a3 = _mm_loadu_si128((const __m128i*)&ref[BPS * 3]);1246const __m128i b0 = _mm_srli_epi16(a0, 8); // hi byte1247const __m128i b1 = _mm_srli_epi16(a1, 8);1248const __m128i b2 = _mm_srli_epi16(a2, 8);1249const __m128i b3 = _mm_srli_epi16(a3, 8);1250const __m128i c0 = _mm_and_si128(a0, mask); // lo byte1251const __m128i c1 = _mm_and_si128(a1, mask);1252const __m128i c2 = _mm_and_si128(a2, mask);1253const __m128i c3 = _mm_and_si128(a3, mask);1254const __m128i d0 = _mm_add_epi32(b0, c0);1255const __m128i d1 = _mm_add_epi32(b1, c1);1256const __m128i d2 = _mm_add_epi32(b2, c2);1257const __m128i d3 = _mm_add_epi32(b3, c3);1258const __m128i e0 = _mm_add_epi32(d0, d1);1259const __m128i e1 = _mm_add_epi32(d2, d3);1260const __m128i f0 = _mm_add_epi32(e0, e1);1261uint16_t tmp[8];1262_mm_storeu_si128((__m128i*)tmp, f0);1263dc[0] = tmp[0] + tmp[1];1264dc[1] = tmp[2] + tmp[3];1265dc[2] = tmp[4] + tmp[5];1266dc[3] = tmp[6] + tmp[7];1267}12681269//------------------------------------------------------------------------------1270// Texture distortion1271//1272// We try to match the spectral content (weighted) between source and1273// reconstructed samples.12741275// Hadamard transform1276// Returns the weighted sum of the absolute value of transformed coefficients.1277// w[] contains a row-major 4 by 4 symmetric matrix.1278static int TTransform_SSE2(const uint8_t* WEBP_RESTRICT inA,1279const uint8_t* WEBP_RESTRICT inB,1280const uint16_t* WEBP_RESTRICT const w) {1281int32_t sum[4];1282__m128i tmp_0, tmp_1, tmp_2, tmp_3;1283const __m128i zero = _mm_setzero_si128();12841285// Load and combine inputs.1286{1287const __m128i inA_0 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 0]);1288const __m128i inA_1 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 1]);1289const __m128i inA_2 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 2]);1290const __m128i inA_3 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 3]);1291const __m128i inB_0 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 0]);1292const __m128i inB_1 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 1]);1293const __m128i inB_2 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 2]);1294const __m128i inB_3 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 3]);12951296// Combine inA and inB (we'll do two transforms in parallel).1297const __m128i inAB_0 = _mm_unpacklo_epi32(inA_0, inB_0);1298const __m128i inAB_1 = _mm_unpacklo_epi32(inA_1, inB_1);1299const __m128i inAB_2 = _mm_unpacklo_epi32(inA_2, inB_2);1300const __m128i inAB_3 = _mm_unpacklo_epi32(inA_3, inB_3);1301tmp_0 = _mm_unpacklo_epi8(inAB_0, zero);1302tmp_1 = _mm_unpacklo_epi8(inAB_1, zero);1303tmp_2 = _mm_unpacklo_epi8(inAB_2, zero);1304tmp_3 = _mm_unpacklo_epi8(inAB_3, zero);1305// a00 a01 a02 a03 b00 b01 b02 b031306// a10 a11 a12 a13 b10 b11 b12 b131307// a20 a21 a22 a23 b20 b21 b22 b231308// a30 a31 a32 a33 b30 b31 b32 b331309}13101311// Vertical pass first to avoid a transpose (vertical and horizontal passes1312// are commutative because w/kWeightY is symmetric) and subsequent transpose.1313{1314// Calculate a and b (two 4x4 at once).1315const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2);1316const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3);1317const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3);1318const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2);1319const __m128i b0 = _mm_add_epi16(a0, a1);1320const __m128i b1 = _mm_add_epi16(a3, a2);1321const __m128i b2 = _mm_sub_epi16(a3, a2);1322const __m128i b3 = _mm_sub_epi16(a0, a1);1323// a00 a01 a02 a03 b00 b01 b02 b031324// a10 a11 a12 a13 b10 b11 b12 b131325// a20 a21 a22 a23 b20 b21 b22 b231326// a30 a31 a32 a33 b30 b31 b32 b3313271328// Transpose the two 4x4.1329VP8Transpose_2_4x4_16b(&b0, &b1, &b2, &b3, &tmp_0, &tmp_1, &tmp_2, &tmp_3);1330}13311332// Horizontal pass and difference of weighted sums.1333{1334// Load all inputs.1335const __m128i w_0 = _mm_loadu_si128((const __m128i*)&w[0]);1336const __m128i w_8 = _mm_loadu_si128((const __m128i*)&w[8]);13371338// Calculate a and b (two 4x4 at once).1339const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2);1340const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3);1341const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3);1342const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2);1343const __m128i b0 = _mm_add_epi16(a0, a1);1344const __m128i b1 = _mm_add_epi16(a3, a2);1345const __m128i b2 = _mm_sub_epi16(a3, a2);1346const __m128i b3 = _mm_sub_epi16(a0, a1);13471348// Separate the transforms of inA and inB.1349__m128i A_b0 = _mm_unpacklo_epi64(b0, b1);1350__m128i A_b2 = _mm_unpacklo_epi64(b2, b3);1351__m128i B_b0 = _mm_unpackhi_epi64(b0, b1);1352__m128i B_b2 = _mm_unpackhi_epi64(b2, b3);13531354{1355const __m128i d0 = _mm_sub_epi16(zero, A_b0);1356const __m128i d1 = _mm_sub_epi16(zero, A_b2);1357const __m128i d2 = _mm_sub_epi16(zero, B_b0);1358const __m128i d3 = _mm_sub_epi16(zero, B_b2);1359A_b0 = _mm_max_epi16(A_b0, d0); // abs(v), 16b1360A_b2 = _mm_max_epi16(A_b2, d1);1361B_b0 = _mm_max_epi16(B_b0, d2);1362B_b2 = _mm_max_epi16(B_b2, d3);1363}13641365// weighted sums1366A_b0 = _mm_madd_epi16(A_b0, w_0);1367A_b2 = _mm_madd_epi16(A_b2, w_8);1368B_b0 = _mm_madd_epi16(B_b0, w_0);1369B_b2 = _mm_madd_epi16(B_b2, w_8);1370A_b0 = _mm_add_epi32(A_b0, A_b2);1371B_b0 = _mm_add_epi32(B_b0, B_b2);13721373// difference of weighted sums1374A_b0 = _mm_sub_epi32(A_b0, B_b0);1375_mm_storeu_si128((__m128i*)&sum[0], A_b0);1376}1377return sum[0] + sum[1] + sum[2] + sum[3];1378}13791380static int Disto4x4_SSE2(const uint8_t* WEBP_RESTRICT const a,1381const uint8_t* WEBP_RESTRICT const b,1382const uint16_t* WEBP_RESTRICT const w) {1383const int diff_sum = TTransform_SSE2(a, b, w);1384return abs(diff_sum) >> 5;1385}13861387static int Disto16x16_SSE2(const uint8_t* WEBP_RESTRICT const a,1388const uint8_t* WEBP_RESTRICT const b,1389const uint16_t* WEBP_RESTRICT const w) {1390int D = 0;1391int x, y;1392for (y = 0; y < 16 * BPS; y += 4 * BPS) {1393for (x = 0; x < 16; x += 4) {1394D += Disto4x4_SSE2(a + x + y, b + x + y, w);1395}1396}1397return D;1398}13991400//------------------------------------------------------------------------------1401// Quantization1402//14031404static WEBP_INLINE int DoQuantizeBlock_SSE2(1405int16_t in[16], int16_t out[16],1406const uint16_t* WEBP_RESTRICT const sharpen,1407const VP8Matrix* WEBP_RESTRICT const mtx) {1408const __m128i max_coeff_2047 = _mm_set1_epi16(MAX_LEVEL);1409const __m128i zero = _mm_setzero_si128();1410__m128i coeff0, coeff8;1411__m128i out0, out8;1412__m128i packed_out;14131414// Load all inputs.1415__m128i in0 = _mm_loadu_si128((__m128i*)&in[0]);1416__m128i in8 = _mm_loadu_si128((__m128i*)&in[8]);1417const __m128i iq0 = _mm_loadu_si128((const __m128i*)&mtx->iq[0]);1418const __m128i iq8 = _mm_loadu_si128((const __m128i*)&mtx->iq[8]);1419const __m128i q0 = _mm_loadu_si128((const __m128i*)&mtx->q[0]);1420const __m128i q8 = _mm_loadu_si128((const __m128i*)&mtx->q[8]);14211422// extract sign(in) (0x0000 if positive, 0xffff if negative)1423const __m128i sign0 = _mm_cmpgt_epi16(zero, in0);1424const __m128i sign8 = _mm_cmpgt_epi16(zero, in8);14251426// coeff = abs(in) = (in ^ sign) - sign1427coeff0 = _mm_xor_si128(in0, sign0);1428coeff8 = _mm_xor_si128(in8, sign8);1429coeff0 = _mm_sub_epi16(coeff0, sign0);1430coeff8 = _mm_sub_epi16(coeff8, sign8);14311432// coeff = abs(in) + sharpen1433if (sharpen != NULL) {1434const __m128i sharpen0 = _mm_loadu_si128((const __m128i*)&sharpen[0]);1435const __m128i sharpen8 = _mm_loadu_si128((const __m128i*)&sharpen[8]);1436coeff0 = _mm_add_epi16(coeff0, sharpen0);1437coeff8 = _mm_add_epi16(coeff8, sharpen8);1438}14391440// out = (coeff * iQ + B) >> QFIX1441{1442// doing calculations with 32b precision (QFIX=17)1443// out = (coeff * iQ)1444const __m128i coeff_iQ0H = _mm_mulhi_epu16(coeff0, iq0);1445const __m128i coeff_iQ0L = _mm_mullo_epi16(coeff0, iq0);1446const __m128i coeff_iQ8H = _mm_mulhi_epu16(coeff8, iq8);1447const __m128i coeff_iQ8L = _mm_mullo_epi16(coeff8, iq8);1448__m128i out_00 = _mm_unpacklo_epi16(coeff_iQ0L, coeff_iQ0H);1449__m128i out_04 = _mm_unpackhi_epi16(coeff_iQ0L, coeff_iQ0H);1450__m128i out_08 = _mm_unpacklo_epi16(coeff_iQ8L, coeff_iQ8H);1451__m128i out_12 = _mm_unpackhi_epi16(coeff_iQ8L, coeff_iQ8H);1452// out = (coeff * iQ + B)1453const __m128i bias_00 = _mm_loadu_si128((const __m128i*)&mtx->bias[0]);1454const __m128i bias_04 = _mm_loadu_si128((const __m128i*)&mtx->bias[4]);1455const __m128i bias_08 = _mm_loadu_si128((const __m128i*)&mtx->bias[8]);1456const __m128i bias_12 = _mm_loadu_si128((const __m128i*)&mtx->bias[12]);1457out_00 = _mm_add_epi32(out_00, bias_00);1458out_04 = _mm_add_epi32(out_04, bias_04);1459out_08 = _mm_add_epi32(out_08, bias_08);1460out_12 = _mm_add_epi32(out_12, bias_12);1461// out = QUANTDIV(coeff, iQ, B, QFIX)1462out_00 = _mm_srai_epi32(out_00, QFIX);1463out_04 = _mm_srai_epi32(out_04, QFIX);1464out_08 = _mm_srai_epi32(out_08, QFIX);1465out_12 = _mm_srai_epi32(out_12, QFIX);14661467// pack result as 16b1468out0 = _mm_packs_epi32(out_00, out_04);1469out8 = _mm_packs_epi32(out_08, out_12);14701471// if (coeff > 2047) coeff = 20471472out0 = _mm_min_epi16(out0, max_coeff_2047);1473out8 = _mm_min_epi16(out8, max_coeff_2047);1474}14751476// get sign back (if (sign[j]) out_n = -out_n)1477out0 = _mm_xor_si128(out0, sign0);1478out8 = _mm_xor_si128(out8, sign8);1479out0 = _mm_sub_epi16(out0, sign0);1480out8 = _mm_sub_epi16(out8, sign8);14811482// in = out * Q1483in0 = _mm_mullo_epi16(out0, q0);1484in8 = _mm_mullo_epi16(out8, q8);14851486_mm_storeu_si128((__m128i*)&in[0], in0);1487_mm_storeu_si128((__m128i*)&in[8], in8);14881489// zigzag the output before storing it.1490//1491// The zigzag pattern can almost be reproduced with a small sequence of1492// shuffles. After it, we only need to swap the 7th (ending up in third1493// position instead of twelfth) and 8th values.1494{1495__m128i outZ0, outZ8;1496outZ0 = _mm_shufflehi_epi16(out0, _MM_SHUFFLE(2, 1, 3, 0));1497outZ0 = _mm_shuffle_epi32 (outZ0, _MM_SHUFFLE(3, 1, 2, 0));1498outZ0 = _mm_shufflehi_epi16(outZ0, _MM_SHUFFLE(3, 1, 0, 2));1499outZ8 = _mm_shufflelo_epi16(out8, _MM_SHUFFLE(3, 0, 2, 1));1500outZ8 = _mm_shuffle_epi32 (outZ8, _MM_SHUFFLE(3, 1, 2, 0));1501outZ8 = _mm_shufflelo_epi16(outZ8, _MM_SHUFFLE(1, 3, 2, 0));1502_mm_storeu_si128((__m128i*)&out[0], outZ0);1503_mm_storeu_si128((__m128i*)&out[8], outZ8);1504packed_out = _mm_packs_epi16(outZ0, outZ8);1505}1506{1507const int16_t outZ_12 = out[12];1508const int16_t outZ_3 = out[3];1509out[3] = outZ_12;1510out[12] = outZ_3;1511}15121513// detect if all 'out' values are zeroes or not1514return (_mm_movemask_epi8(_mm_cmpeq_epi8(packed_out, zero)) != 0xffff);1515}15161517static int QuantizeBlock_SSE2(int16_t in[16], int16_t out[16],1518const VP8Matrix* WEBP_RESTRICT const mtx) {1519return DoQuantizeBlock_SSE2(in, out, &mtx->sharpen[0], mtx);1520}15211522static int QuantizeBlockWHT_SSE2(int16_t in[16], int16_t out[16],1523const VP8Matrix* WEBP_RESTRICT const mtx) {1524return DoQuantizeBlock_SSE2(in, out, NULL, mtx);1525}15261527static int Quantize2Blocks_SSE2(int16_t in[32], int16_t out[32],1528const VP8Matrix* WEBP_RESTRICT const mtx) {1529int nz;1530const uint16_t* const sharpen = &mtx->sharpen[0];1531nz = DoQuantizeBlock_SSE2(in + 0 * 16, out + 0 * 16, sharpen, mtx) << 0;1532nz |= DoQuantizeBlock_SSE2(in + 1 * 16, out + 1 * 16, sharpen, mtx) << 1;1533return nz;1534}15351536//------------------------------------------------------------------------------1537// Entry point15381539extern void VP8EncDspInitSSE2(void);15401541WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspInitSSE2(void) {1542VP8CollectHistogram = CollectHistogram_SSE2;1543VP8EncPredLuma16 = Intra16Preds_SSE2;1544VP8EncPredChroma8 = IntraChromaPreds_SSE2;1545VP8EncPredLuma4 = Intra4Preds_SSE2;1546VP8EncQuantizeBlock = QuantizeBlock_SSE2;1547VP8EncQuantize2Blocks = Quantize2Blocks_SSE2;1548VP8EncQuantizeBlockWHT = QuantizeBlockWHT_SSE2;1549VP8ITransform = ITransform_SSE2;1550VP8FTransform = FTransform_SSE2;1551VP8FTransform2 = FTransform2_SSE2;1552VP8FTransformWHT = FTransformWHT_SSE2;1553VP8SSE16x16 = SSE16x16_SSE2;1554VP8SSE16x8 = SSE16x8_SSE2;1555VP8SSE8x8 = SSE8x8_SSE2;1556VP8SSE4x4 = SSE4x4_SSE2;1557VP8TDisto4x4 = Disto4x4_SSE2;1558VP8TDisto16x16 = Disto16x16_SSE2;1559VP8Mean16x4 = Mean16x4_SSE2;1560}15611562#else // !WEBP_USE_SSE215631564WEBP_DSP_INIT_STUB(VP8EncDspInitSSE2)15651566#endif // WEBP_USE_SSE2156715681569