Coverage Report

Created: 2025-12-03 07:28

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.4M
static WEBP_INLINE uint32_t Average2(uint32_t a0, uint32_t a1) {
36
33.4M
  return (((a0 ^ a1) & 0xfefefefeu) >> 1) + (a0 & a1);
37
33.4M
}
38
39
3.15M
static WEBP_INLINE uint32_t Average3(uint32_t a0, uint32_t a1, uint32_t a2) {
40
3.15M
  return Average2(Average2(a0, a2), a1);
41
3.15M
}
42
43
static WEBP_INLINE uint32_t Average4(uint32_t a0, uint32_t a1, uint32_t a2,
44
3.63M
                                     uint32_t a3) {
45
3.63M
  return Average2(Average2(a0, a1), Average2(a2, a3));
46
3.63M
}
47
48
27.8M
static WEBP_INLINE uint32_t Clip255(uint32_t a) {
49
27.8M
  if (a < 256) {
50
27.2M
    return a;
51
27.2M
  }
52
  // return 0, when a is a negative integer.
53
  // return 255, when a is positive.
54
569k
  return ~a >> 24;
55
27.8M
}
56
57
15.2M
static WEBP_INLINE int AddSubtractComponentFull(int a, int b, int c) {
58
15.2M
  return Clip255((uint32_t)(a + b - c));
59
15.2M
}
60
61
static WEBP_INLINE uint32_t ClampedAddSubtractFull(uint32_t c0, uint32_t c1,
62
3.81M
                                                   uint32_t c2) {
63
3.81M
  const int a = AddSubtractComponentFull(c0 >> 24, c1 >> 24, c2 >> 24);
64
3.81M
  const int r = AddSubtractComponentFull((c0 >> 16) & 0xff, (c1 >> 16) & 0xff,
65
3.81M
                                         (c2 >> 16) & 0xff);
66
3.81M
  const int g = AddSubtractComponentFull((c0 >> 8) & 0xff, (c1 >> 8) & 0xff,
67
3.81M
                                         (c2 >> 8) & 0xff);
68
3.81M
  const int b = AddSubtractComponentFull(c0 & 0xff, c1 & 0xff, c2 & 0xff);
69
3.81M
  return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b;
70
3.81M
}
71
72
12.5M
static WEBP_INLINE int AddSubtractComponentHalf(int a, int b) {
73
12.5M
  return Clip255((uint32_t)(a + (a - b) / 2));
74
12.5M
}
75
76
static WEBP_INLINE uint32_t ClampedAddSubtractHalf(uint32_t c0, uint32_t c1,
77
3.14M
                                                   uint32_t c2) {
78
3.14M
  const uint32_t ave = Average2(c0, c1);
79
3.14M
  const int a = AddSubtractComponentHalf(ave >> 24, c2 >> 24);
80
3.14M
  const int r = AddSubtractComponentHalf((ave >> 16) & 0xff, (c2 >> 16) & 0xff);
81
3.14M
  const int g = AddSubtractComponentHalf((ave >> 8) & 0xff, (c2 >> 8) & 0xff);
82
3.14M
  const int b = AddSubtractComponentHalf((ave >> 0) & 0xff, (c2 >> 0) & 0xff);
83
3.14M
  return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b;
84
3.14M
}
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
14.0M
static LOCAL_INLINE int Sub3(int a, int b, int c) {
95
14.0M
  const int pb = b - c;
96
14.0M
  const int pa = a - c;
97
14.0M
  return abs(pb) - abs(pa);
98
14.0M
}
99
100
#undef LOCAL_INLINE
101
102
3.51M
static WEBP_INLINE uint32_t Select(uint32_t a, uint32_t b, uint32_t c) {
103
3.51M
  const int pa_minus_pb =
104
3.51M
      Sub3((a >> 24), (b >> 24), (c >> 24)) +
105
3.51M
      Sub3((a >> 16) & 0xff, (b >> 16) & 0xff, (c >> 16) & 0xff) +
106
3.51M
      Sub3((a >> 8) & 0xff, (b >> 8) & 0xff, (c >> 8) & 0xff) +
107
3.51M
      Sub3((a) & 0xff, (b) & 0xff, (c) & 0xff);
108
3.51M
  return (pa_minus_pb <= 0) ? a : b;
109
3.51M
}
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
16.3M
                          const uint32_t* const top) {
127
16.3M
  (void)left;
128
16.3M
  return top[0];
129
16.3M
}
130
uint32_t VP8LPredictor3_C(const uint32_t* const left,
131
3.31M
                          const uint32_t* const top) {
132
3.31M
  (void)left;
133
3.31M
  return top[1];
134
3.31M
}
135
uint32_t VP8LPredictor4_C(const uint32_t* const left,
136
3.52M
                          const uint32_t* const top) {
137
3.52M
  (void)left;
138
3.52M
  return top[-1];
139
3.52M
}
140
uint32_t VP8LPredictor5_C(const uint32_t* const left,
141
3.15M
                          const uint32_t* const top) {
142
3.15M
  const uint32_t pred = Average3(*left, top[0], top[1]);
143
3.15M
  return pred;
144
3.15M
}
145
uint32_t VP8LPredictor6_C(const uint32_t* const left,
146
3.27M
                          const uint32_t* const top) {
147
3.27M
  const uint32_t pred = Average2(*left, top[-1]);
148
3.27M
  return pred;
149
3.27M
}
150
uint32_t VP8LPredictor7_C(const uint32_t* const left,
151
3.12M
                          const uint32_t* const top) {
152
3.12M
  const uint32_t pred = Average2(*left, top[0]);
153
3.12M
  return pred;
154
3.12M
}
155
uint32_t VP8LPredictor8_C(const uint32_t* const left,
156
3.31M
                          const uint32_t* const top) {
157
3.31M
  const uint32_t pred = Average2(top[-1], top[0]);
158
3.31M
  (void)left;
159
3.31M
  return pred;
160
3.31M
}
161
uint32_t VP8LPredictor9_C(const uint32_t* const left,
162
3.33M
                          const uint32_t* const top) {
163
3.33M
  const uint32_t pred = Average2(top[0], top[1]);
164
3.33M
  (void)left;
165
3.33M
  return pred;
166
3.33M
}
167
uint32_t VP8LPredictor10_C(const uint32_t* const left,
168
3.63M
                           const uint32_t* const top) {
169
3.63M
  const uint32_t pred = Average4(*left, top[-1], top[0], top[1]);
170
3.63M
  return pred;
171
3.63M
}
172
uint32_t VP8LPredictor11_C(const uint32_t* const left,
173
3.51M
                           const uint32_t* const top) {
174
3.51M
  const uint32_t pred = Select(top[0], *left, top[-1]);
175
3.51M
  return pred;
176
3.51M
}
177
uint32_t VP8LPredictor12_C(const uint32_t* const left,
178
3.81M
                           const uint32_t* const top) {
179
3.81M
  const uint32_t pred = ClampedAddSubtractFull(*left, top[0], top[-1]);
180
3.81M
  return pred;
181
3.81M
}
182
uint32_t VP8LPredictor13_C(const uint32_t* const left,
183
3.14M
                           const uint32_t* const top) {
184
3.14M
  const uint32_t pred = ClampedAddSubtractHalf(*left, top[0], top[-1]);
185
3.14M
  return pred;
186
3.14M
}
187
188
static void PredictorAdd0_C(const uint32_t* in, const uint32_t* upper,
189
367k
                            int num_pixels, uint32_t* WEBP_RESTRICT out) {
190
367k
  int x;
191
367k
  (void)upper;
192
1.24M
  for (x = 0; x < num_pixels; ++x) out[x] = VP8LAddPixels(in[x], ARGB_BLACK);
193
367k
}
194
static void PredictorAdd1_C(const uint32_t* in, const uint32_t* upper,
195
404k
                            int num_pixels, uint32_t* WEBP_RESTRICT out) {
196
404k
  int i;
197
404k
  uint32_t left = out[-1];
198
404k
  (void)upper;
199
2.39M
  for (i = 0; i < num_pixels; ++i) {
200
1.99M
    out[i] = left = VP8LAddPixels(in[i], left);
201
1.99M
  }
202
404k
}
203
1.27M
GENERATE_PREDICTOR_ADD(VP8LPredictor2_C, PredictorAdd2_C)
204
25.1k
GENERATE_PREDICTOR_ADD(VP8LPredictor3_C, PredictorAdd3_C)
205
88.2k
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
56.9k
GENERATE_PREDICTOR_ADD(VP8LPredictor8_C, PredictorAdd8_C)
210
56.1k
GENERATE_PREDICTOR_ADD(VP8LPredictor9_C, PredictorAdd9_C)
211
121k
GENERATE_PREDICTOR_ADD(VP8LPredictor10_C, PredictorAdd10_C)
212
80.5k
GENERATE_PREDICTOR_ADD(VP8LPredictor11_C, PredictorAdd11_C)
213
203k
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
78.6k
                                        const uint32_t* in, uint32_t* out) {
222
78.6k
  const int width = transform->xsize;
223
78.6k
  if (y_start == 0) {  // First Row follows the L (mode=1) mode.
224
3.66k
    PredictorAdd0_C(in, NULL, 1, out);
225
3.66k
    PredictorAdd1_C(in + 1, NULL, width - 1, out + 1);
226
3.66k
    in += width;
227
3.66k
    out += width;
228
3.66k
    ++y_start;
229
3.66k
  }
230
231
78.6k
  {
232
78.6k
    int y = y_start;
233
78.6k
    const int tile_width = 1 << transform->bits;
234
78.6k
    const int mask = tile_width - 1;
235
78.6k
    const int tiles_per_row = VP8LSubSampleSize(width, transform->bits);
236
78.6k
    const uint32_t* pred_mode_base =
237
78.6k
        transform->data + (y >> transform->bits) * tiles_per_row;
238
239
1.27M
    while (y < y_end) {
240
1.19M
      const uint32_t* pred_mode_src = pred_mode_base;
241
1.19M
      int x = 1;
242
      // First pixel follows the T (mode=2) mode.
243
1.19M
      PredictorAdd2_C(in, out - width, 1, out);
244
      // .. the rest:
245
16.2M
      while (x < width) {
246
15.1M
        const VP8LPredictorAddSubFunc pred_func =
247
15.1M
            VP8LPredictorsAdd[((*pred_mode_src++) >> 8) & 0xf];
248
15.1M
        int x_end = (x & ~mask) + tile_width;
249
15.1M
        if (x_end > width) x_end = width;
250
15.1M
        pred_func(in + x, out + x - width, x_end - x, out + x);
251
15.1M
        x = x_end;
252
15.1M
      }
253
1.19M
      in += width;
254
1.19M
      out += width;
255
1.19M
      ++y;
256
1.19M
      if ((y & mask) == 0) {  // Use the same mask, since tiles are squares.
257
23.1k
        pred_mode_base += tiles_per_row;
258
23.1k
      }
259
1.19M
    }
260
78.6k
  }
261
78.6k
}
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
6.11k
                                uint32_t* dst) {
267
6.11k
  int i;
268
12.8k
  for (i = 0; i < num_pixels; ++i) {
269
6.70k
    const uint32_t argb = src[i];
270
6.70k
    const uint32_t green = ((argb >> 8) & 0xff);
271
6.70k
    uint32_t red_blue = (argb & 0x00ff00ffu);
272
6.70k
    red_blue += (green << 16) | green;
273
6.70k
    red_blue &= 0x00ff00ffu;
274
6.70k
    dst[i] = (argb & 0xff00ff00u) | red_blue;
275
6.70k
  }
276
6.11k
}
277
278
208k
static WEBP_INLINE int ColorTransformDelta(int8_t color_pred, int8_t color) {
279
208k
  return ((int)color_pred * color) >> 5;
280
208k
}
281
282
static WEBP_INLINE void ColorCodeToMultipliers(uint32_t color_code,
283
768k
                                               VP8LMultipliers* const m) {
284
768k
  m->green_to_red = (color_code >> 0) & 0xff;
285
768k
  m->green_to_blue = (color_code >> 8) & 0xff;
286
768k
  m->red_to_blue = (color_code >> 16) & 0xff;
287
768k
}
288
289
void VP8LTransformColorInverse_C(const VP8LMultipliers* const m,
290
                                 const uint32_t* src, int num_pixels,
291
36.3k
                                 uint32_t* dst) {
292
36.3k
  int i;
293
105k
  for (i = 0; i < num_pixels; ++i) {
294
69.4k
    const uint32_t argb = src[i];
295
69.4k
    const int8_t green = (int8_t)(argb >> 8);
296
69.4k
    const uint32_t red = argb >> 16;
297
69.4k
    int new_red = red & 0xff;
298
69.4k
    int new_blue = argb & 0xff;
299
69.4k
    new_red += ColorTransformDelta((int8_t)m->green_to_red, green);
300
69.4k
    new_red &= 0xff;
301
69.4k
    new_blue += ColorTransformDelta((int8_t)m->green_to_blue, green);
302
69.4k
    new_blue += ColorTransformDelta((int8_t)m->red_to_blue, (int8_t)new_red);
303
69.4k
    new_blue &= 0xff;
304
69.4k
    dst[i] = (argb & 0xff00ff00u) | (new_red << 16) | (new_blue);
305
69.4k
  }
306
36.3k
}
307
308
// Color space inverse transform.
309
static void ColorSpaceInverseTransform_C(const VP8LTransform* const transform,
310
                                         int y_start, int y_end,
311
5.98k
                                         const uint32_t* src, uint32_t* dst) {
312
5.98k
  const int width = transform->xsize;
313
5.98k
  const int tile_width = 1 << transform->bits;
314
5.98k
  const int mask = tile_width - 1;
315
5.98k
  const int safe_width = width & ~mask;
316
5.98k
  const int remaining_width = width - safe_width;
317
5.98k
  const int tiles_per_row = VP8LSubSampleSize(width, transform->bits);
318
5.98k
  int y = y_start;
319
5.98k
  const uint32_t* pred_row =
320
5.98k
      transform->data + (y >> transform->bits) * tiles_per_row;
321
322
49.5k
  while (y < y_end) {
323
43.5k
    const uint32_t* pred = pred_row;
324
43.5k
    VP8LMultipliers m = {0, 0, 0};
325
43.5k
    const uint32_t* const src_safe_end = src + safe_width;
326
43.5k
    const uint32_t* const src_end = src + width;
327
772k
    while (src < src_safe_end) {
328
729k
      ColorCodeToMultipliers(*pred++, &m);
329
729k
      VP8LTransformColorInverse(&m, src, tile_width, dst);
330
729k
      src += tile_width;
331
729k
      dst += tile_width;
332
729k
    }
333
43.5k
    if (src < src_end) {  // Left-overs using C-version.
334
39.6k
      ColorCodeToMultipliers(*pred++, &m);
335
39.6k
      VP8LTransformColorInverse(&m, src, remaining_width, dst);
336
39.6k
      src += remaining_width;
337
39.6k
      dst += remaining_width;
338
39.6k
    }
339
43.5k
    ++y;
340
43.5k
    if ((y & mask) == 0) pred_row += tiles_per_row;
341
43.5k
  }
342
5.98k
}
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
27.9k
                   TYPE* dst, int y_start, int y_end, int width) {             \
351
27.9k
  int y;                                                                       \
352
93.8k
  for (y = y_start; y < y_end; ++y) {                                          \
353
65.9k
    int x;                                                                     \
354
8.56M
    for (x = 0; x < width; ++x) {                                              \
355
8.49M
      *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]);                        \
356
8.49M
    }                                                                          \
