swresample_x86.c
Go to the documentation of this file.
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 
23 
24 #define PROTO(pre, in, out, cap) void ff ## pre ## _ ##in## _to_ ##out## _a_ ##cap(uint8_t **dst, const uint8_t **src, int len);
25 #define PROTO2(pre, out, cap) PROTO(pre, int16, out, cap) PROTO(pre, int32, out, cap) PROTO(pre, float, out, cap)
26 #define PROTO3(pre, cap) PROTO2(pre, int16, cap) PROTO2(pre, int32, cap) PROTO2(pre, float, cap)
27 #define PROTO4(pre) PROTO3(pre, mmx) PROTO3(pre, sse) PROTO3(pre, sse2) PROTO3(pre, ssse3) PROTO3(pre, sse4) PROTO3(pre, avx)
28 PROTO4()
29 PROTO4(_pack_2ch)
30 PROTO4(_pack_6ch)
31 PROTO4(_unpack_2ch)
32 
34  enum AVSampleFormat out_fmt,
35  enum AVSampleFormat in_fmt,
36  int channels){
37  int mm_flags = av_get_cpu_flags();
38 
39  ac->simd_f= NULL;
40 
41 //FIXME add memcpy case
42 
43 #define MULTI_CAPS_FUNC(flag, cap) \
44  if (mm_flags & flag) {\
45  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)\
46  ac->simd_f = ff_int16_to_int32_a_ ## cap;\
47  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)\
48  ac->simd_f = ff_int32_to_int16_a_ ## cap;\
49  }
50 
53 
54  if(mm_flags & AV_CPU_FLAG_MMX) {
55  if(channels == 6) {
56  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)
57  ac->simd_f = ff_pack_6ch_float_to_float_a_mmx;
58  }
59  }
60 
61  if(mm_flags & AV_CPU_FLAG_SSE2) {
62  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)
63  ac->simd_f = ff_int32_to_float_a_sse2;
64  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)
65  ac->simd_f = ff_int16_to_float_a_sse2;
66  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)
67  ac->simd_f = ff_float_to_int32_a_sse2;
68  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)
69  ac->simd_f = ff_float_to_int16_a_sse2;
70 
71  if(channels == 2) {
72  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)
73  ac->simd_f = ff_pack_2ch_int32_to_int32_a_sse2;
74  if( out_fmt == AV_SAMPLE_FMT_S16 && in_fmt == AV_SAMPLE_FMT_S16P)
75  ac->simd_f = ff_pack_2ch_int16_to_int16_a_sse2;
76  if( out_fmt == AV_SAMPLE_FMT_S32 && in_fmt == AV_SAMPLE_FMT_S16P)
77  ac->simd_f = ff_pack_2ch_int16_to_int32_a_sse2;
78  if( out_fmt == AV_SAMPLE_FMT_S16 && in_fmt == AV_SAMPLE_FMT_S32P)
79  ac->simd_f = ff_pack_2ch_int32_to_int16_a_sse2;
80 
81  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)
82  ac->simd_f = ff_unpack_2ch_int32_to_int32_a_sse2;
83  if( out_fmt == AV_SAMPLE_FMT_S16P && in_fmt == AV_SAMPLE_FMT_S16)
84  ac->simd_f = ff_unpack_2ch_int16_to_int16_a_sse2;
85  if( out_fmt == AV_SAMPLE_FMT_S32P && in_fmt == AV_SAMPLE_FMT_S16)
86  ac->simd_f = ff_unpack_2ch_int16_to_int32_a_sse2;
87  if( out_fmt == AV_SAMPLE_FMT_S16P && in_fmt == AV_SAMPLE_FMT_S32)
88  ac->simd_f = ff_unpack_2ch_int32_to_int16_a_sse2;
89 
90  if( out_fmt == AV_SAMPLE_FMT_FLT && in_fmt == AV_SAMPLE_FMT_S32P)
91  ac->simd_f = ff_pack_2ch_int32_to_float_a_sse2;
92  if( out_fmt == AV_SAMPLE_FMT_S32 && in_fmt == AV_SAMPLE_FMT_FLTP)
93  ac->simd_f = ff_pack_2ch_float_to_int32_a_sse2;
94  if( out_fmt == AV_SAMPLE_FMT_FLT && in_fmt == AV_SAMPLE_FMT_S16P)
95  ac->simd_f = ff_pack_2ch_int16_to_float_a_sse2;
96  if( out_fmt == AV_SAMPLE_FMT_S16 && in_fmt == AV_SAMPLE_FMT_FLTP)
97  ac->simd_f = ff_pack_2ch_float_to_int16_a_sse2;
98  if( out_fmt == AV_SAMPLE_FMT_FLTP && in_fmt == AV_SAMPLE_FMT_S32)
99  ac->simd_f = ff_unpack_2ch_int32_to_float_a_sse2;
100  if( out_fmt == AV_SAMPLE_FMT_S32P && in_fmt == AV_SAMPLE_FMT_FLT)
101  ac->simd_f = ff_unpack_2ch_float_to_int32_a_sse2;
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_sse2;
104  if( out_fmt == AV_SAMPLE_FMT_S16P && in_fmt == AV_SAMPLE_FMT_FLT)
105  ac->simd_f = ff_unpack_2ch_float_to_int16_a_sse2;
106  }
107  }
108  if(mm_flags & AV_CPU_FLAG_SSSE3) {
109  if(channels == 2) {
110  if( out_fmt == AV_SAMPLE_FMT_S16P && in_fmt == AV_SAMPLE_FMT_S16)
111  ac->simd_f = ff_unpack_2ch_int16_to_int16_a_ssse3;
112  if( out_fmt == AV_SAMPLE_FMT_S32P && in_fmt == AV_SAMPLE_FMT_S16)
113  ac->simd_f = ff_unpack_2ch_int16_to_int32_a_ssse3;
114  if( out_fmt == AV_SAMPLE_FMT_FLTP && in_fmt == AV_SAMPLE_FMT_S16)
115  ac->simd_f = ff_unpack_2ch_int16_to_float_a_ssse3;
116  }
117  }
118  if(mm_flags & AV_CPU_FLAG_SSE4) {
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_sse4;
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_sse4;
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_sse4;
126  }
127  }
128  if(HAVE_AVX_EXTERNAL && mm_flags & AV_CPU_FLAG_AVX) {
129  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)
130  ac->simd_f = ff_int32_to_float_a_avx;
131  if(channels == 6) {
132  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)
133  ac->simd_f = ff_pack_6ch_float_to_float_a_avx;
134  if( out_fmt == AV_SAMPLE_FMT_FLT && in_fmt == AV_SAMPLE_FMT_S32P)
135  ac->simd_f = ff_pack_6ch_int32_to_float_a_avx;
136  if( out_fmt == AV_SAMPLE_FMT_S32 && in_fmt == AV_SAMPLE_FMT_FLTP)
137  ac->simd_f = ff_pack_6ch_float_to_int32_a_avx;
138  }
139  }
140 }
141 
142 #define D(type, simd) \
143 mix_1_1_func_type ff_mix_1_1_a_## type ## _ ## simd;\
144 mix_2_1_func_type ff_mix_2_1_a_## type ## _ ## simd;
145 
146 D(float, sse)
147 D(float, avx)
148 D(int16, mmx)
149 D(int16, sse2)
150 
151 
153  int mm_flags = av_get_cpu_flags();
154  int nb_in = av_get_channel_layout_nb_channels(s->in_ch_layout);
155  int nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
156  int num = nb_in * nb_out;
157  int i,j;
158 
159  s->mix_1_1_simd = NULL;
160  s->mix_2_1_simd = NULL;
161 
162  if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
163  if(mm_flags & AV_CPU_FLAG_MMX) {
164  s->mix_1_1_simd = ff_mix_1_1_a_int16_mmx;
165  s->mix_2_1_simd = ff_mix_2_1_a_int16_mmx;
166  }
167  if(mm_flags & AV_CPU_FLAG_SSE2) {
168  s->mix_1_1_simd = ff_mix_1_1_a_int16_sse2;
169  s->mix_2_1_simd = ff_mix_2_1_a_int16_sse2;
170  }
171  s->native_simd_matrix = av_mallocz(2 * num * sizeof(int16_t));
172  for(i=0; i<nb_out; i++){
173  int sh = 0;
174  for(j=0; j<nb_in; j++)
175  sh = FFMAX(sh, FFABS(((int*)s->native_matrix)[i * nb_in + j]));
176  sh = FFMAX(av_log2(sh) - 14, 0);
177  for(j=0; j<nb_in; j++) {
178  ((int16_t*)s->native_simd_matrix)[2*(i * nb_in + j)+1] = 15 - sh;
179  ((int16_t*)s->native_simd_matrix)[2*(i * nb_in + j)] =
180  ((((int*)s->native_matrix)[i * nb_in + j]) + (1<<sh>>1)) >> sh;
181  }
182  }
183  } else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
184  if(mm_flags & AV_CPU_FLAG_SSE) {
185  s->mix_1_1_simd = ff_mix_1_1_a_float_sse;
186  s->mix_2_1_simd = ff_mix_2_1_a_float_sse;
187  }
188  if(HAVE_AVX_EXTERNAL && mm_flags & AV_CPU_FLAG_AVX) {
189  s->mix_1_1_simd = ff_mix_1_1_a_float_avx;
190  s->mix_2_1_simd = ff_mix_2_1_a_float_avx;
191  }
192  s->native_simd_matrix = av_mallocz(num * sizeof(float));
193  memcpy(s->native_simd_matrix, s->native_matrix, num * sizeof(float));
194  }
195 }
#define AV_CPU_FLAG_AVX
AVX functions: requires OS support even if YMM registers aren&#39;t used.
Definition: cpu.h:43
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
const char * s
Definition: avisynth_c.h:668
#define AV_CPU_FLAG_SSE
SSE functions.
Definition: cpu.h:33
#define MULTI_CAPS_FUNC(flag, cap)
static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride)
signed 16 bits
Definition: samplefmt.h:52
#define av_cold
Definition: attributes.h:78
end end ac
#define HAVE_AVX_EXTERNAL
Definition: config.h:71
signed 32 bits, planar
Definition: samplefmt.h:59
float, planar
Definition: samplefmt.h:60
#define AV_CPU_FLAG_SSSE3
Conroe SSSE3 functions.
Definition: cpu.h:39
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
#define FFMAX(a, b)
Definition: common.h:56
signed 32 bits
Definition: samplefmt.h:53
void swri_rematrix_init_x86(struct SwrContext *s)
#define FFABS(a)
Definition: common.h:53
NULL
Definition: eval.c:55
#define AV_CPU_FLAG_MMX
standard MMX
Definition: cpu.h:29
synthesis window for stochastic i
#define AV_CPU_FLAG_SSE4
Penryn SSE4.1 functions.
Definition: cpu.h:41
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:30
Audio format conversion routines.
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
#define av_log2
Definition: intmath.h:89
signed 16 bits, planar
Definition: samplefmt.h:58
#define AV_CPU_FLAG_SSE2
PIV SSE2 functions.
Definition: cpu.h:34
av_cold void swri_audio_convert_init_x86(struct AudioConvert *ac, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels)
#define PROTO4(pre)
#define D(type, simd)