swscale_altivec.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 "libavutil/intfloat.h"
  30. #include "yuv2rgb_altivec.h"
  31. #include "libavutil/ppc/util_altivec.h"
  32. #if HAVE_ALTIVEC
  33. #if HAVE_BIGENDIAN
  34. #define vzero vec_splat_s32(0)
  35. #define GET_LS(a,b,c,s) {\
  36. vector signed short l2 = vec_ld(((b) << 1) + 16, s);\
  37. ls = vec_perm(a, l2, c);\
  38. a = l2;\
  39. }
  40. #define yuv2planeX_8(d1, d2, l1, src, x, perm, filter) do {\
  41. vector signed short ls;\
  42. vector signed int vf1, vf2, i1, i2;\
  43. GET_LS(l1, x, perm, src);\
  44. i1 = vec_mule(filter, ls);\
  45. i2 = vec_mulo(filter, ls);\
  46. vf1 = vec_mergeh(i1, i2);\
  47. vf2 = vec_mergel(i1, i2);\
  48. d1 = vec_add(d1, vf1);\
  49. d2 = vec_add(d2, vf2);\
  50. } while (0)
  51. #define LOAD_FILTER(vf,f) {\
  52. vector unsigned char perm0 = vec_lvsl(joffset, f);\
  53. vf = vec_ld(joffset, f);\
  54. vf = vec_perm(vf, vf, perm0);\
  55. }
  56. #define LOAD_L1(ll1,s,p){\
  57. p = vec_lvsl(xoffset, s);\
  58. ll1 = vec_ld(xoffset, s);\
  59. }
  60. // The 3 above is 2 (filterSize == 4) + 1 (sizeof(short) == 2).
  61. // The neat trick: We only care for half the elements,
  62. // high or low depending on (i<<3)%16 (it's 0 or 8 here),
  63. // and we're going to use vec_mule, so we choose
  64. // carefully how to "unpack" the elements into the even slots.
  65. #define GET_VF4(a, vf, f) {\
  66. vf = vec_ld(a<< 3, f);\
  67. if ((a << 3) % 16)\
  68. vf = vec_mergel(vf, (vector signed short)vzero);\
  69. else\
  70. vf = vec_mergeh(vf, (vector signed short)vzero);\
  71. }
  72. #define FIRST_LOAD(sv, pos, s, per) {\
  73. sv = vec_ld(pos, s);\
  74. per = vec_lvsl(pos, s);\
  75. }
  76. #define UPDATE_PTR(s0, d0, s1, d1) {\
  77. d0 = s0;\
  78. d1 = s1;\
  79. }
  80. #define LOAD_SRCV(pos, a, s, per, v0, v1, vf) {\
  81. v1 = vec_ld(pos + a + 16, s);\
  82. vf = vec_perm(v0, v1, per);\
  83. }
  84. #define LOAD_SRCV8(pos, a, s, per, v0, v1, vf) {\
  85. if ((((uintptr_t)s + pos) % 16) > 8) {\
  86. v1 = vec_ld(pos + a + 16, s);\
  87. }\
  88. vf = vec_perm(v0, src_v1, per);\
  89. }
  90. #define GET_VFD(a, b, f, vf0, vf1, per, vf, off) {\
  91. vf1 = vec_ld((a * 2 * filterSize) + (b * 2) + 16 + off, f);\
  92. vf = vec_perm(vf0, vf1, per);\
  93. }
  94. #define FUNC(name) name ## _altivec
  95. #include "swscale_ppc_template.c"
  96. #undef FUNC
  97. #undef vzero
  98. #endif /* HAVE_BIGENDIAN */
  99. #define SHIFT 3
  100. #define get_pixel(val, bias, signedness) \
  101. (bias + av_clip_ ## signedness ## 16(val >> shift))
  102. static void
  103. yuv2plane1_float_u(const int32_t *src, float *dest, int dstW, int start)
  104. {
  105. static const int shift = 3;
  106. static const float float_mult = 1.0f / 65535.0f;
  107. int i, val;
  108. uint16_t val_uint;
  109. for (i = start; i < dstW; ++i){
  110. val = src[i] + (1 << (shift - 1));
  111. val_uint = get_pixel(val, 0, uint);
  112. dest[i] = float_mult * (float)val_uint;
  113. }
  114. }
  115. static void
  116. yuv2plane1_float_bswap_u(const int32_t *src, uint32_t *dest, int dstW, int start)
  117. {
  118. static const int shift = 3;
  119. static const float float_mult = 1.0f / 65535.0f;
  120. int i, val;
  121. uint16_t val_uint;
  122. for (i = start; i < dstW; ++i){
  123. val = src[i] + (1 << (shift - 1));
  124. val_uint = get_pixel(val, 0, uint);
  125. dest[i] = av_bswap32(av_float2int(float_mult * (float)val_uint));
  126. }
  127. }
  128. static void yuv2plane1_float_altivec(const int32_t *src, float *dest, int dstW)
  129. {
  130. const int dst_u = -(uintptr_t)dest & 3;
  131. const int add = (1 << (SHIFT - 1));
  132. const int clip = (1 << 16) - 1;
  133. const float fmult = 1.0f / 65535.0f;
  134. const vec_u32 vadd = (vec_u32) {add, add, add, add};
  135. const vec_u32 vshift = (vec_u32) vec_splat_u32(SHIFT);
  136. const vec_u32 vlargest = (vec_u32) {clip, clip, clip, clip};
  137. const vec_f vmul = (vec_f) {fmult, fmult, fmult, fmult};
  138. const vec_f vzero = (vec_f) {0, 0, 0, 0};
  139. vec_u32 v;
  140. vec_f vd;
  141. int i;
  142. yuv2plane1_float_u(src, dest, dst_u, 0);
  143. for (i = dst_u; i < dstW - 3; i += 4) {
  144. v = vec_ld(0, (const uint32_t *) &src[i]);
  145. v = vec_add(v, vadd);
  146. v = vec_sr(v, vshift);
  147. v = vec_min(v, vlargest);
  148. vd = vec_ctf(v, 0);
  149. vd = vec_madd(vd, vmul, vzero);
  150. vec_st(vd, 0, &dest[i]);
  151. }
  152. yuv2plane1_float_u(src, dest, dstW, i);
  153. }
  154. static void yuv2plane1_float_bswap_altivec(const int32_t *src, uint32_t *dest, int dstW)
  155. {
  156. const int dst_u = -(uintptr_t)dest & 3;
  157. const int add = (1 << (SHIFT - 1));
  158. const int clip = (1 << 16) - 1;
  159. const float fmult = 1.0f / 65535.0f;
  160. const vec_u32 vadd = (vec_u32) {add, add, add, add};
  161. const vec_u32 vshift = (vec_u32) vec_splat_u32(SHIFT);
  162. const vec_u32 vlargest = (vec_u32) {clip, clip, clip, clip};
  163. const vec_f vmul = (vec_f) {fmult, fmult, fmult, fmult};
  164. const vec_f vzero = (vec_f) {0, 0, 0, 0};
  165. const vec_u32 vswapbig = (vec_u32) {16, 16, 16, 16};
  166. const vec_u16 vswapsmall = vec_splat_u16(8);
  167. vec_u32 v;
  168. vec_f vd;
  169. int i;
  170. yuv2plane1_float_bswap_u(src, dest, dst_u, 0);
  171. for (i = dst_u; i < dstW - 3; i += 4) {
  172. v = vec_ld(0, (const uint32_t *) &src[i]);
  173. v = vec_add(v, vadd);
  174. v = vec_sr(v, vshift);
  175. v = vec_min(v, vlargest);
  176. vd = vec_ctf(v, 0);
  177. vd = vec_madd(vd, vmul, vzero);
  178. vd = (vec_f) vec_rl((vec_u32) vd, vswapbig);
  179. vd = (vec_f) vec_rl((vec_u16) vd, vswapsmall);
  180. vec_st(vd, 0, (float *) &dest[i]);
  181. }
  182. yuv2plane1_float_bswap_u(src, dest, dstW, i);
  183. }
  184. #define yuv2plane1_float(template, dest_type, BE_LE) \
  185. static void yuv2plane1_float ## BE_LE ## _altivec(const int16_t *src, uint8_t *dest, \
  186. int dstW, \
  187. const uint8_t *dither, int offset) \
  188. { \
  189. template((const int32_t *)src, (dest_type *)dest, dstW); \
  190. }
  191. #if HAVE_BIGENDIAN
  192. yuv2plane1_float(yuv2plane1_float_altivec, float, BE)
  193. yuv2plane1_float(yuv2plane1_float_bswap_altivec, uint32_t, LE)
  194. #else
  195. yuv2plane1_float(yuv2plane1_float_altivec, float, LE)
  196. yuv2plane1_float(yuv2plane1_float_bswap_altivec, uint32_t, BE)
  197. #endif
  198. #endif /* HAVE_ALTIVEC */
  199. av_cold void ff_sws_init_swscale_ppc(SwsInternal *c)
  200. {
  201. #if HAVE_ALTIVEC
  202. enum AVPixelFormat dstFormat = c->opts.dst_format;
  203. if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
  204. return;
  205. #if HAVE_BIGENDIAN
  206. if (c->srcBpc == 8 && c->dstBpc <= 14) {
  207. c->hyScale = c->hcScale = hScale_real_altivec;
  208. }
  209. if (!is16BPS(dstFormat) && !isNBPS(dstFormat) && !isSemiPlanarYUV(dstFormat) && !isDataInHighBits(dstFormat) &&
  210. dstFormat != AV_PIX_FMT_GRAYF32BE && dstFormat != AV_PIX_FMT_GRAYF32LE &&
  211. !c->needAlpha) {
  212. c->yuv2planeX = yuv2planeX_altivec;
  213. }
  214. #endif
  215. if (dstFormat == AV_PIX_FMT_GRAYF32BE) {
  216. c->yuv2plane1 = yuv2plane1_floatBE_altivec;
  217. } else if (dstFormat == AV_PIX_FMT_GRAYF32LE) {
  218. c->yuv2plane1 = yuv2plane1_floatLE_altivec;
  219. }
  220. /* The following list of supported dstFormat values should
  221. * match what's found in the body of ff_yuv2packedX_altivec() */
  222. if (!(c->opts.flags & (SWS_BITEXACT | SWS_FULL_CHR_H_INT)) && !c->needAlpha) {
  223. switch (c->opts.dst_format) {
  224. case AV_PIX_FMT_ABGR:
  225. c->yuv2packedX = ff_yuv2abgr_X_altivec;
  226. break;
  227. case AV_PIX_FMT_BGRA:
  228. c->yuv2packedX = ff_yuv2bgra_X_altivec;
  229. break;
  230. case AV_PIX_FMT_ARGB:
  231. c->yuv2packedX = ff_yuv2argb_X_altivec;
  232. break;
  233. case AV_PIX_FMT_RGBA:
  234. c->yuv2packedX = ff_yuv2rgba_X_altivec;
  235. break;
  236. case AV_PIX_FMT_BGR24:
  237. c->yuv2packedX = ff_yuv2bgr24_X_altivec;
  238. break;
  239. case AV_PIX_FMT_RGB24:
  240. c->yuv2packedX = ff_yuv2rgb24_X_altivec;
  241. break;
  242. }
  243. }
  244. #endif /* HAVE_ALTIVEC */
  245. ff_sws_init_swscale_vsx(c);
  246. }