Path: blob/master/thirdparty/libwebp/src/dsp/lossless_enc_sse41.c
9914 views
// Copyright 2015 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// SSE4.1 variant of methods for lossless encoder10//11// Author: Skal ([email protected])1213#include "src/dsp/dsp.h"1415#if defined(WEBP_USE_SSE41)16#include <assert.h>17#include <smmintrin.h>18#include "src/dsp/lossless.h"1920//------------------------------------------------------------------------------21// Cost operations.2223static WEBP_INLINE uint32_t HorizontalSum_SSE41(__m128i cost) {24cost = _mm_add_epi32(cost, _mm_srli_si128(cost, 8));25cost = _mm_add_epi32(cost, _mm_srli_si128(cost, 4));26return _mm_cvtsi128_si32(cost);27}2829static uint32_t ExtraCost_SSE41(const uint32_t* const a, int length) {30int i;31__m128i cost = _mm_set_epi32(2 * a[7], 2 * a[6], a[5], a[4]);32assert(length % 8 == 0);3334for (i = 8; i + 8 <= length; i += 8) {35const int j = (i - 2) >> 1;36const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[i]);37const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[i + 4]);38const __m128i w = _mm_set_epi32(j + 3, j + 2, j + 1, j);39const __m128i a2 = _mm_hadd_epi32(a0, a1);40const __m128i mul = _mm_mullo_epi32(a2, w);41cost = _mm_add_epi32(mul, cost);42}43return HorizontalSum_SSE41(cost);44}4546static uint32_t ExtraCostCombined_SSE41(const uint32_t* WEBP_RESTRICT const a,47const uint32_t* WEBP_RESTRICT const b,48int length) {49int i;50__m128i cost = _mm_add_epi32(_mm_set_epi32(2 * a[7], 2 * a[6], a[5], a[4]),51_mm_set_epi32(2 * b[7], 2 * b[6], b[5], b[4]));52assert(length % 8 == 0);5354for (i = 8; i + 8 <= length; i += 8) {55const int j = (i - 2) >> 1;56const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[i]);57const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[i + 4]);58const __m128i b0 = _mm_loadu_si128((const __m128i*)&b[i]);59const __m128i b1 = _mm_loadu_si128((const __m128i*)&b[i + 4]);60const __m128i w = _mm_set_epi32(j + 3, j + 2, j + 1, j);61const __m128i a2 = _mm_hadd_epi32(a0, a1);62const __m128i b2 = _mm_hadd_epi32(b0, b1);63const __m128i mul = _mm_mullo_epi32(_mm_add_epi32(a2, b2), w);64cost = _mm_add_epi32(mul, cost);65}66return HorizontalSum_SSE41(cost);67}6869//------------------------------------------------------------------------------70// Subtract-Green Transform7172static void SubtractGreenFromBlueAndRed_SSE41(uint32_t* argb_data,73int num_pixels) {74int i;75const __m128i kCstShuffle = _mm_set_epi8(-1, 13, -1, 13, -1, 9, -1, 9,76-1, 5, -1, 5, -1, 1, -1, 1);77for (i = 0; i + 4 <= num_pixels; i += 4) {78const __m128i in = _mm_loadu_si128((__m128i*)&argb_data[i]);79const __m128i in_0g0g = _mm_shuffle_epi8(in, kCstShuffle);80const __m128i out = _mm_sub_epi8(in, in_0g0g);81_mm_storeu_si128((__m128i*)&argb_data[i], out);82}83// fallthrough and finish off with plain-C84if (i != num_pixels) {85VP8LSubtractGreenFromBlueAndRed_C(argb_data + i, num_pixels - i);86}87}8889//------------------------------------------------------------------------------90// Color Transform9192// For sign-extended multiplying constants, pre-shifted by 5:93#define CST_5b(X) (((int16_t)((uint16_t)(X) << 8)) >> 5)9495#define MK_CST_16(HI, LO) \96_mm_set1_epi32((int)(((uint32_t)(HI) << 16) | ((LO) & 0xffff)))9798static void CollectColorBlueTransforms_SSE41(const uint32_t* WEBP_RESTRICT argb,99int stride,100int tile_width, int tile_height,101int green_to_blue, int red_to_blue,102uint32_t histo[]) {103const __m128i mult =104MK_CST_16(CST_5b(red_to_blue) + 256,CST_5b(green_to_blue));105const __m128i perm =106_mm_setr_epi8(-1, 1, -1, 2, -1, 5, -1, 6, -1, 9, -1, 10, -1, 13, -1, 14);107if (tile_width >= 4) {108int y;109for (y = 0; y < tile_height; ++y) {110const uint32_t* const src = argb + y * stride;111const __m128i A1 = _mm_loadu_si128((const __m128i*)src);112const __m128i B1 = _mm_shuffle_epi8(A1, perm);113const __m128i C1 = _mm_mulhi_epi16(B1, mult);114const __m128i D1 = _mm_sub_epi16(A1, C1);115__m128i E = _mm_add_epi16(_mm_srli_epi32(D1, 16), D1);116int x;117for (x = 4; x + 4 <= tile_width; x += 4) {118const __m128i A2 = _mm_loadu_si128((const __m128i*)(src + x));119__m128i B2, C2, D2;120++histo[_mm_extract_epi8(E, 0)];121B2 = _mm_shuffle_epi8(A2, perm);122++histo[_mm_extract_epi8(E, 4)];123C2 = _mm_mulhi_epi16(B2, mult);124++histo[_mm_extract_epi8(E, 8)];125D2 = _mm_sub_epi16(A2, C2);126++histo[_mm_extract_epi8(E, 12)];127E = _mm_add_epi16(_mm_srli_epi32(D2, 16), D2);128}129++histo[_mm_extract_epi8(E, 0)];130++histo[_mm_extract_epi8(E, 4)];131++histo[_mm_extract_epi8(E, 8)];132++histo[_mm_extract_epi8(E, 12)];133}134}135{136const int left_over = tile_width & 3;137if (left_over > 0) {138VP8LCollectColorBlueTransforms_C(argb + tile_width - left_over, stride,139left_over, tile_height,140green_to_blue, red_to_blue, histo);141}142}143}144145static void CollectColorRedTransforms_SSE41(const uint32_t* WEBP_RESTRICT argb,146int stride,147int tile_width, int tile_height,148int green_to_red,149uint32_t histo[]) {150const __m128i mult = MK_CST_16(0, CST_5b(green_to_red));151const __m128i mask_g = _mm_set1_epi32(0x0000ff00);152if (tile_width >= 4) {153int y;154for (y = 0; y < tile_height; ++y) {155const uint32_t* const src = argb + y * stride;156const __m128i A1 = _mm_loadu_si128((const __m128i*)src);157const __m128i B1 = _mm_and_si128(A1, mask_g);158const __m128i C1 = _mm_madd_epi16(B1, mult);159__m128i D = _mm_sub_epi16(A1, C1);160int x;161for (x = 4; x + 4 <= tile_width; x += 4) {162const __m128i A2 = _mm_loadu_si128((const __m128i*)(src + x));163__m128i B2, C2;164++histo[_mm_extract_epi8(D, 2)];165B2 = _mm_and_si128(A2, mask_g);166++histo[_mm_extract_epi8(D, 6)];167C2 = _mm_madd_epi16(B2, mult);168++histo[_mm_extract_epi8(D, 10)];169++histo[_mm_extract_epi8(D, 14)];170D = _mm_sub_epi16(A2, C2);171}172++histo[_mm_extract_epi8(D, 2)];173++histo[_mm_extract_epi8(D, 6)];174++histo[_mm_extract_epi8(D, 10)];175++histo[_mm_extract_epi8(D, 14)];176}177}178{179const int left_over = tile_width & 3;180if (left_over > 0) {181VP8LCollectColorRedTransforms_C(argb + tile_width - left_over, stride,182left_over, tile_height, green_to_red,183histo);184}185}186}187188#undef MK_CST_16189190//------------------------------------------------------------------------------191// Entry point192193extern void VP8LEncDspInitSSE41(void);194195WEBP_TSAN_IGNORE_FUNCTION void VP8LEncDspInitSSE41(void) {196VP8LExtraCost = ExtraCost_SSE41;197VP8LExtraCostCombined = ExtraCostCombined_SSE41;198VP8LSubtractGreenFromBlueAndRed = SubtractGreenFromBlueAndRed_SSE41;199VP8LCollectColorBlueTransforms = CollectColorBlueTransforms_SSE41;200VP8LCollectColorRedTransforms = CollectColorRedTransforms_SSE41;201}202203#else // !WEBP_USE_SSE41204205WEBP_DSP_INIT_STUB(VP8LEncDspInitSSE41)206207#endif // WEBP_USE_SSE41208209210