Coverage Report

Created: 2026-04-12 06:51

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