Path: blob/master/thirdparty/libwebp/src/dsp/lossless_enc_sse41.c
21743 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 <emmintrin.h>17#include <smmintrin.h>1819#include <assert.h>2021#include "src/dsp/cpu.h"22#include "src/dsp/lossless.h"23#include "src/webp/types.h"2425//------------------------------------------------------------------------------26// Cost operations.2728static WEBP_INLINE uint32_t HorizontalSum_SSE41(__m128i cost) {29cost = _mm_add_epi32(cost, _mm_srli_si128(cost, 8));30cost = _mm_add_epi32(cost, _mm_srli_si128(cost, 4));31return _mm_cvtsi128_si32(cost);32}3334static uint32_t ExtraCost_SSE41(const uint32_t* const a, int length) {35int i;36__m128i cost = _mm_set_epi32(2 * a[7], 2 * a[6], a[5], a[4]);37assert(length % 8 == 0);3839for (i = 8; i + 8 <= length; i += 8) {40const int j = (i - 2) >> 1;41const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[i]);42const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[i + 4]);43const __m128i w = _mm_set_epi32(j + 3, j + 2, j + 1, j);44const __m128i a2 = _mm_hadd_epi32(a0, a1);45const __m128i mul = _mm_mullo_epi32(a2, w);46cost = _mm_add_epi32(mul, cost);47}48return HorizontalSum_SSE41(cost);49}5051//------------------------------------------------------------------------------52// Subtract-Green Transform5354static void SubtractGreenFromBlueAndRed_SSE41(uint32_t* argb_data,55int num_pixels) {56int i;57const __m128i kCstShuffle = _mm_set_epi8(-1, 13, -1, 13, -1, 9, -1, 9,58-1, 5, -1, 5, -1, 1, -1, 1);59for (i = 0; i + 4 <= num_pixels; i += 4) {60const __m128i in = _mm_loadu_si128((__m128i*)&argb_data[i]);61const __m128i in_0g0g = _mm_shuffle_epi8(in, kCstShuffle);62const __m128i out = _mm_sub_epi8(in, in_0g0g);63_mm_storeu_si128((__m128i*)&argb_data[i], out);64}65// fallthrough and finish off with plain-C66if (i != num_pixels) {67VP8LSubtractGreenFromBlueAndRed_C(argb_data + i, num_pixels - i);68}69}7071//------------------------------------------------------------------------------72// Color Transform7374// For sign-extended multiplying constants, pre-shifted by 5:75#define CST_5b(X) (((int16_t)((uint16_t)(X) << 8)) >> 5)7677#define MK_CST_16(HI, LO) \78_mm_set1_epi32((int)(((uint32_t)(HI) << 16) | ((LO) & 0xffff)))7980static void CollectColorBlueTransforms_SSE41(const uint32_t* WEBP_RESTRICT argb,81int stride,82int tile_width, int tile_height,83int green_to_blue, int red_to_blue,84uint32_t histo[]) {85const __m128i mult =86MK_CST_16(CST_5b(red_to_blue) + 256,CST_5b(green_to_blue));87const __m128i perm =88_mm_setr_epi8(-1, 1, -1, 2, -1, 5, -1, 6, -1, 9, -1, 10, -1, 13, -1, 14);89if (tile_width >= 4) {90int y;91for (y = 0; y < tile_height; ++y) {92const uint32_t* const src = argb + y * stride;93const __m128i A1 = _mm_loadu_si128((const __m128i*)src);94const __m128i B1 = _mm_shuffle_epi8(A1, perm);95const __m128i C1 = _mm_mulhi_epi16(B1, mult);96const __m128i D1 = _mm_sub_epi16(A1, C1);97__m128i E = _mm_add_epi16(_mm_srli_epi32(D1, 16), D1);98int x;99for (x = 4; x + 4 <= tile_width; x += 4) {100const __m128i A2 = _mm_loadu_si128((const __m128i*)(src + x));101__m128i B2, C2, D2;102++histo[_mm_extract_epi8(E, 0)];103B2 = _mm_shuffle_epi8(A2, perm);104++histo[_mm_extract_epi8(E, 4)];105C2 = _mm_mulhi_epi16(B2, mult);106++histo[_mm_extract_epi8(E, 8)];107D2 = _mm_sub_epi16(A2, C2);108++histo[_mm_extract_epi8(E, 12)];109E = _mm_add_epi16(_mm_srli_epi32(D2, 16), D2);110}111++histo[_mm_extract_epi8(E, 0)];112++histo[_mm_extract_epi8(E, 4)];113++histo[_mm_extract_epi8(E, 8)];114++histo[_mm_extract_epi8(E, 12)];115}116}117{118const int left_over = tile_width & 3;119if (left_over > 0) {120VP8LCollectColorBlueTransforms_C(argb + tile_width - left_over, stride,121left_over, tile_height,122green_to_blue, red_to_blue, histo);123}124}125}126127static void CollectColorRedTransforms_SSE41(const uint32_t* WEBP_RESTRICT argb,128int stride,129int tile_width, int tile_height,130int green_to_red,131uint32_t histo[]) {132const __m128i mult = MK_CST_16(0, CST_5b(green_to_red));133const __m128i mask_g = _mm_set1_epi32(0x0000ff00);134if (tile_width >= 4) {135int y;136for (y = 0; y < tile_height; ++y) {137const uint32_t* const src = argb + y * stride;138const __m128i A1 = _mm_loadu_si128((const __m128i*)src);139const __m128i B1 = _mm_and_si128(A1, mask_g);140const __m128i C1 = _mm_madd_epi16(B1, mult);141__m128i D = _mm_sub_epi16(A1, C1);142int x;143for (x = 4; x + 4 <= tile_width; x += 4) {144const __m128i A2 = _mm_loadu_si128((const __m128i*)(src + x));145__m128i B2, C2;146++histo[_mm_extract_epi8(D, 2)];147B2 = _mm_and_si128(A2, mask_g);148++histo[_mm_extract_epi8(D, 6)];149C2 = _mm_madd_epi16(B2, mult);150++histo[_mm_extract_epi8(D, 10)];151++histo[_mm_extract_epi8(D, 14)];152D = _mm_sub_epi16(A2, C2);153}154++histo[_mm_extract_epi8(D, 2)];155++histo[_mm_extract_epi8(D, 6)];156++histo[_mm_extract_epi8(D, 10)];157++histo[_mm_extract_epi8(D, 14)];158}159}160{161const int left_over = tile_width & 3;162if (left_over > 0) {163VP8LCollectColorRedTransforms_C(argb + tile_width - left_over, stride,164left_over, tile_height, green_to_red,165histo);166}167}168}169170#undef MK_CST_16171172//------------------------------------------------------------------------------173// Entry point174175extern void VP8LEncDspInitSSE41(void);176177WEBP_TSAN_IGNORE_FUNCTION void VP8LEncDspInitSSE41(void) {178VP8LExtraCost = ExtraCost_SSE41;179VP8LSubtractGreenFromBlueAndRed = SubtractGreenFromBlueAndRed_SSE41;180VP8LCollectColorBlueTransforms = CollectColorBlueTransforms_SSE41;181VP8LCollectColorRedTransforms = CollectColorRedTransforms_SSE41;182183// SSE exports for AVX and above.184VP8LSubtractGreenFromBlueAndRed_SSE = SubtractGreenFromBlueAndRed_SSE41;185VP8LCollectColorBlueTransforms_SSE = CollectColorBlueTransforms_SSE41;186VP8LCollectColorRedTransforms_SSE = CollectColorRedTransforms_SSE41;187}188189#else // !WEBP_USE_SSE41190191WEBP_DSP_INIT_STUB(VP8LEncDspInitSSE41)192193#endif // WEBP_USE_SSE41194195196