357
65.9k
  }                                                                            \
358
27.9k
}                                                                              \
lossless.c:MapARGB_C
Line
Count
Source
350
26.2k
                   TYPE* dst, int y_start, int y_end, int width) {             \
351
26.2k
  int y;                                                                       \
352
70.1k
  for (y = y_start; y < y_end; ++y) {                                          \
353
43.8k
    int x;                                                                     \
354
8.26M
    for (x = 0; x < width; ++x) {                                              \
355
8.22M
      *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]);                        \
356
8.22M
    }                                                                          \
357
43.8k
  }                                                                            \
358
26.2k
}                                                                              \
lossless.c:MapAlpha_C
Line
Count
Source
350
1.66k
                   TYPE* dst, int y_start, int y_end, int width) {             \
351
1.66k
  int y;                                                                       \
352
23.7k
  for (y = y_start; y < y_end; ++y) {                                          \
353
22.1k
    int x;                                                                     \
354
296k
    for (x = 0; x < width; ++x) {                                              \
355
274k
      *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]);                        \
356
274k
    }                                                                          \
357
22.1k
  }                                                                            \
358
1.66k
}                                                                              \
359
STATIC_DECL void FUNC_NAME(const VP8LTransform* const transform,               \
360
                           int y_start, int y_end, const TYPE* src,            \
361
33.2k
                           TYPE* dst) {                                        \
362
33.2k
  int y;                                                                       \
363
33.2k
  const int bits_per_pixel = 8 >> transform->bits;                             \
364
33.2k
  const int width = transform->xsize;                                          \
365
33.2k
  const uint32_t* const color_map = transform->data;                           \
366
33.2k
  if (bits_per_pixel < 8) {                                                    \
367
5.32k
    const int pixels_per_byte = 1 << transform->bits;                          \
368
5.32k
    const int count_mask = pixels_per_byte - 1;                                \
369
5.32k
    const uint32_t bit_mask = (1 << bits_per_pixel) - 1;                       \
370
83.0k
    for (y = y_start; y < y_end; ++y) {                                        \
371
77.6k
      uint32_t packed_pixels = 0;                                              \
372
77.6k
      int x;                                                                   \
373
9.90M
      for (x = 0; x < width; ++x) {                                            \
374
9.83M
        /* We need to load fresh 'packed_pixels' once every                */  \
375
9.83M
        /* 'pixels_per_byte' increments of x. Fortunately, pixels_per_byte */  \
376
9.83M
        /* is a power of 2, so can just use a mask for that, instead of    */  \
377
9.83M
        /* decrementing a counter.                                         */  \
378
9.83M
        if ((x & count_mask) == 0) packed_pixels = GET_INDEX(*src++);          \
379
9.83M
        *dst++ = GET_VALUE(color_map[packed_pixels & bit_mask]);               \
380
9.83M
        packed_pixels >>= bits_per_pixel;                                      \
381
9.83M
      }                                                                        \
382
77.6k
    }                                                                          \
383
27.9k
  } else {                                                                     \
384
27.9k
    VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width);      \
385
27.9k
  }                                                                            \
