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