Add libavresample
[libav.git] / libavresample / audio_convert.c
1 /*
2 * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdint.h>
23
24 #include "config.h"
25 #include "libavutil/libm.h"
26 #include "libavutil/log.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/samplefmt.h"
29 #include "audio_convert.h"
30 #include "audio_data.h"
31
32 enum ConvFuncType {
33 CONV_FUNC_TYPE_FLAT,
34 CONV_FUNC_TYPE_INTERLEAVE,
35 CONV_FUNC_TYPE_DEINTERLEAVE,
36 };
37
38 typedef void (conv_func_flat)(uint8_t *out, const uint8_t *in, int len);
39
40 typedef void (conv_func_interleave)(uint8_t *out, uint8_t *const *in,
41 int len, int channels);
42
43 typedef void (conv_func_deinterleave)(uint8_t **out, const uint8_t *in, int len,
44 int channels);
45
46 struct AudioConvert {
47 AVAudioResampleContext *avr;
48 enum AVSampleFormat in_fmt;
49 enum AVSampleFormat out_fmt;
50 int channels;
51 int planes;
52 int ptr_align;
53 int samples_align;
54 int has_optimized_func;
55 const char *func_descr;
56 const char *func_descr_generic;
57 enum ConvFuncType func_type;
58 conv_func_flat *conv_flat;
59 conv_func_flat *conv_flat_generic;
60 conv_func_interleave *conv_interleave;
61 conv_func_interleave *conv_interleave_generic;
62 conv_func_deinterleave *conv_deinterleave;
63 conv_func_deinterleave *conv_deinterleave_generic;
64 };
65
66 void ff_audio_convert_set_func(AudioConvert *ac, enum AVSampleFormat out_fmt,
67 enum AVSampleFormat in_fmt, int channels,
68 int ptr_align, int samples_align,
69 const char *descr, void *conv)
70 {
71 int found = 0;
72
73 switch (ac->func_type) {
74 case CONV_FUNC_TYPE_FLAT:
75 if (av_get_packed_sample_fmt(ac->in_fmt) == in_fmt &&
76 av_get_packed_sample_fmt(ac->out_fmt) == out_fmt) {
77 ac->conv_flat = conv;
78 ac->func_descr = descr;
79 ac->ptr_align = ptr_align;
80 ac->samples_align = samples_align;
81 if (ptr_align == 1 && samples_align == 1) {
82 ac->conv_flat_generic = conv;
83 ac->func_descr_generic = descr;
84 } else {
85 ac->has_optimized_func = 1;
86 }
87 found = 1;
88 }
89 break;
90 case CONV_FUNC_TYPE_INTERLEAVE:
91 if (ac->in_fmt == in_fmt && ac->out_fmt == out_fmt &&
92 (!channels || ac->channels == channels)) {
93 ac->conv_interleave = conv;
94 ac->func_descr = descr;
95 ac->ptr_align = ptr_align;
96 ac->samples_align = samples_align;
97 if (ptr_align == 1 && samples_align == 1) {
98 ac->conv_interleave_generic = conv;
99 ac->func_descr_generic = descr;
100 } else {
101 ac->has_optimized_func = 1;
102 }
103 found = 1;
104 }
105 break;
106 case CONV_FUNC_TYPE_DEINTERLEAVE:
107 if (ac->in_fmt == in_fmt && ac->out_fmt == out_fmt &&
108 (!channels || ac->channels == channels)) {
109 ac->conv_deinterleave = conv;
110 ac->func_descr = descr;
111 ac->ptr_align = ptr_align;
112 ac->samples_align = samples_align;
113 if (ptr_align == 1 && samples_align == 1) {
114 ac->conv_deinterleave_generic = conv;
115 ac->func_descr_generic = descr;
116 } else {
117 ac->has_optimized_func = 1;
118 }
119 found = 1;
120 }
121 break;
122 }
123 if (found) {
124 av_log(ac->avr, AV_LOG_DEBUG, "audio_convert: found function: %-4s "
125 "to %-4s (%s)\n", av_get_sample_fmt_name(ac->in_fmt),
126 av_get_sample_fmt_name(ac->out_fmt), descr);
127 }
128 }
129
130 #define CONV_FUNC_NAME(dst_fmt, src_fmt) conv_ ## src_fmt ## _to_ ## dst_fmt
131
132 #define CONV_LOOP(otype, expr) \
133 do { \
134 *(otype *)po = expr; \
135 pi += is; \
136 po += os; \
137 } while (po < end); \
138
139 #define CONV_FUNC_FLAT(ofmt, otype, ifmt, itype, expr) \
140 static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t *out, const uint8_t *in, \
141 int len) \
142 { \
143 int is = sizeof(itype); \
144 int os = sizeof(otype); \
145 const uint8_t *pi = in; \
146 uint8_t *po = out; \
147 uint8_t *end = out + os * len; \
148 CONV_LOOP(otype, expr) \
149 }
150
151 #define CONV_FUNC_INTERLEAVE(ofmt, otype, ifmt, itype, expr) \
152 static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t *out, const uint8_t **in, \
153 int len, int channels) \
154 { \
155 int ch; \
156 int out_bps = sizeof(otype); \
157 int is = sizeof(itype); \
158 int os = channels * out_bps; \
159 for (ch = 0; ch < channels; ch++) { \
160 const uint8_t *pi = in[ch]; \
161 uint8_t *po = out + ch * out_bps; \
162 uint8_t *end = po + os * len; \
163 CONV_LOOP(otype, expr) \
164 } \
165 }
166
167 #define CONV_FUNC_DEINTERLEAVE(ofmt, otype, ifmt, itype, expr) \
168 static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t **out, const uint8_t *in, \
169 int len, int channels) \
170 { \
171 int ch; \
172 int in_bps = sizeof(itype); \
173 int is = channels * in_bps; \
174 int os = sizeof(otype); \
175 for (ch = 0; ch < channels; ch++) { \
176 const uint8_t *pi = in + ch * in_bps; \
177 uint8_t *po = out[ch]; \
178 uint8_t *end = po + os * len; \
179 CONV_LOOP(otype, expr) \
180 } \
181 }
182
183 #define CONV_FUNC_GROUP(ofmt, otype, ifmt, itype, expr) \
184 CONV_FUNC_FLAT( ofmt, otype, ifmt, itype, expr) \
185 CONV_FUNC_INTERLEAVE( ofmt, otype, ifmt ## P, itype, expr) \
186 CONV_FUNC_DEINTERLEAVE(ofmt ## P, otype, ifmt, itype, expr)
187
188 CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_U8, uint8_t, *(const uint8_t *)pi)
189 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_U8, uint8_t, (*(const uint8_t *)pi - 0x80) << 8)
190 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_U8, uint8_t, (*(const uint8_t *)pi - 0x80) << 24)
191 CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t, (*(const uint8_t *)pi - 0x80) * (1.0f / (1 << 7)))
192 CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t, (*(const uint8_t *)pi - 0x80) * (1.0 / (1 << 7)))
193 CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t, (*(const int16_t *)pi >> 8) + 0x80)
194 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi)
195 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi << 16)
196 CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi * (1.0f / (1 << 15)))
197 CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi * (1.0 / (1 << 15)))
198 CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t, (*(const int32_t *)pi >> 24) + 0x80)
199 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi >> 16)
200 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi)
201 CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi * (1.0f / (1U << 31)))
202 CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi * (1.0 / (1U << 31)))
203 CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8( lrintf(*(const float *)pi * (1 << 7)) + 0x80))
204 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16( lrintf(*(const float *)pi * (1 << 15))))
205 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *)pi * (1U << 31))))
206 CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_FLT, float, *(const float *)pi)
207 CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_FLT, float, *(const float *)pi)
208 CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8( lrint(*(const double *)pi * (1 << 7)) + 0x80))
209 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16( lrint(*(const double *)pi * (1 << 15))))
210 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *)pi * (1U << 31))))
211 CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_DBL, double, *(const double *)pi)
212 CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_DBL, double, *(const double *)pi)
213
214 #define SET_CONV_FUNC_GROUP(ofmt, ifmt) \
215 ff_audio_convert_set_func(ac, ofmt, ifmt, 0, 1, 1, "C", CONV_FUNC_NAME(ofmt, ifmt)); \
216 ff_audio_convert_set_func(ac, ofmt ## P, ifmt, 0, 1, 1, "C", CONV_FUNC_NAME(ofmt ## P, ifmt)); \
217 ff_audio_convert_set_func(ac, ofmt, ifmt ## P, 0, 1, 1, "C", CONV_FUNC_NAME(ofmt, ifmt ## P));
218
219 static void set_generic_function(AudioConvert *ac)
220 {
221 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8)
222 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8)
223 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8)
224 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8)
225 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8)
226 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16)
227 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16)
228 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16)
229 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16)
230 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16)
231 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32)
232 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32)
233 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32)
234 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32)
235 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32)
236 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT)
237 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT)
238 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT)
239 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT)
240 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT)
241 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL)
242 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL)
243 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL)
244 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL)
245 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL)
246 }
247
248 AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr,
249 enum AVSampleFormat out_fmt,
250 enum AVSampleFormat in_fmt,
251 int channels)
252 {
253 AudioConvert *ac;
254 int in_planar, out_planar;
255
256 ac = av_mallocz(sizeof(*ac));
257 if (!ac)
258 return NULL;
259
260 ac->avr = avr;
261 ac->out_fmt = out_fmt;
262 ac->in_fmt = in_fmt;
263 ac->channels = channels;
264
265 in_planar = av_sample_fmt_is_planar(in_fmt);
266 out_planar = av_sample_fmt_is_planar(out_fmt);
267
268 if (in_planar == out_planar) {
269 ac->func_type = CONV_FUNC_TYPE_FLAT;
270 ac->planes = in_planar ? ac->channels : 1;
271 } else if (in_planar)
272 ac->func_type = CONV_FUNC_TYPE_INTERLEAVE;
273 else
274 ac->func_type = CONV_FUNC_TYPE_DEINTERLEAVE;
275
276 set_generic_function(ac);
277
278 if (ARCH_X86)
279 ff_audio_convert_init_x86(ac);
280
281 return ac;
282 }
283
284 int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in, int len)
285 {
286 int use_generic = 1;
287
288 /* determine whether to use the optimized function based on pointer and
289 samples alignment in both the input and output */
290 if (ac->has_optimized_func) {
291 int ptr_align = FFMIN(in->ptr_align, out->ptr_align);
292 int samples_align = FFMIN(in->samples_align, out->samples_align);
293 int aligned_len = FFALIGN(len, ac->samples_align);
294 if (!(ptr_align % ac->ptr_align) && samples_align >= aligned_len) {
295 len = aligned_len;
296 use_generic = 0;
297 }
298 }
299 av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (%s)\n", len,
300 av_get_sample_fmt_name(ac->in_fmt),
301 av_get_sample_fmt_name(ac->out_fmt),
302 use_generic ? ac->func_descr_generic : ac->func_descr);
303
304 switch (ac->func_type) {
305 case CONV_FUNC_TYPE_FLAT: {
306 int p;
307 if (!in->is_planar)
308 len *= in->channels;
309 if (use_generic) {
310 for (p = 0; p < ac->planes; p++)
311 ac->conv_flat_generic(out->data[p], in->data[p], len);
312 } else {
313 for (p = 0; p < ac->planes; p++)
314 ac->conv_flat(out->data[p], in->data[p], len);
315 }
316 break;
317 }
318 case CONV_FUNC_TYPE_INTERLEAVE:
319 if (use_generic)
320 ac->conv_interleave_generic(out->data[0], in->data, len, ac->channels);
321 else
322 ac->conv_interleave(out->data[0], in->data, len, ac->channels);
323 break;
324 case CONV_FUNC_TYPE_DEINTERLEAVE:
325 if (use_generic)
326 ac->conv_deinterleave_generic(out->data, in->data[0], len, ac->channels);
327 else
328 ac->conv_deinterleave(out->data, in->data[0], len, ac->channels);
329 break;
330 }
331
332 out->nb_samples = in->nb_samples;
333 return 0;
334 }