Coverage Report

Created: 2024-07-27 06:27

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