Browse code

lavfi: add asrc_abuffer - audio buffer source

Originally based on code by Stefano Sabatini and S. N. Hemanth.

Signed-off-by: Stefano Sabatini <stefano.sabatini-lala@poste.it>

Mina Nagy Zaki authored on 2011/08/01 17:33:26
Showing 7 changed files
... ...
@@ -1501,6 +1501,7 @@ tcp_protocol_deps="network"
1501 1501
 udp_protocol_deps="network"
1502 1502
 
1503 1503
 # filters
1504
+abuffer="strtok_r"
1504 1505
 aformat_filter_deps="strtok_r"
1505 1506
 blackframe_filter_deps="gpl"
1506 1507
 boxblur_filter_deps="gpl"
... ...
@@ -194,6 +194,51 @@ Adler-32 checksum for each input frame plane, expressed in the form
194 194
 
195 195
 Below is a description of the currently available audio sources.
196 196
 
197
+@section abuffer
198
+
199
+Buffer audio frames, and make them available to the filter chain.
200
+
201
+This source is mainly intended for a programmatic use, in particular
202
+through the interface defined in @file{libavfilter/asrc_abuffer.h}.
203
+
204
+It accepts the following mandatory parameters:
205
+@var{sample_rate}:@var{sample_fmt}:@var{channel_layout}:@var{packing}
206
+
207
+@table @option
208
+
209
+@item sample_rate
210
+The sample rate of the incoming audio buffers.
211
+
212
+@item sample_fmt
213
+The sample format of the incoming audio buffers.
214
+Either a sample format name or its corresponging integer representation from
215
+the enum AVSampleFormat in @file{libavutil/samplefmt.h}
216
+
217
+@item channel_layout
218
+The channel layout of the incoming audio buffers.
219
+Either a channel layout name from channel_layout_map in
220
+@file{libavutil/audioconvert.c} or its corresponding integer representation
221
+from the AV_CH_LAYOUT_* macros in @file{libavutil/audioconvert.h}
222
+
223
+@item packing
224
+Either "packed" or "planar", or their integer representation: 0 or 1
225
+respectively.
226
+
227
+@end table
228
+
229
+For example:
230
+@example
231
+abuffer=44100:s16:stereo:planar
232
+@end example
233
+
234
+will instruct the source to accept planar 16bit signed stereo at 44100Hz.
235
+Since the sample format with name "s16" corresponds to the number
236
+1 and the "stereo" channel layout corresponds to the value 3, this is
237
+equivalent to:
238
+@example
239
+abuffer=44100:1:3:1
240
+@end example
241
+
197 242
 @section anullsrc
198 243
 
199 244
 Null audio source, never return audio frames. It is mainly useful as a
... ...
@@ -24,6 +24,7 @@ OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
24 24
 OBJS-$(CONFIG_ARESAMPLE_FILTER)              += af_aresample.o
25 25
 OBJS-$(CONFIG_ASHOWINFO_FILTER)              += af_ashowinfo.o
26 26
 
27
+OBJS-$(CONFIG_ABUFFER_FILTER)                += asrc_abuffer.o
27 28
 OBJS-$(CONFIG_ANULLSRC_FILTER)               += asrc_anullsrc.o
28 29
 
29 30
 OBJS-$(CONFIG_ABUFFERSINK_FILTER)            += asink_abuffer.o
... ...
@@ -39,6 +39,7 @@ void avfilter_register_all(void)
39 39
     REGISTER_FILTER (ARESAMPLE,   aresample,   af);
40 40
     REGISTER_FILTER (ASHOWINFO,   ashowinfo,   af);
41 41
 
42
+    REGISTER_FILTER (ABUFFER,     abuffer,     asrc);
42 43
     REGISTER_FILTER (ANULLSRC,    anullsrc,    asrc);
43 44
 
44 45
     REGISTER_FILTER (ABUFFERSINK, abuffersink, asink);
