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.

254 lines
12KB

  1. /*
  2. * x86-optimized AC-3 DSP utils
  3. * Copyright (c) 2011 Justin Ruggles
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/attributes.h"
  22. #include "libavutil/mem.h"
  23. #include "libavutil/x86/asm.h"
  24. #include "libavutil/x86/cpu.h"
  25. #include "dsputil_x86.h"
  26. #include "libavcodec/ac3.h"
  27. #include "libavcodec/ac3dsp.h"
  28. void ff_ac3_exponent_min_mmx (uint8_t *exp, int num_reuse_blocks, int nb_coefs);
  29. void ff_ac3_exponent_min_mmxext(uint8_t *exp, int num_reuse_blocks, int nb_coefs);
  30. void ff_ac3_exponent_min_sse2 (uint8_t *exp, int num_reuse_blocks, int nb_coefs);
  31. int ff_ac3_max_msb_abs_int16_mmx (const int16_t *src, int len);
  32. int ff_ac3_max_msb_abs_int16_mmxext(const int16_t *src, int len);
  33. int ff_ac3_max_msb_abs_int16_sse2 (const int16_t *src, int len);
  34. int ff_ac3_max_msb_abs_int16_ssse3(const int16_t *src, int len);
  35. void ff_ac3_lshift_int16_mmx (int16_t *src, unsigned int len, unsigned int shift);
  36. void ff_ac3_lshift_int16_sse2(int16_t *src, unsigned int len, unsigned int shift);
  37. void ff_ac3_rshift_int32_mmx (int32_t *src, unsigned int len, unsigned int shift);
  38. void ff_ac3_rshift_int32_sse2(int32_t *src, unsigned int len, unsigned int shift);
  39. void ff_float_to_fixed24_3dnow(int32_t *dst, const float *src, unsigned int len);
  40. void ff_float_to_fixed24_sse (int32_t *dst, const float *src, unsigned int len);
  41. void ff_float_to_fixed24_sse2 (int32_t *dst, const float *src, unsigned int len);
  42. int ff_ac3_compute_mantissa_size_sse2(uint16_t mant_cnt[6][16]);
  43. void ff_ac3_extract_exponents_3dnow(uint8_t *exp, int32_t *coef, int nb_coefs);
  44. void ff_ac3_extract_exponents_sse2 (uint8_t *exp, int32_t *coef, int nb_coefs);
  45. void ff_ac3_extract_exponents_ssse3(uint8_t *exp, int32_t *coef, int nb_coefs);
  46. void ff_apply_window_int16_round_mmxext(int16_t *output, const int16_t *input,
  47. const int16_t *window, unsigned int len);
  48. void ff_apply_window_int16_round_sse2(int16_t *output, const int16_t *input,
  49. const int16_t *window, unsigned int len);
  50. void ff_apply_window_int16_mmxext(int16_t *output, const int16_t *input,
  51. const int16_t *window, unsigned int len);
  52. void ff_apply_window_int16_sse2(int16_t *output, const int16_t *input,
  53. const int16_t *window, unsigned int len);
  54. void ff_apply_window_int16_ssse3(int16_t *output, const int16_t *input,
  55. const int16_t *window, unsigned int len);
  56. void ff_apply_window_int16_ssse3_atom(int16_t *output, const int16_t *input,
  57. const int16_t *window, unsigned int len);
  58. #if HAVE_SSE_INLINE && HAVE_7REGS
  59. #define IF1(x) x
  60. #define IF0(x)
  61. #define MIX5(mono, stereo) \
  62. __asm__ volatile ( \
  63. "movss 0(%1), %%xmm5 \n" \
  64. "movss 8(%1), %%xmm6 \n" \
  65. "movss 24(%1), %%xmm7 \n" \
  66. "shufps $0, %%xmm5, %%xmm5 \n" \
  67. "shufps $0, %%xmm6, %%xmm6 \n" \
  68. "shufps $0, %%xmm7, %%xmm7 \n" \
  69. "1: \n" \
  70. "movaps (%0, %2), %%xmm0 \n" \
  71. "movaps (%0, %3), %%xmm1 \n" \
  72. "movaps (%0, %4), %%xmm2 \n" \
  73. "movaps (%0, %5), %%xmm3 \n" \
  74. "movaps (%0, %6), %%xmm4 \n" \
  75. "mulps %%xmm5, %%xmm0 \n" \
  76. "mulps %%xmm6, %%xmm1 \n" \
  77. "mulps %%xmm5, %%xmm2 \n" \
  78. "mulps %%xmm7, %%xmm3 \n" \
  79. "mulps %%xmm7, %%xmm4 \n" \
  80. stereo("addps %%xmm1, %%xmm0 \n") \
  81. "addps %%xmm1, %%xmm2 \n" \
  82. "addps %%xmm3, %%xmm0 \n" \
  83. "addps %%xmm4, %%xmm2 \n" \
  84. mono("addps %%xmm2, %%xmm0 \n") \
  85. "movaps %%xmm0, (%0, %2) \n" \
  86. stereo("movaps %%xmm2, (%0, %3) \n") \
  87. "add $16, %0 \n" \
  88. "jl 1b \n" \
  89. : "+&r"(i) \
  90. : "r"(matrix), \
  91. "r"(samples[0] + len), \
  92. "r"(samples[1] + len), \
  93. "r"(samples[2] + len), \
  94. "r"(samples[3] + len), \
  95. "r"(samples[4] + len) \
  96. : XMM_CLOBBERS("%xmm0", "%xmm1", "%xmm2", "%xmm3", \
  97. "%xmm4", "%xmm5", "%xmm6", "%xmm7",) \
  98. "memory" \
  99. );
  100. #define MIX_MISC(stereo) \
  101. __asm__ volatile ( \
  102. "mov %5, %2 \n" \
  103. "1: \n" \
  104. "mov -%c7(%6, %2, %c8), %3 \n" \
  105. "movaps (%3, %0), %%xmm0 \n" \
  106. stereo("movaps %%xmm0, %%xmm1 \n") \
  107. "mulps %%xmm4, %%xmm0 \n" \
  108. stereo("mulps %%xmm5, %%xmm1 \n") \
  109. "2: \n" \
  110. "mov (%6, %2, %c8), %1 \n" \
  111. "movaps (%1, %0), %%xmm2 \n" \
  112. stereo("movaps %%xmm2, %%xmm3 \n") \
  113. "mulps (%4, %2, 8), %%xmm2 \n" \
  114. stereo("mulps 16(%4, %2, 8), %%xmm3 \n") \
  115. "addps %%xmm2, %%xmm0 \n" \
  116. stereo("addps %%xmm3, %%xmm1 \n") \
  117. "add $4, %2 \n" \
  118. "jl 2b \n" \
  119. "mov %5, %2 \n" \
  120. stereo("mov (%6, %2, %c8), %1 \n") \
  121. "movaps %%xmm0, (%3, %0) \n" \
  122. stereo("movaps %%xmm1, (%1, %0) \n") \
  123. "add $16, %0 \n" \
  124. "jl 1b \n" \
  125. : "+&r"(i), "=&r"(j), "=&r"(k), "=&r"(m) \
  126. : "r"(matrix_simd + in_ch), \
  127. "g"((intptr_t) - 4 * (in_ch - 1)), \
  128. "r"(samp + in_ch), \
  129. "i"(sizeof(float *)), "i"(sizeof(float *)/4) \
  130. : "memory" \
  131. );
  132. static void ac3_downmix_sse(float **samples, float (*matrix)[2],
  133. int out_ch, int in_ch, int len)
  134. {
  135. int (*matrix_cmp)[2] = (int(*)[2])matrix;
  136. intptr_t i, j, k, m;
  137. i = -len * sizeof(float);
  138. if (in_ch == 5 && out_ch == 2 &&
  139. !(matrix_cmp[0][1] | matrix_cmp[2][0] |
  140. matrix_cmp[3][1] | matrix_cmp[4][0] |
  141. (matrix_cmp[1][0] ^ matrix_cmp[1][1]) |
  142. (matrix_cmp[0][0] ^ matrix_cmp[2][1]))) {
  143. MIX5(IF0, IF1);
  144. } else if (in_ch == 5 && out_ch == 1 &&
  145. matrix_cmp[0][0] == matrix_cmp[2][0] &&
  146. matrix_cmp[3][0] == matrix_cmp[4][0]) {
  147. MIX5(IF1, IF0);
  148. } else {
  149. DECLARE_ALIGNED(16, float, matrix_simd)[AC3_MAX_CHANNELS][2][4];
  150. float *samp[AC3_MAX_CHANNELS];
  151. for (j = 0; j < in_ch; j++)
  152. samp[j] = samples[j] + len;
  153. j = 2 * in_ch * sizeof(float);
  154. __asm__ volatile (
  155. "1: \n"
  156. "sub $8, %0 \n"
  157. "movss (%2, %0), %%xmm4 \n"
  158. "movss 4(%2, %0), %%xmm5 \n"
  159. "shufps $0, %%xmm4, %%xmm4 \n"
  160. "shufps $0, %%xmm5, %%xmm5 \n"
  161. "movaps %%xmm4, (%1, %0, 4) \n"
  162. "movaps %%xmm5, 16(%1, %0, 4) \n"
  163. "jg 1b \n"
  164. : "+&r"(j)
  165. : "r"(matrix_simd), "r"(matrix)
  166. : "memory"
  167. );
  168. if (out_ch == 2) {
  169. MIX_MISC(IF1);
  170. } else {
  171. MIX_MISC(IF0);
  172. }
  173. }
  174. }
  175. #endif /* HAVE_SSE_INLINE && HAVE_7REGS */
  176. av_cold void ff_ac3dsp_init_x86(AC3DSPContext *c, int bit_exact)
  177. {
  178. int cpu_flags = av_get_cpu_flags();
  179. if (EXTERNAL_MMX(cpu_flags)) {
  180. c->ac3_exponent_min = ff_ac3_exponent_min_mmx;
  181. c->ac3_max_msb_abs_int16 = ff_ac3_max_msb_abs_int16_mmx;
  182. c->ac3_lshift_int16 = ff_ac3_lshift_int16_mmx;
  183. c->ac3_rshift_int32 = ff_ac3_rshift_int32_mmx;
  184. }
  185. if (EXTERNAL_AMD3DNOW(cpu_flags)) {
  186. if (!bit_exact) {
  187. c->float_to_fixed24 = ff_float_to_fixed24_3dnow;
  188. }
  189. }
  190. if (EXTERNAL_MMXEXT(cpu_flags)) {
  191. c->ac3_exponent_min = ff_ac3_exponent_min_mmxext;
  192. c->ac3_max_msb_abs_int16 = ff_ac3_max_msb_abs_int16_mmxext;
  193. if (bit_exact) {
  194. c->apply_window_int16 = ff_apply_window_int16_mmxext;
  195. } else {
  196. c->apply_window_int16 = ff_apply_window_int16_round_mmxext;
  197. }
  198. }
  199. if (EXTERNAL_SSE(cpu_flags)) {
  200. c->float_to_fixed24 = ff_float_to_fixed24_sse;
  201. }
  202. if (EXTERNAL_SSE2(cpu_flags)) {
  203. c->ac3_exponent_min = ff_ac3_exponent_min_sse2;
  204. c->ac3_max_msb_abs_int16 = ff_ac3_max_msb_abs_int16_sse2;
  205. c->float_to_fixed24 = ff_float_to_fixed24_sse2;
  206. c->compute_mantissa_size = ff_ac3_compute_mantissa_size_sse2;
  207. c->extract_exponents = ff_ac3_extract_exponents_sse2;
  208. if (!(cpu_flags & AV_CPU_FLAG_SSE2SLOW)) {
  209. c->ac3_lshift_int16 = ff_ac3_lshift_int16_sse2;
  210. c->ac3_rshift_int32 = ff_ac3_rshift_int32_sse2;
  211. }
  212. if (bit_exact) {
  213. c->apply_window_int16 = ff_apply_window_int16_sse2;
  214. } else if (!(cpu_flags & AV_CPU_FLAG_SSE2SLOW)) {
  215. c->apply_window_int16 = ff_apply_window_int16_round_sse2;
  216. }
  217. }
  218. if (EXTERNAL_SSSE3(cpu_flags)) {
  219. c->ac3_max_msb_abs_int16 = ff_ac3_max_msb_abs_int16_ssse3;
  220. if (cpu_flags & AV_CPU_FLAG_ATOM) {
  221. c->apply_window_int16 = ff_apply_window_int16_ssse3_atom;
  222. } else {
  223. c->extract_exponents = ff_ac3_extract_exponents_ssse3;
  224. c->apply_window_int16 = ff_apply_window_int16_ssse3;
  225. }
  226. }
  227. #if HAVE_SSE_INLINE && HAVE_7REGS
  228. if (INLINE_SSE(cpu_flags)) {
  229. c->downmix = ac3_downmix_sse;
  230. }
  231. #endif
  232. }