/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 | 10.2k | static WEBP_INLINE uint32_t Average2(uint32_t a0, uint32_t a1) { |
36 | 10.2k | return (((a0 ^ a1) & 0xfefefefeu) >> 1) + (a0 & a1); |
37 | 10.2k | } |
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 | 1.91k | uint32_t a3) { |
45 | 1.91k | return Average2(Average2(a0, a1), Average2(a2, a3)); |
46 | 1.91k | } |
47 | | |
48 | 106k | static WEBP_INLINE uint32_t Clip255(uint32_t a) { |
49 | 106k | if (a < 256) { |
50 | 91.7k | return a; |
51 | 91.7k | } |
52 | | // return 0, when a is a negative integer. |
53 | | // return 255, when a is positive. |
54 | 14.8k | return ~a >> 24; |
55 | 106k | } |
56 | | |
57 | 106k | static WEBP_INLINE int AddSubtractComponentFull(int a, int b, int c) { |
58 | 106k | return Clip255((uint32_t)(a + b - c)); |
59 | 106k | } |
60 | | |
61 | | static WEBP_INLINE uint32_t ClampedAddSubtractFull(uint32_t c0, uint32_t c1, |
62 | 26.6k | uint32_t c2) { |
63 | 26.6k | const int a = AddSubtractComponentFull(c0 >> 24, c1 >> 24, c2 >> 24); |
64 | 26.6k | const int r = AddSubtractComponentFull((c0 >> 16) & 0xff, (c1 >> 16) & 0xff, |
65 | 26.6k | (c2 >> 16) & 0xff); |
66 | 26.6k | const int g = AddSubtractComponentFull((c0 >> 8) & 0xff, (c1 >> 8) & 0xff, |
67 | 26.6k | (c2 >> 8) & 0xff); |
68 | 26.6k | const int b = AddSubtractComponentFull(c0 & 0xff, c1 & 0xff, c2 & 0xff); |
69 | 26.6k | return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b; |
70 | 26.6k | } |
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 | 48.2k | static LOCAL_INLINE int Sub3(int a, int b, int c) { |
95 | 48.2k | const int pb = b - c; |
96 | 48.2k | const int pa = a - c; |
97 | 48.2k | return abs(pb) - abs(pa); |
98 | 48.2k | } |
99 | | |
100 | | #undef LOCAL_INLINE |
101 | | |
102 | 12.0k | static WEBP_INLINE uint32_t Select(uint32_t a, uint32_t b, uint32_t c) { |
103 | 12.0k | const int pa_minus_pb = |
104 | 12.0k | Sub3((a >> 24), (b >> 24), (c >> 24)) + |
105 | 12.0k | Sub3((a >> 16) & 0xff, (b >> 16) & 0xff, (c >> 16) & 0xff) + |
106 | 12.0k | Sub3((a >> 8) & 0xff, (b >> 8) & 0xff, (c >> 8) & 0xff) + |
107 | 12.0k | Sub3((a) & 0xff, (b) & 0xff, (c) & 0xff); |
108 | 12.0k | return (pa_minus_pb <= 0) ? a : b; |
109 | 12.0k | } |
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 | 300k | const uint32_t* const top) { |
127 | 300k | (void)left; |
128 | 300k | return top[0]; |
129 | 300k | } |
130 | | uint32_t VP8LPredictor3_C(const uint32_t* const left, |
131 | 3.82k | const uint32_t* const top) { |
132 | 3.82k | (void)left; |
133 | 3.82k | return top[1]; |
134 | 3.82k | } |
135 | | uint32_t VP8LPredictor4_C(const uint32_t* const left, |
136 | 4.28k | const uint32_t* const top) { |
137 | 4.28k | (void)left; |
138 | 4.28k | return top[-1]; |
139 | 4.28k | } |
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 | 1.78k | const uint32_t* const top) { |
157 | 1.78k | const uint32_t pred = Average2(top[-1], top[0]); |
158 | 1.78k | (void)left; |
159 | 1.78k | return pred; |
160 | 1.78k | } |
161 | | uint32_t VP8LPredictor9_C(const uint32_t* const left, |
162 | 2.72k | const uint32_t* const top) { |
163 | 2.72k | const uint32_t pred = Average2(top[0], top[1]); |
164 | 2.72k | (void)left; |
165 | 2.72k | return pred; |
166 | 2.72k | } |
167 | | uint32_t VP8LPredictor10_C(const uint32_t* const left, |
168 | 1.91k | const uint32_t* const top) { |
169 | 1.91k | const uint32_t pred = Average4(*left, top[-1], top[0], top[1]); |
170 | 1.91k | return pred; |
171 | 1.91k | } |
172 | | uint32_t VP8LPredictor11_C(const uint32_t* const left, |
173 | 12.0k | const uint32_t* const top) { |
174 | 12.0k | const uint32_t pred = Select(top[0], *left, top[-1]); |
175 | 12.0k | return pred; |
176 | 12.0k | } |
177 | | uint32_t VP8LPredictor12_C(const uint32_t* const left, |
178 | 26.6k | const uint32_t* const top) { |
179 | 26.6k | const uint32_t pred = ClampedAddSubtractFull(*left, top[0], top[-1]); |
180 | 26.6k | return pred; |
181 | 26.6k | } |
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 | 181k | int num_pixels, uint32_t* WEBP_RESTRICT out) { |
190 | 181k | int x; |
191 | 181k | (void)upper; |
192 | 638k | for (x = 0; x < num_pixels; ++x) out[x] = VP8LAddPixels(in[x], ARGB_BLACK); |
193 | 181k | } |
194 | | static void PredictorAdd1_C(const uint32_t* in, const uint32_t* upper, |
195 | 227k | int num_pixels, uint32_t* WEBP_RESTRICT out) { |
196 | 227k | int i; |
197 | 227k | uint32_t left = out[-1]; |
198 | 227k | (void)upper; |
199 | 1.14M | for (i = 0; i < num_pixels; ++i) { |
200 | 916k | out[i] = left = VP8LAddPixels(in[i], left); |
201 | 916k | } |
202 | 227k | } |
203 | 296k | GENERATE_PREDICTOR_ADD(VP8LPredictor2_C, PredictorAdd2_C) |
204 | 1.69k | GENERATE_PREDICTOR_ADD(VP8LPredictor3_C, PredictorAdd3_C) |
205 | 1.68k | 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 | 723 | GENERATE_PREDICTOR_ADD(VP8LPredictor8_C, PredictorAdd8_C) |
210 | 1.15k | GENERATE_PREDICTOR_ADD(VP8LPredictor9_C, PredictorAdd9_C) |
211 | 802 | GENERATE_PREDICTOR_ADD(VP8LPredictor10_C, PredictorAdd10_C) |
212 | 4.50k | GENERATE_PREDICTOR_ADD(VP8LPredictor11_C, PredictorAdd11_C) |
213 | 11.2k | 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 | 18.6k | const uint32_t* in, uint32_t* out) { |
222 | 18.6k | const int width = transform->xsize; |
223 | 18.6k | if (y_start == 0) { // First Row follows the L (mode=1) mode. |
224 | 301 | PredictorAdd0_C(in, NULL, 1, out); |
225 | 301 | PredictorAdd1_C(in + 1, NULL, width - 1, out + 1); |
226 | 301 | in += width; |
227 | 301 | out += width; |
228 | 301 | ++y_start; |
229 | 301 | } |
230 | | |
231 | 18.6k | { |
232 | 18.6k | int y = y_start; |
233 | 18.6k | const int tile_width = 1 << transform->bits; |
234 | 18.6k | const int mask = tile_width - 1; |
235 | 18.6k | const int tiles_per_row = VP8LSubSampleSize(width, transform->bits); |
236 | 18.6k | const uint32_t* pred_mode_base = |
237 | 18.6k | transform->data + (y >> transform->bits) * tiles_per_row; |
238 | | |
239 | 313k | while (y < y_end) { |
240 | 294k | const uint32_t* pred_mode_src = pred_mode_base; |
241 | 294k | int x = 1; |
242 | | // First pixel follows the T (mode=2) mode. |
243 | 294k | PredictorAdd2_C(in, out - width, 1, out); |
244 | | // .. the rest: |
245 | 8.51M | while (x < width) { |
246 | 8.21M | const VP8LPredictorAddSubFunc pred_func = |
247 | 8.21M | VP8LPredictorsAdd[((*pred_mode_src++) >> 8) & 0xf]; |
248 | 8.21M | int x_end = (x & ~mask) + tile_width; |
249 | 8.21M | if (x_end > width) x_end = width; |
250 | 8.21M | pred_func(in + x, out + x - width, x_end - x, out + x); |
251 | 8.21M | x = x_end; |
252 | 8.21M | } |
253 | 294k | in += width; |
254 | 294k | out += width; |
255 | 294k | ++y; |
256 | 294k | if ((y & mask) == 0) { // Use the same mask, since tiles are squares. |
257 | 12.8k | pred_mode_base += tiles_per_row; |
258 | 12.8k | } |
259 | 294k | } |
260 | 18.6k | } |
261 | 18.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 | 53 | uint32_t* dst) { |
267 | 53 | int i; |
268 | 161 | for (i = 0; i < num_pixels; ++i) { |
269 | 108 | const uint32_t argb = src[i]; |
270 | 108 | const uint32_t green = ((argb >> 8) & 0xff); |
271 | 108 | uint32_t red_blue = (argb & 0x00ff00ffu); |
272 | 108 | red_blue += (green << 16) | green; |
273 | 108 | red_blue &= 0x00ff00ffu; |
274 | 108 | dst[i] = (argb & 0xff00ff00u) | red_blue; |
275 | 108 | } |
276 | 53 | } |
277 | | |
278 | 322k | static WEBP_INLINE int ColorTransformDelta(int8_t color_pred, int8_t color) { |
279 | 322k | return ((int)color_pred * color) >> 5; |
280 | 322k | } |
281 | | |
282 | | static WEBP_INLINE void ColorCodeToMultipliers(uint32_t color_code, |
283 | 13.6M | VP8LMultipliers* const m) { |
284 | 13.6M | m->green_to_red = (color_code >> 0) & 0xff; |
285 | 13.6M | m->green_to_blue = (color_code >> 8) & 0xff; |
286 | 13.6M | m->red_to_blue = (color_code >> 16) & 0xff; |
287 | 13.6M | } |
288 | | |
289 | | void VP8LTransformColorInverse_C(const VP8LMultipliers* const m, |
290 | | const uint32_t* src, int num_pixels, |
291 | 52.0k | uint32_t* dst) { |
292 | 52.0k | int i; |
293 | 159k | for (i = 0; i < num_pixels; ++i) { |
294 | 107k | const uint32_t argb = src[i]; |
295 | 107k | const int8_t green = (int8_t)(argb >> 8); |
296 | 107k | const uint32_t red = argb >> 16; |
297 | 107k | int new_red = red & 0xff; |
298 | 107k | int new_blue = argb & 0xff; |
299 | 107k | new_red += ColorTransformDelta((int8_t)m->green_to_red, green); |
300 | 107k | new_red &= 0xff; |
301 | 107k | new_blue += ColorTransformDelta((int8_t)m->green_to_blue, green); |
302 | 107k | new_blue += ColorTransformDelta((int8_t)m->red_to_blue, (int8_t)new_red); |
303 | 107k | new_blue &= 0xff; |
304 | 107k | dst[i] = (argb & 0xff00ff00u) | (new_red << 16) | (new_blue); |
305 | 107k | } |
306 | 52.0k | } |
307 | | |
308 | | // Color space inverse transform. |
309 | | static void ColorSpaceInverseTransform_C(const VP8LTransform* const transform, |
310 | | int y_start, int y_end, |
311 | 4.43k | const uint32_t* src, uint32_t* dst) { |
312 | 4.43k | const int width = transform->xsize; |
313 | 4.43k | const int tile_width = 1 << transform->bits; |
314 | 4.43k | const int mask = tile_width - 1; |
315 | 4.43k | const int safe_width = width & ~mask; |
316 | 4.43k | const int remaining_width = width - safe_width; |
317 | 4.43k | const int tiles_per_row = VP8LSubSampleSize(width, transform->bits); |
318 | 4.43k | int y = y_start; |
319 | 4.43k | const uint32_t* pred_row = |
320 | 4.43k | transform->data + (y >> transform->bits) * tiles_per_row; |
321 | | |
322 | 74.9k | while (y < y_end) { |
323 | 70.4k | const uint32_t* pred = pred_row; |
324 | 70.4k | VP8LMultipliers m = {0, 0, 0}; |
325 | 70.4k | const uint32_t* const src_safe_end = src + safe_width; |
326 | 70.4k | const uint32_t* const src_end = src + width; |
327 | 13.6M | while (src < src_safe_end) { |
328 | 13.5M | ColorCodeToMultipliers(*pred++, &m); |
329 | 13.5M | VP8LTransformColorInverse(&m, src, tile_width, dst); |
330 | 13.5M | src += tile_width; |
331 | 13.5M | dst += tile_width; |
332 | 13.5M | } |
333 | 70.4k | if (src < src_end) { // Left-overs using C-version. |
334 | 66.6k | ColorCodeToMultipliers(*pred++, &m); |
335 | 66.6k | VP8LTransformColorInverse(&m, src, remaining_width, dst); |
336 | 66.6k | src += remaining_width; |
337 | 66.6k | dst += remaining_width; |
338 | 66.6k | } |
339 | 70.4k | ++y; |
340 | 70.4k | if ((y & mask) == 0) pred_row += tiles_per_row; |
341 | 70.4k | } |
342 | 4.43k | } |
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.13k | TYPE* dst, int y_start, int y_end, int width) { \ |
351 | 3.13k | int y; \ |
352 | 52.9k | for (y = y_start; y < y_end; ++y) { \ |
353 | 49.8k | int x; \ |
354 | 73.7M | for (x = 0; x < width; ++x) { \ |
355 | 73.7M | *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]); \ |
356 | 73.7M | } \ |
357 | 49.8k | } \ |
358 | 3.13k | } \ Line | Count | Source | 350 | 3.13k | TYPE* dst, int y_start, int y_end, int width) { \ | 351 | 3.13k | int y; \ | 352 | 52.9k | for (y = y_start; y < y_end; ++y) { \ | 353 | 49.8k | int x; \ | 354 | 73.7M | for (x = 0; x < width; ++x) { \ | 355 | 73.7M | *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]); \ | 356 | 73.7M | } \ | 357 | 49.8k | } \ | 358 | 3.13k | } \ |
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 | 6.96k | TYPE* dst) { \ |
362 | 6.96k | int y; \ |
363 | 6.96k | const int bits_per_pixel = 8 >> transform->bits; \ |
364 | 6.96k | const int width = transform->xsize; \ |
365 | 6.96k | const uint32_t* const color_map = transform->data; \ |
366 | 6.96k | if (bits_per_pixel < 8) { \ |
367 | 3.83k | const int pixels_per_byte = 1 << transform->bits; \ |
368 | 3.83k | const int count_mask = pixels_per_byte - 1; \ |
369 | 3.83k | const uint32_t bit_mask = (1 << bits_per_pixel) - 1; \ |
370 | 64.7k | for (y = y_start; y < y_end; ++y) { \ |
371 | 60.9k | uint32_t packed_pixels = 0; \ |
372 | 60.9k | int x; \ |
373 | 64.9M | for (x = 0; x < width; ++x) { \ |
374 | 64.8M | /* We need to load fresh 'packed_pixels' once every */ \ |
375 | 64.8M | /* 'pixels_per_byte' increments of x. Fortunately, pixels_per_byte */ \ |
376 | 64.8M | /* is a power of 2, so can just use a mask for that, instead of */ \ |
377 | 64.8M | /* decrementing a counter. */ \ |
378 | 64.8M | if ((x & count_mask) == 0) packed_pixels = GET_INDEX(*src++); \ |
379 | 64.8M | *dst++ = GET_VALUE(color_map[packed_pixels & bit_mask]); \ |
380 | 64.8M | packed_pixels >>= bits_per_pixel; \ |
381 | 64.8M | } \ |
382 | 60.9k | } \ |
383 | 3.83k | } else { \ |
384 | 3.13k | VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width); \ |
385 | 3.13k | } \ |
386 | 6.96k | } Unexecuted instantiation: VP8LColorIndexInverseTransformAlpha lossless.c:ColorIndexInverseTransform_C Line | Count | Source | 361 | 6.96k | TYPE* dst) { \ | 362 | 6.96k | int y; \ | 363 | 6.96k | const int bits_per_pixel = 8 >> transform->bits; \ | 364 | 6.96k | const int width = transform->xsize; \ | 365 | 6.96k | const uint32_t* const color_map = transform->data; \ | 366 | 6.96k | if (bits_per_pixel < 8) { \ | 367 | 3.83k | const int pixels_per_byte = 1 << transform->bits; \ | 368 | 3.83k | const int count_mask = pixels_per_byte - 1; \ | 369 | 3.83k | const uint32_t bit_mask = (1 << bits_per_pixel) - 1; \ | 370 | 64.7k | for (y = y_start; y < y_end; ++y) { \ | 371 | 60.9k | uint32_t packed_pixels = 0; \ | 372 | 60.9k | int x; \ | 373 | 64.9M | for (x = 0; x < width; ++x) { \ | 374 | 64.8M | /* We need to load fresh 'packed_pixels' once every */ \ | 375 | 64.8M | /* 'pixels_per_byte' increments of x. Fortunately, pixels_per_byte */ \ | 376 | 64.8M | /* is a power of 2, so can just use a mask for that, instead of */ \ | 377 | 64.8M | /* decrementing a counter. */ \ | 378 | 64.8M | if ((x & count_mask) == 0) packed_pixels = GET_INDEX(*src++); \ | 379 | 64.8M | *dst++ = GET_VALUE(color_map[packed_pixels & bit_mask]); \ | 380 | 64.8M | packed_pixels >>= bits_per_pixel; \ | 381 | 64.8M | } \ | 382 | 60.9k | } \ | 383 | 3.83k | } else { \ | 384 | 3.13k | VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width); \ | 385 | 3.13k | } \ | 386 | 6.96k | } |
|
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 | 38.5k | uint32_t* const out) { |
399 | 38.5k | const int width = transform->xsize; |
400 | 38.5k | assert(row_start < row_end); |
401 | 38.5k | assert(row_end <= transform->ysize); |
402 | 38.5k | switch (transform->type) { |
403 | 8.57k | case SUBTRACT_GREEN_TRANSFORM: |
404 | 8.57k | VP8LAddGreenToBlueAndRed(in, (row_end - row_start) * width, out); |
405 | 8.57k | break; |
406 | 18.6k | case PREDICTOR_TRANSFORM: |
407 | 18.6k | PredictorInverseTransform_C(transform, row_start, row_end, in, out); |
408 | 18.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 | 18.3k | memcpy(out - width, out + (row_end - row_start - 1) * width, |
412 | 18.3k | width * sizeof(*out)); |
413 | 18.3k | } |
414 | 18.6k | break; |
415 | 4.43k | case CROSS_COLOR_TRANSFORM: |
416 | 4.43k | ColorSpaceInverseTransform_C(transform, row_start, row_end, in, out); |
417 | 4.43k | break; |
418 | 6.96k | case COLOR_INDEXING_TRANSFORM: |
419 | 6.96k | 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 | 564 | const int out_stride = (row_end - row_start) * width; |
426 | 564 | const int in_stride = |
427 | 564 | (row_end - row_start) * |
428 | 564 | VP8LSubSampleSize(transform->xsize, transform->bits); |
429 | 564 | uint32_t* const src = out + out_stride - in_stride; |
430 | 564 | memmove(src, out, in_stride * sizeof(*src)); |
431 | 564 | ColorIndexInverseTransform_C(transform, row_start, row_end, src, out); |
432 | 6.40k | } else { |
433 | 6.40k | ColorIndexInverseTransform_C(transform, row_start, row_end, in, out); |
434 | 6.40k | } |
435 | 6.96k | break; |
436 | 38.5k | } |
437 | 38.5k | } |
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.17M | uint8_t* WEBP_RESTRICT dst) { |
463 | 1.17M | const uint32_t* const src_end = src + num_pixels; |
464 | 5.78M | while (src < src_end) { |
465 | 4.60M | const uint32_t argb = *src++; |
466 | 4.60M | *dst++ = (argb >> 16) & 0xff; |
467 | 4.60M | *dst++ = (argb >> 8) & 0xff; |
468 | 4.60M | *dst++ = (argb >> 0) & 0xff; |
469 | 4.60M | *dst++ = (argb >> 24) & 0xff; |
470 | 4.60M | } |
471 | 1.17M | } |
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.30M | WEBP_CSP_MODE out_colorspace, uint8_t* const rgba) { |
534 | 1.30M | switch (out_colorspace) { |
535 | 0 | case MODE_RGB: |
536 | 0 | VP8LConvertBGRAToRGB(in_data, num_pixels, rgba); |
537 | 0 | break; |
538 | 1.30M | case MODE_RGBA: |
539 | 1.30M | VP8LConvertBGRAToRGBA(in_data, num_pixels, rgba); |
540 | 1.30M | 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.30M | } |
575 | 1.30M | } |
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 | | //------------------------------------------------------------------------------ |