Coverage Report

Created: 2025-12-31 07:15

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
0
                                     __m128i* const G, __m128i* const B) {
39
0
  const __m128i k19077 = _mm_set1_epi16(19077);
40
0
  const __m128i k26149 = _mm_set1_epi16(26149);
41
0
  const __m128i k14234 = _mm_set1_epi16(14234);
42
  // 33050 doesn't fit in a signed short: only use this with unsigned arithmetic
43
0
  const __m128i k33050 = _mm_set1_epi16((short)33050);
44
0
  const __m128i k17685 = _mm_set1_epi16(17685);
45
0
  const __m128i k6419 = _mm_set1_epi16(6419);
46
0
  const __m128i k13320 = _mm_set1_epi16(13320);
47
0
  const __m128i k8708 = _mm_set1_epi16(8708);
48
49
0
  const __m128i Y1 = _mm_mulhi_epu16(*Y0, k19077);
50
51
0
  const __m128i R0 = _mm_mulhi_epu16(*V0, k26149);
52
0
  const __m128i R1 = _mm_sub_epi16(Y1, k14234);
53
0
  const __m128i R2 = _mm_add_epi16(R1, R0);
54
55
0
  const __m128i G0 = _mm_mulhi_epu16(*U0, k6419);
56
0
  const __m128i G1 = _mm_mulhi_epu16(*V0, k13320);
57
0
  const __m128i G2 = _mm_add_epi16(Y1, k8708);
58
0
  const __m128i G3 = _mm_add_epi16(G0, G1);
59
0
  const __m128i G4 = _mm_sub_epi16(G2, G3);
60
61
  // be careful with the saturated *unsigned* arithmetic here!
62
0
  const __m128i B0 = _mm_mulhi_epu16(*U0, k33050);
63
0
  const __m128i B1 = _mm_adds_epu16(B0, Y1);
64
0
  const __m128i B2 = _mm_subs_epu16(B1, k17685);
65
66
  // use logical shift for B2, which can be larger than 32767
67
0
  *R = _mm_srai_epi16(R2, 6);  // range: [-14234, 30815]
68
0
  *G = _mm_srai_epi16(G4, 6);  // range: [-10953, 27710]
69
0
  *B = _mm_srli_epi16(B2, 6);  // range: [0, 34238]
70
0
}
71
72
// Load the bytes into the *upper* part of 16b words. That's "<< 8", basically.
73
0
static WEBP_INLINE __m128i Load_HI_16_SSE41(const uint8_t* src) {
74
0
  const __m128i zero = _mm_setzero_si128();
75
0
  return _mm_unpacklo_epi8(zero, _mm_loadl_epi64((const __m128i*)src));
76
0
}
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
0
                              __m128i* const B) {
92
0
  const __m128i Y0 = Load_HI_16_SSE41(y), U0 = Load_HI_16_SSE41(u),
93
0
                V0 = Load_HI_16_SSE41(v);
94
0
  ConvertYUV444ToRGB_SSE41(&Y0, &U0, &V0, R, G, B);
95
0
}
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
0
    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
0
  VP8PlanarTo24b_SSE41(in0, in1, in2, in3, in4, in5);
128
129
0
  _mm_storeu_si128((__m128i*)(rgb + 0), *in0);
130
0
  _mm_storeu_si128((__m128i*)(rgb + 16), *in1);
131
0
  _mm_storeu_si128((__m128i*)(rgb + 32), *in2);
132
0
  _mm_storeu_si128((__m128i*)(rgb + 48), *in3);
133
0
  _mm_storeu_si128((__m128i*)(rgb + 64), *in4);
134
0
  _mm_storeu_si128((__m128i*)(rgb + 80), *in5);