386
33.2k
}
VP8LColorIndexInverseTransformAlpha
Line
Count
Source
361
5.33k
                           TYPE* dst) {                                        \
362
5.33k
  int y;                                                                       \
363
5.33k
  const int bits_per_pixel = 8 >> transform->bits;                             \
364
5.33k
  const int width = transform->xsize;                                          \
365
5.33k
  const uint32_t* const color_map = transform->data;                           \
366
5.33k
  if (bits_per_pixel < 8) {                                                    \
367
3.67k
    const int pixels_per_byte = 1 << transform->bits;                          \
368
3.67k
    const int count_mask = pixels_per_byte - 1;                                \
369
3.67k
    const uint32_t bit_mask = (1 << bits_per_pixel) - 1;                       \
370
57.7k
    for (y = y_start; y < y_end; ++y) {                                        \
371
54.0k
      uint32_t packed_pixels = 0;                                              \
372
54.0k
      int x;                                                                   \
373
2.62M
      for (x = 0; x < width; ++x) {                                            \
374
2.56M
        /* We need to load fresh 'packed_pixels' once every                */  \
375
2.56M
        /* 'pixels_per_byte' increments of x. Fortunately, pixels_per_byte */  \
376
2.56M
        /* is a power of 2, so can just use a mask for that, instead of    */  \
377
2.56M
        /* decrementing a counter.                                         */  \
378
2.56M
        if ((x & count_mask) == 0) packed_pixels = GET_INDEX(*src++);          \
379
2.56M
        *dst++ = GET_VALUE(color_map[packed_pixels & bit_mask]);               \
380
2.56M
        packed_pixels >>= bits_per_pixel;                                      \
381
2.56M
      }                                                                        \
382
54.0k
    }                                                                          \
383
3.67k
  } else {                                                                     \
384
1.66k
    VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width);      \
385
1.66k
  }                                                                            \
