Coverage Report

Created: 2025-12-31 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/dsp/upsampling_sse2.c
Line
Count
Source
1
// Copyright 2011 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
// SSE2 version of YUV to RGB upsampling functions.
11
//
12
// Author: somnath@google.com (Somnath Banerjee)
13
14
#include "src/dsp/dsp.h"
15
16
#if defined(WEBP_USE_SSE2)
17
#include <assert.h>
18
#include <emmintrin.h>
19
#include <string.h>
20
21
#include "src/dsp/cpu.h"
22
#include "src/dsp/yuv.h"
23
#include "src/webp/decode.h"
24
#include "src/webp/types.h"
25
26
#ifdef FANCY_UPSAMPLING
27
28
// We compute (9*a + 3*b + 3*c + d + 8) / 16 as follows
29
// u = (9*a + 3*b + 3*c + d + 8) / 16
30
//   = (a + (a + 3*b + 3*c + d) / 8 + 1) / 2
31
//   = (a + m + 1) / 2
32
// where m = (a + 3*b + 3*c + d) / 8
33
//         = ((a + b + c + d) / 2 + b + c) / 4
34
//
35
// Let's say  k = (a + b + c + d) / 4.
36
// We can compute k as
37
// k = (s + t + 1) / 2 - ((a^d) | (b^c) | (s^t)) & 1
38
// where s = (a + d + 1) / 2 and t = (b + c + 1) / 2
39
//
40
// Then m can be written as
41
// m = (k + t + 1) / 2 - (((b^c) & (s^t)) | (k^t)) & 1
42
43
// Computes out = (k + in + 1) / 2 - ((ij & (s^t)) | (k^in)) & 1
44
#define GET_M(ij, in, out)                                                     \
45
4.19M
  do {                                                                         \
46
4.19M
    const __m128i tmp0 = _mm_avg_epu8(k, (in));   /* (k + in + 1) / 2 */       \
47
4.19M
    const __m128i tmp1 = _mm_and_si128((ij), st); /* (ij) & (s^t) */           \
48
4.19M
    const __m128i tmp2 = _mm_xor_si128(k, (in));  /* (k^in) */                 \
49
4.19M
    const __m128i tmp3 =                                                       \
50
4.19M
        _mm_or_si128(tmp1, tmp2); /* ((ij) & (s^t)) | (k^in) */                \
51
4.19M
    const __m128i tmp4 = _mm_and_si128(tmp3, one); /* & 1 -> lsb_correction */ \
52
4.19M
    (out) = _mm_sub_epi8(tmp0, tmp4); /* (k + in + 1) / 2 - lsb_correction */  \
53
4.19M
  } while (0)
54
55
// pack and store two alternating pixel rows
56
#define PACK_AND_STORE(a, b, da, db, out)                       \
57
4.19M
  do {                                                          \
58
4.19M
    const __m128i t_a =                                         \
59
4.19M
        _mm_avg_epu8(a, da); /* (9a + 3b + 3c +  d + 8) / 16 */ \
60
4.19M
    const __m128i t_b =                                         \
61
4.19M
        _mm_avg_epu8(b, db); /* (3a + 9b +  c + 3d + 8) / 16 */ \
62
4.19M
    const __m128i t_1 = _mm_unpacklo_epi8(t_a, t_b);            \
63
4.19M
    const __m128i t_2 = _mm_unpackhi_epi8(t_a, t_b);            \
64
4.19M
    _mm_store_si128(((__m128i*)(out)) + 0, t_1);                \
65
4.19M
    _mm_store_si128(((__m128i*)(out)) + 1, t_2);                \
66
4.19M
  } while (0)
