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