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/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"
34 CONV_FUNC_TYPE_INTERLEAVE,
35 CONV_FUNC_TYPE_DEINTERLEAVE,
38 typedef void (conv_func_flat)(uint8_t *out, const uint8_t *in, int len);
40 typedef void (conv_func_interleave)(uint8_t *out, uint8_t *const *in,
41 int len, int channels);
43 typedef void (conv_func_deinterleave)(uint8_t **out, const uint8_t *in, int len,
47 AVAudioResampleContext *avr;
48 enum AVSampleFormat in_fmt;
49 enum AVSampleFormat out_fmt;
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;
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)
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) {
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;
85 ac->has_optimized_func = 1;
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;
101 ac->has_optimized_func = 1;
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;
117 ac->has_optimized_func = 1;
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);
130 #define CONV_FUNC_NAME(dst_fmt, src_fmt) conv_ ## src_fmt ## _to_ ## dst_fmt
132 #define CONV_LOOP(otype, expr) \
134 *(otype *)po = expr; \
137 } while (po < end); \
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, \
143 int is = sizeof(itype); \
144 int os = sizeof(otype); \
145 const uint8_t *pi = in; \
147 uint8_t *end = out + os * len; \
148 CONV_LOOP(otype, expr) \
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) \
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) \
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) \
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) \
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)
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)
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));
219 static void set_generic_function(AudioConvert *ac)
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)
248 AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr,
249 enum AVSampleFormat out_fmt,
250 enum AVSampleFormat in_fmt,
254 int in_planar, out_planar;
256 ac = av_mallocz(sizeof(*ac));
261 ac->out_fmt = out_fmt;
263 ac->channels = channels;
265 in_planar = av_sample_fmt_is_planar(in_fmt);
266 out_planar = av_sample_fmt_is_planar(out_fmt);
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;
274 ac->func_type = CONV_FUNC_TYPE_DEINTERLEAVE;
276 set_generic_function(ac);
279 ff_audio_convert_init_x86(ac);
284 int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in, int len)
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) {
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);
304 switch (ac->func_type) {
305 case CONV_FUNC_TYPE_FLAT: {
310 for (p = 0; p < ac->planes; p++)
311 ac->conv_flat_generic(out->data[p], in->data[p], len);
313 for (p = 0; p < ac->planes; p++)
314 ac->conv_flat(out->data[p], in->data[p], len);
318 case CONV_FUNC_TYPE_INTERLEAVE:
320 ac->conv_interleave_generic(out->data[0], in->data, len, ac->channels);
322 ac->conv_interleave(out->data[0], in->data, len, ac->channels);
324 case CONV_FUNC_TYPE_DEINTERLEAVE:
326 ac->conv_deinterleave_generic(out->data, in->data[0], len, ac->channels);
328 ac->conv_deinterleave(out->data, in->data[0], len, ac->channels);
332 out->nb_samples = in->nb_samples;