swresample_x86.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (C) 2012 Michael Niedermayer (michaelni@gmx.at)
  3. *
  4. * This file is part of libswresample
  5. *
  6. * libswresample 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. * libswresample 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 libswresample; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libswresample/swresample_internal.h"
  21. #include "libswresample/audioconvert.h"
  22. #define PROTO(pre, in, out, cap) void ff ## pre ## _ ##in## _to_ ##out## _a_ ##cap(uint8_t **dst, const uint8_t **src, int len);
  23. #define PROTO2(pre, out, cap) PROTO(pre, int16, out, cap) PROTO(pre, int32, out, cap) PROTO(pre, float, out, cap)
  24. #define PROTO3(pre, cap) PROTO2(pre, int16, cap) PROTO2(pre, int32, cap) PROTO2(pre, float, cap)
  25. #define PROTO4(pre) PROTO3(pre, mmx) PROTO3(pre, sse) PROTO3(pre, sse2) PROTO3(pre, ssse3) PROTO3(pre, sse4) PROTO3(pre, avx)
  26. PROTO4()
  27. PROTO4(_pack_2ch)
  28. PROTO4(_pack_6ch)
  29. PROTO4(_unpack_2ch)
  30. av_cold void swri_audio_convert_init_x86(struct AudioConvert *ac,
  31. enum AVSampleFormat out_fmt,
  32. enum AVSampleFormat in_fmt,
  33. int channels){
  34. int mm_flags = av_get_cpu_flags();
  35. ac->simd_f= NULL;
  36. //FIXME add memcpy case
  37. #define MULTI_CAPS_FUNC(flag, cap) \
  38. if (mm_flags & flag) {\
  39. if( out_fmt == AV_SAMPLE_FMT_S32 && in_fmt == AV_SAMPLE_FMT_S16 || out_fmt == AV_SAMPLE_FMT_S32P && in_fmt == AV_SAMPLE_FMT_S16P)\
  40. ac->simd_f = ff_int16_to_int32_a_ ## cap;\
  41. if( out_fmt == AV_SAMPLE_FMT_S16 && in_fmt == AV_SAMPLE_FMT_S32 || out_fmt == AV_SAMPLE_FMT_S16P && in_fmt == AV_SAMPLE_FMT_S32P)\
  42. ac->simd_f = ff_int32_to_int16_a_ ## cap;\
  43. }
  44. MULTI_CAPS_FUNC(AV_CPU_FLAG_MMX, mmx)
  45. MULTI_CAPS_FUNC(AV_CPU_FLAG_SSE2, sse2)
  46. if(mm_flags & AV_CPU_FLAG_MMX) {
  47. if(channels == 6) {
  48. if( out_fmt == AV_SAMPLE_FMT_FLT && in_fmt == AV_SAMPLE_FMT_FLTP || out_fmt == AV_SAMPLE_FMT_S32 && in_fmt == AV_SAMPLE_FMT_S32P)
  49. ac->simd_f = ff_pack_6ch_float_to_float_a_mmx;
  50. }
  51. }
  52. if(mm_flags & AV_CPU_FLAG_SSE2) {
  53. if( out_fmt == AV_SAMPLE_FMT_FLT && in_fmt == AV_SAMPLE_FMT_S32 || out_fmt == AV_SAMPLE_FMT_FLTP && in_fmt == AV_SAMPLE_FMT_S32P)
  54. ac->simd_f = ff_int32_to_float_a_sse2;
  55. if( out_fmt == AV_SAMPLE_FMT_FLT && in_fmt == AV_SAMPLE_FMT_S16 || out_fmt == AV_SAMPLE_FMT_FLTP && in_fmt == AV_SAMPLE_FMT_S16P)
  56. ac->simd_f = ff_int16_to_float_a_sse2;
  57. if( out_fmt == AV_SAMPLE_FMT_S32 && in_fmt == AV_SAMPLE_FMT_FLT || out_fmt == AV_SAMPLE_FMT_S32P && in_fmt == AV_SAMPLE_FMT_FLTP)
  58. ac->simd_f = ff_float_to_int32_a_sse2;
  59. if( out_fmt == AV_SAMPLE_FMT_S16 && in_fmt == AV_SAMPLE_FMT_FLT || out_fmt == AV_SAMPLE_FMT_S16P && in_fmt == AV_SAMPLE_FMT_FLTP)
  60. ac->simd_f = ff_float_to_int16_a_sse2;
  61. if(channels == 2) {
  62. if( out_fmt == AV_SAMPLE_FMT_FLT && in_fmt == AV_SAMPLE_FMT_FLTP || out_fmt == AV_SAMPLE_FMT_S32 && in_fmt == AV_SAMPLE_FMT_S32P)
  63. ac->simd_f = ff_pack_2ch_int32_to_int32_a_sse2;
  64. if( out_fmt == AV_SAMPLE_FMT_S16 && in_fmt == AV_SAMPLE_FMT_S16P)
  65. ac->simd_f = ff_pack_2ch_int16_to_int16_a_sse2;
  66. if( out_fmt == AV_SAMPLE_FMT_S32 && in_fmt == AV_SAMPLE_FMT_S16P)
  67. ac->simd_f = ff_pack_2ch_int16_to_int32_a_sse2;
  68. if( out_fmt == AV_SAMPLE_FMT_S16 && in_fmt == AV_SAMPLE_FMT_S32P)
  69. ac->simd_f = ff_pack_2ch_int32_to_int16_a_sse2;
  70. if( out_fmt == AV_SAMPLE_FMT_FLTP && in_fmt == AV_SAMPLE_FMT_FLT || out_fmt == AV_SAMPLE_FMT_S32P && in_fmt == AV_SAMPLE_FMT_S32)
  71. ac->simd_f = ff_unpack_2ch_int32_to_int32_a_sse2;
  72. if( out_fmt == AV_SAMPLE_FMT_S16P && in_fmt == AV_SAMPLE_FMT_S16)
  73. ac->simd_f = ff_unpack_2ch_int16_to_int16_a_sse2;
  74. if( out_fmt == AV_SAMPLE_FMT_S32P && in_fmt == AV_SAMPLE_FMT_S16)
  75. ac->simd_f = ff_unpack_2ch_int16_to_int32_a_sse2;
  76. if( out_fmt == AV_SAMPLE_FMT_S16P && in_fmt == AV_SAMPLE_FMT_S32)
  77. ac->simd_f = ff_unpack_2ch_int32_to_int16_a_sse2;
  78. if( out_fmt == AV_SAMPLE_FMT_FLT && in_fmt == AV_SAMPLE_FMT_S32P)
  79. ac->simd_f = ff_pack_2ch_int32_to_float_a_sse2;
  80. if( out_fmt == AV_SAMPLE_FMT_S32 && in_fmt == AV_SAMPLE_FMT_FLTP)
  81. ac->simd_f = ff_pack_2ch_float_to_int32_a_sse2;
  82. if( out_fmt == AV_SAMPLE_FMT_FLT && in_fmt == AV_SAMPLE_FMT_S16P)
  83. ac->simd_f = ff_pack_2ch_int16_to_float_a_sse2;
  84. if( out_fmt == AV_SAMPLE_FMT_S16 && in_fmt == AV_SAMPLE_FMT_FLTP)
  85. ac->simd_f = ff_pack_2ch_float_to_int16_a_sse2;
  86. if( out_fmt == AV_SAMPLE_FMT_FLTP && in_fmt == AV_SAMPLE_FMT_S32)
  87. ac->simd_f = ff_unpack_2ch_int32_to_float_a_sse2;
  88. if( out_fmt == AV_SAMPLE_FMT_S32P && in_fmt == AV_SAMPLE_FMT_FLT)
  89. ac->simd_f = ff_unpack_2ch_float_to_int32_a_sse2;
  90. if( out_fmt == AV_SAMPLE_FMT_FLTP && in_fmt == AV_SAMPLE_FMT_S16)
  91. ac->simd_f = ff_unpack_2ch_int16_to_float_a_sse2;
  92. if( out_fmt == AV_SAMPLE_FMT_S16P && in_fmt == AV_SAMPLE_FMT_FLT)
  93. ac->simd_f = ff_unpack_2ch_float_to_int16_a_sse2;
  94. }
  95. }
  96. if(mm_flags & AV_CPU_FLAG_SSSE3) {
  97. if(channels == 2) {
  98. if( out_fmt == AV_SAMPLE_FMT_S16P && in_fmt == AV_SAMPLE_FMT_S16)
  99. ac->simd_f = ff_unpack_2ch_int16_to_int16_a_ssse3;
  100. if( out_fmt == AV_SAMPLE_FMT_S32P && in_fmt == AV_SAMPLE_FMT_S16)
  101. ac->simd_f = ff_unpack_2ch_int16_to_int32_a_ssse3;
  102. if( out_fmt == AV_SAMPLE_FMT_FLTP && in_fmt == AV_SAMPLE_FMT_S16)
  103. ac->simd_f = ff_unpack_2ch_int16_to_float_a_ssse3;
  104. }
  105. }
  106. if(mm_flags & AV_CPU_FLAG_SSE4) {
  107. if(channels == 6) {
  108. if( out_fmt == AV_SAMPLE_FMT_FLT && in_fmt == AV_SAMPLE_FMT_FLTP || out_fmt == AV_SAMPLE_FMT_S32 && in_fmt == AV_SAMPLE_FMT_S32P)
  109. ac->simd_f = ff_pack_6ch_float_to_float_a_sse4;
  110. if( out_fmt == AV_SAMPLE_FMT_FLT && in_fmt == AV_SAMPLE_FMT_S32P)
  111. ac->simd_f = ff_pack_6ch_int32_to_float_a_sse4;
  112. if( out_fmt == AV_SAMPLE_FMT_S32 && in_fmt == AV_SAMPLE_FMT_FLTP)
  113. ac->simd_f = ff_pack_6ch_float_to_int32_a_sse4;
  114. }
  115. }
  116. if(HAVE_AVX_EXTERNAL && mm_flags & AV_CPU_FLAG_AVX) {
  117. if( out_fmt == AV_SAMPLE_FMT_FLT && in_fmt == AV_SAMPLE_FMT_S32 || out_fmt == AV_SAMPLE_FMT_FLTP && in_fmt == AV_SAMPLE_FMT_S32P)
  118. ac->simd_f = ff_int32_to_float_a_avx;
  119. if(channels == 6) {
  120. if( out_fmt == AV_SAMPLE_FMT_FLT && in_fmt == AV_SAMPLE_FMT_FLTP || out_fmt == AV_SAMPLE_FMT_S32 && in_fmt == AV_SAMPLE_FMT_S32P)
  121. ac->simd_f = ff_pack_6ch_float_to_float_a_avx;
  122. if( out_fmt == AV_SAMPLE_FMT_FLT && in_fmt == AV_SAMPLE_FMT_S32P)
  123. ac->simd_f = ff_pack_6ch_int32_to_float_a_avx;
  124. if( out_fmt == AV_SAMPLE_FMT_S32 && in_fmt == AV_SAMPLE_FMT_FLTP)
  125. ac->simd_f = ff_pack_6ch_float_to_int32_a_avx;
  126. }
  127. }
  128. }
  129. #define D(type, simd) \
  130. mix_1_1_func_type ff_mix_1_1_a_## type ## _ ## simd;\
  131. mix_2_1_func_type ff_mix_2_1_a_## type ## _ ## simd;
  132. D(float, sse)
  133. D(float, avx)
  134. D(int16, mmx)
  135. D(int16, sse2)
  136. av_cold void swri_rematrix_init_x86(struct SwrContext *s){
  137. int mm_flags = av_get_cpu_flags();
  138. int nb_in = av_get_channel_layout_nb_channels(s->in_ch_layout);
  139. int nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
  140. int num = nb_in * nb_out;
  141. int i,j;
  142. s->mix_1_1_simd = NULL;
  143. s->mix_2_1_simd = NULL;
  144. if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
  145. if(mm_flags & AV_CPU_FLAG_MMX) {
  146. s->mix_1_1_simd = ff_mix_1_1_a_int16_mmx;
  147. s->mix_2_1_simd = ff_mix_2_1_a_int16_mmx;
  148. }
  149. if(mm_flags & AV_CPU_FLAG_SSE2) {
  150. s->mix_1_1_simd = ff_mix_1_1_a_int16_sse2;
  151. s->mix_2_1_simd = ff_mix_2_1_a_int16_sse2;
  152. }
  153. s->native_simd_matrix = av_mallocz(2 * num * sizeof(int16_t));
  154. s->native_simd_one = av_mallocz(2 * sizeof(int16_t));
  155. for(i=0; i<nb_out; i++){
  156. int sh = 0;
  157. for(j=0; j<nb_in; j++)
  158. sh = FFMAX(sh, FFABS(((int*)s->native_matrix)[i * nb_in + j]));
  159. sh = FFMAX(av_log2(sh) - 14, 0);
  160. for(j=0; j<nb_in; j++) {
  161. ((int16_t*)s->native_simd_matrix)[2*(i * nb_in + j)+1] = 15 - sh;
  162. ((int16_t*)s->native_simd_matrix)[2*(i * nb_in + j)] =
  163. ((((int*)s->native_matrix)[i * nb_in + j]) + (1<<sh>>1)) >> sh;
  164. }
  165. }
  166. ((int16_t*)s->native_simd_one)[1] = 14;
  167. ((int16_t*)s->native_simd_one)[0] = 16384;
  168. } else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
  169. if(mm_flags & AV_CPU_FLAG_SSE) {
  170. s->mix_1_1_simd = ff_mix_1_1_a_float_sse;
  171. s->mix_2_1_simd = ff_mix_2_1_a_float_sse;
  172. }
  173. if(HAVE_AVX_EXTERNAL && mm_flags & AV_CPU_FLAG_AVX) {
  174. s->mix_1_1_simd = ff_mix_1_1_a_float_avx;
  175. s->mix_2_1_simd = ff_mix_2_1_a_float_avx;
  176. }
  177. s->native_simd_matrix = av_mallocz(num * sizeof(float));
  178. memcpy(s->native_simd_matrix, s->native_matrix, num * sizeof(float));
  179. s->native_simd_one = av_mallocz(sizeof(float));
  180. memcpy(s->native_simd_one, s->native_one, sizeof(float));
  181. }
  182. }