yading@11
|
1 /*
|
yading@11
|
2 * audio conversion
|
yading@11
|
3 * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
|
yading@11
|
4 *
|
yading@11
|
5 * This file is part of FFmpeg.
|
yading@11
|
6 *
|
yading@11
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
9 * License as published by the Free Software Foundation; either
|
yading@11
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
11 *
|
yading@11
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
15 * Lesser General Public License for more details.
|
yading@11
|
16 *
|
yading@11
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
20 */
|
yading@11
|
21
|
yading@11
|
22 /**
|
yading@11
|
23 * @file
|
yading@11
|
24 * audio conversion
|
yading@11
|
25 * @author Michael Niedermayer <michaelni@gmx.at>
|
yading@11
|
26 */
|
yading@11
|
27
|
yading@11
|
28 #include "libavutil/avstring.h"
|
yading@11
|
29 #include "libavutil/avassert.h"
|
yading@11
|
30 #include "libavutil/libm.h"
|
yading@11
|
31 #include "libavutil/samplefmt.h"
|
yading@11
|
32 #include "audioconvert.h"
|
yading@11
|
33
|
yading@11
|
34
|
yading@11
|
35 #define CONV_FUNC_NAME(dst_fmt, src_fmt) conv_ ## src_fmt ## _to_ ## dst_fmt
|
yading@11
|
36
|
yading@11
|
37 //FIXME rounding ?
|
yading@11
|
38 #define CONV_FUNC(ofmt, otype, ifmt, expr)\
|
yading@11
|
39 static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t *po, const uint8_t *pi, int is, int os, uint8_t *end)\
|
yading@11
|
40 {\
|
yading@11
|
41 uint8_t *end2 = end - 3*os;\
|
yading@11
|
42 while(po < end2){\
|
yading@11
|
43 *(otype*)po = expr; pi += is; po += os;\
|
yading@11
|
44 *(otype*)po = expr; pi += is; po += os;\
|
yading@11
|
45 *(otype*)po = expr; pi += is; po += os;\
|
yading@11
|
46 *(otype*)po = expr; pi += is; po += os;\
|
yading@11
|
47 }\
|
yading@11
|
48 while(po < end){\
|
yading@11
|
49 *(otype*)po = expr; pi += is; po += os;\
|
yading@11
|
50 }\
|
yading@11
|
51 }
|
yading@11
|
52
|
yading@11
|
53 //FIXME put things below under ifdefs so we do not waste space for cases no codec will need
|
yading@11
|
54 CONV_FUNC(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_U8 , *(const uint8_t*)pi)
|
yading@11
|
55 CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<8)
|
yading@11
|
56 CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<24)
|
yading@11
|
57 CONV_FUNC(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0f/ (1<<7)))
|
yading@11
|
58 CONV_FUNC(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7)))
|
yading@11
|
59 CONV_FUNC(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_S16, (*(const int16_t*)pi>>8) + 0x80)
|
yading@11
|
60 CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S16, *(const int16_t*)pi)
|
yading@11
|
61 CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S16, *(const int16_t*)pi<<16)
|
yading@11
|
62 CONV_FUNC(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_S16, *(const int16_t*)pi*(1.0f/ (1<<15)))
|
yading@11
|
63 CONV_FUNC(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_S16, *(const int16_t*)pi*(1.0 / (1<<15)))
|
yading@11
|
64 CONV_FUNC(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_S32, (*(const int32_t*)pi>>24) + 0x80)
|
yading@11
|
65 CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S32, *(const int32_t*)pi>>16)
|
yading@11
|
66 CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S32, *(const int32_t*)pi)
|
yading@11
|
67 CONV_FUNC(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_S32, *(const int32_t*)pi*(1.0f/ (1U<<31)))
|
yading@11
|
68 CONV_FUNC(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_S32, *(const int32_t*)pi*(1.0 / (1U<<31)))
|
yading@11
|
69 CONV_FUNC(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8( lrintf(*(const float*)pi * (1<<7)) + 0x80))
|
yading@11
|
70 CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16( lrintf(*(const float*)pi * (1<<15))))
|
yading@11
|
71 CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float*)pi * (1U<<31))))
|
yading@11
|
72 CONV_FUNC(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_FLT, *(const float*)pi)
|
yading@11
|
73 CONV_FUNC(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_FLT, *(const float*)pi)
|
yading@11
|
74 CONV_FUNC(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8( lrint(*(const double*)pi * (1<<7)) + 0x80))
|
yading@11
|
75 CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16( lrint(*(const double*)pi * (1<<15))))
|
yading@11
|
76 CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double*)pi * (1U<<31))))
|
yading@11
|
77 CONV_FUNC(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_DBL, *(const double*)pi)
|
yading@11
|
78 CONV_FUNC(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_DBL, *(const double*)pi)
|
yading@11
|
79
|
yading@11
|
80 #define FMT_PAIR_FUNC(out, in) [out + AV_SAMPLE_FMT_NB*in] = CONV_FUNC_NAME(out, in)
|
yading@11
|
81
|
yading@11
|
82 static conv_func_type * const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB*AV_SAMPLE_FMT_NB] = {
|
yading@11
|
83 FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8 , AV_SAMPLE_FMT_U8 ),
|
yading@11
|
84 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8 ),
|
yading@11
|
85 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8 ),
|
yading@11
|
86 FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8 ),
|
yading@11
|
87 FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8 ),
|
yading@11
|
88 FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8 , AV_SAMPLE_FMT_S16),
|
yading@11
|
89 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16),
|
yading@11
|
90 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16),
|
yading@11
|
91 FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16),
|
yading@11
|
92 FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16),
|
yading@11
|
93 FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8 , AV_SAMPLE_FMT_S32),
|
yading@11
|
94 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32),
|
yading@11
|
95 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32),
|
yading@11
|
96 FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32),
|
yading@11
|
97 FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32),
|
yading@11
|
98 FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8 , AV_SAMPLE_FMT_FLT),
|
yading@11
|
99 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT),
|
yading@11
|
100 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT),
|
yading@11
|
101 FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT),
|
yading@11
|
102 FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT),
|
yading@11
|
103 FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8 , AV_SAMPLE_FMT_DBL),
|
yading@11
|
104 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL),
|
yading@11
|
105 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL),
|
yading@11
|
106 FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL),
|
yading@11
|
107 FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL),
|
yading@11
|
108 };
|
yading@11
|
109
|
yading@11
|
110 static void cpy1(uint8_t **dst, const uint8_t **src, int len){
|
yading@11
|
111 memcpy(*dst, *src, len);
|
yading@11
|
112 }
|
yading@11
|
113 static void cpy2(uint8_t **dst, const uint8_t **src, int len){
|
yading@11
|
114 memcpy(*dst, *src, 2*len);
|
yading@11
|
115 }
|
yading@11
|
116 static void cpy4(uint8_t **dst, const uint8_t **src, int len){
|
yading@11
|
117 memcpy(*dst, *src, 4*len);
|
yading@11
|
118 }
|
yading@11
|
119 static void cpy8(uint8_t **dst, const uint8_t **src, int len){
|
yading@11
|
120 memcpy(*dst, *src, 8*len);
|
yading@11
|
121 }
|
yading@11
|
122
|
yading@11
|
123 AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt,
|
yading@11
|
124 enum AVSampleFormat in_fmt,
|
yading@11
|
125 int channels, const int *ch_map,
|
yading@11
|
126 int flags)
|
yading@11
|
127 {
|
yading@11
|
128 AudioConvert *ctx;
|
yading@11
|
129 conv_func_type *f = fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt) + AV_SAMPLE_FMT_NB*av_get_packed_sample_fmt(in_fmt)];
|
yading@11
|
130
|
yading@11
|
131 if (!f)
|
yading@11
|
132 return NULL;
|
yading@11
|
133 ctx = av_mallocz(sizeof(*ctx));
|
yading@11
|
134 if (!ctx)
|
yading@11
|
135 return NULL;
|
yading@11
|
136
|
yading@11
|
137 if(channels == 1){
|
yading@11
|
138 in_fmt = av_get_planar_sample_fmt( in_fmt);
|
yading@11
|
139 out_fmt = av_get_planar_sample_fmt(out_fmt);
|
yading@11
|
140 }
|
yading@11
|
141
|
yading@11
|
142 ctx->channels = channels;
|
yading@11
|
143 ctx->conv_f = f;
|
yading@11
|
144 ctx->ch_map = ch_map;
|
yading@11
|
145 if (in_fmt == AV_SAMPLE_FMT_U8 || in_fmt == AV_SAMPLE_FMT_U8P)
|
yading@11
|
146 memset(ctx->silence, 0x80, sizeof(ctx->silence));
|
yading@11
|
147
|
yading@11
|
148 if(out_fmt == in_fmt && !ch_map) {
|
yading@11
|
149 switch(av_get_bytes_per_sample(in_fmt)){
|
yading@11
|
150 case 1:ctx->simd_f = cpy1; break;
|
yading@11
|
151 case 2:ctx->simd_f = cpy2; break;
|
yading@11
|
152 case 4:ctx->simd_f = cpy4; break;
|
yading@11
|
153 case 8:ctx->simd_f = cpy8; break;
|
yading@11
|
154 }
|
yading@11
|
155 }
|
yading@11
|
156
|
yading@11
|
157 if(HAVE_YASM && HAVE_MMX) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);
|
yading@11
|
158 if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);
|
yading@11
|
159
|
yading@11
|
160 return ctx;
|
yading@11
|
161 }
|
yading@11
|
162
|
yading@11
|
163 void swri_audio_convert_free(AudioConvert **ctx)
|
yading@11
|
164 {
|
yading@11
|
165 av_freep(ctx);
|
yading@11
|
166 }
|
yading@11
|
167
|
yading@11
|
168 int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len)
|
yading@11
|
169 {
|
yading@11
|
170 int ch;
|
yading@11
|
171 int off=0;
|
yading@11
|
172 const int os= (out->planar ? 1 :out->ch_count) *out->bps;
|
yading@11
|
173 unsigned misaligned = 0;
|
yading@11
|
174
|
yading@11
|
175 av_assert0(ctx->channels == out->ch_count);
|
yading@11
|
176
|
yading@11
|
177 if (ctx->in_simd_align_mask) {
|
yading@11
|
178 int planes = in->planar ? in->ch_count : 1;
|
yading@11
|
179 unsigned m = 0;
|
yading@11
|
180 for (ch = 0; ch < planes; ch++)
|
yading@11
|
181 m |= (intptr_t)in->ch[ch];
|
yading@11
|
182 misaligned |= m & ctx->in_simd_align_mask;
|
yading@11
|
183 }
|
yading@11
|
184 if (ctx->out_simd_align_mask) {
|
yading@11
|
185 int planes = out->planar ? out->ch_count : 1;
|
yading@11
|
186 unsigned m = 0;
|
yading@11
|
187 for (ch = 0; ch < planes; ch++)
|
yading@11
|
188 m |= (intptr_t)out->ch[ch];
|
yading@11
|
189 misaligned |= m & ctx->out_simd_align_mask;
|
yading@11
|
190 }
|
yading@11
|
191
|
yading@11
|
192 //FIXME optimize common cases
|
yading@11
|
193
|
yading@11
|
194 if(ctx->simd_f && !ctx->ch_map && !misaligned){
|
yading@11
|
195 off = len&~15;
|
yading@11
|
196 av_assert1(off>=0);
|
yading@11
|
197 av_assert1(off<=len);
|
yading@11
|
198 av_assert2(ctx->channels == SWR_CH_MAX || !in->ch[ctx->channels]);
|
yading@11
|
199 if(off>0){
|
yading@11
|
200 if(out->planar == in->planar){
|
yading@11
|
201 int planes = out->planar ? out->ch_count : 1;
|
yading@11
|
202 for(ch=0; ch<planes; ch++){
|
yading@11
|
203 ctx->simd_f(out->ch+ch, (const uint8_t **)in->ch+ch, off * (out->planar ? 1 :out->ch_count));
|
yading@11
|
204 }
|
yading@11
|
205 }else{
|
yading@11
|
206 ctx->simd_f(out->ch, (const uint8_t **)in->ch, off);
|
yading@11
|
207 }
|
yading@11
|
208 }
|
yading@11
|
209 if(off == len)
|
yading@11
|
210 return 0;
|
yading@11
|
211 }
|
yading@11
|
212
|
yading@11
|
213 for(ch=0; ch<ctx->channels; ch++){
|
yading@11
|
214 const int ich= ctx->ch_map ? ctx->ch_map[ch] : ch;
|
yading@11
|
215 const int is= ich < 0 ? 0 : (in->planar ? 1 : in->ch_count) * in->bps;
|
yading@11
|
216 const uint8_t *pi= ich < 0 ? ctx->silence : in->ch[ich];
|
yading@11
|
217 uint8_t *po= out->ch[ch];
|
yading@11
|
218 uint8_t *end= po + os*len;
|
yading@11
|
219 if(!po)
|
yading@11
|
220 continue;
|
yading@11
|
221 ctx->conv_f(po+off*os, pi+off*is, is, os, end);
|
yading@11
|
222 }
|
yading@11
|
223 return 0;
|
yading@11
|
224 }
|