swscale_altivec.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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/cpu.h"
  28. #include "yuv2rgb_altivec.h"
  29. #define vzero vec_splat_s32(0)
  30. static inline void altivec_packIntArrayToCharArray(int *val, uint8_t *dest,
  31. int dstW)
  32. {
  33. register int i;
  34. vector unsigned int altivec_vectorShiftInt19 =
  35. vec_add(vec_splat_u32(10), vec_splat_u32(9));
  36. if ((uintptr_t)dest % 16) {
  37. /* badly aligned store, we force store alignment */
  38. /* and will handle load misalignment on val w/ vec_perm */
  39. vector unsigned char perm1;
  40. vector signed int v1;
  41. for (i = 0; (i < dstW) &&
  42. (((uintptr_t)dest + i) % 16); i++) {
  43. int t = val[i] >> 19;
  44. dest[i] = (t < 0) ? 0 : ((t > 255) ? 255 : t);
  45. }
  46. perm1 = vec_lvsl(i << 2, val);
  47. v1 = vec_ld(i << 2, val);
  48. for (; i < (dstW - 15); i += 16) {
  49. int offset = i << 2;
  50. vector signed int v2 = vec_ld(offset + 16, val);
  51. vector signed int v3 = vec_ld(offset + 32, val);
  52. vector signed int v4 = vec_ld(offset + 48, val);
  53. vector signed int v5 = vec_ld(offset + 64, val);
  54. vector signed int v12 = vec_perm(v1, v2, perm1);
  55. vector signed int v23 = vec_perm(v2, v3, perm1);
  56. vector signed int v34 = vec_perm(v3, v4, perm1);
  57. vector signed int v45 = vec_perm(v4, v5, perm1);
  58. vector signed int vA = vec_sra(v12, altivec_vectorShiftInt19);
  59. vector signed int vB = vec_sra(v23, altivec_vectorShiftInt19);
  60. vector signed int vC = vec_sra(v34, altivec_vectorShiftInt19);
  61. vector signed int vD = vec_sra(v45, altivec_vectorShiftInt19);
  62. vector unsigned short vs1 = vec_packsu(vA, vB);
  63. vector unsigned short vs2 = vec_packsu(vC, vD);
  64. vector unsigned char vf = vec_packsu(vs1, vs2);
  65. vec_st(vf, i, dest);
  66. v1 = v5;
  67. }
  68. } else { // dest is properly aligned, great
  69. for (i = 0; i < (dstW - 15); i += 16) {
  70. int offset = i << 2;
  71. vector signed int v1 = vec_ld(offset, val);
  72. vector signed int v2 = vec_ld(offset + 16, val);
  73. vector signed int v3 = vec_ld(offset + 32, val);
  74. vector signed int v4 = vec_ld(offset + 48, val);
  75. vector signed int v5 = vec_sra(v1, altivec_vectorShiftInt19);
  76. vector signed int v6 = vec_sra(v2, altivec_vectorShiftInt19);
  77. vector signed int v7 = vec_sra(v3, altivec_vectorShiftInt19);
  78. vector signed int v8 = vec_sra(v4, altivec_vectorShiftInt19);
  79. vector unsigned short vs1 = vec_packsu(v5, v6);
  80. vector unsigned short vs2 = vec_packsu(v7, v8);
  81. vector unsigned char vf = vec_packsu(vs1, vs2);
  82. vec_st(vf, i, dest);
  83. }
  84. }
  85. for (; i < dstW; i++) {
  86. int t = val[i] >> 19;
  87. dest[i] = (t < 0) ? 0 : ((t > 255) ? 255 : t);
  88. }
  89. }
  90. // FIXME remove the usage of scratch buffers.
  91. static void yuv2planeX_altivec(const int16_t *filter, int filterSize,
  92. const int16_t **src, uint8_t *dest, int dstW,
  93. const uint8_t *dither, int offset)
  94. {
  95. register int i, j;
  96. DECLARE_ALIGNED(16, int, val)[dstW];
  97. for (i = 0; i < dstW; i++)
  98. val[i] = dither[(i + offset) & 7] << 12;
  99. for (j = 0; j < filterSize; j++) {
  100. vector signed short l1, vLumFilter = vec_ld(j << 1, filter);
  101. vector unsigned char perm, perm0 = vec_lvsl(j << 1, filter);
  102. vLumFilter = vec_perm(vLumFilter, vLumFilter, perm0);
  103. vLumFilter = vec_splat(vLumFilter, 0); // lumFilter[j] is loaded 8 times in vLumFilter
  104. perm = vec_lvsl(0, src[j]);
  105. l1 = vec_ld(0, src[j]);
  106. for (i = 0; i < (dstW - 7); i += 8) {
  107. int offset = i << 2;
  108. vector signed short l2 = vec_ld((i << 1) + 16, src[j]);
  109. vector signed int v1 = vec_ld(offset, val);
  110. vector signed int v2 = vec_ld(offset + 16, val);
  111. vector signed short ls = vec_perm(l1, l2, perm); // lumSrc[j][i] ... lumSrc[j][i+7]
  112. vector signed int i1 = vec_mule(vLumFilter, ls);
  113. vector signed int i2 = vec_mulo(vLumFilter, ls);
  114. vector signed int vf1 = vec_mergeh(i1, i2);
  115. vector signed int vf2 = vec_mergel(i1, i2); // lumSrc[j][i] * lumFilter[j] ... lumSrc[j][i+7] * lumFilter[j]
  116. vector signed int vo1 = vec_add(v1, vf1);
  117. vector signed int vo2 = vec_add(v2, vf2);
  118. vec_st(vo1, offset, val);
  119. vec_st(vo2, offset + 16, val);
  120. l1 = l2;
  121. }
  122. for (; i < dstW; i++)
  123. val[i] += src[j][i] * filter[j];
  124. }
  125. altivec_packIntArrayToCharArray(val, dest, dstW);
  126. }
  127. static void hScale_altivec_real(SwsContext *c, int16_t *dst, int dstW,
  128. const uint8_t *src, const int16_t *filter,
  129. const int32_t *filterPos, int filterSize)
  130. {
  131. register int i;
  132. DECLARE_ALIGNED(16, int, tempo)[4];
  133. if (filterSize % 4) {
  134. for (i = 0; i < dstW; i++) {
  135. register int j;
  136. register int srcPos = filterPos[i];
  137. register int val = 0;
  138. for (j = 0; j < filterSize; j++)
  139. val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
  140. dst[i] = FFMIN(val >> 7, (1 << 15) - 1);
  141. }
  142. } else
  143. switch (filterSize) {
  144. case 4:
  145. for (i = 0; i < dstW; i++) {
  146. register int srcPos = filterPos[i];
  147. vector unsigned char src_v0 = vec_ld(srcPos, src);
  148. vector unsigned char src_v1, src_vF;
  149. vector signed short src_v, filter_v;
  150. vector signed int val_vEven, val_s;
  151. if ((((uintptr_t)src + srcPos) % 16) > 12) {
  152. src_v1 = vec_ld(srcPos + 16, src);
  153. }
  154. src_vF = vec_perm(src_v0, src_v1, vec_lvsl(srcPos, src));
  155. src_v = // vec_unpackh sign-extends...
  156. (vector signed short)(vec_mergeh((vector unsigned char)vzero, src_vF));
  157. // now put our elements in the even slots
  158. src_v = vec_mergeh(src_v, (vector signed short)vzero);
  159. filter_v = vec_ld(i << 3, filter);
  160. // The 3 above is 2 (filterSize == 4) + 1 (sizeof(short) == 2).
  161. // The neat trick: We only care for half the elements,
  162. // high or low depending on (i<<3)%16 (it's 0 or 8 here),
  163. // and we're going to use vec_mule, so we choose
  164. // carefully how to "unpack" the elements into the even slots.
  165. if ((i << 3) % 16)
  166. filter_v = vec_mergel(filter_v, (vector signed short)vzero);
  167. else
  168. filter_v = vec_mergeh(filter_v, (vector signed short)vzero);
  169. val_vEven = vec_mule(src_v, filter_v);
  170. val_s = vec_sums(val_vEven, vzero);
  171. vec_st(val_s, 0, tempo);
  172. dst[i] = FFMIN(tempo[3] >> 7, (1 << 15) - 1);
  173. }
  174. break;
  175. case 8:
  176. for (i = 0; i < dstW; i++) {
  177. register int srcPos = filterPos[i];
  178. vector unsigned char src_v0 = vec_ld(srcPos, src);
  179. vector unsigned char src_v1, src_vF;
  180. vector signed short src_v, filter_v;
  181. vector signed int val_v, val_s;
  182. if ((((uintptr_t)src + srcPos) % 16) > 8) {
  183. src_v1 = vec_ld(srcPos + 16, src);
  184. }
  185. src_vF = vec_perm(src_v0, src_v1, vec_lvsl(srcPos, src));
  186. src_v = // vec_unpackh sign-extends...
  187. (vector signed short)(vec_mergeh((vector unsigned char)vzero, src_vF));
  188. filter_v = vec_ld(i << 4, filter);
  189. // the 4 above is 3 (filterSize == 8) + 1 (sizeof(short) == 2)
  190. val_v = vec_msums(src_v, filter_v, (vector signed int)vzero);
  191. val_s = vec_sums(val_v, vzero);
  192. vec_st(val_s, 0, tempo);
  193. dst[i] = FFMIN(tempo[3] >> 7, (1 << 15) - 1);
  194. }
  195. break;
  196. case 16:
  197. for (i = 0; i < dstW; i++) {
  198. register int srcPos = filterPos[i];
  199. vector unsigned char src_v0 = vec_ld(srcPos, src);
  200. vector unsigned char src_v1 = vec_ld(srcPos + 16, src);
  201. vector unsigned char src_vF = vec_perm(src_v0, src_v1, vec_lvsl(srcPos, src));
  202. vector signed short src_vA = // vec_unpackh sign-extends...
  203. (vector signed short)(vec_mergeh((vector unsigned char)vzero, src_vF));
  204. vector signed short src_vB = // vec_unpackh sign-extends...
  205. (vector signed short)(vec_mergel((vector unsigned char)vzero, src_vF));
  206. vector signed short filter_v0 = vec_ld(i << 5, filter);
  207. vector signed short filter_v1 = vec_ld((i << 5) + 16, filter);
  208. // the 5 above are 4 (filterSize == 16) + 1 (sizeof(short) == 2)
  209. vector signed int val_acc = vec_msums(src_vA, filter_v0, (vector signed int)vzero);
  210. vector signed int val_v = vec_msums(src_vB, filter_v1, val_acc);
  211. vector signed int val_s = vec_sums(val_v, vzero);
  212. vec_st(val_s, 0, tempo);
  213. dst[i] = FFMIN(tempo[3] >> 7, (1 << 15) - 1);
  214. }
  215. break;
  216. default:
  217. for (i = 0; i < dstW; i++) {
  218. register int j;
  219. register int srcPos = filterPos[i];
  220. vector signed int val_s, val_v = (vector signed int)vzero;
  221. vector signed short filter_v0R = vec_ld(i * 2 * filterSize, filter);
  222. vector unsigned char permF = vec_lvsl((i * 2 * filterSize), filter);
  223. vector unsigned char src_v0 = vec_ld(srcPos, src);
  224. vector unsigned char permS = vec_lvsl(srcPos, src);
  225. for (j = 0; j < filterSize - 15; j += 16) {
  226. vector unsigned char src_v1 = vec_ld(srcPos + j + 16, src);
  227. vector unsigned char src_vF = vec_perm(src_v0, src_v1, permS);
  228. vector signed short src_vA = // vec_unpackh sign-extends...
  229. (vector signed short)(vec_mergeh((vector unsigned char)vzero, src_vF));
  230. vector signed short src_vB = // vec_unpackh sign-extends...
  231. (vector signed short)(vec_mergel((vector unsigned char)vzero, src_vF));
  232. vector signed short filter_v1R = vec_ld((i * 2 * filterSize) + (j * 2) + 16, filter);
  233. vector signed short filter_v2R = vec_ld((i * 2 * filterSize) + (j * 2) + 32, filter);
  234. vector signed short filter_v0 = vec_perm(filter_v0R, filter_v1R, permF);
  235. vector signed short filter_v1 = vec_perm(filter_v1R, filter_v2R, permF);
  236. vector signed int val_acc = vec_msums(src_vA, filter_v0, val_v);
  237. val_v = vec_msums(src_vB, filter_v1, val_acc);
  238. filter_v0R = filter_v2R;
  239. src_v0 = src_v1;
  240. }
  241. if (j < filterSize - 7) {
  242. // loading src_v0 is useless, it's already done above
  243. // vector unsigned char src_v0 = vec_ld(srcPos + j, src);
  244. vector unsigned char src_v1, src_vF;
  245. vector signed short src_v, filter_v1R, filter_v;
  246. if ((((uintptr_t)src + srcPos) % 16) > 8) {
  247. src_v1 = vec_ld(srcPos + j + 16, src);
  248. }
  249. src_vF = vec_perm(src_v0, src_v1, permS);
  250. src_v = // vec_unpackh sign-extends...
  251. (vector signed short)(vec_mergeh((vector unsigned char)vzero, src_vF));
  252. // loading filter_v0R is useless, it's already done above
  253. // vector signed short filter_v0R = vec_ld((i * 2 * filterSize) + j, filter);
  254. filter_v1R = vec_ld((i * 2 * filterSize) + (j * 2) + 16, filter);
  255. filter_v = vec_perm(filter_v0R, filter_v1R, permF);
  256. val_v = vec_msums(src_v, filter_v, val_v);
  257. }
  258. val_s = vec_sums(val_v, vzero);
  259. vec_st(val_s, 0, tempo);
  260. dst[i] = FFMIN(tempo[3] >> 7, (1 << 15) - 1);
  261. }
  262. }
  263. }
  264. void ff_sws_init_swScale_altivec(SwsContext *c)
  265. {
  266. enum PixelFormat dstFormat = c->dstFormat;
  267. if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
  268. return;
  269. if (c->srcBpc == 8 && c->dstBpc <= 10) {
  270. c->hyScale = c->hcScale = hScale_altivec_real;
  271. }
  272. if (!is16BPS(dstFormat) && !is9_OR_10BPS(dstFormat) &&
  273. dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21 &&
  274. !c->alpPixBuf) {
  275. c->yuv2planeX = yuv2planeX_altivec;
  276. }
  277. /* The following list of supported dstFormat values should
  278. * match what's found in the body of ff_yuv2packedX_altivec() */
  279. if (!(c->flags & (SWS_BITEXACT | SWS_FULL_CHR_H_INT)) && !c->alpPixBuf) {
  280. switch (c->dstFormat) {
  281. case PIX_FMT_ABGR:
  282. c->yuv2packedX = ff_yuv2abgr_X_altivec;
  283. break;
  284. case PIX_FMT_BGRA:
  285. c->yuv2packedX = ff_yuv2bgra_X_altivec;
  286. break;
  287. case PIX_FMT_ARGB:
  288. c->yuv2packedX = ff_yuv2argb_X_altivec;
  289. break;
  290. case PIX_FMT_RGBA:
  291. c->yuv2packedX = ff_yuv2rgba_X_altivec;
  292. break;
  293. case PIX_FMT_BGR24:
  294. c->yuv2packedX = ff_yuv2bgr24_X_altivec;
  295. break;
  296. case PIX_FMT_RGB24:
  297. c->yuv2packedX = ff_yuv2rgb24_X_altivec;
  298. break;
  299. }
  300. }
  301. }