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.

192 lines
5.4KB

  1. /*
  2. * Copyright (C) 2009 David Conrad
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <string.h>
  21. #include "config.h"
  22. #include "libavutil/attributes.h"
  23. #include "libavutil/cpu.h"
  24. #include "libavcodec/vp3dsp.h"
  25. #if HAVE_ALTIVEC
  26. #include "libavutil/ppc/types_altivec.h"
  27. #include "libavutil/ppc/util_altivec.h"
  28. #include "libavcodec/dsputil.h"
  29. #include "dsputil_altivec.h"
  30. static const vec_s16 constants =
  31. {0, 64277, 60547, 54491, 46341, 36410, 25080, 12785};
  32. static const vec_u8 interleave_high =
  33. {0, 1, 16, 17, 4, 5, 20, 21, 8, 9, 24, 25, 12, 13, 28, 29};
  34. #define IDCT_START \
  35. vec_s16 A, B, C, D, Ad, Bd, Cd, Dd, E, F, G, H;\
  36. vec_s16 Ed, Gd, Add, Bdd, Fd, Hd;\
  37. vec_s16 eight = vec_splat_s16(8);\
  38. vec_u16 four = vec_splat_u16(4);\
  39. \
  40. vec_s16 C1 = vec_splat(constants, 1);\
  41. vec_s16 C2 = vec_splat(constants, 2);\
  42. vec_s16 C3 = vec_splat(constants, 3);\
  43. vec_s16 C4 = vec_splat(constants, 4);\
  44. vec_s16 C5 = vec_splat(constants, 5);\
  45. vec_s16 C6 = vec_splat(constants, 6);\
  46. vec_s16 C7 = vec_splat(constants, 7);\
  47. \
  48. vec_s16 b0 = vec_ld(0x00, block);\
  49. vec_s16 b1 = vec_ld(0x10, block);\
  50. vec_s16 b2 = vec_ld(0x20, block);\
  51. vec_s16 b3 = vec_ld(0x30, block);\
  52. vec_s16 b4 = vec_ld(0x40, block);\
  53. vec_s16 b5 = vec_ld(0x50, block);\
  54. vec_s16 b6 = vec_ld(0x60, block);\
  55. vec_s16 b7 = vec_ld(0x70, block);
  56. // these functions do (a*C)>>16
  57. // things are tricky because a is signed, but C unsigned.
  58. // M15 is used if C fits in 15 bit unsigned (C6,C7)
  59. // M16 is used if C requires 16 bits unsigned
  60. static inline vec_s16 M15(vec_s16 a, vec_s16 C)
  61. {
  62. return (vec_s16)vec_perm(vec_mule(a,C), vec_mulo(a,C), interleave_high);
  63. }
  64. static inline vec_s16 M16(vec_s16 a, vec_s16 C)
  65. {
  66. return vec_add(a, M15(a, C));
  67. }
  68. #define IDCT_1D(ADD, SHIFT)\
  69. A = vec_add(M16(b1, C1), M15(b7, C7));\
  70. B = vec_sub(M15(b1, C7), M16(b7, C1));\
  71. C = vec_add(M16(b3, C3), M16(b5, C5));\
  72. D = vec_sub(M16(b5, C3), M16(b3, C5));\
  73. \
  74. Ad = M16(vec_sub(A, C), C4);\
  75. Bd = M16(vec_sub(B, D), C4);\
  76. \
  77. Cd = vec_add(A, C);\
  78. Dd = vec_add(B, D);\
  79. \
  80. E = ADD(M16(vec_add(b0, b4), C4));\
  81. F = ADD(M16(vec_sub(b0, b4), C4));\
  82. \
  83. G = vec_add(M16(b2, C2), M15(b6, C6));\
  84. H = vec_sub(M15(b2, C6), M16(b6, C2));\
  85. \
  86. Ed = vec_sub(E, G);\
  87. Gd = vec_add(E, G);\
  88. \
  89. Add = vec_add(F, Ad);\
  90. Bdd = vec_sub(Bd, H);\
  91. \
  92. Fd = vec_sub(F, Ad);\
  93. Hd = vec_add(Bd, H);\
  94. \
  95. b0 = SHIFT(vec_add(Gd, Cd));\
  96. b7 = SHIFT(vec_sub(Gd, Cd));\
  97. \
  98. b1 = SHIFT(vec_add(Add, Hd));\
  99. b2 = SHIFT(vec_sub(Add, Hd));\
  100. \
  101. b3 = SHIFT(vec_add(Ed, Dd));\
  102. b4 = SHIFT(vec_sub(Ed, Dd));\
  103. \
  104. b5 = SHIFT(vec_add(Fd, Bdd));\
  105. b6 = SHIFT(vec_sub(Fd, Bdd));
  106. #define NOP(a) a
  107. #define ADD8(a) vec_add(a, eight)
  108. #define SHIFT4(a) vec_sra(a, four)
  109. static void vp3_idct_put_altivec(uint8_t *dst, int stride, int16_t block[64])
  110. {
  111. vec_u8 t;
  112. IDCT_START
  113. // pixels are signed; so add 128*16 in addition to the normal 8
  114. vec_s16 v2048 = vec_sl(vec_splat_s16(1), vec_splat_u16(11));
  115. eight = vec_add(eight, v2048);
  116. IDCT_1D(NOP, NOP)
  117. TRANSPOSE8(b0, b1, b2, b3, b4, b5, b6, b7);
  118. IDCT_1D(ADD8, SHIFT4)
  119. #define PUT(a)\
  120. t = vec_packsu(a, a);\
  121. vec_ste((vec_u32)t, 0, (unsigned int *)dst);\
  122. vec_ste((vec_u32)t, 4, (unsigned int *)dst);
  123. PUT(b0) dst += stride;
  124. PUT(b1) dst += stride;
  125. PUT(b2) dst += stride;
  126. PUT(b3) dst += stride;
  127. PUT(b4) dst += stride;
  128. PUT(b5) dst += stride;
  129. PUT(b6) dst += stride;
  130. PUT(b7)
  131. memset(block, 0, sizeof(*block) * 64);
  132. }
  133. static void vp3_idct_add_altivec(uint8_t *dst, int stride, int16_t block[64])
  134. {
  135. LOAD_ZERO;
  136. vec_u8 t, vdst;
  137. vec_s16 vdst_16;
  138. vec_u8 vdst_mask = vec_mergeh(vec_splat_u8(-1), vec_lvsl(0, dst));
  139. IDCT_START
  140. IDCT_1D(NOP, NOP)
  141. TRANSPOSE8(b0, b1, b2, b3, b4, b5, b6, b7);
  142. IDCT_1D(ADD8, SHIFT4)
  143. #define ADD(a)\
  144. vdst = vec_ld(0, dst);\
  145. vdst_16 = (vec_s16)vec_perm(vdst, zero_u8v, vdst_mask);\
  146. vdst_16 = vec_adds(a, vdst_16);\
  147. t = vec_packsu(vdst_16, vdst_16);\
  148. vec_ste((vec_u32)t, 0, (unsigned int *)dst);\
  149. vec_ste((vec_u32)t, 4, (unsigned int *)dst);
  150. ADD(b0) dst += stride;
  151. ADD(b1) dst += stride;
  152. ADD(b2) dst += stride;
  153. ADD(b3) dst += stride;
  154. ADD(b4) dst += stride;
  155. ADD(b5) dst += stride;
  156. ADD(b6) dst += stride;
  157. ADD(b7)
  158. memset(block, 0, sizeof(*block) * 64);
  159. }
  160. #endif /* HAVE_ALTIVEC */
  161. av_cold void ff_vp3dsp_init_ppc(VP3DSPContext *c, int flags)
  162. {
  163. #if HAVE_ALTIVEC
  164. if (av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) {
  165. c->idct_put = vp3_idct_put_altivec;
  166. c->idct_add = vp3_idct_add_altivec;
  167. c->idct_perm = FF_TRANSPOSE_IDCT_PERM;
  168. }
  169. #endif
  170. }