input_lasx.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. void planar_rgb_to_uv_lasx(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *src[4],
  24. int width, int32_t *rgb2yuv, void *opq)
  25. {
  26. int i;
  27. uint16_t *dstU = (uint16_t *)_dstU;
  28. uint16_t *dstV = (uint16_t *)_dstV;
  29. int set = 0x4001 << (RGB2YUV_SHIFT - 7);
  30. int len = width - 15;
  31. int32_t tem_ru = rgb2yuv[RU_IDX], tem_gu = rgb2yuv[GU_IDX];
  32. int32_t tem_bu = rgb2yuv[BU_IDX], tem_rv = rgb2yuv[RV_IDX];
  33. int32_t tem_gv = rgb2yuv[GV_IDX], tem_bv = rgb2yuv[BV_IDX];
  34. int shift = RGB2YUV_SHIFT - 6;
  35. const uint8_t *src0 = src[0], *src1 = src[1], *src2 = src[2];
  36. __m256i ru, gu, bu, rv, gv, bv;
  37. __m256i mask = {0x0D0C090805040100, 0x1D1C191815141110,
  38. 0x0D0C090805040100, 0x1D1C191815141110};
  39. __m256i temp = __lasx_xvreplgr2vr_w(set);
  40. __m256i sra = __lasx_xvreplgr2vr_w(shift);
  41. ru = __lasx_xvreplgr2vr_w(tem_ru);
  42. gu = __lasx_xvreplgr2vr_w(tem_gu);
  43. bu = __lasx_xvreplgr2vr_w(tem_bu);
  44. rv = __lasx_xvreplgr2vr_w(tem_rv);
  45. gv = __lasx_xvreplgr2vr_w(tem_gv);
  46. bv = __lasx_xvreplgr2vr_w(tem_bv);
  47. for (i = 0; i < len; i += 16) {
  48. __m256i _g, _b, _r;
  49. __m256i g_l, g_h, b_l, b_h, r_l, r_h;
  50. __m256i v_l, v_h, u_l, u_h, u_lh, v_lh;
  51. _g = __lasx_xvldx(src0, i);
  52. _b = __lasx_xvldx(src1, i);
  53. _r = __lasx_xvldx(src2, i);
  54. g_l = __lasx_vext2xv_wu_bu(_g);
  55. b_l = __lasx_vext2xv_wu_bu(_b);
  56. r_l = __lasx_vext2xv_wu_bu(_r);
  57. _g = __lasx_xvpermi_d(_g, 0x01);
  58. _b = __lasx_xvpermi_d(_b, 0x01);
  59. _r = __lasx_xvpermi_d(_r, 0x01);
  60. g_h = __lasx_vext2xv_wu_bu(_g);
  61. b_h = __lasx_vext2xv_wu_bu(_b);
  62. r_h = __lasx_vext2xv_wu_bu(_r);
  63. u_l = __lasx_xvmadd_w(temp, ru, r_l);
  64. u_h = __lasx_xvmadd_w(temp, ru, r_h);
  65. v_l = __lasx_xvmadd_w(temp, rv, r_l);
  66. v_h = __lasx_xvmadd_w(temp, rv, r_h);
  67. u_l = __lasx_xvmadd_w(u_l, gu, g_l);
  68. u_l = __lasx_xvmadd_w(u_l, bu, b_l);
  69. u_h = __lasx_xvmadd_w(u_h, gu, g_h);
  70. u_h = __lasx_xvmadd_w(u_h, bu, b_h);
  71. v_l = __lasx_xvmadd_w(v_l, gv, g_l);
  72. v_l = __lasx_xvmadd_w(v_l, bv, b_l);
  73. v_h = __lasx_xvmadd_w(v_h, gv, g_h);
  74. v_h = __lasx_xvmadd_w(v_h, bv, b_h);
  75. u_l = __lasx_xvsra_w(u_l, sra);
  76. u_h = __lasx_xvsra_w(u_h, sra);
  77. v_l = __lasx_xvsra_w(v_l, sra);
  78. v_h = __lasx_xvsra_w(v_h, sra);
  79. u_lh = __lasx_xvshuf_b(u_h, u_l, mask);
  80. v_lh = __lasx_xvshuf_b(v_h, v_l, mask);
  81. u_lh = __lasx_xvpermi_d(u_lh, 0xD8);
  82. v_lh = __lasx_xvpermi_d(v_lh, 0xD8);
  83. __lasx_xvst(u_lh, (dstU + i), 0);
  84. __lasx_xvst(v_lh, (dstV + i), 0);
  85. }
  86. if (width - i >= 8) {
  87. __m256i _g, _b, _r;
  88. __m256i g_l, b_l, r_l;
  89. __m256i v_l, u_l, u, v;
  90. _g = __lasx_xvldrepl_d((src0 + i), 0);
  91. _b = __lasx_xvldrepl_d((src1 + i), 0);
  92. _r = __lasx_xvldrepl_d((src2 + i), 0);
  93. g_l = __lasx_vext2xv_wu_bu(_g);
  94. b_l = __lasx_vext2xv_wu_bu(_b);
  95. r_l = __lasx_vext2xv_wu_bu(_r);
  96. u_l = __lasx_xvmadd_w(temp, ru, r_l);
  97. v_l = __lasx_xvmadd_w(temp, rv, r_l);
  98. u_l = __lasx_xvmadd_w(u_l, gu, g_l);
  99. u_l = __lasx_xvmadd_w(u_l, bu, b_l);
  100. v_l = __lasx_xvmadd_w(v_l, gv, g_l);
  101. v_l = __lasx_xvmadd_w(v_l, bv, b_l);
  102. u_l = __lasx_xvsra_w(u_l, sra);
  103. v_l = __lasx_xvsra_w(v_l, sra);
  104. u = __lasx_xvshuf_b(u_l, u_l, mask);
  105. v = __lasx_xvshuf_b(v_l, v_l, mask);
  106. __lasx_xvstelm_d(u, (dstU + i), 0, 0);
  107. __lasx_xvstelm_d(u, (dstU + i), 8, 2);
  108. __lasx_xvstelm_d(v, (dstV + i), 0, 0);
  109. __lasx_xvstelm_d(v, (dstV + i), 8, 2);
  110. i += 8;
  111. }
  112. for (; i < width; i++) {
  113. int g = src[0][i];
  114. int b = src[1][i];
  115. int r = src[2][i];
  116. dstU[i] = (tem_ru * r + tem_gu * g + tem_bu * b + set) >> shift;
  117. dstV[i] = (tem_rv * r + tem_gv * g + tem_bv * b + set) >> shift;
  118. }
  119. }
  120. void planar_rgb_to_y_lasx(uint8_t *_dst, const uint8_t *src[4], int width,
  121. int32_t *rgb2yuv, void *opq)
  122. {
  123. int i;
  124. int shift = (RGB2YUV_SHIFT - 6);
  125. int set = 0x801 << (RGB2YUV_SHIFT - 7);
  126. int len = width - 15;
  127. uint16_t *dst = (uint16_t *)_dst;
  128. int32_t tem_ry = rgb2yuv[RY_IDX], tem_gy = rgb2yuv[GY_IDX];
  129. int32_t tem_by = rgb2yuv[BY_IDX];
  130. const uint8_t *src0 = src[0], *src1 = src[1], *src2 = src[2];
  131. __m256i mask = {0x0D0C090805040100, 0x1D1C191815141110,
  132. 0x0D0C090805040100, 0x1D1C191815141110};
  133. __m256i temp = __lasx_xvreplgr2vr_w(set);
  134. __m256i sra = __lasx_xvreplgr2vr_w(shift);
  135. __m256i ry = __lasx_xvreplgr2vr_w(tem_ry);
  136. __m256i gy = __lasx_xvreplgr2vr_w(tem_gy);
  137. __m256i by = __lasx_xvreplgr2vr_w(tem_by);
  138. for (i = 0; i < len; i += 16) {
  139. __m256i _g, _b, _r;
  140. __m256i g_l, g_h, b_l, b_h, r_l, r_h;
  141. __m256i y_l, y_h, y_lh;
  142. _g = __lasx_xvldx(src0, i);
  143. _b = __lasx_xvldx(src1, i);
  144. _r = __lasx_xvldx(src2, i);
  145. g_l = __lasx_vext2xv_wu_bu(_g);
  146. b_l = __lasx_vext2xv_wu_bu(_b);
  147. r_l = __lasx_vext2xv_wu_bu(_r);
  148. _g = __lasx_xvpermi_d(_g, 0x01);
  149. _b = __lasx_xvpermi_d(_b, 0x01);
  150. _r = __lasx_xvpermi_d(_r, 0x01);
  151. g_h = __lasx_vext2xv_wu_bu(_g);
  152. b_h = __lasx_vext2xv_wu_bu(_b);
  153. r_h = __lasx_vext2xv_wu_bu(_r);
  154. y_l = __lasx_xvmadd_w(temp, ry, r_l);
  155. y_h = __lasx_xvmadd_w(temp, ry, r_h);
  156. y_l = __lasx_xvmadd_w(y_l, gy, g_l);
  157. y_l = __lasx_xvmadd_w(y_l, by, b_l);
  158. y_h = __lasx_xvmadd_w(y_h, gy, g_h);
  159. y_h = __lasx_xvmadd_w(y_h, by, b_h);
  160. y_l = __lasx_xvsra_w(y_l, sra);
  161. y_h = __lasx_xvsra_w(y_h, sra);
  162. y_lh = __lasx_xvshuf_b(y_h, y_l, mask);
  163. y_lh = __lasx_xvpermi_d(y_lh, 0xD8);
  164. __lasx_xvst(y_lh, (dst + i), 0);
  165. }
  166. if (width - i >= 8) {
  167. __m256i _g, _b, _r;
  168. __m256i g_l, b_l, r_l;
  169. __m256i y_l, y;
  170. _g = __lasx_xvldrepl_d((src0 + i), 0);
  171. _b = __lasx_xvldrepl_d((src1 + i), 0);
  172. _r = __lasx_xvldrepl_d((src2 + i), 0);
  173. g_l = __lasx_vext2xv_wu_bu(_g);
  174. b_l = __lasx_vext2xv_wu_bu(_b);
  175. r_l = __lasx_vext2xv_wu_bu(_r);
  176. y_l = __lasx_xvmadd_w(temp, ry, r_l);
  177. y_l = __lasx_xvmadd_w(y_l, gy, g_l);
  178. y_l = __lasx_xvmadd_w(y_l, by, b_l);
  179. y_l = __lasx_xvsra_w(y_l, sra);
  180. y = __lasx_xvshuf_b(y_l, y_l, mask);
  181. __lasx_xvstelm_d(y, (dst + i), 0, 0);
  182. __lasx_xvstelm_d(y, (dst + i), 8, 2);
  183. i += 8;
  184. }
  185. for (; i < width; i++) {
  186. int g = src[0][i];
  187. int b = src[1][i];
  188. int r = src[2][i];
  189. dst[i] = (tem_ry * r + tem_gy * g + tem_by * b + set) >> shift;
  190. }
  191. }