lossless.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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 ([email protected])
  13. // Jyrki Alakuijala ([email protected])
  14. // Urvang Joshi ([email protected])
  15. #include "./dsp.h"
  16. #include <math.h>
  17. #include <stdlib.h>
  18. #include "../dec/vp8li.h"
  19. #include "../utils/endian_inl.h"
  20. #include "./lossless.h"
  21. #define MAX_DIFF_COST (1e30f)
  22. //------------------------------------------------------------------------------
  23. // Image transforms.
  24. // In-place sum of each component with mod 256.
  25. static WEBP_INLINE void AddPixelsEq(uint32_t* a, uint32_t b) {
  26. *a = VP8LAddPixels(*a, b);
  27. }
  28. static WEBP_INLINE uint32_t Average2(uint32_t a0, uint32_t a1) {
  29. return (((a0 ^ a1) & 0xfefefefeu) >> 1) + (a0 & a1);
  30. }
  31. static WEBP_INLINE uint32_t Average3(uint32_t a0, uint32_t a1, uint32_t a2) {
  32. return Average2(Average2(a0, a2), a1);
  33. }
  34. static WEBP_INLINE uint32_t Average4(uint32_t a0, uint32_t a1,
  35. uint32_t a2, uint32_t a3) {
  36. return Average2(Average2(a0, a1), Average2(a2, a3));
  37. }
  38. static WEBP_INLINE uint32_t Clip255(uint32_t a) {
  39. if (a < 256) {
  40. return a;
  41. }
  42. // return 0, when a is a negative integer.
  43. // return 255, when a is positive.
  44. return ~a >> 24;
  45. }
  46. static WEBP_INLINE int AddSubtractComponentFull(int a, int b, int c) {
  47. return Clip255(a + b - c);
  48. }
  49. static WEBP_INLINE uint32_t ClampedAddSubtractFull(uint32_t c0, uint32_t c1,
  50. uint32_t c2) {
  51. const int a = AddSubtractComponentFull(c0 >> 24, c1 >> 24, c2 >> 24);
  52. const int r = AddSubtractComponentFull((c0 >> 16) & 0xff,
  53. (c1 >> 16) & 0xff,
  54. (c2 >> 16) & 0xff);
  55. const int g = AddSubtractComponentFull((c0 >> 8) & 0xff,
  56. (c1 >> 8) & 0xff,
  57. (c2 >> 8) & 0xff);
  58. const int b = AddSubtractComponentFull(c0 & 0xff, c1 & 0xff, c2 & 0xff);
  59. return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b;
  60. }
  61. static WEBP_INLINE int AddSubtractComponentHalf(int a, int b) {
  62. return Clip255(a + (a - b) / 2);
  63. }
  64. static WEBP_INLINE uint32_t ClampedAddSubtractHalf(uint32_t c0, uint32_t c1,
  65. uint32_t c2) {
  66. const uint32_t ave = Average2(c0, c1);
  67. const int a = AddSubtractComponentHalf(ave >> 24, c2 >> 24);
  68. const int r = AddSubtractComponentHalf((ave >> 16) & 0xff, (c2 >> 16) & 0xff);
  69. const int g = AddSubtractComponentHalf((ave >> 8) & 0xff, (c2 >> 8) & 0xff);
  70. const int b = AddSubtractComponentHalf((ave >> 0) & 0xff, (c2 >> 0) & 0xff);
  71. return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b;
  72. }
  73. // gcc-4.9 on ARM generates incorrect code in Select() when Sub3() is inlined.
  74. #if defined(__arm__) && LOCAL_GCC_VERSION == 0x409
  75. # define LOCAL_INLINE __attribute__ ((noinline))
  76. #else
  77. # define LOCAL_INLINE WEBP_INLINE
  78. #endif
  79. static LOCAL_INLINE int Sub3(int a, int b, int c) {
  80. const int pb = b - c;
  81. const int pa = a - c;
  82. return abs(pb) - abs(pa);
  83. }
  84. #undef LOCAL_INLINE
  85. static WEBP_INLINE uint32_t Select(uint32_t a, uint32_t b, uint32_t c) {
  86. const int pa_minus_pb =
  87. Sub3((a >> 24) , (b >> 24) , (c >> 24) ) +
  88. Sub3((a >> 16) & 0xff, (b >> 16) & 0xff, (c >> 16) & 0xff) +
  89. Sub3((a >> 8) & 0xff, (b >> 8) & 0xff, (c >> 8) & 0xff) +
  90. Sub3((a ) & 0xff, (b ) & 0xff, (c ) & 0xff);
  91. return (pa_minus_pb <= 0) ? a : b;
  92. }
  93. //------------------------------------------------------------------------------
  94. // Predictors
  95. static uint32_t Predictor0(uint32_t left, const uint32_t* const top) {
  96. (void)top;
  97. (void)left;
  98. return ARGB_BLACK;
  99. }
  100. static uint32_t Predictor1(uint32_t left, const uint32_t* const top) {
  101. (void)top;
  102. return left;
  103. }
  104. static uint32_t Predictor2(uint32_t left, const uint32_t* const top) {
  105. (void)left;
  106. return top[0];
  107. }
  108. static uint32_t Predictor3(uint32_t left, const uint32_t* const top) {
  109. (void)left;
  110. return top[1];
  111. }
  112. static uint32_t Predictor4(uint32_t left, const uint32_t* const top) {
  113. (void)left;
  114. return top[-1];
  115. }
  116. static uint32_t Predictor5(uint32_t left, const uint32_t* const top) {
  117. const uint32_t pred = Average3(left, top[0], top[1]);
  118. return pred;
  119. }
  120. static uint32_t Predictor6(uint32_t left, const uint32_t* const top) {
  121. const uint32_t pred = Average2(left, top[-1]);
  122. return pred;
  123. }
  124. static uint32_t Predictor7(uint32_t left, const uint32_t* const top) {
  125. const uint32_t pred = Average2(left, top[0]);
  126. return pred;
  127. }
  128. static uint32_t Predictor8(uint32_t left, const uint32_t* const top) {
  129. const uint32_t pred = Average2(top[-1], top[0]);
  130. (void)left;
  131. return pred;
  132. }
  133. static uint32_t Predictor9(uint32_t left, const uint32_t* const top) {
  134. const uint32_t pred = Average2(top[0], top[1]);
  135. (void)left;
  136. return pred;
  137. }
  138. static uint32_t Predictor10(uint32_t left, const uint32_t* const top) {
  139. const uint32_t pred = Average4(left, top[-1], top[0], top[1]);
  140. return pred;
  141. }
  142. static uint32_t Predictor11(uint32_t left, const uint32_t* const top) {
  143. const uint32_t pred = Select(top[0], left, top[-1]);
  144. return pred;
  145. }
  146. static uint32_t Predictor12(uint32_t left, const uint32_t* const top) {
  147. const uint32_t pred = ClampedAddSubtractFull(left, top[0], top[-1]);
  148. return pred;
  149. }
  150. static uint32_t Predictor13(uint32_t left, const uint32_t* const top) {
  151. const uint32_t pred = ClampedAddSubtractHalf(left, top[0], top[-1]);
  152. return pred;
  153. }
  154. //------------------------------------------------------------------------------
  155. // Inverse prediction.
  156. static void PredictorInverseTransform(const VP8LTransform* const transform,
  157. int y_start, int y_end, uint32_t* data) {
  158. const int width = transform->xsize_;
  159. if (y_start == 0) { // First Row follows the L (mode=1) mode.
  160. int x;
  161. const uint32_t pred0 = Predictor0(data[-1], NULL);
  162. AddPixelsEq(data, pred0);
  163. for (x = 1; x < width; ++x) {
  164. const uint32_t pred1 = Predictor1(data[x - 1], NULL);
  165. AddPixelsEq(data + x, pred1);
  166. }
  167. data += width;
  168. ++y_start;
  169. }
  170. {
  171. int y = y_start;
  172. const int tile_width = 1 << transform->bits_;
  173. const int mask = tile_width - 1;
  174. const int safe_width = width & ~mask;
  175. const int tiles_per_row = VP8LSubSampleSize(width, transform->bits_);
  176. const uint32_t* pred_mode_base =
  177. transform->data_ + (y >> transform->bits_) * tiles_per_row;
  178. while (y < y_end) {
  179. const uint32_t pred2 = Predictor2(data[-1], data - width);
  180. const uint32_t* pred_mode_src = pred_mode_base;
  181. VP8LPredictorFunc pred_func;
  182. int x = 1;
  183. int t = 1;
  184. // First pixel follows the T (mode=2) mode.
  185. AddPixelsEq(data, pred2);
  186. // .. the rest:
  187. while (x < safe_width) {
  188. pred_func = VP8LPredictors[((*pred_mode_src++) >> 8) & 0xf];
  189. for (; t < tile_width; ++t, ++x) {
  190. const uint32_t pred = pred_func(data[x - 1], data + x - width);
  191. AddPixelsEq(data + x, pred);
  192. }
  193. t = 0;
  194. }
  195. if (x < width) {
  196. pred_func = VP8LPredictors[((*pred_mode_src++) >> 8) & 0xf];
  197. for (; x < width; ++x) {
  198. const uint32_t pred = pred_func(data[x - 1], data + x - width);
  199. AddPixelsEq(data + x, pred);
  200. }
  201. }
  202. data += width;
  203. ++y;
  204. if ((y & mask) == 0) { // Use the same mask, since tiles are squares.
  205. pred_mode_base += tiles_per_row;
  206. }
  207. }
  208. }
  209. }
  210. // Add green to blue and red channels (i.e. perform the inverse transform of
  211. // 'subtract green').
  212. void VP8LAddGreenToBlueAndRed_C(uint32_t* data, int num_pixels) {
  213. int i;
  214. for (i = 0; i < num_pixels; ++i) {
  215. const uint32_t argb = data[i];
  216. const uint32_t green = ((argb >> 8) & 0xff);
  217. uint32_t red_blue = (argb & 0x00ff00ffu);
  218. red_blue += (green << 16) | green;
  219. red_blue &= 0x00ff00ffu;
  220. data[i] = (argb & 0xff00ff00u) | red_blue;
  221. }
  222. }
  223. static WEBP_INLINE uint32_t ColorTransformDelta(int8_t color_pred,
  224. int8_t color) {
  225. return (uint32_t)((int)(color_pred) * color) >> 5;
  226. }
  227. static WEBP_INLINE void ColorCodeToMultipliers(uint32_t color_code,
  228. VP8LMultipliers* const m) {
  229. m->green_to_red_ = (color_code >> 0) & 0xff;
  230. m->green_to_blue_ = (color_code >> 8) & 0xff;
  231. m->red_to_blue_ = (color_code >> 16) & 0xff;
  232. }
  233. void VP8LTransformColorInverse_C(const VP8LMultipliers* const m, uint32_t* data,
  234. int num_pixels) {
  235. int i;
  236. for (i = 0; i < num_pixels; ++i) {
  237. const uint32_t argb = data[i];
  238. const uint32_t green = argb >> 8;
  239. const uint32_t red = argb >> 16;
  240. uint32_t new_red = red;
  241. uint32_t new_blue = argb;
  242. new_red += ColorTransformDelta(m->green_to_red_, green);
  243. new_red &= 0xff;
  244. new_blue += ColorTransformDelta(m->green_to_blue_, green);
  245. new_blue += ColorTransformDelta(m->red_to_blue_, new_red);
  246. new_blue &= 0xff;
  247. data[i] = (argb & 0xff00ff00u) | (new_red << 16) | (new_blue);
  248. }
  249. }
  250. // Color space inverse transform.
  251. static void ColorSpaceInverseTransform(const VP8LTransform* const transform,
  252. int y_start, int y_end, uint32_t* data) {
  253. const int width = transform->xsize_;
  254. const int tile_width = 1 << transform->bits_;
  255. const int mask = tile_width - 1;
  256. const int safe_width = width & ~mask;
  257. const int remaining_width = width - safe_width;
  258. const int tiles_per_row = VP8LSubSampleSize(width, transform->bits_);
  259. int y = y_start;
  260. const uint32_t* pred_row =
  261. transform->data_ + (y >> transform->bits_) * tiles_per_row;
  262. while (y < y_end) {
  263. const uint32_t* pred = pred_row;
  264. VP8LMultipliers m = { 0, 0, 0 };
  265. const uint32_t* const data_safe_end = data + safe_width;
  266. const uint32_t* const data_end = data + width;
  267. while (data < data_safe_end) {
  268. ColorCodeToMultipliers(*pred++, &m);
  269. VP8LTransformColorInverse(&m, data, tile_width);
  270. data += tile_width;
  271. }
  272. if (data < data_end) { // Left-overs using C-version.
  273. ColorCodeToMultipliers(*pred++, &m);
  274. VP8LTransformColorInverse(&m, data, remaining_width);
  275. data += remaining_width;
  276. }
  277. ++y;
  278. if ((y & mask) == 0) pred_row += tiles_per_row;
  279. }
  280. }
  281. // Separate out pixels packed together using pixel-bundling.
  282. // We define two methods for ARGB data (uint32_t) and alpha-only data (uint8_t).
  283. #define COLOR_INDEX_INVERSE(FUNC_NAME, F_NAME, STATIC_DECL, TYPE, BIT_SUFFIX, \
  284. GET_INDEX, GET_VALUE) \
  285. static void F_NAME(const TYPE* src, const uint32_t* const color_map, \
  286. TYPE* dst, int y_start, int y_end, int width) { \
  287. int y; \
  288. for (y = y_start; y < y_end; ++y) { \
  289. int x; \
  290. for (x = 0; x < width; ++x) { \
  291. *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]); \
  292. } \
  293. } \
  294. } \
  295. STATIC_DECL void FUNC_NAME(const VP8LTransform* const transform, \
  296. int y_start, int y_end, const TYPE* src, \
  297. TYPE* dst) { \
  298. int y; \
  299. const int bits_per_pixel = 8 >> transform->bits_; \
  300. const int width = transform->xsize_; \
  301. const uint32_t* const color_map = transform->data_; \
  302. if (bits_per_pixel < 8) { \
  303. const int pixels_per_byte = 1 << transform->bits_; \
  304. const int count_mask = pixels_per_byte - 1; \
  305. const uint32_t bit_mask = (1 << bits_per_pixel) - 1; \
  306. for (y = y_start; y < y_end; ++y) { \
  307. uint32_t packed_pixels = 0; \
  308. int x; \
  309. for (x = 0; x < width; ++x) { \
  310. /* We need to load fresh 'packed_pixels' once every */ \
  311. /* 'pixels_per_byte' increments of x. Fortunately, pixels_per_byte */ \
  312. /* is a power of 2, so can just use a mask for that, instead of */ \
  313. /* decrementing a counter. */ \
  314. if ((x & count_mask) == 0) packed_pixels = GET_INDEX(*src++); \
  315. *dst++ = GET_VALUE(color_map[packed_pixels & bit_mask]); \
  316. packed_pixels >>= bits_per_pixel; \
  317. } \
  318. } \
  319. } else { \
  320. VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width); \
  321. } \
  322. }
  323. COLOR_INDEX_INVERSE(ColorIndexInverseTransform, MapARGB, static, uint32_t, 32b,
  324. VP8GetARGBIndex, VP8GetARGBValue)
  325. COLOR_INDEX_INVERSE(VP8LColorIndexInverseTransformAlpha, MapAlpha, , uint8_t,
  326. 8b, VP8GetAlphaIndex, VP8GetAlphaValue)
  327. #undef COLOR_INDEX_INVERSE
  328. void VP8LInverseTransform(const VP8LTransform* const transform,
  329. int row_start, int row_end,
  330. const uint32_t* const in, uint32_t* const out) {
  331. const int width = transform->xsize_;
  332. assert(row_start < row_end);
  333. assert(row_end <= transform->ysize_);
  334. switch (transform->type_) {
  335. case SUBTRACT_GREEN:
  336. VP8LAddGreenToBlueAndRed(out, (row_end - row_start) * width);
  337. break;
  338. case PREDICTOR_TRANSFORM:
  339. PredictorInverseTransform(transform, row_start, row_end, out);
  340. if (row_end != transform->ysize_) {
  341. // The last predicted row in this iteration will be the top-pred row
  342. // for the first row in next iteration.
  343. memcpy(out - width, out + (row_end - row_start - 1) * width,
  344. width * sizeof(*out));
  345. }
  346. break;
  347. case CROSS_COLOR_TRANSFORM:
  348. ColorSpaceInverseTransform(transform, row_start, row_end, out);
  349. break;
  350. case COLOR_INDEXING_TRANSFORM:
  351. if (in == out && transform->bits_ > 0) {
  352. // Move packed pixels to the end of unpacked region, so that unpacking
  353. // can occur seamlessly.
  354. // Also, note that this is the only transform that applies on
  355. // the effective width of VP8LSubSampleSize(xsize_, bits_). All other
  356. // transforms work on effective width of xsize_.
  357. const int out_stride = (row_end - row_start) * width;
  358. const int in_stride = (row_end - row_start) *
  359. VP8LSubSampleSize(transform->xsize_, transform->bits_);
  360. uint32_t* const src = out + out_stride - in_stride;
  361. memmove(src, out, in_stride * sizeof(*src));
  362. ColorIndexInverseTransform(transform, row_start, row_end, src, out);
  363. } else {
  364. ColorIndexInverseTransform(transform, row_start, row_end, in, out);
  365. }
  366. break;
  367. }
  368. }
  369. //------------------------------------------------------------------------------
  370. // Color space conversion.
  371. static int is_big_endian(void) {
  372. static const union {
  373. uint16_t w;
  374. uint8_t b[2];
  375. } tmp = { 1 };
  376. return (tmp.b[0] != 1);
  377. }
  378. void VP8LConvertBGRAToRGB_C(const uint32_t* src,
  379. int num_pixels, uint8_t* dst) {
  380. const uint32_t* const src_end = src + num_pixels;
  381. while (src < src_end) {
  382. const uint32_t argb = *src++;
  383. *dst++ = (argb >> 16) & 0xff;
  384. *dst++ = (argb >> 8) & 0xff;
  385. *dst++ = (argb >> 0) & 0xff;
  386. }
  387. }
  388. void VP8LConvertBGRAToRGBA_C(const uint32_t* src,
  389. int num_pixels, uint8_t* dst) {
  390. const uint32_t* const src_end = src + num_pixels;
  391. while (src < src_end) {
  392. const uint32_t argb = *src++;
  393. *dst++ = (argb >> 16) & 0xff;
  394. *dst++ = (argb >> 8) & 0xff;
  395. *dst++ = (argb >> 0) & 0xff;
  396. *dst++ = (argb >> 24) & 0xff;
  397. }
  398. }
  399. void VP8LConvertBGRAToRGBA4444_C(const uint32_t* src,
  400. int num_pixels, uint8_t* dst) {
  401. const uint32_t* const src_end = src + num_pixels;
  402. while (src < src_end) {
  403. const uint32_t argb = *src++;
  404. const uint8_t rg = ((argb >> 16) & 0xf0) | ((argb >> 12) & 0xf);
  405. const uint8_t ba = ((argb >> 0) & 0xf0) | ((argb >> 28) & 0xf);
  406. #ifdef WEBP_SWAP_16BIT_CSP
  407. *dst++ = ba;
  408. *dst++ = rg;
  409. #else
  410. *dst++ = rg;
  411. *dst++ = ba;
  412. #endif
  413. }
  414. }
  415. void VP8LConvertBGRAToRGB565_C(const uint32_t* src,
  416. int num_pixels, uint8_t* dst) {
  417. const uint32_t* const src_end = src + num_pixels;
  418. while (src < src_end) {
  419. const uint32_t argb = *src++;
  420. const uint8_t rg = ((argb >> 16) & 0xf8) | ((argb >> 13) & 0x7);
  421. const uint8_t gb = ((argb >> 5) & 0xe0) | ((argb >> 3) & 0x1f);
  422. #ifdef WEBP_SWAP_16BIT_CSP
  423. *dst++ = gb;
  424. *dst++ = rg;
  425. #else
  426. *dst++ = rg;
  427. *dst++ = gb;
  428. #endif
  429. }
  430. }
  431. void VP8LConvertBGRAToBGR_C(const uint32_t* src,
  432. int num_pixels, uint8_t* dst) {
  433. const uint32_t* const src_end = src + num_pixels;
  434. while (src < src_end) {
  435. const uint32_t argb = *src++;
  436. *dst++ = (argb >> 0) & 0xff;
  437. *dst++ = (argb >> 8) & 0xff;
  438. *dst++ = (argb >> 16) & 0xff;
  439. }
  440. }
  441. static void CopyOrSwap(const uint32_t* src, int num_pixels, uint8_t* dst,
  442. int swap_on_big_endian) {
  443. if (is_big_endian() == swap_on_big_endian) {
  444. const uint32_t* const src_end = src + num_pixels;
  445. while (src < src_end) {
  446. const uint32_t argb = *src++;
  447. #if !defined(WORDS_BIGENDIAN)
  448. #if !defined(WEBP_REFERENCE_IMPLEMENTATION)
  449. WebPUint32ToMem(dst, BSwap32(argb));
  450. #else // WEBP_REFERENCE_IMPLEMENTATION
  451. dst[0] = (argb >> 24) & 0xff;
  452. dst[1] = (argb >> 16) & 0xff;
  453. dst[2] = (argb >> 8) & 0xff;
  454. dst[3] = (argb >> 0) & 0xff;
  455. #endif
  456. #else // WORDS_BIGENDIAN
  457. dst[0] = (argb >> 0) & 0xff;
  458. dst[1] = (argb >> 8) & 0xff;
  459. dst[2] = (argb >> 16) & 0xff;
  460. dst[3] = (argb >> 24) & 0xff;
  461. #endif
  462. dst += sizeof(argb);
  463. }
  464. } else {
  465. memcpy(dst, src, num_pixels * sizeof(*src));
  466. }
  467. }
  468. void VP8LConvertFromBGRA(const uint32_t* const in_data, int num_pixels,
  469. WEBP_CSP_MODE out_colorspace, uint8_t* const rgba) {
  470. switch (out_colorspace) {
  471. case MODE_RGB:
  472. VP8LConvertBGRAToRGB(in_data, num_pixels, rgba);
  473. break;
  474. case MODE_RGBA:
  475. VP8LConvertBGRAToRGBA(in_data, num_pixels, rgba);
  476. break;
  477. case MODE_rgbA:
  478. VP8LConvertBGRAToRGBA(in_data, num_pixels, rgba);
  479. WebPApplyAlphaMultiply(rgba, 0, num_pixels, 1, 0);
  480. break;
  481. case MODE_BGR:
  482. VP8LConvertBGRAToBGR(in_data, num_pixels, rgba);
  483. break;
  484. case MODE_BGRA:
  485. CopyOrSwap(in_data, num_pixels, rgba, 1);
  486. break;
  487. case MODE_bgrA:
  488. CopyOrSwap(in_data, num_pixels, rgba, 1);
  489. WebPApplyAlphaMultiply(rgba, 0, num_pixels, 1, 0);
  490. break;
  491. case MODE_ARGB:
  492. CopyOrSwap(in_data, num_pixels, rgba, 0);
  493. break;
  494. case MODE_Argb:
  495. CopyOrSwap(in_data, num_pixels, rgba, 0);
  496. WebPApplyAlphaMultiply(rgba, 1, num_pixels, 1, 0);
  497. break;
  498. case MODE_RGBA_4444:
  499. VP8LConvertBGRAToRGBA4444(in_data, num_pixels, rgba);
  500. break;
  501. case MODE_rgbA_4444:
  502. VP8LConvertBGRAToRGBA4444(in_data, num_pixels, rgba);
  503. WebPApplyAlphaMultiply4444(rgba, num_pixels, 1, 0);
  504. break;
  505. case MODE_RGB_565:
  506. VP8LConvertBGRAToRGB565(in_data, num_pixels, rgba);
  507. break;
  508. default:
  509. assert(0); // Code flow should not reach here.
  510. }
  511. }
  512. //------------------------------------------------------------------------------
  513. VP8LProcessBlueAndRedFunc VP8LAddGreenToBlueAndRed;
  514. VP8LPredictorFunc VP8LPredictors[16];
  515. VP8LTransformColorFunc VP8LTransformColorInverse;
  516. VP8LConvertFunc VP8LConvertBGRAToRGB;
  517. VP8LConvertFunc VP8LConvertBGRAToRGBA;
  518. VP8LConvertFunc VP8LConvertBGRAToRGBA4444;
  519. VP8LConvertFunc VP8LConvertBGRAToRGB565;
  520. VP8LConvertFunc VP8LConvertBGRAToBGR;
  521. VP8LMapARGBFunc VP8LMapColor32b;
  522. VP8LMapAlphaFunc VP8LMapColor8b;
  523. extern void VP8LDspInitSSE2(void);
  524. extern void VP8LDspInitNEON(void);
  525. extern void VP8LDspInitMIPSdspR2(void);
  526. static volatile VP8CPUInfo lossless_last_cpuinfo_used =
  527. (VP8CPUInfo)&lossless_last_cpuinfo_used;
  528. WEBP_TSAN_IGNORE_FUNCTION void VP8LDspInit(void) {
  529. if (lossless_last_cpuinfo_used == VP8GetCPUInfo) return;
  530. VP8LPredictors[0] = Predictor0;
  531. VP8LPredictors[1] = Predictor1;
  532. VP8LPredictors[2] = Predictor2;
  533. VP8LPredictors[3] = Predictor3;
  534. VP8LPredictors[4] = Predictor4;
  535. VP8LPredictors[5] = Predictor5;
  536. VP8LPredictors[6] = Predictor6;
  537. VP8LPredictors[7] = Predictor7;
  538. VP8LPredictors[8] = Predictor8;
  539. VP8LPredictors[9] = Predictor9;
  540. VP8LPredictors[10] = Predictor10;
  541. VP8LPredictors[11] = Predictor11;
  542. VP8LPredictors[12] = Predictor12;
  543. VP8LPredictors[13] = Predictor13;
  544. VP8LPredictors[14] = Predictor0; // <- padding security sentinels
  545. VP8LPredictors[15] = Predictor0;
  546. VP8LAddGreenToBlueAndRed = VP8LAddGreenToBlueAndRed_C;
  547. VP8LTransformColorInverse = VP8LTransformColorInverse_C;
  548. VP8LConvertBGRAToRGB = VP8LConvertBGRAToRGB_C;
  549. VP8LConvertBGRAToRGBA = VP8LConvertBGRAToRGBA_C;
  550. VP8LConvertBGRAToRGBA4444 = VP8LConvertBGRAToRGBA4444_C;
  551. VP8LConvertBGRAToRGB565 = VP8LConvertBGRAToRGB565_C;
  552. VP8LConvertBGRAToBGR = VP8LConvertBGRAToBGR_C;
  553. VP8LMapColor32b = MapARGB;
  554. VP8LMapColor8b = MapAlpha;
  555. // If defined, use CPUInfo() to overwrite some pointers with faster versions.
  556. if (VP8GetCPUInfo != NULL) {
  557. #if defined(WEBP_USE_SSE2)
  558. if (VP8GetCPUInfo(kSSE2)) {
  559. VP8LDspInitSSE2();
  560. }
  561. #endif
  562. #if defined(WEBP_USE_NEON)
  563. if (VP8GetCPUInfo(kNEON)) {
  564. VP8LDspInitNEON();
  565. }
  566. #endif
  567. #if defined(WEBP_USE_MIPS_DSP_R2)
  568. if (VP8GetCPUInfo(kMIPSdspR2)) {
  569. VP8LDspInitMIPSdspR2();
  570. }
  571. #endif
  572. }
  573. lossless_last_cpuinfo_used = VP8GetCPUInfo;
  574. }
  575. //------------------------------------------------------------------------------