upsampling_msa.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. static void YuvToRgb565(int y, int u, int v, uint8_t* const rgb) {
  240. const int y1 = MultHi(y, 19077);
  241. const int r1 = y1 + MultHi(v, 26149) - 14234;
  242. const int g1 = y1 - MultHi(u, 6419) - MultHi(v, 13320) + 8708;
  243. const int b1 = y1 + MultHi(u, 33050) - 17685;
  244. const int r = Clip8(r1 >> 6);
  245. const int g = Clip8(g1 >> 6);
  246. const int b = Clip8(b1 >> 6);
  247. const int rg = (r & 0xf8) | (g >> 5);
  248. const int gb = ((g << 3) & 0xe0) | (b >> 3);
  249. #ifdef WEBP_SWAP_16BIT_CSP
  250. rgb[0] = gb;
  251. rgb[1] = rg;
  252. #else
  253. rgb[0] = rg;
  254. rgb[1] = gb;
  255. #endif
  256. }
  257. static void YuvToRgba4444(int y, int u, int v, uint8_t* const argb) {
  258. const int y1 = MultHi(y, 19077);
  259. const int r1 = y1 + MultHi(v, 26149) - 14234;
  260. const int g1 = y1 - MultHi(u, 6419) - MultHi(v, 13320) + 8708;
  261. const int b1 = y1 + MultHi(u, 33050) - 17685;
  262. const int r = Clip8(r1 >> 6);
  263. const int g = Clip8(g1 >> 6);
  264. const int b = Clip8(b1 >> 6);
  265. const int rg = (r & 0xf0) | (g >> 4);
  266. const int ba = (b & 0xf0) | 0x0f; // overwrite the lower 4 bits
  267. #ifdef WEBP_SWAP_16BIT_CSP
  268. argb[0] = ba;
  269. argb[1] = rg;
  270. #else
  271. argb[0] = rg;
  272. argb[1] = ba;
  273. #endif
  274. }
  275. static void YuvToArgb(uint8_t y, uint8_t u, uint8_t v, uint8_t* const argb) {
  276. argb[0] = 0xff;
  277. YuvToRgb(y, u, v, argb + 1);
  278. }
  279. static void YuvToBgra(uint8_t y, uint8_t u, uint8_t v, uint8_t* const bgra) {
  280. YuvToBgr(y, u, v, bgra);
  281. bgra[3] = 0xff;
  282. }
  283. static void YuvToRgba(uint8_t y, uint8_t u, uint8_t v, uint8_t* const rgba) {
  284. YuvToRgb(y, u, v, rgba);
  285. rgba[3] = 0xff;
  286. }
  287. static void YuvToRgbLine(const uint8_t* y, const uint8_t* u,
  288. const uint8_t* v, uint8_t* dst, int length) {
  289. v16u8 R, G, B;
  290. while (length >= 16) {
  291. CALC_RGB16(y, u, v, R, G, B);
  292. STORE16_3(R, G, B, dst);
  293. y += 16;
  294. u += 16;
  295. v += 16;
  296. dst += 16 * 3;
  297. length -= 16;
  298. }
  299. if (length > 8) {
  300. uint8_t temp[3 * 16] = { 0 };
  301. memcpy(temp, y, length * sizeof(*temp));
  302. CALC_RGB16(temp, u, v, R, G, B);
  303. STORE16_3(R, G, B, temp);
  304. memcpy(dst, temp, length * 3 * sizeof(*dst));
  305. } else if (length > 0) {
  306. uint8_t temp[3 * 8] = { 0 };
  307. memcpy(temp, y, length * sizeof(*temp));
  308. CALC_RGB8(temp, u, v, R, G, B);
  309. STORE8_3(R, G, B, temp);
  310. memcpy(dst, temp, length * 3 * sizeof(*dst));
  311. }
  312. }
  313. static void YuvToBgrLine(const uint8_t* y, const uint8_t* u,
  314. const uint8_t* v, uint8_t* dst, int length) {
  315. v16u8 R, G, B;
  316. while (length >= 16) {
  317. CALC_RGB16(y, u, v, R, G, B);
  318. STORE16_3(B, G, R, dst);
  319. y += 16;
  320. u += 16;
  321. v += 16;
  322. dst += 16 * 3;
  323. length -= 16;
  324. }
  325. if (length > 8) {
  326. uint8_t temp[3 * 16] = { 0 };
  327. memcpy(temp, y, length * sizeof(*temp));
  328. CALC_RGB16(temp, u, v, R, G, B);
  329. STORE16_3(B, G, R, temp);
  330. memcpy(dst, temp, length * 3 * sizeof(*dst));
  331. } else if (length > 0) {
  332. uint8_t temp[3 * 8] = { 0 };
  333. memcpy(temp, y, length * sizeof(*temp));
  334. CALC_RGB8(temp, u, v, R, G, B);
  335. STORE8_3(B, G, R, temp);
  336. memcpy(dst, temp, length * 3 * sizeof(*dst));
  337. }
  338. }
  339. static void YuvToRgbaLine(const uint8_t* y, const uint8_t* u,
  340. const uint8_t* v, uint8_t* dst, int length) {
  341. v16u8 R, G, B;
  342. const v16u8 A = (v16u8)__msa_ldi_b(0xff);
  343. while (length >= 16) {
  344. CALC_RGB16(y, u, v, R, G, B);
  345. STORE16_4(R, G, B, A, dst);
  346. y += 16;
  347. u += 16;
  348. v += 16;
  349. dst += 16 * 4;
  350. length -= 16;
  351. }
  352. if (length > 8) {
  353. uint8_t temp[4 * 16] = { 0 };
  354. memcpy(temp, y, length * sizeof(*temp));
  355. CALC_RGB16(&temp[0], u, v, R, G, B);
  356. STORE16_4(R, G, B, A, temp);
  357. memcpy(dst, temp, length * 4 * sizeof(*dst));
  358. } else if (length > 0) {
  359. uint8_t temp[4 * 8] = { 0 };
  360. memcpy(temp, y, length * sizeof(*temp));
  361. CALC_RGB8(temp, u, v, R, G, B);
  362. STORE8_4(R, G, B, A, temp);
  363. memcpy(dst, temp, length * 4 * sizeof(*dst));
  364. }
  365. }
  366. static void YuvToBgraLine(const uint8_t* y, const uint8_t* u,
  367. const uint8_t* v, uint8_t* dst, int length) {
  368. v16u8 R, G, B;
  369. const v16u8 A = (v16u8)__msa_ldi_b(0xff);
  370. while (length >= 16) {
  371. CALC_RGB16(y, u, v, R, G, B);
  372. STORE16_4(B, G, R, A, dst);
  373. y += 16;
  374. u += 16;
  375. v += 16;
  376. dst += 16 * 4;
  377. length -= 16;
  378. }
  379. if (length > 8) {
  380. uint8_t temp[4 * 16] = { 0 };
  381. memcpy(temp, y, length * sizeof(*temp));
  382. CALC_RGB16(temp, u, v, R, G, B);
  383. STORE16_4(B, G, R, A, temp);
  384. memcpy(dst, temp, length * 4 * sizeof(*dst));
  385. } else if (length > 0) {
  386. uint8_t temp[4 * 8] = { 0 };
  387. memcpy(temp, y, length * sizeof(*temp));
  388. CALC_RGB8(temp, u, v, R, G, B);
  389. STORE8_4(B, G, R, A, temp);
  390. memcpy(dst, temp, length * 4 * sizeof(*dst));
  391. }
  392. }
  393. static void YuvToArgbLine(const uint8_t* y, const uint8_t* u,
  394. const uint8_t* v, uint8_t* dst, int length) {
  395. v16u8 R, G, B;
  396. const v16u8 A = (v16u8)__msa_ldi_b(0xff);
  397. while (length >= 16) {
  398. CALC_RGB16(y, u, v, R, G, B);
  399. STORE16_4(A, R, G, B, dst);
  400. y += 16;
  401. u += 16;
  402. v += 16;
  403. dst += 16 * 4;
  404. length -= 16;
  405. }
  406. if (length > 8) {
  407. uint8_t temp[4 * 16] = { 0 };
  408. memcpy(temp, y, length * sizeof(*temp));
  409. CALC_RGB16(temp, u, v, R, G, B);
  410. STORE16_4(A, R, G, B, temp);
  411. memcpy(dst, temp, length * 4 * sizeof(*dst));
  412. } else if (length > 0) {
  413. uint8_t temp[4 * 8] = { 0 };
  414. memcpy(temp, y, length * sizeof(*temp));
  415. CALC_RGB8(temp, u, v, R, G, B);
  416. STORE8_4(A, R, G, B, temp);
  417. memcpy(dst, temp, length * 4 * sizeof(*dst));
  418. }
  419. }
  420. static void YuvToRgba4444Line(const uint8_t* y, const uint8_t* u,
  421. const uint8_t* v, uint8_t* dst, int length) {
  422. v16u8 R, G, B, RG, BA, tmp0, tmp1;
  423. while (length >= 16) {
  424. #ifdef WEBP_SWAP_16BIT_CSP
  425. CALC_RGBA4444(y, u, v, BA, RG, 16, dst);
  426. #else
  427. CALC_RGBA4444(y, u, v, RG, BA, 16, dst);
  428. #endif
  429. y += 16;
  430. u += 16;
  431. v += 16;
  432. dst += 16 * 2;
  433. length -= 16;
  434. }
  435. if (length > 8) {
  436. uint8_t temp[2 * 16] = { 0 };
  437. memcpy(temp, y, length * sizeof(*temp));
  438. #ifdef WEBP_SWAP_16BIT_CSP
  439. CALC_RGBA4444(temp, u, v, BA, RG, 16, temp);
  440. #else
  441. CALC_RGBA4444(temp, u, v, RG, BA, 16, temp);
  442. #endif
  443. memcpy(dst, temp, length * 2 * sizeof(*dst));
  444. } else if (length > 0) {
  445. uint8_t temp[2 * 8] = { 0 };
  446. memcpy(temp, y, length * sizeof(*temp));
  447. #ifdef WEBP_SWAP_16BIT_CSP
  448. CALC_RGBA4444(temp, u, v, BA, RG, 8, temp);
  449. #else
  450. CALC_RGBA4444(temp, u, v, RG, BA, 8, temp);
  451. #endif
  452. memcpy(dst, temp, length * 2 * sizeof(*dst));
  453. }
  454. }
  455. static void YuvToRgb565Line(const uint8_t* y, const uint8_t* u,
  456. const uint8_t* v, uint8_t* dst, int length) {
  457. v16u8 R, G, B, RG, GB, tmp0, tmp1;
  458. while (length >= 16) {
  459. #ifdef WEBP_SWAP_16BIT_CSP
  460. CALC_RGB565(y, u, v, GB, RG, 16, dst);
  461. #else
  462. CALC_RGB565(y, u, v, RG, GB, 16, dst);
  463. #endif
  464. y += 16;
  465. u += 16;
  466. v += 16;
  467. dst += 16 * 2;
  468. length -= 16;
  469. }
  470. if (length > 8) {
  471. uint8_t temp[2 * 16] = { 0 };
  472. memcpy(temp, y, length * sizeof(*temp));
  473. #ifdef WEBP_SWAP_16BIT_CSP
  474. CALC_RGB565(temp, u, v, GB, RG, 16, temp);
  475. #else
  476. CALC_RGB565(temp, u, v, RG, GB, 16, temp);
  477. #endif
  478. memcpy(dst, temp, length * 2 * sizeof(*dst));
  479. } else if (length > 0) {
  480. uint8_t temp[2 * 8] = { 0 };
  481. memcpy(temp, y, length * sizeof(*temp));
  482. #ifdef WEBP_SWAP_16BIT_CSP
  483. CALC_RGB565(temp, u, v, GB, RG, 8, temp);
  484. #else
  485. CALC_RGB565(temp, u, v, RG, GB, 8, temp);
  486. #endif
  487. memcpy(dst, temp, length * 2 * sizeof(*dst));
  488. }
  489. }
  490. #define UPSAMPLE_32PIXELS(a, b, c, d) do { \
  491. v16u8 s = __msa_aver_u_b(a, d); \
  492. v16u8 t = __msa_aver_u_b(b, c); \
  493. const v16u8 st = s ^ t; \
  494. v16u8 ad = a ^ d; \
  495. v16u8 bc = b ^ c; \
  496. v16u8 t0 = ad | bc; \
  497. v16u8 t1 = t0 | st; \
  498. v16u8 t2 = ANDI_B(t1, 1); \
  499. v16u8 t3 = __msa_aver_u_b(s, t); \
  500. const v16u8 k = t3 - t2; \
  501. v16u8 diag1, diag2; \
  502. AVER_UB2_UB(t, k, s, k, t0, t1); \
  503. bc = bc & st; \
  504. ad = ad & st; \
  505. t = t ^ k; \
  506. s = s ^ k; \
  507. t2 = bc | t; \
  508. t3 = ad | s; \
  509. t2 = ANDI_B(t2, 1); \
  510. t3 = ANDI_B(t3, 1); \
  511. SUB2(t0, t2, t1, t3, diag1, diag2); \
  512. AVER_UB2_UB(a, diag1, b, diag2, t0, t1); \
  513. ILVRL_B2_UB(t1, t0, a, b); \
  514. if (pbot_y != NULL) { \
  515. AVER_UB2_UB(c, diag2, d, diag1, t0, t1); \
  516. ILVRL_B2_UB(t1, t0, c, d); \
  517. } \
  518. } while (0)
  519. #define UPSAMPLE_FUNC(FUNC_NAME, FUNC, XSTEP) \
  520. static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bot_y, \
  521. const uint8_t* top_u, const uint8_t* top_v, \
  522. const uint8_t* cur_u, const uint8_t* cur_v, \
  523. uint8_t* top_dst, uint8_t* bot_dst, int len) \
  524. { \
  525. int size = (len - 1) >> 1; \
  526. uint8_t temp_u[64]; \
  527. uint8_t temp_v[64]; \
  528. const uint32_t tl_uv = ((top_u[0]) | ((top_v[0]) << 16)); \
  529. const uint32_t l_uv = ((cur_u[0]) | ((cur_v[0]) << 16)); \
  530. const uint32_t uv0 = (3 * tl_uv + l_uv + 0x00020002u) >> 2; \
  531. const uint8_t* ptop_y = &top_y[1]; \
  532. uint8_t *ptop_dst = top_dst + XSTEP; \
  533. const uint8_t* pbot_y = &bot_y[1]; \
  534. uint8_t *pbot_dst = bot_dst + XSTEP; \
  535. \
  536. FUNC(top_y[0], uv0 & 0xff, (uv0 >> 16), top_dst); \
  537. if (bot_y != NULL) { \
  538. const uint32_t uv1 = (3 * l_uv + tl_uv + 0x00020002u) >> 2; \
  539. FUNC(bot_y[0], uv1 & 0xff, (uv1 >> 16), bot_dst); \
  540. } \
  541. while (size >= 16) { \
  542. v16u8 tu0, tu1, tv0, tv1, cu0, cu1, cv0, cv1; \
  543. LD_UB2(top_u, 1, tu0, tu1); \
  544. LD_UB2(cur_u, 1, cu0, cu1); \
  545. LD_UB2(top_v, 1, tv0, tv1); \
  546. LD_UB2(cur_v, 1, cv0, cv1); \
  547. UPSAMPLE_32PIXELS(tu0, tu1, cu0, cu1); \
  548. UPSAMPLE_32PIXELS(tv0, tv1, cv0, cv1); \
  549. ST_UB4(tu0, tu1, cu0, cu1, &temp_u[0], 16); \
  550. ST_UB4(tv0, tv1, cv0, cv1, &temp_v[0], 16); \
  551. FUNC##Line(ptop_y, &temp_u[ 0], &temp_v[0], ptop_dst, 32); \
  552. if (bot_y != NULL) { \
  553. FUNC##Line(pbot_y, &temp_u[32], &temp_v[32], pbot_dst, 32); \
  554. } \
  555. ptop_y += 32; \
  556. pbot_y += 32; \
  557. ptop_dst += XSTEP * 32; \
  558. pbot_dst += XSTEP * 32; \
  559. top_u += 16; \
  560. top_v += 16; \
  561. cur_u += 16; \
  562. cur_v += 16; \
  563. size -= 16; \
  564. } \
  565. if (size > 0) { \
  566. v16u8 tu0, tu1, tv0, tv1, cu0, cu1, cv0, cv1; \
  567. memcpy(&temp_u[ 0], top_u, 17 * sizeof(uint8_t)); \
  568. memcpy(&temp_u[32], cur_u, 17 * sizeof(uint8_t)); \
  569. memcpy(&temp_v[ 0], top_v, 17 * sizeof(uint8_t)); \
  570. memcpy(&temp_v[32], cur_v, 17 * sizeof(uint8_t)); \
  571. LD_UB2(&temp_u[ 0], 1, tu0, tu1); \
  572. LD_UB2(&temp_u[32], 1, cu0, cu1); \
  573. LD_UB2(&temp_v[ 0], 1, tv0, tv1); \
  574. LD_UB2(&temp_v[32], 1, cv0, cv1); \
  575. UPSAMPLE_32PIXELS(tu0, tu1, cu0, cu1); \
  576. UPSAMPLE_32PIXELS(tv0, tv1, cv0, cv1); \
  577. ST_UB4(tu0, tu1, cu0, cu1, &temp_u[0], 16); \
  578. ST_UB4(tv0, tv1, cv0, cv1, &temp_v[0], 16); \
  579. FUNC##Line(ptop_y, &temp_u[ 0], &temp_v[0], ptop_dst, size * 2); \
  580. if (bot_y != NULL) { \
  581. FUNC##Line(pbot_y, &temp_u[32], &temp_v[32], pbot_dst, size * 2); \
  582. } \
  583. top_u += size; \
  584. top_v += size; \
  585. cur_u += size; \
  586. cur_v += size; \
  587. } \
  588. if (!(len & 1)) { \
  589. const uint32_t t0 = ((top_u[0]) | ((top_v[0]) << 16)); \
  590. const uint32_t c0 = ((cur_u[0]) | ((cur_v[0]) << 16)); \
  591. const uint32_t tmp0 = (3 * t0 + c0 + 0x00020002u) >> 2; \
  592. FUNC(top_y[len - 1], tmp0 & 0xff, (tmp0 >> 16), \
  593. top_dst + (len - 1) * XSTEP); \
  594. if (bot_y != NULL) { \
  595. const uint32_t tmp1 = (3 * c0 + t0 + 0x00020002u) >> 2; \
  596. FUNC(bot_y[len - 1], tmp1 & 0xff, (tmp1 >> 16), \
  597. bot_dst + (len - 1) * XSTEP); \
  598. } \
  599. } \
  600. }
  601. UPSAMPLE_FUNC(UpsampleRgbLinePair, YuvToRgb, 3)
  602. UPSAMPLE_FUNC(UpsampleBgrLinePair, YuvToBgr, 3)
  603. UPSAMPLE_FUNC(UpsampleRgbaLinePair, YuvToRgba, 4)
  604. UPSAMPLE_FUNC(UpsampleBgraLinePair, YuvToBgra, 4)
  605. UPSAMPLE_FUNC(UpsampleArgbLinePair, YuvToArgb, 4)
  606. UPSAMPLE_FUNC(UpsampleRgba4444LinePair, YuvToRgba4444, 2)
  607. UPSAMPLE_FUNC(UpsampleRgb565LinePair, YuvToRgb565, 2)
  608. //------------------------------------------------------------------------------
  609. // Entry point
  610. extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */];
  611. extern void WebPInitUpsamplersMSA(void);
  612. WEBP_TSAN_IGNORE_FUNCTION void WebPInitUpsamplersMSA(void) {
  613. WebPUpsamplers[MODE_RGB] = UpsampleRgbLinePair;
  614. WebPUpsamplers[MODE_RGBA] = UpsampleRgbaLinePair;
  615. WebPUpsamplers[MODE_BGR] = UpsampleBgrLinePair;
  616. WebPUpsamplers[MODE_BGRA] = UpsampleBgraLinePair;
  617. WebPUpsamplers[MODE_ARGB] = UpsampleArgbLinePair;
  618. WebPUpsamplers[MODE_rgbA] = UpsampleRgbaLinePair;
  619. WebPUpsamplers[MODE_bgrA] = UpsampleBgraLinePair;
  620. WebPUpsamplers[MODE_Argb] = UpsampleArgbLinePair;
  621. WebPUpsamplers[MODE_RGB_565] = UpsampleRgb565LinePair;
  622. WebPUpsamplers[MODE_RGBA_4444] = UpsampleRgba4444LinePair;
  623. WebPUpsamplers[MODE_rgbA_4444] = UpsampleRgba4444LinePair;
  624. }
  625. #endif // FANCY_UPSAMPLING
  626. #endif // WEBP_USE_MSA
  627. #if !(defined(FANCY_UPSAMPLING) && defined(WEBP_USE_MSA))
  628. WEBP_DSP_INIT_STUB(WebPInitUpsamplersMSA)
  629. #endif