Browse code

lavfi: add audio convert filter

Add aconvert filter to perform sample format, channel layout, and
packing format conversion.

The aconvert code depends on audio conversion code in libavcodec, so
this requires a dependency on libavcodec.

Based on previous work by S.N. Hemanth Meenakshisundaram and Mina Nagy
Zaki, performed for the GSoC 2010 and 2011.

Stefano Sabatini authored on 2011/07/04 17:35:39
Showing 8 changed files
... ...
@@ -55,6 +55,7 @@ easier to use. The changes are:
55 55
 - H.264 Decoding on Android via Stagefright
56 56
 - Prores decoder
57 57
 - BIN/XBIN/ADF/IDF text file decoder
58
+- aconvert audio filter added
58 59
 
59 60
 
60 61
 version 0.8:
... ...
@@ -1575,6 +1575,7 @@ udp_protocol_deps="network"
1575 1575
 
1576 1576
 # filters
1577 1577
 abuffer_filter_deps="strtok_r"
1578
+aconvert_filter_deps="strtok_r"
1578 1579
 aformat_filter_deps="strtok_r"
1579 1580
 amovie_filter_deps="avcodec avformat"
1580 1581
 blackframe_filter_deps="gpl"
... ...
@@ -99,6 +99,42 @@ build.
99 99
 
100 100
 Below is a description of the currently available audio filters.
101 101
 
102
+@section aconvert
103
+
104
+Convert the input audio format to the specified formats.
105
+
106
+The filter accepts a string of the form:
107
+"@var{sample_format}:@var{channel_layout}:@var{packing_format}".
108
+
109
+@var{sample_format} specifies the sample format, and can be a string or
110
+the corresponding numeric value defined in @file{libavutil/samplefmt.h}.
111
+
112
+@var{channel_layout} specifies the channel layout, and can be a string
113
+or the corresponding numer value defined in @file{libavutil/chlayout.h}.
114
+
115
+@var{packing_format} specifies the type of packing in output, can be one
116
+of "planar" or "packed", or the corresponding numeric values "0" or "1".
117
+
118
+The special parameter "auto", signifies that the filter will
119
+automatically select the output format depending on the output filter.
120
+
121
+Some examples follow.
122
+
123
+@itemize
124
+@item
125
+Convert input to unsigned 8-bit, stereo, packed:
126
+@example
127
+aconvert=u8:stereo:packed
128
+@end example
129
+
130
+@item
131
+Convert input to unsigned 8-bit, automatically select out channel layout
132
+and packing format:
133
+@example
134
+aconvert=u8:auto:auto
135
+@end example
136
+@end itemize
137
+
102 138
 @section aformat
103 139
 
104 140
 Convert the input audio to one of the specified formats. The framework will
... ...
@@ -2,6 +2,8 @@ include $(SUBDIR)../config.mak
2 2
 
3 3
 NAME = avfilter
4 4
 FFLIBS = avutil
5
+
6
+FFLIBS-$(CONFIG_ACONVERT_FILTER) += avcodec
5 7
 FFLIBS-$(CONFIG_AMOVIE_FILTER) += avformat avcodec
6 8
 FFLIBS-$(CONFIG_ARESAMPLE_FILTER) += avcodec
7 9
 FFLIBS-$(CONFIG_MOVIE_FILTER) += avformat avcodec
... ...
@@ -20,6 +22,7 @@ OBJS = allfilters.o                                                     \
20 20
 
21 21
 OBJS-$(CONFIG_AVCODEC)                       += avcodec.o
22 22
 
23
+OBJS-$(CONFIG_ACONVERT_FILTER)               += af_aconvert.o
23 24
 OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
24 25
 OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
25 26
 OBJS-$(CONFIG_ARESAMPLE_FILTER)              += af_aresample.o
