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.

246 lines
8.5KB

  1. /*
  2. * Copyright (c) 2001 Michel Lespinasse
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. */
  19. /*
  20. * NOTE: This code is based on GPL code from the libmpeg2 project. The
  21. * author, Michel Lespinasses, has given explicit permission to release
  22. * under LGPL as part of ffmpeg.
  23. *
  24. */
  25. /*
  26. * FFMpeg integration by Dieter Shirley
  27. *
  28. * This file is a direct copy of the altivec idct module from the libmpeg2
  29. * project. I've deleted all of the libmpeg2 specific code, renamed the functions and
  30. * re-ordered the function parameters. The only change to the IDCT function
  31. * itself was to factor out the partial transposition, and to perform a full
  32. * transpose at the end of the function.
  33. */
  34. #include <stdlib.h> /* malloc(), free() */
  35. #include <string.h>
  36. #include "../dsputil.h"
  37. #include "gcc_fixes.h"
  38. #include "dsputil_altivec.h"
  39. #define vector_s16_t vector signed short
  40. #define vector_u16_t vector unsigned short
  41. #define vector_s8_t vector signed char
  42. #define vector_u8_t vector unsigned char
  43. #define vector_s32_t vector signed int
  44. #define vector_u32_t vector unsigned int
  45. #define IDCT_HALF \
  46. /* 1st stage */ \
  47. t1 = vec_mradds (a1, vx7, vx1 ); \
  48. t8 = vec_mradds (a1, vx1, vec_subs (zero, vx7)); \
  49. t7 = vec_mradds (a2, vx5, vx3); \
  50. t3 = vec_mradds (ma2, vx3, vx5); \
  51. \
  52. /* 2nd stage */ \
  53. t5 = vec_adds (vx0, vx4); \
  54. t0 = vec_subs (vx0, vx4); \
  55. t2 = vec_mradds (a0, vx6, vx2); \
  56. t4 = vec_mradds (a0, vx2, vec_subs (zero, vx6)); \
  57. t6 = vec_adds (t8, t3); \
  58. t3 = vec_subs (t8, t3); \
  59. t8 = vec_subs (t1, t7); \
  60. t1 = vec_adds (t1, t7); \
  61. \
  62. /* 3rd stage */ \
  63. t7 = vec_adds (t5, t2); \
  64. t2 = vec_subs (t5, t2); \
  65. t5 = vec_adds (t0, t4); \
  66. t0 = vec_subs (t0, t4); \
  67. t4 = vec_subs (t8, t3); \
  68. t3 = vec_adds (t8, t3); \
  69. \
  70. /* 4th stage */ \
  71. vy0 = vec_adds (t7, t1); \
  72. vy7 = vec_subs (t7, t1); \
  73. vy1 = vec_mradds (c4, t3, t5); \
  74. vy6 = vec_mradds (mc4, t3, t5); \
  75. vy2 = vec_mradds (c4, t4, t0); \
  76. vy5 = vec_mradds (mc4, t4, t0); \
  77. vy3 = vec_adds (t2, t6); \
  78. vy4 = vec_subs (t2, t6);
  79. #define IDCT \
  80. vector_s16_t vx0, vx1, vx2, vx3, vx4, vx5, vx6, vx7; \
  81. vector_s16_t vy0, vy1, vy2, vy3, vy4, vy5, vy6, vy7; \
  82. vector_s16_t a0, a1, a2, ma2, c4, mc4, zero, bias; \
  83. vector_s16_t t0, t1, t2, t3, t4, t5, t6, t7, t8; \
  84. vector_u16_t shift; \
  85. \
  86. c4 = vec_splat (constants[0], 0); \
  87. a0 = vec_splat (constants[0], 1); \
  88. a1 = vec_splat (constants[0], 2); \
  89. a2 = vec_splat (constants[0], 3); \
  90. mc4 = vec_splat (constants[0], 4); \
  91. ma2 = vec_splat (constants[0], 5); \
  92. bias = (vector_s16_t)vec_splat ((vector_s32_t)constants[0], 3); \
  93. \
  94. zero = vec_splat_s16 (0); \
  95. shift = vec_splat_u16 (4); \
  96. \
  97. vx0 = vec_mradds (vec_sl (block[0], shift), constants[1], zero); \
  98. vx1 = vec_mradds (vec_sl (block[1], shift), constants[2], zero); \
  99. vx2 = vec_mradds (vec_sl (block[2], shift), constants[3], zero); \
  100. vx3 = vec_mradds (vec_sl (block[3], shift), constants[4], zero); \
  101. vx4 = vec_mradds (vec_sl (block[4], shift), constants[1], zero); \
  102. vx5 = vec_mradds (vec_sl (block[5], shift), constants[4], zero); \
  103. vx6 = vec_mradds (vec_sl (block[6], shift), constants[3], zero); \
  104. vx7 = vec_mradds (vec_sl (block[7], shift), constants[2], zero); \
  105. \
  106. IDCT_HALF \
  107. \
  108. vx0 = vec_mergeh (vy0, vy4); \
  109. vx1 = vec_mergel (vy0, vy4); \
  110. vx2 = vec_mergeh (vy1, vy5); \
  111. vx3 = vec_mergel (vy1, vy5); \
  112. vx4 = vec_mergeh (vy2, vy6); \
  113. vx5 = vec_mergel (vy2, vy6); \
  114. vx6 = vec_mergeh (vy3, vy7); \
  115. vx7 = vec_mergel (vy3, vy7); \
  116. \
  117. vy0 = vec_mergeh (vx0, vx4); \
  118. vy1 = vec_mergel (vx0, vx4); \
  119. vy2 = vec_mergeh (vx1, vx5); \
  120. vy3 = vec_mergel (vx1, vx5); \
  121. vy4 = vec_mergeh (vx2, vx6); \
  122. vy5 = vec_mergel (vx2, vx6); \
  123. vy6 = vec_mergeh (vx3, vx7); \
  124. vy7 = vec_mergel (vx3, vx7); \
  125. \
  126. vx0 = vec_adds (vec_mergeh (vy0, vy4), bias); \
  127. vx1 = vec_mergel (vy0, vy4); \
  128. vx2 = vec_mergeh (vy1, vy5); \
  129. vx3 = vec_mergel (vy1, vy5); \
  130. vx4 = vec_mergeh (vy2, vy6); \
  131. vx5 = vec_mergel (vy2, vy6); \
  132. vx6 = vec_mergeh (vy3, vy7); \
  133. vx7 = vec_mergel (vy3, vy7); \
  134. \
  135. IDCT_HALF \
  136. \
  137. shift = vec_splat_u16 (6); \
  138. vx0 = vec_sra (vy0, shift); \
  139. vx1 = vec_sra (vy1, shift); \
  140. vx2 = vec_sra (vy2, shift); \
  141. vx3 = vec_sra (vy3, shift); \
  142. vx4 = vec_sra (vy4, shift); \
  143. vx5 = vec_sra (vy5, shift); \
  144. vx6 = vec_sra (vy6, shift); \
  145. vx7 = vec_sra (vy7, shift);
  146. static const vector_s16_t constants[5] = {
  147. (vector_s16_t) AVV(23170, 13573, 6518, 21895, -23170, -21895, 32, 31),
  148. (vector_s16_t) AVV(16384, 22725, 21407, 19266, 16384, 19266, 21407, 22725),
  149. (vector_s16_t) AVV(22725, 31521, 29692, 26722, 22725, 26722, 29692, 31521),
  150. (vector_s16_t) AVV(21407, 29692, 27969, 25172, 21407, 25172, 27969, 29692),
  151. (vector_s16_t) AVV(19266, 26722, 25172, 22654, 19266, 22654, 25172, 26722)
  152. };
  153. void idct_put_altivec(uint8_t* dest, int stride, vector_s16_t* block)
  154. {
  155. POWERPC_PERF_DECLARE(altivec_idct_put_num, 1);
  156. #ifdef ALTIVEC_USE_REFERENCE_C_CODE
  157. POWERPC_PERF_START_COUNT(altivec_idct_put_num, 1);
  158. void simple_idct_put(uint8_t *dest, int line_size, int16_t *block);
  159. simple_idct_put(dest, stride, (int16_t*)block);
  160. POWERPC_PERF_STOP_COUNT(altivec_idct_put_num, 1);
  161. #else /* ALTIVEC_USE_REFERENCE_C_CODE */
  162. vector_u8_t tmp;
  163. POWERPC_PERF_START_COUNT(altivec_idct_put_num, 1);
  164. IDCT
  165. #define COPY(dest,src) \
  166. tmp = vec_packsu (src, src); \
  167. vec_ste ((vector_u32_t)tmp, 0, (unsigned int *)dest); \
  168. vec_ste ((vector_u32_t)tmp, 4, (unsigned int *)dest);
  169. COPY (dest, vx0) dest += stride;
  170. COPY (dest, vx1) dest += stride;
  171. COPY (dest, vx2) dest += stride;
  172. COPY (dest, vx3) dest += stride;
  173. COPY (dest, vx4) dest += stride;
  174. COPY (dest, vx5) dest += stride;
  175. COPY (dest, vx6) dest += stride;
  176. COPY (dest, vx7)
  177. POWERPC_PERF_STOP_COUNT(altivec_idct_put_num, 1);
  178. #endif /* ALTIVEC_USE_REFERENCE_C_CODE */
  179. }
  180. void idct_add_altivec(uint8_t* dest, int stride, vector_s16_t* block)
  181. {
  182. POWERPC_PERF_DECLARE(altivec_idct_add_num, 1);
  183. #ifdef ALTIVEC_USE_REFERENCE_C_CODE
  184. POWERPC_PERF_START_COUNT(altivec_idct_add_num, 1);
  185. void simple_idct_add(uint8_t *dest, int line_size, int16_t *block);
  186. simple_idct_add(dest, stride, (int16_t*)block);
  187. POWERPC_PERF_STOP_COUNT(altivec_idct_add_num, 1);
  188. #else /* ALTIVEC_USE_REFERENCE_C_CODE */
  189. vector_u8_t tmp;
  190. vector_s16_t tmp2, tmp3;
  191. vector_u8_t perm0;
  192. vector_u8_t perm1;
  193. vector_u8_t p0, p1, p;
  194. POWERPC_PERF_START_COUNT(altivec_idct_add_num, 1);
  195. IDCT
  196. p0 = vec_lvsl (0, dest);
  197. p1 = vec_lvsl (stride, dest);
  198. p = vec_splat_u8 (-1);
  199. perm0 = vec_mergeh (p, p0);
  200. perm1 = vec_mergeh (p, p1);
  201. #define ADD(dest,src,perm) \
  202. /* *(uint64_t *)&tmp = *(uint64_t *)dest; */ \
  203. tmp = vec_ld (0, dest); \
  204. tmp2 = (vector_s16_t)vec_perm (tmp, (vector_u8_t)zero, perm); \
  205. tmp3 = vec_adds (tmp2, src); \
  206. tmp = vec_packsu (tmp3, tmp3); \
  207. vec_ste ((vector_u32_t)tmp, 0, (unsigned int *)dest); \
  208. vec_ste ((vector_u32_t)tmp, 4, (unsigned int *)dest);
  209. ADD (dest, vx0, perm0) dest += stride;
  210. ADD (dest, vx1, perm1) dest += stride;
  211. ADD (dest, vx2, perm0) dest += stride;
  212. ADD (dest, vx3, perm1) dest += stride;
  213. ADD (dest, vx4, perm0) dest += stride;
  214. ADD (dest, vx5, perm1) dest += stride;
  215. ADD (dest, vx6, perm0) dest += stride;
  216. ADD (dest, vx7, perm1)
  217. POWERPC_PERF_STOP_COUNT(altivec_idct_add_num, 1);
  218. #endif /* ALTIVEC_USE_REFERENCE_C_CODE */
  219. }