135
0
}
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
0
                         uint8_t* WEBP_RESTRICT dst) {
141
0
  __m128i R0, R1, R2, R3, G0, G1, G2, G3, B0, B1, B2, B3;
142
0
  __m128i rgb0, rgb1, rgb2, rgb3, rgb4, rgb5;
143
144
0
  YUV444ToRGB_SSE41(y + 0, u + 0, v + 0, &R0, &G0, &B0);
145
0
  YUV444ToRGB_SSE41(y + 8, u + 8, v + 8, &R1, &G1, &B1);
146
0
  YUV444ToRGB_SSE41(y + 16, u + 16, v + 16, &R2, &G2, &B2);
147
0
  YUV444ToRGB_SSE41(y + 24, u + 24, v + 24, &R3, &G3, &B3);
148
149
  // Cast to 8b and store as RRRRGGGGBBBB.
150
0
  rgb0 = _mm_packus_epi16(R0, R1);
151
0
  rgb1 = _mm_packus_epi16(R2, R3);
152
0
  rgb2 = _mm_packus_epi16(G0, G1);
153
0
  rgb3 = _mm_packus_epi16(G2, G3);
154
0
  rgb4 = _mm_packus_epi16(B0, B1);
155
0
  rgb5 = _mm_packus_epi16(B2, B3);
156
157
  // Pack as RGBRGBRGBRGB.
158
0
  PlanarTo24b_SSE41(&rgb0, &rgb1, &rgb2, &rgb3, &rgb4, &rgb5, dst);
159
0
}
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
1
WEBP_TSAN_IGNORE_FUNCTION void WebPInitSamplersSSE41(void) {
270
1
  WebPSamplers[MODE_RGB] = YuvToRgbRow_SSE41;
271
1
  WebPSamplers[MODE_BGR] = YuvToBgrRow_SSE41;
272
1
}
273
274
//------------------------------------------------------------------------------
275
// RGB24/32 -> YUV converters
276
277
// Load eight 16b-words from *src.
278
0
#define LOAD_16(src) _mm_loadu_si128((const __m128i*)(src))
279
// Store either 16b-words into *dst
280
0
#define STORE_16(V, dst) _mm_storeu_si128((__m128i*)(dst), (V))
281
282
#define WEBP_SSE41_SHUFF(OUT)                          \
283
0
  do {                                                 \
284
0
    const __m128i tmp0 = _mm_shuffle_epi8(A0, shuff0); \
285
0
    const __m128i tmp1 = _mm_shuffle_epi8(A1, shuff1); \
286
0
    const __m128i tmp2 = _mm_shuffle_epi8(A2, shuff2); \
287
0
    const __m128i tmp3 = _mm_shuffle_epi8(A3, shuff0); \
288
0
    const __m128i tmp4 = _mm_shuffle_epi8(A4, shuff1); \
289
0
    const __m128i tmp5 = _mm_shuffle_epi8(A5, shuff2); \
290
0
                                                       \
291
0
    /* OR everything to get one channel */             \
292
0
    const __m128i tmp6 = _mm_or_si128(tmp0, tmp1);     \
293
0
    const __m128i tmp7 = _mm_or_si128(tmp3, tmp4);     \
294
0
    out[OUT + 0] = _mm_or_si128(tmp6, tmp2);           \
295
0
    out[OUT + 1] = _mm_or_si128(tmp7, tmp5);           \
296
0
  } 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
0
    const uint8_t* WEBP_RESTRICT const rgb, __m128i* const out /*out[6]*/) {
303
0
  const __m128i A0 = _mm_loadu_si128((const __m128i*)(rgb + 0));
304
0
  const __m128i A1 = _mm_loadu_si128((const __m128i*)(rgb + 16));
305
0
  const __m128i A2 = _mm_loadu_si128((const __m128i*)(rgb + 32));
306
0
  const __m128i A3 = _mm_loadu_si128((const __m128i*)(rgb + 48));
307
0
  const __m128i A4 = _mm_loadu_si128((const __m128i*)(rgb + 64));
308
0
  const __m128i A5 = _mm_loadu_si128((const __m128i*)(rgb + 80));
309
310
  // Compute RR.
311
0
  {
312
0
    const __m128i shuff0 = _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
313
0
                                        15, 12, 9, 6, 3, 0);
314
0
    const __m128i shuff1 = _mm_set_epi8(-1, -1, -1, -1, -1, 14, 11, 8, 5, 2, -1,
315
0
                                        -1, -1, -1, -1, -1);
316
0
    const __m128i shuff2 = _mm_set_epi8(13, 10, 7, 4, 1, -1, -1, -1, -1, -1, -1,
317
0
                                        -1, -1, -1, -1, -1);
318
0
    WEBP_SSE41_SHUFF(0)
319
0
  }
320
  // Compute GG.
321
0
  {
322
0
    const __m128i shuff0 = _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
323
0
                                        -1, 13, 10, 7, 4, 1);
324
0
    const __m128i shuff1 = _mm_set_epi8(-1, -1, -1, -1, -1, 15, 12, 9, 6, 3, 0,
325
0
                                        -1, -1, -1, -1, -1);
