Coverage Report

Created: 2026-01-20 07:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
2.21M
                                     __m128i* const G, __m128i* const B) {
39
2.21M
  const __m128i k19077 = _mm_set1_epi16(19077);
40
2.21M
  const __m128i k26149 = _mm_set1_epi16(26149);
41
2.21M
  const __m128i k14234 = _mm_set1_epi16(14234);
42
  // 33050 doesn't fit in a signed short: only use this with unsigned arithmetic
43
2.21M
  const __m128i k33050 = _mm_set1_epi16((short)33050);
44
2.21M
  const __m128i k17685 = _mm_set1_epi16(17685);
45
2.21M
  const __m128i k6419 = _mm_set1_epi16(6419);
46
2.21M
  const __m128i k13320 = _mm_set1_epi16(13320);
47
2.21M
  const __m128i k8708 = _mm_set1_epi16(8708);
48
49
2.21M
  const __m128i Y1 = _mm_mulhi_epu16(*Y0, k19077);
50
51
2.21M
  const __m128i R0 = _mm_mulhi_epu16(*V0, k26149);
52
2.21M
  const __m128i R1 = _mm_sub_epi16(Y1, k14234);
53
2.21M
  const __m128i R2 = _mm_add_epi16(R1, R0);
54
55
2.21M
  const __m128i G0 = _mm_mulhi_epu16(*U0, k6419);
56
2.21M
  const __m128i G1 = _mm_mulhi_epu16(*V0, k13320);
57
2.21M
  const __m128i G2 = _mm_add_epi16(Y1, k8708);
58
2.21M
  const __m128i G3 = _mm_add_epi16(G0, G1);
59
2.21M
  const __m128i G4 = _mm_sub_epi16(G2, G3);
60
61
  // be careful with the saturated *unsigned* arithmetic here!
62
2.21M
  const __m128i B0 = _mm_mulhi_epu16(*U0, k33050);
63
2.21M
  const __m128i B1 = _mm_adds_epu16(B0, Y1);
64
2.21M
  const __m128i B2 = _mm_subs_epu16(B1, k17685);
65
66
  // use logical shift for B2, which can be larger than 32767
67
2.21M
  *R = _mm_srai_epi16(R2, 6);  // range: [-14234, 30815]
68
2.21M
  *G = _mm_srai_epi16(G4, 6);  // range: [-10953, 27710]
69
2.21M
  *B = _mm_srli_epi16(B2, 6);  // range: [0, 34238]
70
2.21M
}
71
72
// Load the bytes into the *upper* part of 16b words. That's "<< 8", basically.
73
6.65M
static WEBP_INLINE __m128i Load_HI_16_SSE41(const uint8_t* src) {
74
6.65M
  const __m128i zero = _mm_setzero_si128();
75
6.65M
  return _mm_unpacklo_epi8(zero, _mm_loadl_epi64((const __m128i*)src));
76
6.65M
}
77
78
// Load and replicate the U/V samples
79
0
static WEBP_INLINE __m128i Load_UV_HI_8_SSE41(const uint8_t* src) {
80
0
  const __m128i zero = _mm_setzero_si128();
81
0
  const __m128i tmp0 = _mm_cvtsi32_si128(WebPMemToInt32(src));
82
0
  const __m128i tmp1 = _mm_unpacklo_epi8(zero, tmp0);
83
0
  return _mm_unpacklo_epi16(tmp1, tmp1);  // replicate samples
84
0
}
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
2.21M
                              __m128i* const B) {
92
2.21M
  const __m128i Y0 = Load_HI_16_SSE41(y), U0 = Load_HI_16_SSE41(u),
93
2.21M
                V0 = Load_HI_16_SSE41(v);
94
2.21M
  ConvertYUV444ToRGB_SSE41(&Y0, &U0, &V0, R, G, B);
95
2.21M
}
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
0
                              __m128i* const B) {
103
0
  const __m128i Y0 = Load_HI_16_SSE41(y), U0 = Load_UV_HI_8_SSE41(u),
104
0
                V0 = Load_UV_HI_8_SSE41(v);
105
0
  ConvertYUV444ToRGB_SSE41(&Y0, &U0, &V0, R, G, B);
106
0
}
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
554k
    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
554k
  VP8PlanarTo24b_SSE41(in0, in1, in2, in3, in4, in5);