386
5.33k
}
lossless.c:ColorIndexInverseTransform_C
Line
Count
Source
361
27.9k
                           TYPE* dst) {                                        \
362
27.9k
  int y;                                                                       \
363
27.9k
  const int bits_per_pixel = 8 >> transform->bits;                             \
364
27.9k
  const int width = transform->xsize;                                          \
365
27.9k
  const uint32_t* const color_map = transform->data;                           \
366
27.9k
  if (bits_per_pixel < 8) {                                                    \
367
1.65k
    const int pixels_per_byte = 1 << transform->bits;                          \
368
1.65k
    const int count_mask = pixels_per_byte - 1;                                \
369
1.65k
    const uint32_t bit_mask = (1 << bits_per_pixel) - 1;                       \
370
25.2k
    for (y = y_start; y < y_end; ++y) {                                        \
371
23.5k
      uint32_t packed_pixels = 0;                                              \
372
23.5k
      int x;                                                                   \
373
7.28M
      for (x = 0; x < width; ++x) {                                            \
374
7.26M
        /* We need to load fresh 'packed_pixels' once every                */  \
375
7.26M
        /* 'pixels_per_byte' increments of x. Fortunately, pixels_per_byte */  \
376
7.26M
        /* is a power of 2, so can just use a mask for that, instead of    */  \
377
7.26M
        /* decrementing a counter.                                         */  \
378
7.26M
        if ((x & count_mask) == 0) packed_pixels = GET_INDEX(*src++);          \
379
7.26M
        *dst++ = GET_VALUE(color_map[packed_pixels & bit_mask]);               \
380
7.26M
        packed_pixels >>= bits_per_pixel;                                      \
381
7.26M
      }                                                                        \
382
23.5k
    }                                                                          \
383
26.2k
  } else {                                                                     \
384
26.2k
    VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width);      \
385
26.2k
  }                                                                            \
