You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

311 lines
11KB

  1. /*
  2. * VP8 compatible video decoder
  3. *
  4. * Copyright (C) 2010 David Conrad
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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/cpu.h"
  24. #include "libavutil/mem.h"
  25. #include "libavutil/ppc/types_altivec.h"
  26. #include "libavutil/ppc/util_altivec.h"
  27. #include "libavcodec/vp8dsp.h"
  28. #include "dsputil_altivec.h"
  29. #if HAVE_ALTIVEC
  30. #define REPT4(...) { __VA_ARGS__, __VA_ARGS__, __VA_ARGS__, __VA_ARGS__ }
  31. // h subpel filter uses msum to multiply+add 4 pixel taps at once
  32. static const vec_s8 h_subpel_filters_inner[7] =
  33. {
  34. REPT4( -6, 123, 12, -1),
  35. REPT4(-11, 108, 36, -8),
  36. REPT4( -9, 93, 50, -6),
  37. REPT4(-16, 77, 77, -16),
  38. REPT4( -6, 50, 93, -9),
  39. REPT4( -8, 36, 108, -11),
  40. REPT4( -1, 12, 123, -6),
  41. };
  42. // for 6tap filters, these are the outer two taps
  43. // The zeros mask off pixels 4-7 when filtering 0-3
  44. // and vice-versa
  45. static const vec_s8 h_subpel_filters_outer[3] =
  46. {
  47. REPT4(0, 0, 2, 1),
  48. REPT4(0, 0, 3, 3),
  49. REPT4(0, 0, 1, 2),
  50. };
  51. #define LOAD_H_SUBPEL_FILTER(i) \
  52. vec_s8 filter_inner = h_subpel_filters_inner[i]; \
  53. vec_s8 filter_outerh = h_subpel_filters_outer[(i)>>1]; \
  54. vec_s8 filter_outerl = vec_sld(filter_outerh, filter_outerh, 2)
  55. #define FILTER_H(dstv, off) \
  56. a = vec_ld((off)-is6tap-1, src); \
  57. b = vec_ld((off)-is6tap-1+15, src); \
  58. \
  59. pixh = vec_perm(a, b, permh##off); \
  60. pixl = vec_perm(a, b, perml##off); \
  61. filth = vec_msum(filter_inner, pixh, c64); \
  62. filtl = vec_msum(filter_inner, pixl, c64); \
  63. \
  64. if (is6tap) { \
  65. outer = vec_perm(a, b, perm_6tap##off); \
  66. filth = vec_msum(filter_outerh, outer, filth); \
  67. filtl = vec_msum(filter_outerl, outer, filtl); \
  68. } \
  69. if (w == 4) \
  70. filtl = filth; /* discard pixels 4-7 */ \
  71. dstv = vec_packs(filth, filtl); \
  72. dstv = vec_sra(dstv, c7)
  73. static av_always_inline
  74. void put_vp8_epel_h_altivec_core(uint8_t *dst, ptrdiff_t dst_stride,
  75. uint8_t *src, ptrdiff_t src_stride,
  76. int h, int mx, int w, int is6tap)
  77. {
  78. LOAD_H_SUBPEL_FILTER(mx-1);
  79. vec_u8 align_vec0, align_vec8, permh0, permh8, filt;
  80. vec_u8 perm_6tap0, perm_6tap8, perml0, perml8;
  81. vec_u8 a, b, pixh, pixl, outer;
  82. vec_s16 f16h, f16l;
  83. vec_s32 filth, filtl;
  84. vec_u8 perm_inner6 = { 1,2,3,4, 2,3,4,5, 3,4,5,6, 4,5,6,7 };
  85. vec_u8 perm_inner4 = { 0,1,2,3, 1,2,3,4, 2,3,4,5, 3,4,5,6 };
  86. vec_u8 perm_inner = is6tap ? perm_inner6 : perm_inner4;
  87. vec_u8 perm_outer = { 4,9, 0,5, 5,10, 1,6, 6,11, 2,7, 7,12, 3,8 };
  88. vec_s32 c64 = vec_sl(vec_splat_s32(1), vec_splat_u32(6));
  89. vec_u16 c7 = vec_splat_u16(7);
  90. align_vec0 = vec_lvsl( -is6tap-1, src);
  91. align_vec8 = vec_lvsl(8-is6tap-1, src);
  92. permh0 = vec_perm(align_vec0, align_vec0, perm_inner);
  93. permh8 = vec_perm(align_vec8, align_vec8, perm_inner);
  94. perm_inner = vec_add(perm_inner, vec_splat_u8(4));
  95. perml0 = vec_perm(align_vec0, align_vec0, perm_inner);
  96. perml8 = vec_perm(align_vec8, align_vec8, perm_inner);
  97. perm_6tap0 = vec_perm(align_vec0, align_vec0, perm_outer);
  98. perm_6tap8 = vec_perm(align_vec8, align_vec8, perm_outer);
  99. while (h --> 0) {
  100. FILTER_H(f16h, 0);
  101. if (w == 16) {
  102. FILTER_H(f16l, 8);
  103. filt = vec_packsu(f16h, f16l);
  104. vec_st(filt, 0, dst);
  105. } else {
  106. filt = vec_packsu(f16h, f16h);
  107. vec_ste((vec_u32)filt, 0, (uint32_t*)dst);
  108. if (w == 8)
  109. vec_ste((vec_u32)filt, 4, (uint32_t*)dst);
  110. }
  111. src += src_stride;
  112. dst += dst_stride;
  113. }
  114. }
  115. // v subpel filter does a simple vertical multiply + add
  116. static const vec_u8 v_subpel_filters[7] =
  117. {
  118. { 0, 6, 123, 12, 1, 0 },
  119. { 2, 11, 108, 36, 8, 1 },
  120. { 0, 9, 93, 50, 6, 0 },
  121. { 3, 16, 77, 77, 16, 3 },
  122. { 0, 6, 50, 93, 9, 0 },
  123. { 1, 8, 36, 108, 11, 2 },
  124. { 0, 1, 12, 123, 6, 0 },
  125. };
  126. #define LOAD_V_SUBPEL_FILTER(i) \
  127. vec_u8 subpel_filter = v_subpel_filters[i]; \
  128. vec_u8 f0 = vec_splat(subpel_filter, 0); \
  129. vec_u8 f1 = vec_splat(subpel_filter, 1); \
  130. vec_u8 f2 = vec_splat(subpel_filter, 2); \
  131. vec_u8 f3 = vec_splat(subpel_filter, 3); \
  132. vec_u8 f4 = vec_splat(subpel_filter, 4); \
  133. vec_u8 f5 = vec_splat(subpel_filter, 5)
  134. #define FILTER_V(dstv, vec_mul) \
  135. s1f = (vec_s16)vec_mul(s1, f1); \
  136. s2f = (vec_s16)vec_mul(s2, f2); \
  137. s3f = (vec_s16)vec_mul(s3, f3); \
  138. s4f = (vec_s16)vec_mul(s4, f4); \
  139. s2f = vec_subs(s2f, s1f); \
  140. s3f = vec_subs(s3f, s4f); \
  141. if (is6tap) { \
  142. s0f = (vec_s16)vec_mul(s0, f0); \
  143. s5f = (vec_s16)vec_mul(s5, f5); \
  144. s2f = vec_adds(s2f, s0f); \
  145. s3f = vec_adds(s3f, s5f); \
  146. } \
  147. dstv = vec_adds(s2f, s3f); \
  148. dstv = vec_adds(dstv, c64); \
  149. dstv = vec_sra(dstv, c7)
  150. static av_always_inline
  151. void put_vp8_epel_v_altivec_core(uint8_t *dst, ptrdiff_t dst_stride,
  152. uint8_t *src, ptrdiff_t src_stride,
  153. int h, int my, int w, int is6tap)
  154. {
  155. LOAD_V_SUBPEL_FILTER(my-1);
  156. vec_u8 s0, s1, s2, s3, s4, s5, filt, align_vech, perm_vec, align_vecl;
  157. vec_s16 s0f, s1f, s2f, s3f, s4f, s5f, f16h, f16l;
  158. vec_s16 c64 = vec_sl(vec_splat_s16(1), vec_splat_u16(6));
  159. vec_u16 c7 = vec_splat_u16(7);
  160. // we want pixels 0-7 to be in the even positions and 8-15 in the odd,
  161. // so combine this permute with the alignment permute vector
  162. align_vech = vec_lvsl(0, src);
  163. align_vecl = vec_sld(align_vech, align_vech, 8);
  164. if (w ==16)
  165. perm_vec = vec_mergeh(align_vech, align_vecl);
  166. else
  167. perm_vec = vec_mergeh(align_vech, align_vech);
  168. if (is6tap)
  169. s0 = load_with_perm_vec(-2*src_stride, src, perm_vec);
  170. s1 = load_with_perm_vec(-1*src_stride, src, perm_vec);
  171. s2 = load_with_perm_vec( 0*src_stride, src, perm_vec);
  172. s3 = load_with_perm_vec( 1*src_stride, src, perm_vec);
  173. if (is6tap)
  174. s4 = load_with_perm_vec( 2*src_stride, src, perm_vec);
  175. src += (2+is6tap)*src_stride;
  176. while (h --> 0) {
  177. if (is6tap)
  178. s5 = load_with_perm_vec(0, src, perm_vec);
  179. else
  180. s4 = load_with_perm_vec(0, src, perm_vec);
  181. FILTER_V(f16h, vec_mule);
  182. if (w == 16) {
  183. FILTER_V(f16l, vec_mulo);
  184. filt = vec_packsu(f16h, f16l);
  185. vec_st(filt, 0, dst);
  186. } else {
  187. filt = vec_packsu(f16h, f16h);
  188. if (w == 4)
  189. filt = (vec_u8)vec_splat((vec_u32)filt, 0);
  190. else
  191. vec_ste((vec_u32)filt, 4, (uint32_t*)dst);
  192. vec_ste((vec_u32)filt, 0, (uint32_t*)dst);
  193. }
  194. if (is6tap)
  195. s0 = s1;
  196. s1 = s2;
  197. s2 = s3;
  198. s3 = s4;
  199. if (is6tap)
  200. s4 = s5;
  201. dst += dst_stride;
  202. src += src_stride;
  203. }
  204. }
  205. #define EPEL_FUNCS(WIDTH, TAPS) \
  206. static av_noinline \
  207. void put_vp8_epel ## WIDTH ## _h ## TAPS ## _altivec(uint8_t *dst, ptrdiff_t dst_stride, uint8_t *src, ptrdiff_t src_stride, int h, int mx, int my) \
  208. { \
  209. put_vp8_epel_h_altivec_core(dst, dst_stride, src, src_stride, h, mx, WIDTH, TAPS == 6); \
  210. } \
  211. \
  212. static av_noinline \
  213. void put_vp8_epel ## WIDTH ## _v ## TAPS ## _altivec(uint8_t *dst, ptrdiff_t dst_stride, uint8_t *src, ptrdiff_t src_stride, int h, int mx, int my) \
  214. { \
  215. put_vp8_epel_v_altivec_core(dst, dst_stride, src, src_stride, h, my, WIDTH, TAPS == 6); \
  216. }
  217. #define EPEL_HV(WIDTH, HTAPS, VTAPS) \
  218. static void put_vp8_epel ## WIDTH ## _h ## HTAPS ## v ## VTAPS ## _altivec(uint8_t *dst, ptrdiff_t stride, uint8_t *src, ptrdiff_t s, int h, int mx, int my) \
  219. { \
  220. DECLARE_ALIGNED(16, uint8_t, tmp)[(2*WIDTH+5)*16]; \
  221. if (VTAPS == 6) { \
  222. put_vp8_epel ## WIDTH ## _h ## HTAPS ## _altivec(tmp, 16, src-2*stride, stride, h+5, mx, my); \
  223. put_vp8_epel ## WIDTH ## _v ## VTAPS ## _altivec(dst, stride, tmp+2*16, 16, h, mx, my); \
  224. } else { \
  225. put_vp8_epel ## WIDTH ## _h ## HTAPS ## _altivec(tmp, 16, src-stride, stride, h+4, mx, my); \
  226. put_vp8_epel ## WIDTH ## _v ## VTAPS ## _altivec(dst, stride, tmp+16, 16, h, mx, my); \
  227. } \
  228. }
  229. EPEL_FUNCS(16,6)
  230. EPEL_FUNCS(8, 6)
  231. EPEL_FUNCS(8, 4)
  232. EPEL_FUNCS(4, 6)
  233. EPEL_FUNCS(4, 4)
  234. EPEL_HV(16, 6,6)
  235. EPEL_HV(8, 6,6)
  236. EPEL_HV(8, 4,6)
  237. EPEL_HV(8, 6,4)
  238. EPEL_HV(8, 4,4)
  239. EPEL_HV(4, 6,6)
  240. EPEL_HV(4, 4,6)
  241. EPEL_HV(4, 6,4)
  242. EPEL_HV(4, 4,4)
  243. static void put_vp8_pixels16_altivec(uint8_t *dst, ptrdiff_t stride, uint8_t *src, ptrdiff_t s, int h, int mx, int my)
  244. {
  245. ff_put_pixels16_altivec(dst, src, stride, h);
  246. }
  247. #endif /* HAVE_ALTIVEC */
  248. av_cold void ff_vp8dsp_init_ppc(VP8DSPContext *c)
  249. {
  250. #if HAVE_ALTIVEC
  251. if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
  252. return;
  253. c->put_vp8_epel_pixels_tab[0][0][0] = put_vp8_pixels16_altivec;
  254. c->put_vp8_epel_pixels_tab[0][0][2] = put_vp8_epel16_h6_altivec;
  255. c->put_vp8_epel_pixels_tab[0][2][0] = put_vp8_epel16_v6_altivec;
  256. c->put_vp8_epel_pixels_tab[0][2][2] = put_vp8_epel16_h6v6_altivec;
  257. c->put_vp8_epel_pixels_tab[1][0][2] = put_vp8_epel8_h6_altivec;
  258. c->put_vp8_epel_pixels_tab[1][2][0] = put_vp8_epel8_v6_altivec;
  259. c->put_vp8_epel_pixels_tab[1][0][1] = put_vp8_epel8_h4_altivec;
  260. c->put_vp8_epel_pixels_tab[1][1][0] = put_vp8_epel8_v4_altivec;
  261. c->put_vp8_epel_pixels_tab[1][2][2] = put_vp8_epel8_h6v6_altivec;
  262. c->put_vp8_epel_pixels_tab[1][1][1] = put_vp8_epel8_h4v4_altivec;
  263. c->put_vp8_epel_pixels_tab[1][1][2] = put_vp8_epel8_h6v4_altivec;
  264. c->put_vp8_epel_pixels_tab[1][2][1] = put_vp8_epel8_h4v6_altivec;
  265. c->put_vp8_epel_pixels_tab[2][0][2] = put_vp8_epel4_h6_altivec;
  266. c->put_vp8_epel_pixels_tab[2][2][0] = put_vp8_epel4_v6_altivec;
  267. c->put_vp8_epel_pixels_tab[2][0][1] = put_vp8_epel4_h4_altivec;
  268. c->put_vp8_epel_pixels_tab[2][1][0] = put_vp8_epel4_v4_altivec;
  269. c->put_vp8_epel_pixels_tab[2][2][2] = put_vp8_epel4_h6v6_altivec;
  270. c->put_vp8_epel_pixels_tab[2][1][1] = put_vp8_epel4_h4v4_altivec;
  271. c->put_vp8_epel_pixels_tab[2][1][2] = put_vp8_epel4_h6v4_altivec;
  272. c->put_vp8_epel_pixels_tab[2][2][1] = put_vp8_epel4_h4v6_altivec;
  273. #endif /* HAVE_ALTIVEC */
  274. }