67
68
// Loads 17 pixels each from rows r1 and r2 and generates 32 pixels.
69
#define UPSAMPLE_32PIXELS(r1, r2, out)                                         \
70
2.09M
  do {                                                                         \
71
2.09M
    const __m128i one = _mm_set1_epi8(1);                                      \
72
2.09M
    const __m128i a = _mm_loadu_si128((const __m128i*)&(r1)[0]);               \
73
2.09M
    const __m128i b = _mm_loadu_si128((const __m128i*)&(r1)[1]);               \
74
2.09M
    const __m128i c = _mm_loadu_si128((const __m128i*)&(r2)[0]);               \
75
2.09M
    const __m128i d = _mm_loadu_si128((const __m128i*)&(r2)[1]);               \
76
2.09M
                                                                               \
77
2.09M
    const __m128i s = _mm_avg_epu8(a, d);   /* s = (a + d + 1) / 2 */          \
78
2.09M
    const __m128i t = _mm_avg_epu8(b, c);   /* t = (b + c + 1) / 2 */          \
79
2.09M
    const __m128i st = _mm_xor_si128(s, t); /* st = s^t */                     \
80
2.09M
                                                                               \
81
2.09M
    const __m128i ad = _mm_xor_si128(a, d); /* ad = a^d */                     \
82
2.09M
    const __m128i bc = _mm_xor_si128(b, c); /* bc = b^c */                     \
83
2.09M
                                                                               \
84
2.09M
    const __m128i t1 = _mm_or_si128(ad, bc);   /* (a^d) | (b^c) */             \
85
2.09M
    const __m128i t2 = _mm_or_si128(t1, st);   /* (a^d) | (b^c) | (s^t) */     \
86
2.09M
    const __m128i t3 = _mm_and_si128(t2, one); /* (a^d) | (b^c) | (s^t) & 1 */ \
87
2.09M
    const __m128i t4 = _mm_avg_epu8(s, t);                                     \
88
2.09M
    const __m128i k = _mm_sub_epi8(t4, t3); /* k = (a + b + c + d) / 4 */      \
89
2.09M
    __m128i diag1, diag2;                                                      \
90
2.09M
                                                                               \
91
2.09M
    GET_M(bc, t, diag1); /* diag1 = (a + 3b + 3c + d) / 8 */                   \
92
2.09M
    GET_M(ad, s, diag2); /* diag2 = (3a + b + c + 3d) / 8 */                   \
93
2.09M
                                                                               \
94
2.09M
    /* pack the alternate pixels */                                            \
95
2.09M
    PACK_AND_STORE(a, b, diag1, diag2, (out) + 0);      /* store top */        \
96
2.09M
    PACK_AND_STORE(c, d, diag2, diag1, (out) + 2 * 32); /* store bottom */     \
97
2.09M
  } while (0)
98
99
// Turn the macro into a function for reducing code-size when non-critical
100
static void Upsample32Pixels_SSE2(const uint8_t* WEBP_RESTRICT const r1,
101
                                  const uint8_t* WEBP_RESTRICT const r2,
102
286k
                                  uint8_t* WEBP_RESTRICT const out) {
103
286k
  UPSAMPLE_32PIXELS(r1, r2, out);
104
286k
}
105
106
#define UPSAMPLE_LAST_BLOCK(tb, bb, num_pixels, out)                         \
107
286k
  {                                                                          \
108
286k
    uint8_t r1[17], r2[17];                                                  \
109
286k
    memcpy(r1, (tb), (num_pixels));                                          \
110
286k
    memcpy(r2, (bb), (num_pixels));                                          \
111
286k
    /* replicate last byte */                                                \
112
286k
    memset(r1 + (num_pixels), r1[(num_pixels) - 1], 17 - (num_pixels));      \
113
286k
    memset(r2 + (num_pixels), r2[(num_pixels) - 1], 17 - (num_pixels));      \
114
286k
    /* using the shared function instead of the macro saves ~3k code size */ \
115
286k
    Upsample32Pixels_SSE2(r1, r2, out);                                      \
116
286k
  }
117
118
#define CONVERT2RGB_32(FUNC, XSTEP, top_y, bottom_y, top_dst, bottom_dst,      \
119
                       cur_x)                                                  \
120
1.04M
  do {                                                                         \
121
1.04M
    FUNC##32_SSE2((top_y) + (cur_x), r_u, r_v, (top_dst) + (cur_x) * (XSTEP)); \
122
1.04M
    if ((bottom_y) != NULL) {                                                  \
123
1.03M
      FUNC##32_SSE2((bottom_y) + (cur_x), r_u + 64, r_v + 64,                  \
124
1.03M
                    (bottom_dst) + (cur_x) * (XSTEP));                         \
125
1.03M
    }                                                                          \
126
1.04M
  } while (0)
