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.

214 lines
9.6KB

  1. /*
  2. * x86-optimized AC-3 DSP utils
  3. * Copyright (c) 2011 Justin Ruggles
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/mem.h"
  22. #include "libavutil/x86/asm.h"
  23. #include "libavutil/x86/cpu.h"
  24. #include "dsputil_mmx.h"
  25. #include "libavcodec/ac3.h"
  26. #include "libavcodec/ac3dsp.h"
  27. extern void ff_ac3_exponent_min_mmx (uint8_t *exp, int num_reuse_blocks, int nb_coefs);
  28. extern void ff_ac3_exponent_min_mmxext(uint8_t *exp, int num_reuse_blocks, int nb_coefs);
  29. extern void ff_ac3_exponent_min_sse2 (uint8_t *exp, int num_reuse_blocks, int nb_coefs);
  30. extern int ff_ac3_max_msb_abs_int16_mmx (const int16_t *src, int len);
  31. extern int ff_ac3_max_msb_abs_int16_mmxext(const int16_t *src, int len);
  32. extern int ff_ac3_max_msb_abs_int16_sse2 (const int16_t *src, int len);
  33. extern int ff_ac3_max_msb_abs_int16_ssse3(const int16_t *src, int len);
  34. extern void ff_ac3_lshift_int16_mmx (int16_t *src, unsigned int len, unsigned int shift);
  35. extern void ff_ac3_lshift_int16_sse2(int16_t *src, unsigned int len, unsigned int shift);
  36. extern void ff_ac3_rshift_int32_mmx (int32_t *src, unsigned int len, unsigned int shift);
  37. extern void ff_ac3_rshift_int32_sse2(int32_t *src, unsigned int len, unsigned int shift);
  38. extern void ff_float_to_fixed24_3dnow(int32_t *dst, const float *src, unsigned int len);
  39. extern void ff_float_to_fixed24_sse (int32_t *dst, const float *src, unsigned int len);
  40. extern void ff_float_to_fixed24_sse2 (int32_t *dst, const float *src, unsigned int len);
  41. extern int ff_ac3_compute_mantissa_size_sse2(uint16_t mant_cnt[6][16]);
  42. extern void ff_ac3_extract_exponents_3dnow(uint8_t *exp, int32_t *coef, int nb_coefs);
  43. extern void ff_ac3_extract_exponents_sse2 (uint8_t *exp, int32_t *coef, int nb_coefs);
  44. extern void ff_ac3_extract_exponents_ssse3(uint8_t *exp, int32_t *coef, int nb_coefs);
  45. #if HAVE_SSE_INLINE
  46. #define IF1(x) x
  47. #define IF0(x)
  48. #define MIX5(mono, stereo) \
  49. __asm__ volatile ( \
  50. "movss 0(%2), %%xmm5 \n" \
  51. "movss 8(%2), %%xmm6 \n" \
  52. "movss 24(%2), %%xmm7 \n" \
  53. "shufps $0, %%xmm5, %%xmm5 \n" \
  54. "shufps $0, %%xmm6, %%xmm6 \n" \
  55. "shufps $0, %%xmm7, %%xmm7 \n" \
  56. "1: \n" \
  57. "movaps (%0, %1), %%xmm0 \n" \
  58. "movaps 0x400(%0, %1), %%xmm1 \n" \
  59. "movaps 0x800(%0, %1), %%xmm2 \n" \
  60. "movaps 0xc00(%0, %1), %%xmm3 \n" \
  61. "movaps 0x1000(%0, %1), %%xmm4 \n" \
  62. "mulps %%xmm5, %%xmm0 \n" \
  63. "mulps %%xmm6, %%xmm1 \n" \
  64. "mulps %%xmm5, %%xmm2 \n" \
  65. "mulps %%xmm7, %%xmm3 \n" \
  66. "mulps %%xmm7, %%xmm4 \n" \
  67. stereo("addps %%xmm1, %%xmm0 \n") \
  68. "addps %%xmm1, %%xmm2 \n" \
  69. "addps %%xmm3, %%xmm0 \n" \
  70. "addps %%xmm4, %%xmm2 \n" \
  71. mono("addps %%xmm2, %%xmm0 \n") \
  72. "movaps %%xmm0, (%0, %1) \n" \
  73. stereo("movaps %%xmm2, 0x400(%0, %1) \n") \
  74. "add $16, %0 \n" \
  75. "jl 1b \n" \
  76. : "+&r"(i) \
  77. : "r"(samples[0] + len), "r"(matrix) \
  78. : XMM_CLOBBERS("%xmm0", "%xmm1", "%xmm2", "%xmm3", \
  79. "%xmm4", "%xmm5", "%xmm6", "%xmm7",) \
  80. "memory" \
  81. );
  82. #define MIX_MISC(stereo) \
  83. __asm__ volatile ( \
  84. "1: \n" \
  85. "movaps (%3, %0), %%xmm0 \n" \
  86. stereo("movaps %%xmm0, %%xmm1 \n") \
  87. "mulps %%xmm4, %%xmm0 \n" \
  88. stereo("mulps %%xmm5, %%xmm1 \n") \
  89. "lea 1024(%3, %0), %1 \n" \
  90. "mov %5, %2 \n" \
  91. "2: \n" \
  92. "movaps (%1), %%xmm2 \n" \
  93. stereo("movaps %%xmm2, %%xmm3 \n") \
  94. "mulps (%4, %2), %%xmm2 \n" \
  95. stereo("mulps 16(%4, %2), %%xmm3 \n") \
  96. "addps %%xmm2, %%xmm0 \n" \
  97. stereo("addps %%xmm3, %%xmm1 \n") \
  98. "add $1024, %1 \n" \
  99. "add $32, %2 \n" \
  100. "jl 2b \n" \
  101. "movaps %%xmm0, (%3, %0) \n" \
  102. stereo("movaps %%xmm1, 1024(%3, %0) \n") \
  103. "add $16, %0 \n" \
  104. "jl 1b \n" \
  105. : "+&r"(i), "=&r"(j), "=&r"(k) \
  106. : "r"(samples[0] + len), "r"(matrix_simd + in_ch), \
  107. "g"((intptr_t) - 32 * (in_ch - 1)) \
  108. : "memory" \
  109. );
  110. static void ac3_downmix_sse(float (*samples)[256], float (*matrix)[2],
  111. int out_ch, int in_ch, int len)
  112. {
  113. int (*matrix_cmp)[2] = (int(*)[2])matrix;
  114. intptr_t i, j, k;
  115. i = -len * sizeof(float);
  116. if (in_ch == 5 && out_ch == 2 &&
  117. !(matrix_cmp[0][1] | matrix_cmp[2][0] |
  118. matrix_cmp[3][1] | matrix_cmp[4][0] |
  119. (matrix_cmp[1][0] ^ matrix_cmp[1][1]) |
  120. (matrix_cmp[0][0] ^ matrix_cmp[2][1]))) {
  121. MIX5(IF0, IF1);
  122. } else if (in_ch == 5 && out_ch == 1 &&
  123. matrix_cmp[0][0] == matrix_cmp[2][0] &&
  124. matrix_cmp[3][0] == matrix_cmp[4][0]) {
  125. MIX5(IF1, IF0);
  126. } else {
  127. DECLARE_ALIGNED(16, float, matrix_simd)[AC3_MAX_CHANNELS][2][4];
  128. j = 2 * in_ch * sizeof(float);
  129. __asm__ volatile (
  130. "1: \n"
  131. "sub $8, %0 \n"
  132. "movss (%2, %0), %%xmm4 \n"
  133. "movss 4(%2, %0), %%xmm5 \n"
  134. "shufps $0, %%xmm4, %%xmm4 \n"
  135. "shufps $0, %%xmm5, %%xmm5 \n"
  136. "movaps %%xmm4, (%1, %0, 4) \n"
  137. "movaps %%xmm5, 16(%1, %0, 4) \n"
  138. "jg 1b \n"
  139. : "+&r"(j)
  140. : "r"(matrix_simd), "r"(matrix)
  141. : "memory"
  142. );
  143. if (out_ch == 2) {
  144. MIX_MISC(IF1);
  145. } else {
  146. MIX_MISC(IF0);
  147. }
  148. }
  149. }
  150. #endif /* HAVE_SSE_INLINE */
  151. av_cold void ff_ac3dsp_init_x86(AC3DSPContext *c, int bit_exact)
  152. {
  153. int mm_flags = av_get_cpu_flags();
  154. if (EXTERNAL_MMX(mm_flags)) {
  155. c->ac3_exponent_min = ff_ac3_exponent_min_mmx;
  156. c->ac3_max_msb_abs_int16 = ff_ac3_max_msb_abs_int16_mmx;
  157. c->ac3_lshift_int16 = ff_ac3_lshift_int16_mmx;
  158. c->ac3_rshift_int32 = ff_ac3_rshift_int32_mmx;
  159. }
  160. if (EXTERNAL_AMD3DNOW(mm_flags)) {
  161. c->extract_exponents = ff_ac3_extract_exponents_3dnow;
  162. if (!bit_exact) {
  163. c->float_to_fixed24 = ff_float_to_fixed24_3dnow;
  164. }
  165. }
  166. if (EXTERNAL_MMXEXT(mm_flags)) {
  167. c->ac3_exponent_min = ff_ac3_exponent_min_mmxext;
  168. c->ac3_max_msb_abs_int16 = ff_ac3_max_msb_abs_int16_mmxext;
  169. }
  170. if (EXTERNAL_SSE(mm_flags)) {
  171. c->float_to_fixed24 = ff_float_to_fixed24_sse;
  172. }
  173. if (EXTERNAL_SSE2(mm_flags)) {
  174. c->ac3_exponent_min = ff_ac3_exponent_min_sse2;
  175. c->ac3_max_msb_abs_int16 = ff_ac3_max_msb_abs_int16_sse2;
  176. c->float_to_fixed24 = ff_float_to_fixed24_sse2;
  177. c->compute_mantissa_size = ff_ac3_compute_mantissa_size_sse2;
  178. c->extract_exponents = ff_ac3_extract_exponents_sse2;
  179. if (!(mm_flags & AV_CPU_FLAG_SSE2SLOW)) {
  180. c->ac3_lshift_int16 = ff_ac3_lshift_int16_sse2;
  181. c->ac3_rshift_int32 = ff_ac3_rshift_int32_sse2;
  182. }
  183. }
  184. if (EXTERNAL_SSSE3(mm_flags)) {
  185. c->ac3_max_msb_abs_int16 = ff_ac3_max_msb_abs_int16_ssse3;
  186. if (!(mm_flags & AV_CPU_FLAG_ATOM)) {
  187. c->extract_exponents = ff_ac3_extract_exponents_ssse3;
  188. }
  189. }
  190. #if HAVE_SSE_INLINE
  191. if (INLINE_SSE(mm_flags)) {
  192. c->downmix = ac3_downmix_sse;
  193. }
  194. #endif
  195. }