yuv2rgb_lasx.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright (C) 2022 Loongson Technology Corporation Limited
  3. * Contributed by Hao Chen(chenhao@loongson.cn)
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "swscale_loongarch.h"
  22. #include "libavutil/loongarch/loongson_intrinsics.h"
  23. #define YUV2RGB_LOAD_COE \
  24. /* Load x_offset */ \
  25. __m256i y_offset = __lasx_xvreplgr2vr_d(c->yOffset); \
  26. __m256i u_offset = __lasx_xvreplgr2vr_d(c->uOffset); \
  27. __m256i v_offset = __lasx_xvreplgr2vr_d(c->vOffset); \
  28. /* Load x_coeff */ \
  29. __m256i ug_coeff = __lasx_xvreplgr2vr_d(c->ugCoeff); \
  30. __m256i vg_coeff = __lasx_xvreplgr2vr_d(c->vgCoeff); \
  31. __m256i y_coeff = __lasx_xvreplgr2vr_d(c->yCoeff); \
  32. __m256i ub_coeff = __lasx_xvreplgr2vr_d(c->ubCoeff); \
  33. __m256i vr_coeff = __lasx_xvreplgr2vr_d(c->vrCoeff); \
  34. #define LOAD_YUV_16 \
  35. m_y1 = __lasx_xvld(py_1, 0); \
  36. m_y2 = __lasx_xvld(py_2, 0); \
  37. m_u = __lasx_xvldrepl_d(pu, 0); \
  38. m_v = __lasx_xvldrepl_d(pv, 0); \
  39. m_u = __lasx_xvilvl_b(m_u, m_u); \
  40. m_v = __lasx_xvilvl_b(m_v, m_v); \
  41. DUP4_ARG1(__lasx_vext2xv_hu_bu, m_y1, m_y2, m_u, m_v, \
  42. m_y1, m_y2, m_u, m_v); \
  43. /* YUV2RGB method
  44. * The conversion method is as follows:
  45. * R = Y' * y_coeff + V' * vr_coeff
  46. * G = Y' * y_coeff + V' * vg_coeff + U' * ug_coeff
  47. * B = Y' * y_coeff + U' * ub_coeff
  48. *
  49. * where X' = X * 8 - x_offset
  50. *
  51. */
  52. #define YUV2RGB \
  53. m_y1 = __lasx_xvslli_h(m_y1, 3); \
  54. m_y2 = __lasx_xvslli_h(m_y2, 3); \
  55. m_u = __lasx_xvslli_h(m_u, 3); \
  56. m_v = __lasx_xvslli_h(m_v, 3); \
  57. m_y1 = __lasx_xvsub_h(m_y1, y_offset); \
  58. m_y2 = __lasx_xvsub_h(m_y2, y_offset); \
  59. m_u = __lasx_xvsub_h(m_u, u_offset); \
  60. m_v = __lasx_xvsub_h(m_v, v_offset); \
  61. y_1 = __lasx_xvmuh_h(m_y1, y_coeff); \
  62. y_2 = __lasx_xvmuh_h(m_y2, y_coeff); \
  63. u2g = __lasx_xvmuh_h(m_u, ug_coeff); \
  64. u2b = __lasx_xvmuh_h(m_u, ub_coeff); \
  65. v2r = __lasx_xvmuh_h(m_v, vr_coeff); \
  66. v2g = __lasx_xvmuh_h(m_v, vg_coeff); \
  67. r1 = __lasx_xvsadd_h(y_1, v2r); \
  68. v2g = __lasx_xvsadd_h(v2g, u2g); \
  69. g1 = __lasx_xvsadd_h(y_1, v2g); \
  70. b1 = __lasx_xvsadd_h(y_1, u2b); \
  71. r2 = __lasx_xvsadd_h(y_2, v2r); \
  72. g2 = __lasx_xvsadd_h(y_2, v2g); \
  73. b2 = __lasx_xvsadd_h(y_2, u2b); \
  74. DUP4_ARG1(__lasx_xvclip255_h, r1, g1, b1, r2, r1, g1, b1, r2); \
  75. DUP2_ARG1(__lasx_xvclip255_h, g2, b2, g2, b2); \
  76. #define YUV2RGB_RES \
  77. m_y1 = __lasx_xvldrepl_d(py_1, 0); \
  78. m_y2 = __lasx_xvldrepl_d(py_2, 0); \
  79. m_u = __lasx_xvldrepl_d(pu, 0); \
  80. m_v = __lasx_xvldrepl_d(pv, 0); \
  81. m_y1 = __lasx_xvilvl_d(m_y2, m_y1); \
  82. m_u = __lasx_xvilvl_b(m_u, m_u); \
  83. m_v = __lasx_xvilvl_b(m_v, m_v); \
  84. m_y1 = __lasx_vext2xv_hu_bu(m_y1); \
  85. m_u = __lasx_vext2xv_hu_bu(m_u); \
  86. m_v = __lasx_vext2xv_hu_bu(m_v); \
  87. m_y1 = __lasx_xvslli_h(m_y1, 3); \
  88. m_u = __lasx_xvslli_h(m_u, 3); \
  89. m_v = __lasx_xvslli_h(m_v, 3); \
  90. m_y1 = __lasx_xvsub_h(m_y1, y_offset); \
  91. m_u = __lasx_xvsub_h(m_u, u_offset); \
  92. m_v = __lasx_xvsub_h(m_v, v_offset); \
  93. y_1 = __lasx_xvmuh_h(m_y1, y_coeff); \
  94. u2g = __lasx_xvmuh_h(m_u, ug_coeff); \
  95. u2b = __lasx_xvmuh_h(m_u, ub_coeff); \
  96. v2r = __lasx_xvmuh_h(m_v, vr_coeff); \
  97. v2g = __lasx_xvmuh_h(m_v, vg_coeff); \
  98. r1 = __lasx_xvsadd_h(y_1, v2r); \
  99. v2g = __lasx_xvsadd_h(v2g, u2g); \
  100. g1 = __lasx_xvsadd_h(y_1, v2g); \
  101. b1 = __lasx_xvsadd_h(y_1, u2b); \
  102. r1 = __lasx_xvclip255_h(r1); \
  103. g1 = __lasx_xvclip255_h(g1); \
  104. b1 = __lasx_xvclip255_h(b1); \
  105. #define RGB_PACK(r, g, b, rgb_l, rgb_h) \
  106. { \
  107. __m256i rg; \
  108. rg = __lasx_xvpackev_b(g, r); \
  109. DUP2_ARG3(__lasx_xvshuf_b, b, rg, shuf2, b, rg, shuf3, rgb_l, rgb_h); \
  110. }
  111. #define RGB32_PACK(a, r, g, b, rgb_l, rgb_h) \
  112. { \
  113. __m256i ra, bg, tmp0, tmp1; \
  114. ra = __lasx_xvpackev_b(r, a); \
  115. bg = __lasx_xvpackev_b(b, g); \
  116. tmp0 = __lasx_xvilvl_h(bg, ra); \
  117. tmp1 = __lasx_xvilvh_h(bg, ra); \
  118. rgb_l = __lasx_xvpermi_q(tmp1, tmp0, 0x20); \
  119. rgb_h = __lasx_xvpermi_q(tmp1, tmp0, 0x31); \
  120. }
  121. #define RGB_STORE_RES(rgb_l, rgb_h, image_1, image_2) \
  122. { \
  123. __lasx_xvstelm_d(rgb_l, image_1, 0, 0); \
  124. __lasx_xvstelm_d(rgb_l, image_1, 8, 1); \
  125. __lasx_xvstelm_d(rgb_h, image_1, 16, 0); \
  126. __lasx_xvstelm_d(rgb_l, image_2, 0, 2); \
  127. __lasx_xvstelm_d(rgb_l, image_2, 8, 3); \
  128. __lasx_xvstelm_d(rgb_h, image_2, 16, 2); \
  129. }
  130. #define RGB_STORE(rgb_l, rgb_h, image) \
  131. { \
  132. __lasx_xvstelm_d(rgb_l, image, 0, 0); \
  133. __lasx_xvstelm_d(rgb_l, image, 8, 1); \
  134. __lasx_xvstelm_d(rgb_h, image, 16, 0); \
  135. __lasx_xvstelm_d(rgb_l, image, 24, 2); \
  136. __lasx_xvstelm_d(rgb_l, image, 32, 3); \
  137. __lasx_xvstelm_d(rgb_h, image, 40, 2); \
  138. }
  139. #define RGB32_STORE(rgb_l, rgb_h, image) \
  140. { \
  141. __lasx_xvst(rgb_l, image, 0); \
  142. __lasx_xvst(rgb_h, image, 32); \
  143. }
  144. #define RGB32_STORE_RES(rgb_l, rgb_h, image_1, image_2) \
  145. { \
  146. __lasx_xvst(rgb_l, image_1, 0); \
  147. __lasx_xvst(rgb_h, image_2, 0); \
  148. }
  149. #define YUV2RGBFUNC(func_name, dst_type, alpha) \
  150. int func_name(SwsContext *c, const uint8_t *src[], \
  151. int srcStride[], int srcSliceY, int srcSliceH, \
  152. uint8_t *dst[], int dstStride[]) \
  153. { \
  154. int x, y, h_size, vshift, res; \
  155. __m256i m_y1, m_y2, m_u, m_v; \
  156. __m256i y_1, y_2, u2g, v2g, u2b, v2r, rgb1_l, rgb1_h; \
  157. __m256i rgb2_l, rgb2_h, r1, g1, b1, r2, g2, b2; \
  158. __m256i shuf2 = {0x0504120302100100, 0x0A18090816070614, \
  159. 0x0504120302100100, 0x0A18090816070614}; \
  160. __m256i shuf3 = {0x1E0F0E1C0D0C1A0B, 0x0101010101010101, \
  161. 0x1E0F0E1C0D0C1A0B, 0x0101010101010101}; \
  162. YUV2RGB_LOAD_COE \
  163. y = (c->dstW + 7) & ~7; \
  164. h_size = y >> 4; \
  165. res = y & 15; \
  166. \
  167. vshift = c->srcFormat != AV_PIX_FMT_YUV422P; \
  168. for (y = 0; y < srcSliceH; y += 2) { \
  169. dst_type *image1 = (dst_type *)(dst[0] + (y + srcSliceY) * dstStride[0]);\
  170. dst_type *image2 = (dst_type *)(image1 + dstStride[0]);\
  171. const uint8_t *py_1 = src[0] + y * srcStride[0]; \
  172. const uint8_t *py_2 = py_1 + srcStride[0]; \
  173. const uint8_t *pu = src[1] + (y >> vshift) * srcStride[1]; \
  174. const uint8_t *pv = src[2] + (y >> vshift) * srcStride[2]; \
  175. for(x = 0; x < h_size; x++) { \
  176. #define YUV2RGBFUNC32(func_name, dst_type, alpha) \
  177. int func_name(SwsContext *c, const uint8_t *src[], \
  178. int srcStride[], int srcSliceY, int srcSliceH, \
  179. uint8_t *dst[], int dstStride[]) \
  180. { \
  181. int x, y, h_size, vshift, res; \
  182. __m256i m_y1, m_y2, m_u, m_v; \
  183. __m256i y_1, y_2, u2g, v2g, u2b, v2r, rgb1_l, rgb1_h; \
  184. __m256i rgb2_l, rgb2_h, r1, g1, b1, r2, g2, b2; \
  185. __m256i a = __lasx_xvldi(0xFF); \
  186. \
  187. YUV2RGB_LOAD_COE \
  188. y = (c->dstW + 7) & ~7; \
  189. h_size = y >> 4; \
  190. res = y & 15; \
  191. \
  192. vshift = c->srcFormat != AV_PIX_FMT_YUV422P; \
  193. for (y = 0; y < srcSliceH; y += 2) { \
  194. int yd = y + srcSliceY; \
  195. dst_type av_unused *r, *g, *b; \
  196. dst_type *image1 = (dst_type *)(dst[0] + (yd) * dstStride[0]); \
  197. dst_type *image2 = (dst_type *)(dst[0] + (yd + 1) * dstStride[0]); \
  198. const uint8_t *py_1 = src[0] + y * srcStride[0]; \
  199. const uint8_t *py_2 = py_1 + srcStride[0]; \
  200. const uint8_t *pu = src[1] + (y >> vshift) * srcStride[1]; \
  201. const uint8_t *pv = src[2] + (y >> vshift) * srcStride[2]; \
  202. for(x = 0; x < h_size; x++) { \
  203. #define DEALYUV2RGBREMAIN \
  204. py_1 += 16; \
  205. py_2 += 16; \
  206. pu += 8; \
  207. pv += 8; \
  208. image1 += 48; \
  209. image2 += 48; \
  210. } \
  211. if (res) { \
  212. #define DEALYUV2RGBREMAIN32 \
  213. py_1 += 16; \
  214. py_2 += 16; \
  215. pu += 8; \
  216. pv += 8; \
  217. image1 += 16; \
  218. image2 += 16; \
  219. } \
  220. if (res) { \
  221. #define END_FUNC() \
  222. } \
  223. } \
  224. return srcSliceH; \
  225. }
  226. YUV2RGBFUNC(yuv420_rgb24_lasx, uint8_t, 0)
  227. LOAD_YUV_16
  228. YUV2RGB
  229. RGB_PACK(r1, g1, b1, rgb1_l, rgb1_h);
  230. RGB_PACK(r2, g2, b2, rgb2_l, rgb2_h);
  231. RGB_STORE(rgb1_l, rgb1_h, image1);
  232. RGB_STORE(rgb2_l, rgb2_h, image2);
  233. DEALYUV2RGBREMAIN
  234. YUV2RGB_RES
  235. RGB_PACK(r1, g1, b1, rgb1_l, rgb1_h);
  236. RGB_STORE_RES(rgb1_l, rgb1_h, image1, image2);
  237. END_FUNC()
  238. YUV2RGBFUNC(yuv420_bgr24_lasx, uint8_t, 0)
  239. LOAD_YUV_16
  240. YUV2RGB
  241. RGB_PACK(b1, g1, r1, rgb1_l, rgb1_h);
  242. RGB_PACK(b2, g2, r2, rgb2_l, rgb2_h);
  243. RGB_STORE(rgb1_l, rgb1_h, image1);
  244. RGB_STORE(rgb2_l, rgb2_h, image2);
  245. DEALYUV2RGBREMAIN
  246. YUV2RGB_RES
  247. RGB_PACK(b1, g1, r1, rgb1_l, rgb1_h);
  248. RGB_STORE_RES(rgb1_l, rgb1_h, image1, image2);
  249. END_FUNC()
  250. YUV2RGBFUNC32(yuv420_rgba32_lasx, uint32_t, 0)
  251. LOAD_YUV_16
  252. YUV2RGB
  253. RGB32_PACK(r1, g1, b1, a, rgb1_l, rgb1_h);
  254. RGB32_PACK(r2, g2, b2, a, rgb2_l, rgb2_h);
  255. RGB32_STORE(rgb1_l, rgb1_h, image1);
  256. RGB32_STORE(rgb2_l, rgb2_h, image2);
  257. DEALYUV2RGBREMAIN32
  258. YUV2RGB_RES
  259. RGB32_PACK(r1, g1, b1, a, rgb1_l, rgb1_h);
  260. RGB32_STORE_RES(rgb1_l, rgb1_h, image1, image2);
  261. END_FUNC()
  262. YUV2RGBFUNC32(yuv420_bgra32_lasx, uint32_t, 0)
  263. LOAD_YUV_16
  264. YUV2RGB
  265. RGB32_PACK(b1, g1, r1, a, rgb1_l, rgb1_h);
  266. RGB32_PACK(b2, g2, r2, a, rgb2_l, rgb2_h);
  267. RGB32_STORE(rgb1_l, rgb1_h, image1);
  268. RGB32_STORE(rgb2_l, rgb2_h, image2);
  269. DEALYUV2RGBREMAIN32
  270. YUV2RGB_RES
  271. RGB32_PACK(b1, g1, r1, a, rgb1_l, rgb1_h);
  272. RGB32_STORE_RES(rgb1_l, rgb1_h, image1, image2);
  273. END_FUNC()
  274. YUV2RGBFUNC32(yuv420_argb32_lasx, uint32_t, 0)
  275. LOAD_YUV_16
  276. YUV2RGB
  277. RGB32_PACK(a, r1, g1, b1, rgb1_l, rgb1_h);
  278. RGB32_PACK(a, r2, g2, b2, rgb2_l, rgb2_h);
  279. RGB32_STORE(rgb1_l, rgb1_h, image1);
  280. RGB32_STORE(rgb2_l, rgb2_h, image2);
  281. DEALYUV2RGBREMAIN32
  282. YUV2RGB_RES
  283. RGB32_PACK(a, r1, g1, b1, rgb1_l, rgb1_h);
  284. RGB32_STORE_RES(rgb1_l, rgb1_h, image1, image2);
  285. END_FUNC()
  286. YUV2RGBFUNC32(yuv420_abgr32_lasx, uint32_t, 0)
  287. LOAD_YUV_16
  288. YUV2RGB
  289. RGB32_PACK(a, b1, g1, r1, rgb1_l, rgb1_h);
  290. RGB32_PACK(a, b2, g2, r2, rgb2_l, rgb2_h);
  291. RGB32_STORE(rgb1_l, rgb1_h, image1);
  292. RGB32_STORE(rgb2_l, rgb2_h, image2);
  293. DEALYUV2RGBREMAIN32
  294. YUV2RGB_RES
  295. RGB32_PACK(a, b1, g1, r1, rgb1_l, rgb1_h);
  296. RGB32_STORE_RES(rgb1_l, rgb1_h, image1, image2);
  297. END_FUNC()