386
27.9k
}
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
127k
                          uint32_t* const out) {
399
127k
  const int width = transform->xsize;
400
127k
  assert(row_start < row_end);
401
127k
  assert(row_end <= transform->ysize);
402
127k
  switch (transform->type) {
403
14.9k
    case SUBTRACT_GREEN_TRANSFORM:
404
14.9k
      VP8LAddGreenToBlueAndRed(in, (row_end - row_start) * width, out);
405
14.9k
      break;
406
78.6k
    case PREDICTOR_TRANSFORM:
407
78.6k
      PredictorInverseTransform_C(transform, row_start, row_end, in, out);
408
78.6k
      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
75.1k
        memcpy(out - width, out + (row_end - row_start - 1) * width,
412
75.1k
               width * sizeof(*out));
413
75.1k
      }
414
78.6k
      break;
415
5.98k
    case CROSS_COLOR_TRANSFORM:
416
5.98k
      ColorSpaceInverseTransform_C(transform, row_start, row_end, in, out);
417
5.98k
      break;
418
27.9k
    case COLOR_INDEXING_TRANSFORM:
419
27.9k
      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
743
        const int out_stride = (row_end - row_start) * width;
426
743
        const int in_stride =
427
743
            (row_end - row_start) *
428
743
            VP8LSubSampleSize(transform->xsize, transform->bits);
429
743
        uint32_t* const src = out + out_stride - in_stride;
430
743
        memmove(src, out, in_stride * sizeof(*src));
431
743
        ColorIndexInverseTransform_C(transform, row_start, row_end, src, out);
432
27.1k
      } else {
433
27.1k
        ColorIndexInverseTransform_C(transform, row_start, row_end, in, out);
434
27.1k
      }