45 46
new file mode 100644
... ...
@@ -0,0 +1,366 @@
0
+/*
1
+ * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram
2
+ * Copyright (c) 2011 Mina Nagy Zaki
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * memory buffer source for audio
24
+ */
25
+
26
+#include "libavutil/audioconvert.h"
27
+#include "libavutil/fifo.h"
28
+#include "asrc_abuffer.h"
29
+#include "internal.h"
30
+
31
+typedef struct {
32
+    // Audio format of incoming buffers
33
+    int sample_rate;
34
+    unsigned int sample_format;
35
+    int64_t channel_layout;
36
+    int packing_format;
37
+
38
+    // FIFO buffer of audio buffer ref pointers
39
+    AVFifoBuffer *fifo;
40
+
41
+    // Normalization filters
42
+    AVFilterContext *aconvert;
43
+    AVFilterContext *aresample;
44
+} ABufferSourceContext;
45
+
46
+#define FIFO_SIZE 8
47
+
48
+static void buf_free(AVFilterBuffer *ptr)
49
+{
50
+    av_free(ptr);
51
+    return;
52
+}
53
+
54
+static void set_link_source(AVFilterContext *src, AVFilterLink *link)
55
+{
56
+    link->src       = src;
57
+    link->srcpad    = &(src->output_pads[0]);
58
+    src->outputs[0] = link;
59
+}
60
+
61
+static int reconfigure_filter(ABufferSourceContext *abuffer, AVFilterContext *filt_ctx)
62
+{
63
+    int ret;
64
+    AVFilterLink * const inlink  = filt_ctx->inputs[0];
65
+    AVFilterLink * const outlink = filt_ctx->outputs[0];
66
+
67
+    inlink->format         = abuffer->sample_format;
68
+    inlink->channel_layout = abuffer->channel_layout;
69
+    inlink->planar         = abuffer->packing_format;
70
+    inlink->sample_rate    = abuffer->sample_rate;
71
+
72
+    filt_ctx->filter->uninit(filt_ctx);
73
+    memset(filt_ctx->priv, 0, filt_ctx->filter->priv_size);
74
+    if ((ret = filt_ctx->filter->init(filt_ctx, NULL , NULL)) < 0)
75
+        return ret;
76
+    if ((ret = inlink->srcpad->config_props(inlink)) < 0)
77
+        return ret;
78
+    return outlink->srcpad->config_props(outlink);
79
+}
80
+
81
+static int insert_filter(ABufferSourceContext *abuffer,
82
+                         AVFilterLink *link, AVFilterContext **filt_ctx,
83
+                         const char *filt_name)
84
+{
85
+    int ret;
86
+
87
+    if ((ret = avfilter_open(filt_ctx, avfilter_get_by_name(filt_name), NULL)) < 0)
88
+        return ret;
89
+
90
+    link->src->outputs[0] = NULL;
91
+    if ((ret = avfilter_link(link->src, 0, *filt_ctx, 0)) < 0) {
92
+        link->src->outputs[0] = link;
93
+        return ret;
94
+    }
95
+
96
+    set_link_source(*filt_ctx, link);
97
+
98
+    if ((ret = reconfigure_filter(abuffer, *filt_ctx)) < 0) {
99
+        avfilter_free(*filt_ctx);
100
+        return ret;
101
+    }
102
+
103
+    return 0;
104
+}
105
+
106
+static void remove_filter(AVFilterContext **filt_ctx)
107
+{
108
+    AVFilterLink *outlink = (*filt_ctx)->outputs[0];
109
+    AVFilterContext *src  = (*filt_ctx)->inputs[0]->src;
110
+
111
+    (*filt_ctx)->outputs[0] = NULL;
112
+    avfilter_free(*filt_ctx);
113
+    *filt_ctx = NULL;
114
+
115
+    set_link_source(src, outlink);
116
+}
117
+
118
+static inline void log_input_change(void *ctx, AVFilterLink *link, AVFilterBufferRef *ref)
119
+{
120
+    char old_layout_str[16], new_layout_str[16];
121
+    av_get_channel_layout_string(old_layout_str, sizeof(old_layout_str),
122
+                                 -1, link->channel_layout);
123
+    av_get_channel_layout_string(new_layout_str, sizeof(new_layout_str),
124
+                                 -1, ref->audio->channel_layout);
125
+    av_log(ctx, AV_LOG_INFO,
126
+           "Audio input format changed: "
127
+           "%s:%s:%"PRId64" -> %s:%s:%u, normalizing\n",
128
+           av_get_sample_fmt_name(link->format),
129
+           old_layout_str, link->sample_rate,
130
+           av_get_sample_fmt_name(ref->format),
131
+           new_layout_str, ref->audio->sample_rate);
132
+}
133
+
134
+int av_asrc_buffer_add_audio_buffer_ref(AVFilterContext *ctx,
135
+                                        AVFilterBufferRef *samplesref,
136
+                                        int av_unused flags)
137
+{
138
+    ABufferSourceContext *abuffer = ctx->priv;
139
+    AVFilterLink *link;
140
+    int ret, logged = 0;
141
+
142
+    if (av_fifo_space(abuffer->fifo) < sizeof(samplesref)) {
143
+        av_log(ctx, AV_LOG_ERROR,
144
+               "Buffering limit reached. Please consume some available frames "
145
+               "before adding new ones.\n");
146
+        return AVERROR(EINVAL);
147
+    }
148
+
149
+    // Normalize input
150
+
151
+    link = ctx->outputs[0];
152
+    if (samplesref->audio->sample_rate != link->sample_rate) {
153
+
154
+        log_input_change(ctx, link, samplesref);
155
+        logged = 1;
156
+
157
+        abuffer->sample_rate = samplesref->audio->sample_rate;
158
+
159
+        if (!abuffer->aresample) {
160
+            ret = insert_filter(abuffer, link, &abuffer->aresample, "aresample");
161
+            if (ret < 0) return ret;
162
+        } else {
163
+            link = abuffer->aresample->outputs[0];
164
+            if (samplesref->audio->sample_rate == link->sample_rate)
165
+                remove_filter(&abuffer->aresample);
166
+            else
167
+                if ((ret = reconfigure_filter(abuffer, abuffer->aresample)) < 0)
168
+                    return ret;
169
+        }
170
+    }
171
+
172
+    link = ctx->outputs[0];
173
+    if (samplesref->format                != link->format         ||
174
+        samplesref->audio->channel_layout != link->channel_layout ||
175
+        samplesref->audio->planar         != link->planar) {
176
+
177
+        if (!logged) log_input_change(ctx, link, samplesref);
178
+
179
+        abuffer->sample_format  = samplesref->format;
180
+        abuffer->channel_layout = samplesref->audio->channel_layout;
181
+        abuffer->packing_format = samplesref->audio->planar;
182
+
183
+        if (!abuffer->aconvert) {
184
+            ret = insert_filter(abuffer, link, &abuffer->aconvert, "aconvert");
185
+            if (ret < 0) return ret;
186
+        } else {
187
+            link = abuffer->aconvert->outputs[0];
188
+            if (samplesref->format                == link->format         &&
189
+                samplesref->audio->channel_layout == link->channel_layout &&
190
+                samplesref->audio->planar         == link->planar
191
+               )
192
+                remove_filter(&abuffer->aconvert);
193
+            else
194
+                if ((ret = reconfigure_filter(abuffer, abuffer->aconvert)) < 0)
195
+                    return ret;
196
+        }
197
+    }
198
+
199
+    if (sizeof(samplesref) != av_fifo_generic_write(abuffer->fifo, &samplesref,
200
+                                                    sizeof(samplesref), NULL)) {
201
+        av_log(ctx, AV_LOG_ERROR, "Error while writing to FIFO\n");
202
+        return AVERROR(EINVAL);
203
+    }
204
+
205
+    return 0;
206
+}
207
+
208
+int av_asrc_buffer_add_samples(AVFilterContext *ctx,
209
+                               uint8_t *data[8], int linesize[8],
210
+                               int nb_samples, int sample_rate,
211
+                               int sample_fmt, int64_t channel_layout, int planar,
212
+                               int64_t pts, int av_unused flags)
213
+{
214
+    AVFilterBufferRef *samplesref;
215
+
216
+    samplesref = avfilter_get_audio_buffer_ref_from_arrays(
217
+                     data, linesize, AV_PERM_WRITE,
218
+                     nb_samples,
219
+                     sample_fmt, channel_layout, planar);
220
+    if (!samplesref)
221
+        return AVERROR(ENOMEM);
222
+
223
+    samplesref->buf->free  = buf_free;
224
+    samplesref->pts = pts;
225
+    samplesref->audio->sample_rate = sample_rate;
226
+
227
+    return av_asrc_buffer_add_audio_buffer_ref(ctx, samplesref, 0);
228
+}
229
+
230
+int av_asrc_buffer_add_buffer(AVFilterContext *ctx,
231
+                              uint8_t *buf, int buf_size, int sample_rate,
232
+                              int sample_fmt, int64_t channel_layout, int planar,
233
+                              int64_t pts, int av_unused flags)
234
+{
235
+    uint8_t *data[8];
236
+    int linesize[8];
237
+    int nb_channels = av_get_channel_layout_nb_channels(channel_layout),
238
+        nb_samples  = buf_size / nb_channels / av_get_bytes_per_sample(sample_fmt);
239
+
240
+    av_samples_fill_arrays(data, linesize,
241
+                           buf, nb_channels, nb_samples,
242
+                           sample_fmt, planar, 16);
243
+
244
+    return av_asrc_buffer_add_samples(ctx,
245
+                                      data, linesize, nb_samples,
246
+                                      sample_rate,
247
+                                      sample_fmt, channel_layout, planar,
248
+                                      pts, flags);
249
+}
250
+
251
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
252
+{
253
+    ABufferSourceContext *abuffer = ctx->priv;
254
+    char *arg = NULL, *ptr, chlayout_str[16];
255
+    int ret;
256
+
257
+    arg = strtok_r(args, ":", &ptr);
258
+
259
+#define ADD_FORMAT(fmt_name)                                            \
260
+    if (!arg)                                                           \
261
+        goto arg_fail;                                                  \
262
+    if ((ret = ff_parse_##fmt_name(&abuffer->fmt_name, arg, ctx)) < 0)  \
263
+        return ret;                                                     \
264
+    if (*args)                                                          \
265
+        arg = strtok_r(NULL, ":", &ptr)
266
+
267
+    ADD_FORMAT(sample_rate);
268
+    ADD_FORMAT(sample_format);
269
+    ADD_FORMAT(channel_layout);
270
+    ADD_FORMAT(packing_format);
271
+
272
+    abuffer->fifo = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterBufferRef*));
273
+    if (!abuffer->fifo) {
274
+        av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo, filter init failed.\n");
275
+        return AVERROR(ENOMEM);
276
+    }
277
+
278
+    av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str),
279
+                                 -1, abuffer->channel_layout);
280
+    av_log(ctx, AV_LOG_INFO, "format:%s layout:%s rate:%d\n",
281
+           av_get_sample_fmt_name(abuffer->sample_format), chlayout_str,
282
+           abuffer->sample_rate);
283
+
284
+    return 0;
285
+
286
+arg_fail:
287
+    av_log(ctx, AV_LOG_ERROR, "Invalid arguments, must be of the form "
288
+                              "sample_rate:sample_fmt:channel_layout:packing\n");
289
+    return AVERROR(EINVAL);
290
+}
291
+
292
+static av_cold void uninit(AVFilterContext *ctx)
293
+{
294
+    ABufferSourceContext *abuffer = ctx->priv;
295
+    av_fifo_free(abuffer->fifo);
296
+}
297
+
298
+static int query_formats(AVFilterContext *ctx)
299
+{
300
+    ABufferSourceContext *abuffer = ctx->priv;
301
+    AVFilterFormats *formats;
302
+
303
+    formats = NULL;
304
+    avfilter_add_format(&formats, abuffer->sample_format);
305
+    avfilter_set_common_sample_formats(ctx, formats);
306
+
307
+    formats = NULL;
308
+    avfilter_add_format(&formats, abuffer->channel_layout);
309
+    avfilter_set_common_channel_layouts(ctx, formats);
310
+
311
+    formats = NULL;
312
+    avfilter_add_format(&formats, abuffer->packing_format);
313
+    avfilter_set_common_packing_formats(ctx, formats);
314
+
315
+    return 0;
316
+}
317
+
318
+static int config_output(AVFilterLink *outlink)
319
+{
320
+    ABufferSourceContext *abuffer = outlink->src->priv;
321
+    outlink->sample_rate = abuffer->sample_rate;
322
+    return 0;
323
+}
324
+
325
+static int request_frame(AVFilterLink *outlink)
326
+{
327
+    ABufferSourceContext *abuffer = outlink->src->priv;
328
+    AVFilterBufferRef *samplesref;
329
+
330
+    if (!av_fifo_size(abuffer->fifo)) {
331
+        av_log(outlink->src, AV_LOG_ERROR,
332
+               "request_frame() called with no available frames!\n");
333
+        return AVERROR(EINVAL);
334
+    }
335
+
336
+    av_fifo_generic_read(abuffer->fifo, &samplesref, sizeof(samplesref), NULL);
337
+    avfilter_filter_samples(outlink, avfilter_ref_buffer(samplesref, ~0));
338
+    avfilter_unref_buffer(samplesref);
339
+
340
+    return 0;
341
+}
342
+
343
+static int poll_frame(AVFilterLink *outlink)
344
+{
345
+    ABufferSourceContext *abuffer = outlink->src->priv;
346
+    return av_fifo_size(abuffer->fifo)/sizeof(AVFilterBufferRef*);
347
+}
348
+
349
+AVFilter avfilter_asrc_abuffer = {
350
+    .name        = "abuffer",
351
+    .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
352
+    .priv_size   = sizeof(ABufferSourceContext),
353
+    .query_formats = query_formats,
354
+
355
+    .init        = init,
356
+    .uninit      = uninit,
357
+
358
+    .inputs      = (AVFilterPad[]) {{ .name = NULL }},
359
+    .outputs     = (AVFilterPad[]) {{ .name            = "default",
360
+                                      .type            = AVMEDIA_TYPE_AUDIO,
361
+                                      .request_frame   = request_frame,
362
+                                      .poll_frame      = poll_frame,
363
+                                      .config_props    = config_output, },
364
+                                    { .name = NULL}},
365
+};
0 366
new file mode 100644
... ...
@@ -0,0 +1,80 @@
0
+/*
1
+ * This file is part of FFmpeg.
2
+ *
3
+ * FFmpeg is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU Lesser General Public
5
+ * License as published by the Free Software Foundation; either
6
+ * version 2.1 of the License, or (at your option) any later version.
7
+ *
8
+ * FFmpeg is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
+ * Lesser General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Lesser General Public
14
+ * License along with FFmpeg; if not, write to the Free Software
15
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+ */
17
+
18
+#ifndef AVFILTER_ASRC_ABUFFER_H
19
+#define AVFILTER_ASRC_ABUFFER_H
20
+
21
+#include "avfilter.h"
22
+
23
+/**
24
+ * @file
25
+ * memory buffer source for audio
26
+ */
27
+
28
+/**
29
+ * Queue an audio buffer to the audio buffer source.
30
+ *
31
+ * @param abuffersrc audio source buffer context
32
+ * @param data pointers to the samples planes
33
+ * @param linesize linesizes of each audio buffer plane
34
+ * @param nb_samples number of samples per channel
35
+ * @param sample_fmt sample format of the audio data
36
+ * @param ch_layout channel layout of the audio data
37
+ * @param planar flag to indicate if audio data is planar or packed
38
+ * @param pts presentation timestamp of the audio buffer
39
+ * @param flags unused
40
+ */
41
+int av_asrc_buffer_add_samples(AVFilterContext *abuffersrc,
42
+                               uint8_t *data[8], int linesize[8],
43
+                               int nb_samples, int sample_rate,
44
+                               int sample_fmt, int64_t ch_layout, int planar,
45
+                               int64_t pts, int av_unused flags);
46
+
47
+/**
48
+ * Queue an audio buffer to the audio buffer source.
49
+ *
50
+ * This is similar to av_asrc_buffer_add_samples(), but the samples
51
+ * are stored in a buffer with known size.
52
+ *
53
+ * @param abuffersrc audio source buffer context
54
+ * @param buf pointer to the samples data, packed is assumed
55
+ * @param size the size in bytes of the buffer, it must contain an
56
+ * integer number of samples
57
+ * @param sample_fmt sample format of the audio data
58
+ * @param ch_layout channel layout of the audio data
59
+ * @param pts presentation timestamp of the audio buffer
60
+ * @param flags unused
61
+ */
62
+int av_asrc_buffer_add_buffer(AVFilterContext *abuffersrc,
63
+                              uint8_t *buf, int buf_size,
64
+                              int sample_rate,
65
+                              int sample_fmt, int64_t ch_layout, int planar,
66
+                              int64_t pts, int av_unused flags);
67
+
68
+/**
69
+ * Queue an audio buffer to the audio buffer source.
70
+ *
71
+ * @param abuffersrc audio source buffer context
72
+ * @param samplesref buffer ref to queue
73
+ * @param flags unused
74
+ */
75
+int av_asrc_buffer_add_audio_buffer_ref(AVFilterContext *abuffersrc,
76
+                                        AVFilterBufferRef *samplesref,
77
+                                        int av_unused flags);
78
+
79
+#endif /* AVFILTER_ASRC_ABUFFER_H */
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/rational.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  2
32
-#define LIBAVFILTER_VERSION_MINOR 33
32
+#define LIBAVFILTER_VERSION_MINOR 34
33 33
 #define LIBAVFILTER_VERSION_MICRO  0
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \