swscale_altivec.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * AltiVec-enhanced yuv2yuvX
  3. *
  4. * Copyright (C) 2004 Romain Dolbeau <romain@dolbeau.org>
  5. * based on the equivalent C code in swscale.c
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <inttypes.h>
  24. #include "config.h"
  25. #include "libswscale/swscale.h"
  26. #include "libswscale/swscale_internal.h"
  27. #include "libavutil/attributes.h"
  28. #include "libavutil/cpu.h"
  29. #include "yuv2rgb_altivec.h"
  30. #include "libavutil/ppc/util_altivec.h"
  31. #if HAVE_ALTIVEC
  32. #if HAVE_BIGENDIAN
  33. #define vzero vec_splat_s32(0)
  34. #define GET_LS(a,b,c,s) {\
  35. vector signed short l2 = vec_ld(((b) << 1) + 16, s);\
  36. ls = vec_perm(a, l2, c);\
  37. a = l2;\
  38. }
  39. #define yuv2planeX_8(d1, d2, l1, src, x, perm, filter) do {\
  40. vector signed short ls;\
  41. vector signed int vf1, vf2, i1, i2;\
  42. GET_LS(l1, x, perm, src);\
  43. i1 = vec_mule(filter, ls);\
  44. i2 = vec_mulo(filter, ls);\
  45. vf1 = vec_mergeh(i1, i2);\
  46. vf2 = vec_mergel(i1, i2);\
  47. d1 = vec_add(d1, vf1);\
  48. d2 = vec_add(d2, vf2);\
  49. } while (0)
  50. #define LOAD_FILTER(vf,f) {\
  51. vector unsigned char perm0 = vec_lvsl(joffset, f);\
  52. vf = vec_ld(joffset, f);\
  53. vf = vec_perm(vf, vf, perm0);\
  54. }
  55. #define LOAD_L1(ll1,s,p){\
  56. p = vec_lvsl(xoffset, s);\
  57. ll1 = vec_ld(xoffset, s);\
  58. }
  59. // The 3 above is 2 (filterSize == 4) + 1 (sizeof(short) == 2).
  60. // The neat trick: We only care for half the elements,
  61. // high or low depending on (i<<3)%16 (it's 0 or 8 here),
  62. // and we're going to use vec_mule, so we choose
  63. // carefully how to "unpack" the elements into the even slots.
  64. #define GET_VF4(a, vf, f) {\
  65. vf = vec_ld(a<< 3, f);\
  66. if ((a << 3) % 16)\
  67. vf = vec_mergel(vf, (vector signed short)vzero);\
  68. else\
  69. vf = vec_mergeh(vf, (vector signed short)vzero);\
  70. }
  71. #define FIRST_LOAD(sv, pos, s, per) {\
  72. sv = vec_ld(pos, s);\
  73. per = vec_lvsl(pos, s);\
  74. }
  75. #define UPDATE_PTR(s0, d0, s1, d1) {\
  76. d0 = s0;\
  77. d1 = s1;\
  78. }
  79. #define LOAD_SRCV(pos, a, s, per, v0, v1, vf) {\
  80. v1 = vec_ld(pos + a + 16, s);\
  81. vf = vec_perm(v0, v1, per);\
  82. }
  83. #define LOAD_SRCV8(pos, a, s, per, v0, v1, vf) {\
  84. if ((((uintptr_t)s + pos) % 16) > 8) {\
  85. v1 = vec_ld(pos + a + 16, s);\
  86. }\
  87. vf = vec_perm(v0, src_v1, per);\
  88. }
  89. #define GET_VFD(a, b, f, vf0, vf1, per, vf, off) {\
  90. vf1 = vec_ld((a * 2 * filterSize) + (b * 2) + 16 + off, f);\
  91. vf = vec_perm(vf0, vf1, per);\
  92. }
  93. #define FUNC(name) name ## _altivec
  94. #include "swscale_ppc_template.c"
  95. #undef FUNC
  96. #undef vzero
  97. #endif /* HAVE_BIGENDIAN */
  98. #define output_pixel(pos, val, bias, signedness) \
  99. if (big_endian) { \
  100. AV_WB16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
  101. } else { \
  102. AV_WL16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
  103. }
  104. static void
  105. yuv2plane1_float_u(const int32_t *src, float *dest, int dstW, int start)
  106. {
  107. static const int big_endian = HAVE_BIGENDIAN;
  108. static const int shift = 3;
  109. static const float float_mult = 1.0f / 65535.0f;
  110. int i, val;
  111. uint16_t val_uint;
  112. for (i = start; i < dstW; ++i){
  113. val = src[i] + (1 << (shift - 1));
  114. output_pixel(&val_uint, val, 0, uint);
  115. dest[i] = float_mult * (float)val_uint;
  116. }
  117. }
  118. static void
  119. yuv2plane1_float_bswap_u(const int32_t *src, uint32_t *dest, int dstW, int start)
  120. {
  121. static const int big_endian = HAVE_BIGENDIAN;
  122. static const int shift = 3;
  123. static const float float_mult = 1.0f / 65535.0f;
  124. int i, val;
  125. uint16_t val_uint;
  126. for (i = start; i < dstW; ++i){
  127. val = src[i] + (1 << (shift - 1));
  128. output_pixel(&val_uint, val, 0, uint);
  129. dest[i] = av_bswap32(av_float2int(float_mult * (float)val_uint));
  130. }
  131. }
  132. static void yuv2plane1_float_altivec(const int32_t *src, float *dest, int dstW)
  133. {
  134. const int dst_u = -(uintptr_t)dest & 3;
  135. const int shift = 3;
  136. const int add = (1 << (shift - 1));
  137. const int clip = (1 << 16) - 1;
  138. const float fmult = 1.0f / 65535.0f;
  139. const vec_u32 vadd = (vec_u32) {add, add, add, add};
  140. const vec_u32 vshift = (vec_u32) vec_splat_u32(shift);
  141. const vec_u32 vlargest = (vec_u32) {clip, clip, clip, clip};
  142. const vec_f vmul = (vec_f) {fmult, fmult, fmult, fmult};
  143. const vec_f vzero = (vec_f) {0, 0, 0, 0};
  144. vec_u32 v;
  145. vec_f vd;
  146. int i;
  147. yuv2plane1_float_u(src, dest, dst_u, 0);
  148. for (i = dst_u; i < dstW - 3; i += 4) {
  149. v = vec_ld(0, (const uint32_t *) &src[i]);
  150. v = vec_add(v, vadd);
  151. v = vec_sr(v, vshift);
  152. v = vec_min(v, vlargest);
  153. vd = vec_ctf(v, 0);
  154. vd = vec_madd(vd, vmul, vzero);
  155. vec_st(vd, 0, &dest[i]);
  156. }
  157. yuv2plane1_float_u(src, dest, dstW, i);
  158. }
  159. static void yuv2plane1_float_bswap_altivec(const int32_t *src, uint32_t *dest, int dstW)
  160. {
  161. const int dst_u = -(uintptr_t)dest & 3;
  162. const int shift = 3;
  163. const int add = (1 << (shift - 1));
  164. const int clip = (1 << 16) - 1;
  165. const float fmult = 1.0f / 65535.0f;
  166. const vec_u32 vadd = (vec_u32) {add, add, add, add};
  167. const vec_u32 vshift = (vec_u32) vec_splat_u32(shift);
  168. const vec_u32 vlargest = (vec_u32) {clip, clip, clip, clip};
  169. const vec_f vmul = (vec_f) {fmult, fmult, fmult, fmult};
  170. const vec_f vzero = (vec_f) {0, 0, 0, 0};
  171. const vec_u32 vswapbig = (vec_u32) {16, 16, 16, 16};
  172. const vec_u16 vswapsmall = vec_splat_u16(8);
  173. vec_u32 v;
  174. vec_f vd;
  175. int i;
  176. yuv2plane1_float_bswap_u(src, dest, dst_u, 0);
  177. for (i = dst_u; i < dstW - 3; i += 4) {
  178. v = vec_ld(0, (const uint32_t *) &src[i]);
  179. v = vec_add(v, vadd);
  180. v = vec_sr(v, vshift);
  181. v = vec_min(v, vlargest);
  182. vd = vec_ctf(v, 0);
  183. vd = vec_madd(vd, vmul, vzero);
  184. vd = (vec_f) vec_rl((vec_u32) vd, vswapbig);
  185. vd = (vec_f) vec_rl((vec_u16) vd, vswapsmall);
  186. vec_st(vd, 0, (float *) &dest[i]);
  187. }
  188. yuv2plane1_float_bswap_u(src, dest, dstW, i);
  189. }
  190. #define yuv2plane1_float(template, dest_type, BE_LE) \
  191. static void yuv2plane1_float ## BE_LE ## _altivec(const int16_t *src, uint8_t *dest, \
  192. int dstW, \
  193. const uint8_t *dither, int offset) \
  194. { \
  195. template((const int32_t *)src, (dest_type *)dest, dstW); \
  196. }
  197. #if HAVE_BIGENDIAN
  198. yuv2plane1_float(yuv2plane1_float_altivec, float, BE)
  199. yuv2plane1_float(yuv2plane1_float_bswap_altivec, uint32_t, LE)
  200. #else
  201. yuv2plane1_float(yuv2plane1_float_altivec, float, LE)
  202. yuv2plane1_float(yuv2plane1_float_bswap_altivec, uint32_t, BE)
  203. #endif
  204. #endif /* HAVE_ALTIVEC */
  205. av_cold void ff_sws_init_swscale_ppc(SwsContext *c)
  206. {
  207. #if HAVE_ALTIVEC
  208. enum AVPixelFormat dstFormat = c->dstFormat;
  209. if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
  210. return;
  211. #if HAVE_BIGENDIAN
  212. if (c->srcBpc == 8 && c->dstBpc <= 14) {
  213. c->hyScale = c->hcScale = hScale_real_altivec;
  214. }
  215. if (!is16BPS(dstFormat) && !isNBPS(dstFormat) && !isSemiPlanarYUV(dstFormat) &&
  216. dstFormat != AV_PIX_FMT_GRAYF32BE && dstFormat != AV_PIX_FMT_GRAYF32LE &&
  217. !c->needAlpha) {
  218. c->yuv2planeX = yuv2planeX_altivec;
  219. }
  220. #endif
  221. if (dstFormat == AV_PIX_FMT_GRAYF32BE) {
  222. c->yuv2plane1 = yuv2plane1_floatBE_altivec;
  223. } else if (dstFormat == AV_PIX_FMT_GRAYF32LE) {
  224. c->yuv2plane1 = yuv2plane1_floatLE_altivec;
  225. }
  226. /* The following list of supported dstFormat values should
  227. * match what's found in the body of ff_yuv2packedX_altivec() */
  228. if (!(c->flags & (SWS_BITEXACT | SWS_FULL_CHR_H_INT)) && !c->needAlpha) {
  229. switch (c->dstFormat) {
  230. case AV_PIX_FMT_ABGR:
  231. c->yuv2packedX = ff_yuv2abgr_X_altivec;
  232. break;
  233. case AV_PIX_FMT_BGRA:
  234. c->yuv2packedX = ff_yuv2bgra_X_altivec;
  235. break;
  236. case AV_PIX_FMT_ARGB:
  237. c->yuv2packedX = ff_yuv2argb_X_altivec;
  238. break;
  239. case AV_PIX_FMT_RGBA:
  240. c->yuv2packedX = ff_yuv2rgba_X_altivec;
  241. break;
  242. case AV_PIX_FMT_BGR24:
  243. c->yuv2packedX = ff_yuv2bgr24_X_altivec;
  244. break;
  245. case AV_PIX_FMT_RGB24:
  246. c->yuv2packedX = ff_yuv2rgb24_X_altivec;
  247. break;
  248. }
  249. }
  250. #endif /* HAVE_ALTIVEC */
  251. ff_sws_init_swscale_vsx(c);
  252. }