lossless.c 25 KB

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