127
128
#define SSE2_UPSAMPLE_FUNC(FUNC_NAME, FUNC, XSTEP)                            \
129
  static void FUNC_NAME(                                                      \
130
      const uint8_t* WEBP_RESTRICT top_y,                                     \
131
      const uint8_t* WEBP_RESTRICT bottom_y,                                  \
132
      const uint8_t* WEBP_RESTRICT top_u, const uint8_t* WEBP_RESTRICT top_v, \
133
      const uint8_t* WEBP_RESTRICT cur_u, const uint8_t* WEBP_RESTRICT cur_v, \
134
      uint8_t* WEBP_RESTRICT top_dst, uint8_t* WEBP_RESTRICT bottom_dst,      \
135
204k
      int len) {                                                              \
136
204k
    int uv_pos, pos;                                                          \
137
204k
    /* 16byte-aligned array to cache reconstructed u and v */                 \
138
204k
    uint8_t uv_buf[14 * 32 + 15] = {0};                                       \
139
204k
    uint8_t* const r_u =                                                      \
140
204k
        (uint8_t*)((uintptr_t)(uv_buf + 15) & ~(uintptr_t)15);                \
141
204k
    uint8_t* const r_v = r_u + 32;                                            \
142
204k
                                                                              \
143
204k
    assert(top_y != NULL);                                                    \
144
204k
    { /* Treat the first pixel in regular way */                              \
145
204k
      const int u_diag = ((top_u[0] + cur_u[0]) >> 1) + 1;                    \
146
204k
      const int v_diag = ((top_v[0] + cur_v[0]) >> 1) + 1;                    \
147
204k
      const int u0_t = (top_u[0] + u_diag) >> 1;                              \
148
204k
      const int v0_t = (top_v[0] + v_diag) >> 1;                              \
149
204k
      FUNC(top_y[0], u0_t, v0_t, top_dst);                                    \
150
204k
      if (bottom_y != NULL) {                                                 \
151
203k
        const int u0_b = (cur_u[0] + u_diag) >> 1;                            \
152
203k
        const int v0_b = (cur_v[0] + v_diag) >> 1;                            \
153
203k
        FUNC(bottom_y[0], u0_b, v0_b, bottom_dst);                            \
154
203k
      }                                                                       \
155
204k
    }                                                                         \
156
204k
    /* For UPSAMPLE_32PIXELS, 17 u/v values must be read-able for each block  \
157
204k
     */                                                                       \
158
1.10M
    for (pos = 1, uv_pos = 0; pos + 32 + 1 <= len; pos += 32, uv_pos += 16) { \
159
905k
      UPSAMPLE_32PIXELS(top_u + uv_pos, cur_u + uv_pos, r_u);                 \
160
905k
      UPSAMPLE_32PIXELS(top_v + uv_pos, cur_v + uv_pos, r_v);                 \
161
905k
      CONVERT2RGB_32(FUNC, XSTEP, top_y, bottom_y, top_dst, bottom_dst, pos); \
162
905k
    }                                                                         \
163
204k
    if (len > 1) {                                                            \
164
143k
      const int left_over = ((len + 1) >> 1) - (pos >> 1);                    \
165
143k
      uint8_t* const tmp_top_dst = r_u + 4 * 32;                              \
166
143k
      uint8_t* const tmp_bottom_dst = tmp_top_dst + 4 * 32;                   \
167
143k
      uint8_t* const tmp_top = tmp_bottom_dst + 4 * 32;                       \
168
143k
      uint8_t* const tmp_bottom = (bottom_y == NULL) ? NULL : tmp_top + 32;   \
169
143k
      assert(left_over > 0);                                                  \
170
143k
      UPSAMPLE_LAST_BLOCK(top_u + uv_pos, cur_u + uv_pos, left_over, r_u);    \
171
143k
      UPSAMPLE_LAST_BLOCK(top_v + uv_pos, cur_v + uv_pos, left_over, r_v);    \
172
143k
      memcpy(tmp_top, top_y + pos, len - pos);                                \
173
143k
      if (bottom_y != NULL) memcpy(tmp_bottom, bottom_y + pos, len - pos);    \
174
143k
      CONVERT2RGB_32(FUNC, XSTEP, tmp_top, tmp_bottom, tmp_top_dst,           \
175
143k
                     tmp_bottom_dst, 0);                                      \
176
143k
      memcpy(top_dst + pos * (XSTEP), tmp_top_dst, (len - pos) * (XSTEP));    \
177
143k
      if (bottom_y != NULL) {                                                 \
178
142k
        memcpy(bottom_dst + pos * (XSTEP), tmp_bottom_dst,                    \
179
142k
               (len - pos) * (XSTEP));                                        \
180
142k
      }                                                                       \
181
143k
    }                                                                         \
182
204k
  }
