2 * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
5 * This file is part of Libav.
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.
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.
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
25 #include "libavutil/common.h"
26 #include "libavutil/libm.h"
27 #include "libavutil/log.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/samplefmt.h"
30 #include "audio_convert.h"
31 #include "audio_data.h"
35 CONV_FUNC_TYPE_INTERLEAVE,
36 CONV_FUNC_TYPE_DEINTERLEAVE,
39 typedef void (conv_func_flat)(uint8_t *out, const uint8_t *in, int len);
41 typedef void (conv_func_interleave)(uint8_t *out, uint8_t *const *in,
42 int len, int channels);
44 typedef void (conv_func_deinterleave)(uint8_t **out, const uint8_t *in, int len,
48 AVAudioResampleContext *avr;
49 enum AVSampleFormat in_fmt;
50 enum AVSampleFormat out_fmt;
55 int has_optimized_func;
56 const char *func_descr;
57 const char *func_descr_generic;
58 enum ConvFuncType func_type;
59 conv_func_flat *conv_flat;
60 conv_func_flat *conv_flat_generic;
61 conv_func_interleave *conv_interleave;
62 conv_func_interleave *conv_interleave_generic;
63 conv_func_deinterleave *conv_deinterleave;
64 conv_func_deinterleave *conv_deinterleave_generic;
67 void ff_audio_convert_set_func(AudioConvert *ac, enum AVSampleFormat out_fmt,
68 enum AVSampleFormat in_fmt, int channels,
69 int ptr_align, int samples_align,
70 const char *descr, void *conv)
74 switch (ac->func_type) {
75 case CONV_FUNC_TYPE_FLAT:
76 if (av_get_packed_sample_fmt(ac->in_fmt) == in_fmt &&
77 av_get_packed_sample_fmt(ac->out_fmt) == out_fmt) {
79 ac->func_descr = descr;
80 ac->ptr_align = ptr_align;
81 ac->samples_align = samples_align;
82 if (ptr_align == 1 && samples_align == 1) {
83 ac->conv_flat_generic = conv;
84 ac->func_descr_generic = descr;
86 ac->has_optimized_func = 1;
91 case CONV_FUNC_TYPE_INTERLEAVE:
92 if (ac->in_fmt == in_fmt && ac->out_fmt == out_fmt &&
93 (!channels || ac->channels == channels)) {
94 ac->conv_interleave = conv;
95 ac->func_descr = descr;
96 ac->ptr_align = ptr_align;
97 ac->samples_align = samples_align;
98 if (ptr_align == 1 && samples_align == 1) {
99 ac->conv_interleave_generic = conv;
100 ac->func_descr_generic = descr;
102 ac->has_optimized_func = 1;
107 case CONV_FUNC_TYPE_DEINTERLEAVE:
108 if (ac->in_fmt == in_fmt && ac->out_fmt == out_fmt &&
109 (!channels || ac->channels == channels)) {
110 ac->conv_deinterleave = conv;
111 ac->func_descr = descr;
112 ac->ptr_align = ptr_align;
113 ac->samples_align = samples_align;
114 if (ptr_align == 1 && samples_align == 1) {
115 ac->conv_deinterleave_generic = conv;
116 ac->func_descr_generic = descr;
118 ac->has_optimized_func = 1;
125 av_log(ac->avr, AV_LOG_DEBUG, "audio_convert: found function: %-4s "
126 "to %-4s (%s)\n", av_get_sample_fmt_name(ac->in_fmt),
127 av_get_sample_fmt_name(ac->out_fmt), descr);
131 #define CONV_FUNC_NAME(dst_fmt, src_fmt) conv_ ## src_fmt ## _to_ ## dst_fmt
133 #define CONV_LOOP(otype, expr) \
135 *(otype *)po = expr; \
138 } while (po < end); \
140 #define CONV_FUNC_FLAT(ofmt, otype, ifmt, itype, expr) \
141 static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t *out, const uint8_t *in, \
144 int is = sizeof(itype); \
145 int os = sizeof(otype); \
146 const uint8_t *pi = in; \
148 uint8_t *end = out + os * len; \
149 CONV_LOOP(otype, expr) \
152 #define CONV_FUNC_INTERLEAVE(ofmt, otype, ifmt, itype, expr) \
153 static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t *out, const uint8_t **in, \
154 int len, int channels) \
157 int out_bps = sizeof(otype); \
158 int is = sizeof(itype); \
159 int os = channels * out_bps; \
160 for (ch = 0; ch < channels; ch++) { \
161 const uint8_t *pi = in[ch]; \
162 uint8_t *po = out + ch * out_bps; \
163 uint8_t *end = po + os * len; \
164 CONV_LOOP(otype, expr) \
168 #define CONV_FUNC_DEINTERLEAVE(ofmt, otype, ifmt, itype, expr) \
169 static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t **out, const uint8_t *in, \
170 int len, int channels) \
173 int in_bps = sizeof(itype); \
174 int is = channels * in_bps; \
175 int os = sizeof(otype); \
176 for (ch = 0; ch < channels; ch++) { \
177 const uint8_t *pi = in + ch * in_bps; \
178 uint8_t *po = out[ch]; \
179 uint8_t *end = po + os * len; \
180 CONV_LOOP(otype, expr) \
184 #define CONV_FUNC_GROUP(ofmt, otype, ifmt, itype, expr) \
185 CONV_FUNC_FLAT( ofmt, otype, ifmt, itype, expr) \
186 CONV_FUNC_INTERLEAVE( ofmt, otype, ifmt ## P, itype, expr) \
187 CONV_FUNC_DEINTERLEAVE(ofmt ## P, otype, ifmt, itype, expr)
189 CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_U8, uint8_t, *(const uint8_t *)pi)
190 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_U8, uint8_t, (*(const uint8_t *)pi - 0x80) << 8)
191 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_U8, uint8_t, (*(const uint8_t *)pi - 0x80) << 24)
192 CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t, (*(const uint8_t *)pi - 0x80) * (1.0f / (1 << 7)))
193 CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t, (*(const uint8_t *)pi - 0x80) * (1.0 / (1 << 7)))
194 CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t, (*(const int16_t *)pi >> 8) + 0x80)
195 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi)
196 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi << 16)
197 CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi * (1.0f / (1 << 15)))
198 CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi * (1.0 / (1 << 15)))
199 CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t, (*(const int32_t *)pi >> 24) + 0x80)
200 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi >> 16)
201 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi)
202 CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi * (1.0f / (1U << 31)))
203 CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi * (1.0 / (1U << 31)))
204 CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8( lrintf(*(const float *)pi * (1 << 7)) + 0x80))
205 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16( lrintf(*(const float *)pi * (1 << 15))))
206 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *)pi * (1U << 31))))
207 CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_FLT, float, *(const float *)pi)
208 CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_FLT, float, *(const float *)pi)
209 CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8( lrint(*(const double *)pi * (1 << 7)) + 0x80))
210 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16( lrint(*(const double *)pi * (1 << 15))))
211 CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *)pi * (1U << 31))))
212 CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_DBL, double, *(const double *)pi)
213 CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_DBL, double, *(const double *)pi)
215 #define SET_CONV_FUNC_GROUP(ofmt, ifmt) \
216 ff_audio_convert_set_func(ac, ofmt, ifmt, 0, 1, 1, "C", CONV_FUNC_NAME(ofmt, ifmt)); \
217 ff_audio_convert_set_func(ac, ofmt ## P, ifmt, 0, 1, 1, "C", CONV_FUNC_NAME(ofmt ## P, ifmt)); \
218 ff_audio_convert_set_func(ac, ofmt, ifmt ## P, 0, 1, 1, "C", CONV_FUNC_NAME(ofmt, ifmt ## P));
220 static void set_generic_function(AudioConvert *ac)
222 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8)
223 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8)
224 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8)
225 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8)
226 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8)
227 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16)
228 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16)
229 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16)
230 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16)
231 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16)
232 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32)
233 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32)
234 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32)
235 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32)
236 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32)
237 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT)
238 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT)
239 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT)
240 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT)
241 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT)
242 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL)
243 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL)
244 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL)
245 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL)
246 SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL)
249 AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr,
250 enum AVSampleFormat out_fmt,
251 enum AVSampleFormat in_fmt,
255 int in_planar, out_planar;
257 ac = av_mallocz(sizeof(*ac));
262 ac->out_fmt = out_fmt;
264 ac->channels = channels;
266 in_planar = av_sample_fmt_is_planar(in_fmt);
267 out_planar = av_sample_fmt_is_planar(out_fmt);
269 if (in_planar == out_planar) {
270 ac->func_type = CONV_FUNC_TYPE_FLAT;
271 ac->planes = in_planar ? ac->channels : 1;
272 } else if (in_planar)
273 ac->func_type = CONV_FUNC_TYPE_INTERLEAVE;
275 ac->func_type = CONV_FUNC_TYPE_DEINTERLEAVE;
277 set_generic_function(ac);
280 ff_audio_convert_init_arm(ac);
282 ff_audio_convert_init_x86(ac);
287 int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in, int len)
291 /* determine whether to use the optimized function based on pointer and
292 samples alignment in both the input and output */
293 if (ac->has_optimized_func) {
294 int ptr_align = FFMIN(in->ptr_align, out->ptr_align);
295 int samples_align = FFMIN(in->samples_align, out->samples_align);
296 int aligned_len = FFALIGN(len, ac->samples_align);
297 if (!(ptr_align % ac->ptr_align) && samples_align >= aligned_len) {
302 av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (%s)\n", len,
303 av_get_sample_fmt_name(ac->in_fmt),
304 av_get_sample_fmt_name(ac->out_fmt),
305 use_generic ? ac->func_descr_generic : ac->func_descr);
307 switch (ac->func_type) {
308 case CONV_FUNC_TYPE_FLAT: {
313 for (p = 0; p < ac->planes; p++)
314 ac->conv_flat_generic(out->data[p], in->data[p], len);
316 for (p = 0; p < ac->planes; p++)
317 ac->conv_flat(out->data[p], in->data[p], len);
321 case CONV_FUNC_TYPE_INTERLEAVE:
323 ac->conv_interleave_generic(out->data[0], in->data, len, ac->channels);
325 ac->conv_interleave(out->data[0], in->data, len, ac->channels);
327 case CONV_FUNC_TYPE_DEINTERLEAVE:
329 ac->conv_deinterleave_generic(out->data, in->data[0], len, ac->channels);
331 ac->conv_deinterleave(out->data, in->data[0], len, ac->channels);
335 out->nb_samples = in->nb_samples;