vp8dsp_altivec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * VP8 compatible video decoder
  3. *
  4. * Copyright (C) 2010 David Conrad
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "config.h"
  23. #include "libavutil/attributes.h"
  24. #include "libavutil/cpu.h"
  25. #include "libavutil/mem_internal.h"
  26. #include "libavutil/ppc/cpu.h"
  27. #include "libavutil/ppc/util_altivec.h"
  28. #include "libavcodec/vp8dsp.h"
  29. #include "hpeldsp_altivec.h"
  30. #if HAVE_ALTIVEC
  31. #define REPT4(...) { __VA_ARGS__, __VA_ARGS__, __VA_ARGS__, __VA_ARGS__ }
  32. // h subpel filter uses msum to multiply+add 4 pixel taps at once
  33. static const vec_s8 h_subpel_filters_inner[7] =
  34. {
  35. REPT4( -6, 123, 12, -1),
  36. REPT4(-11, 108, 36, -8),
  37. REPT4( -9, 93, 50, -6),
  38. REPT4(-16, 77, 77, -16),
  39. REPT4( -6, 50, 93, -9),
  40. REPT4( -8, 36, 108, -11),
  41. REPT4( -1, 12, 123, -6),
  42. };
  43. // for 6tap filters, these are the outer two taps
  44. // The zeros mask off pixels 4-7 when filtering 0-3
  45. // and vice-versa
  46. static const vec_s8 h_subpel_filters_outer[4] =
  47. {
  48. REPT4(0, 0, 2, 1),
  49. REPT4(0, 0, 3, 3),
  50. REPT4(0, 0, 1, 2),
  51. REPT4(0, 0, 0, 0),
  52. };
  53. #define LOAD_H_SUBPEL_FILTER(i) \
  54. vec_s8 filter_inner = h_subpel_filters_inner[i]; \
  55. vec_s8 filter_outerh = h_subpel_filters_outer[(i)>>1]; \
  56. vec_s8 filter_outerl = vec_sld(filter_outerh, filter_outerh, 2)
  57. #if HAVE_BIGENDIAN
  58. #define GET_PIXHL(offset) \
  59. a = vec_ld((offset)-is6tap-1, src); \
  60. b = vec_ld((offset)-is6tap-1+15, src); \
  61. pixh = vec_perm(a, b, permh##offset); \
  62. pixl = vec_perm(a, b, perml##offset)
  63. #define GET_OUTER(offset) outer = vec_perm(a, b, perm_6tap##offset)
  64. #else
  65. #define GET_PIXHL(offset) \
  66. a = vec_vsx_ld((offset)-is6tap-1, src); \
  67. pixh = vec_perm(a, a, perm_inner); \
  68. pixl = vec_perm(a, a, vec_add(perm_inner, vec_splat_u8(4)))
  69. #define GET_OUTER(offset) outer = vec_perm(a, a, perm_outer)
  70. #endif
  71. #define FILTER_H(dstv, off) \
  72. GET_PIXHL(off); \
  73. filth = vec_msum(filter_inner, pixh, c64); \
  74. filtl = vec_msum(filter_inner, pixl, c64); \
  75. \
  76. if (is6tap) { \
  77. GET_OUTER(off); \
  78. filth = vec_msum(filter_outerh, outer, filth); \
  79. filtl = vec_msum(filter_outerl, outer, filtl); \
  80. } \
  81. if (w == 4) \
  82. filtl = filth; /* discard pixels 4-7 */ \
  83. dstv = vec_packs(filth, filtl); \
  84. dstv = vec_sra(dstv, c7)
  85. static av_always_inline
  86. void put_vp8_epel_h_altivec_core(uint8_t *dst, ptrdiff_t dst_stride,
  87. const uint8_t *src, ptrdiff_t src_stride,
  88. int h, int mx, int w, int is6tap)
  89. {
  90. LOAD_H_SUBPEL_FILTER(mx-1);
  91. #if HAVE_BIGENDIAN
  92. vec_u8 align_vec0, align_vec8, permh0, permh8;
  93. vec_u8 perm_6tap0, perm_6tap8, perml0, perml8;
  94. vec_u8 b;
  95. #endif
  96. vec_u8 filt, a, pixh, pixl, outer;
  97. vec_s16 f16h, f16l;
  98. vec_s32 filth, filtl;
  99. vec_u8 perm_inner6 = { 1,2,3,4, 2,3,4,5, 3,4,5,6, 4,5,6,7 };
  100. vec_u8 perm_inner4 = { 0,1,2,3, 1,2,3,4, 2,3,4,5, 3,4,5,6 };
  101. vec_u8 perm_inner = is6tap ? perm_inner6 : perm_inner4;
  102. vec_u8 perm_outer = { 4,9, 0,5, 5,10, 1,6, 6,11, 2,7, 7,12, 3,8 };
  103. vec_s32 c64 = vec_sl(vec_splat_s32(1), vec_splat_u32(6));
  104. vec_u16 c7 = vec_splat_u16(7);
  105. #if HAVE_BIGENDIAN
  106. align_vec0 = vec_lvsl( -is6tap-1, src);
  107. align_vec8 = vec_lvsl(8-is6tap-1, src);
  108. permh0 = vec_perm(align_vec0, align_vec0, perm_inner);
  109. permh8 = vec_perm(align_vec8, align_vec8, perm_inner);
  110. perm_inner = vec_add(perm_inner, vec_splat_u8(4));
  111. perml0 = vec_perm(align_vec0, align_vec0, perm_inner);
  112. perml8 = vec_perm(align_vec8, align_vec8, perm_inner);
  113. perm_6tap0 = vec_perm(align_vec0, align_vec0, perm_outer);
  114. perm_6tap8 = vec_perm(align_vec8, align_vec8, perm_outer);
  115. #endif
  116. while (h --> 0) {
  117. FILTER_H(f16h, 0);
  118. if (w == 16) {
  119. FILTER_H(f16l, 8);
  120. filt = vec_packsu(f16h, f16l);
  121. vec_st(filt, 0, dst);
  122. } else {
  123. filt = vec_packsu(f16h, f16h);
  124. vec_ste((vec_u32)filt, 0, (uint32_t*)dst);
  125. if (w == 8)
  126. vec_ste((vec_u32)filt, 4, (uint32_t*)dst);
  127. }
  128. src += src_stride;
  129. dst += dst_stride;
  130. }
  131. }
  132. // v subpel filter does a simple vertical multiply + add
  133. static const vec_u8 v_subpel_filters[7] =
  134. {
  135. { 0, 6, 123, 12, 1, 0 },
  136. { 2, 11, 108, 36, 8, 1 },
  137. { 0, 9, 93, 50, 6, 0 },
  138. { 3, 16, 77, 77, 16, 3 },
  139. { 0, 6, 50, 93, 9, 0 },
  140. { 1, 8, 36, 108, 11, 2 },
  141. { 0, 1, 12, 123, 6, 0 },
  142. };
  143. #define LOAD_V_SUBPEL_FILTER(i) \
  144. vec_u8 subpel_filter = v_subpel_filters[i]; \
  145. vec_u8 f0 = vec_splat(subpel_filter, 0); \
  146. vec_u8 f1 = vec_splat(subpel_filter, 1); \
  147. vec_u8 f2 = vec_splat(subpel_filter, 2); \
  148. vec_u8 f3 = vec_splat(subpel_filter, 3); \
  149. vec_u8 f4 = vec_splat(subpel_filter, 4); \
  150. vec_u8 f5 = vec_splat(subpel_filter, 5)
  151. #define FILTER_V(dstv, vec_mul) \
  152. s1f = (vec_s16)vec_mul(s1, f1); \
  153. s2f = (vec_s16)vec_mul(s2, f2); \
  154. s3f = (vec_s16)vec_mul(s3, f3); \
  155. s4f = (vec_s16)vec_mul(s4, f4); \
  156. s2f = vec_subs(s2f, s1f); \
  157. s3f = vec_subs(s3f, s4f); \
  158. if (is6tap) { \
  159. s0f = (vec_s16)vec_mul(s0, f0); \
  160. s5f = (vec_s16)vec_mul(s5, f5); \
  161. s2f = vec_adds(s2f, s0f); \
  162. s3f = vec_adds(s3f, s5f); \
  163. } \
  164. dstv = vec_adds(s2f, s3f); \
  165. dstv = vec_adds(dstv, c64); \
  166. dstv = vec_sra(dstv, c7)
  167. #if HAVE_BIGENDIAN
  168. #define LOAD_HL(off, s, perm) load_with_perm_vec(off, s, perm)
  169. #else
  170. #define LOAD_HL(off, s, perm) vec_mergeh(vec_vsx_ld(off,s), vec_vsx_ld(off+8,s))
  171. #endif
  172. static av_always_inline
  173. void put_vp8_epel_v_altivec_core(uint8_t *dst, ptrdiff_t dst_stride,
  174. const uint8_t *src, ptrdiff_t src_stride,
  175. int h, int my, int w, int is6tap)
  176. {
  177. LOAD_V_SUBPEL_FILTER(my-1);
  178. vec_u8 s0, s1, s2, s3, s4, s5, filt, align_vech, perm_vec, align_vecl;
  179. vec_s16 s0f, s1f, s2f, s3f, s4f, s5f, f16h, f16l;
  180. vec_s16 c64 = vec_sl(vec_splat_s16(1), vec_splat_u16(6));
  181. vec_u16 c7 = vec_splat_u16(7);
  182. #if HAVE_BIGENDIAN
  183. // we want pixels 0-7 to be in the even positions and 8-15 in the odd,
  184. // so combine this permute with the alignment permute vector
  185. align_vech = vec_lvsl(0, src);
  186. align_vecl = vec_sld(align_vech, align_vech, 8);
  187. if (w ==16)
  188. perm_vec = vec_mergeh(align_vech, align_vecl);
  189. else
  190. perm_vec = vec_mergeh(align_vech, align_vech);
  191. #endif
  192. if (is6tap)
  193. s0 = LOAD_HL(-2*src_stride, src, perm_vec);
  194. s1 = LOAD_HL(-1*src_stride, src, perm_vec);
  195. s2 = LOAD_HL( 0*src_stride, src, perm_vec);
  196. s3 = LOAD_HL( 1*src_stride, src, perm_vec);
  197. if (is6tap)
  198. s4 = LOAD_HL( 2*src_stride, src, perm_vec);
  199. src += (2+is6tap)*src_stride;
  200. while (h --> 0) {
  201. if (is6tap)
  202. s5 = LOAD_HL(0, src, perm_vec);
  203. else
  204. s4 = LOAD_HL(0, src, perm_vec);
  205. FILTER_V(f16h, vec_mule);
  206. if (w == 16) {
  207. FILTER_V(f16l, vec_mulo);
  208. filt = vec_packsu(f16h, f16l);
  209. vec_st(filt, 0, dst);
  210. } else {
  211. filt = vec_packsu(f16h, f16h);
  212. if (w == 4)
  213. filt = (vec_u8)vec_splat((vec_u32)filt, 0);
  214. else
  215. vec_ste((vec_u32)filt, 4, (uint32_t*)dst);
  216. vec_ste((vec_u32)filt, 0, (uint32_t*)dst);
  217. }
  218. if (is6tap)
  219. s0 = s1;
  220. s1 = s2;
  221. s2 = s3;
  222. s3 = s4;
  223. if (is6tap)
  224. s4 = s5;
  225. dst += dst_stride;
  226. src += src_stride;
  227. }
  228. }
  229. #define EPEL_FUNCS(WIDTH, TAPS) \
  230. static av_noinline \
  231. void put_vp8_epel ## WIDTH ## _h ## TAPS ## _altivec(uint8_t *dst, ptrdiff_t dst_stride, const uint8_t *src, ptrdiff_t src_stride, int h, int mx, int my) \
  232. { \
  233. put_vp8_epel_h_altivec_core(dst, dst_stride, src, src_stride, h, mx, WIDTH, TAPS == 6); \
  234. } \
  235. \
  236. static av_noinline \
  237. void put_vp8_epel ## WIDTH ## _v ## TAPS ## _altivec(uint8_t *dst, ptrdiff_t dst_stride, const uint8_t *src, ptrdiff_t src_stride, int h, int mx, int my) \
  238. { \
  239. put_vp8_epel_v_altivec_core(dst, dst_stride, src, src_stride, h, my, WIDTH, TAPS == 6); \
  240. }
  241. #define EPEL_HV(WIDTH, HTAPS, VTAPS) \
  242. static void put_vp8_epel ## WIDTH ## _h ## HTAPS ## v ## VTAPS ## _altivec(uint8_t *dst, ptrdiff_t dstride, const uint8_t *src, ptrdiff_t sstride, int h, int mx, int my) \
  243. { \
  244. DECLARE_ALIGNED(16, uint8_t, tmp)[(2*WIDTH+5)*16]; \
  245. if (VTAPS == 6) { \
  246. put_vp8_epel ## WIDTH ## _h ## HTAPS ## _altivec(tmp, 16, src-2*sstride, sstride, h+5, mx, my); \
  247. put_vp8_epel ## WIDTH ## _v ## VTAPS ## _altivec(dst, dstride, tmp+2*16, 16, h, mx, my); \
  248. } else { \
  249. put_vp8_epel ## WIDTH ## _h ## HTAPS ## _altivec(tmp, 16, src-sstride, sstride, h+4, mx, my); \
  250. put_vp8_epel ## WIDTH ## _v ## VTAPS ## _altivec(dst, dstride, tmp+16, 16, h, mx, my); \
  251. } \
  252. }
  253. EPEL_FUNCS(16,6)
  254. EPEL_FUNCS(8, 6)
  255. EPEL_FUNCS(8, 4)
  256. EPEL_FUNCS(4, 6)
  257. EPEL_FUNCS(4, 4)
  258. EPEL_HV(16, 6,6)
  259. EPEL_HV(8, 6,6)
  260. EPEL_HV(8, 4,6)
  261. EPEL_HV(8, 6,4)
  262. EPEL_HV(8, 4,4)
  263. EPEL_HV(4, 6,6)
  264. EPEL_HV(4, 4,6)
  265. EPEL_HV(4, 6,4)
  266. EPEL_HV(4, 4,4)
  267. static void put_vp8_pixels16_altivec(uint8_t *dst, ptrdiff_t dstride, const uint8_t *src, ptrdiff_t sstride, int h, int mx, int my)
  268. {
  269. register vector unsigned char perm;
  270. int i;
  271. register ptrdiff_t dstride2 = dstride << 1, sstride2 = sstride << 1;
  272. register ptrdiff_t dstride3 = dstride2 + dstride, sstride3 = sstride + sstride2;
  273. register ptrdiff_t dstride4 = dstride << 2, sstride4 = sstride << 2;
  274. #if HAVE_BIGENDIAN
  275. perm = vec_lvsl(0, src);
  276. #endif
  277. // hand-unrolling the loop by 4 gains about 15%
  278. // minimum execution time goes from 74 to 60 cycles
  279. // it's faster than -funroll-loops, but using
  280. // -funroll-loops w/ this is bad - 74 cycles again.
  281. // all this is on a 7450, tuning for the 7450
  282. for (i = 0; i < h; i += 4) {
  283. vec_st(load_with_perm_vec(0, src, perm), 0, dst);
  284. vec_st(load_with_perm_vec(sstride, src, perm), dstride, dst);
  285. vec_st(load_with_perm_vec(sstride2, src, perm), dstride2, dst);
  286. vec_st(load_with_perm_vec(sstride3, src, perm), dstride3, dst);
  287. src += sstride4;
  288. dst += dstride4;
  289. }
  290. }
  291. #endif /* HAVE_ALTIVEC */
  292. av_cold void ff_vp78dsp_init_ppc(VP8DSPContext *c)
  293. {
  294. #if HAVE_ALTIVEC
  295. if (!PPC_ALTIVEC(av_get_cpu_flags()))
  296. return;
  297. c->put_vp8_epel_pixels_tab[0][0][0] = put_vp8_pixels16_altivec;
  298. c->put_vp8_epel_pixels_tab[0][0][2] = put_vp8_epel16_h6_altivec;
  299. c->put_vp8_epel_pixels_tab[0][2][0] = put_vp8_epel16_v6_altivec;
  300. c->put_vp8_epel_pixels_tab[0][2][2] = put_vp8_epel16_h6v6_altivec;
  301. c->put_vp8_epel_pixels_tab[1][0][2] = put_vp8_epel8_h6_altivec;
  302. c->put_vp8_epel_pixels_tab[1][2][0] = put_vp8_epel8_v6_altivec;
  303. c->put_vp8_epel_pixels_tab[1][0][1] = put_vp8_epel8_h4_altivec;
  304. c->put_vp8_epel_pixels_tab[1][1][0] = put_vp8_epel8_v4_altivec;
  305. c->put_vp8_epel_pixels_tab[1][2][2] = put_vp8_epel8_h6v6_altivec;
  306. c->put_vp8_epel_pixels_tab[1][1][1] = put_vp8_epel8_h4v4_altivec;
  307. c->put_vp8_epel_pixels_tab[1][1][2] = put_vp8_epel8_h6v4_altivec;
  308. c->put_vp8_epel_pixels_tab[1][2][1] = put_vp8_epel8_h4v6_altivec;
  309. c->put_vp8_epel_pixels_tab[2][0][2] = put_vp8_epel4_h6_altivec;
  310. c->put_vp8_epel_pixels_tab[2][2][0] = put_vp8_epel4_v6_altivec;
  311. c->put_vp8_epel_pixels_tab[2][0][1] = put_vp8_epel4_h4_altivec;
  312. c->put_vp8_epel_pixels_tab[2][1][0] = put_vp8_epel4_v4_altivec;
  313. c->put_vp8_epel_pixels_tab[2][2][2] = put_vp8_epel4_h6v6_altivec;
  314. c->put_vp8_epel_pixels_tab[2][1][1] = put_vp8_epel4_h4v4_altivec;
  315. c->put_vp8_epel_pixels_tab[2][1][2] = put_vp8_epel4_h6v4_altivec;
  316. c->put_vp8_epel_pixels_tab[2][2][1] = put_vp8_epel4_h4v6_altivec;
  317. #endif /* HAVE_ALTIVEC */
  318. }