183
184
// SSE2 variants of the fancy upsampler.
185
204k
SSE2_UPSAMPLE_FUNC(UpsampleRgbaLinePair_SSE2, VP8YuvToRgba, 4)
186
0
SSE2_UPSAMPLE_FUNC(UpsampleBgraLinePair_SSE2, VP8YuvToBgra, 4)
187
188
#if !defined(WEBP_REDUCE_CSP)
189
0
SSE2_UPSAMPLE_FUNC(UpsampleRgbLinePair_SSE2, VP8YuvToRgb, 3)
190
0
SSE2_UPSAMPLE_FUNC(UpsampleBgrLinePair_SSE2, VP8YuvToBgr, 3)
191
0
SSE2_UPSAMPLE_FUNC(UpsampleArgbLinePair_SSE2, VP8YuvToArgb, 4)
192
0
SSE2_UPSAMPLE_FUNC(UpsampleRgba4444LinePair_SSE2, VP8YuvToRgba4444, 2)
193
0
SSE2_UPSAMPLE_FUNC(UpsampleRgb565LinePair_SSE2, VP8YuvToRgb565, 2)
194
#endif  // WEBP_REDUCE_CSP
195
196
#undef GET_M
197
#undef PACK_AND_STORE
198
#undef UPSAMPLE_32PIXELS
199
#undef UPSAMPLE_LAST_BLOCK
200
#undef CONVERT2RGB
201
#undef CONVERT2RGB_32
202
#undef SSE2_UPSAMPLE_FUNC
203
204
//------------------------------------------------------------------------------
205
// Entry point
206
207
extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */];
208
209
extern void WebPInitUpsamplersSSE2(void);
210
211
1
WEBP_TSAN_IGNORE_FUNCTION void WebPInitUpsamplersSSE2(void) {
212
1
  WebPUpsamplers[MODE_RGBA] = UpsampleRgbaLinePair_SSE2;
213
1
  WebPUpsamplers[MODE_BGRA] = UpsampleBgraLinePair_SSE2;
214
1
  WebPUpsamplers[MODE_rgbA] = UpsampleRgbaLinePair_SSE2;
215
1
  WebPUpsamplers[MODE_bgrA] = UpsampleBgraLinePair_SSE2;
216
1
#if !defined(WEBP_REDUCE_CSP)
217
1
  WebPUpsamplers[MODE_RGB] = UpsampleRgbLinePair_SSE2;
218
1
  WebPUpsamplers[MODE_BGR] = UpsampleBgrLinePair_SSE2;
219
1
  WebPUpsamplers[MODE_ARGB] = UpsampleArgbLinePair_SSE2;
220
1
  WebPUpsamplers[MODE_Argb] = UpsampleArgbLinePair_SSE2;
221
1
  WebPUpsamplers[MODE_RGB_565] = UpsampleRgb565LinePair_SSE2;
222
1
  WebPUpsamplers[MODE_RGBA_4444] = UpsampleRgba4444LinePair_SSE2;
223
1
  WebPUpsamplers[MODE_rgbA_4444] = UpsampleRgba4444LinePair_SSE2;
224
1
#endif  // WEBP_REDUCE_CSP
225
1
}
226
227
#endif  // FANCY_UPSAMPLING
228
229
//------------------------------------------------------------------------------
230
231
extern WebPYUV444Converter WebPYUV444Converters[/* MODE_LAST */];
232
extern void WebPInitYUV444ConvertersSSE2(void);
233
234
#define YUV444_FUNC(FUNC_NAME, CALL, CALL_C, XSTEP)                          \
235
  extern void CALL_C(                                                        \
236
      const uint8_t* WEBP_RESTRICT y, const uint8_t* WEBP_RESTRICT u,        \
237
      const uint8_t* WEBP_RESTRICT v, uint8_t* WEBP_RESTRICT dst, int len);  \