26 27
new file mode 100644
... ...
@@ -0,0 +1,417 @@
0
+/*
1
+ * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram <smeenaks@ucsd.edu>
2
+ * Copyright (c) 2011 Stefano Sabatini
3
+ * Copyright (c) 2011 Mina Nagy Zaki
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg 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
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+/**
23
+ * @file
24
+ * sample format and channel layout conversion audio filter
25
+ * based on code in libavcodec/resample.c by Fabrice Bellard and
26
+ * libavcodec/audioconvert.c by Michael Niedermayer
27
+ */
28
+
29
+#include "libavutil/audioconvert.h"
30
+#include "libavcodec/audioconvert.h"
31
+#include "avfilter.h"
32
+#include "internal.h"
33
+
34
+typedef struct {
35
+    enum AVSampleFormat  out_sample_fmt,  in_sample_fmt;   ///< in/out sample formats
36
+    int64_t              out_chlayout,    in_chlayout;     ///< in/out channel layout
37
+    int                  out_nb_channels, in_nb_channels;  ///< number of in/output channels
38
+    enum AVFilterPacking out_packing_fmt, in_packing_fmt;  ///< output packing format
39
+
40
+    int max_nb_samples;                     ///< maximum number of buffered samples
41
+    AVFilterBufferRef *mix_samplesref;      ///< rematrixed buffer
42
+    AVFilterBufferRef *out_samplesref;      ///< output buffer after required conversions
43
+
44
+    uint8_t *in_mix[8], *out_mix[8];        ///< input/output for rematrixing functions
45
+    uint8_t *packed_data[8];                ///< pointers for packing conversion
46
+    int out_strides[8], in_strides[8];      ///< input/output strides for av_audio_convert
47
+    uint8_t **in_conv, **out_conv;          ///< input/output for av_audio_convert
48
+
49
+    AVAudioConvert *audioconvert_ctx;       ///< context for conversion to output sample format
50
+
51
+    void (*convert_chlayout)();             ///< function to do the requested rematrixing
52
+} AConvertContext;
53
+
54
+#define REMATRIX_FUNC_SIG(NAME) static void REMATRIX_FUNC_NAME(NAME) \
55
+    (FMT_TYPE *outp[], FMT_TYPE *inp[], int nb_samples, AConvertContext *aconvert)
56
+
57
+#define FMT_TYPE uint8_t
58
+#define REMATRIX_FUNC_NAME(NAME) NAME ## _u8
59
+#include "af_aconvert_rematrix.c"
60
+
61
+#define FMT_TYPE int16_t
62
+#define REMATRIX_FUNC_NAME(NAME) NAME ## _s16
63
+#include "af_aconvert_rematrix.c"
64
+
65
+#define FMT_TYPE int32_t
66
+#define REMATRIX_FUNC_NAME(NAME) NAME ## _s32
67
+#include "af_aconvert_rematrix.c"
68
+
69
+#define FLOATING
70
+
71
+#define FMT_TYPE float
72
+#define REMATRIX_FUNC_NAME(NAME) NAME ## _flt
73
+#include "af_aconvert_rematrix.c"
74
+
75
+#define FMT_TYPE double
76
+#define REMATRIX_FUNC_NAME(NAME) NAME ## _dbl
77
+#include "af_aconvert_rematrix.c"
78
+
79
+#define FMT_TYPE uint8_t
80
+#define REMATRIX_FUNC_NAME(NAME) NAME
81
+REMATRIX_FUNC_SIG(stereo_remix_planar)
82
+{
83
+    int size = av_get_bytes_per_sample(aconvert->in_sample_fmt) * nb_samples;
84
+
85
+    memcpy(outp[0], inp[0], size);
86
+    memcpy(outp[1], inp[aconvert->in_nb_channels == 1 ? 0 : 1], size);
87
+}
88
+
89
+#define REGISTER_FUNC_PACKING(INCHLAYOUT, OUTCHLAYOUT, FUNC, PACKING)   \
90
+    {INCHLAYOUT, OUTCHLAYOUT, PACKING, AV_SAMPLE_FMT_U8,  FUNC##_u8},   \
91
+    {INCHLAYOUT, OUTCHLAYOUT, PACKING, AV_SAMPLE_FMT_S16, FUNC##_s16},  \
92
+    {INCHLAYOUT, OUTCHLAYOUT, PACKING, AV_SAMPLE_FMT_S32, FUNC##_s32},  \
93
+    {INCHLAYOUT, OUTCHLAYOUT, PACKING, AV_SAMPLE_FMT_FLT, FUNC##_flt},  \
94
+    {INCHLAYOUT, OUTCHLAYOUT, PACKING, AV_SAMPLE_FMT_DBL, FUNC##_dbl},
95
+
96
+#define REGISTER_FUNC(INCHLAYOUT, OUTCHLAYOUT, FUNC)                                \
97
+    REGISTER_FUNC_PACKING(INCHLAYOUT, OUTCHLAYOUT, FUNC##_packed, AVFILTER_PACKED)  \
98
+    REGISTER_FUNC_PACKING(INCHLAYOUT, OUTCHLAYOUT, FUNC##_planar, AVFILTER_PLANAR)
99
+
100
+static struct RematrixFunctionInfo {
101
+    int64_t in_chlayout, out_chlayout;
102
+    int planar, sfmt;
103
+    void (*func)();
104
+} rematrix_funcs[] = {
105
+    REGISTER_FUNC        (AV_CH_LAYOUT_STEREO,  AV_CH_LAYOUT_5POINT1, stereo_to_surround_5p1)
106
+    REGISTER_FUNC        (AV_CH_LAYOUT_5POINT1, AV_CH_LAYOUT_STEREO,  surround_5p1_to_stereo)
107
+    REGISTER_FUNC_PACKING(AV_CH_LAYOUT_STEREO,  AV_CH_LAYOUT_MONO,    stereo_to_mono_packed, AVFILTER_PACKED)
108
+    REGISTER_FUNC_PACKING(AV_CH_LAYOUT_MONO,    AV_CH_LAYOUT_STEREO,  mono_to_stereo_packed, AVFILTER_PACKED)
109
+    REGISTER_FUNC        (0,                    AV_CH_LAYOUT_MONO,    mono_downmix)
110
+    REGISTER_FUNC_PACKING(0,                    AV_CH_LAYOUT_STEREO,  stereo_downmix_packed, AVFILTER_PACKED)
111
+
112
+    // This function works for all sample formats
113
+    {0, AV_CH_LAYOUT_STEREO, AVFILTER_PLANAR, -1, stereo_remix_planar}
114
+};
115
+
116
+static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
117
+{
118
+    AConvertContext *aconvert = ctx->priv;
119
+    char *arg, *ptr = NULL;
120
+    int ret = 0;
121
+    char *args = av_strdup(args0);
122
+
123
+    aconvert->out_sample_fmt  = AV_SAMPLE_FMT_NONE;
124
+    aconvert->out_chlayout    = 0;
125
+    aconvert->out_packing_fmt = -1;
126
+
127
+    if ((arg = strtok_r(args, ":", &ptr)) && strcmp(arg, "auto")) {
128
+        if ((ret = ff_parse_sample_format(&aconvert->out_sample_fmt, arg, ctx)) < 0)
129
+            goto end;
130
+    }
131
+    if ((arg = strtok_r(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
132
+        if ((ret = ff_parse_channel_layout(&aconvert->out_chlayout, arg, ctx)) < 0)
133
+            goto end;
134
+    }
135
+    if ((arg = strtok_r(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
136
+        if ((ret = ff_parse_packing_format((int *)&aconvert->out_packing_fmt, arg, ctx)) < 0)
137
+            goto end;
138
+    }
139
+
140
+end:
141
+    av_freep(&args);
142
+    return ret;
143
+}
144
+
145
+static av_cold void uninit(AVFilterContext *ctx)
146
+{
147
+    AConvertContext *aconvert = ctx->priv;
148
+    avfilter_unref_buffer(aconvert->mix_samplesref);
149
+    avfilter_unref_buffer(aconvert->out_samplesref);
150
+    if (aconvert->audioconvert_ctx)
151
+        av_audio_convert_free(aconvert->audioconvert_ctx);
152
+}
153
+
154
+static int query_formats(AVFilterContext *ctx)
155
+{
156
+    AVFilterFormats *formats = NULL;
157
+    AConvertContext *aconvert = ctx->priv;
158
+    AVFilterLink *inlink  = ctx->inputs[0];
159
+    AVFilterLink *outlink = ctx->outputs[0];
160
+
161
+    avfilter_formats_ref(avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO),
162
+                         &inlink->out_formats);
163
+    if (aconvert->out_sample_fmt != AV_SAMPLE_FMT_NONE) {
164
+        formats = NULL;
165
+        avfilter_add_format(&formats, aconvert->out_sample_fmt);
166
+        avfilter_formats_ref(formats, &outlink->in_formats);
167
+    } else
168
+        avfilter_formats_ref(avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO),
169
+                             &outlink->in_formats);
170
+
171
+    avfilter_formats_ref(avfilter_make_all_channel_layouts(),
172
+                         &inlink->out_chlayouts);
173
+    if (aconvert->out_chlayout != 0) {
174
+        formats = NULL;
175
+        avfilter_add_format(&formats, aconvert->out_chlayout);
176
+        avfilter_formats_ref(formats, &outlink->in_chlayouts);
177
+    } else
178
+        avfilter_formats_ref(avfilter_make_all_channel_layouts(),
179
+                             &outlink->in_chlayouts);
180
+
181
+    avfilter_formats_ref(avfilter_make_all_packing_formats(),
182
+                         &inlink->out_packing);
183
+    if (aconvert->out_packing_fmt != -1) {
184
+        formats = NULL;
185
+        avfilter_add_format(&formats, aconvert->out_packing_fmt);
186
+        avfilter_formats_ref(formats, &outlink->in_packing);
187
+    } else
188
+        avfilter_formats_ref(avfilter_make_all_packing_formats(),
189
+                             &outlink->in_packing);
190
+
191
+    return 0;
192
+}
193
+
194
+static int config_output(AVFilterLink *outlink)
195
+{
196
+    AVFilterLink *inlink = outlink->src->inputs[0];
197
+    AConvertContext *aconvert = outlink->src->priv;
198
+    char buf1[64], buf2[64];
199
+
200
+    aconvert->in_sample_fmt  = inlink->format;
201
+    aconvert->in_packing_fmt = inlink->planar;
202
+    if (aconvert->out_packing_fmt == -1)
203
+        aconvert->out_packing_fmt = outlink->planar;
204
+    aconvert->in_chlayout    = inlink->channel_layout;
205
+    aconvert->in_nb_channels =
206
+        av_get_channel_layout_nb_channels(inlink->channel_layout);
207
+
208
+    /* if not specified in args, use the format and layout of the output */
209
+    if (aconvert->out_sample_fmt == AV_SAMPLE_FMT_NONE)
210
+        aconvert->out_sample_fmt = outlink->format;
211
+    if (aconvert->out_chlayout   == 0)
212
+        aconvert->out_chlayout   = outlink->channel_layout;
213
+    aconvert->out_nb_channels  =
214
+        av_get_channel_layout_nb_channels(outlink->channel_layout);
215
+
216
+    av_get_channel_layout_string(buf1, sizeof(buf1),
217
+                                 -1, inlink ->channel_layout);
218
+    av_get_channel_layout_string(buf2, sizeof(buf2),
219
+                                 -1, outlink->channel_layout);
220
+    av_log(outlink->src, AV_LOG_INFO,
221
+           "fmt:%s cl:%s planar:%i -> fmt:%s cl:%s planar:%i\n",
222
+           av_get_sample_fmt_name(inlink ->format), buf1, inlink ->planar,
223
+           av_get_sample_fmt_name(outlink->format), buf2, outlink->planar);
224
+
225
+    /* compute which channel layout conversion to use */
226
+    if (inlink->channel_layout != outlink->channel_layout) {
227
+        int i;
228
+        for (i = 0; i < sizeof(rematrix_funcs); i++) {
229
+            const struct RematrixFunctionInfo *f = &rematrix_funcs[i];
230
+            if ((f->in_chlayout  == 0 || f->in_chlayout  == inlink ->channel_layout) &&
231
+                (f->out_chlayout == 0 || f->out_chlayout == outlink->channel_layout) &&
232
+                (f->planar == -1 || f->planar == inlink->planar) &&
233
+                (f->sfmt   == -1 || f->sfmt   == inlink->format)
234
+               ) {
235
+                aconvert->convert_chlayout = f->func;
236
+                break;
237
+            }
238
+        }
239
+        if (!aconvert->convert_chlayout) {
240
+            av_log(outlink->src, AV_LOG_ERROR,
241
+                   "Unsupported channel layout conversion '%s -> %s' requested!\n",
242
+                   buf1, buf2);
243
+            return AVERROR(EINVAL);
244
+        }
245
+    }
246
+
247
+    return 0;
248
+}
249
+
250
+static int init_buffers(AVFilterLink *inlink, int nb_samples)
251
+{
252
+    AConvertContext *aconvert = inlink->dst->priv;
253
+    AVFilterLink * const outlink = inlink->dst->outputs[0];
254
+    int i, packed_stride = 0;
255
+    const unsigned
256
+        packing_conv = inlink->planar != outlink->planar &&
257
+                       aconvert->out_nb_channels != 1,
258
+        format_conv  = inlink->format != outlink->format;
259
+    int nb_channels  = aconvert->out_nb_channels;
260
+
261
+    uninit(inlink->dst);
262
+    aconvert->max_nb_samples = nb_samples;
263
+
264
+    if (aconvert->convert_chlayout) {
265
+        /* allocate buffer for storing intermediary mixing samplesref */
266
+        uint8_t *data[8];
267
+        int linesize[8];
268
+        int nb_channels = av_get_channel_layout_nb_channels(outlink->channel_layout);
269
+
270
+        if (av_samples_alloc(data, linesize, nb_channels, nb_samples,
271
+                             inlink->format, inlink->planar, 16) < 0)
272
+            goto fail_no_mem;
273
+        aconvert->mix_samplesref =
274
+            avfilter_get_audio_buffer_ref_from_arrays(data, linesize, AV_PERM_WRITE,
275
+                                                      nb_samples, inlink->format,
276
+                                                      outlink->channel_layout,
277
+                                                      inlink->planar);
278
+        if (!aconvert->mix_samplesref)
279
+            goto fail_no_mem;
280
+    }
281
+
282
+    // if there's a format/packing conversion we need an audio_convert context
283
+    if (format_conv || packing_conv) {
284
+        aconvert->out_samplesref =
285
+            avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
286
+        if (!aconvert->out_samplesref)
287
+            goto fail_no_mem;
288
+
289
+        aconvert->in_strides [0] = av_get_bytes_per_sample(inlink ->format);
290
+        aconvert->out_strides[0] = av_get_bytes_per_sample(outlink->format);
291
+
292
+        aconvert->out_conv = aconvert->out_samplesref->data;
293
+        if (aconvert->mix_samplesref)
294
+            aconvert->in_conv = aconvert->mix_samplesref->data;
295
+
296
+        if (packing_conv) {
297
+            // packed -> planar
298
+            if (outlink->planar == AVFILTER_PLANAR) {
299
+                if (aconvert->mix_samplesref)
300
+                    aconvert->packed_data[0] = aconvert->mix_samplesref->data[0];
301
+                aconvert->in_conv         = aconvert->packed_data;
302
+                packed_stride             = aconvert->in_strides[0];
303
+                aconvert->in_strides[0]  *= nb_channels;
304
+            // planar -> packed
305
+            } else {
306
+                aconvert->packed_data[0]  = aconvert->out_samplesref->data[0];
307
+                aconvert->out_conv        = aconvert->packed_data;
308
+                packed_stride             = aconvert->out_strides[0];
309
+                aconvert->out_strides[0] *= nb_channels;
310
+            }
311
+        } else if (outlink->planar == AVFILTER_PACKED) {
312
+            /* If there's no packing conversion, and the stream is packed
313
+             * then we treat the entire stream as one big channel
314
+             */
315
+            nb_channels = 1;
316
+        }
317
+
318
+        for (i = 1; i < nb_channels; i++) {
319
+            aconvert->packed_data[i] = aconvert->packed_data[i-1] + packed_stride;
320
+            aconvert->in_strides[i]  = aconvert->in_strides[0];
321
+            aconvert->out_strides[i] = aconvert->out_strides[0];
322
+        }
323
+
324
+        aconvert->audioconvert_ctx =
325
+                av_audio_convert_alloc(outlink->format, nb_channels,
326
+                                       inlink->format,  nb_channels, NULL, 0);
327
+        if (!aconvert->audioconvert_ctx)
328
+            goto fail_no_mem;
329
+    }
330
+
331
+    return 0;
332
+
333
+fail_no_mem:
334
+    av_log(inlink->dst, AV_LOG_ERROR, "Could not allocate memory.\n");
335
+    return AVERROR(ENOMEM);
336
+}
337
+
338
+static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
339
+{
340
+    AConvertContext *aconvert = inlink->dst->priv;
341
+    AVFilterBufferRef *curbuf = insamplesref;
342
+    AVFilterLink * const outlink = inlink->dst->outputs[0];
343
+    int chan_mult;
344
+
345
+    /* in/reinint the internal buffers if this is the first buffer
346
+     * provided or it is needed to use a bigger one */
347
+    if (!aconvert->max_nb_samples ||
348
+        (curbuf->audio->nb_samples > aconvert->max_nb_samples))
349
+        if (init_buffers(inlink, curbuf->audio->nb_samples) < 0) {
350
+            av_log(inlink->dst, AV_LOG_ERROR, "Could not initialize buffers.\n");
351
+            return;
352
+        }
353
+
354
+    /* if channel mixing is required */
355
+    if (aconvert->mix_samplesref) {
356
+        memcpy(aconvert->in_mix,  curbuf->data, sizeof(aconvert->in_mix));
357
+        memcpy(aconvert->out_mix, aconvert->mix_samplesref->data, sizeof(aconvert->out_mix));
358
+        aconvert->convert_chlayout(aconvert->out_mix,
359
+                                   aconvert->in_mix,
360
+                                   curbuf->audio->nb_samples,
361
+                                   aconvert);
362
+        curbuf = aconvert->mix_samplesref;
363
+    }
364
+
365
+    if (aconvert->audioconvert_ctx) {
366
+        if (!aconvert->mix_samplesref) {
367
+            if (aconvert->in_conv == aconvert->packed_data) {
368
+                int i, packed_stride = av_get_bytes_per_sample(inlink->format);
369
+                aconvert->packed_data[0] = curbuf->data[0];
370
+                for (i = 1; i < aconvert->out_nb_channels; i++)
371
+                    aconvert->packed_data[i] = aconvert->packed_data[i-1] + packed_stride;
372
+            } else {
373
+                aconvert->in_conv = curbuf->data;
374
+            }
375
+        }
376
+
377
+        chan_mult = inlink->planar == outlink->planar && inlink->planar == 0 ?
378
+            aconvert->out_nb_channels : 1;
379
+
380
+        av_audio_convert(aconvert->audioconvert_ctx,
381
+                         (void * const *) aconvert->out_conv,
382
+                         aconvert->out_strides,
383
+                         (const void * const *) aconvert->in_conv,
384
+                         aconvert->in_strides,
385
+                         curbuf->audio->nb_samples * chan_mult);
386
+
387
+        curbuf = aconvert->out_samplesref;
388
+    }
389
+
390
+    avfilter_copy_buffer_ref_props(curbuf, insamplesref);
391
+    curbuf->audio->channel_layout = outlink->channel_layout;
392
+    curbuf->audio->planar         = outlink->planar;
393
+
394
+    avfilter_filter_samples(inlink->dst->outputs[0],
395
+                            avfilter_ref_buffer(curbuf, ~0));
396
+    avfilter_unref_buffer(insamplesref);
397
+}
398
+
399
+AVFilter avfilter_af_aconvert = {
400
+    .name          = "aconvert",
401
+    .description   = NULL_IF_CONFIG_SMALL("Convert the input audio to sample_fmt:channel_layout:packed_fmt."),
402
+    .priv_size     = sizeof(AConvertContext),
403
+    .init          = init,
404
+    .uninit        = uninit,
405
+    .query_formats = query_formats,
406
+
407
+    .inputs    = (AVFilterPad[]) {{ .name            = "default",
408
+                                    .type            = AVMEDIA_TYPE_AUDIO,
409
+                                    .filter_samples  = filter_samples,
410
+                                    .min_perms       = AV_PERM_READ, },
411
+                                  { .name = NULL}},
412
+    .outputs   = (AVFilterPad[]) {{ .name            = "default",
413
+                                    .type            = AVMEDIA_TYPE_AUDIO,
414
+                                    .config_props    = config_output, },
415
+                                  { .name = NULL}},
416
+};
0 417
new file mode 100644
... ...
@@ -0,0 +1,172 @@
0
+/*
1
+ * Copyright (c) 2011 Mina Nagy Zaki
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * audio rematrixing functions, based on functions from libavcodec/resample.c
23
+ */
24
+
25
+#if defined(FLOATING)
26
+# define DIV2 /2
27
+#else
28
+# define DIV2 >>1
29
+#endif
30
+
31
+REMATRIX_FUNC_SIG(stereo_to_mono_packed)
32
+{
33
+    while (nb_samples >= 4) {
34
+        outp[0][0] = (inp[0][0] + inp[0][1]) DIV2;
35
+        outp[0][1] = (inp[0][2] + inp[0][3]) DIV2;
36
+        outp[0][2] = (inp[0][4] + inp[0][5]) DIV2;
37
+        outp[0][3] = (inp[0][6] + inp[0][7]) DIV2;
38
+        outp[0] += 4;
39
+        inp[0]  += 8;
40
+        nb_samples -= 4;
41
+    }
42
+    while (nb_samples--) {
43
+        outp[0][0] = (inp[0][0] + inp[0][1]) DIV2;
44
+        outp[0]++;
45
+        inp[0] += 2;
46
+    }
47
+}
48
+
49
+REMATRIX_FUNC_SIG(stereo_downmix_packed)
50
+{
51
+    while (nb_samples--) {
52
+        *outp[0]++ = inp[0][0];
53
+        *outp[0]++ = inp[0][1];
54
+        inp[0] += aconvert->in_nb_channels;
55
+    }
56
+}
57
+
58
+REMATRIX_FUNC_SIG(mono_to_stereo_packed)
59
+{
60
+    while (nb_samples >= 4) {
61
+        outp[0][0] = outp[0][1] = inp[0][0];
62
+        outp[0][2] = outp[0][3] = inp[0][1];
63
+        outp[0][4] = outp[0][5] = inp[0][2];
64
+        outp[0][6] = outp[0][7] = inp[0][3];
65
+        outp[0] += 8;
66
+        inp[0]  += 4;
67
+        nb_samples -= 4;
68
+    }
69
+    while (nb_samples--) {
70
+        outp[0][0] = outp[0][1] = inp[0][0];
71
+        outp[0] += 2;
72
+        inp[0]  += 1;
73
+    }
74
+}
75
+
76
+/**
77
+ * This is for when we have more than 2 input channels, need to downmix to mono
78
+ * and do not have a conversion formula available.  We just use first two input
79
+ * channels - left and right. This is a placeholder until more conversion
80
+ * functions are written.
81
+ */
82
+REMATRIX_FUNC_SIG(mono_downmix_packed)
83
+{
84
+    while (nb_samples--) {
85
+        outp[0][0] = (inp[0][0] + inp[0][1]) DIV2;
86
+        inp[0] += aconvert->in_nb_channels;
87
+        outp[0]++;
88
+    }
89
+}
90
+
91
+REMATRIX_FUNC_SIG(mono_downmix_planar)
92
+{
93
+    FMT_TYPE *out = outp[0];
94
+
95
+    while (nb_samples >= 4) {
96
+        out[0] = (inp[0][0] + inp[1][0]) DIV2;
97
+        out[1] = (inp[0][1] + inp[1][1]) DIV2;
98
+        out[2] = (inp[0][2] + inp[1][2]) DIV2;
99
+        out[3] = (inp[0][3] + inp[1][3]) DIV2;
100
+        out    += 4;
101
+        inp[0] += 4;
102
+        inp[1] += 4;
103
+        nb_samples -= 4;
104
+    }
105
+    while (nb_samples--) {
106
+        out[0] = (inp[0][0] + inp[1][0]) DIV2;
107
+        out++;
108
+        inp[0]++;
109
+        inp[1]++;
110
+    }
111
+}
112
+
113
+/* Stereo to 5.1 output */
114
+REMATRIX_FUNC_SIG(stereo_to_surround_5p1_packed)
115
+{
116
+    while (nb_samples--) {
117
+      outp[0][0] = inp[0][0];  /* left */
118
+      outp[0][1] = inp[0][1];  /* right */
119
+      outp[0][2] = (inp[0][0] + inp[0][1]) DIV2; /* center */
120
+      outp[0][3] = 0;          /* low freq */
121
+      outp[0][4] = 0;          /* FIXME: left surround: -3dB or -6dB or -9dB of stereo left  */
122
+      outp[0][5] = 0;          /* FIXME: right surroud: -3dB or -6dB or -9dB of stereo right */
123
+      inp[0]  += 2;
124
+      outp[0] += 6;
125
+    }
126
+}
127
+
128
+REMATRIX_FUNC_SIG(stereo_to_surround_5p1_planar)
129
+{
130
+    while (nb_samples--) {
131
+      *outp[0]++ = *inp[0];    /* left */
132
+      *outp[1]++ = *inp[1];    /* right */
133
+      *outp[2]++ = (*inp[0] + *inp[1]) DIV2; /* center */
134
+      *outp[3]++ = 0;          /* low freq */
135
+      *outp[4]++ = 0;          /* FIXME: left surround: -3dB or -6dB or -9dB of stereo left  */
136
+      *outp[5]++ = 0;          /* FIXME: right surroud: -3dB or -6dB or -9dB of stereo right */
137
+      inp[0]++; inp[1]++;
138
+    }
139
+}
140
+
141
+
142
+/*
143
+5.1 to stereo input: [fl, fr, c, lfe, rl, rr]
144
+- Left = front_left + rear_gain * rear_left + center_gain * center
145
+- Right = front_right + rear_gain * rear_right + center_gain * center
146
+Where rear_gain is usually around 0.5-1.0 and
147
+      center_gain is almost always 0.7 (-3 dB)
148
+*/
149
+REMATRIX_FUNC_SIG(surround_5p1_to_stereo_packed)
150
+{
151
+    while (nb_samples--) {
152
+        *outp[0]++ = inp[0][0] + (0.5 * inp[0][4]) + (0.7 * inp[0][2]); //FIXME CLIPPING!
153
+        *outp[0]++ = inp[0][1] + (0.5 * inp[0][5]) + (0.7 * inp[0][2]); //FIXME CLIPPING!
154
+
155
+        inp[0] += 6;
156
+    }
157
+}
158
+
159
+REMATRIX_FUNC_SIG(surround_5p1_to_stereo_planar)
160
+{
161
+    while (nb_samples--) {
162
+        *outp[0]++ = *inp[0] + (0.5 * *inp[4]) + (0.7 * *inp[2]); //FIXME CLIPPING!
163
+        *outp[1]++ = *inp[1] + (0.5 * *inp[5]) + (0.7 * *inp[2]); //FIXME CLIPPING!
164
+
165
+        inp[0]++; inp[1]++; inp[2]++; inp[3]++; inp[4]++; inp[5]++;
166
+    }
167
+}
168
+
169
+#undef DIV2
170
+#undef REMATRIX_FUNC_NAME
171
+#undef FMT_TYPE
... ...
@@ -34,6 +34,7 @@ void avfilter_register_all(void)
34 34
         return;
35 35
     initialized = 1;
36 36
 
37
+    REGISTER_FILTER (ACONVERT,    aconvert,    af);
37 38
     REGISTER_FILTER (AFORMAT,     aformat,     af);
38 39
     REGISTER_FILTER (ANULL,       anull,       af);
39 40
     REGISTER_FILTER (ARESAMPLE,   aresample,   af);
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/rational.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  2
32
-#define LIBAVFILTER_VERSION_MINOR 42
32
+#define LIBAVFILTER_VERSION_MINOR 43
33 33
 #define LIBAVFILTER_VERSION_MICRO  0
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \