Coverage Report

Created: 2025-08-11 08:01

/src/libwebp/src/dsp/yuv.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2010 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
#include <assert.h>
17
#include <stdlib.h>
18
19
#include "src/dsp/cpu.h"
20
#include "src/dsp/dsp.h"
21
#include "src/webp/decode.h"
22
#include "src/webp/types.h"
23
24
//-----------------------------------------------------------------------------
25
// Plain-C version
26
27
#define ROW_FUNC(FUNC_NAME, FUNC, XSTEP)                                     \
28
  static void FUNC_NAME(                                                     \
29
      const uint8_t* WEBP_RESTRICT y, const uint8_t* WEBP_RESTRICT u,        \
30
0
      const uint8_t* WEBP_RESTRICT v, uint8_t* WEBP_RESTRICT dst, int len) { \
31
0
    const uint8_t* const end = dst + (len & ~1) * (XSTEP);                   \
32
0
    while (dst != end) {                                                     \
33
0
      FUNC(y[0], u[0], v[0], dst);                                           \
34
0
      FUNC(y[1], u[0], v[0], dst + (XSTEP));                                 \
35
0
      y += 2;                                                                \
36
0
      ++u;                                                                   \
37
0
      ++v;                                                                   \
38
0
      dst += 2 * (XSTEP);                                                    \
39
0
    }                                                                        \
40
0
    if (len & 1) {                                                           \
41
0
      FUNC(y[0], u[0], v[0], dst);                                           \
42
0
    }                                                                        \
43
0
  }
