• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 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 // Image transforms and color space conversion methods for lossless decoder.
11 //
12 // Authors: Vikas Arora (vikaas.arora@gmail.com)
13 //          Jyrki Alakuijala (jyrki@google.com)
14 //          Urvang Joshi (urvang@google.com)
15 
16 #include "src/dsp/dsp.h"
17 
18 #include <assert.h>
19 #include <math.h>
20 #include <stdlib.h>
21 #include "src/dec/vp8li_dec.h"
22 #include "src/utils/endian_inl_utils.h"
23 #include "src/dsp/lossless.h"
24 #include "src/dsp/lossless_common.h"
25 
26 //------------------------------------------------------------------------------
27 // Image transforms.
28 
Average2(uint32_t a0,uint32_t a1)29 static WEBP_INLINE uint32_t Average2(uint32_t a0, uint32_t a1) {
30   return (((a0 ^ a1) & 0xfefefefeu) >> 1) + (a0 & a1);
31 }
32 
Average3(uint32_t a0,uint32_t a1,uint32_t a2)33 static WEBP_INLINE uint32_t Average3(uint32_t a0, uint32_t a1, uint32_t a2) {
34   return Average2(Average2(a0, a2), a1);
35 }
36 
Average4(uint32_t a0,uint32_t a1,uint32_t a2,uint32_t a3)37 static WEBP_INLINE uint32_t Average4(uint32_t a0, uint32_t a1,
38                                      uint32_t a2, uint32_t a3) {
39   return Average2(Average2(a0, a1), Average2(a2, a3));
40 }
41 
Clip255(uint32_t a)42 static WEBP_INLINE uint32_t Clip255(uint32_t a) {
43   if (a < 256) {
44     return a;
45   }
46   // return 0, when a is a negative integer.
47   // return 255, when a is positive.
48   return ~a >> 24;
49 }
50 
AddSubtractComponentFull(int a,int b,int c)51 static WEBP_INLINE int AddSubtractComponentFull(int a, int b, int c) {
52   return Clip255(a + b - c);
53 }
54 
ClampedAddSubtractFull(uint32_t c0,uint32_t c1,uint32_t c2)55 static WEBP_INLINE uint32_t ClampedAddSubtractFull(uint32_t c0, uint32_t c1,
56                                                    uint32_t c2) {
57   const int a = AddSubtractComponentFull(c0 >> 24, c1 >> 24, c2 >> 24);
58   const int r = AddSubtractComponentFull((c0 >> 16) & 0xff,
59                                          (c1 >> 16) & 0xff,
60                                          (c2 >> 16) & 0xff);
61   const int g = AddSubtractComponentFull((c0 >> 8) & 0xff,
62                                          (c1 >> 8) & 0xff,
63                                          (c2 >> 8) & 0xff);
64   const int b = AddSubtractComponentFull(c0 & 0xff, c1 & 0xff, c2 & 0xff);
65   return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b;
66 }
67 
AddSubtractComponentHalf(int a,int b)68 static WEBP_INLINE int AddSubtractComponentHalf(int a, int b) {
69   return Clip255(a + (a - b) / 2);
70 }
71 
ClampedAddSubtractHalf(uint32_t c0,uint32_t c1,uint32_t c2)72 static WEBP_INLINE uint32_t ClampedAddSubtractHalf(uint32_t c0, uint32_t c1,
73                                                    uint32_t c2) {
74   const uint32_t ave = Average2(c0, c1);
75   const int a = AddSubtractComponentHalf(ave >> 24, c2 >> 24);
76   const int r = AddSubtractComponentHalf((ave >> 16) & 0xff, (c2 >> 16) & 0xff);
77   const int g = AddSubtractComponentHalf((ave >> 8) & 0xff, (c2 >> 8) & 0xff);
78   const int b = AddSubtractComponentHalf((ave >> 0) & 0xff, (c2 >> 0) & 0xff);
79   return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b;
80 }
81 
82 // gcc <= 4.9 on ARM generates incorrect code in Select() when Sub3() is
83 // inlined.
84 #if defined(__arm__) && LOCAL_GCC_VERSION <= 0x409
85 # define LOCAL_INLINE __attribute__ ((noinline))
86 #else
87 # define LOCAL_INLINE WEBP_INLINE
88 #endif
89 
Sub3(int a,int b,int c)90 static LOCAL_INLINE int Sub3(int a, int b, int c) {
91   const int pb = b - c;
92   const int pa = a - c;
93   return abs(pb) - abs(pa);
94 }
95 
96 #undef LOCAL_INLINE
97 
Select(uint32_t a,uint32_t b,uint32_t c)98 static WEBP_INLINE uint32_t Select(uint32_t a, uint32_t b, uint32_t c) {
99   const int pa_minus_pb =
100       Sub3((a >> 24)       , (b >> 24)       , (c >> 24)       ) +
101       Sub3((a >> 16) & 0xff, (b >> 16) & 0xff, (c >> 16) & 0xff) +
102       Sub3((a >>  8) & 0xff, (b >>  8) & 0xff, (c >>  8) & 0xff) +
103       Sub3((a      ) & 0xff, (b      ) & 0xff, (c      ) & 0xff);
104   return (pa_minus_pb <= 0) ? a : b;
105 }
106 
107 //------------------------------------------------------------------------------
108 // Predictors
109 
Predictor0_C(uint32_t left,const uint32_t * const top)110 static uint32_t Predictor0_C(uint32_t left, const uint32_t* const top) {
111   (void)top;
112   (void)left;
113   return ARGB_BLACK;
114 }
Predictor1_C(uint32_t left,const uint32_t * const top)115 static uint32_t Predictor1_C(uint32_t left, const uint32_t* const top) {
116   (void)top;
117   return left;
118 }
Predictor2_C(uint32_t left,const uint32_t * const top)119 static uint32_t Predictor2_C(uint32_t left, const uint32_t* const top) {
120   (void)left;
121   return top[0];
122 }
Predictor3_C(uint32_t left,const uint32_t * const top)123 static uint32_t Predictor3_C(uint32_t left, const uint32_t* const top) {
124   (void)left;
125   return top[1];
126 }
Predictor4_C(uint32_t left,const uint32_t * const top)127 static uint32_t Predictor4_C(uint32_t left, const uint32_t* const top) {
128   (void)left;
129   return top[-1];
130 }
Predictor5_C(uint32_t left,const uint32_t * const top)131 static uint32_t Predictor5_C(uint32_t left, const uint32_t* const top) {
132   const uint32_t pred = Average3(left, top[0], top[1]);
133   return pred;
134 }
Predictor6_C(uint32_t left,const uint32_t * const top)135 static uint32_t Predictor6_C(uint32_t left, const uint32_t* const top) {
136   const uint32_t pred = Average2(left, top[-1]);
137   return pred;
138 }
Predictor7_C(uint32_t left,const uint32_t * const top)139 static uint32_t Predictor7_C(uint32_t left, const uint32_t* const top) {
140   const uint32_t pred = Average2(left, top[0]);
141   return pred;
142 }
Predictor8_C(uint32_t left,const uint32_t * const top)143 static uint32_t Predictor8_C(uint32_t left, const uint32_t* const top) {
144   const uint32_t pred = Average2(top[-1], top[0]);
145   (void)left;
146   return pred;
147 }
Predictor9_C(uint32_t left,const uint32_t * const top)148 static uint32_t Predictor9_C(uint32_t left, const uint32_t* const top) {
149   const uint32_t pred = Average2(top[0], top[1]);
150   (void)left;
151   return pred;
152 }
Predictor10_C(uint32_t left,const uint32_t * const top)153 static uint32_t Predictor10_C(uint32_t left, const uint32_t* const top) {
154   const uint32_t pred = Average4(left, top[-1], top[0], top[1]);
155   return pred;
156 }
Predictor11_C(uint32_t left,const uint32_t * const top)157 static uint32_t Predictor11_C(uint32_t left, const uint32_t* const top) {
158   const uint32_t pred = Select(top[0], left, top[-1]);
159   return pred;
160 }
Predictor12_C(uint32_t left,const uint32_t * const top)161 static uint32_t Predictor12_C(uint32_t left, const uint32_t* const top) {
162   const uint32_t pred = ClampedAddSubtractFull(left, top[0], top[-1]);
163   return pred;
164 }
Predictor13_C(uint32_t left,const uint32_t * const top)165 static uint32_t Predictor13_C(uint32_t left, const uint32_t* const top) {
166   const uint32_t pred = ClampedAddSubtractHalf(left, top[0], top[-1]);
167   return pred;
168 }
169 
GENERATE_PREDICTOR_ADD(Predictor0_C,PredictorAdd0_C)170 GENERATE_PREDICTOR_ADD(Predictor0_C, PredictorAdd0_C)
171 static void PredictorAdd1_C(const uint32_t* in, const uint32_t* upper,
172                             int num_pixels, uint32_t* out) {
173   int i;
174   uint32_t left = out[-1];
175   for (i = 0; i < num_pixels; ++i) {
176     out[i] = left = VP8LAddPixels(in[i], left);
177   }
178   (void)upper;
179 }
GENERATE_PREDICTOR_ADD(Predictor2_C,PredictorAdd2_C)180 GENERATE_PREDICTOR_ADD(Predictor2_C, PredictorAdd2_C)
181 GENERATE_PREDICTOR_ADD(Predictor3_C, PredictorAdd3_C)
182 GENERATE_PREDICTOR_ADD(Predictor4_C, PredictorAdd4_C)
183 GENERATE_PREDICTOR_ADD(Predictor5_C, PredictorAdd5_C)
184 GENERATE_PREDICTOR_ADD(Predictor6_C, PredictorAdd6_C)
185 GENERATE_PREDICTOR_ADD(Predictor7_C, PredictorAdd7_C)
186 GENERATE_PREDICTOR_ADD(Predictor8_C, PredictorAdd8_C)
187 GENERATE_PREDICTOR_ADD(Predictor9_C, PredictorAdd9_C)
188 GENERATE_PREDICTOR_ADD(Predictor10_C, PredictorAdd10_C)
189 GENERATE_PREDICTOR_ADD(Predictor11_C, PredictorAdd11_C)
190 GENERATE_PREDICTOR_ADD(Predictor12_C, PredictorAdd12_C)
191 GENERATE_PREDICTOR_ADD(Predictor13_C, PredictorAdd13_C)
192 
193 //------------------------------------------------------------------------------
194 
195 // Inverse prediction.
196 static void PredictorInverseTransform_C(const VP8LTransform* const transform,
197                                         int y_start, int y_end,
198                                         const uint32_t* in, uint32_t* out) {
199   const int width = transform->xsize_;
200   if (y_start == 0) {  // First Row follows the L (mode=1) mode.
201     PredictorAdd0_C(in, NULL, 1, out);
202     PredictorAdd1_C(in + 1, NULL, width - 1, out + 1);
203     in += width;
204     out += width;
205     ++y_start;
206   }
207 
208   {
209     int y = y_start;
210     const int tile_width = 1 << transform->bits_;
211     const int mask = tile_width - 1;
212     const int tiles_per_row = VP8LSubSampleSize(width, transform->bits_);
213     const uint32_t* pred_mode_base =
214         transform->data_ + (y >> transform->bits_) * tiles_per_row;
215 
216     while (y < y_end) {
217       const uint32_t* pred_mode_src = pred_mode_base;
218       int x = 1;
219       // First pixel follows the T (mode=2) mode.
220       PredictorAdd2_C(in, out - width, 1, out);
221       // .. the rest:
222       while (x < width) {
223         const VP8LPredictorAddSubFunc pred_func =
224             VP8LPredictorsAdd[((*pred_mode_src++) >> 8) & 0xf];
225         int x_end = (x & ~mask) + tile_width;
226         if (x_end > width) x_end = width;
227         pred_func(in + x, out + x - width, x_end - x, out + x);
228         x = x_end;
229       }
230       in += width;
231       out += width;
232       ++y;
233       if ((y & mask) == 0) {   // Use the same mask, since tiles are squares.
234         pred_mode_base += tiles_per_row;
235       }
236     }
237   }
238 }
239 
240 // Add green to blue and red channels (i.e. perform the inverse transform of
241 // 'subtract green').
VP8LAddGreenToBlueAndRed_C(const uint32_t * src,int num_pixels,uint32_t * dst)242 void VP8LAddGreenToBlueAndRed_C(const uint32_t* src, int num_pixels,
243                                 uint32_t* dst) {
244   int i;
245   for (i = 0; i < num_pixels; ++i) {
246     const uint32_t argb = src[i];
247     const uint32_t green = ((argb >> 8) & 0xff);
248     uint32_t red_blue = (argb & 0x00ff00ffu);
249     red_blue += (green << 16) | green;
250     red_blue &= 0x00ff00ffu;
251     dst[i] = (argb & 0xff00ff00u) | red_blue;
252   }
253 }
254 
ColorTransformDelta(int8_t color_pred,int8_t color)255 static WEBP_INLINE int ColorTransformDelta(int8_t color_pred,
256                                            int8_t color) {
257   return ((int)color_pred * color) >> 5;
258 }
259 
ColorCodeToMultipliers(uint32_t color_code,VP8LMultipliers * const m)260 static WEBP_INLINE void ColorCodeToMultipliers(uint32_t color_code,
261                                                VP8LMultipliers* const m) {
262   m->green_to_red_  = (color_code >>  0) & 0xff;
263   m->green_to_blue_ = (color_code >>  8) & 0xff;
264   m->red_to_blue_   = (color_code >> 16) & 0xff;
265 }
266 
VP8LTransformColorInverse_C(const VP8LMultipliers * const m,const uint32_t * src,int num_pixels,uint32_t * dst)267 void VP8LTransformColorInverse_C(const VP8LMultipliers* const m,
268                                  const uint32_t* src, int num_pixels,
269                                  uint32_t* dst) {
270   int i;
271   for (i = 0; i < num_pixels; ++i) {
272     const uint32_t argb = src[i];
273     const uint32_t green = argb >> 8;
274     const uint32_t red = argb >> 16;
275     int new_red = red & 0xff;
276     int new_blue = argb & 0xff;
277     new_red += ColorTransformDelta(m->green_to_red_, green);
278     new_red &= 0xff;
279     new_blue += ColorTransformDelta(m->green_to_blue_, green);
280     new_blue += ColorTransformDelta(m->red_to_blue_, new_red);
281     new_blue &= 0xff;
282     dst[i] = (argb & 0xff00ff00u) | (new_red << 16) | (new_blue);
283   }
284 }
285 
286 // Color space inverse transform.
ColorSpaceInverseTransform_C(const VP8LTransform * const transform,int y_start,int y_end,const uint32_t * src,uint32_t * dst)287 static void ColorSpaceInverseTransform_C(const VP8LTransform* const transform,
288                                          int y_start, int y_end,
289                                          const uint32_t* src, uint32_t* dst) {
290   const int width = transform->xsize_;
291   const int tile_width = 1 << transform->bits_;
292   const int mask = tile_width - 1;
293   const int safe_width = width & ~mask;
294   const int remaining_width = width - safe_width;
295   const int tiles_per_row = VP8LSubSampleSize(width, transform->bits_);
296   int y = y_start;
297   const uint32_t* pred_row =
298       transform->data_ + (y >> transform->bits_) * tiles_per_row;
299 
300   while (y < y_end) {
301     const uint32_t* pred = pred_row;
302     VP8LMultipliers m = { 0, 0, 0 };
303     const uint32_t* const src_safe_end = src + safe_width;
304     const uint32_t* const src_end = src + width;
305     while (src < src_safe_end) {
306       ColorCodeToMultipliers(*pred++, &m);
307       VP8LTransformColorInverse(&m, src, tile_width, dst);
308       src += tile_width;
309       dst += tile_width;
310     }
311     if (src < src_end) {  // Left-overs using C-version.
312       ColorCodeToMultipliers(*pred++, &m);
313       VP8LTransformColorInverse(&m, src, remaining_width, dst);
314       src += remaining_width;
315       dst += remaining_width;
316     }
317     ++y;
318     if ((y & mask) == 0) pred_row += tiles_per_row;
319   }
320 }
321 
322 // Separate out pixels packed together using pixel-bundling.
323 // We define two methods for ARGB data (uint32_t) and alpha-only data (uint8_t).
324 #define COLOR_INDEX_INVERSE(FUNC_NAME, F_NAME, STATIC_DECL, TYPE, BIT_SUFFIX,  \
325                             GET_INDEX, GET_VALUE)                              \
326 static void F_NAME(const TYPE* src, const uint32_t* const color_map,           \
327                    TYPE* dst, int y_start, int y_end, int width) {             \
328   int y;                                                                       \
329   for (y = y_start; y < y_end; ++y) {                                          \
330     int x;                                                                     \
331     for (x = 0; x < width; ++x) {                                              \
332       *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]);                        \
333     }                                                                          \
334   }                                                                            \
335 }                                                                              \
336 STATIC_DECL void FUNC_NAME(const VP8LTransform* const transform,               \
337                            int y_start, int y_end, const TYPE* src,            \
338                            TYPE* dst) {                                        \
339   int y;                                                                       \
340   const int bits_per_pixel = 8 >> transform->bits_;                            \
341   const int width = transform->xsize_;                                         \
342   const uint32_t* const color_map = transform->data_;                          \
343   if (bits_per_pixel < 8) {                                                    \
344     const int pixels_per_byte = 1 << transform->bits_;                         \
345     const int count_mask = pixels_per_byte - 1;                                \
346     const uint32_t bit_mask = (1 << bits_per_pixel) - 1;                       \
347     for (y = y_start; y < y_end; ++y) {                                        \
348       uint32_t packed_pixels = 0;                                              \
349       int x;                                                                   \
350       for (x = 0; x < width; ++x) {                                            \
351         /* We need to load fresh 'packed_pixels' once every                */  \
352         /* 'pixels_per_byte' increments of x. Fortunately, pixels_per_byte */  \
353         /* is a power of 2, so can just use a mask for that, instead of    */  \
354         /* decrementing a counter.                                         */  \
355         if ((x & count_mask) == 0) packed_pixels = GET_INDEX(*src++);          \
356         *dst++ = GET_VALUE(color_map[packed_pixels & bit_mask]);               \
357         packed_pixels >>= bits_per_pixel;                                      \
358       }                                                                        \
359     }                                                                          \
360   } else {                                                                     \
361     VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width);      \
362   }                                                                            \
363 }
364 
365 COLOR_INDEX_INVERSE(ColorIndexInverseTransform_C, MapARGB_C, static,
366                     uint32_t, 32b, VP8GetARGBIndex, VP8GetARGBValue)
367 COLOR_INDEX_INVERSE(VP8LColorIndexInverseTransformAlpha, MapAlpha_C, ,
368                     uint8_t, 8b, VP8GetAlphaIndex, VP8GetAlphaValue)
369 
370 #undef COLOR_INDEX_INVERSE
371 
VP8LInverseTransform(const VP8LTransform * const transform,int row_start,int row_end,const uint32_t * const in,uint32_t * const out)372 void VP8LInverseTransform(const VP8LTransform* const transform,
373                           int row_start, int row_end,
374                           const uint32_t* const in, uint32_t* const out) {
375   const int width = transform->xsize_;
376   assert(row_start < row_end);
377   assert(row_end <= transform->ysize_);
378   switch (transform->type_) {
379     case SUBTRACT_GREEN:
380       VP8LAddGreenToBlueAndRed(in, (row_end - row_start) * width, out);
381       break;
382     case PREDICTOR_TRANSFORM:
383       PredictorInverseTransform_C(transform, row_start, row_end, in, out);
384       if (row_end != transform->ysize_) {
385         // The last predicted row in this iteration will be the top-pred row
386         // for the first row in next iteration.
387         memcpy(out - width, out + (row_end - row_start - 1) * width,
388                width * sizeof(*out));
389       }
390       break;
391     case CROSS_COLOR_TRANSFORM:
392       ColorSpaceInverseTransform_C(transform, row_start, row_end, in, out);
393       break;
394     case COLOR_INDEXING_TRANSFORM:
395       if (in == out && transform->bits_ > 0) {
396         // Move packed pixels to the end of unpacked region, so that unpacking
397         // can occur seamlessly.
398         // Also, note that this is the only transform that applies on
399         // the effective width of VP8LSubSampleSize(xsize_, bits_). All other
400         // transforms work on effective width of xsize_.
401         const int out_stride = (row_end - row_start) * width;
402         const int in_stride = (row_end - row_start) *
403             VP8LSubSampleSize(transform->xsize_, transform->bits_);
404         uint32_t* const src = out + out_stride - in_stride;
405         memmove(src, out, in_stride * sizeof(*src));
406         ColorIndexInverseTransform_C(transform, row_start, row_end, src, out);
407       } else {
408         ColorIndexInverseTransform_C(transform, row_start, row_end, in, out);
409       }
410       break;
411   }
412 }
413 
414 //------------------------------------------------------------------------------
415 // Color space conversion.
416 
is_big_endian(void)417 static int is_big_endian(void) {
418   static const union {
419     uint16_t w;
420     uint8_t b[2];
421   } tmp = { 1 };
422   return (tmp.b[0] != 1);
423 }
424 
VP8LConvertBGRAToRGB_C(const uint32_t * src,int num_pixels,uint8_t * dst)425 void VP8LConvertBGRAToRGB_C(const uint32_t* src,
426                             int num_pixels, uint8_t* dst) {
427   const uint32_t* const src_end = src + num_pixels;
428   while (src < src_end) {
429     const uint32_t argb = *src++;
430     *dst++ = (argb >> 16) & 0xff;
431     *dst++ = (argb >>  8) & 0xff;
432     *dst++ = (argb >>  0) & 0xff;
433   }
434 }
435 
VP8LConvertBGRAToRGBA_C(const uint32_t * src,int num_pixels,uint8_t * dst)436 void VP8LConvertBGRAToRGBA_C(const uint32_t* src,
437                              int num_pixels, uint8_t* dst) {
438   const uint32_t* const src_end = src + num_pixels;
439   while (src < src_end) {
440     const uint32_t argb = *src++;
441     *dst++ = (argb >> 16) & 0xff;
442     *dst++ = (argb >>  8) & 0xff;
443     *dst++ = (argb >>  0) & 0xff;
444     *dst++ = (argb >> 24) & 0xff;
445   }
446 }
447 
VP8LConvertBGRAToRGBA4444_C(const uint32_t * src,int num_pixels,uint8_t * dst)448 void VP8LConvertBGRAToRGBA4444_C(const uint32_t* src,
449                                  int num_pixels, uint8_t* dst) {
450   const uint32_t* const src_end = src + num_pixels;
451   while (src < src_end) {
452     const uint32_t argb = *src++;
453     const uint8_t rg = ((argb >> 16) & 0xf0) | ((argb >> 12) & 0xf);
454     const uint8_t ba = ((argb >>  0) & 0xf0) | ((argb >> 28) & 0xf);
455 #if (WEBP_SWAP_16BIT_CSP == 1)
456     *dst++ = ba;
457     *dst++ = rg;
458 #else
459     *dst++ = rg;
460     *dst++ = ba;
461 #endif
462   }
463 }
464 
VP8LConvertBGRAToRGB565_C(const uint32_t * src,int num_pixels,uint8_t * dst)465 void VP8LConvertBGRAToRGB565_C(const uint32_t* src,
466                                int num_pixels, uint8_t* dst) {
467   const uint32_t* const src_end = src + num_pixels;
468   while (src < src_end) {
469     const uint32_t argb = *src++;
470     const uint8_t rg = ((argb >> 16) & 0xf8) | ((argb >> 13) & 0x7);
471     const uint8_t gb = ((argb >>  5) & 0xe0) | ((argb >>  3) & 0x1f);
472 #if (WEBP_SWAP_16BIT_CSP == 1)
473     *dst++ = gb;
474     *dst++ = rg;
475 #else
476     *dst++ = rg;
477     *dst++ = gb;
478 #endif
479   }
480 }
481 
VP8LConvertBGRAToBGR_C(const uint32_t * src,int num_pixels,uint8_t * dst)482 void VP8LConvertBGRAToBGR_C(const uint32_t* src,
483                             int num_pixels, uint8_t* dst) {
484   const uint32_t* const src_end = src + num_pixels;
485   while (src < src_end) {
486     const uint32_t argb = *src++;
487     *dst++ = (argb >>  0) & 0xff;
488     *dst++ = (argb >>  8) & 0xff;
489     *dst++ = (argb >> 16) & 0xff;
490   }
491 }
492 
CopyOrSwap(const uint32_t * src,int num_pixels,uint8_t * dst,int swap_on_big_endian)493 static void CopyOrSwap(const uint32_t* src, int num_pixels, uint8_t* dst,
494                        int swap_on_big_endian) {
495   if (is_big_endian() == swap_on_big_endian) {
496     const uint32_t* const src_end = src + num_pixels;
497     while (src < src_end) {
498       const uint32_t argb = *src++;
499       WebPUint32ToMem(dst, BSwap32(argb));
500       dst += sizeof(argb);
501     }
502   } else {
503     memcpy(dst, src, num_pixels * sizeof(*src));
504   }
505 }
506 
VP8LConvertFromBGRA(const uint32_t * const in_data,int num_pixels,WEBP_CSP_MODE out_colorspace,uint8_t * const rgba)507 void VP8LConvertFromBGRA(const uint32_t* const in_data, int num_pixels,
508                          WEBP_CSP_MODE out_colorspace, uint8_t* const rgba) {
509   switch (out_colorspace) {
510     case MODE_RGB:
511       VP8LConvertBGRAToRGB(in_data, num_pixels, rgba);
512       break;
513     case MODE_RGBA:
514       VP8LConvertBGRAToRGBA(in_data, num_pixels, rgba);
515       break;
516     case MODE_rgbA:
517       VP8LConvertBGRAToRGBA(in_data, num_pixels, rgba);
518       WebPApplyAlphaMultiply(rgba, 0, num_pixels, 1, 0);
519       break;
520     case MODE_BGR:
521       VP8LConvertBGRAToBGR(in_data, num_pixels, rgba);
522       break;
523     case MODE_BGRA:
524       CopyOrSwap(in_data, num_pixels, rgba, 1);
525       break;
526     case MODE_bgrA:
527       CopyOrSwap(in_data, num_pixels, rgba, 1);
528       WebPApplyAlphaMultiply(rgba, 0, num_pixels, 1, 0);
529       break;
530     case MODE_ARGB:
531       CopyOrSwap(in_data, num_pixels, rgba, 0);
532       break;
533     case MODE_Argb:
534       CopyOrSwap(in_data, num_pixels, rgba, 0);
535       WebPApplyAlphaMultiply(rgba, 1, num_pixels, 1, 0);
536       break;
537     case MODE_RGBA_4444:
538       VP8LConvertBGRAToRGBA4444(in_data, num_pixels, rgba);
539       break;
540     case MODE_rgbA_4444:
541       VP8LConvertBGRAToRGBA4444(in_data, num_pixels, rgba);
542       WebPApplyAlphaMultiply4444(rgba, num_pixels, 1, 0);
543       break;
544     case MODE_RGB_565:
545       VP8LConvertBGRAToRGB565(in_data, num_pixels, rgba);
546       break;
547     default:
548       assert(0);          // Code flow should not reach here.
549   }
550 }
551 
552 //------------------------------------------------------------------------------
553 
554 VP8LProcessDecBlueAndRedFunc VP8LAddGreenToBlueAndRed;
555 VP8LPredictorAddSubFunc VP8LPredictorsAdd[16];
556 VP8LPredictorFunc VP8LPredictors[16];
557 
558 // exposed plain-C implementations
559 VP8LPredictorAddSubFunc VP8LPredictorsAdd_C[16];
560 VP8LPredictorFunc VP8LPredictors_C[16];
561 
562 VP8LTransformColorInverseFunc VP8LTransformColorInverse;
563 
564 VP8LConvertFunc VP8LConvertBGRAToRGB;
565 VP8LConvertFunc VP8LConvertBGRAToRGBA;
566 VP8LConvertFunc VP8LConvertBGRAToRGBA4444;
567 VP8LConvertFunc VP8LConvertBGRAToRGB565;
568 VP8LConvertFunc VP8LConvertBGRAToBGR;
569 
570 VP8LMapARGBFunc VP8LMapColor32b;
571 VP8LMapAlphaFunc VP8LMapColor8b;
572 
573 extern void VP8LDspInitSSE2(void);
574 extern void VP8LDspInitNEON(void);
575 extern void VP8LDspInitMIPSdspR2(void);
576 extern void VP8LDspInitMSA(void);
577 
578 #define COPY_PREDICTOR_ARRAY(IN, OUT) do {                \
579   (OUT)[0] = IN##0_C;                                     \
580   (OUT)[1] = IN##1_C;                                     \
581   (OUT)[2] = IN##2_C;                                     \
582   (OUT)[3] = IN##3_C;                                     \
583   (OUT)[4] = IN##4_C;                                     \
584   (OUT)[5] = IN##5_C;                                     \
585   (OUT)[6] = IN##6_C;                                     \
586   (OUT)[7] = IN##7_C;                                     \
587   (OUT)[8] = IN##8_C;                                     \
588   (OUT)[9] = IN##9_C;                                     \
589   (OUT)[10] = IN##10_C;                                   \
590   (OUT)[11] = IN##11_C;                                   \
591   (OUT)[12] = IN##12_C;                                   \
592   (OUT)[13] = IN##13_C;                                   \
593   (OUT)[14] = IN##0_C; /* <- padding security sentinels*/ \
594   (OUT)[15] = IN##0_C;                                    \
595 } while (0);
596 
WEBP_DSP_INIT_FUNC(VP8LDspInit)597 WEBP_DSP_INIT_FUNC(VP8LDspInit) {
598   COPY_PREDICTOR_ARRAY(Predictor, VP8LPredictors)
599   COPY_PREDICTOR_ARRAY(Predictor, VP8LPredictors_C)
600   COPY_PREDICTOR_ARRAY(PredictorAdd, VP8LPredictorsAdd)
601   COPY_PREDICTOR_ARRAY(PredictorAdd, VP8LPredictorsAdd_C)
602 
603 #if !WEBP_NEON_OMIT_C_CODE
604   VP8LAddGreenToBlueAndRed = VP8LAddGreenToBlueAndRed_C;
605 
606   VP8LTransformColorInverse = VP8LTransformColorInverse_C;
607 
608   VP8LConvertBGRAToRGBA = VP8LConvertBGRAToRGBA_C;
609   VP8LConvertBGRAToRGB = VP8LConvertBGRAToRGB_C;
610   VP8LConvertBGRAToBGR = VP8LConvertBGRAToBGR_C;
611 #endif
612 
613   VP8LConvertBGRAToRGBA4444 = VP8LConvertBGRAToRGBA4444_C;
614   VP8LConvertBGRAToRGB565 = VP8LConvertBGRAToRGB565_C;
615 
616   VP8LMapColor32b = MapARGB_C;
617   VP8LMapColor8b = MapAlpha_C;
618 
619   // If defined, use CPUInfo() to overwrite some pointers with faster versions.
620   if (VP8GetCPUInfo != NULL) {
621 #if defined(WEBP_USE_SSE2)
622     if (VP8GetCPUInfo(kSSE2)) {
623       VP8LDspInitSSE2();
624     }
625 #endif
626 #if defined(WEBP_USE_MIPS_DSP_R2)
627     if (VP8GetCPUInfo(kMIPSdspR2)) {
628       VP8LDspInitMIPSdspR2();
629     }
630 #endif
631 #if defined(WEBP_USE_MSA)
632     if (VP8GetCPUInfo(kMSA)) {
633       VP8LDspInitMSA();
634     }
635 #endif
636   }
637 
638 #if defined(WEBP_USE_NEON)
639   if (WEBP_NEON_OMIT_C_CODE ||
640       (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kNEON))) {
641     VP8LDspInitNEON();
642   }
643 #endif
644 
645   assert(VP8LAddGreenToBlueAndRed != NULL);
646   assert(VP8LTransformColorInverse != NULL);
647   assert(VP8LConvertBGRAToRGBA != NULL);
648   assert(VP8LConvertBGRAToRGB != NULL);
649   assert(VP8LConvertBGRAToBGR != NULL);
650   assert(VP8LConvertBGRAToRGBA4444 != NULL);
651   assert(VP8LConvertBGRAToRGB565 != NULL);
652   assert(VP8LMapColor32b != NULL);
653   assert(VP8LMapColor8b != NULL);
654 }
655 #undef COPY_PREDICTOR_ARRAY
656 
657 //------------------------------------------------------------------------------
658