238
  static void FUNC_NAME(                                                     \
239
      const uint8_t* WEBP_RESTRICT y, const uint8_t* WEBP_RESTRICT u,        \
240
0
      const uint8_t* WEBP_RESTRICT v, uint8_t* WEBP_RESTRICT dst, int len) { \
241
0
    int i;                                                                   \
242
0
    const int max_len = len & ~31;                                           \
243
0
    for (i = 0; i < max_len; i += 32) {                                      \
244
0
      CALL(y + i, u + i, v + i, dst + i * (XSTEP));                          \
245
0
    }                                                                        \
246
0
    if (i < len) { /* C-fallback */                                          \
247
0
      CALL_C(y + i, u + i, v + i, dst + i * (XSTEP), len - i);               \
248
0
    }                                                                        \
249
0
  }
Unexecuted instantiation: upsampling_sse2.c:Yuv444ToRgba_SSE2
Unexecuted instantiation: upsampling_sse2.c:Yuv444ToBgra_SSE2
Unexecuted instantiation: upsampling_sse2.c:Yuv444ToRgb_SSE2
Unexecuted instantiation: upsampling_sse2.c:Yuv444ToBgr_SSE2
Unexecuted instantiation: upsampling_sse2.c:Yuv444ToArgb_SSE2
Unexecuted instantiation: upsampling_sse2.c:Yuv444ToRgba4444_SSE2
Unexecuted instantiation: upsampling_sse2.c:Yuv444ToRgb565_SSE2
250
251
YUV444_FUNC(Yuv444ToRgba_SSE2, VP8YuvToRgba32_SSE2, WebPYuv444ToRgba_C, 4)
252
YUV444_FUNC(Yuv444ToBgra_SSE2, VP8YuvToBgra32_SSE2, WebPYuv444ToBgra_C, 4)
253
#if !defined(WEBP_REDUCE_CSP)
254
YUV444_FUNC(Yuv444ToRgb_SSE2, VP8YuvToRgb32_SSE2, WebPYuv444ToRgb_C, 3)
255
YUV444_FUNC(Yuv444ToBgr_SSE2, VP8YuvToBgr32_SSE2, WebPYuv444ToBgr_C, 3)
256
YUV444_FUNC(Yuv444ToArgb_SSE2, VP8YuvToArgb32_SSE2, WebPYuv444ToArgb_C, 4)
257
YUV444_FUNC(Yuv444ToRgba4444_SSE2, VP8YuvToRgba444432_SSE2,
258
            WebPYuv444ToRgba4444_C, 2)
259
YUV444_FUNC(Yuv444ToRgb565_SSE2, VP8YuvToRgb56532_SSE2, WebPYuv444ToRgb565_C, 2)
260
#endif  // WEBP_REDUCE_CSP
261
262
0
WEBP_TSAN_IGNORE_FUNCTION void WebPInitYUV444ConvertersSSE2(void) {
263
0
  WebPYUV444Converters[MODE_RGBA] = Yuv444ToRgba_SSE2;
264
0
  WebPYUV444Converters[MODE_BGRA] = Yuv444ToBgra_SSE2;
265
0
  WebPYUV444Converters[MODE_rgbA] = Yuv444ToRgba_SSE2;
266
0
  WebPYUV444Converters[MODE_bgrA] = Yuv444ToBgra_SSE2;
267
0
#if !defined(WEBP_REDUCE_CSP)
268
0
  WebPYUV444Converters[MODE_RGB] = Yuv444ToRgb_SSE2;
269
0
  WebPYUV444Converters[MODE_BGR] = Yuv444ToBgr_SSE2;
270
0
  WebPYUV444Converters[MODE_ARGB] = Yuv444ToArgb_SSE2;
271
0
  WebPYUV444Converters[MODE_RGBA_4444] = Yuv444ToRgba4444_SSE2;
272
0
  WebPYUV444Converters[MODE_RGB_565] = Yuv444ToRgb565_SSE2;
273
0
  WebPYUV444Converters[MODE_Argb] = Yuv444ToArgb_SSE2;
274
0
  WebPYUV444Converters[MODE_rgbA_4444] = Yuv444ToRgba4444_SSE2;
275
0
#endif  // WEBP_REDUCE_CSP
276
0
}
277
278
#else
279
280
WEBP_DSP_INIT_STUB(WebPInitYUV444ConvertersSSE2)
281
282
#endif  // WEBP_USE_SSE2
283
284
#if !(defined(FANCY_UPSAMPLING) && defined(WEBP_USE_SSE2))
285
WEBP_DSP_INIT_STUB(WebPInitUpsamplersSSE2)
286
#endif