128
129
554k
  _mm_storeu_si128((__m128i*)(rgb + 0), *in0);
130
554k
  _mm_storeu_si128((__m128i*)(rgb + 16), *in1);
131
554k
  _mm_storeu_si128((__m128i*)(rgb + 32), *in2);
132
554k
  _mm_storeu_si128((__m128i*)(rgb + 48), *in3);
133
554k
  _mm_storeu_si128((__m128i*)(rgb + 64), *in4);
134
554k
  _mm_storeu_si128((__m128i*)(rgb + 80), *in5);
135
554k
}
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
554k
                         uint8_t* WEBP_RESTRICT dst) {
141
554k
  __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
142
554k
  __m128i rgb0, rgb1, rgb2, rgb3, rgb4, rgb5;
143
144
554k
  YUV444ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0);
145
554k
  YUV444ToRGB_SSE41(y + 8, u + 8, v + 8, &R1, &G1, &B1);
146
554k
  YUV444ToRGB_SSE41(y + 16, u + 16, v + 16, &R2, &G2, &B2);
147
554k
  YUV444ToRGB_SSE41(y + 24, u + 24, v + 24, &R3, &G3, &B3);
148
149
  // Cast to 8b and store as RRRRGGGGBBBB.
150
554k
  rgb0 = _mm_packus_epi16(R0, R1);
151
554k
  rgb1 = _mm_packus_epi16(R2, R3);
152
554k
  rgb2 = _mm_packus_epi16(G0, G1);
153
554k
  rgb3 = _mm_packus_epi16(G2, G3);
154
554k
  rgb4 = _mm_packus_epi16(B0, B1);
155
554k
  rgb5 = _mm_packus_epi16(B2, B3);
156
157
  // Pack as RGBRGBRGBRGB.
158
554k
  PlanarTo24b_SSE41(&rgb0, &rgb1, &rgb2, &rgb3, &rgb4, &rgb5, dst);
159
554k
}
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
0
                         uint8_t* WEBP_RESTRICT dst) {
165
0
  __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
166
0
  __m128i bgr0, bgr1, bgr2, bgr3, bgr4, bgr5;
167
168
0
  YUV444ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0);
169
0
  YUV444ToRGB_SSE41(y + 8, u + 8, v + 8, &R1, &G1, &B1);
170
0
  YUV444ToRGB_SSE41(y + 16, u + 16, v + 16, &R2, &G2, &B2);
171
0
  YUV444ToRGB_SSE41(y + 24, u + 24, v + 24, &R3, &G3, &B3);
172
173
  // Cast to 8b and store as BBBBGGGGRRRR.
174
0
  bgr0 = _mm_packus_epi16(B0, B1);
175
0
  bgr1 = _mm_packus_epi16(B2, B3);
176
0
  bgr2 = _mm_packus_epi16(G0, G1);
177
0
  bgr3 = _mm_packus_epi16(G2, G3);
178
0
  bgr4 = _mm_packus_epi16(R0, R1);
179
0
  bgr5 = _mm_packus_epi16(R2, R3);
180
181
  // Pack as BGRBGRBGRBGR.
182
0
  PlanarTo24b_SSE41(&bgr0, &bgr1, &bgr2, &bgr3, &bgr4, &bgr5, dst);
183
0
}
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
0
                              uint8_t* WEBP_RESTRICT dst, int len) {
192
0
  int n;
193
0
  for (n = 0; n + 32 <= len; n += 32, dst += 32 * 3) {
194
0
    __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
195
0
    __m128i rgb0, rgb1, rgb2, rgb3, rgb4, rgb5;
196
197
0
    YUV420ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0);
198
0
    YUV420ToRGB_SSE41(y + 8, u + 4, v + 4, &R1, &G1, &B1);
199
0
    YUV420ToRGB_SSE41(y + 16, u + 8, v + 8, &R2, &G2, &B2);
200
0
    YUV420ToRGB_SSE41(y + 24, u + 12, v + 12, &R3, &G3, &B3);
201
202
    // Cast to 8b and store as RRRRGGGGBBBB.
203
0
    rgb0 = _mm_packus_epi16(R0, R1);
204
0
    rgb1 = _mm_packus_epi16(R2, R3);
205
0
    rgb2 = _mm_packus_epi16(G0, G1);
206
0
    rgb3 = _mm_packus_epi16(G2, G3);
207
0
    rgb4 = _mm_packus_epi16(B0, B1);
208
0
    rgb5 = _mm_packus_epi16(B2, B3);
209
210
    // Pack as RGBRGBRGBRGB.
211
0
    PlanarTo24b_SSE41(&rgb0, &rgb1, &rgb2, &rgb3, &rgb4, &rgb5, dst);
212
213
0
    y += 32;
214
0
    u += 16;
215
0
    v += 16;
216
0
  }
