Path: blob/master/3rdparty/libwebp/src/dsp/lossless_enc_sse41.c
16348 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// For sign-extended multiplying constants, pre-shifted by 5:21#define CST_5b(X) (((int16_t)((uint16_t)(X) << 8)) >> 5)2223//------------------------------------------------------------------------------24// Subtract-Green Transform2526static void SubtractGreenFromBlueAndRed_SSE41(uint32_t* argb_data,27int num_pixels) {28int i;29const __m128i kCstShuffle = _mm_set_epi8(-1, 13, -1, 13, -1, 9, -1, 9,30-1, 5, -1, 5, -1, 1, -1, 1);31for (i = 0; i + 4 <= num_pixels; i += 4) {32const __m128i in = _mm_loadu_si128((__m128i*)&argb_data[i]);33const __m128i in_0g0g = _mm_shuffle_epi8(in, kCstShuffle);34const __m128i out = _mm_sub_epi8(in, in_0g0g);35_mm_storeu_si128((__m128i*)&argb_data[i], out);36}37// fallthrough and finish off with plain-C38if (i != num_pixels) {39VP8LSubtractGreenFromBlueAndRed_C(argb_data + i, num_pixels - i);40}41}4243//------------------------------------------------------------------------------44// Color Transform4546#define SPAN 847static void CollectColorBlueTransforms_SSE41(const uint32_t* argb, int stride,48int tile_width, int tile_height,49int green_to_blue, int red_to_blue,50int histo[]) {51const __m128i mults_r = _mm_set1_epi16(CST_5b(red_to_blue));52const __m128i mults_g = _mm_set1_epi16(CST_5b(green_to_blue));53const __m128i mask_g = _mm_set1_epi16(0xff00); // green mask54const __m128i mask_gb = _mm_set1_epi32(0xffff); // green/blue mask55const __m128i mask_b = _mm_set1_epi16(0x00ff); // blue mask56const __m128i shuffler_lo = _mm_setr_epi8(-1, 2, -1, 6, -1, 10, -1, 14, -1,57-1, -1, -1, -1, -1, -1, -1);58const __m128i shuffler_hi = _mm_setr_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1,592, -1, 6, -1, 10, -1, 14);60int y;61for (y = 0; y < tile_height; ++y) {62const uint32_t* const src = argb + y * stride;63int i, x;64for (x = 0; x + SPAN <= tile_width; x += SPAN) {65uint16_t values[SPAN];66const __m128i in0 = _mm_loadu_si128((__m128i*)&src[x + 0]);67const __m128i in1 = _mm_loadu_si128((__m128i*)&src[x + SPAN / 2]);68const __m128i r0 = _mm_shuffle_epi8(in0, shuffler_lo);69const __m128i r1 = _mm_shuffle_epi8(in1, shuffler_hi);70const __m128i r = _mm_or_si128(r0, r1); // r 071const __m128i gb0 = _mm_and_si128(in0, mask_gb);72const __m128i gb1 = _mm_and_si128(in1, mask_gb);73const __m128i gb = _mm_packus_epi32(gb0, gb1); // g b74const __m128i g = _mm_and_si128(gb, mask_g); // g 075const __m128i A = _mm_mulhi_epi16(r, mults_r); // x dbr76const __m128i B = _mm_mulhi_epi16(g, mults_g); // x dbg77const __m128i C = _mm_sub_epi8(gb, B); // x b'78const __m128i D = _mm_sub_epi8(C, A); // x b''79const __m128i E = _mm_and_si128(D, mask_b); // 0 b''80_mm_storeu_si128((__m128i*)values, E);81for (i = 0; i < SPAN; ++i) ++histo[values[i]];82}83}84{85const int left_over = tile_width & (SPAN - 1);86if (left_over > 0) {87VP8LCollectColorBlueTransforms_C(argb + tile_width - left_over, stride,88left_over, tile_height,89green_to_blue, red_to_blue, histo);90}91}92}9394static void CollectColorRedTransforms_SSE41(const uint32_t* argb, int stride,95int tile_width, int tile_height,96int green_to_red, int histo[]) {97const __m128i mults_g = _mm_set1_epi16(CST_5b(green_to_red));98const __m128i mask_g = _mm_set1_epi32(0x00ff00); // green mask99const __m128i mask = _mm_set1_epi16(0xff);100101int y;102for (y = 0; y < tile_height; ++y) {103const uint32_t* const src = argb + y * stride;104int i, x;105for (x = 0; x + SPAN <= tile_width; x += SPAN) {106uint16_t values[SPAN];107const __m128i in0 = _mm_loadu_si128((__m128i*)&src[x + 0]);108const __m128i in1 = _mm_loadu_si128((__m128i*)&src[x + SPAN / 2]);109const __m128i g0 = _mm_and_si128(in0, mask_g); // 0 0 | g 0110const __m128i g1 = _mm_and_si128(in1, mask_g);111const __m128i g = _mm_packus_epi32(g0, g1); // g 0112const __m128i A0 = _mm_srli_epi32(in0, 16); // 0 0 | x r113const __m128i A1 = _mm_srli_epi32(in1, 16);114const __m128i A = _mm_packus_epi32(A0, A1); // x r115const __m128i B = _mm_mulhi_epi16(g, mults_g); // x dr116const __m128i C = _mm_sub_epi8(A, B); // x r'117const __m128i D = _mm_and_si128(C, mask); // 0 r'118_mm_storeu_si128((__m128i*)values, D);119for (i = 0; i < SPAN; ++i) ++histo[values[i]];120}121}122{123const int left_over = tile_width & (SPAN - 1);124if (left_over > 0) {125VP8LCollectColorRedTransforms_C(argb + tile_width - left_over, stride,126left_over, tile_height, green_to_red,127histo);128}129}130}131132//------------------------------------------------------------------------------133// Entry point134135extern void VP8LEncDspInitSSE41(void);136137WEBP_TSAN_IGNORE_FUNCTION void VP8LEncDspInitSSE41(void) {138VP8LSubtractGreenFromBlueAndRed = SubtractGreenFromBlueAndRed_SSE41;139VP8LCollectColorBlueTransforms = CollectColorBlueTransforms_SSE41;140VP8LCollectColorRedTransforms = CollectColorRedTransforms_SSE41;141}142143#else // !WEBP_USE_SSE41144145WEBP_DSP_INIT_STUB(VP8LEncDspInitSSE41)146147#endif // WEBP_USE_SSE41148149150