]> git.sesse.net Git - ffmpeg/blob - libavresample/audio_convert.c
av_parse_cpu_caps: add "mmxext" as alias for mmx2
[ffmpeg] / 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/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"
32
33 enum ConvFuncType {
34     CONV_FUNC_TYPE_FLAT,
35     CONV_FUNC_TYPE_INTERLEAVE,
36     CONV_FUNC_TYPE_DEINTERLEAVE,
37 };
38
39 typedef void (conv_func_flat)(uint8_t *out, const uint8_t *in, int len);
40
41 typedef void (conv_func_interleave)(uint8_t *out, uint8_t *const *in,
42                                     int len, int channels);
43
44 typedef void (conv_func_deinterleave)(uint8_t **out, const uint8_t *in, int len,
45                                       int channels);
46
47 struct AudioConvert {
48     AVAudioResampleContext *avr;
49     enum AVSampleFormat in_fmt;
50     enum AVSampleFormat out_fmt;
51     int channels;
52     int planes;
53     int ptr_align;
54     int samples_align;
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;
65 };
66
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)
71 {
72     int found = 0;
73
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) {
78             ac->conv_flat     = conv;
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;
85             } else {
86                 ac->has_optimized_func = 1;
87             }
88             found = 1;
89         }
90         break;
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;
101             } else {
102                 ac->has_optimized_func = 1;
103             }
104             found = 1;
105         }
106         break;
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;
117             } else {
118                 ac->has_optimized_func = 1;
119             }
120             found = 1;
121         }
122         break;
123     }
124     if (found) {
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);
128     }
129 }
130
131 #define CONV_FUNC_NAME(dst_fmt, src_fmt) conv_ ## src_fmt ## _to_ ## dst_fmt
132
133 #define CONV_LOOP(otype, expr)                                              \
134     do {                                                                    \
135         *(otype *)po = expr;                                                \
136         pi += is;                                                           \
137         po += os;                                                           \
138     } while (po < end);                                                     \
139
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,     \
142                                        int len)                             \
143 {                                                                           \
144     int is       = sizeof(itype);                                           \
145     int os       = sizeof(otype);                                           \
146     const uint8_t *pi = in;                                                 \
147     uint8_t       *po = out;                                                \
148     uint8_t *end = out + os * len;                                          \
149     CONV_LOOP(otype, expr)                                                  \
150 }
151
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)               \
155 {                                                                           \
156     int ch;                                                                 \
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)                                              \
165     }                                                                       \
166 }
167
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)               \
171 {                                                                           \
172     int ch;                                                                 \
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)                                              \
181     }                                                                       \
182 }
183
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)
188
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)
214
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));
219
220 static void set_generic_function(AudioConvert *ac)
221 {
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)
247 }
248
249 AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr,
250                                      enum AVSampleFormat out_fmt,
251                                      enum AVSampleFormat in_fmt,
252                                      int channels)
253 {
254     AudioConvert *ac;
255     int in_planar, out_planar;
256
257     ac = av_mallocz(sizeof(*ac));
258     if (!ac)
259         return NULL;
260
261     ac->avr      = avr;
262     ac->out_fmt  = out_fmt;
263     ac->in_fmt   = in_fmt;
264     ac->channels = channels;
265
266     in_planar  = av_sample_fmt_is_planar(in_fmt);
267     out_planar = av_sample_fmt_is_planar(out_fmt);
268
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;
274     else
275         ac->func_type = CONV_FUNC_TYPE_DEINTERLEAVE;
276
277     set_generic_function(ac);
278
279     if (ARCH_X86)
280         ff_audio_convert_init_x86(ac);
281
282     return ac;
283 }
284
285 int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in, int len)
286 {
287     int use_generic = 1;
288
289     /* determine whether to use the optimized function based on pointer and
290        samples alignment in both the input and output */
291     if (ac->has_optimized_func) {
292         int ptr_align     = FFMIN(in->ptr_align,     out->ptr_align);
293         int samples_align = FFMIN(in->samples_align, out->samples_align);
294         int aligned_len   = FFALIGN(len, ac->samples_align);
295         if (!(ptr_align % ac->ptr_align) && samples_align >= aligned_len) {
296             len = aligned_len;
297             use_generic = 0;
298         }
299     }
300     av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (%s)\n", len,
301             av_get_sample_fmt_name(ac->in_fmt),
302             av_get_sample_fmt_name(ac->out_fmt),
303             use_generic ? ac->func_descr_generic : ac->func_descr);
304
305     switch (ac->func_type) {
306     case CONV_FUNC_TYPE_FLAT: {
307         int p;
308         if (!in->is_planar)
309             len *= in->channels;
310         if (use_generic) {
311             for (p = 0; p < ac->planes; p++)
312                 ac->conv_flat_generic(out->data[p], in->data[p], len);
313         } else {
314             for (p = 0; p < ac->planes; p++)
315                 ac->conv_flat(out->data[p], in->data[p], len);
316         }
317         break;
318     }
319     case CONV_FUNC_TYPE_INTERLEAVE:
320         if (use_generic)
321             ac->conv_interleave_generic(out->data[0], in->data, len, ac->channels);
322         else
323             ac->conv_interleave(out->data[0], in->data, len, ac->channels);
324         break;
325     case CONV_FUNC_TYPE_DEINTERLEAVE:
326         if (use_generic)
327             ac->conv_deinterleave_generic(out->data, in->data[0], len, ac->channels);
328         else
329             ac->conv_deinterleave(out->data, in->data[0], len, ac->channels);
330         break;
331     }
332
333     out->nb_samples = in->nb_samples;
334     return 0;
335 }