Coverage Report

Created: 2026-02-14 06:26

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