217
0
  for (; n < len; ++n) {  // Finish off
218
0
    VP8YuvToRgb(y[0], u[0], v[0], dst);
219
0
    dst += 3;
220
0
    y += 1;
221
0
    u += (n & 1);
222
0
    v += (n & 1);
223
0
  }
224
0
}
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
0
                              uint8_t* WEBP_RESTRICT dst, int len) {
230
0
  int n;
231
0
  for (n = 0; n + 32 <= len; n += 32, dst += 32 * 3) {
232
0
    __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
233
0
    __m128i bgr0, bgr1, bgr2, bgr3, bgr4, bgr5;
234
235
0
    YUV420ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0);
236
0
    YUV420ToRGB_SSE41(y + 8, u + 4, v + 4, &R1, &G1, &B1);
237
0
    YUV420ToRGB_SSE41(y + 16, u + 8, v + 8, &R2, &G2, &B2);
238
0
    YUV420ToRGB_SSE41(y + 24, u + 12, v + 12, &R3, &G3, &B3);
239
240
    // Cast to 8b and store as BBBBGGGGRRRR.
241
0
    bgr0 = _mm_packus_epi16(B0, B1);
242
0
    bgr1 = _mm_packus_epi16(B2, B3);
243
0
    bgr2 = _mm_packus_epi16(G0, G1);
244
0
    bgr3 = _mm_packus_epi16(G2, G3);
245
0
    bgr4 = _mm_packus_epi16(R0, R1);
246
0
    bgr5 = _mm_packus_epi16(R2, R3);
247
248
    // Pack as BGRBGRBGRBGR.
249
0
    PlanarTo24b_SSE41(&bgr0, &bgr1, &bgr2, &bgr3, &bgr4, &bgr5, dst);
250
251
0
    y += 32;
252
0
    u += 16;
253
0
    v += 16;
254
0
  }
255
0
  for (; n < len; ++n) {  // Finish off
256
0
    VP8YuvToBgr(y[0], u[0], v[0], dst);
257
0
    dst += 3;
258
0
    y += 1;
259
0
    u += (n & 1);
260
0
    v += (n & 1);
261
0
  }
262
0
}
263
264
//------------------------------------------------------------------------------
265
// Entry point
266
267
extern void WebPInitSamplersSSE41(void);
268
269
7
WEBP_TSAN_IGNORE_FUNCTION void WebPInitSamplersSSE41(void) {
270
7
  WebPSamplers[MODE_RGB] = YuvToRgbRow_SSE41;
271
7
  WebPSamplers[MODE_BGR] = YuvToBgrRow_SSE41;
272
7
}
273
274
//------------------------------------------------------------------------------
275
// RGB24/32 -> YUV converters
276
277
// Load eight 16b-words from *src.
278
145M
#define LOAD_16(src) _mm_loadu_si128((const __m128i*)(src))
279
// Store either 16b-words into *dst
280
108M
#define STORE_16(V, dst) _mm_storeu_si128((__m128i*)(dst), (V))
281
282
#define WEBP_SSE41_SHUFF(OUT)                          \
283
49.9k
  do {                                                 \
284
49.9k
    const __m128i tmp0 = _mm_shuffle_epi8(A0, shuff0); \
285
49.9k
    const __m128i tmp1 = _mm_shuffle_epi8(A1, shuff1); \
286
49.9k
    const __m128i tmp2 = _mm_shuffle_epi8(A2, shuff2); \
287
49.9k
    const __m128i tmp3 = _mm_shuffle_epi8(A3, shuff0); \
288
49.9k
    const __m128i tmp4 = _mm_shuffle_epi8(A4, shuff1); \
289
49.9k
    const __m128i tmp5 = _mm_shuffle_epi8(A5, shuff2); \
290
49.9k
                                                       \
291
49.9k
    /* OR everything to get one channel */             \
292
49.9k
    const __m128i tmp6 = _mm_or_si128(tmp0, tmp1);     \
293
49.9k
    const __m128i tmp7 = _mm_or_si128(tmp3, tmp4);     \
294
49.9k
    out[OUT + 0] = _mm_or_si128(tmp6, tmp2);           \
295
49.9k
    out[OUT + 1] = _mm_or_si128(tmp7, tmp5);           \
296
49.9k
  } 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