435
27.9k
      break;
436
127k
  }
437
127k
}
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
97.2k
                            uint8_t* WEBP_RESTRICT dst) {
452
97.2k
  const uint32_t* const src_end = src + num_pixels;
453
460k
  while (src < src_end) {
454
363k
    const uint32_t argb = *src++;
455
363k
    *dst++ = (argb >> 16) & 0xff;
456
363k
    *dst++ = (argb >> 8) & 0xff;
457
363k
    *dst++ = (argb >> 0) & 0xff;
458
363k
  }
459
97.2k
}
460
461
void VP8LConvertBGRAToRGBA_C(const uint32_t* WEBP_RESTRICT src, int num_pixels,
462
2.20M
                             uint8_t* WEBP_RESTRICT dst) {
463
2.20M
  const uint32_t* const src_end = src + num_pixels;
464
10.0M
  while (src < src_end) {
465
7.83M
    const uint32_t argb = *src++;
466
7.83M
    *dst++ = (argb >> 16) & 0xff;
467
7.83M
    *dst++ = (argb >> 8) & 0xff;
468
7.83M
    *dst++ = (argb >> 0) & 0xff;
469
7.83M
    *dst++ = (argb >> 24) & 0xff;
470
7.83M
  }
471
2.20M
}
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.39M
                         WEBP_CSP_MODE out_colorspace, uint8_t* const rgba) {
534
1.39M
  switch (out_colorspace) {
535
108k
    case MODE_RGB:
536
108k
      VP8LConvertBGRAToRGB(in_data, num_pixels, rgba);
537
108k
      break;
538
1.28M
    case MODE_RGBA:
539
1.28M
      VP8LConvertBGRAToRGBA(in_data, num_pixels, rgba);
540
1.28M
      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.39M
  }
575
1.39M
}
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
21
  do {                                                      \
612
21
    (OUT)[0] = IN##0_C;                                     \
613
21
    (OUT)[1] = IN##1_C;                                     \
614
21
    (OUT)[2] = IN##2_C;                                     \
615
21
    (OUT)[3] = IN##3_C;                                     \
616
21
    (OUT)[4] = IN##4_C;                                     \
617
21
    (OUT)[5] = IN##5_C;                                     \
618
21
    (OUT)[6] = IN##6_C;                                     \
619
21
    (OUT)[7] = IN##7_C;                                     \
620
21
    (OUT)[8] = IN##8_C;                                     \
621
21
    (OUT)[9] = IN##9_C;                                     \
622
21
    (OUT)[10] = IN##10_C;                                   \
623
21
    (OUT)[11] = IN##11_C;                                   \
624
21
    (OUT)[12] = IN##12_C;                                   \
625
21
    (OUT)[13] = IN##13_C;                                   \
626
21
    (OUT)[14] = IN##0_C; /* <- padding security sentinels*/ \
627
21
    (OUT)[15] = IN##0_C;                                    \
628
21
  } while (0);
