lossless_msa.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // Copyright 2016 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. // MSA variant of methods for lossless decoder
  11. //
  12. // Author: Prashant Patil (prashant.patil@imgtec.com)
  13. #include "./dsp.h"
  14. #if defined(WEBP_USE_MSA)
  15. #include "./lossless.h"
  16. #include "./msa_macro.h"
  17. //------------------------------------------------------------------------------
  18. // Colorspace conversion functions
  19. #define CONVERT16_BGRA_XXX(psrc, pdst, m0, m1, m2) do { \
  20. v16u8 src0, src1, src2, src3, dst0, dst1, dst2; \
  21. LD_UB4(psrc, 16, src0, src1, src2, src3); \
  22. VSHF_B2_UB(src0, src1, src1, src2, m0, m1, dst0, dst1); \
  23. dst2 = VSHF_UB(src2, src3, m2); \
  24. ST_UB2(dst0, dst1, pdst, 16); \
  25. ST_UB(dst2, pdst + 32); \
  26. } while (0)
  27. #define CONVERT12_BGRA_XXX(psrc, pdst, m0, m1, m2) do { \
  28. uint32_t pix_w; \
  29. v16u8 src0, src1, src2, dst0, dst1, dst2; \
  30. LD_UB3(psrc, 16, src0, src1, src2); \
  31. VSHF_B2_UB(src0, src1, src1, src2, m0, m1, dst0, dst1); \
  32. dst2 = VSHF_UB(src2, src2, m2); \
  33. ST_UB2(dst0, dst1, pdst, 16); \
  34. pix_w = __msa_copy_s_w((v4i32)dst2, 0); \
  35. SW(pix_w, pdst + 32); \
  36. } while (0)
  37. #define CONVERT8_BGRA_XXX(psrc, pdst, m0, m1) do { \
  38. uint64_t pix_d; \
  39. v16u8 src0, src1, src2 = { 0 }, dst0, dst1; \
  40. LD_UB2(psrc, 16, src0, src1); \
  41. VSHF_B2_UB(src0, src1, src1, src2, m0, m1, dst0, dst1); \
  42. ST_UB(dst0, pdst); \
  43. pix_d = __msa_copy_s_d((v2i64)dst1, 0); \
  44. SD(pix_d, pdst + 16); \
  45. } while (0)
  46. #define CONVERT4_BGRA_XXX(psrc, pdst, m) do { \
  47. const v16u8 src0 = LD_UB(psrc); \
  48. const v16u8 dst0 = VSHF_UB(src0, src0, m); \
  49. uint64_t pix_d = __msa_copy_s_d((v2i64)dst0, 0); \
  50. uint32_t pix_w = __msa_copy_s_w((v4i32)dst0, 2); \
  51. SD(pix_d, pdst + 0); \
  52. SW(pix_w, pdst + 8); \
  53. } while (0)
  54. #define CONVERT1_BGRA_BGR(psrc, pdst) do { \
  55. const int32_t b = (psrc)[0]; \
  56. const int32_t g = (psrc)[1]; \
  57. const int32_t r = (psrc)[2]; \
  58. (pdst)[0] = b; \
  59. (pdst)[1] = g; \
  60. (pdst)[2] = r; \
  61. } while (0)
  62. #define CONVERT1_BGRA_RGB(psrc, pdst) do { \
  63. const int32_t b = (psrc)[0]; \
  64. const int32_t g = (psrc)[1]; \
  65. const int32_t r = (psrc)[2]; \
  66. (pdst)[0] = r; \
  67. (pdst)[1] = g; \
  68. (pdst)[2] = b; \
  69. } while (0)
  70. #define TRANSFORM_COLOR_INVERSE_8(src0, src1, dst0, dst1, \
  71. c0, c1, mask0, mask1) do { \
  72. v8i16 g0, g1, t0, t1, t2, t3; \
  73. v4i32 t4, t5; \
  74. VSHF_B2_SH(src0, src0, src1, src1, mask0, mask0, g0, g1); \
  75. DOTP_SB2_SH(g0, g1, c0, c0, t0, t1); \
  76. SRAI_H2_SH(t0, t1, 5); \
  77. t0 = __msa_addv_h(t0, (v8i16)src0); \
  78. t1 = __msa_addv_h(t1, (v8i16)src1); \
  79. t4 = __msa_srli_w((v4i32)t0, 16); \
  80. t5 = __msa_srli_w((v4i32)t1, 16); \
  81. DOTP_SB2_SH(t4, t5, c1, c1, t2, t3); \
  82. SRAI_H2_SH(t2, t3, 5); \
  83. ADD2(t0, t2, t1, t3, t0, t1); \
  84. VSHF_B2_UB(src0, t0, src1, t1, mask1, mask1, dst0, dst1); \
  85. } while (0)
  86. #define TRANSFORM_COLOR_INVERSE_4(src, dst, c0, c1, mask0, mask1) do { \
  87. const v16i8 g0 = VSHF_SB(src, src, mask0); \
  88. v8i16 t0 = __msa_dotp_s_h(c0, g0); \
  89. v8i16 t1; \
  90. v4i32 t2; \
  91. t0 = SRAI_H(t0, 5); \
  92. t0 = __msa_addv_h(t0, (v8i16)src); \
  93. t2 = __msa_srli_w((v4i32)t0, 16); \
  94. t1 = __msa_dotp_s_h(c1, (v16i8)t2); \
  95. t1 = SRAI_H(t1, 5); \
  96. t0 = t0 + t1; \
  97. dst = VSHF_UB(src, t0, mask1); \
  98. } while (0)
  99. static void ConvertBGRAToRGBA_MSA(const uint32_t* src,
  100. int num_pixels, uint8_t* dst) {
  101. int i;
  102. const uint8_t* ptemp_src = (const uint8_t*)src;
  103. uint8_t* ptemp_dst = (uint8_t*)dst;
  104. v16u8 src0, dst0;
  105. const v16u8 mask = { 2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15 };
  106. while (num_pixels >= 8) {
  107. v16u8 src1, dst1;
  108. LD_UB2(ptemp_src, 16, src0, src1);
  109. VSHF_B2_UB(src0, src0, src1, src1, mask, mask, dst0, dst1);
  110. ST_UB2(dst0, dst1, ptemp_dst, 16);
  111. ptemp_src += 32;
  112. ptemp_dst += 32;
  113. num_pixels -= 8;
  114. }
  115. if (num_pixels > 0) {
  116. if (num_pixels >= 4) {
  117. src0 = LD_UB(ptemp_src);
  118. dst0 = VSHF_UB(src0, src0, mask);
  119. ST_UB(dst0, ptemp_dst);
  120. ptemp_src += 16;
  121. ptemp_dst += 16;
  122. num_pixels -= 4;
  123. }
  124. for (i = 0; i < num_pixels; i++) {
  125. const uint8_t b = ptemp_src[2];
  126. const uint8_t g = ptemp_src[1];
  127. const uint8_t r = ptemp_src[0];
  128. const uint8_t a = ptemp_src[3];
  129. ptemp_dst[0] = b;
  130. ptemp_dst[1] = g;
  131. ptemp_dst[2] = r;
  132. ptemp_dst[3] = a;
  133. ptemp_src += 4;
  134. ptemp_dst += 4;
  135. }
  136. }
  137. }
  138. static void ConvertBGRAToBGR_MSA(const uint32_t* src,
  139. int num_pixels, uint8_t* dst) {
  140. const uint8_t* ptemp_src = (const uint8_t*)src;
  141. uint8_t* ptemp_dst = (uint8_t*)dst;
  142. const v16u8 mask0 = { 0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14,
  143. 16, 17, 18, 20 };
  144. const v16u8 mask1 = { 5, 6, 8, 9, 10, 12, 13, 14, 16, 17, 18, 20,
  145. 21, 22, 24, 25 };
  146. const v16u8 mask2 = { 10, 12, 13, 14, 16, 17, 18, 20, 21, 22, 24, 25,
  147. 26, 28, 29, 30 };
  148. while (num_pixels >= 16) {
  149. CONVERT16_BGRA_XXX(ptemp_src, ptemp_dst, mask0, mask1, mask2);
  150. ptemp_src += 64;
  151. ptemp_dst += 48;
  152. num_pixels -= 16;
  153. }
  154. if (num_pixels > 0) {
  155. if (num_pixels >= 12) {
  156. CONVERT12_BGRA_XXX(ptemp_src, ptemp_dst, mask0, mask1, mask2);
  157. ptemp_src += 48;
  158. ptemp_dst += 36;
  159. num_pixels -= 12;
  160. } else if (num_pixels >= 8) {
  161. CONVERT8_BGRA_XXX(ptemp_src, ptemp_dst, mask0, mask1);
  162. ptemp_src += 32;
  163. ptemp_dst += 24;
  164. num_pixels -= 8;
  165. } else if (num_pixels >= 4) {
  166. CONVERT4_BGRA_XXX(ptemp_src, ptemp_dst, mask0);
  167. ptemp_src += 16;
  168. ptemp_dst += 12;
  169. num_pixels -= 4;
  170. }
  171. if (num_pixels == 3) {
  172. CONVERT1_BGRA_BGR(ptemp_src + 0, ptemp_dst + 0);
  173. CONVERT1_BGRA_BGR(ptemp_src + 4, ptemp_dst + 3);
  174. CONVERT1_BGRA_BGR(ptemp_src + 8, ptemp_dst + 6);
  175. } else if (num_pixels == 2) {
  176. CONVERT1_BGRA_BGR(ptemp_src + 0, ptemp_dst + 0);
  177. CONVERT1_BGRA_BGR(ptemp_src + 4, ptemp_dst + 3);
  178. } else if (num_pixels == 1) {
  179. CONVERT1_BGRA_BGR(ptemp_src, ptemp_dst);
  180. }
  181. }
  182. }
  183. static void ConvertBGRAToRGB_MSA(const uint32_t* src,
  184. int num_pixels, uint8_t* dst) {
  185. const uint8_t* ptemp_src = (const uint8_t*)src;
  186. uint8_t* ptemp_dst = (uint8_t*)dst;
  187. const v16u8 mask0 = { 2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12,
  188. 18, 17, 16, 22 };
  189. const v16u8 mask1 = { 5, 4, 10, 9, 8, 14, 13, 12, 18, 17, 16, 22,
  190. 21, 20, 26, 25 };
  191. const v16u8 mask2 = { 8, 14, 13, 12, 18, 17, 16, 22, 21, 20, 26, 25,
  192. 24, 30, 29, 28 };
  193. while (num_pixels >= 16) {
  194. CONVERT16_BGRA_XXX(ptemp_src, ptemp_dst, mask0, mask1, mask2);
  195. ptemp_src += 64;
  196. ptemp_dst += 48;
  197. num_pixels -= 16;
  198. }
  199. if (num_pixels) {
  200. if (num_pixels >= 12) {
  201. CONVERT12_BGRA_XXX(ptemp_src, ptemp_dst, mask0, mask1, mask2);
  202. ptemp_src += 48;
  203. ptemp_dst += 36;
  204. num_pixels -= 12;
  205. } else if (num_pixels >= 8) {
  206. CONVERT8_BGRA_XXX(ptemp_src, ptemp_dst, mask0, mask1);
  207. ptemp_src += 32;
  208. ptemp_dst += 24;
  209. num_pixels -= 8;
  210. } else if (num_pixels >= 4) {
  211. CONVERT4_BGRA_XXX(ptemp_src, ptemp_dst, mask0);
  212. ptemp_src += 16;
  213. ptemp_dst += 12;
  214. num_pixels -= 4;
  215. }
  216. if (num_pixels == 3) {
  217. CONVERT1_BGRA_RGB(ptemp_src + 0, ptemp_dst + 0);
  218. CONVERT1_BGRA_RGB(ptemp_src + 4, ptemp_dst + 3);
  219. CONVERT1_BGRA_RGB(ptemp_src + 8, ptemp_dst + 6);
  220. } else if (num_pixels == 2) {
  221. CONVERT1_BGRA_RGB(ptemp_src + 0, ptemp_dst + 0);
  222. CONVERT1_BGRA_RGB(ptemp_src + 4, ptemp_dst + 3);
  223. } else if (num_pixels == 1) {
  224. CONVERT1_BGRA_RGB(ptemp_src, ptemp_dst);
  225. }
  226. }
  227. }
  228. static void AddGreenToBlueAndRed_MSA(const uint32_t* const src, int num_pixels,
  229. uint32_t* dst) {
  230. int i;
  231. const uint8_t* in = (const uint8_t*)src;
  232. uint8_t* out = (uint8_t*)dst;
  233. v16u8 src0, dst0, tmp0;
  234. const v16u8 mask = { 1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255,
  235. 13, 255, 13, 255 };
  236. while (num_pixels >= 8) {
  237. v16u8 src1, dst1, tmp1;
  238. LD_UB2(in, 16, src0, src1);
  239. VSHF_B2_UB(src0, src1, src1, src0, mask, mask, tmp0, tmp1);
  240. ADD2(src0, tmp0, src1, tmp1, dst0, dst1);
  241. ST_UB2(dst0, dst1, out, 16);
  242. in += 32;
  243. out += 32;
  244. num_pixels -= 8;
  245. }
  246. if (num_pixels > 0) {
  247. if (num_pixels >= 4) {
  248. src0 = LD_UB(in);
  249. tmp0 = VSHF_UB(src0, src0, mask);
  250. dst0 = src0 + tmp0;
  251. ST_UB(dst0, out);
  252. in += 16;
  253. out += 16;
  254. num_pixels -= 4;
  255. }
  256. for (i = 0; i < num_pixels; i++) {
  257. const uint8_t b = in[0];
  258. const uint8_t g = in[1];
  259. const uint8_t r = in[2];
  260. out[0] = (b + g) & 0xff;
  261. out[1] = g;
  262. out[2] = (r + g) & 0xff;
  263. out[4] = in[4];
  264. out += 4;
  265. }
  266. }
  267. }
  268. static void TransformColorInverse_MSA(const VP8LMultipliers* const m,
  269. const uint32_t* src, int num_pixels,
  270. uint32_t* dst) {
  271. v16u8 src0, dst0;
  272. const v16i8 g2br = (v16i8)__msa_fill_w(m->green_to_blue_ |
  273. (m->green_to_red_ << 16));
  274. const v16i8 r2b = (v16i8)__msa_fill_w(m->red_to_blue_);
  275. const v16u8 mask0 = { 1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255,
  276. 13, 255, 13, 255 };
  277. const v16u8 mask1 = { 16, 1, 18, 3, 20, 5, 22, 7, 24, 9, 26, 11,
  278. 28, 13, 30, 15 };
  279. while (num_pixels >= 8) {
  280. v16u8 src1, dst1;
  281. LD_UB2(src, 4, src0, src1);
  282. TRANSFORM_COLOR_INVERSE_8(src0, src1, dst0, dst1, g2br, r2b, mask0, mask1);
  283. ST_UB2(dst0, dst1, dst, 4);
  284. src += 8;
  285. dst += 8;
  286. num_pixels -= 8;
  287. }
  288. if (num_pixels > 0) {
  289. if (num_pixels >= 4) {
  290. src0 = LD_UB(src);
  291. TRANSFORM_COLOR_INVERSE_4(src0, dst0, g2br, r2b, mask0, mask1);
  292. ST_UB(dst0, dst);
  293. src += 4;
  294. dst += 4;
  295. num_pixels -= 4;
  296. }
  297. if (num_pixels > 0) {
  298. src0 = LD_UB(src);
  299. TRANSFORM_COLOR_INVERSE_4(src0, dst0, g2br, r2b, mask0, mask1);
  300. if (num_pixels == 3) {
  301. const uint64_t pix_d = __msa_copy_s_d((v2i64)dst0, 0);
  302. const uint32_t pix_w = __msa_copy_s_w((v4i32)dst0, 2);
  303. SD(pix_d, dst + 0);
  304. SW(pix_w, dst + 2);
  305. } else if (num_pixels == 2) {
  306. const uint64_t pix_d = __msa_copy_s_d((v2i64)dst0, 0);
  307. SD(pix_d, dst);
  308. } else {
  309. const uint32_t pix_w = __msa_copy_s_w((v4i32)dst0, 0);
  310. SW(pix_w, dst);
  311. }
  312. }
  313. }
  314. }
  315. //------------------------------------------------------------------------------
  316. // Entry point
  317. extern void VP8LDspInitMSA(void);
  318. WEBP_TSAN_IGNORE_FUNCTION void VP8LDspInitMSA(void) {
  319. VP8LConvertBGRAToRGBA = ConvertBGRAToRGBA_MSA;
  320. VP8LConvertBGRAToBGR = ConvertBGRAToBGR_MSA;
  321. VP8LConvertBGRAToRGB = ConvertBGRAToRGB_MSA;
  322. VP8LAddGreenToBlueAndRed = AddGreenToBlueAndRed_MSA;
  323. VP8LTransformColorInverse = TransformColorInverse_MSA;
  324. }
  325. #else // !WEBP_USE_MSA
  326. WEBP_DSP_INIT_STUB(VP8LDspInitMSA)
  327. #endif // WEBP_USE_MSA