16.6k
    const uint8_t* WEBP_RESTRICT const rgb, __m128i* const out /*out[6]*/) {
303
16.6k
  const __m128i A0 = _mm_loadu_si128((const __m128i*)(rgb + 0));
304
16.6k
  const __m128i A1 = _mm_loadu_si128((const __m128i*)(rgb + 16));
305
16.6k
  const __m128i A2 = _mm_loadu_si128((const __m128i*)(rgb + 32));
306
16.6k
  const __m128i A3 = _mm_loadu_si128((const __m128i*)(rgb + 48));
307
16.6k
  const __m128i A4 = _mm_loadu_si128((const __m128i*)(rgb + 64));
308
16.6k
  const __m128i A5 = _mm_loadu_si128((const __m128i*)(rgb + 80));
309
310
  // Compute RR.
311
16.6k
  {
312
16.6k
    const __m128i shuff0 = _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
313
16.6k
                                        15, 12, 9, 6, 3, 0);
314
16.6k
    const __m128i shuff1 = _mm_set_epi8(-1, -1, -1, -1, -1, 14, 11, 8, 5, 2, -1,
315
16.6k
                                        -1, -1, -1, -1, -1);
316
16.6k
    const __m128i shuff2 = _mm_set_epi8(13, 10, 7, 4, 1, -1, -1, -1, -1, -1, -1,
317
16.6k
                                        -1, -1, -1, -1, -1);
318
16.6k
    WEBP_SSE41_SHUFF(0)
319
16.6k
  }
320
  // Compute GG.
321
16.6k
  {
322
16.6k
    const __m128i shuff0 = _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
323
16.6k
                                        -1, 13, 10, 7, 4, 1);
324
16.6k
    const __m128i shuff1 = _mm_set_epi8(-1, -1, -1, -1, -1, 15, 12, 9, 6, 3, 0,
325
16.6k
                                        -1, -1, -1, -1, -1);
326
16.6k
    const __m128i shuff2 = _mm_set_epi8(14, 11, 8, 5, 2, -1, -1, -1, -1, -1, -1,
327
16.6k
                                        -1, -1, -1, -1, -1);
328
16.6k
    WEBP_SSE41_SHUFF(2)
329
16.6k
  }
330
  // Compute BB.
331
16.6k
  {
332
16.6k
    const __m128i shuff0 = _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
333
16.6k
                                        -1, 14, 11, 8, 5, 2);
334
16.6k
    const __m128i shuff1 = _mm_set_epi8(-1, -1, -1, -1, -1, -1, 13, 10, 7, 4, 1,
335
16.6k
                                        -1, -1, -1, -1, -1);
336
16.6k
    const __m128i shuff2 = _mm_set_epi8(15, 12, 9, 6, 3, 0, -1, -1, -1, -1, -1,
337
16.6k
                                        -1, -1, -1, -1, -1);
338
16.6k
    WEBP_SSE41_SHUFF(4)
339
16.6k
  }