326
0
    const __m128i shuff2 = _mm_set_epi8(14, 11, 8, 5, 2, -1, -1, -1, -1, -1, -1,
327
0
                                        -1, -1, -1, -1, -1);
328
0
    WEBP_SSE41_SHUFF(2)
329
0
  }
330
  // Compute BB.
331
0
  {
332
0
    const __m128i shuff0 = _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
333
0
                                        -1, 14, 11, 8, 5, 2);
334
0
    const __m128i shuff1 = _mm_set_epi8(-1, -1, -1, -1, -1, -1, 13, 10, 7, 4, 1,
335
0
                                        -1, -1, -1, -1, -1);
336
0
    const __m128i shuff2 = _mm_set_epi8(15, 12, 9, 6, 3, 0, -1, -1, -1, -1, -1,
337
0
                                        -1, -1, -1, -1, -1);
338
0
    WEBP_SSE41_SHUFF(4)
339
0
  }
340
0
}
341
342
#undef WEBP_SSE41_SHUFF
343
344
static WEBP_INLINE void RGBAPackedToRGBPlanar_SSE41(
345
0
    const uint8_t* WEBP_RESTRICT const rgba, __m128i* const rgb /*in[6]*/) {
346
0
  __m128i a0 = _mm_loadu_si128((const __m128i*)(rgba + 0));
347
0
  __m128i a1 = _mm_loadu_si128((const __m128i*)(rgba + 16));
348
0
  __m128i a2 = _mm_loadu_si128((const __m128i*)(rgba + 32));
349
0
  __m128i a3 = _mm_loadu_si128((const __m128i*)(rgba + 48));
350
0
  __m128i a4 = _mm_loadu_si128((const __m128i*)(rgba + 64));
351
0
  __m128i a5 = _mm_loadu_si128((const __m128i*)(rgba + 80));
352
0
  __m128i a6 = _mm_loadu_si128((const __m128i*)(rgba + 96));
353
0
  __m128i a7 = _mm_loadu_si128((const __m128i*)(rgba + 112));
354
0
  VP8L32bToPlanar_SSE41(&a0, &a1, &a2, &a3);
355
0
  rgb[0] = a3;
356
0
  rgb[2] = a2;
357
0
  rgb[4] = a1;
358
0
  VP8L32bToPlanar_SSE41(&a4, &a5, &a6, &a7);
359
0
  rgb[1] = a7;
360
0
  rgb[3] = a6;
361
0
  rgb[5] = a5;
362
0
}
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
0
  do {                                                                   \
388
0
    const __m128i V0_lo = _mm_madd_epi16(RG_LO, MULT_RG);                \
389
0
    const __m128i V0_hi = _mm_madd_epi16(RG_HI, MULT_RG);                \
390
0
    const __m128i V1_lo = _mm_madd_epi16(GB_LO, MULT_GB);                \
391
0
    const __m128i V1_hi = _mm_madd_epi16(GB_HI, MULT_GB);                \
392
0
    const __m128i V2_lo = _mm_add_epi32(V0_lo, V1_lo);                   \
393
0
    const __m128i V2_hi = _mm_add_epi32(V0_hi, V1_hi);                   \
394
0
    const __m128i V3_lo = _mm_add_epi32(V2_lo, ROUNDER);                 \
395
0
    const __m128i V3_hi = _mm_add_epi32(V2_hi, ROUNDER);                 \
396
0
    const __m128i V5_lo = _mm_srai_epi32(V3_lo, DESCALE_FIX);            \
397
0
    const __m128i V5_hi = _mm_srai_epi32(V3_hi, DESCALE_FIX);            \
398
0
    (OUT) = _mm_packs_epi32(V5_lo, V5_hi);                               \
399
0
  } while (0)
400
401
0
#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
0
                                                __m128i* const Y) {
406
0
  const __m128i kRG_y = MK_CST_16(16839, 33059 - 16384);
407
0
  const __m128i kGB_y = MK_CST_16(16384, 6420);
408
0
  const __m128i kHALF_Y = _mm_set1_epi32((16 << YUV_FIX) + YUV_HALF);
409
410
0
  const __m128i RG_lo = _mm_unpacklo_epi16(*R, *G);
411
0
  const __m128i RG_hi = _mm_unpackhi_epi16(*R, *G);
412
0
  const __m128i GB_lo = _mm_unpacklo_epi16(*G, *B);
413
0
  const __m128i GB_hi = _mm_unpackhi_epi16(*G, *B);
414
0
  TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_y, kGB_y, kHALF_Y, YUV_FIX, *Y);
