swscale_altivec.c 15 KB

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