Coverage Report

Created: 2025-06-13 06:49

/src/libwebp/src/dsp/upsampling.c
Line
Count
Source (jump to first uncovered line)
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
// YUV to RGB upsampling functions.
11
//
12
// Author: somnath@google.com (Somnath Banerjee)
13
14
#include <assert.h>
15
#include <stddef.h>
16
17
#include "src/dsp/cpu.h"
18
#include "src/webp/types.h"
19
#include "src/dsp/dsp.h"
20
#include "src/dsp/yuv.h"
21
#include "src/webp/decode.h"
22
23
//------------------------------------------------------------------------------
24
// Fancy upsampler
25
26
#ifdef FANCY_UPSAMPLING
27
28
// Fancy upsampling functions to convert YUV to RGB
29
WebPUpsampleLinePairFunc WebPUpsamplers[MODE_LAST];
30
31
// Given samples laid out in a square as:
32
//  [a b]
33
//  [c d]
34
// we interpolate u/v as:
35
//  ([9*a + 3*b + 3*c +   d    3*a + 9*b + 3*c +   d] + [8 8]) / 16
36
//  ([3*a +   b + 9*c + 3*d      a + 3*b + 3*c + 9*d]   [8 8]) / 16
37
38
// We process u and v together stashed into 32bit (16bit each).
39
0
#define LOAD_UV(u, v) ((u) | ((v) << 16))
40
41
#define UPSAMPLE_FUNC(FUNC_NAME, FUNC, XSTEP)                                  \
42
static void FUNC_NAME(const uint8_t* WEBP_RESTRICT top_y,                      \
43
                      const uint8_t* WEBP_RESTRICT bottom_y,                   \
44
                      const uint8_t* WEBP_RESTRICT top_u,                      \
45
                      const uint8_t* WEBP_RESTRICT top_v,                      \
46
                      const uint8_t* WEBP_RESTRICT cur_u,                      \
47
                      const uint8_t* WEBP_RESTRICT cur_v,                      \
48
                      uint8_t* WEBP_RESTRICT top_dst,                          \
49
0
                      uint8_t* WEBP_RESTRICT bottom_dst, int len) {            \
50
0
  int x;                                                                       \
51
0
  const int last_pixel_pair = (len - 1) >> 1;                                  \
52
0
  uint32_t tl_uv = LOAD_UV(top_u[0], top_v[0]);   /* top-left sample */        \
53
0
  uint32_t l_uv  = LOAD_UV(cur_u[0], cur_v[0]);   /* left-sample */            \
54
0
  assert(top_y != NULL);                                                       \
55
0
  {                                                                            \
56
0
    const uint32_t uv0 = (3 * tl_uv + l_uv + 0x00020002u) >> 2;                \
57
0
    FUNC(top_y[0], uv0 & 0xff, (uv0 >> 16), top_dst);                          \
58
0
  }                                                                            \
59
0
  if (bottom_y != NULL) {                                                      \
60
0
    const uint32_t uv0 = (3 * l_uv + tl_uv + 0x00020002u) >> 2;                \
61
0
    FUNC(bottom_y[0], uv0 & 0xff, (uv0 >> 16), bottom_dst);                    \
62
0
  }                                                                            \
63
0
  for (x = 1; x <= last_pixel_pair; ++x) {                                     \
64
0
    const uint32_t t_uv = LOAD_UV(top_u[x], top_v[x]);  /* top sample */       \
65
0
    const uint32_t uv   = LOAD_UV(cur_u[x], cur_v[x]);  /* sample */           \
66
0
    /* precompute invariant values associated with first and second diagonals*/\
67
0
    const uint32_t avg = tl_uv + t_uv + l_uv + uv + 0x00080008u;               \
68
0
    const uint32_t diag_12 = (avg + 2 * (t_uv + l_uv)) >> 3;                   \
69
0
    const uint32_t diag_03 = (avg + 2 * (tl_uv + uv)) >> 3;                    \
70
0
    {                                                                          \
71
0
      const uint32_t uv0 = (diag_12 + tl_uv) >> 1;                             \
72
0
      const uint32_t uv1 = (diag_03 + t_uv) >> 1;                              \
73
0
      FUNC(top_y[2 * x - 1], uv0 & 0xff, (uv0 >> 16),                          \
74
0
           top_dst + (2 * x - 1) * (XSTEP));                                   \
75
0
      FUNC(top_y[2 * x - 0], uv1 & 0xff, (uv1 >> 16),                          \
76
0
           top_dst + (2 * x - 0) * (XSTEP));                                   \
77
0
    }                                                                          \
78
0
    if (bottom_y != NULL) {                                                    \
79
0
      const uint32_t uv0 = (diag_03 + l_uv) >> 1;                              \
80
0
      const uint32_t uv1 = (diag_12 + uv) >> 1;                                \
81
0
      FUNC(bottom_y[2 * x - 1], uv0 & 0xff, (uv0 >> 16),                       \
82
0
           bottom_dst + (2 * x - 1) * (XSTEP));                                \
83
0
      FUNC(bottom_y[2 * x + 0], uv1 & 0xff, (uv1 >> 16),                       \
84
0
           bottom_dst + (2 * x + 0) * (XSTEP));                                \
85
0
    }                                                                          \
86
0
    tl_uv = t_uv;                                                              \
87
0
    l_uv = uv;                                                                 \
88
0
  }                                                                            \
89
0
  if (!(len & 1)) {                                                            \
90
0
    {                                                                          \
91
0
      const uint32_t uv0 = (3 * tl_uv + l_uv + 0x00020002u) >> 2;              \
92
0
      FUNC(top_y[len - 1], uv0 & 0xff, (uv0 >> 16),                            \
93
0
           top_dst + (len - 1) * (XSTEP));                                     \
94
0
    }                                                                          \
95
0
    if (bottom_y != NULL) {                                                    \
96
0
      const uint32_t uv0 = (3 * l_uv + tl_uv + 0x00020002u) >> 2;              \
97
0
      FUNC(bottom_y[len - 1], uv0 & 0xff, (uv0 >> 16),                         \
98
0
           bottom_dst + (len - 1) * (XSTEP));                                  \
99
0
    }                                                                          \
100
0
  }                                                                            \
101
0
}
Unexecuted instantiation: upsampling.c:UpsampleRgbaLinePair_C
Unexecuted instantiation: upsampling.c:UpsampleBgraLinePair_C
Unexecuted instantiation: upsampling.c:UpsampleRgbLinePair_C
Unexecuted instantiation: upsampling.c:UpsampleBgrLinePair_C
Unexecuted instantiation: upsampling.c:UpsampleArgbLinePair_C
Unexecuted instantiation: upsampling.c:UpsampleRgba4444LinePair_C
Unexecuted instantiation: upsampling.c:UpsampleRgb565LinePair_C
102
103
// All variants implemented.
104
#if !WEBP_NEON_OMIT_C_CODE
105
UPSAMPLE_FUNC(UpsampleRgbaLinePair_C, VP8YuvToRgba, 4)
106
UPSAMPLE_FUNC(UpsampleBgraLinePair_C, VP8YuvToBgra, 4)
107
#if !defined(WEBP_REDUCE_CSP)
108
UPSAMPLE_FUNC(UpsampleArgbLinePair_C, VP8YuvToArgb, 4)
109
UPSAMPLE_FUNC(UpsampleRgbLinePair_C,  VP8YuvToRgb,  3)
110
UPSAMPLE_FUNC(UpsampleBgrLinePair_C,  VP8YuvToBgr,  3)
111
UPSAMPLE_FUNC(UpsampleRgba4444LinePair_C, VP8YuvToRgba4444, 2)
112
UPSAMPLE_FUNC(UpsampleRgb565LinePair_C,  VP8YuvToRgb565,  2)
113
#else
114
static void EmptyUpsampleFunc(const uint8_t* top_y, const uint8_t* bottom_y,
115
                              const uint8_t* top_u, const uint8_t* top_v,
116
                              const uint8_t* cur_u, const uint8_t* cur_v,
117
                              uint8_t* top_dst, uint8_t* bottom_dst, int len) {
118
  (void)top_y;
119
  (void)bottom_y;
120
  (void)top_u;
121
  (void)top_v;
122
  (void)cur_u;
123
  (void)cur_v;
124
  (void)top_dst;
125
  (void)bottom_dst;
126
  (void)len;
127
  assert(0);   // COLORSPACE SUPPORT NOT COMPILED
128
}
129
#define UpsampleArgbLinePair_C EmptyUpsampleFunc
130
#define UpsampleRgbLinePair_C EmptyUpsampleFunc
131
#define UpsampleBgrLinePair_C EmptyUpsampleFunc
132
#define UpsampleRgba4444LinePair_C EmptyUpsampleFunc
133
#define UpsampleRgb565LinePair_C EmptyUpsampleFunc
134
#endif   // WEBP_REDUCE_CSP
135
136
#endif
137
138
#undef LOAD_UV
139
#undef UPSAMPLE_FUNC
140
141
#endif  // FANCY_UPSAMPLING
142
143
//------------------------------------------------------------------------------
144
145
#if !defined(FANCY_UPSAMPLING)
146
#define DUAL_SAMPLE_FUNC(FUNC_NAME, FUNC)                                      \
147
static void FUNC_NAME(const uint8_t* WEBP_RESTRICT top_y,                      \
148
                      const uint8_t* WEBP_RESTRICT bot_y,                      \
149
                      const uint8_t* WEBP_RESTRICT top_u,                      \
150
                      const uint8_t* WEBP_RESTRICT top_v,                      \
151
                      const uint8_t* WEBP_RESTRICT bot_u,                      \
152
                      const uint8_t* WEBP_RESTRICT bot_v,                      \
153
                      uint8_t* WEBP_RESTRICT top_dst,                          \
154
                      uint8_t* WEBP_RESTRICT bot_dst, int len) {               \
155
  const int half_len = len >> 1;                                               \
156
  int x;                                                                       \
157
  assert(top_dst != NULL);                                                     \
158
  {                                                                            \
159
    for (x = 0; x < half_len; ++x) {                                           \
160
      FUNC(top_y[2 * x + 0], top_u[x], top_v[x], top_dst + 8 * x + 0);         \
161
      FUNC(top_y[2 * x + 1], top_u[x], top_v[x], top_dst + 8 * x + 4);         \
162
    }                                                                          \
163
    if (len & 1) FUNC(top_y[2 * x + 0], top_u[x], top_v[x], top_dst + 8 * x);  \
164
  }                                                                            \
165
  if (bot_dst != NULL) {                                                       \
166
    for (x = 0; x < half_len; ++x) {                                           \
167
      FUNC(bot_y[2 * x + 0], bot_u[x], bot_v[x], bot_dst + 8 * x + 0);         \
168
      FUNC(bot_y[2 * x + 1], bot_u[x], bot_v[x], bot_dst + 8 * x + 4);         \
169
    }                                                                          \
170
    if (len & 1) FUNC(bot_y[2 * x + 0], bot_u[x], bot_v[x], bot_dst + 8 * x);  \
171
  }                                                                            \
172
}
173
174
DUAL_SAMPLE_FUNC(DualLineSamplerBGRA, VP8YuvToBgra)
175
DUAL_SAMPLE_FUNC(DualLineSamplerARGB, VP8YuvToArgb)
176
#undef DUAL_SAMPLE_FUNC
177
178
#endif  // !FANCY_UPSAMPLING
179
180
0
WebPUpsampleLinePairFunc WebPGetLinePairConverter(int alpha_is_last) {
181
0
  WebPInitUpsamplers();
182
0
#ifdef FANCY_UPSAMPLING
183
0
  return WebPUpsamplers[alpha_is_last ? MODE_BGRA : MODE_ARGB];
184
#else
185
  return (alpha_is_last ? DualLineSamplerBGRA : DualLineSamplerARGB);
186
#endif
187
0
}
188
189
//------------------------------------------------------------------------------
190
// YUV444 converter
191
192
#define YUV444_FUNC(FUNC_NAME, FUNC, XSTEP)                                    \
193
extern void FUNC_NAME(const uint8_t* WEBP_RESTRICT y,                          \
194
                      const uint8_t* WEBP_RESTRICT u,                          \
195
                      const uint8_t* WEBP_RESTRICT v,                          \
196
                      uint8_t* WEBP_RESTRICT dst, int len);                    \
