upsampling_msa.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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 version of YUV to RGB upsampling functions.
  11. //
  12. // Author: Prashant Patil (prashant.patil@imgtec.com)
  13. #include <string.h>
  14. #include "./dsp.h"
  15. #if defined(WEBP_USE_MSA)
  16. #include "./msa_macro.h"
  17. #include "./yuv.h"
  18. #ifdef FANCY_UPSAMPLING
  19. #define ILVR_UW2(in, out0, out1) do { \
  20. const v8i16 t0 = (v8i16)__msa_ilvr_b((v16i8)zero, (v16i8)in); \
  21. out0 = (v4u32)__msa_ilvr_h((v8i16)zero, t0); \
  22. out1 = (v4u32)__msa_ilvl_h((v8i16)zero, t0); \
  23. } while (0)
  24. #define ILVRL_UW4(in, out0, out1, out2, out3) do { \
  25. v16u8 t0, t1; \
  26. ILVRL_B2_UB(zero, in, t0, t1); \
  27. ILVRL_H2_UW(zero, t0, out0, out1); \
  28. ILVRL_H2_UW(zero, t1, out2, out3); \
  29. } while (0)
  30. #define MULTHI_16(in0, in1, in2, in3, cnst, out0, out1) do { \
  31. const v4i32 const0 = (v4i32)__msa_fill_w(cnst * 256); \
  32. v4u32 temp0, temp1, temp2, temp3; \
  33. MUL4(in0, const0, in1, const0, in2, const0, in3, const0, \
  34. temp0, temp1, temp2, temp3); \
  35. PCKOD_H2_UH(temp1, temp0, temp3, temp2, out0, out1); \
  36. } while (0)
  37. #define MULTHI_8(in0, in1, cnst, out0) do { \
  38. const v4i32 const0 = (v4i32)__msa_fill_w(cnst * 256); \
  39. v4u32 temp0, temp1; \
  40. MUL2(in0, const0, in1, const0, temp0, temp1); \
  41. out0 = (v8u16)__msa_pckod_h((v8i16)temp1, (v8i16)temp0); \
  42. } while (0)
  43. #define CALC_R16(y0, y1, v0, v1, dst) do { \
  44. const v8i16 const_a = (v8i16)__msa_fill_h(14234); \
  45. const v8i16 a0 = __msa_adds_s_h((v8i16)y0, (v8i16)v0); \
  46. const v8i16 a1 = __msa_adds_s_h((v8i16)y1, (v8i16)v1); \
  47. v8i16 b0 = __msa_subs_s_h(a0, const_a); \
  48. v8i16 b1 = __msa_subs_s_h(a1, const_a); \
  49. SRAI_H2_SH(b0, b1, 6); \
  50. CLIP_SH2_0_255(b0, b1); \
  51. dst = (v16u8)__msa_pckev_b((v16i8)b1, (v16i8)b0); \
  52. } while (0)
  53. #define CALC_R8(y0, v0, dst) do { \
  54. const v8i16 const_a = (v8i16)__msa_fill_h(14234); \
  55. const v8i16 a0 = __msa_adds_s_h((v8i16)y0, (v8i16)v0); \
  56. v8i16 b0 = __msa_subs_s_h(a0, const_a); \
  57. b0 = SRAI_H(b0, 6); \
  58. CLIP_SH_0_255(b0); \
  59. dst = (v16u8)__msa_pckev_b((v16i8)b0, (v16i8)b0); \
  60. } while (0)
  61. #define CALC_G16(y0, y1, u0, u1, v0, v1, dst) do { \
  62. const v8i16 const_a = (v8i16)__msa_fill_h(8708); \
  63. v8i16 a0 = __msa_subs_s_h((v8i16)y0, (v8i16)u0); \
  64. v8i16 a1 = __msa_subs_s_h((v8i16)y1, (v8i16)u1); \
  65. const v8i16 b0 = __msa_subs_s_h(a0, (v8i16)v0); \
  66. const v8i16 b1 = __msa_subs_s_h(a1, (v8i16)v1); \
  67. a0 = __msa_adds_s_h(b0, const_a); \
  68. a1 = __msa_adds_s_h(b1, const_a); \
  69. SRAI_H2_SH(a0, a1, 6); \
  70. CLIP_SH2_0_255(a0, a1); \
  71. dst = (v16u8)__msa_pckev_b((v16i8)a1, (v16i8)a0); \
  72. } while (0)
  73. #define CALC_G8(y0, u0, v0, dst) do { \
  74. const v8i16 const_a = (v8i16)__msa_fill_h(8708); \
  75. v8i16 a0 = __msa_subs_s_h((v8i16)y0, (v8i16)u0); \
  76. const v8i16 b0 = __msa_subs_s_h(a0, (v8i16)v0); \
  77. a0 = __msa_adds_s_h(b0, const_a); \
  78. a0 = SRAI_H(a0, 6); \
  79. CLIP_SH_0_255(a0); \
  80. dst = (v16u8)__msa_pckev_b((v16i8)a0, (v16i8)a0); \
  81. } while (0)
  82. #define CALC_B16(y0, y1, u0, u1, dst) do { \
  83. const v8u16 const_a = (v8u16)__msa_fill_h(17685); \
  84. const v8u16 a0 = __msa_adds_u_h((v8u16)y0, u0); \
  85. const v8u16 a1 = __msa_adds_u_h((v8u16)y1, u1); \
  86. v8u16 b0 = __msa_subs_u_h(a0, const_a); \
  87. v8u16 b1 = __msa_subs_u_h(a1, const_a); \
  88. SRAI_H2_UH(b0, b1, 6); \
  89. CLIP_UH2_0_255(b0, b1); \
  90. dst = (v16u8)__msa_pckev_b((v16i8)b1, (v16i8)b0); \
  91. } while (0)
  92. #define CALC_B8(y0, u0, dst) do { \
  93. const v8u16 const_a = (v8u16)__msa_fill_h(17685); \
  94. const v8u16 a0 = __msa_adds_u_h((v8u16)y0, u0); \
  95. v8u16 b0 = __msa_subs_u_h(a0, const_a); \
  96. b0 = SRAI_H(b0, 6); \
  97. CLIP_UH_0_255(b0); \
  98. dst = (v16u8)__msa_pckev_b((v16i8)b0, (v16i8)b0); \
  99. } while (0)
  100. #define CALC_RGB16(y, u, v, R, G, B) do { \
  101. const v16u8 zero = { 0 }; \
  102. v8u16 y0, y1, u0, u1, v0, v1; \
  103. v4u32 p0, p1, p2, p3; \
  104. const v16u8 in_y = LD_UB(y); \
  105. const v16u8 in_u = LD_UB(u); \
  106. const v16u8 in_v = LD_UB(v); \
  107. ILVRL_UW4(in_y, p0, p1, p2, p3); \
  108. MULTHI_16(p0, p1, p2, p3, 19077, y0, y1); \
  109. ILVRL_UW4(in_v, p0, p1, p2, p3); \
  110. MULTHI_16(p0, p1, p2, p3, 26149, v0, v1); \
  111. CALC_R16(y0, y1, v0, v1, R); \
  112. MULTHI_16(p0, p1, p2, p3, 13320, v0, v1); \
  113. ILVRL_UW4(in_u, p0, p1, p2, p3); \
  114. MULTHI_16(p0, p1, p2, p3, 6419, u0, u1); \
  115. CALC_G16(y0, y1, u0, u1, v0, v1, G); \
  116. MULTHI_16(p0, p1, p2, p3, 33050, u0, u1); \
  117. CALC_B16(y0, y1, u0, u1, B); \
  118. } while (0)
  119. #define CALC_RGB8(y, u, v, R, G, B) do { \
  120. const v16u8 zero = { 0 }; \
  121. v8u16 y0, u0, v0; \
  122. v4u32 p0, p1; \
  123. const v16u8 in_y = LD_UB(y); \
  124. const v16u8 in_u = LD_UB(u); \
  125. const v16u8 in_v = LD_UB(v); \
  126. ILVR_UW2(in_y, p0, p1); \
  127. MULTHI_8(p0, p1, 19077, y0); \
  128. ILVR_UW2(in_v, p0, p1); \
  129. MULTHI_8(p0, p1, 26149, v0); \
  130. CALC_R8(y0, v0, R); \
  131. MULTHI_8(p0, p1, 13320, v0); \
  132. ILVR_UW2(in_u, p0, p1); \
  133. MULTHI_8(p0, p1, 6419, u0); \
  134. CALC_G8(y0, u0, v0, G); \
  135. MULTHI_8(p0, p1, 33050, u0); \
  136. CALC_B8(y0, u0, B); \
  137. } while (0)
  138. #define STORE16_3(a0, a1, a2, dst) do { \
  139. const v16u8 mask0 = { 0, 1, 16, 2, 3, 17, 4, 5, 18, 6, 7, 19, \
  140. 8, 9, 20, 10 }; \
  141. const v16u8 mask1 = { 0, 21, 1, 2, 22, 3, 4, 23, 5, 6, 24, 7, \
  142. 8, 25, 9, 10 }; \
  143. const v16u8 mask2 = { 26, 0, 1, 27, 2, 3, 28, 4, 5, 29, 6, 7, \
  144. 30, 8, 9, 31 }; \
  145. v16u8 out0, out1, out2, tmp0, tmp1, tmp2; \
  146. ILVRL_B2_UB(a1, a0, tmp0, tmp1); \
  147. out0 = VSHF_UB(tmp0, a2, mask0); \
  148. tmp2 = SLDI_UB(tmp1, tmp0, 11); \
  149. out1 = VSHF_UB(tmp2, a2, mask1); \
  150. tmp2 = SLDI_UB(tmp1, tmp1, 6); \
  151. out2 = VSHF_UB(tmp2, a2, mask2); \
  152. ST_UB(out0, dst + 0); \
  153. ST_UB(out1, dst + 16); \
  154. ST_UB(out2, dst + 32); \
  155. } while (0)
  156. #define STORE8_3(a0, a1, a2, dst) do { \
  157. int64_t out_m; \
  158. const v16u8 mask0 = { 0, 1, 16, 2, 3, 17, 4, 5, 18, 6, 7, 19, \
  159. 8, 9, 20, 10 }; \
  160. const v16u8 mask1 = { 11, 21, 12, 13, 22, 14, 15, 23, \
  161. 255, 255, 255, 255, 255, 255, 255, 255 }; \
  162. const v16u8 tmp0 = (v16u8)__msa_ilvr_b((v16i8)a1, (v16i8)a0); \
  163. v16u8 out0, out1; \
  164. VSHF_B2_UB(tmp0, a2, tmp0, a2, mask0, mask1, out0, out1); \
  165. ST_UB(out0, dst); \
  166. out_m = __msa_copy_s_d((v2i64)out1, 0); \
  167. SD(out_m, dst + 16); \
  168. } while (0)
  169. #define STORE16_4(a0, a1, a2, a3, dst) do { \
  170. v16u8 tmp0, tmp1, tmp2, tmp3; \
  171. v16u8 out0, out1, out2, out3; \
  172. ILVRL_B2_UB(a1, a0, tmp0, tmp1); \
  173. ILVRL_B2_UB(a3, a2, tmp2, tmp3); \
  174. ILVRL_H2_UB(tmp2, tmp0, out0, out1); \
  175. ILVRL_H2_UB(tmp3, tmp1, out2, out3); \
  176. ST_UB(out0, dst + 0); \
  177. ST_UB(out1, dst + 16); \
  178. ST_UB(out2, dst + 32); \
  179. ST_UB(out3, dst + 48); \
  180. } while (0)
  181. #define STORE8_4(a0, a1, a2, a3, dst) do { \
  182. v16u8 tmp0, tmp1, tmp2, tmp3; \
  183. ILVR_B2_UB(a1, a0, a3, a2, tmp0, tmp1); \
  184. ILVRL_H2_UB(tmp1, tmp0, tmp2, tmp3); \
  185. ST_UB(tmp2, dst + 0); \
  186. ST_UB(tmp3, dst + 16); \
  187. } while (0)
  188. #define STORE2_16(a0, a1, dst) do { \
  189. v16u8 out0, out1; \
  190. ILVRL_B2_UB(a1, a0, out0, out1); \
  191. ST_UB(out0, dst + 0); \
  192. ST_UB(out1, dst + 16); \
  193. } while (0)
  194. #define STORE2_8(a0, a1, dst) do { \
  195. const v16u8 out0 = (v16u8)__msa_ilvr_b((v16i8)a1, (v16i8)a0); \
  196. ST_UB(out0, dst); \
  197. } while (0)
  198. #define CALC_RGBA4444(y, u, v, out0, out1, N, dst) do { \
  199. CALC_RGB##N(y, u, v, R, G, B); \
  200. tmp0 = ANDI_B(R, 0xf0); \
  201. tmp1 = SRAI_B(G, 4); \
  202. RG = tmp0 | tmp1; \
  203. tmp0 = ANDI_B(B, 0xf0); \
  204. BA = ORI_B(tmp0, 0x0f); \
  205. STORE2_##N(out0, out1, dst); \
  206. } while (0)
  207. #define CALC_RGB565(y, u, v, out0, out1, N, dst) do { \
  208. CALC_RGB##N(y, u, v, R, G, B); \
  209. tmp0 = ANDI_B(R, 0xf8); \
  210. tmp1 = SRAI_B(G, 5); \
  211. RG = tmp0 | tmp1; \
  212. tmp0 = SLLI_B(G, 3); \
  213. tmp1 = ANDI_B(tmp0, 0xe0); \
  214. tmp0 = SRAI_B(B, 3); \
  215. GB = tmp0 | tmp1; \
  216. STORE2_##N(out0, out1, dst); \
  217. } while (0)
  218. static WEBP_INLINE int Clip8(int v) {
  219. return v < 0 ? 0 : v > 255 ? 255 : v;
  220. }
  221. static void YuvToRgb(int y, int u, int v, uint8_t* const rgb) {
  222. const int y1 = MultHi(y, 19077);
  223. const int r1 = y1 + MultHi(v, 26149) - 14234;
  224. const int g1 = y1 - MultHi(u, 6419) - MultHi(v, 13320) + 8708;
  225. const int b1 = y1 + MultHi(u, 33050) - 17685;
  226. rgb[0] = Clip8(r1 >> 6);
  227. rgb[1] = Clip8(g1 >> 6);
  228. rgb[2] = Clip8(b1 >> 6);
  229. }
  230. static void YuvToBgr(int y, int u, int v, uint8_t* const bgr) {
  231. const int y1 = MultHi(y, 19077);
  232. const int r1 = y1 + MultHi(v, 26149) - 14234;
  233. const int g1 = y1 - MultHi(u, 6419) - MultHi(v, 13320) + 8708;
  234. const int b1 = y1 + MultHi(u, 33050) - 17685;
  235. bgr[0] = Clip8(b1 >> 6);
  236. bgr[1] = Clip8(g1 >> 6);
  237. bgr[2] = Clip8(r1 >> 6);
  238. }
  239. #if !defined(WEBP_REDUCE_CSP)
  240. static void YuvToRgb565(int y, int u, int v, uint8_t* const rgb) {
  241. const int y1 = MultHi(y, 19077);
  242. const int r1 = y1 + MultHi(v, 26149) - 14234;
  243. const int g1 = y1 - MultHi(u, 6419) - MultHi(v, 13320) + 8708;
  244. const int b1 = y1 + MultHi(u, 33050) - 17685;
  245. const int r = Clip8(r1 >> 6);
  246. const int g = Clip8(g1 >> 6);
  247. const int b = Clip8(b1 >> 6);
  248. const int rg = (r & 0xf8) | (g >> 5);
  249. const int gb = ((g << 3) & 0xe0) | (b >> 3);
  250. #if (WEBP_SWAP_16BIT_CSP == 1)
  251. rgb[0] = gb;
  252. rgb[1] = rg;
  253. #else
  254. rgb[0] = rg;
  255. rgb[1] = gb;
  256. #endif
  257. }
  258. static void YuvToRgba4444(int y, int u, int v, uint8_t* const argb) {
  259. const int y1 = MultHi(y, 19077);
  260. const int r1 = y1 + MultHi(v, 26149) - 14234;
  261. const int g1 = y1 - MultHi(u, 6419) - MultHi(v, 13320) + 8708;
  262. const int b1 = y1 + MultHi(u, 33050) - 17685;
  263. const int r = Clip8(r1 >> 6);
  264. const int g = Clip8(g1 >> 6);
  265. const int b = Clip8(b1 >> 6);
  266. const int rg = (r & 0xf0) | (g >> 4);
  267. const int ba = (b & 0xf0) | 0x0f; // overwrite the lower 4 bits
  268. #if (WEBP_SWAP_16BIT_CSP == 1)
  269. argb[0] = ba;
  270. argb[1] = rg;
  271. #else
  272. argb[0] = rg;
  273. argb[1] = ba;
  274. #endif
  275. }
  276. static void YuvToArgb(uint8_t y, uint8_t u, uint8_t v, uint8_t* const argb) {
  277. argb[0] = 0xff;
  278. YuvToRgb(y, u, v, argb + 1);
  279. }
  280. #endif // WEBP_REDUCE_CSP
  281. static void YuvToBgra(uint8_t y, uint8_t u, uint8_t v, uint8_t* const bgra) {
  282. YuvToBgr(y, u, v, bgra);
  283. bgra[3] = 0xff;
  284. }
  285. static void YuvToRgba(uint8_t y, uint8_t u, uint8_t v, uint8_t* const rgba) {
  286. YuvToRgb(y, u, v, rgba);
  287. rgba[3] = 0xff;
  288. }
  289. #if !defined(WEBP_REDUCE_CSP)
  290. static void YuvToRgbLine(const uint8_t* y, const uint8_t* u,
  291. const uint8_t* v, uint8_t* dst, int length) {
  292. v16u8 R, G, B;
  293. while (length >= 16) {
  294. CALC_RGB16(y, u, v, R, G, B);
  295. STORE16_3(R, G, B, dst);
  296. y += 16;
  297. u += 16;
  298. v += 16;
  299. dst += 16 * 3;
  300. length -= 16;
  301. }
  302. if (length > 8) {
  303. uint8_t temp[3 * 16] = { 0 };
  304. memcpy(temp, y, length * sizeof(*temp));
  305. CALC_RGB16(temp, u, v, R, G, B);
  306. STORE16_3(R, G, B, temp);
  307. memcpy(dst, temp, length * 3 * sizeof(*dst));
  308. } else if (length > 0) {
  309. uint8_t temp[3 * 8] = { 0 };
  310. memcpy(temp, y, length * sizeof(*temp));
  311. CALC_RGB8(temp, u, v, R, G, B);
  312. STORE8_3(R, G, B, temp);
  313. memcpy(dst, temp, length * 3 * sizeof(*dst));
  314. }
  315. }
  316. static void YuvToBgrLine(const uint8_t* y, const uint8_t* u,
  317. const uint8_t* v, uint8_t* dst, int length) {
  318. v16u8 R, G, B;
  319. while (length >= 16) {
  320. CALC_RGB16(y, u, v, R, G, B);
  321. STORE16_3(B, G, R, dst);
  322. y += 16;
  323. u += 16;
  324. v += 16;
  325. dst += 16 * 3;
  326. length -= 16;
  327. }
  328. if (length > 8) {
  329. uint8_t temp[3 * 16] = { 0 };
  330. memcpy(temp, y, length * sizeof(*temp));
  331. CALC_RGB16(temp, u, v, R, G, B);
  332. STORE16_3(B, G, R, temp);
  333. memcpy(dst, temp, length * 3 * sizeof(*dst));
  334. } else if (length > 0) {
  335. uint8_t temp[3 * 8] = { 0 };
  336. memcpy(temp, y, length * sizeof(*temp));
  337. CALC_RGB8(temp, u, v, R, G, B);
  338. STORE8_3(B, G, R, temp);
  339. memcpy(dst, temp, length * 3 * sizeof(*dst));
  340. }
  341. }
  342. #endif // WEBP_REDUCE_CSP
  343. static void YuvToRgbaLine(const uint8_t* y, const uint8_t* u,
  344. const uint8_t* v, uint8_t* dst, int length) {
  345. v16u8 R, G, B;
  346. const v16u8 A = (v16u8)__msa_ldi_b(ALPHAVAL);
  347. while (length >= 16) {
  348. CALC_RGB16(y, u, v, R, G, B);
  349. STORE16_4(R, G, B, A, dst);
  350. y += 16;
  351. u += 16;
  352. v += 16;
  353. dst += 16 * 4;
  354. length -= 16;
  355. }
  356. if (length > 8) {
  357. uint8_t temp[4 * 16] = { 0 };
  358. memcpy(temp, y, length * sizeof(*temp));
  359. CALC_RGB16(&temp[0], u, v, R, G, B);
  360. STORE16_4(R, G, B, A, temp);
  361. memcpy(dst, temp, length * 4 * sizeof(*dst));
  362. } else if (length > 0) {
  363. uint8_t temp[4 * 8] = { 0 };
  364. memcpy(temp, y, length * sizeof(*temp));
  365. CALC_RGB8(temp, u, v, R, G, B);
  366. STORE8_4(R, G, B, A, temp);
  367. memcpy(dst, temp, length * 4 * sizeof(*dst));
  368. }
  369. }
  370. static void YuvToBgraLine(const uint8_t* y, const uint8_t* u,
  371. const uint8_t* v, uint8_t* dst, int length) {
  372. v16u8 R, G, B;
  373. const v16u8 A = (v16u8)__msa_ldi_b(ALPHAVAL);
  374. while (length >= 16) {
  375. CALC_RGB16(y, u, v, R, G, B);
  376. STORE16_4(B, G, R, A, dst);
  377. y += 16;
  378. u += 16;
  379. v += 16;
  380. dst += 16 * 4;
  381. length -= 16;
  382. }
  383. if (length > 8) {
  384. uint8_t temp[4 * 16] = { 0 };
  385. memcpy(temp, y, length * sizeof(*temp));
  386. CALC_RGB16(temp, u, v, R, G, B);
  387. STORE16_4(B, G, R, A, temp);
  388. memcpy(dst, temp, length * 4 * sizeof(*dst));
  389. } else if (length > 0) {
  390. uint8_t temp[4 * 8] = { 0 };
  391. memcpy(temp, y, length * sizeof(*temp));
  392. CALC_RGB8(temp, u, v, R, G, B);
  393. STORE8_4(B, G, R, A, temp);
  394. memcpy(dst, temp, length * 4 * sizeof(*dst));
  395. }
  396. }
  397. #if !defined(WEBP_REDUCE_CSP)
  398. static void YuvToArgbLine(const uint8_t* y, const uint8_t* u,
  399. const uint8_t* v, uint8_t* dst, int length) {
  400. v16u8 R, G, B;
  401. const v16u8 A = (v16u8)__msa_ldi_b(ALPHAVAL);
  402. while (length >= 16) {
  403. CALC_RGB16(y, u, v, R, G, B);
  404. STORE16_4(A, R, G, B, dst);
  405. y += 16;
  406. u += 16;
  407. v += 16;
  408. dst += 16 * 4;
  409. length -= 16;
  410. }
  411. if (length > 8) {
  412. uint8_t temp[4 * 16] = { 0 };
  413. memcpy(temp, y, length * sizeof(*temp));
  414. CALC_RGB16(temp, u, v, R, G, B);
  415. STORE16_4(A, R, G, B, temp);
  416. memcpy(dst, temp, length * 4 * sizeof(*dst));
  417. } else if (length > 0) {
  418. uint8_t temp[4 * 8] = { 0 };
  419. memcpy(temp, y, length * sizeof(*temp));
  420. CALC_RGB8(temp, u, v, R, G, B);
  421. STORE8_4(A, R, G, B, temp);
  422. memcpy(dst, temp, length * 4 * sizeof(*dst));
  423. }
  424. }
  425. static void YuvToRgba4444Line(const uint8_t* y, const uint8_t* u,
  426. const uint8_t* v, uint8_t* dst, int length) {
  427. v16u8 R, G, B, RG, BA, tmp0, tmp1;
  428. while (length >= 16) {
  429. #if (WEBP_SWAP_16BIT_CSP == 1)
  430. CALC_RGBA4444(y, u, v, BA, RG, 16, dst);
  431. #else
  432. CALC_RGBA4444(y, u, v, RG, BA, 16, dst);
  433. #endif
  434. y += 16;
  435. u += 16;
  436. v += 16;
  437. dst += 16 * 2;
  438. length -= 16;
  439. }
  440. if (length > 8) {
  441. uint8_t temp[2 * 16] = { 0 };
  442. memcpy(temp, y, length * sizeof(*temp));
  443. #if (WEBP_SWAP_16BIT_CSP == 1)
  444. CALC_RGBA4444(temp, u, v, BA, RG, 16, temp);
  445. #else
  446. CALC_RGBA4444(temp, u, v, RG, BA, 16, temp);
  447. #endif
  448. memcpy(dst, temp, length * 2 * sizeof(*dst));
  449. } else if (length > 0) {
  450. uint8_t temp[2 * 8] = { 0 };
  451. memcpy(temp, y, length * sizeof(*temp));
  452. #if (WEBP_SWAP_16BIT_CSP == 1)
  453. CALC_RGBA4444(temp, u, v, BA, RG, 8, temp);
  454. #else
  455. CALC_RGBA4444(temp, u, v, RG, BA, 8, temp);
  456. #endif
  457. memcpy(dst, temp, length * 2 * sizeof(*dst));
  458. }
  459. }
  460. static void YuvToRgb565Line(const uint8_t* y, const uint8_t* u,
  461. const uint8_t* v, uint8_t* dst, int length) {
  462. v16u8 R, G, B, RG, GB, tmp0, tmp1;
  463. while (length >= 16) {
  464. #if (WEBP_SWAP_16BIT_CSP == 1)
  465. CALC_RGB565(y, u, v, GB, RG, 16, dst);
  466. #else
  467. CALC_RGB565(y, u, v, RG, GB, 16, dst);
  468. #endif
  469. y += 16;
  470. u += 16;
  471. v += 16;
  472. dst += 16 * 2;
  473. length -= 16;
  474. }
  475. if (length > 8) {
  476. uint8_t temp[2 * 16] = { 0 };
  477. memcpy(temp, y, length * sizeof(*temp));
  478. #if (WEBP_SWAP_16BIT_CSP == 1)
  479. CALC_RGB565(temp, u, v, GB, RG, 16, temp);
  480. #else
  481. CALC_RGB565(temp, u, v, RG, GB, 16, temp);
  482. #endif
  483. memcpy(dst, temp, length * 2 * sizeof(*dst));
  484. } else if (length > 0) {
  485. uint8_t temp[2 * 8] = { 0 };
  486. memcpy(temp, y, length * sizeof(*temp));
  487. #if (WEBP_SWAP_16BIT_CSP == 1)
  488. CALC_RGB565(temp, u, v, GB, RG, 8, temp);
  489. #else
  490. CALC_RGB565(temp, u, v, RG, GB, 8, temp);
  491. #endif
  492. memcpy(dst, temp, length * 2 * sizeof(*dst));
  493. }
  494. }
  495. #endif // WEBP_REDUCE_CSP
  496. #define UPSAMPLE_32PIXELS(a, b, c, d) do { \
  497. v16u8 s = __msa_aver_u_b(a, d); \
  498. v16u8 t = __msa_aver_u_b(b, c); \
  499. const v16u8 st = s ^ t; \
  500. v16u8 ad = a ^ d; \
  501. v16u8 bc = b ^ c; \
  502. v16u8 t0 = ad | bc; \
  503. v16u8 t1 = t0 | st; \
  504. v16u8 t2 = ANDI_B(t1, 1); \
  505. v16u8 t3 = __msa_aver_u_b(s, t); \
  506. const v16u8 k = t3 - t2; \
  507. v16u8 diag1, diag2; \
  508. AVER_UB2_UB(t, k, s, k, t0, t1); \
  509. bc = bc & st; \
  510. ad = ad & st; \
  511. t = t ^ k; \
  512. s = s ^ k; \
  513. t2 = bc | t; \
  514. t3 = ad | s; \
  515. t2 = ANDI_B(t2, 1); \
  516. t3 = ANDI_B(t3, 1); \
  517. SUB2(t0, t2, t1, t3, diag1, diag2); \
  518. AVER_UB2_UB(a, diag1, b, diag2, t0, t1); \
  519. ILVRL_B2_UB(t1, t0, a, b); \
  520. if (pbot_y != NULL) { \
  521. AVER_UB2_UB(c, diag2, d, diag1, t0, t1); \
  522. ILVRL_B2_UB(t1, t0, c, d); \
  523. } \
  524. } while (0)
  525. #define UPSAMPLE_FUNC(FUNC_NAME, FUNC, XSTEP) \
  526. static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bot_y, \
  527. const uint8_t* top_u, const uint8_t* top_v, \
  528. const uint8_t* cur_u, const uint8_t* cur_v, \
  529. uint8_t* top_dst, uint8_t* bot_dst, int len) \
  530. { \
  531. int size = (len - 1) >> 1; \
  532. uint8_t temp_u[64]; \
  533. uint8_t temp_v[64]; \
  534. const uint32_t tl_uv = ((top_u[0]) | ((top_v[0]) << 16)); \
  535. const uint32_t l_uv = ((cur_u[0]) | ((cur_v[0]) << 16)); \
  536. const uint32_t uv0 = (3 * tl_uv + l_uv + 0x00020002u) >> 2; \
  537. const uint8_t* ptop_y = &top_y[1]; \
  538. uint8_t* ptop_dst = top_dst + XSTEP; \
  539. const uint8_t* pbot_y = &bot_y[1]; \
  540. uint8_t* pbot_dst = bot_dst + XSTEP; \
  541. \
  542. FUNC(top_y[0], uv0 & 0xff, (uv0 >> 16), top_dst); \
  543. if (bot_y != NULL) { \
  544. const uint32_t uv1 = (3 * l_uv + tl_uv + 0x00020002u) >> 2; \
  545. FUNC(bot_y[0], uv1 & 0xff, (uv1 >> 16), bot_dst); \
  546. } \
  547. while (size >= 16) { \
  548. v16u8 tu0, tu1, tv0, tv1, cu0, cu1, cv0, cv1; \
  549. LD_UB2(top_u, 1, tu0, tu1); \
  550. LD_UB2(cur_u, 1, cu0, cu1); \
  551. LD_UB2(top_v, 1, tv0, tv1); \
  552. LD_UB2(cur_v, 1, cv0, cv1); \
  553. UPSAMPLE_32PIXELS(tu0, tu1, cu0, cu1); \
  554. UPSAMPLE_32PIXELS(tv0, tv1, cv0, cv1); \
  555. ST_UB4(tu0, tu1, cu0, cu1, &temp_u[0], 16); \
  556. ST_UB4(tv0, tv1, cv0, cv1, &temp_v[0], 16); \
  557. FUNC##Line(ptop_y, &temp_u[ 0], &temp_v[0], ptop_dst, 32); \
  558. if (bot_y != NULL) { \
  559. FUNC##Line(pbot_y, &temp_u[32], &temp_v[32], pbot_dst, 32); \
  560. } \
  561. ptop_y += 32; \
  562. pbot_y += 32; \
  563. ptop_dst += XSTEP * 32; \
  564. pbot_dst += XSTEP * 32; \
  565. top_u += 16; \
  566. top_v += 16; \
  567. cur_u += 16; \
  568. cur_v += 16; \
  569. size -= 16; \
  570. } \
  571. if (size > 0) { \
  572. v16u8 tu0, tu1, tv0, tv1, cu0, cu1, cv0, cv1; \
  573. memcpy(&temp_u[ 0], top_u, 17 * sizeof(uint8_t)); \
  574. memcpy(&temp_u[32], cur_u, 17 * sizeof(uint8_t)); \
  575. memcpy(&temp_v[ 0], top_v, 17 * sizeof(uint8_t)); \
  576. memcpy(&temp_v[32], cur_v, 17 * sizeof(uint8_t)); \
  577. LD_UB2(&temp_u[ 0], 1, tu0, tu1); \
  578. LD_UB2(&temp_u[32], 1, cu0, cu1); \
  579. LD_UB2(&temp_v[ 0], 1, tv0, tv1); \
  580. LD_UB2(&temp_v[32], 1, cv0, cv1); \
  581. UPSAMPLE_32PIXELS(tu0, tu1, cu0, cu1); \
  582. UPSAMPLE_32PIXELS(tv0, tv1, cv0, cv1); \
  583. ST_UB4(tu0, tu1, cu0, cu1, &temp_u[0], 16); \
  584. ST_UB4(tv0, tv1, cv0, cv1, &temp_v[0], 16); \
  585. FUNC##Line(ptop_y, &temp_u[ 0], &temp_v[0], ptop_dst, size * 2); \
  586. if (bot_y != NULL) { \
  587. FUNC##Line(pbot_y, &temp_u[32], &temp_v[32], pbot_dst, size * 2); \
  588. } \
  589. top_u += size; \
  590. top_v += size; \
  591. cur_u += size; \
  592. cur_v += size; \
  593. } \
  594. if (!(len & 1)) { \
  595. const uint32_t t0 = ((top_u[0]) | ((top_v[0]) << 16)); \
  596. const uint32_t c0 = ((cur_u[0]) | ((cur_v[0]) << 16)); \
  597. const uint32_t tmp0 = (3 * t0 + c0 + 0x00020002u) >> 2; \
  598. FUNC(top_y[len - 1], tmp0 & 0xff, (tmp0 >> 16), \
  599. top_dst + (len - 1) * XSTEP); \
  600. if (bot_y != NULL) { \
  601. const uint32_t tmp1 = (3 * c0 + t0 + 0x00020002u) >> 2; \
  602. FUNC(bot_y[len - 1], tmp1 & 0xff, (tmp1 >> 16), \
  603. bot_dst + (len - 1) * XSTEP); \
  604. } \
  605. } \
  606. }
  607. UPSAMPLE_FUNC(UpsampleRgbaLinePair, YuvToRgba, 4)
  608. UPSAMPLE_FUNC(UpsampleBgraLinePair, YuvToBgra, 4)
  609. #if !defined(WEBP_REDUCE_CSP)
  610. UPSAMPLE_FUNC(UpsampleRgbLinePair, YuvToRgb, 3)
  611. UPSAMPLE_FUNC(UpsampleBgrLinePair, YuvToBgr, 3)
  612. UPSAMPLE_FUNC(UpsampleArgbLinePair, YuvToArgb, 4)
  613. UPSAMPLE_FUNC(UpsampleRgba4444LinePair, YuvToRgba4444, 2)
  614. UPSAMPLE_FUNC(UpsampleRgb565LinePair, YuvToRgb565, 2)
  615. #endif // WEBP_REDUCE_CSP
  616. //------------------------------------------------------------------------------
  617. // Entry point
  618. extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */];
  619. extern void WebPInitUpsamplersMSA(void);
  620. WEBP_TSAN_IGNORE_FUNCTION void WebPInitUpsamplersMSA(void) {
  621. WebPUpsamplers[MODE_RGBA] = UpsampleRgbaLinePair;
  622. WebPUpsamplers[MODE_BGRA] = UpsampleBgraLinePair;
  623. WebPUpsamplers[MODE_rgbA] = UpsampleRgbaLinePair;
  624. WebPUpsamplers[MODE_bgrA] = UpsampleBgraLinePair;
  625. #if !defined(WEBP_REDUCE_CSP)
  626. WebPUpsamplers[MODE_RGB] = UpsampleRgbLinePair;
  627. WebPUpsamplers[MODE_BGR] = UpsampleBgrLinePair;
  628. WebPUpsamplers[MODE_ARGB] = UpsampleArgbLinePair;
  629. WebPUpsamplers[MODE_Argb] = UpsampleArgbLinePair;
  630. WebPUpsamplers[MODE_RGB_565] = UpsampleRgb565LinePair;
  631. WebPUpsamplers[MODE_RGBA_4444] = UpsampleRgba4444LinePair;
  632. WebPUpsamplers[MODE_rgbA_4444] = UpsampleRgba4444LinePair;
  633. #endif // WEBP_REDUCE_CSP
  634. }
  635. #endif // FANCY_UPSAMPLING
  636. #endif // WEBP_USE_MSA
  637. #if !(defined(FANCY_UPSAMPLING) && defined(WEBP_USE_MSA))
  638. WEBP_DSP_INIT_STUB(WebPInitUpsamplersMSA)
  639. #endif