Coverage Report

Created: 2024-07-27 06:27

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