/src/libwebp/src/dsp/yuv_sse41.c
Line | Count | Source |
1 | | // Copyright 2014 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 | | // YUV->RGB conversion functions |
11 | | // |
12 | | // Author: Skal (pascal.massimino@gmail.com) |
13 | | |
14 | | #include "src/dsp/yuv.h" |
15 | | |
16 | | #if defined(WEBP_USE_SSE41) |
17 | | #include <emmintrin.h> |
18 | | #include <smmintrin.h> |
19 | | #include <stdlib.h> |
20 | | |
21 | | #include "src/dsp/common_sse41.h" |
22 | | #include "src/dsp/cpu.h" |
23 | | #include "src/dsp/dsp.h" |
24 | | #include "src/utils/utils.h" |
25 | | #include "src/webp/decode.h" |
26 | | #include "src/webp/types.h" |
27 | | |
28 | | //----------------------------------------------------------------------------- |
29 | | // Convert spans of 32 pixels to various RGB formats for the fancy upsampler. |
30 | | |
31 | | // These constants are 14b fixed-point version of ITU-R BT.601 constants. |
32 | | // R = (19077 * y + 26149 * v - 14234) >> 6 |
33 | | // G = (19077 * y - 6419 * u - 13320 * v + 8708) >> 6 |
34 | | // B = (19077 * y + 33050 * u - 17685) >> 6 |
35 | | static void ConvertYUV444ToRGB_SSE41(const __m128i* const Y0, |
36 | | const __m128i* const U0, |
37 | | const __m128i* const V0, __m128i* const R, |
38 | 147M | __m128i* const G, __m128i* const B) { |
39 | 147M | const __m128i k19077 = _mm_set1_epi16(19077); |
40 | 147M | const __m128i k26149 = _mm_set1_epi16(26149); |
41 | 147M | const __m128i k14234 = _mm_set1_epi16(14234); |
42 | | // 33050 doesn't fit in a signed short: only use this with unsigned arithmetic |
43 | 147M | const __m128i k33050 = _mm_set1_epi16((short)33050); |
44 | 147M | const __m128i k17685 = _mm_set1_epi16(17685); |
45 | 147M | const __m128i k6419 = _mm_set1_epi16(6419); |
46 | 147M | const __m128i k13320 = _mm_set1_epi16(13320); |
47 | 147M | const __m128i k8708 = _mm_set1_epi16(8708); |
48 | | |
49 | 147M | const __m128i Y1 = _mm_mulhi_epu16(*Y0, k19077); |
50 | | |
51 | 147M | const __m128i R0 = _mm_mulhi_epu16(*V0, k26149); |
52 | 147M | const __m128i R1 = _mm_sub_epi16(Y1, k14234); |
53 | 147M | const __m128i R2 = _mm_add_epi16(R1, R0); |
54 | | |
55 | 147M | const __m128i G0 = _mm_mulhi_epu16(*U0, k6419); |
56 | 147M | const __m128i G1 = _mm_mulhi_epu16(*V0, k13320); |
57 | 147M | const __m128i G2 = _mm_add_epi16(Y1, k8708); |
58 | 147M | const __m128i G3 = _mm_add_epi16(G0, G1); |
59 | 147M | const __m128i G4 = _mm_sub_epi16(G2, G3); |
60 | | |
61 | | // be careful with the saturated *unsigned* arithmetic here! |
62 | 147M | const __m128i B0 = _mm_mulhi_epu16(*U0, k33050); |
63 | 147M | const __m128i B1 = _mm_adds_epu16(B0, Y1); |
64 | 147M | const __m128i B2 = _mm_subs_epu16(B1, k17685); |
65 | | |
66 | | // use logical shift for B2, which can be larger than 32767 |
67 | 147M | *R = _mm_srai_epi16(R2, 6); // range: [-14234, 30815] |
68 | 147M | *G = _mm_srai_epi16(G4, 6); // range: [-10953, 27710] |
69 | 147M | *B = _mm_srli_epi16(B2, 6); // range: [0, 34238] |
70 | 147M | } |
71 | | |
72 | | // Load the bytes into the *upper* part of 16b words. That's "<< 8", basically. |
73 | 441M | static WEBP_INLINE __m128i Load_HI_16_SSE41(const uint8_t* src) { |
74 | 441M | const __m128i zero = _mm_setzero_si128(); |
75 | 441M | return _mm_unpacklo_epi8(zero, _mm_loadl_epi64((const __m128i*)src)); |
76 | 441M | } |
77 | | |
78 | | // Load and replicate the U/V samples |
79 | 191k | static WEBP_INLINE __m128i Load_UV_HI_8_SSE41(const uint8_t* src) { |
80 | 191k | const __m128i zero = _mm_setzero_si128(); |
81 | 191k | const __m128i tmp0 = _mm_cvtsi32_si128(WebPMemToInt32(src)); |
82 | 191k | const __m128i tmp1 = _mm_unpacklo_epi8(zero, tmp0); |
83 | 191k | return _mm_unpacklo_epi16(tmp1, tmp1); // replicate samples |
84 | 191k | } |
85 | | |
86 | | // Convert 32 samples of YUV444 to R/G/B |
87 | | static void YUV444ToRGB_SSE41(const uint8_t* WEBP_RESTRICT const y, |
88 | | const uint8_t* WEBP_RESTRICT const u, |
89 | | const uint8_t* WEBP_RESTRICT const v, |
90 | | __m128i* const R, __m128i* const G, |
91 | 146M | __m128i* const B) { |
92 | 146M | const __m128i Y0 = Load_HI_16_SSE41(y), U0 = Load_HI_16_SSE41(u), |
93 | 146M | V0 = Load_HI_16_SSE41(v); |
94 | 146M | ConvertYUV444ToRGB_SSE41(&Y0, &U0, &V0, R, G, B); |
95 | 146M | } |
96 | | |
97 | | // Convert 32 samples of YUV420 to R/G/B |
98 | | static void YUV420ToRGB_SSE41(const uint8_t* WEBP_RESTRICT const y, |
99 | | const uint8_t* WEBP_RESTRICT const u, |
100 | | const uint8_t* WEBP_RESTRICT const v, |
101 | | __m128i* const R, __m128i* const G, |
102 | 95.9k | __m128i* const B) { |
103 | 95.9k | const __m128i Y0 = Load_HI_16_SSE41(y), U0 = Load_UV_HI_8_SSE41(u), |
104 | 95.9k | V0 = Load_UV_HI_8_SSE41(v); |
105 | 95.9k | ConvertYUV444ToRGB_SSE41(&Y0, &U0, &V0, R, G, B); |
106 | 95.9k | } |
107 | | |
108 | | // Pack the planar buffers |
109 | | // rrrr... rrrr... gggg... gggg... bbbb... bbbb.... |
110 | | // triplet by triplet in the output buffer rgb as rgbrgbrgbrgb ... |
111 | | static WEBP_INLINE void PlanarTo24b_SSE41( |
112 | | __m128i* const in0, __m128i* const in1, __m128i* const in2, |
113 | | __m128i* const in3, __m128i* const in4, __m128i* const in5, |
114 | 36.7M | uint8_t* WEBP_RESTRICT const rgb) { |
115 | | // The input is 6 registers of sixteen 8b but for the sake of explanation, |
116 | | // let's take 6 registers of four 8b values. |
117 | | // To pack, we will keep taking one every two 8b integer and move it |
118 | | // around as follows: |
119 | | // Input: |
120 | | // r0r1r2r3 | r4r5r6r7 | g0g1g2g3 | g4g5g6g7 | b0b1b2b3 | b4b5b6b7 |
121 | | // Split the 6 registers in two sets of 3 registers: the first set as the even |
122 | | // 8b bytes, the second the odd ones: |
123 | | // r0r2r4r6 | g0g2g4g6 | b0b2b4b6 | r1r3r5r7 | g1g3g5g7 | b1b3b5b7 |
124 | | // Repeat the same permutations twice more: |
125 | | // r0r4g0g4 | b0b4r1r5 | g1g5b1b5 | r2r6g2g6 | b2b6r3r7 | g3g7b3b7 |
126 | | // r0g0b0r1 | g1b1r2g2 | b2r3g3b3 | r4g4b4r5 | g5b5r6g6 | b6r7g7b7 |
127 | 36.7M | VP8PlanarTo24b_SSE41(in0, in1, in2, in3, in4, in5); |
128 | | |
129 | 36.7M | _mm_storeu_si128((__m128i*)(rgb + 0), *in0); |
130 | 36.7M | _mm_storeu_si128((__m128i*)(rgb + 16), *in1); |
131 | 36.7M | _mm_storeu_si128((__m128i*)(rgb + 32), *in2); |
132 | 36.7M | _mm_storeu_si128((__m128i*)(rgb + 48), *in3); |
133 | 36.7M | _mm_storeu_si128((__m128i*)(rgb + 64), *in4); |
134 | 36.7M | _mm_storeu_si128((__m128i*)(rgb + 80), *in5); |
135 | 36.7M | } |
136 | | |
137 | | void VP8YuvToRgb32_SSE41(const uint8_t* WEBP_RESTRICT y, |
138 | | const uint8_t* WEBP_RESTRICT u, |
139 | | const uint8_t* WEBP_RESTRICT v, |
140 | 10.6M | uint8_t* WEBP_RESTRICT dst) { |
141 | 10.6M | __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3; |
142 | 10.6M | __m128i rgb0, rgb1, rgb2, rgb3, rgb4, rgb5; |
143 | | |
144 | 10.6M | YUV444ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0); |
145 | 10.6M | YUV444ToRGB_SSE41(y + 8, u + 8, v + 8, &R1, &G1, &B1); |
146 | 10.6M | YUV444ToRGB_SSE41(y + 16, u + 16, v + 16, &R2, &G2, &B2); |
147 | 10.6M | YUV444ToRGB_SSE41(y + 24, u + 24, v + 24, &R3, &G3, &B3); |
148 | | |
149 | | // Cast to 8b and store as RRRRGGGGBBBB. |
150 | 10.6M | rgb0 = _mm_packus_epi16(R0, R1); |
151 | 10.6M | rgb1 = _mm_packus_epi16(R2, R3); |
152 | 10.6M | rgb2 = _mm_packus_epi16(G0, G1); |
153 | 10.6M | rgb3 = _mm_packus_epi16(G2, G3); |
154 | 10.6M | rgb4 = _mm_packus_epi16(B0, B1); |
155 | 10.6M | rgb5 = _mm_packus_epi16(B2, B3); |
156 | | |
157 | | // Pack as RGBRGBRGBRGB. |
158 | 10.6M | PlanarTo24b_SSE41(&rgb0, &rgb1, &rgb2, &rgb3, &rgb4, &rgb5, dst); |
159 | 10.6M | } |
160 | | |
161 | | void VP8YuvToBgr32_SSE41(const uint8_t* WEBP_RESTRICT y, |
162 | | const uint8_t* WEBP_RESTRICT u, |
163 | | const uint8_t* WEBP_RESTRICT v, |
164 | 26.0M | uint8_t* WEBP_RESTRICT dst) { |
165 | 26.0M | __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3; |
166 | 26.0M | __m128i bgr0, bgr1, bgr2, bgr3, bgr4, bgr5; |
167 | | |
168 | 26.0M | YUV444ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0); |
169 | 26.0M | YUV444ToRGB_SSE41(y + 8, u + 8, v + 8, &R1, &G1, &B1); |
170 | 26.0M | YUV444ToRGB_SSE41(y + 16, u + 16, v + 16, &R2, &G2, &B2); |
171 | 26.0M | YUV444ToRGB_SSE41(y + 24, u + 24, v + 24, &R3, &G3, &B3); |
172 | | |
173 | | // Cast to 8b and store as BBBBGGGGRRRR. |
174 | 26.0M | bgr0 = _mm_packus_epi16(B0, B1); |
175 | 26.0M | bgr1 = _mm_packus_epi16(B2, B3); |
176 | 26.0M | bgr2 = _mm_packus_epi16(G0, G1); |
177 | 26.0M | bgr3 = _mm_packus_epi16(G2, G3); |
178 | 26.0M | bgr4 = _mm_packus_epi16(R0, R1); |
179 | 26.0M | bgr5 = _mm_packus_epi16(R2, R3); |
180 | | |
181 | | // Pack as BGRBGRBGRBGR. |
182 | 26.0M | PlanarTo24b_SSE41(&bgr0, &bgr1, &bgr2, &bgr3, &bgr4, &bgr5, dst); |
183 | 26.0M | } |
184 | | |
185 | | //----------------------------------------------------------------------------- |
186 | | // Arbitrary-length row conversion functions |
187 | | |
188 | | static void YuvToRgbRow_SSE41(const uint8_t* WEBP_RESTRICT y, |
189 | | const uint8_t* WEBP_RESTRICT u, |
190 | | const uint8_t* WEBP_RESTRICT v, |
191 | 14.2k | uint8_t* WEBP_RESTRICT dst, int len) { |
192 | 14.2k | int n; |
193 | 24.5k | for (n = 0; n + 32 <= len; n += 32, dst += 32 * 3) { |
194 | 10.2k | __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3; |
195 | 10.2k | __m128i rgb0, rgb1, rgb2, rgb3, rgb4, rgb5; |
196 | | |
197 | 10.2k | YUV420ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0); |
198 | 10.2k | YUV420ToRGB_SSE41(y + 8, u + 4, v + 4, &R1, &G1, &B1); |
199 | 10.2k | YUV420ToRGB_SSE41(y + 16, u + 8, v + 8, &R2, &G2, &B2); |
200 | 10.2k | YUV420ToRGB_SSE41(y + 24, u + 12, v + 12, &R3, &G3, &B3); |
201 | | |
202 | | // Cast to 8b and store as RRRRGGGGBBBB. |
203 | 10.2k | rgb0 = _mm_packus_epi16(R0, R1); |
204 | 10.2k | rgb1 = _mm_packus_epi16(R2, R3); |
205 | 10.2k | rgb2 = _mm_packus_epi16(G0, G1); |
206 | 10.2k | rgb3 = _mm_packus_epi16(G2, G3); |
207 | 10.2k | rgb4 = _mm_packus_epi16(B0, B1); |
208 | 10.2k | rgb5 = _mm_packus_epi16(B2, B3); |
209 | | |
210 | | // Pack as RGBRGBRGBRGB. |
211 | 10.2k | PlanarTo24b_SSE41(&rgb0, &rgb1, &rgb2, &rgb3, &rgb4, &rgb5, dst); |
212 | | |
213 | 10.2k | y += 32; |
214 | 10.2k | u += 16; |
215 | 10.2k | v += 16; |
216 | 10.2k | } |
217 | 137k | for (; n < len; ++n) { // Finish off |
218 | 123k | VP8YuvToRgb(y[0], u[0], v[0], dst); |
219 | 123k | dst += 3; |
220 | 123k | y += 1; |
221 | 123k | u += (n & 1); |
222 | 123k | v += (n & 1); |
223 | 123k | } |
224 | 14.2k | } |
225 | | |
226 | | static void YuvToBgrRow_SSE41(const uint8_t* WEBP_RESTRICT y, |
227 | | const uint8_t* WEBP_RESTRICT u, |
228 | | const uint8_t* WEBP_RESTRICT v, |
229 | 14.5k | uint8_t* WEBP_RESTRICT dst, int len) { |
230 | 14.5k | int n; |
231 | 28.2k | for (n = 0; n + 32 <= len; n += 32, dst += 32 * 3) { |
232 | 13.7k | __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3; |
233 | 13.7k | __m128i bgr0, bgr1, bgr2, bgr3, bgr4, bgr5; |
234 | | |
235 | 13.7k | YUV420ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0); |
236 | 13.7k | YUV420ToRGB_SSE41(y + 8, u + 4, v + 4, &R1, &G1, &B1); |
237 | 13.7k | YUV420ToRGB_SSE41(y + 16, u + 8, v + 8, &R2, &G2, &B2); |
238 | 13.7k | YUV420ToRGB_SSE41(y + 24, u + 12, v + 12, &R3, &G3, &B3); |
239 | | |
240 | | // Cast to 8b and store as BBBBGGGGRRRR. |
241 | 13.7k | bgr0 = _mm_packus_epi16(B0, B1); |
242 | 13.7k | bgr1 = _mm_packus_epi16(B2, B3); |
243 | 13.7k | bgr2 = _mm_packus_epi16(G0, G1); |
244 | 13.7k | bgr3 = _mm_packus_epi16(G2, G3); |
245 | 13.7k | bgr4 = _mm_packus_epi16(R0, R1); |
246 | 13.7k | bgr5 = _mm_packus_epi16(R2, R3); |
247 | | |
248 | | // Pack as BGRBGRBGRBGR. |
249 | 13.7k | PlanarTo24b_SSE41(&bgr0, &bgr1, &bgr2, &bgr3, &bgr4, &bgr5, dst); |
250 | | |
251 | 13.7k | y += 32; |
252 | 13.7k | u += 16; |
253 | 13.7k | v += 16; |
254 | 13.7k | } |
255 | 180k | for (; n < len; ++n) { // Finish off |
256 | 166k | VP8YuvToBgr(y[0], u[0], v[0], dst); |
257 | 166k | dst += 3; |
258 | 166k | y += 1; |
259 | 166k | u += (n & 1); |
260 | 166k | v += (n & 1); |
261 | 166k | } |
262 | 14.5k | } |
263 | | |
264 | | //------------------------------------------------------------------------------ |
265 | | // Entry point |
266 | | |
267 | | extern void WebPInitSamplersSSE41(void); |
268 | | |
269 | 3.34k | WEBP_TSAN_IGNORE_FUNCTION void WebPInitSamplersSSE41(void) { |
270 | 3.34k | WebPSamplers[MODE_RGB] = YuvToRgbRow_SSE41; |
271 | 3.34k | WebPSamplers[MODE_BGR] = YuvToBgrRow_SSE41; |
272 | 3.34k | } |
273 | | |
274 | | //------------------------------------------------------------------------------ |
275 | | // RGB24/32 -> YUV converters |
276 | | |
277 | | // Load eight 16b-words from *src. |
278 | 176M | #define LOAD_16(src) _mm_loadu_si128((const __m128i*)(src)) |
279 | | // Store either 16b-words into *dst |
280 | 169M | #define STORE_16(V, dst) _mm_storeu_si128((__m128i*)(dst), (V)) |
281 | | |
282 | | #define WEBP_SSE41_SHUFF(OUT) \ |
283 | 2.49k | do { \ |
284 | 2.49k | const __m128i tmp0 = _mm_shuffle_epi8(A0, shuff0); \ |
285 | 2.49k | const __m128i tmp1 = _mm_shuffle_epi8(A1, shuff1); \ |
286 | 2.49k | const __m128i tmp2 = _mm_shuffle_epi8(A2, shuff2); \ |
287 | 2.49k | const __m128i tmp3 = _mm_shuffle_epi8(A3, shuff0); \ |
288 | 2.49k | const __m128i tmp4 = _mm_shuffle_epi8(A4, shuff1); \ |
289 | 2.49k | const __m128i tmp5 = _mm_shuffle_epi8(A5, shuff2); \ |
290 | 2.49k | \ |
291 | 2.49k | /* OR everything to get one channel */ \ |
292 | 2.49k | const __m128i tmp6 = _mm_or_si128(tmp0, tmp1); \ |
293 | 2.49k | const __m128i tmp7 = _mm_or_si128(tmp3, tmp4); \ |
294 | 2.49k | out[OUT + 0] = _mm_or_si128(tmp6, tmp2); \ |
295 | 2.49k | out[OUT + 1] = _mm_or_si128(tmp7, tmp5); \ |
296 | 2.49k | } while (0); |
297 | | |
298 | | // Unpack the 8b input rgbrgbrgbrgb ... as contiguous registers: |
299 | | // rrrr... rrrr... gggg... gggg... bbbb... bbbb.... |
300 | | // Similar to PlanarTo24bHelper(), but in reverse order. |
301 | | static WEBP_INLINE void RGBPackedToPlanar_SSE41( |
302 | 832 | const uint8_t* WEBP_RESTRICT const rgb, __m128i* const out /*out[6]*/) { |
303 | 832 | const __m128i A0 = _mm_loadu_si128((const __m128i*)(rgb + 0)); |
304 | 832 | const __m128i A1 = _mm_loadu_si128((const __m128i*)(rgb + 16)); |
305 | 832 | const __m128i A2 = _mm_loadu_si128((const __m128i*)(rgb + 32)); |
306 | 832 | const __m128i A3 = _mm_loadu_si128((const __m128i*)(rgb + 48)); |
307 | 832 | const __m128i A4 = _mm_loadu_si128((const __m128i*)(rgb + 64)); |
308 | 832 | const __m128i A5 = _mm_loadu_si128((const __m128i*)(rgb + 80)); |
309 | | |
310 | | // Compute RR. |
311 | 832 | { |
312 | 832 | const __m128i shuff0 = _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
313 | 832 | 15, 12, 9, 6, 3, 0); |
314 | 832 | const __m128i shuff1 = _mm_set_epi8(-1, -1, -1, -1, -1, 14, 11, 8, 5, 2, -1, |
315 | 832 | -1, -1, -1, -1, -1); |
316 | 832 | const __m128i shuff2 = _mm_set_epi8(13, 10, 7, 4, 1, -1, -1, -1, -1, -1, -1, |
317 | 832 | -1, -1, -1, -1, -1); |
318 | 832 | WEBP_SSE41_SHUFF(0) |
319 | 832 | } |
320 | | // Compute GG. |
321 | 832 | { |
322 | 832 | const __m128i shuff0 = _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
323 | 832 | -1, 13, 10, 7, 4, 1); |
324 | 832 | const __m128i shuff1 = _mm_set_epi8(-1, -1, -1, -1, -1, 15, 12, 9, 6, 3, 0, |
325 | 832 | -1, -1, -1, -1, -1); |
326 | 832 | const __m128i shuff2 = _mm_set_epi8(14, 11, 8, 5, 2, -1, -1, -1, -1, -1, -1, |
327 | 832 | -1, -1, -1, -1, -1); |
328 | 832 | WEBP_SSE41_SHUFF(2) |
329 | 832 | } |
330 | | // Compute BB. |
331 | 832 | { |
332 | 832 | const __m128i shuff0 = _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
333 | 832 | -1, 14, 11, 8, 5, 2); |
334 | 832 | const __m128i shuff1 = _mm_set_epi8(-1, -1, -1, -1, -1, -1, 13, 10, 7, 4, 1, |
335 | 832 | -1, -1, -1, -1, -1); |
336 | 832 | const __m128i shuff2 = _mm_set_epi8(15, 12, 9, 6, 3, 0, -1, -1, -1, -1, -1, |
337 | 832 | -1, -1, -1, -1, -1); |
338 | 832 | WEBP_SSE41_SHUFF(4) |
339 | 832 | } |
340 | 832 | } |
341 | | |
342 | | #undef WEBP_SSE41_SHUFF |
343 | | |
344 | | static WEBP_INLINE void RGBAPackedToRGBPlanar_SSE41( |
345 | 41.6M | const uint8_t* WEBP_RESTRICT const rgba, __m128i* const rgb /*in[6]*/) { |
346 | 41.6M | __m128i a0 = _mm_loadu_si128((const __m128i*)(rgba + 0)); |
347 | 41.6M | __m128i a1 = _mm_loadu_si128((const __m128i*)(rgba + 16)); |
348 | 41.6M | __m128i a2 = _mm_loadu_si128((const __m128i*)(rgba + 32)); |
349 | 41.6M | __m128i a3 = _mm_loadu_si128((const __m128i*)(rgba + 48)); |
350 | 41.6M | __m128i a4 = _mm_loadu_si128((const __m128i*)(rgba + 64)); |
351 | 41.6M | __m128i a5 = _mm_loadu_si128((const __m128i*)(rgba + 80)); |
352 | 41.6M | __m128i a6 = _mm_loadu_si128((const __m128i*)(rgba + 96)); |
353 | 41.6M | __m128i a7 = _mm_loadu_si128((const __m128i*)(rgba + 112)); |
354 | 41.6M | VP8L32bToPlanar_SSE41(&a0, &a1, &a2, &a3); |
355 | 41.6M | rgb[0] = a3; |
356 | 41.6M | rgb[2] = a2; |
357 | 41.6M | rgb[4] = a1; |
358 | 41.6M | VP8L32bToPlanar_SSE41(&a4, &a5, &a6, &a7); |
359 | 41.6M | rgb[1] = a7; |
360 | 41.6M | rgb[3] = a6; |
361 | 41.6M | rgb[5] = a5; |
362 | 41.6M | } |
363 | | |
364 | | // Unpack the 8b input argbargbargb... as contiguous registers: |
365 | | // 0r0r0r... 0r0r0r... 0g0g0g... 0g0g0g0... 0b0b0b... 0b0b0b.... |
366 | | static WEBP_INLINE void ARGBPackedToRGBPlanar16_SSE41( |
367 | 43.9M | const uint32_t* WEBP_RESTRICT const argb, __m128i* const rgb /*in[6]*/) { |
368 | 43.9M | const __m128i zero = _mm_setzero_si128(); |
369 | 43.9M | __m128i a0 = _mm_loadu_si128((const __m128i*)(argb + 0)); |
370 | 43.9M | __m128i a1 = _mm_loadu_si128((const __m128i*)(argb + 4)); |
371 | 43.9M | __m128i a2 = _mm_loadu_si128((const __m128i*)(argb + 8)); |
372 | 43.9M | __m128i a3 = _mm_loadu_si128((const __m128i*)(argb + 12)); |
373 | 43.9M | VP8L32bToPlanar_SSE41(&a0, &a1, &a2, &a3); |
374 | 43.9M | rgb[0] = _mm_unpacklo_epi8(a1, zero); |
375 | 43.9M | rgb[1] = _mm_unpackhi_epi8(a1, zero); |
376 | 43.9M | rgb[2] = _mm_unpacklo_epi8(a2, zero); |
377 | 43.9M | rgb[3] = _mm_unpackhi_epi8(a2, zero); |
378 | 43.9M | rgb[4] = _mm_unpacklo_epi8(a3, zero); |
379 | 43.9M | rgb[5] = _mm_unpackhi_epi8(a3, zero); |
380 | 43.9M | } |
381 | | |
382 | | // This macro computes (RG * MULT_RG + GB * MULT_GB + ROUNDER) >> DESCALE_FIX |
383 | | // It's a macro and not a function because we need to use immediate values with |
384 | | // srai_epi32, e.g. |
385 | | #define TRANSFORM(RG_LO, RG_HI, GB_LO, GB_HI, MULT_RG, MULT_GB, ROUNDER, \ |
386 | | DESCALE_FIX, OUT) \ |
387 | 338M | do { \ |
388 | 338M | const __m128i V0_lo = _mm_madd_epi16(RG_LO, MULT_RG); \ |
389 | 338M | const __m128i V0_hi = _mm_madd_epi16(RG_HI, MULT_RG); \ |
390 | 338M | const __m128i V1_lo = _mm_madd_epi16(GB_LO, MULT_GB); \ |
391 | 338M | const __m128i V1_hi = _mm_madd_epi16(GB_HI, MULT_GB); \ |
392 | 338M | const __m128i V2_lo = _mm_add_epi32(V0_lo, V1_lo); \ |
393 | 338M | const __m128i V2_hi = _mm_add_epi32(V0_hi, V1_hi); \ |
394 | 338M | const __m128i V3_lo = _mm_add_epi32(V2_lo, ROUNDER); \ |
395 | 338M | const __m128i V3_hi = _mm_add_epi32(V2_hi, ROUNDER); \ |
396 | 338M | const __m128i V5_lo = _mm_srai_epi32(V3_lo, DESCALE_FIX); \ |
397 | 338M | const __m128i V5_hi = _mm_srai_epi32(V3_hi, DESCALE_FIX); \ |
398 | 338M | (OUT) = _mm_packs_epi32(V5_lo, V5_hi); \ |
399 | 338M | } while (0) |
400 | | |
401 | 676M | #define MK_CST_16(A, B) _mm_set_epi16((B), (A), (B), (A), (B), (A), (B), (A)) |
402 | | static WEBP_INLINE void ConvertRGBToYImpl_SSE41(const __m128i* const R, |
403 | | const __m128i* const G, |
404 | | const __m128i* const B, |
405 | 211M | __m128i* const Y) { |
406 | 211M | const __m128i kRG_y = MK_CST_16(16839, 33059 - 16384); |
407 | 211M | const __m128i kGB_y = MK_CST_16(16384, 6420); |
408 | 211M | const __m128i kHALF_Y = _mm_set1_epi32((16 << YUV_FIX) + YUV_HALF); |
409 | | |
410 | 211M | const __m128i RG_lo = _mm_unpacklo_epi16(*R, *G); |
411 | 211M | const __m128i RG_hi = _mm_unpackhi_epi16(*R, *G); |
412 | 211M | const __m128i GB_lo = _mm_unpacklo_epi16(*G, *B); |
413 | 211M | const __m128i GB_hi = _mm_unpackhi_epi16(*G, *B); |
414 | 211M | TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_y, kGB_y, kHALF_Y, YUV_FIX, *Y); |
415 | 211M | } |
416 | | |
417 | | static WEBP_INLINE void ConvertRGBToUV_SSE41(const __m128i* const R, |
418 | | const __m128i* const G, |
419 | | const __m128i* const B, |
420 | | __m128i* const U, |
421 | 63.4M | __m128i* const V) { |
422 | 63.4M | const __m128i kRG_u = MK_CST_16(-9719, -19081); |
423 | 63.4M | const __m128i kGB_u = MK_CST_16(0, 28800); |
424 | 63.4M | const __m128i kRG_v = MK_CST_16(28800, 0); |
425 | 63.4M | const __m128i kGB_v = MK_CST_16(-24116, -4684); |
426 | 63.4M | const __m128i kHALF_UV = _mm_set1_epi32(((128 << YUV_FIX) + YUV_HALF) << 2); |
427 | | |
428 | 63.4M | const __m128i RG_lo = _mm_unpacklo_epi16(*R, *G); |
429 | 63.4M | const __m128i RG_hi = _mm_unpackhi_epi16(*R, *G); |
430 | 63.4M | const __m128i GB_lo = _mm_unpacklo_epi16(*G, *B); |
431 | 63.4M | const __m128i GB_hi = _mm_unpackhi_epi16(*G, *B); |
432 | 63.4M | TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_u, kGB_u, kHALF_UV, YUV_FIX + 2, |
433 | 63.4M | *U); |
434 | 63.4M | TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_v, kGB_v, kHALF_UV, YUV_FIX + 2, |
435 | 63.4M | *V); |
436 | 63.4M | } |
437 | | |
438 | | #undef MK_CST_16 |
439 | | #undef TRANSFORM |
440 | | |
441 | | static WEBP_INLINE void ConvertRGBToYHelper_SSE41( |
442 | | const __m128i* const rgb_plane /*in[6]*/, int swap_rb, int* i, |
443 | 41.6M | uint8_t* WEBP_RESTRICT y) { |
444 | 41.6M | int j; |
445 | | |
446 | 125M | for (j = 0; j < 2; ++j, *i += 16) { |
447 | 83.3M | const __m128i zero = _mm_setzero_si128(); |
448 | 83.3M | __m128i r, g, b, Y0, Y1; |
449 | | |
450 | | // Convert to 16-bit Y. |
451 | 83.3M | r = _mm_unpacklo_epi8(rgb_plane[(swap_rb ? 4 : 0) + j], zero); |
452 | 83.3M | g = _mm_unpacklo_epi8(rgb_plane[2 + j], zero); |
453 | 83.3M | b = _mm_unpacklo_epi8(rgb_plane[(swap_rb ? 0 : 4) + j], zero); |
454 | 83.3M | ConvertRGBToYImpl_SSE41(&r, &g, &b, &Y0); |
455 | | |
456 | | // Convert to 16-bit Y. |
457 | 83.3M | r = _mm_unpackhi_epi8(rgb_plane[(swap_rb ? 4 : 0) + j], zero); |
458 | 83.3M | g = _mm_unpackhi_epi8(rgb_plane[2 + j], zero); |
459 | 83.3M | b = _mm_unpackhi_epi8(rgb_plane[(swap_rb ? 0 : 4) + j], zero); |
460 | 83.3M | ConvertRGBToYImpl_SSE41(&r, &g, &b, &Y1); |
461 | | |
462 | | // Cast to 8-bit and store. |
463 | 83.3M | STORE_16(_mm_packus_epi16(Y0, Y1), y + *i); |
464 | 83.3M | } |
465 | 41.6M | } |
466 | | |
467 | | static void ConvertRGBToY_SSE41(const uint8_t* WEBP_RESTRICT rgb, |
468 | 428k | uint8_t* WEBP_RESTRICT y, int width, int step) { |
469 | 428k | const int max_width = width & ~31; |
470 | 428k | int i; |
471 | 428k | __m128i rgb_plane[6]; |
472 | 428k | if (step == 3) { |
473 | 19.9k | for (i = 0; i < max_width; rgb += 3 * 16 * 2) { |
474 | 832 | RGBPackedToPlanar_SSE41(rgb, rgb_plane); |
475 | 832 | ConvertRGBToYHelper_SSE41(rgb_plane, /*swap_rb=*/0, &i, y); |
476 | 832 | } |
477 | 408k | } else { |
478 | 1.74M | for (i = 0; i < max_width; rgb += 4 * 16 * 2) { |
479 | 1.33M | RGBAPackedToRGBPlanar_SSE41(rgb, rgb_plane); |
480 | 1.33M | ConvertRGBToYHelper_SSE41(rgb_plane, /*swap_rb=*/0, &i, y); |
481 | 1.33M | } |
482 | 408k | } |
483 | 645k | for (; i < width; ++i, rgb += step) { // left-over |
484 | 217k | y[i] = VP8RGBToY(rgb[0], rgb[1], rgb[2], YUV_HALF); |
485 | 217k | } |
486 | 428k | } |
487 | | |
488 | | static void ConvertBGRToY_SSE41(const uint8_t* WEBP_RESTRICT bgr, |
489 | 1.72M | uint8_t* WEBP_RESTRICT y, int width, int step) { |
490 | 1.72M | const int max_width = width & ~31; |
491 | 1.72M | int i; |
492 | 1.72M | __m128i bgr_plane[6]; |
493 | 1.72M | if (step == 3) { |
494 | 0 | for (i = 0; i < max_width; bgr += 3 * 16 * 2) { |
495 | 0 | RGBPackedToPlanar_SSE41(bgr, bgr_plane); |
496 | 0 | ConvertRGBToYHelper_SSE41(bgr_plane, /*swap_rb=*/1, &i, y); |
497 | 0 | } |
498 | 1.72M | } else { |
499 | 42.0M | for (i = 0; i < max_width; bgr += 4 * 16 * 2) { |
500 | 40.3M | RGBAPackedToRGBPlanar_SSE41(bgr, bgr_plane); |
501 | 40.3M | ConvertRGBToYHelper_SSE41(bgr_plane, /*swap_rb=*/1, &i, y); |
502 | 40.3M | } |
503 | 1.72M | } |
504 | 13.7M | for (; i < width; ++i, bgr += step) { // left-over |
505 | 12.0M | y[i] = VP8RGBToY(bgr[2], bgr[1], bgr[0], YUV_HALF); |
506 | 12.0M | } |
507 | 1.72M | } |
508 | | |
509 | | static void ConvertARGBToY_SSE41(const uint32_t* WEBP_RESTRICT argb, |
510 | 8.98M | uint8_t* WEBP_RESTRICT y, int width) { |
511 | 8.98M | const int max_width = width & ~15; |
512 | 8.98M | int i; |
513 | 31.2M | for (i = 0; i < max_width; i += 16) { |
514 | 22.2M | __m128i Y0, Y1, rgb[6]; |
515 | 22.2M | ARGBPackedToRGBPlanar16_SSE41(&argb[i], rgb); |
516 | 22.2M | ConvertRGBToYImpl_SSE41(&rgb[0], &rgb[2], &rgb[4], &Y0); |
517 | 22.2M | ConvertRGBToYImpl_SSE41(&rgb[1], &rgb[3], &rgb[5], &Y1); |
518 | 22.2M | STORE_16(_mm_packus_epi16(Y0, Y1), y + i); |
519 | 22.2M | } |
520 | 59.8M | for (; i < width; ++i) { // left-over |
521 | 50.8M | const uint32_t p = argb[i]; |
522 | 50.8M | y[i] = |
523 | 50.8M | VP8RGBToY((p >> 16) & 0xff, (p >> 8) & 0xff, (p >> 0) & 0xff, YUV_HALF); |
524 | 50.8M | } |
525 | 8.98M | } |
526 | | |
527 | | // Horizontal add (doubled) of two 16b values, result is 16b. |
528 | | // in: A | B | C | D | ... -> out: 2*(A+B) | 2*(C+D) | ... |
529 | | static void HorizontalAddPack_SSE41(const __m128i* const A, |
530 | | const __m128i* const B, |
531 | 65.1M | __m128i* const out) { |
532 | 65.1M | const __m128i k2 = _mm_set1_epi16(2); |
533 | 65.1M | const __m128i C = _mm_madd_epi16(*A, k2); |
534 | 65.1M | const __m128i D = _mm_madd_epi16(*B, k2); |
535 | 65.1M | *out = _mm_packs_epi32(C, D); |
536 | 65.1M | } |
537 | | |
538 | | static void ConvertARGBToUV_SSE41(const uint32_t* WEBP_RESTRICT argb, |
539 | | uint8_t* WEBP_RESTRICT u, |
540 | | uint8_t* WEBP_RESTRICT v, int src_width, |
541 | 8.98M | int do_store) { |
542 | 8.98M | const int max_width = src_width & ~31; |
543 | 8.98M | int i; |
544 | 19.8M | for (i = 0; i < max_width; i += 32, u += 16, v += 16) { |
545 | 10.8M | __m128i rgb[6], U0, V0, U1, V1; |
546 | 10.8M | ARGBPackedToRGBPlanar16_SSE41(&argb[i], rgb); |
547 | 10.8M | HorizontalAddPack_SSE41(&rgb[0], &rgb[1], &rgb[0]); |
548 | 10.8M | HorizontalAddPack_SSE41(&rgb[2], &rgb[3], &rgb[2]); |
549 | 10.8M | HorizontalAddPack_SSE41(&rgb[4], &rgb[5], &rgb[4]); |
550 | 10.8M | ConvertRGBToUV_SSE41(&rgb[0], &rgb[2], &rgb[4], &U0, &V0); |
551 | | |
552 | 10.8M | ARGBPackedToRGBPlanar16_SSE41(&argb[i + 16], rgb); |
553 | 10.8M | HorizontalAddPack_SSE41(&rgb[0], &rgb[1], &rgb[0]); |
554 | 10.8M | HorizontalAddPack_SSE41(&rgb[2], &rgb[3], &rgb[2]); |
555 | 10.8M | HorizontalAddPack_SSE41(&rgb[4], &rgb[5], &rgb[4]); |
556 | 10.8M | ConvertRGBToUV_SSE41(&rgb[0], &rgb[2], &rgb[4], &U1, &V1); |
557 | | |
558 | 10.8M | U0 = _mm_packus_epi16(U0, U1); |
559 | 10.8M | V0 = _mm_packus_epi16(V0, V1); |
560 | 10.8M | if (!do_store) { |
561 | 4.78M | const __m128i prev_u = LOAD_16(u); |
562 | 4.78M | const __m128i prev_v = LOAD_16(v); |
563 | 4.78M | U0 = _mm_avg_epu8(U0, prev_u); |
564 | 4.78M | V0 = _mm_avg_epu8(V0, prev_v); |
565 | 4.78M | } |
566 | 10.8M | STORE_16(U0, u); |
567 | 10.8M | STORE_16(V0, v); |
568 | 10.8M | } |
569 | 8.98M | if (i < src_width) { // left-over |
570 | 8.97M | WebPConvertARGBToUV_C(argb + i, u, v, src_width - i, do_store); |
571 | 8.97M | } |
572 | 8.98M | } |
573 | | |
574 | | // Convert 16 packed ARGB 16b-values to r[], g[], b[] |
575 | | static WEBP_INLINE void RGBA32PackedToPlanar_16b_SSE41( |
576 | | const uint16_t* WEBP_RESTRICT const rgbx, __m128i* const r, |
577 | 41.7M | __m128i* const g, __m128i* const b) { |
578 | 41.7M | const __m128i in0 = LOAD_16(rgbx + 0); // r0 | g0 | b0 |x| r1 | g1 | b1 |x |
579 | 41.7M | const __m128i in1 = LOAD_16(rgbx + 8); // r2 | g2 | b2 |x| r3 | g3 | b3 |x |
580 | 41.7M | const __m128i in2 = LOAD_16(rgbx + 16); // r4 | ... |
581 | 41.7M | const __m128i in3 = LOAD_16(rgbx + 24); // r6 | ... |
582 | | // aarrggbb as 16-bit. |
583 | 41.7M | const __m128i shuff0 = |
584 | 41.7M | _mm_set_epi8(-1, -1, -1, -1, 13, 12, 5, 4, 11, 10, 3, 2, 9, 8, 1, 0); |
585 | 41.7M | const __m128i shuff1 = |
586 | 41.7M | _mm_set_epi8(13, 12, 5, 4, -1, -1, -1, -1, 11, 10, 3, 2, 9, 8, 1, 0); |
587 | 41.7M | const __m128i A0 = _mm_shuffle_epi8(in0, shuff0); |
588 | 41.7M | const __m128i A1 = _mm_shuffle_epi8(in1, shuff1); |
589 | 41.7M | const __m128i A2 = _mm_shuffle_epi8(in2, shuff0); |
590 | 41.7M | const __m128i A3 = _mm_shuffle_epi8(in3, shuff1); |
591 | | // R0R1G0G1 |
592 | | // B0B1**** |
593 | | // R2R3G2G3 |
594 | | // B2B3**** |
595 | | // (OR is used to free port 5 for the unpack) |
596 | 41.7M | const __m128i B0 = _mm_unpacklo_epi32(A0, A1); |
597 | 41.7M | const __m128i B1 = _mm_or_si128(A0, A1); |
598 | 41.7M | const __m128i B2 = _mm_unpacklo_epi32(A2, A3); |
599 | 41.7M | const __m128i B3 = _mm_or_si128(A2, A3); |
600 | | // Gather the channels. |
601 | 41.7M | *r = _mm_unpacklo_epi64(B0, B2); |
602 | 41.7M | *g = _mm_unpackhi_epi64(B0, B2); |
603 | 41.7M | *b = _mm_unpackhi_epi64(B1, B3); |
604 | 41.7M | } |
605 | | |
606 | | static void ConvertRGBA32ToUV_SSE41(const uint16_t* WEBP_RESTRICT rgb, |
607 | | uint8_t* WEBP_RESTRICT u, |
608 | 1.07M | uint8_t* WEBP_RESTRICT v, int width) { |
609 | 1.07M | const int max_width = width & ~15; |
610 | 1.07M | const uint16_t* const last_rgb = rgb + 4 * max_width; |
611 | 21.9M | while (rgb < last_rgb) { |
612 | 20.8M | __m128i r, g, b, U0, V0, U1, V1; |
613 | 20.8M | RGBA32PackedToPlanar_16b_SSE41(rgb + 0, &r, &g, &b); |
614 | 20.8M | ConvertRGBToUV_SSE41(&r, &g, &b, &U0, &V0); |
615 | 20.8M | RGBA32PackedToPlanar_16b_SSE41(rgb + 32, &r, &g, &b); |
616 | 20.8M | ConvertRGBToUV_SSE41(&r, &g, &b, &U1, &V1); |
617 | 20.8M | STORE_16(_mm_packus_epi16(U0, U1), u); |
618 | 20.8M | STORE_16(_mm_packus_epi16(V0, V1), v); |
619 | 20.8M | u += 16; |
620 | 20.8M | v += 16; |
621 | 20.8M | rgb += 2 * 32; |
622 | 20.8M | } |
623 | 1.07M | if (max_width < width) { // left-over |
624 | 814k | WebPConvertRGBA32ToUV_C(rgb, u, v, width - max_width); |
625 | 814k | } |
626 | 1.07M | } |
627 | | |
628 | | //------------------------------------------------------------------------------ |
629 | | |
630 | | extern void WebPInitConvertARGBToYUVSSE41(void); |
631 | | |
632 | 6.70k | WEBP_TSAN_IGNORE_FUNCTION void WebPInitConvertARGBToYUVSSE41(void) { |
633 | 6.70k | WebPConvertARGBToY = ConvertARGBToY_SSE41; |
634 | 6.70k | WebPConvertARGBToUV = ConvertARGBToUV_SSE41; |
635 | | |
636 | 6.70k | WebPConvertRGBToY = ConvertRGBToY_SSE41; |
637 | 6.70k | WebPConvertBGRToY = ConvertBGRToY_SSE41; |
638 | | |
639 | 6.70k | WebPConvertRGBA32ToUV = ConvertRGBA32ToUV_SSE41; |
640 | 6.70k | } |
641 | | |
642 | | //------------------------------------------------------------------------------ |
643 | | |
644 | | #else // !WEBP_USE_SSE41 |
645 | | |
646 | | WEBP_DSP_INIT_STUB(WebPInitSamplersSSE41) |
647 | | WEBP_DSP_INIT_STUB(WebPInitConvertARGBToYUVSSE41) |
648 | | |
649 | | #endif // WEBP_USE_SSE41 |