415
0
}
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
0
                                             __m128i* const V) {
422
0
  const __m128i kRG_u = MK_CST_16(-9719, -19081);
423
0
  const __m128i kGB_u = MK_CST_16(0, 28800);
424
0
  const __m128i kRG_v = MK_CST_16(28800, 0);
425
0
  const __m128i kGB_v = MK_CST_16(-24116, -4684);
426
0
  const __m128i kHALF_UV = _mm_set1_epi32(((128 << YUV_FIX) + YUV_HALF) << 2);
427
428
0
  const __m128i RG_lo = _mm_unpacklo_epi16(*R, *G);
429
0
  const __m128i RG_hi = _mm_unpackhi_epi16(*R, *G);
430
0
  const __m128i GB_lo = _mm_unpacklo_epi16(*G, *B);
431
0
  const __m128i GB_hi = _mm_unpackhi_epi16(*G, *B);
432
0
  TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_u, kGB_u, kHALF_UV, YUV_FIX + 2,
433
0
            *U);
434
0
  TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_v, kGB_v, kHALF_UV, YUV_FIX + 2,
435
0
            *V);
436
0
}
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
0
    uint8_t* WEBP_RESTRICT y) {
444
0
  int j;
445
446
0
  for (j = 0; j < 2; ++j, *i += 16) {
447
0
    const __m128i zero = _mm_setzero_si128();
448
0
    __m128i r, g, b, Y0, Y1;
449
450
    // Convert to 16-bit Y.
451
0
    r = _mm_unpacklo_epi8(rgb_plane[(swap_rb ? 4 : 0) + j], zero);
452
0
    g = _mm_unpacklo_epi8(rgb_plane[2 + j], zero);
453
0
    b = _mm_unpacklo_epi8(rgb_plane[(swap_rb ? 0 : 4) + j], zero);
454
0
    ConvertRGBToYImpl_SSE41(&r, &g, &b, &Y0);
455
456
    // Convert to 16-bit Y.
457
0
    r = _mm_unpackhi_epi8(rgb_plane[(swap_rb ? 4 : 0) + j], zero);
458
0
    g = _mm_unpackhi_epi8(rgb_plane[2 + j], zero);
459
0
    b = _mm_unpackhi_epi8(rgb_plane[(swap_rb ? 0 : 4) + j], zero);
460
0
    ConvertRGBToYImpl_SSE41(&r, &g, &b, &Y1);
461
462
    // Cast to 8-bit and store.
463
0
    STORE_16(_mm_packus_epi16(Y0, Y1), y + *i);
464
0
  }
465
0
}
466
467
static void ConvertRGBToY_SSE41(const uint8_t* WEBP_RESTRICT rgb,
468
0
                                uint8_t* WEBP_RESTRICT y, int width, int step) {
469
0
  const int max_width = width & ~31;
470
0
  int i;
471
0
  __m128i rgb_plane[6];
472
0
  if (step == 3) {
473
0
    for (i = 0; i < max_width; rgb += 3 * 16 * 2) {
474
0
      RGBPackedToPlanar_SSE41(rgb, rgb_plane);
475
0
      ConvertRGBToYHelper_SSE41(rgb_plane, /*swap_rb=*/0, &i, y);
476
0
    }
477
0
  } else {
478
0
    for (i = 0; i < max_width; rgb += 4 * 16 * 2) {
479
0
      RGBAPackedToRGBPlanar_SSE41(rgb, rgb_plane);
480
0
      ConvertRGBToYHelper_SSE41(rgb_plane, /*swap_rb=*/0, &i, y);
481
0
    }
482
0
  }
483
0
  for (; i < width; ++i, rgb += step) {  // left-over
484
0
    y[i] = VP8RGBToY(rgb[0], rgb[1], rgb[2], YUV_HALF);
485
0
  }
486
0
}
487
488
static void ConvertBGRToY_SSE41(const uint8_t* WEBP_RESTRICT bgr,
489
0
                                uint8_t* WEBP_RESTRICT y, int width, int step) {
490
0
  const int max_width = width & ~31;
491
0
  int i;
492
0
  __m128i bgr_plane[6];
493
0
  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
0
  } else {
499
0
    for (i = 0; i < max_width; bgr += 4 * 16 * 2) {
500
0
      RGBAPackedToRGBPlanar_SSE41(bgr, bgr_plane);
501
0
      ConvertRGBToYHelper_SSE41(bgr_plane, /*swap_rb=*/1, &i, y);
502
0
    }
503
0
  }
