xref: /aosp_15_r20/external/webp/src/dsp/yuv.c (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
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 ([email protected])
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                       uint8_t* dst, int len) {                                 \
26   const uint8_t* const end = dst + (len & ~1) * (XSTEP);                       \
27   while (dst != end) {                                                         \
28     FUNC(y[0], u[0], v[0], dst);                                               \
29     FUNC(y[1], u[0], v[0], dst + (XSTEP));                                     \
30     y += 2;                                                                    \
31     ++u;                                                                       \
32     ++v;                                                                       \
33     dst += 2 * (XSTEP);                                                        \
34   }                                                                            \
35   if (len & 1) {                                                               \
36     FUNC(y[0], u[0], v[0], dst);                                               \
37   }                                                                            \
38 }                                                                              \
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:
WebPSamplerProcessPlane(const uint8_t * y,int y_stride,const uint8_t * u,const uint8_t * v,int uv_stride,uint8_t * dst,int dst_stride,int width,int height,WebPSamplerRowFunc func)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                              int width, int height, WebPSamplerRowFunc func) {
56   int j;
57   for (j = 0; j < height; ++j) {
58     func(y, u, v, dst, width);
59     y += y_stride;
60     if (j & 1) {
61       u += uv_stride;
62       v += uv_stride;
63     }
64     dst += dst_stride;
65   }
66 }
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 
WEBP_DSP_INIT_FUNC(WebPInitSamplers)79 WEBP_DSP_INIT_FUNC(WebPInitSamplers) {
80   WebPSamplers[MODE_RGB]       = YuvToRgbRow;
81   WebPSamplers[MODE_RGBA]      = YuvToRgbaRow;
82   WebPSamplers[MODE_BGR]       = YuvToBgrRow;
83   WebPSamplers[MODE_BGRA]      = YuvToBgraRow;
84   WebPSamplers[MODE_ARGB]      = YuvToArgbRow;
85   WebPSamplers[MODE_RGBA_4444] = YuvToRgba4444Row;
86   WebPSamplers[MODE_RGB_565]   = YuvToRgb565Row;
87   WebPSamplers[MODE_rgbA]      = YuvToRgbaRow;
88   WebPSamplers[MODE_bgrA]      = YuvToBgraRow;
89   WebPSamplers[MODE_Argb]      = YuvToArgbRow;
90   WebPSamplers[MODE_rgbA_4444] = YuvToRgba4444Row;
91 
92   // If defined, use CPUInfo() to overwrite some pointers with faster versions.
93   if (VP8GetCPUInfo != NULL) {
94 #if defined(WEBP_HAVE_SSE2)
95     if (VP8GetCPUInfo(kSSE2)) {
96       WebPInitSamplersSSE2();
97     }
98 #endif  // WEBP_HAVE_SSE2
99 #if defined(WEBP_HAVE_SSE41)
100     if (VP8GetCPUInfo(kSSE4_1)) {
101       WebPInitSamplersSSE41();
102     }
103 #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   }
115 }
116 
117 //-----------------------------------------------------------------------------
118 // ARGB -> YUV converters
119 
ConvertARGBToY_C(const uint32_t * argb,uint8_t * y,int width)120 static void ConvertARGBToY_C(const uint32_t* argb, uint8_t* y, int width) {
121   int i;
122   for (i = 0; i < width; ++i) {
123     const uint32_t p = argb[i];
124     y[i] = VP8RGBToY((p >> 16) & 0xff, (p >> 8) & 0xff, (p >>  0) & 0xff,
125                      YUV_HALF);
126   }
127 }
128 
WebPConvertARGBToUV_C(const uint32_t * argb,uint8_t * u,uint8_t * v,int src_width,int do_store)129 void WebPConvertARGBToUV_C(const uint32_t* argb, uint8_t* u, uint8_t* v,
130                            int src_width, int do_store) {
131   // No rounding. Last pixel is dealt with separately.
132   const int uv_width = src_width >> 1;
133   int i;
134   for (i = 0; i < uv_width; ++i) {
135     const uint32_t v0 = argb[2 * i + 0];
136     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     const int r = ((v0 >> 15) & 0x1fe) + ((v1 >> 15) & 0x1fe);
140     const int g = ((v0 >>  7) & 0x1fe) + ((v1 >>  7) & 0x1fe);
141     const int b = ((v0 <<  1) & 0x1fe) + ((v1 <<  1) & 0x1fe);
142     const int tmp_u = VP8RGBToU(r, g, b, YUV_HALF << 2);
143     const int tmp_v = VP8RGBToV(r, g, b, YUV_HALF << 2);
144     if (do_store) {
145       u[i] = tmp_u;
146       v[i] = tmp_v;
147     } else {
148       // Approximated average-of-four. But it's an acceptable diff.
149       u[i] = (u[i] + tmp_u + 1) >> 1;
150       v[i] = (v[i] + tmp_v + 1) >> 1;
151     }
152   }
153   if (src_width & 1) {       // last pixel
154     const uint32_t v0 = argb[2 * i + 0];
155     const int r = (v0 >> 14) & 0x3fc;
156     const int g = (v0 >>  6) & 0x3fc;
157     const int b = (v0 <<  2) & 0x3fc;
158     const int tmp_u = VP8RGBToU(r, g, b, YUV_HALF << 2);
159     const int tmp_v = VP8RGBToV(r, g, b, YUV_HALF << 2);
160     if (do_store) {
161       u[i] = tmp_u;
162       v[i] = tmp_v;
163     } else {
164       u[i] = (u[i] + tmp_u + 1) >> 1;
165       v[i] = (v[i] + tmp_v + 1) >> 1;
166     }
167   }
168 }
169 
170 //-----------------------------------------------------------------------------
171 
ConvertRGB24ToY_C(const uint8_t * rgb,uint8_t * y,int width)172 static void ConvertRGB24ToY_C(const uint8_t* rgb, uint8_t* y, int width) {
173   int i;
174   for (i = 0; i < width; ++i, rgb += 3) {
175     y[i] = VP8RGBToY(rgb[0], rgb[1], rgb[2], YUV_HALF);
176   }
177 }
178 
ConvertBGR24ToY_C(const uint8_t * bgr,uint8_t * y,int width)179 static void ConvertBGR24ToY_C(const uint8_t* bgr, uint8_t* y, int width) {
180   int i;
181   for (i = 0; i < width; ++i, bgr += 3) {
182     y[i] = VP8RGBToY(bgr[2], bgr[1], bgr[0], YUV_HALF);
183   }
184 }
185 
WebPConvertRGBA32ToUV_C(const uint16_t * rgb,uint8_t * u,uint8_t * v,int width)186 void WebPConvertRGBA32ToUV_C(const uint16_t* rgb,
187                              uint8_t* u, uint8_t* v, int width) {
188   int i;
189   for (i = 0; i < width; i += 1, rgb += 4) {
190     const int r = rgb[0], g = rgb[1], b = rgb[2];
191     u[i] = VP8RGBToU(r, g, b, YUV_HALF << 2);
192     v[i] = VP8RGBToV(r, g, b, YUV_HALF << 2);
193   }
194 }
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 
WEBP_DSP_INIT_FUNC(WebPInitConvertARGBToYUV)211 WEBP_DSP_INIT_FUNC(WebPInitConvertARGBToYUV) {
212   WebPConvertARGBToY = ConvertARGBToY_C;
213   WebPConvertARGBToUV = WebPConvertARGBToUV_C;
214 
215   WebPConvertRGB24ToY = ConvertRGB24ToY_C;
216   WebPConvertBGR24ToY = ConvertBGR24ToY_C;
217 
218   WebPConvertRGBA32ToUV = WebPConvertRGBA32ToUV_C;
219 
220   if (VP8GetCPUInfo != NULL) {
221 #if defined(WEBP_HAVE_SSE2)
222     if (VP8GetCPUInfo(kSSE2)) {
223       WebPInitConvertARGBToYUVSSE2();
224     }
225 #endif  // WEBP_HAVE_SSE2
226 #if defined(WEBP_HAVE_SSE41)
227     if (VP8GetCPUInfo(kSSE4_1)) {
228       WebPInitConvertARGBToYUVSSE41();
229     }
230 #endif  // WEBP_HAVE_SSE41
231   }
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   assert(WebPConvertARGBToY != NULL);
241   assert(WebPConvertARGBToUV != NULL);
242   assert(WebPConvertRGB24ToY != NULL);
243   assert(WebPConvertBGR24ToY != NULL);
244   assert(WebPConvertRGBA32ToUV != NULL);
245 }
246