629
630
7
WEBP_DSP_INIT_FUNC(VP8LDspInit) {
631
7
  COPY_PREDICTOR_ARRAY(VP8LPredictor, VP8LPredictors)
632
7
  COPY_PREDICTOR_ARRAY(PredictorAdd, VP8LPredictorsAdd)
633
7
  COPY_PREDICTOR_ARRAY(PredictorAdd, VP8LPredictorsAdd_C)
634
635
7
#if !WEBP_NEON_OMIT_C_CODE
636
7
  VP8LAddGreenToBlueAndRed = VP8LAddGreenToBlueAndRed_C;
637
638
7
  VP8LTransformColorInverse = VP8LTransformColorInverse_C;
639
640
7
  VP8LConvertBGRAToRGBA = VP8LConvertBGRAToRGBA_C;
641
7
  VP8LConvertBGRAToRGB = VP8LConvertBGRAToRGB_C;
642
7
  VP8LConvertBGRAToBGR = VP8LConvertBGRAToBGR_C;
643
7
#endif
644
645
7
  VP8LConvertBGRAToRGBA4444 = VP8LConvertBGRAToRGBA4444_C;
646
7
  VP8LConvertBGRAToRGB565 = VP8LConvertBGRAToRGB565_C;
647
648
7
  VP8LMapColor32b = MapARGB_C;
649
7
  VP8LMapColor8b = MapAlpha_C;
650
651
  // If defined, use CPUInfo() to overwrite some pointers with faster versions.
652
7
  if (VP8GetCPUInfo != NULL) {
653
7
#if defined(WEBP_HAVE_SSE2)
654
7
    if (VP8GetCPUInfo(kSSE2)) {
655
7
      VP8LDspInitSSE2();
656
7
#if defined(WEBP_HAVE_SSE41)
657
7
      if (VP8GetCPUInfo(kSSE4_1)) {
658
7
        VP8LDspInitSSE41();
659
#if defined(WEBP_HAVE_AVX2)
660
        if (VP8GetCPUInfo(kAVX2)) {
661
          VP8LDspInitAVX2();
662
        }
663
#endif
664
7
      }
665
7
#endif
666
7
    }
667
7
#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
7
  }
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
7
  assert(VP8LAddGreenToBlueAndRed != NULL);
688
7
  assert(VP8LTransformColorInverse != NULL);
689
7
  assert(VP8LConvertBGRAToRGBA != NULL);
690
7
  assert(VP8LConvertBGRAToRGB != NULL);
691
7
  assert(VP8LConvertBGRAToBGR != NULL);
692
7
  assert(VP8LConvertBGRAToRGBA4444 != NULL);
693
7
  assert(VP8LConvertBGRAToRGB565 != NULL);
694
7
  assert(VP8LMapColor32b != NULL);
695
  assert(VP8LMapColor8b != NULL);
696
7
}
697
#undef COPY_PREDICTOR_ARRAY
698
699
//------------------------------------------------------------------------------