Unexecuted instantiation: yuv.c:YuvToRgbRow
Unexecuted instantiation: yuv.c:YuvToRgbaRow
Unexecuted instantiation: yuv.c:YuvToBgrRow
Unexecuted instantiation: yuv.c:YuvToBgraRow
Unexecuted instantiation: yuv.c:YuvToArgbRow
Unexecuted instantiation: yuv.c:YuvToRgba4444Row
Unexecuted instantiation: yuv.c:YuvToRgb565Row
44
45
// All variants implemented.
46
ROW_FUNC(YuvToRgbRow, VP8YuvToRgb, 3)
47
ROW_FUNC(YuvToBgrRow, VP8YuvToBgr, 3)
48
ROW_FUNC(YuvToRgbaRow, VP8YuvToRgba, 4)
49
ROW_FUNC(YuvToBgraRow, VP8YuvToBgra, 4)
50
ROW_FUNC(YuvToArgbRow, VP8YuvToArgb, 4)
51
ROW_FUNC(YuvToRgba4444Row, VP8YuvToRgba4444, 2)
52
ROW_FUNC(YuvToRgb565Row, VP8YuvToRgb565, 2)
53
54
#undef ROW_FUNC
55
56
// Main call for processing a plane with a WebPSamplerRowFunc function:
57
void WebPSamplerProcessPlane(const uint8_t* WEBP_RESTRICT y, int y_stride,
58
                             const uint8_t* WEBP_RESTRICT u,
59
                             const uint8_t* WEBP_RESTRICT v, int uv_stride,
60
                             uint8_t* WEBP_RESTRICT dst, int dst_stride,
61
0
                             int width, int height, WebPSamplerRowFunc func) {
62
0
  int j;
63
0
  for (j = 0; j < height; ++j) {
64
0
    func(y, u, v, dst, width);
65
0
    y += y_stride;
66
0
    if (j & 1) {
67
0
      u += uv_stride;
68
0
      v += uv_stride;
69
0
    }
70
0
    dst += dst_stride;
71
0
  }
72
0
}
73
74
//-----------------------------------------------------------------------------
75
// Main call
76
77
WebPSamplerRowFunc WebPSamplers[MODE_LAST];
78
79
extern VP8CPUInfo VP8GetCPUInfo;
80
extern void WebPInitSamplersSSE2(void);
81
extern void WebPInitSamplersSSE41(void);
82
extern void WebPInitSamplersMIPS32(void);
83
extern void WebPInitSamplersMIPSdspR2(void);
84
85
6
WEBP_DSP_INIT_FUNC(WebPInitSamplers) {
86
6
  WebPSamplers[MODE_RGB] = YuvToRgbRow;
87
6
  WebPSamplers[MODE_RGBA] = YuvToRgbaRow;
88
6
  WebPSamplers[MODE_BGR] = YuvToBgrRow;
89
6
  WebPSamplers[MODE_BGRA] = YuvToBgraRow;
90
6
  WebPSamplers[MODE_ARGB] = YuvToArgbRow;
91
6
  WebPSamplers[MODE_RGBA_4444] = YuvToRgba4444Row;
92
6
  WebPSamplers[MODE_RGB_565] = YuvToRgb565Row;
93
6
  WebPSamplers[MODE_rgbA] = YuvToRgbaRow;
94
6
  WebPSamplers[MODE_bgrA] = YuvToBgraRow;
95
6
  WebPSamplers[MODE_Argb] = YuvToArgbRow;
96
6
  WebPSamplers[MODE_rgbA_4444] = YuvToRgba4444Row;
97
98
  // If defined, use CPUInfo() to overwrite some pointers with faster versions.
99
6
  if (VP8GetCPUInfo != NULL) {
100
6
#if defined(WEBP_HAVE_SSE2)
101
6
    if (VP8GetCPUInfo(kSSE2)) {
102
6
      WebPInitSamplersSSE2();
103
6
    }
104
6
#endif  // WEBP_HAVE_SSE2
105
6
#if defined(WEBP_HAVE_SSE41)
106
6
    if (VP8GetCPUInfo(kSSE4_1)) {
107
6
      WebPInitSamplersSSE41();
108
6
    }
109
6
#endif  // WEBP_HAVE_SSE41
110
#if defined(WEBP_USE_MIPS32)
111
    if (VP8GetCPUInfo(kMIPS32)) {
112
      WebPInitSamplersMIPS32();
113
    }
114
#endif  // WEBP_USE_MIPS32
115
#if defined(WEBP_USE_MIPS_DSP_R2)
116
    if (VP8GetCPUInfo(kMIPSdspR2)) {
117
      WebPInitSamplersMIPSdspR2();
118
    }
119
#endif  // WEBP_USE_MIPS_DSP_R2
120
6
  }
121
6
}
122
123
//-----------------------------------------------------------------------------
124
// ARGB -> YUV converters
125
126
static void ConvertARGBToY_C(const uint32_t* WEBP_RESTRICT argb,
127
0
                             uint8_t* WEBP_RESTRICT y, int width) {
128
0
  int i;
129
0
  for (i = 0; i < width; ++i) {
130
0
    const uint32_t p = argb[i];
131
0
    y[i] =
132
0
        VP8RGBToY((p >> 16) & 0xff, (p >> 8) & 0xff, (p >> 0) & 0xff, YUV_HALF);
133
0
  }
134
0
}
135
136
void WebPConvertARGBToUV_C(const uint32_t* WEBP_RESTRICT argb,
137
                           uint8_t* WEBP_RESTRICT u, uint8_t* WEBP_RESTRICT v,
138
0
                           int src_width, int do_store) {
139
  // No rounding. Last pixel is dealt with separately.
140
0
  const int uv_width = src_width >> 1;
141
0
  int i;
142
0
  for (i = 0; i < uv_width; ++i) {
143
0
    const uint32_t v0 = argb[2 * i + 0];
144
0
    const uint32_t v1 = argb[2 * i + 1];
145
    // VP8RGBToU/V expects four accumulated pixels. Hence we need to
146
    // scale r/g/b value by a factor 2. We just shift v0/v1 one bit less.
147
0
    const int r = ((v0 >> 15) & 0x1fe) + ((v1 >> 15) & 0x1fe);
148
0
    const int g = ((v0 >> 7) & 0x1fe) + ((v1 >> 7) & 0x1fe);
149
0
    const int b = ((v0 << 1) & 0x1fe) + ((v1 << 1) & 0x1fe);
150
0
    const int tmp_u = VP8RGBToU(r, g, b, YUV_HALF << 2);
151
0
    const int tmp_v = VP8RGBToV(r, g, b, YUV_HALF << 2);
152
0
    if (do_store) {
153
0
      u[i] = tmp_u;
154
0
      v[i] = tmp_v;
155
0
    } else {
156
      // Approximated average-of-four. But it's an acceptable diff.
157
0
      u[i] = (u[i] + tmp_u + 1) >> 1;
158
0
      v[i] = (v[i] + tmp_v + 1) >> 1;
159
0
    }
160
0
  }
161
0
  if (src_width & 1) {  // last pixel
162
0
    const uint32_t v0 = argb[2 * i + 0];
163
0
    const int r = (v0 >> 14) & 0x3fc;
164
0
    const int g = (v0 >> 6) & 0x3fc;
165
0
    const int b = (v0 << 2) & 0x3fc;
166
0
    const int tmp_u = VP8RGBToU(r, g, b, YUV_HALF << 2);
167
0
    const int tmp_v = VP8RGBToV(r, g, b, YUV_HALF << 2);
168
0
    if (do_store) {
169
0
      u[i] = tmp_u;
170
0
      v[i] = tmp_v;
171
0
    } else {
172
0
      u[i] = (u[i] + tmp_u + 1) >> 1;
173
0
      v[i] = (v[i] + tmp_v + 1) >> 1;
174
0
    }
175
0
  }
176
0
}
177
178
//-----------------------------------------------------------------------------
179
180
static void ConvertRGBToY_C(const uint8_t* WEBP_RESTRICT rgb,
181
0
                            uint8_t* WEBP_RESTRICT y, int width, int step) {
182
0
  int i;
183
0
  for (i = 0; i < width; ++i, rgb += step) {
184
0
    y[i] = VP8RGBToY(rgb[0], rgb[1], rgb[2], YUV_HALF);
185
0
  }
186
0
}
187
188
static void ConvertBGRToY_C(const uint8_t* WEBP_RESTRICT bgr,
189
0
                            uint8_t* WEBP_RESTRICT y, int width, int step) {
190
0
  int i;
191
0
  for (i = 0; i < width; ++i, bgr += step) {
192
0
    y[i] = VP8RGBToY(bgr[2], bgr[1], bgr[0], YUV_HALF);
193
0
  }
194
0
}
195
196
void WebPConvertRGBA32ToUV_C(const uint16_t* WEBP_RESTRICT rgb,
197
                             uint8_t* WEBP_RESTRICT u, uint8_t* WEBP_RESTRICT v,
198
1.34M
                             int width) {
199
1.34M
  int i;
200
6.55M
  for (i = 0; i < width; i += 1, rgb += 4) {
201
5.20M
    const int r = rgb[0], g = rgb[1], b = rgb[2];
202
5.20M
    u[i] = VP8RGBToU(r, g, b, YUV_HALF << 2);
203
5.20M
    v[i] = VP8RGBToV(r, g, b, YUV_HALF << 2);
204
5.20M
  }
205
1.34M
}
206
207
//-----------------------------------------------------------------------------
208
209
void (*WebPConvertRGBToY)(const uint8_t* WEBP_RESTRICT rgb,
210
                          uint8_t* WEBP_RESTRICT y, int width, int step);