340
16.6k
}
341
342
#undef WEBP_SSE41_SHUFF
343
344
static WEBP_INLINE void RGBAPackedToRGBPlanar_SSE41(
345
36.2M
    const uint8_t* WEBP_RESTRICT const rgba, __m128i* const rgb /*in[6]*/) {
346
36.2M
  __m128i a0 = _mm_loadu_si128((const __m128i*)(rgba + 0));
347
36.2M
  __m128i a1 = _mm_loadu_si128((const __m128i*)(rgba + 16));
348
36.2M
  __m128i a2 = _mm_loadu_si128((const __m128i*)(rgba + 32));
349
36.2M
  __m128i a3 = _mm_loadu_si128((const __m128i*)(rgba + 48));
350
36.2M
  __m128i a4 = _mm_loadu_si128((const __m128i*)(rgba + 64));
351
36.2M
  __m128i a5 = _mm_loadu_si128((const __m128i*)(rgba + 80));
352
36.2M
  __m128i a6 = _mm_loadu_si128((const __m128i*)(rgba + 96));
353
36.2M
  __m128i a7 = _mm_loadu_si128((const __m128i*)(rgba + 112));
354
36.2M
  VP8L32bToPlanar_SSE41(&a0, &a1, &a2, &a3);
355
36.2M
  rgb[0] = a3;
356
36.2M
  rgb[2] = a2;
357
36.2M
  rgb[4] = a1;
358
36.2M
  VP8L32bToPlanar_SSE41(&a4, &a5, &a6, &a7);
359
36.2M
  rgb[1] = a7;
360
36.2M
  rgb[3] = a6;
361
36.2M
  rgb[5] = a5;
362
36.2M
}
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
0
    const uint32_t* WEBP_RESTRICT const argb, __m128i* const rgb /*in[6]*/) {
368
0
  const __m128i zero = _mm_setzero_si128();
369
0
  __m128i a0 = _mm_loadu_si128((const __m128i*)(argb + 0));
370
0
  __m128i a1 = _mm_loadu_si128((const __m128i*)(argb + 4));
371
0
  __m128i a2 = _mm_loadu_si128((const __m128i*)(argb + 8));
372
0
  __m128i a3 = _mm_loadu_si128((const __m128i*)(argb + 12));
373
0
  VP8L32bToPlanar_SSE41(&a0, &a1, &a2, &a3);
374
0
  rgb[0] = _mm_unpacklo_epi8(a1, zero);
375
0
  rgb[1] = _mm_unpackhi_epi8(a1, zero);
376
0
  rgb[2] = _mm_unpacklo_epi8(a2, zero);
377
0
  rgb[3] = _mm_unpackhi_epi8(a2, zero);
378
0
  rgb[4] = _mm_unpacklo_epi8(a3, zero);
379
0
  rgb[5] = _mm_unpackhi_epi8(a3, zero);
380
0
}
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
217M
  do {                                                                   \
388
217M
    const __m128i V0_lo = _mm_madd_epi16(RG_LO, MULT_RG);                \
389
217M
    const __m128i V0_hi = _mm_madd_epi16(RG_HI, MULT_RG);                \
390
217M
    const __m128i V1_lo = _mm_madd_epi16(GB_LO, MULT_GB);                \
391
217M
    const __m128i V1_hi = _mm_madd_epi16(GB_HI, MULT_GB);                \
392
217M
    const __m128i V2_lo = _mm_add_epi32(V0_lo, V1_lo);                   \
393
217M
    const __m128i V2_hi = _mm_add_epi32(V0_hi, V1_hi);                   \
394
217M
    const __m128i V3_lo = _mm_add_epi32(V2_lo, ROUNDER);                 \
395
217M
    const __m128i V3_hi = _mm_add_epi32(V2_hi, ROUNDER);                 \
396
217M
    const __m128i V5_lo = _mm_srai_epi32(V3_lo, DESCALE_FIX);            \
397
217M
    const __m128i V5_hi = _mm_srai_epi32(V3_hi, DESCALE_FIX);            \
398
217M
    (OUT) = _mm_packs_epi32(V5_lo, V5_hi);                               \
399
217M
  } while (0)
400
401
435M
#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
145M
                                                __m128i* const Y) {
406
145M
  const __m128i kRG_y = MK_CST_16(16839, 33059 - 16384);
407
145M
  const __m128i kGB_y = MK_CST_16(16384, 6420);
408
145M
  const __m128i kHALF_Y = _mm_set1_epi32((16 << YUV_FIX) + YUV_HALF);
409
410
145M
  const __m128i RG_lo = _mm_unpacklo_epi16(*R, *G);
411
145M
  const __m128i RG_hi = _mm_unpackhi_epi16(*R, *G);
412
145M
  const __m128i GB_lo = _mm_unpacklo_epi16(*G, *B);
413
145M
  const __m128i GB_hi = _mm_unpackhi_epi16(*G, *B);
414
145M
  TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_y, kGB_y, kHALF_Y, YUV_FIX, *Y);
