1 // Copyright 2015 Google Inc. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
9 //
10 // SSE4.1 variant of methods for lossless encoder
11 //
12 // Author: Skal (pascal.massimino@gmail.com)
13 
14 #include "src/dsp/dsp.h"
15 
16 #if defined(WEBP_USE_SSE41)
17 #include <assert.h>
18 #include <smmintrin.h>
19 #include "src/dsp/lossless.h"
20 
21 // For sign-extended multiplying constants, pre-shifted by 5:
22 #define CST_5b(X)  (((int16_t)((uint16_t)(X) << 8)) >> 5)
23 
24 //------------------------------------------------------------------------------
25 // Subtract-Green Transform
26 
SubtractGreenFromBlueAndRed_SSE41(uint32_t * argb_data,int num_pixels)27 static void SubtractGreenFromBlueAndRed_SSE41(uint32_t* argb_data,
28                                               int num_pixels) {
29   int i;
30   const __m128i kCstShuffle = _mm_set_epi8(-1, 13, -1, 13, -1, 9, -1, 9,
31                                            -1,  5, -1,  5, -1, 1, -1, 1);
32   for (i = 0; i + 4 <= num_pixels; i += 4) {
33     const __m128i in = _mm_loadu_si128((__m128i*)&argb_data[i]);
34     const __m128i in_0g0g = _mm_shuffle_epi8(in, kCstShuffle);
35     const __m128i out = _mm_sub_epi8(in, in_0g0g);
36     _mm_storeu_si128((__m128i*)&argb_data[i], out);
37   }
38   // fallthrough and finish off with plain-C
39   if (i != num_pixels) {
40     VP8LSubtractGreenFromBlueAndRed_C(argb_data + i, num_pixels - i);
41   }
42 }
43 
44 //------------------------------------------------------------------------------
45 // Color Transform
46 
47 #define SPAN 8
CollectColorBlueTransforms_SSE41(const uint32_t * argb,int stride,int tile_width,int tile_height,int green_to_blue,int red_to_blue,int histo[])48 static void CollectColorBlueTransforms_SSE41(const uint32_t* argb, int stride,
49                                              int tile_width, int tile_height,
50                                              int green_to_blue, int red_to_blue,
51                                              int histo[]) {
52   const __m128i mults_r = _mm_set1_epi16(CST_5b(red_to_blue));
53   const __m128i mults_g = _mm_set1_epi16(CST_5b(green_to_blue));
54   const __m128i mask_g = _mm_set1_epi16((short)0xff00);   // green mask
55   const __m128i mask_gb = _mm_set1_epi32(0xffff);         // green/blue mask
56   const __m128i mask_b = _mm_set1_epi16(0x00ff);          // blue mask
57   const __m128i shuffler_lo = _mm_setr_epi8(-1, 2, -1, 6, -1, 10, -1, 14, -1,
58                                             -1, -1, -1, -1, -1, -1, -1);
59   const __m128i shuffler_hi = _mm_setr_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1,
60                                             2, -1, 6, -1, 10, -1, 14);
61   int y;
62   for (y = 0; y < tile_height; ++y) {
63     const uint32_t* const src = argb + y * stride;
64     int i, x;
65     for (x = 0; x + SPAN <= tile_width; x += SPAN) {
66       uint16_t values[SPAN];
67       const __m128i in0 = _mm_loadu_si128((__m128i*)&src[x + 0]);
68       const __m128i in1 = _mm_loadu_si128((__m128i*)&src[x + SPAN / 2]);
69       const __m128i r0 = _mm_shuffle_epi8(in0, shuffler_lo);
70       const __m128i r1 = _mm_shuffle_epi8(in1, shuffler_hi);
71       const __m128i r = _mm_or_si128(r0, r1);         // r 0
72       const __m128i gb0 = _mm_and_si128(in0, mask_gb);
73       const __m128i gb1 = _mm_and_si128(in1, mask_gb);
74       const __m128i gb = _mm_packus_epi32(gb0, gb1);  // g b
75       const __m128i g = _mm_and_si128(gb, mask_g);    // g 0
76       const __m128i A = _mm_mulhi_epi16(r, mults_r);  // x dbr
77       const __m128i B = _mm_mulhi_epi16(g, mults_g);  // x dbg
78       const __m128i C = _mm_sub_epi8(gb, B);          // x b'
79       const __m128i D = _mm_sub_epi8(C, A);           // x b''
80       const __m128i E = _mm_and_si128(D, mask_b);     // 0 b''
81       _mm_storeu_si128((__m128i*)values, E);
82       for (i = 0; i < SPAN; ++i) ++histo[values[i]];
83     }
84   }
85   {
86     const int left_over = tile_width & (SPAN - 1);
87     if (left_over > 0) {
88       VP8LCollectColorBlueTransforms_C(argb + tile_width - left_over, stride,
89                                        left_over, tile_height,
90                                        green_to_blue, red_to_blue, histo);
91     }
92   }
93 }
94 
CollectColorRedTransforms_SSE41(const uint32_t * argb,int stride,int tile_width,int tile_height,int green_to_red,int histo[])95 static void CollectColorRedTransforms_SSE41(const uint32_t* argb, int stride,
96                                             int tile_width, int tile_height,
97                                             int green_to_red, int histo[]) {
98   const __m128i mults_g = _mm_set1_epi16(CST_5b(green_to_red));
99   const __m128i mask_g = _mm_set1_epi32(0x00ff00);  // green mask
100   const __m128i mask = _mm_set1_epi16(0xff);
101 
102   int y;
103   for (y = 0; y < tile_height; ++y) {
104     const uint32_t* const src = argb + y * stride;
105     int i, x;
106     for (x = 0; x + SPAN <= tile_width; x += SPAN) {
107       uint16_t values[SPAN];
108       const __m128i in0 = _mm_loadu_si128((__m128i*)&src[x + 0]);
109       const __m128i in1 = _mm_loadu_si128((__m128i*)&src[x + SPAN / 2]);
110       const __m128i g0 = _mm_and_si128(in0, mask_g);  // 0 0  | g 0
111       const __m128i g1 = _mm_and_si128(in1, mask_g);
112       const __m128i g = _mm_packus_epi32(g0, g1);     // g 0
113       const __m128i A0 = _mm_srli_epi32(in0, 16);     // 0 0  | x r
114       const __m128i A1 = _mm_srli_epi32(in1, 16);
115       const __m128i A = _mm_packus_epi32(A0, A1);     // x r
116       const __m128i B = _mm_mulhi_epi16(g, mults_g);  // x dr
117       const __m128i C = _mm_sub_epi8(A, B);           // x r'
118       const __m128i D = _mm_and_si128(C, mask);       // 0 r'
119       _mm_storeu_si128((__m128i*)values, D);
120       for (i = 0; i < SPAN; ++i) ++histo[values[i]];
121     }
122   }
123   {
124     const int left_over = tile_width & (SPAN - 1);
125     if (left_over > 0) {
126       VP8LCollectColorRedTransforms_C(argb + tile_width - left_over, stride,
127                                       left_over, tile_height, green_to_red,
128                                       histo);
129     }
130   }
131 }
132 
133 //------------------------------------------------------------------------------
134 // Entry point
135 
136 extern void VP8LEncDspInitSSE41(void);
137 
VP8LEncDspInitSSE41(void)138 WEBP_TSAN_IGNORE_FUNCTION void VP8LEncDspInitSSE41(void) {
139   VP8LSubtractGreenFromBlueAndRed = SubtractGreenFromBlueAndRed_SSE41;
140   VP8LCollectColorBlueTransforms = CollectColorBlueTransforms_SSE41;
141   VP8LCollectColorRedTransforms = CollectColorRedTransforms_SSE41;
142 }
143 
144 #else  // !WEBP_USE_SSE41
145 
146 WEBP_DSP_INIT_STUB(VP8LEncDspInitSSE41)
147 
148 #endif  // WEBP_USE_SSE41
149