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 | |
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 MK_CST_16(HI, LO) \ |
48 | _mm_set1_epi32((int)(((uint32_t)(HI) << 16) | ((LO) & 0xffff))) |
49 | |
50 | static void CollectColorBlueTransforms_SSE41(const uint32_t* argb, int stride, |
51 | int tile_width, int tile_height, |
52 | int green_to_blue, int red_to_blue, |
53 | int histo[]) { |
54 | const __m128i mult = |
55 | MK_CST_16(CST_5b(red_to_blue) + 256,CST_5b(green_to_blue)); |
56 | const __m128i perm = |
57 | _mm_setr_epi8(-1, 1, -1, 2, -1, 5, -1, 6, -1, 9, -1, 10, -1, 13, -1, 14); |
58 | if (tile_width >= 4) { |
59 | int y; |
60 | for (y = 0; y < tile_height; ++y) { |
61 | const uint32_t* const src = argb + y * stride; |
62 | const __m128i A1 = _mm_loadu_si128((const __m128i*)src); |
63 | const __m128i B1 = _mm_shuffle_epi8(A1, perm); |
64 | const __m128i C1 = _mm_mulhi_epi16(B1, mult); |
65 | const __m128i D1 = _mm_sub_epi16(A1, C1); |
66 | __m128i E = _mm_add_epi16(_mm_srli_epi32(D1, 16), D1); |
67 | int x; |
68 | for (x = 4; x + 4 <= tile_width; x += 4) { |
69 | const __m128i A2 = _mm_loadu_si128((const __m128i*)(src + x)); |
70 | __m128i B2, C2, D2; |
71 | ++histo[_mm_extract_epi8(E, 0)]; |
72 | B2 = _mm_shuffle_epi8(A2, perm); |
73 | ++histo[_mm_extract_epi8(E, 4)]; |
74 | C2 = _mm_mulhi_epi16(B2, mult); |
75 | ++histo[_mm_extract_epi8(E, 8)]; |
76 | D2 = _mm_sub_epi16(A2, C2); |
77 | ++histo[_mm_extract_epi8(E, 12)]; |
78 | E = _mm_add_epi16(_mm_srli_epi32(D2, 16), D2); |
79 | } |
80 | ++histo[_mm_extract_epi8(E, 0)]; |
81 | ++histo[_mm_extract_epi8(E, 4)]; |
82 | ++histo[_mm_extract_epi8(E, 8)]; |
83 | ++histo[_mm_extract_epi8(E, 12)]; |
84 | } |
85 | } |
86 | { |
87 | const int left_over = tile_width & 3; |
88 | if (left_over > 0) { |
89 | VP8LCollectColorBlueTransforms_C(argb + tile_width - left_over, stride, |
90 | left_over, tile_height, |
91 | green_to_blue, red_to_blue, histo); |
92 | } |
93 | } |
94 | } |
95 | |
96 | static void CollectColorRedTransforms_SSE41(const uint32_t* argb, int stride, |
97 | int tile_width, int tile_height, |
98 | int green_to_red, int histo[]) { |
99 | |
100 | const __m128i mult = MK_CST_16(0, CST_5b(green_to_red)); |
101 | const __m128i mask_g = _mm_set1_epi32(0x0000ff00); |
102 | if (tile_width >= 4) { |
103 | int y; |
104 | for (y = 0; y < tile_height; ++y) { |
105 | const uint32_t* const src = argb + y * stride; |
106 | const __m128i A1 = _mm_loadu_si128((const __m128i*)src); |
107 | const __m128i B1 = _mm_and_si128(A1, mask_g); |
108 | const __m128i C1 = _mm_madd_epi16(B1, mult); |
109 | __m128i D = _mm_sub_epi16(A1, C1); |
110 | int x; |
111 | for (x = 4; x + 4 <= tile_width; x += 4) { |
112 | const __m128i A2 = _mm_loadu_si128((const __m128i*)(src + x)); |
113 | __m128i B2, C2; |
114 | ++histo[_mm_extract_epi8(D, 2)]; |
115 | B2 = _mm_and_si128(A2, mask_g); |
116 | ++histo[_mm_extract_epi8(D, 6)]; |
117 | C2 = _mm_madd_epi16(B2, mult); |
118 | ++histo[_mm_extract_epi8(D, 10)]; |
119 | ++histo[_mm_extract_epi8(D, 14)]; |
120 | D = _mm_sub_epi16(A2, C2); |
121 | } |
122 | ++histo[_mm_extract_epi8(D, 2)]; |
123 | ++histo[_mm_extract_epi8(D, 6)]; |
124 | ++histo[_mm_extract_epi8(D, 10)]; |
125 | ++histo[_mm_extract_epi8(D, 14)]; |
126 | } |
127 | } |
128 | { |
129 | const int left_over = tile_width & 3; |
130 | if (left_over > 0) { |
131 | VP8LCollectColorRedTransforms_C(argb + tile_width - left_over, stride, |
132 | left_over, tile_height, green_to_red, |
133 | histo); |
134 | } |
135 | } |
136 | } |
137 | |
138 | #undef MK_CST_16 |
139 | |
140 | //------------------------------------------------------------------------------ |
141 | // Entry point |
142 | |
143 | extern void VP8LEncDspInitSSE41(void); |
144 | |
145 | WEBP_TSAN_IGNORE_FUNCTION void VP8LEncDspInitSSE41(void) { |
146 | VP8LSubtractGreenFromBlueAndRed = SubtractGreenFromBlueAndRed_SSE41; |
147 | VP8LCollectColorBlueTransforms = CollectColorBlueTransforms_SSE41; |
148 | VP8LCollectColorRedTransforms = CollectColorRedTransforms_SSE41; |
149 | } |
150 | |
151 | #else // !WEBP_USE_SSE41 |
152 | |
153 | WEBP_DSP_INIT_STUB(VP8LEncDspInitSSE41) |
154 | |
155 | #endif // WEBP_USE_SSE41 |
156 | |