415
145M
}
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
36.3M
                                             __m128i* const V) {
422
36.3M
  const __m128i kRG_u = MK_CST_16(-9719, -19081);
423
36.3M
  const __m128i kGB_u = MK_CST_16(0, 28800);
424
36.3M
  const __m128i kRG_v = MK_CST_16(28800, 0);
425
36.3M
  const __m128i kGB_v = MK_CST_16(-24116, -4684);
426
36.3M
  const __m128i kHALF_UV = _mm_set1_epi32(((128 << YUV_FIX) + YUV_HALF) << 2);
427
428
36.3M
  const __m128i RG_lo = _mm_unpacklo_epi16(*R, *G);
429
36.3M
  const __m128i RG_hi = _mm_unpackhi_epi16(*R, *G);
430
36.3M
  const __m128i GB_lo = _mm_unpacklo_epi16(*G, *B);
431
36.3M
  const __m128i GB_hi = _mm_unpackhi_epi16(*G, *B);
432
36.3M
  TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_u, kGB_u, kHALF_UV, YUV_FIX + 2,
433
36.3M
            *U);
434
36.3M
  TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_v, kGB_v, kHALF_UV, YUV_FIX + 2,
435
36.3M
            *V);
436
36.3M
}
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
36.2M
    uint8_t* WEBP_RESTRICT y) {
444
36.2M
  int j;
445
446
108M
  for (j = 0; j < 2; ++j, *i += 16) {
447
72.5M
    const __m128i zero = _mm_setzero_si128();
448
72.5M
    __m128i r, g, b, Y0, Y1;
449
450
    // Convert to 16-bit Y.
451
72.5M
    r = _mm_unpacklo_epi8(rgb_plane[(swap_rb ? 4 : 0) + j], zero);
452
72.5M
    g = _mm_unpacklo_epi8(rgb_plane[2 + j], zero);
453
72.5M
    b = _mm_unpacklo_epi8(rgb_plane[(swap_rb ? 0 : 4) + j], zero);
454
72.5M
    ConvertRGBToYImpl_SSE41(&r, &g, &b, &Y0);
455
456
    // Convert to 16-bit Y.
457
72.5M
    r = _mm_unpackhi_epi8(rgb_plane[(swap_rb ? 4 : 0) + j], zero);
458
72.5M
    g = _mm_unpackhi_epi8(rgb_plane[2 + j], zero);
459
72.5M
    b = _mm_unpackhi_epi8(rgb_plane[(swap_rb ? 0 : 4) + j], zero);
460
72.5M
    ConvertRGBToYImpl_SSE41(&r, &g, &b, &Y1);
461
462
    // Cast to 8-bit and store.
463
72.5M
    STORE_16(_mm_packus_epi16(Y0, Y1), y + *i);
464
72.5M
  }
465
36.2M
}
466
467
static void ConvertRGBToY_SSE41(const uint8_t* WEBP_RESTRICT rgb,
468
1.13M
                                uint8_t* WEBP_RESTRICT y, int width, int step) {
469
1.13M
  const int max_width = width & ~31;
470
1.13M
  int i;
471
1.13M
  __m128i rgb_plane[6];
472
1.13M
  if (step == 3) {
473
827k
    for (i = 0; i < max_width; rgb += 3 * 16 * 2) {
474
16.6k
      RGBPackedToPlanar_SSE41(rgb, rgb_plane);
475
16.6k
      ConvertRGBToYHelper_SSE41(rgb_plane, /*swap_rb=*/0, &i, y);
476
16.6k
    }
477
811k
  } else {
478
324k
    for (i = 0; i < max_width; rgb += 4 * 16 * 2) {
479
350
      RGBAPackedToRGBPlanar_SSE41(rgb, rgb_plane);
480
350
      ConvertRGBToYHelper_SSE41(rgb_plane, /*swap_rb=*/0, &i, y);
481
350
    }
482
323k
  }
483
3.05M
  for (; i < width; ++i, rgb += step) {  // left-over
484
1.92M
    y[i] = VP8RGBToY(rgb[0], rgb[1], rgb[2], YUV_HALF);
485
1.92M
  }
486
1.13M
}
487
488
static void ConvertBGRToY_SSE41(const uint8_t* WEBP_RESTRICT bgr,
489
1.38M
                                uint8_t* WEBP_RESTRICT y, int width, int step) {
490
1.38M
  const int max_width = width & ~31;
491
1.38M
  int i;
492
1.38M
  __m128i bgr_plane[6];
493
1.38M
  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.38M
  } else {
499
37.6M
    for (i = 0; i < max_width; bgr += 4 * 16 * 2) {
500
36.2M
      RGBAPackedToRGBPlanar_SSE41(bgr, bgr_plane);
501
36.2M
      ConvertRGBToYHelper_SSE41(bgr_plane, /*swap_rb=*/1, &i, y);
502
36.2M
    }
503
1.38M
  }