211
void (*WebPConvertBGRToY)(const uint8_t* WEBP_RESTRICT bgr,
212
                          uint8_t* WEBP_RESTRICT y, int width, int step);
213
void (*WebPConvertRGBA32ToUV)(const uint16_t* WEBP_RESTRICT rgb,
214
                              uint8_t* WEBP_RESTRICT u,
215
                              uint8_t* WEBP_RESTRICT v, int width);
216
217
void (*WebPConvertARGBToY)(const uint32_t* WEBP_RESTRICT argb,
218
                           uint8_t* WEBP_RESTRICT y, int width);
219
void (*WebPConvertARGBToUV)(const uint32_t* WEBP_RESTRICT argb,
220
                            uint8_t* WEBP_RESTRICT u, uint8_t* WEBP_RESTRICT v,
221
                            int src_width, int do_store);
222
223
extern void WebPInitConvertARGBToYUVSSE2(void);
224
extern void WebPInitConvertARGBToYUVSSE41(void);
225
extern void WebPInitConvertARGBToYUVNEON(void);
226
227
4
WEBP_DSP_INIT_FUNC(WebPInitConvertARGBToYUV) {
228
4
  WebPConvertARGBToY = ConvertARGBToY_C;
229
4
  WebPConvertARGBToUV = WebPConvertARGBToUV_C;
230
231
4
  WebPConvertRGBToY = ConvertRGBToY_C;
232
4
  WebPConvertBGRToY = ConvertBGRToY_C;
233
234
4
  WebPConvertRGBA32ToUV = WebPConvertRGBA32ToUV_C;
235
236
4
  if (VP8GetCPUInfo != NULL) {
237
4
#if defined(WEBP_HAVE_SSE2)
238
4
    if (VP8GetCPUInfo(kSSE2)) {
239
4
      WebPInitConvertARGBToYUVSSE2();
240
4
    }
241
4
#endif  // WEBP_HAVE_SSE2
242
4
#if defined(WEBP_HAVE_SSE41)
243
4
    if (VP8GetCPUInfo(kSSE4_1)) {
244
4
      WebPInitConvertARGBToYUVSSE41();
245
4
    }
246
4
#endif  // WEBP_HAVE_SSE41
247
4
  }
248
249
#if defined(WEBP_HAVE_NEON)
250
  if (WEBP_NEON_OMIT_C_CODE ||
251
      (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kNEON))) {
252
    WebPInitConvertARGBToYUVNEON();
253
  }
254
#endif  // WEBP_HAVE_NEON
255
256
4
  assert(WebPConvertARGBToY != NULL);
257
4
  assert(WebPConvertARGBToUV != NULL);
258
4
  assert(WebPConvertRGBToY != NULL);
259
4
  assert(WebPConvertBGRToY != NULL);
260
4
  assert(WebPConvertRGBA32ToUV != NULL);
261
4
}