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