504
18.5M
  for (; i < width; ++i, bgr += step) {  // left-over
505
17.1M
    y[i] = VP8RGBToY(bgr[2], bgr[1], bgr[0], YUV_HALF);
506
17.1M
  }
507
1.38M
}
508
509
static void ConvertARGBToY_SSE41(const uint32_t* WEBP_RESTRICT argb,
510
0
                                 uint8_t* WEBP_RESTRICT y, int width) {
511
0
  const int max_width = width & ~15;
512
0
  int i;
513
0
  for (i = 0; i < max_width; i += 16) {
514
0
    __m128i Y0, Y1, rgb[6];
515
0
    ARGBPackedToRGBPlanar16_SSE41(&argb[i], rgb);
516
0
    ConvertRGBToYImpl_SSE41(&rgb[0], &rgb[2], &rgb[4], &Y0);
517
0
    ConvertRGBToYImpl_SSE41(&rgb[1], &rgb[3], &rgb[5], &Y1);
518
0
    STORE_16(_mm_packus_epi16(Y0, Y1), y + i);
519
0
  }
520
0
  for (; i < width; ++i) {  // left-over
521
0
    const uint32_t p = argb[i];
522
0
    y[i] =
523
0
        VP8RGBToY((p >> 16) & 0xff, (p >> 8) & 0xff, (p >> 0) & 0xff, YUV_HALF);
524
0
  }
525
0
}
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
0
                                    __m128i* const out) {
532
0
  const __m128i k2 = _mm_set1_epi16(2);
533
0
  const __m128i C = _mm_madd_epi16(*A, k2);
534
0
  const __m128i D = _mm_madd_epi16(*B, k2);
535
0
  *out = _mm_packs_epi32(C, D);
536
0
}
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
0
                                  int do_store) {
542
0
  const int max_width = src_width & ~31;
543
0
  int i;
544
0
  for (i = 0; i < max_width; i += 32, u += 16, v += 16) {
545
0
    __m128i rgb[6], U0, V0, U1, V1;
546
0
    ARGBPackedToRGBPlanar16_SSE41(&argb[i], rgb);
547
0
    HorizontalAddPack_SSE41(&rgb[0], &rgb[1], &rgb[0]);
548
0
    HorizontalAddPack_SSE41(&rgb[2], &rgb[3], &rgb[2]);
549
0
    HorizontalAddPack_SSE41(&rgb[4], &rgb[5], &rgb[4]);
550
0
    ConvertRGBToUV_SSE41(&rgb[0], &rgb[2], &rgb[4], &U0, &V0);
551
552
0
    ARGBPackedToRGBPlanar16_SSE41(&argb[i + 16], rgb);
553
0
    HorizontalAddPack_SSE41(&rgb[0], &rgb[1], &rgb[0]);
554
0
    HorizontalAddPack_SSE41(&rgb[2], &rgb[3], &rgb[2]);
555
0
    HorizontalAddPack_SSE41(&rgb[4], &rgb[5], &rgb[4]);
556
0
    ConvertRGBToUV_SSE41(&rgb[0], &rgb[2], &rgb[4], &U1, &V1);
557
558
0
    U0 = _mm_packus_epi16(U0, U1);
559
0
    V0 = _mm_packus_epi16(V0, V1);
560
0
    if (!do_store) {
561
0
      const __m128i prev_u = LOAD_16(u);
562
0
      const __m128i prev_v = LOAD_16(v);
563
0
      U0 = _mm_avg_epu8(U0, prev_u);
564
0
      V0 = _mm_avg_epu8(V0, prev_v);
565
0
    }
566
0
    STORE_16(U0, u);
567
0
    STORE_16(V0, v);
568
0
  }
569
0
  if (i < src_width) {  // left-over
570
0
    WebPConvertARGBToUV_C(argb + i, u, v, src_width - i, do_store);
571
0
  }
572
0
}
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
36.3M
    __m128i* const g, __m128i* const b) {
578
36.3M
  const __m128i in0 = LOAD_16(rgbx + 0);   // r0 | g0 | b0 |x| r1 | g1 | b1 |x
579
36.3M
  const __m128i in1 = LOAD_16(rgbx + 8);   // r2 | g2 | b2 |x| r3 | g3 | b3 |x
580
36.3M
  const __m128i in2 = LOAD_16(rgbx + 16);  // r4 | ...
581
36.3M
  const __m128i in3 = LOAD_16(rgbx + 24);  // r6 | ...
582
  // aarrggbb as 16-bit.
583
36.3M
  const __m128i shuff0 =
584
36.3M
      _mm_set_epi8(-1, -1, -1, -1, 13, 12, 5, 4, 11, 10, 3, 2, 9, 8, 1, 0);
585
36.3M
  const __m128i shuff1 =
586
36.3M
      _mm_set_epi8(13, 12, 5, 4, -1, -1, -1, -1, 11, 10, 3, 2, 9, 8, 1, 0);
587
36.3M
  const __m128i A0 = _mm_shuffle_epi8(in0, shuff0);
588
36.3M
  const __m128i A1 = _mm_shuffle_epi8(in1, shuff1);
589
36.3M
  const __m128i A2 = _mm_shuffle_epi8(in2, shuff0);
590
36.3M
  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
36.3M
  const __m128i B0 = _mm_unpacklo_epi32(A0, A1);
597
36.3M
  const __m128i B1 = _mm_or_si128(A0, A1);
598
36.3M
  const __m128i B2 = _mm_unpacklo_epi32(A2, A3);
599
36.3M
  const __m128i B3 = _mm_or_si128(A2, A3);
600
  // Gather the channels.
601
36.3M
  *r = _mm_unpacklo_epi64(B0, B2);
602
36.3M
  *g = _mm_unpackhi_epi64(B0, B2);
603
36.3M
  *b = _mm_unpackhi_epi64(B1, B3);
604
36.3M
}
605
606
static void ConvertRGBA32ToUV_SSE41(const uint16_t* WEBP_RESTRICT rgb,
607
                                    uint8_t* WEBP_RESTRICT u,
608
1.26M
                                    uint8_t* WEBP_RESTRICT v, int width) {
609
1.26M
  const int max_width = width & ~15;
610
1.26M
  const uint16_t* const last_rgb = rgb + 4 * max_width;
611
19.4M
  while (rgb < last_rgb) {
612
18.1M
    __m128i r, g, b, U0, V0, U1, V1;
613
18.1M
    RGBA32PackedToPlanar_16b_SSE41(rgb + 0, &r, &g, &b);
614
18.1M
    ConvertRGBToUV_SSE41(&r, &g, &b, &U0, &V0);
615
18.1M
    RGBA32PackedToPlanar_16b_SSE41(rgb + 32, &r, &g, &b);
616
18.1M
    ConvertRGBToUV_SSE41(&r, &g, &b, &U1, &V1);
617
18.1M
    STORE_16(_mm_packus_epi16(U0, U1), u);
618
18.1M
    STORE_16(_mm_packus_epi16(V0, V1), v);
619
18.1M
    u += 16;
620
18.1M
    v += 16;
621
18.1M
    rgb += 2 * 32;
622
18.1M
  }
623
1.26M
  if (max_width < width) {  // left-over
624
1.18M
    WebPConvertRGBA32ToUV_C(rgb, u, v, width - max_width);
625
1.18M
  }
626
1.26M
}
627
628
//------------------------------------------------------------------------------
629
630
extern void WebPInitConvertARGBToYUVSSE41(void);
631
632
4
WEBP_TSAN_IGNORE_FUNCTION void WebPInitConvertARGBToYUVSSE41(void) {
633
4
  WebPConvertARGBToY = ConvertARGBToY_SSE41;
634
4
  WebPConvertARGBToUV = ConvertARGBToUV_SSE41;
635
636
4
  WebPConvertRGBToY = ConvertRGBToY_SSE41;
637
4
  WebPConvertBGRToY = ConvertBGRToY_SSE41;
638
639
4
  WebPConvertRGBA32ToUV = ConvertRGBA32ToUV_SSE41;
640
4
}
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