yuv2rgb_lsx.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright (C) 2023 Loongson Technology Co. Ltd.
  3. * Contributed by Bo Jin(jinbo@loongson.cn)
  4. * All rights reserved.
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "swscale_loongarch.h"
  23. #include "libavutil/loongarch/loongson_intrinsics.h"
  24. #define YUV2RGB_LOAD_COE \
  25. /* Load x_offset */ \
  26. __m128i y_offset = __lsx_vreplgr2vr_d(c->yOffset); \
  27. __m128i u_offset = __lsx_vreplgr2vr_d(c->uOffset); \
  28. __m128i v_offset = __lsx_vreplgr2vr_d(c->vOffset); \
  29. /* Load x_coeff */ \
  30. __m128i ug_coeff = __lsx_vreplgr2vr_d(c->ugCoeff); \
  31. __m128i vg_coeff = __lsx_vreplgr2vr_d(c->vgCoeff); \
  32. __m128i y_coeff = __lsx_vreplgr2vr_d(c->yCoeff); \
  33. __m128i ub_coeff = __lsx_vreplgr2vr_d(c->ubCoeff); \
  34. __m128i vr_coeff = __lsx_vreplgr2vr_d(c->vrCoeff); \
  35. #define LOAD_YUV_16 \
  36. m_y1 = __lsx_vld(py_1, 0); \
  37. m_y2 = __lsx_vld(py_2, 0); \
  38. m_u = __lsx_vldrepl_d(pu, 0); \
  39. m_v = __lsx_vldrepl_d(pv, 0); \
  40. DUP2_ARG2(__lsx_vilvl_b, m_u, m_u, m_v, m_v, m_u, m_v); \
  41. DUP2_ARG2(__lsx_vilvh_b, zero, m_u, zero, m_v, m_u_h, m_v_h); \
  42. DUP2_ARG2(__lsx_vilvl_b, zero, m_u, zero, m_v, m_u, m_v); \
  43. DUP2_ARG2(__lsx_vilvh_b, zero, m_y1, zero, m_y2, m_y1_h, m_y2_h); \
  44. DUP2_ARG2(__lsx_vilvl_b, zero, m_y1, zero, m_y2, m_y1, m_y2); \
  45. /* YUV2RGB method
  46. * The conversion method is as follows:
  47. * R = Y' * y_coeff + V' * vr_coeff
  48. * G = Y' * y_coeff + V' * vg_coeff + U' * ug_coeff
  49. * B = Y' * y_coeff + U' * ub_coeff
  50. *
  51. * where X' = X * 8 - x_offset
  52. *
  53. */
  54. #define YUV2RGB(y1, y2, u, v, r1, g1, b1, r2, g2, b2) \
  55. { \
  56. y1 = __lsx_vslli_h(y1, 3); \
  57. y2 = __lsx_vslli_h(y2, 3); \
  58. u = __lsx_vslli_h(u, 3); \
  59. v = __lsx_vslli_h(v, 3); \
  60. y1 = __lsx_vsub_h(y1, y_offset); \
  61. y2 = __lsx_vsub_h(y2, y_offset); \
  62. u = __lsx_vsub_h(u, u_offset); \
  63. v = __lsx_vsub_h(v, v_offset); \
  64. y_1 = __lsx_vmuh_h(y1, y_coeff); \
  65. y_2 = __lsx_vmuh_h(y2, y_coeff); \
  66. u2g = __lsx_vmuh_h(u, ug_coeff); \
  67. u2b = __lsx_vmuh_h(u, ub_coeff); \
  68. v2r = __lsx_vmuh_h(v, vr_coeff); \
  69. v2g = __lsx_vmuh_h(v, vg_coeff); \
  70. r1 = __lsx_vsadd_h(y_1, v2r); \
  71. v2g = __lsx_vsadd_h(v2g, u2g); \
  72. g1 = __lsx_vsadd_h(y_1, v2g); \
  73. b1 = __lsx_vsadd_h(y_1, u2b); \
  74. r2 = __lsx_vsadd_h(y_2, v2r); \
  75. g2 = __lsx_vsadd_h(y_2, v2g); \
  76. b2 = __lsx_vsadd_h(y_2, u2b); \
  77. DUP4_ARG1(__lsx_vclip255_h, r1, g1, b1, r2, r1, g1, b1, r2); \
  78. DUP2_ARG1(__lsx_vclip255_h, g2, b2, g2, b2); \
  79. }
  80. #define RGB_PACK(r, g, b, rgb_l, rgb_h) \
  81. { \
  82. __m128i rg; \
  83. rg = __lsx_vpackev_b(g, r); \
  84. DUP2_ARG3(__lsx_vshuf_b, b, rg, shuf2, b, rg, shuf3, rgb_l, rgb_h); \
  85. }
  86. #define RGB32_PACK(a, r, g, b, rgb_l, rgb_h) \
  87. { \
  88. __m128i ra, bg; \
  89. ra = __lsx_vpackev_b(r, a); \
  90. bg = __lsx_vpackev_b(b, g); \
  91. rgb_l = __lsx_vilvl_h(bg, ra); \
  92. rgb_h = __lsx_vilvh_h(bg, ra); \
  93. }
  94. #define RGB_STORE(rgb_l, rgb_h, image) \
  95. { \
  96. __lsx_vstelm_d(rgb_l, image, 0, 0); \
  97. __lsx_vstelm_d(rgb_l, image, 8, 1); \
  98. __lsx_vstelm_d(rgb_h, image, 16, 0); \
  99. }
  100. #define RGB32_STORE(rgb_l, rgb_h, image) \
  101. { \
  102. __lsx_vst(rgb_l, image, 0); \
  103. __lsx_vst(rgb_h, image, 16); \
  104. }
  105. #define YUV2RGBFUNC(func_name, dst_type, alpha) \
  106. int func_name(SwsInternal *c, const uint8_t *const src[], \
  107. const int srcStride[], int srcSliceY, int srcSliceH, \
  108. uint8_t *const dst[], const int dstStride[]) \
  109. { \
  110. int x, y, h_size, vshift, res; \
  111. __m128i m_y1, m_y2, m_u, m_v; \
  112. __m128i m_y1_h, m_y2_h, m_u_h, m_v_h; \
  113. __m128i y_1, y_2, u2g, v2g, u2b, v2r, rgb1_l, rgb1_h; \
  114. __m128i rgb2_l, rgb2_h, r1, g1, b1, r2, g2, b2; \
  115. __m128i shuf2 = {0x0504120302100100, 0x0A18090816070614}; \
  116. __m128i shuf3 = {0x1E0F0E1C0D0C1A0B, 0x0101010101010101}; \
  117. __m128i zero = __lsx_vldi(0); \
  118. \
  119. YUV2RGB_LOAD_COE \
  120. \
  121. h_size = c->dstW >> 4; \
  122. res = (c->dstW & 15) >> 1; \
  123. vshift = c->srcFormat != AV_PIX_FMT_YUV422P; \
  124. for (y = 0; y < srcSliceH; y += 2) { \
  125. dst_type av_unused *r, *g, *b; \
  126. dst_type *image1 = (dst_type *)(dst[0] + (y + srcSliceY) * dstStride[0]);\
  127. dst_type *image2 = (dst_type *)(image1 + dstStride[0]);\
  128. const uint8_t *py_1 = src[0] + y * srcStride[0]; \
  129. const uint8_t *py_2 = py_1 + srcStride[0]; \
  130. const uint8_t *pu = src[1] + (y >> vshift) * srcStride[1]; \
  131. const uint8_t *pv = src[2] + (y >> vshift) * srcStride[2]; \
  132. for(x = 0; x < h_size; x++) { \
  133. #define YUV2RGBFUNC32(func_name, dst_type, alpha) \
  134. int func_name(SwsInternal *c, const uint8_t *const src[], \
  135. const int srcStride[], int srcSliceY, int srcSliceH, \
  136. uint8_t *const dst[], const int dstStride[]) \
  137. { \
  138. int x, y, h_size, vshift, res; \
  139. __m128i m_y1, m_y2, m_u, m_v; \
  140. __m128i m_y1_h, m_y2_h, m_u_h, m_v_h; \
  141. __m128i y_1, y_2, u2g, v2g, u2b, v2r, rgb1_l, rgb1_h; \
  142. __m128i rgb2_l, rgb2_h, r1, g1, b1, r2, g2, b2; \
  143. __m128i a = __lsx_vldi(0xFF); \
  144. __m128i zero = __lsx_vldi(0); \
  145. \
  146. YUV2RGB_LOAD_COE \
  147. \
  148. h_size = c->dstW >> 4; \
  149. res = (c->dstW & 15) >> 1; \
  150. vshift = c->srcFormat != AV_PIX_FMT_YUV422P; \
  151. for (y = 0; y < srcSliceH; y += 2) { \
  152. int yd = y + srcSliceY; \
  153. dst_type av_unused *r, *g, *b; \
  154. dst_type *image1 = (dst_type *)(dst[0] + (yd) * dstStride[0]); \
  155. dst_type *image2 = (dst_type *)(dst[0] + (yd + 1) * dstStride[0]); \
  156. const uint8_t *py_1 = src[0] + y * srcStride[0]; \
  157. const uint8_t *py_2 = py_1 + srcStride[0]; \
  158. const uint8_t *pu = src[1] + (y >> vshift) * srcStride[1]; \
  159. const uint8_t *pv = src[2] + (y >> vshift) * srcStride[2]; \
  160. for(x = 0; x < h_size; x++) { \
  161. #define DEALYUV2RGBREMAIN \
  162. py_1 += 16; \
  163. py_2 += 16; \
  164. pu += 8; \
  165. pv += 8; \
  166. image1 += 48; \
  167. image2 += 48; \
  168. } \
  169. for (x = 0; x < res; x++) { \
  170. int av_unused U, V, Y; \
  171. U = pu[0]; \
  172. V = pv[0]; \
  173. r = (void *)c->table_rV[V+YUVRGB_TABLE_HEADROOM]; \
  174. g = (void *)(c->table_gU[U+YUVRGB_TABLE_HEADROOM] \
  175. + c->table_gV[V+YUVRGB_TABLE_HEADROOM]); \
  176. b = (void *)c->table_bU[U+YUVRGB_TABLE_HEADROOM];
  177. #define DEALYUV2RGBREMAIN32 \
  178. py_1 += 16; \
  179. py_2 += 16; \
  180. pu += 8; \
  181. pv += 8; \
  182. image1 += 16; \
  183. image2 += 16; \
  184. } \
  185. for (x = 0; x < res; x++) { \
  186. int av_unused U, V, Y; \
  187. U = pu[0]; \
  188. V = pv[0]; \
  189. r = (void *)c->table_rV[V+YUVRGB_TABLE_HEADROOM]; \
  190. g = (void *)(c->table_gU[U+YUVRGB_TABLE_HEADROOM] \
  191. + c->table_gV[V+YUVRGB_TABLE_HEADROOM]); \
  192. b = (void *)c->table_bU[U+YUVRGB_TABLE_HEADROOM]; \
  193. #define PUTRGB24(dst, src) \
  194. Y = src[0]; \
  195. dst[0] = r[Y]; \
  196. dst[1] = g[Y]; \
  197. dst[2] = b[Y]; \
  198. Y = src[1]; \
  199. dst[3] = r[Y]; \
  200. dst[4] = g[Y]; \
  201. dst[5] = b[Y];
  202. #define PUTBGR24(dst, src) \
  203. Y = src[0]; \
  204. dst[0] = b[Y]; \
  205. dst[1] = g[Y]; \
  206. dst[2] = r[Y]; \
  207. Y = src[1]; \
  208. dst[3] = b[Y]; \
  209. dst[4] = g[Y]; \
  210. dst[5] = r[Y];
  211. #define PUTRGB(dst, src) \
  212. Y = src[0]; \
  213. dst[0] = r[Y] + g[Y] + b[Y]; \
  214. Y = src[1]; \
  215. dst[1] = r[Y] + g[Y] + b[Y]; \
  216. #define ENDRES \
  217. pu += 1; \
  218. pv += 1; \
  219. py_1 += 2; \
  220. py_2 += 2; \
  221. image1 += 6; \
  222. image2 += 6; \
  223. #define ENDRES32 \
  224. pu += 1; \
  225. pv += 1; \
  226. py_1 += 2; \
  227. py_2 += 2; \
  228. image1 += 2; \
  229. image2 += 2; \
  230. #define END_FUNC() \
  231. } \
  232. } \
  233. return srcSliceH; \
  234. }
  235. YUV2RGBFUNC(yuv420_rgb24_lsx, uint8_t, 0)
  236. LOAD_YUV_16
  237. YUV2RGB(m_y1, m_y2, m_u, m_v, r1, g1, b1, r2, g2, b2);
  238. RGB_PACK(r1, g1, b1, rgb1_l, rgb1_h);
  239. RGB_PACK(r2, g2, b2, rgb2_l, rgb2_h);
  240. RGB_STORE(rgb1_l, rgb1_h, image1);
  241. RGB_STORE(rgb2_l, rgb2_h, image2);
  242. YUV2RGB(m_y1_h, m_y2_h, m_u_h, m_v_h, r1, g1, b1, r2, g2, b2);
  243. RGB_PACK(r1, g1, b1, rgb1_l, rgb1_h);
  244. RGB_PACK(r2, g2, b2, rgb2_l, rgb2_h);
  245. RGB_STORE(rgb1_l, rgb1_h, image1 + 24);
  246. RGB_STORE(rgb2_l, rgb2_h, image2 + 24);
  247. DEALYUV2RGBREMAIN
  248. PUTRGB24(image1, py_1);
  249. PUTRGB24(image2, py_2);
  250. ENDRES
  251. END_FUNC()
  252. YUV2RGBFUNC(yuv420_bgr24_lsx, uint8_t, 0)
  253. LOAD_YUV_16
  254. YUV2RGB(m_y1, m_y2, m_u, m_v, r1, g1, b1, r2, g2, b2);
  255. RGB_PACK(b1, g1, r1, rgb1_l, rgb1_h);
  256. RGB_PACK(b2, g2, r2, rgb2_l, rgb2_h);
  257. RGB_STORE(rgb1_l, rgb1_h, image1);
  258. RGB_STORE(rgb2_l, rgb2_h, image2);
  259. YUV2RGB(m_y1_h, m_y2_h, m_u_h, m_v_h, r1, g1, b1, r2, g2, b2);
  260. RGB_PACK(b1, g1, r1, rgb1_l, rgb1_h);
  261. RGB_PACK(b2, g2, r2, rgb2_l, rgb2_h);
  262. RGB_STORE(rgb1_l, rgb1_h, image1 + 24);
  263. RGB_STORE(rgb2_l, rgb2_h, image2 + 24);
  264. DEALYUV2RGBREMAIN
  265. PUTBGR24(image1, py_1);
  266. PUTBGR24(image2, py_2);
  267. ENDRES
  268. END_FUNC()
  269. YUV2RGBFUNC32(yuv420_rgba32_lsx, uint32_t, 0)
  270. LOAD_YUV_16
  271. YUV2RGB(m_y1, m_y2, m_u, m_v, r1, g1, b1, r2, g2, b2);
  272. RGB32_PACK(r1, g1, b1, a, rgb1_l, rgb1_h);
  273. RGB32_PACK(r2, g2, b2, a, rgb2_l, rgb2_h);
  274. RGB32_STORE(rgb1_l, rgb1_h, image1);
  275. RGB32_STORE(rgb2_l, rgb2_h, image2);
  276. YUV2RGB(m_y1_h, m_y2_h, m_u_h, m_v_h, r1, g1, b1, r2, g2, b2);
  277. RGB32_PACK(r1, g1, b1, a, rgb1_l, rgb1_h);
  278. RGB32_PACK(r2, g2, b2, a, rgb2_l, rgb2_h);
  279. RGB32_STORE(rgb1_l, rgb1_h, image1 + 8);
  280. RGB32_STORE(rgb2_l, rgb2_h, image2 + 8);
  281. DEALYUV2RGBREMAIN32
  282. PUTRGB(image1, py_1);
  283. PUTRGB(image2, py_2);
  284. ENDRES32
  285. END_FUNC()
  286. YUV2RGBFUNC32(yuv420_bgra32_lsx, uint32_t, 0)
  287. LOAD_YUV_16
  288. YUV2RGB(m_y1, m_y2, m_u, m_v, r1, g1, b1, r2, g2, b2);
  289. RGB32_PACK(b1, g1, r1, a, rgb1_l, rgb1_h);
  290. RGB32_PACK(b2, g2, r2, a, rgb2_l, rgb2_h);
  291. RGB32_STORE(rgb1_l, rgb1_h, image1);
  292. RGB32_STORE(rgb2_l, rgb2_h, image2);
  293. YUV2RGB(m_y1_h, m_y2_h, m_u_h, m_v_h, r1, g1, b1, r2, g2, b2);
  294. RGB32_PACK(b1, g1, r1, a, rgb1_l, rgb1_h);
  295. RGB32_PACK(b2, g2, r2, a, rgb2_l, rgb2_h);
  296. RGB32_STORE(rgb1_l, rgb1_h, image1 + 8);
  297. RGB32_STORE(rgb2_l, rgb2_h, image2 + 8);
  298. DEALYUV2RGBREMAIN32
  299. PUTRGB(image1, py_1);
  300. PUTRGB(image2, py_2);
  301. ENDRES32
  302. END_FUNC()
  303. YUV2RGBFUNC32(yuv420_argb32_lsx, uint32_t, 0)
  304. LOAD_YUV_16
  305. YUV2RGB(m_y1, m_y2, m_u, m_v, r1, g1, b1, r2, g2, b2);
  306. RGB32_PACK(a, r1, g1, b1, rgb1_l, rgb1_h);
  307. RGB32_PACK(a, r2, g2, b2, rgb2_l, rgb2_h);
  308. RGB32_STORE(rgb1_l, rgb1_h, image1);
  309. RGB32_STORE(rgb2_l, rgb2_h, image2);
  310. YUV2RGB(m_y1_h, m_y2_h, m_u_h, m_v_h, r1, g1, b1, r2, g2, b2);
  311. RGB32_PACK(a, r1, g1, b1, rgb1_l, rgb1_h);
  312. RGB32_PACK(a, r2, g2, b2, rgb2_l, rgb2_h);
  313. RGB32_STORE(rgb1_l, rgb1_h, image1 + 8);
  314. RGB32_STORE(rgb2_l, rgb2_h, image2 + 8);
  315. DEALYUV2RGBREMAIN32
  316. PUTRGB(image1, py_1);
  317. PUTRGB(image2, py_2);
  318. ENDRES32
  319. END_FUNC()
  320. YUV2RGBFUNC32(yuv420_abgr32_lsx, uint32_t, 0)
  321. LOAD_YUV_16
  322. YUV2RGB(m_y1, m_y2, m_u, m_v, r1, g1, b1, r2, g2, b2);
  323. RGB32_PACK(a, b1, g1, r1, rgb1_l, rgb1_h);
  324. RGB32_PACK(a, b2, g2, r2, rgb2_l, rgb2_h);
  325. RGB32_STORE(rgb1_l, rgb1_h, image1);
  326. RGB32_STORE(rgb2_l, rgb2_h, image2);
  327. YUV2RGB(m_y1_h, m_y2_h, m_u_h, m_v_h, r1, g1, b1, r2, g2, b2);
  328. RGB32_PACK(a, b1, g1, r1, rgb1_l, rgb1_h);
  329. RGB32_PACK(a, b2, g2, r2, rgb2_l, rgb2_h);
  330. RGB32_STORE(rgb1_l, rgb1_h, image1 + 8);
  331. RGB32_STORE(rgb2_l, rgb2_h, image2 + 8);
  332. DEALYUV2RGBREMAIN32
  333. PUTRGB(image1, py_1);
  334. PUTRGB(image2, py_2);
  335. ENDRES32
  336. END_FUNC()