Coverage Report

Created: 2024-07-27 06:27

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