swscale_altivec.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 SHIFT 3
  99. #define get_pixel(val, bias, signedness) \
  100. (bias + av_clip_ ## signedness ## 16(val >> shift))
  101. static void
  102. yuv2plane1_float_u(const int32_t *src, float *dest, int dstW, int start)
  103. {
  104. static const int shift = 3;
  105. static const float float_mult = 1.0f / 65535.0f;
  106. int i, val;
  107. uint16_t val_uint;
  108. for (i = start; i < dstW; ++i){
  109. val = src[i] + (1 << (shift - 1));
  110. val_uint = get_pixel(val, 0, uint);
  111. dest[i] = float_mult * (float)val_uint;
  112. }
  113. }
  114. static void
  115. yuv2plane1_float_bswap_u(const int32_t *src, uint32_t *dest, int dstW, int start)
  116. {
  117. static const int shift = 3;
  118. static const float float_mult = 1.0f / 65535.0f;
  119. int i, val;
  120. uint16_t val_uint;
  121. for (i = start; i < dstW; ++i){
  122. val = src[i] + (1 << (shift - 1));
  123. val_uint = get_pixel(val, 0, uint);
  124. dest[i] = av_bswap32(av_float2int(float_mult * (float)val_uint));
  125. }
  126. }
  127. static void yuv2plane1_float_altivec(const int32_t *src, float *dest, int dstW)
  128. {
  129. const int dst_u = -(uintptr_t)dest & 3;
  130. const int add = (1 << (SHIFT - 1));
  131. const int clip = (1 << 16) - 1;
  132. const float fmult = 1.0f / 65535.0f;
  133. const vec_u32 vadd = (vec_u32) {add, add, add, add};
  134. const vec_u32 vshift = (vec_u32) vec_splat_u32(SHIFT);
  135. const vec_u32 vlargest = (vec_u32) {clip, clip, clip, clip};
  136. const vec_f vmul = (vec_f) {fmult, fmult, fmult, fmult};
  137. const vec_f vzero = (vec_f) {0, 0, 0, 0};
  138. vec_u32 v;
  139. vec_f vd;
  140. int i;
  141. yuv2plane1_float_u(src, dest, dst_u, 0);
  142. for (i = dst_u; i < dstW - 3; i += 4) {
  143. v = vec_ld(0, (const uint32_t *) &src[i]);
  144. v = vec_add(v, vadd);
  145. v = vec_sr(v, vshift);
  146. v = vec_min(v, vlargest);
  147. vd = vec_ctf(v, 0);
  148. vd = vec_madd(vd, vmul, vzero);
  149. vec_st(vd, 0, &dest[i]);
  150. }
  151. yuv2plane1_float_u(src, dest, dstW, i);
  152. }
  153. static void yuv2plane1_float_bswap_altivec(const int32_t *src, uint32_t *dest, int dstW)
  154. {
  155. const int dst_u = -(uintptr_t)dest & 3;
  156. const int add = (1 << (SHIFT - 1));
  157. const int clip = (1 << 16) - 1;
  158. const float fmult = 1.0f / 65535.0f;
  159. const vec_u32 vadd = (vec_u32) {add, add, add, add};
  160. const vec_u32 vshift = (vec_u32) vec_splat_u32(SHIFT);
  161. const vec_u32 vlargest = (vec_u32) {clip, clip, clip, clip};
  162. const vec_f vmul = (vec_f) {fmult, fmult, fmult, fmult};
  163. const vec_f vzero = (vec_f) {0, 0, 0, 0};
  164. const vec_u32 vswapbig = (vec_u32) {16, 16, 16, 16};
  165. const vec_u16 vswapsmall = vec_splat_u16(8);
  166. vec_u32 v;
  167. vec_f vd;
  168. int i;
  169. yuv2plane1_float_bswap_u(src, dest, dst_u, 0);
  170. for (i = dst_u; i < dstW - 3; i += 4) {
  171. v = vec_ld(0, (const uint32_t *) &src[i]);
  172. v = vec_add(v, vadd);
  173. v = vec_sr(v, vshift);
  174. v = vec_min(v, vlargest);
  175. vd = vec_ctf(v, 0);
  176. vd = vec_madd(vd, vmul, vzero);
  177. vd = (vec_f) vec_rl((vec_u32) vd, vswapbig);
  178. vd = (vec_f) vec_rl((vec_u16) vd, vswapsmall);
  179. vec_st(vd, 0, (float *) &dest[i]);
  180. }
  181. yuv2plane1_float_bswap_u(src, dest, dstW, i);
  182. }
  183. #define yuv2plane1_float(template, dest_type, BE_LE) \
  184. static void yuv2plane1_float ## BE_LE ## _altivec(const int16_t *src, uint8_t *dest, \
  185. int dstW, \
  186. const uint8_t *dither, int offset) \
  187. { \
  188. template((const int32_t *)src, (dest_type *)dest, dstW); \
  189. }
  190. #if HAVE_BIGENDIAN
  191. yuv2plane1_float(yuv2plane1_float_altivec, float, BE)
  192. yuv2plane1_float(yuv2plane1_float_bswap_altivec, uint32_t, LE)
  193. #else
  194. yuv2plane1_float(yuv2plane1_float_altivec, float, LE)
  195. yuv2plane1_float(yuv2plane1_float_bswap_altivec, uint32_t, BE)
  196. #endif
  197. #endif /* HAVE_ALTIVEC */
  198. av_cold void ff_sws_init_swscale_ppc(SwsContext *c)
  199. {
  200. #if HAVE_ALTIVEC
  201. enum AVPixelFormat dstFormat = c->dstFormat;
  202. if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
  203. return;
  204. #if HAVE_BIGENDIAN
  205. if (c->srcBpc == 8 && c->dstBpc <= 14) {
  206. c->hyScale = c->hcScale = hScale_real_altivec;
  207. }
  208. if (!is16BPS(dstFormat) && !isNBPS(dstFormat) && !isSemiPlanarYUV(dstFormat) &&
  209. dstFormat != AV_PIX_FMT_GRAYF32BE && dstFormat != AV_PIX_FMT_GRAYF32LE &&
  210. !c->needAlpha) {
  211. c->yuv2planeX = yuv2planeX_altivec;
  212. }
  213. #endif
  214. if (dstFormat == AV_PIX_FMT_GRAYF32BE) {
  215. c->yuv2plane1 = yuv2plane1_floatBE_altivec;
  216. } else if (dstFormat == AV_PIX_FMT_GRAYF32LE) {
  217. c->yuv2plane1 = yuv2plane1_floatLE_altivec;
  218. }
  219. /* The following list of supported dstFormat values should
  220. * match what's found in the body of ff_yuv2packedX_altivec() */
  221. if (!(c->flags & (SWS_BITEXACT | SWS_FULL_CHR_H_INT)) && !c->needAlpha) {
  222. switch (c->dstFormat) {
  223. case AV_PIX_FMT_ABGR:
  224. c->yuv2packedX = ff_yuv2abgr_X_altivec;
  225. break;
  226. case AV_PIX_FMT_BGRA:
  227. c->yuv2packedX = ff_yuv2bgra_X_altivec;
  228. break;
  229. case AV_PIX_FMT_ARGB:
  230. c->yuv2packedX = ff_yuv2argb_X_altivec;
  231. break;
  232. case AV_PIX_FMT_RGBA:
  233. c->yuv2packedX = ff_yuv2rgba_X_altivec;
  234. break;
  235. case AV_PIX_FMT_BGR24:
  236. c->yuv2packedX = ff_yuv2bgr24_X_altivec;
  237. break;
  238. case AV_PIX_FMT_RGB24:
  239. c->yuv2packedX = ff_yuv2rgb24_X_altivec;
  240. break;
  241. }
  242. }
  243. #endif /* HAVE_ALTIVEC */
  244. ff_sws_init_swscale_vsx(c);
  245. }