lavr: add x86-optimized mixing functions
[ffmpeg.git] / libavresample / x86 / audio_mix_init.c
1 /*
2  * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
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
21 #include "config.h"
22 #include "libavutil/cpu.h"
23 #include "libavresample/audio_mix.h"
24
25 extern void ff_mix_2_to_1_fltp_flt_sse(float **src, float **matrix, int len,
26                                        int out_ch, int in_ch);
27 extern void ff_mix_2_to_1_fltp_flt_avx(float **src, float **matrix, int len,
28                                        int out_ch, int in_ch);
29
30 extern void ff_mix_2_to_1_s16p_flt_sse2(int16_t **src, float **matrix, int len,
31                                         int out_ch, int in_ch);
32 extern void ff_mix_2_to_1_s16p_flt_sse4(int16_t **src, float **matrix, int len,
33                                         int out_ch, int in_ch);
34
35 extern void ff_mix_2_to_1_s16p_q8_sse2(int16_t **src, int16_t **matrix,
36                                        int len, int out_ch, int in_ch);
37
38 extern void ff_mix_1_to_2_fltp_flt_sse(float **src, float **matrix, int len,
39                                        int out_ch, int in_ch);
40 extern void ff_mix_1_to_2_fltp_flt_avx(float **src, float **matrix, int len,
41                                        int out_ch, int in_ch);
42
43 extern void ff_mix_1_to_2_s16p_flt_sse2(int16_t **src, float **matrix, int len,
44                                         int out_ch, int in_ch);
45 extern void ff_mix_1_to_2_s16p_flt_sse4(int16_t **src, float **matrix, int len,
46                                         int out_ch, int in_ch);
47 extern void ff_mix_1_to_2_s16p_flt_avx (int16_t **src, float **matrix, int len,
48                                         int out_ch, int in_ch);
49
50 #define DEFINE_MIX_3_8_TO_1_2(chan)                                         \
51 extern void ff_mix_ ## chan ## _to_1_fltp_flt_sse(float **src,              \
52                                                   float **matrix, int len,  \
53                                                   int out_ch, int in_ch);   \
54 extern void ff_mix_ ## chan ## _to_2_fltp_flt_sse(float **src,              \
55                                                   float **matrix, int len,  \
56                                                   int out_ch, int in_ch);   \
57                                                                             \
58 extern void ff_mix_ ## chan ## _to_1_s16p_flt_sse2(int16_t **src,           \
59                                                    float **matrix, int len, \
60                                                    int out_ch, int in_ch);  \
61 extern void ff_mix_ ## chan ## _to_2_s16p_flt_sse2(int16_t **src,           \
62                                                    float **matrix, int len, \
63                                                    int out_ch, int in_ch);  \
64                                                                             \
65 extern void ff_mix_ ## chan ## _to_1_s16p_flt_sse4(int16_t **src,           \
66                                                    float **matrix, int len, \
67                                                    int out_ch, int in_ch);  \
68 extern void ff_mix_ ## chan ## _to_2_s16p_flt_sse4(int16_t **src,           \
69                                                    float **matrix, int len, \
70                                                    int out_ch, int in_ch);  \
71                                                                             \
72 extern void ff_mix_ ## chan ## _to_1_fltp_flt_avx(float **src,              \
73                                                   float **matrix, int len,  \
74                                                   int out_ch, int in_ch);   \
75 extern void ff_mix_ ## chan ## _to_2_fltp_flt_avx(float **src,              \
76                                                   float **matrix, int len,  \
77                                                   int out_ch, int in_ch);   \
78                                                                             \
79 extern void ff_mix_ ## chan ## _to_1_s16p_flt_avx(int16_t **src,            \
80                                                   float **matrix, int len,  \
81                                                   int out_ch, int in_ch);   \
82 extern void ff_mix_ ## chan ## _to_2_s16p_flt_avx(int16_t **src,            \
83                                                   float **matrix, int len,  \
84                                                   int out_ch, int in_ch);   \
85                                                                             \
86 extern void ff_mix_ ## chan ## _to_1_fltp_flt_fma4(float **src,             \
87                                                    float **matrix, int len, \
88                                                    int out_ch, int in_ch);  \
89 extern void ff_mix_ ## chan ## _to_2_fltp_flt_fma4(float **src,             \
90                                                    float **matrix, int len, \
91                                                    int out_ch, int in_ch);  \
92                                                                             \
93 extern void ff_mix_ ## chan ## _to_1_s16p_flt_fma4(int16_t **src,           \
94                                                    float **matrix, int len, \
95                                                    int out_ch, int in_ch);  \
96 extern void ff_mix_ ## chan ## _to_2_s16p_flt_fma4(int16_t **src,           \
97                                                    float **matrix, int len, \
98                                                    int out_ch, int in_ch);
99
100 DEFINE_MIX_3_8_TO_1_2(3)
101 DEFINE_MIX_3_8_TO_1_2(4)
102 DEFINE_MIX_3_8_TO_1_2(5)
103 DEFINE_MIX_3_8_TO_1_2(6)
104 DEFINE_MIX_3_8_TO_1_2(7)
105 DEFINE_MIX_3_8_TO_1_2(8)
106
107 #define SET_MIX_3_8_TO_1_2(chan)                                            \
108     if (mm_flags & AV_CPU_FLAG_SSE && HAVE_SSE) {                           \
109         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,\
110                               chan, 1, 16, 4, "SSE",                        \
111                               ff_mix_ ## chan ## _to_1_fltp_flt_sse);       \
112         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,\
113                               chan, 2, 16, 4, "SSE",                        \
114                               ff_mix_## chan ##_to_2_fltp_flt_sse);         \
115     }                                                                       \
116     if (mm_flags & AV_CPU_FLAG_SSE2 && HAVE_SSE) {                          \
117         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,\
118                               chan, 1, 16, 8, "SSE2",                       \
119                               ff_mix_ ## chan ## _to_1_s16p_flt_sse2);      \
120         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,\
121                               chan, 2, 16, 8, "SSE2",                       \
122                               ff_mix_ ## chan ## _to_2_s16p_flt_sse2);      \
123     }                                                                       \
124     if (mm_flags & AV_CPU_FLAG_SSE4 && HAVE_SSE) {                          \
125         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,\
126                               chan, 1, 16, 8, "SSE4",                       \
127                               ff_mix_ ## chan ## _to_1_s16p_flt_sse4);      \
128         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,\
129                               chan, 2, 16, 8, "SSE4",                       \
130                               ff_mix_ ## chan ## _to_2_s16p_flt_sse4);      \
131     }                                                                       \
132     if (mm_flags & AV_CPU_FLAG_AVX && HAVE_AVX) {                           \
133         int ptr_align = 32;                                                 \
134         int smp_align = 8;                                                  \
135         if (ARCH_X86_32 || chan >= 6) {                                     \
136             ptr_align = 16;                                                 \
137             smp_align = 4;                                                  \
138         }                                                                   \
139         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,\
140                               chan, 1, ptr_align, smp_align, "AVX",         \
141                               ff_mix_ ## chan ## _to_1_fltp_flt_avx);       \
142         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,\
143                               chan, 2, ptr_align, smp_align, "AVX",         \
144                               ff_mix_ ## chan ## _to_2_fltp_flt_avx);       \
145         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,\
146                               chan, 1, 16, 8, "AVX",                        \
147                               ff_mix_ ## chan ## _to_1_s16p_flt_avx);       \
148         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,\
149                               chan, 2, 16, 8, "AVX",                        \
150                               ff_mix_ ## chan ## _to_2_s16p_flt_avx);       \
151     }                                                                       \
152     if (mm_flags & AV_CPU_FLAG_FMA4 && HAVE_FMA4) {                         \
153         int ptr_align = 32;                                                 \
154         int smp_align = 8;                                                  \
155         if (ARCH_X86_32 || chan >= 6) {                                     \
156             ptr_align = 16;                                                 \
157             smp_align = 4;                                                  \
158         }                                                                   \
159         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,\
160                               chan, 1, ptr_align, smp_align, "FMA4",        \
161                               ff_mix_ ## chan ## _to_1_fltp_flt_fma4);      \
162         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,\
163                               chan, 2, ptr_align, smp_align, "FMA4",        \
164                               ff_mix_ ## chan ## _to_2_fltp_flt_fma4);      \
165         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,\
166                               chan, 1, 16, 8, "FMA4",                       \
167                               ff_mix_ ## chan ## _to_1_s16p_flt_fma4);      \
168         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,\
169                               chan, 2, 16, 8, "FMA4",                       \
170                               ff_mix_ ## chan ## _to_2_s16p_flt_fma4);      \
171     }
172
173 av_cold void ff_audio_mix_init_x86(AudioMix *am)
174 {
175 #if HAVE_YASM
176     int mm_flags = av_get_cpu_flags();
177
178     if (mm_flags & AV_CPU_FLAG_SSE && HAVE_SSE) {
179         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
180                               2, 1, 16, 8, "SSE", ff_mix_2_to_1_fltp_flt_sse);
181         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
182                               1, 2, 16, 4, "SSE", ff_mix_1_to_2_fltp_flt_sse);
183     }
184     if (mm_flags & AV_CPU_FLAG_SSE2 && HAVE_SSE) {
185         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,
186                               2, 1, 16, 8, "SSE2", ff_mix_2_to_1_s16p_flt_sse2);
187         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q8,
188                               2, 1, 16, 8, "SSE2", ff_mix_2_to_1_s16p_q8_sse2);
189         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,
190                               1, 2, 16, 8, "SSE2", ff_mix_1_to_2_s16p_flt_sse2);
191     }
192     if (mm_flags & AV_CPU_FLAG_SSE4 && HAVE_SSE) {
193         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,
194                               2, 1, 16, 8, "SSE4", ff_mix_2_to_1_s16p_flt_sse4);
195         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,
196                               1, 2, 16, 8, "SSE4", ff_mix_1_to_2_s16p_flt_sse4);
197     }
198     if (mm_flags & AV_CPU_FLAG_AVX && HAVE_AVX) {
199         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
200                               2, 1, 32, 16, "AVX", ff_mix_2_to_1_fltp_flt_avx);
201         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
202                               1, 2, 32, 8, "AVX", ff_mix_1_to_2_fltp_flt_avx);
203         ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,
204                               1, 2, 16, 8, "AVX", ff_mix_1_to_2_s16p_flt_avx);
205     }
206
207     SET_MIX_3_8_TO_1_2(3)
208     SET_MIX_3_8_TO_1_2(4)
209     SET_MIX_3_8_TO_1_2(5)
210     SET_MIX_3_8_TO_1_2(6)
211     SET_MIX_3_8_TO_1_2(7)
212     SET_MIX_3_8_TO_1_2(8)
213 #endif
214 }