504
0
  for (; i < width; ++i, bgr += step) {  // left-over
505
0
    y[i] = VP8RGBToY(bgr[2], bgr[1], bgr[0], YUV_HALF);
506
0
  }
507
0
}
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
0
    __m128i* const g, __m128i* const b) {
578
0
  const __m128i in0 = LOAD_16(rgbx + 0);   // r0 | g0 | b0 |x| r1 | g1 | b1 |x
579
0
  const __m128i in1 = LOAD_16(rgbx + 8);   // r2 | g2 | b2 |x| r3 | g3 | b3 |x
580
0
  const __m128i in2 = LOAD_16(rgbx + 16);  // r4 | ...
581
0
  const __m128i in3 = LOAD_16(rgbx + 24);  // r6 | ...
582
  // aarrggbb as 16-bit.
583
0
  const __m128i shuff0 =
584
0
      _mm_set_epi8(-1, -1, -1, -1, 13, 12, 5, 4, 11, 10, 3, 2, 9, 8, 1, 0);
585
0
  const __m128i shuff1 =
586
0
      _mm_set_epi8(13, 12, 5, 4, -1, -1, -1, -1, 11, 10, 3, 2, 9, 8, 1, 0);
587
0
  const __m128i A0 = _mm_shuffle_epi8(in0, shuff0);
588
0
  const __m128i A1 = _mm_shuffle_epi8(in1, shuff1);
589
0
  const __m128i A2 = _mm_shuffle_epi8(in2, shuff0);
590
0
  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
0
  const __m128i B0 = _mm_unpacklo_epi32(A0, A1);
597
0
  const __m128i B1 = _mm_or_si128(A0, A1);
598
0
  const __m128i B2 = _mm_unpacklo_epi32(A2, A3);
599
0
  const __m128i B3 = _mm_or_si128(A2, A3);
600
  // Gather the channels.
601
0
  *r = _mm_unpacklo_epi64(B0, B2);
602
0
  *g = _mm_unpackhi_epi64(B0, B2);
603
0
  *b = _mm_unpackhi_epi64(B1, B3);
604
0
}
605
606
static void ConvertRGBA32ToUV_SSE41(const uint16_t* WEBP_RESTRICT rgb,
607
                                    uint8_t* WEBP_RESTRICT u,
608
0
                                    uint8_t* WEBP_RESTRICT v, int width) {
609
0
  const int max_width = width & ~15;
610
0
  const uint16_t* const last_rgb = rgb + 4 * max_width;
611
0
  while (rgb < last_rgb) {
612
0
    __m128i r, g, b, U0, V0, U1, V1;
613
0
    RGBA32PackedToPlanar_16b_SSE41(rgb + 0, &r, &g, &b);
614
0
    ConvertRGBToUV_SSE41(&r, &g, &b, &U0, &V0);
615
0
    RGBA32PackedToPlanar_16b_SSE41(rgb + 32, &r, &g, &b);
616
0
    ConvertRGBToUV_SSE41(&r, &g, &b, &U1, &V1);
617
0
    STORE_16(_mm_packus_epi16(U0, U1), u);
618
0
    STORE_16(_mm_packus_epi16(V0, V1), v);
619
0
    u += 16;
620
0
    v += 16;
621
0
    rgb += 2 * 32;
622
0
  }
623
0
  if (max_width < width) {  // left-over
624
0
    WebPConvertRGBA32ToUV_C(rgb, u, v, width - max_width);
625
0
  }
626
0
}
627
628
//------------------------------------------------------------------------------
629
630
extern void WebPInitConvertARGBToYUVSSE41(void);
631
632
0
WEBP_TSAN_IGNORE_FUNCTION void WebPInitConvertARGBToYUVSSE41(void) {
633
0
  WebPConvertARGBToY = ConvertARGBToY_SSE41;
634
0
  WebPConvertARGBToUV = ConvertARGBToUV_SSE41;
635
636
0
  WebPConvertRGBToY = ConvertRGBToY_SSE41;
637
0
  WebPConvertBGRToY = ConvertBGRToY_SSE41;
638
639
0
  WebPConvertRGBA32ToUV = ConvertRGBA32ToUV_SSE41;
640
0
}
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