197
void FUNC_NAME(const uint8_t* WEBP_RESTRICT y,                                 \
198
               const uint8_t* WEBP_RESTRICT u,                                 \
199
               const uint8_t* WEBP_RESTRICT v,                                 \
200
0
               uint8_t* WEBP_RESTRICT dst, int len) {                          \
201
0
  int i;                                                                       \
202
0
  for (i = 0; i < len; ++i) FUNC(y[i], u[i], v[i], &dst[i * (XSTEP)]);         \
203
0
}
Unexecuted instantiation: WebPYuv444ToRgba_C
Unexecuted instantiation: WebPYuv444ToBgra_C
Unexecuted instantiation: WebPYuv444ToRgb_C
Unexecuted instantiation: WebPYuv444ToBgr_C
Unexecuted instantiation: WebPYuv444ToArgb_C
Unexecuted instantiation: WebPYuv444ToRgba4444_C
Unexecuted instantiation: WebPYuv444ToRgb565_C
204
205
YUV444_FUNC(WebPYuv444ToRgba_C,     VP8YuvToRgba, 4)
206
YUV444_FUNC(WebPYuv444ToBgra_C,     VP8YuvToBgra, 4)
207
#if !defined(WEBP_REDUCE_CSP)
208
YUV444_FUNC(WebPYuv444ToRgb_C,      VP8YuvToRgb,  3)
209
YUV444_FUNC(WebPYuv444ToBgr_C,      VP8YuvToBgr,  3)
210
YUV444_FUNC(WebPYuv444ToArgb_C,     VP8YuvToArgb, 4)
211
YUV444_FUNC(WebPYuv444ToRgba4444_C, VP8YuvToRgba4444, 2)
212
YUV444_FUNC(WebPYuv444ToRgb565_C,   VP8YuvToRgb565, 2)
213
#else
214
static void EmptyYuv444Func(const uint8_t* y,
215
                            const uint8_t* u, const uint8_t* v,
216
                            uint8_t* dst, int len) {
217
  (void)y;
218
  (void)u;
219
  (void)v;
220
  (void)dst;
221
  (void)len;
222
}
223
#define WebPYuv444ToRgb_C EmptyYuv444Func
224
#define WebPYuv444ToBgr_C EmptyYuv444Func
225
#define WebPYuv444ToArgb_C EmptyYuv444Func
226
#define WebPYuv444ToRgba4444_C EmptyYuv444Func
227
#define WebPYuv444ToRgb565_C EmptyYuv444Func
228
#endif   // WEBP_REDUCE_CSP
229
230
#undef YUV444_FUNC
231
232
WebPYUV444Converter WebPYUV444Converters[MODE_LAST];
233
234
extern VP8CPUInfo VP8GetCPUInfo;
235
extern void WebPInitYUV444ConvertersMIPSdspR2(void);
236
extern void WebPInitYUV444ConvertersSSE2(void);
237
extern void WebPInitYUV444ConvertersSSE41(void);
238
239
0
WEBP_DSP_INIT_FUNC(WebPInitYUV444Converters) {
240
0
  WebPYUV444Converters[MODE_RGBA]      = WebPYuv444ToRgba_C;
241
0
  WebPYUV444Converters[MODE_BGRA]      = WebPYuv444ToBgra_C;
242
0
  WebPYUV444Converters[MODE_RGB]       = WebPYuv444ToRgb_C;
243
0
  WebPYUV444Converters[MODE_BGR]       = WebPYuv444ToBgr_C;
244
0
  WebPYUV444Converters[MODE_ARGB]      = WebPYuv444ToArgb_C;
245
0
  WebPYUV444Converters[MODE_RGBA_4444] = WebPYuv444ToRgba4444_C;
246
0
  WebPYUV444Converters[MODE_RGB_565]   = WebPYuv444ToRgb565_C;
247
0
  WebPYUV444Converters[MODE_rgbA]      = WebPYuv444ToRgba_C;
248
0
  WebPYUV444Converters[MODE_bgrA]      = WebPYuv444ToBgra_C;
249
0
  WebPYUV444Converters[MODE_Argb]      = WebPYuv444ToArgb_C;
250
0
  WebPYUV444Converters[MODE_rgbA_4444] = WebPYuv444ToRgba4444_C;
251
252
0
  if (VP8GetCPUInfo != NULL) {
253
0
#if defined(WEBP_HAVE_SSE2)
254
0
    if (VP8GetCPUInfo(kSSE2)) {
255
0
      WebPInitYUV444ConvertersSSE2();
256
0
    }
257
0
#endif
258
0
#if defined(WEBP_HAVE_SSE41)
259
0
    if (VP8GetCPUInfo(kSSE4_1)) {
260
0
      WebPInitYUV444ConvertersSSE41();
261
0
    }
262
0
#endif
263
#if defined(WEBP_USE_MIPS_DSP_R2)
264
    if (VP8GetCPUInfo(kMIPSdspR2)) {
265
      WebPInitYUV444ConvertersMIPSdspR2();
266
    }
267
#endif
268
0
  }
269
0
}
270
271
//------------------------------------------------------------------------------
272
// Main calls
273
274
extern void WebPInitUpsamplersSSE2(void);
275
extern void WebPInitUpsamplersSSE41(void);
276
extern void WebPInitUpsamplersNEON(void);
277
extern void WebPInitUpsamplersMIPSdspR2(void);
278
extern void WebPInitUpsamplersMSA(void);
279
280
0
WEBP_DSP_INIT_FUNC(WebPInitUpsamplers) {
281
0
#ifdef FANCY_UPSAMPLING
282
0
#if !WEBP_NEON_OMIT_C_CODE
283
0
  WebPUpsamplers[MODE_RGBA]      = UpsampleRgbaLinePair_C;
284
0
  WebPUpsamplers[MODE_BGRA]      = UpsampleBgraLinePair_C;
285
0
  WebPUpsamplers[MODE_rgbA]      = UpsampleRgbaLinePair_C;
286
0
  WebPUpsamplers[MODE_bgrA]      = UpsampleBgraLinePair_C;
287
0
  WebPUpsamplers[MODE_RGB]       = UpsampleRgbLinePair_C;
288
0
  WebPUpsamplers[MODE_BGR]       = UpsampleBgrLinePair_C;
289
0
  WebPUpsamplers[MODE_ARGB]      = UpsampleArgbLinePair_C;
290
0
  WebPUpsamplers[MODE_RGBA_4444] = UpsampleRgba4444LinePair_C;
291
0
  WebPUpsamplers[MODE_RGB_565]   = UpsampleRgb565LinePair_C;
292
0
  WebPUpsamplers[MODE_Argb]      = UpsampleArgbLinePair_C;
293
0
  WebPUpsamplers[MODE_rgbA_4444] = UpsampleRgba4444LinePair_C;
294
0
#endif
295
296
  // If defined, use CPUInfo() to overwrite some pointers with faster versions.
297
0
  if (VP8GetCPUInfo != NULL) {
298
0
#if defined(WEBP_HAVE_SSE2)
299
0
    if (VP8GetCPUInfo(kSSE2)) {
300
0
      WebPInitUpsamplersSSE2();
301
0
    }
302
0
#endif
303
0
#if defined(WEBP_HAVE_SSE41)
304
0
    if (VP8GetCPUInfo(kSSE4_1)) {
305
0
      WebPInitUpsamplersSSE41();
306
0
    }
307
0
#endif
308
#if defined(WEBP_USE_MIPS_DSP_R2)
309
    if (VP8GetCPUInfo(kMIPSdspR2)) {
310
      WebPInitUpsamplersMIPSdspR2();
311
    }
312
#endif
313
#if defined(WEBP_USE_MSA)
314
    if (VP8GetCPUInfo(kMSA)) {
315
      WebPInitUpsamplersMSA();
316
    }
317
#endif
318
0
  }
319
320
#if defined(WEBP_HAVE_NEON)
321
  if (WEBP_NEON_OMIT_C_CODE ||
322
      (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kNEON))) {
323
    WebPInitUpsamplersNEON();
324
  }
325
#endif
326
327
0
  assert(WebPUpsamplers[MODE_RGBA] != NULL);
328
0
  assert(WebPUpsamplers[MODE_BGRA] != NULL);
329
0
  assert(WebPUpsamplers[MODE_rgbA] != NULL);
330
0
  assert(WebPUpsamplers[MODE_bgrA] != NULL);
331
0
#if !defined(WEBP_REDUCE_CSP) || !WEBP_NEON_OMIT_C_CODE
332
0
  assert(WebPUpsamplers[MODE_RGB] != NULL);
333
0
  assert(WebPUpsamplers[MODE_BGR] != NULL);
334
0
  assert(WebPUpsamplers[MODE_ARGB] != NULL);
335
0
  assert(WebPUpsamplers[MODE_RGBA_4444] != NULL);
336
0
  assert(WebPUpsamplers[MODE_RGB_565] != NULL);
337
0
  assert(WebPUpsamplers[MODE_Argb] != NULL);
338
0
  assert(WebPUpsamplers[MODE_rgbA_4444] != NULL);
339
0
#endif
340
341
0
#endif  // FANCY_UPSAMPLING
342
0
}
343
344
//------------------------------------------------------------------------------