Added an asrc buffer. Uses AVFifoBuffer like in Baptiste's vsrc patch.
My ffmpeg.c changes aren't yet complete, will test with ffmpeg.c this
once that is done.

---
 libavfilter/Makefile      |    2 +
 libavfilter/allfilters.c  |    2 +
 libavfilter/asrc_buffer.c |  128 +++++++++++++++++++++++++++++++++++++++++++++
 libavfilter/asrc_buffer.h |   24 ++++++++
 4 files changed, 156 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/asrc_buffer.c
 create mode 100644 libavfilter/asrc_buffer.h



diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 7b26435..90da79f 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -41,6 +41,8 @@ OBJS-$(CONFIG_TRANSPOSE_FILTER)              += vf_transpose.o
 OBJS-$(CONFIG_UNSHARP_FILTER)                += vf_unsharp.o
 OBJS-$(CONFIG_VFLIP_FILTER)                  += vf_vflip.o
 
+OBJS-$(CONFIG_ABUFFER_FILTER)                += asrc_buffer.o
+
 OBJS-$(CONFIG_BUFFER_FILTER)                 += vsrc_buffer.o
 OBJS-$(CONFIG_MOVIE_FILTER)                  += vsrc_movie.o
 OBJS-$(CONFIG_NULLSRC_FILTER)                += vsrc_nullsrc.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 3f3ebd0..6a828fa 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -60,6 +60,8 @@ void avfilter_register_all(void)
     REGISTER_FILTER (UNSHARP,     unsharp,     vf);
     REGISTER_FILTER (VFLIP,       vflip,       vf);
 
+    REGISTER_FILTER (ABUFFER,     abuffer,     asrc);
+
     REGISTER_FILTER (BUFFER,      buffer,      vsrc);
     REGISTER_FILTER (MOVIE,       movie,       vsrc);
     REGISTER_FILTER (NULLSRC,     nullsrc,     vsrc);
diff --git a/libavfilter/asrc_buffer.c b/libavfilter/asrc_buffer.c
new file mode 100644
index 0000000..be77ce3
--- /dev/null
+++ b/libavfilter/asrc_buffer.c
@@ -0,0 +1,128 @@
+/*
+ * Memory buffer source filter
+ * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/fifo.h"
+#include "avfilter.h"
+#include "asrc_buffer.h"
+
+#define FIFO_SIZE 8
+
+typedef struct {
+    int           init_sample_fmt; ///< initial sample format indicated by client
+    AVFifoBuffer *fifo;            ///< fifo buffer of audio frame pointers
+} ABufferSourceContext;
+
+
+int av_asrc_buffer_add_frame(AVFilterContext *filter, uint8_t *frame, int size,
+                             int64_t ch_layout, int sample_fmt, int planar, int64_t pts)
+{
+    AVFilterLink *link = filter->outputs[0];
+    ABufferSourceContext *ctx = filter->priv;
+    AVFilterSamplesRef *samplesref;
+
+    if (av_fifo_space(ctx->fifo) < sizeof(samplesref)) {
+        av_log(filter, AV_LOG_ERROR,
+               "Buffering limit reached. Please consume some available frames before adding new ones.\n");
+        return AVERROR(ENOMEM);
+    }
+
+    samplesref = avfilter_get_samples_ref(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
+                                       AV_PERM_REUSE2, size, ch_layout, sample_fmt, planar);
+
+    memcpy(samplesref->data[0], frame, samplesref->size);
+    samplesref->pts = pts;
+
+    av_fifo_generic_write(ctx->fifo, &samplesref, sizeof(samplesref), NULL);
+
+    return 0;
+}
+
+static av_cold int init(AVFilterContext *filter, const char *args, void *opaque)
+{
+    ABufferSourceContext *ctx = filter->priv;
+    if (args && sscanf(args, "%d", &ctx->init_sample_fmt) != 1)
+    {
+        av_log(filter, AV_LOG_ERROR, "init() expected 1 parameter:'%s'\n", args);
+        return AVERROR(EINVAL);
+    }
+    ctx->fifo = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterSamplesRef*));
+    if (!ctx->fifo) {
+        av_log(filter, AV_LOG_ERROR, "Failed to allocate fifo, filter init failed.\n");
+        return AVERROR(ENOMEM);
+    }
+    return 0;
+}
+
+static av_cold void uninit(AVFilterContext *filter)
+{
+    ABufferSourceContext *ctx = filter->priv;
+    av_fifo_free(ctx->fifo);
+}
+
+static int query_formats(AVFilterContext *filter)
+{
+    ABufferSourceContext *ctx = filter->priv;
+    enum SampleFormat sample_fmts[] = { ctx->init_sample_fmt, SAMPLE_FMT_NONE };
+
+    avfilter_set_common_formats(filter, avfilter_make_format_list(sample_fmts));
+    return 0;
+}
+
+static int request_frame(AVFilterLink *link)
+{
+    ABufferSourceContext *ctx = link->src->priv;
+    AVFilterSamplesRef *samplesref;
+
+    if (!av_fifo_size(ctx->fifo)) {
+        av_log(link->src, AV_LOG_ERROR,
+               "request_frame() called with no available frames!\n");
+    }
+
+    av_fifo_generic_read(ctx->fifo, &samplesref, sizeof(samplesref), NULL);
+    avfilter_filter_samples(link, avfilter_ref_samples(samplesref, ~0));
+    avfilter_unref_samples(samplesref);
+
+    return 0;
+}
+
+static int poll_frame(AVFilterLink *link)
+{
+    ABufferSourceContext *ctx = link->src->priv;
+    return av_fifo_size(ctx->fifo)/sizeof(AVFilterSamplesRef*);
+}
+
+AVFilter avfilter_asrc_buffer =
+{
+    .name      = "buffer",
+    .priv_size = sizeof(ABufferSourceContext),
+    .query_formats = query_formats,
+
+    .init      = init,
+    .uninit    = uninit,
+
+    .inputs    = (AVFilterPad[]) {{ .name = NULL }},
+    .outputs   = (AVFilterPad[]) {{ .name            = "default",
+                                    .type            = AVMEDIA_TYPE_AUDIO,
+                                    .request_frame   = request_frame,
+                                    .poll_frame      = poll_frame, },
+                                  { .name = NULL}},
+};
+
diff --git a/libavfilter/asrc_buffer.h b/libavfilter/asrc_buffer.h
new file mode 100644
index 0000000..6df002b
--- /dev/null
+++ b/libavfilter/asrc_buffer.h
@@ -0,0 +1,24 @@
+/*
+ * Memory buffer source filter for audio
+ * Copyright (c) 2010 by S.N. Hemanth Meenakshisundaram
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+int av_asrc_buffer_add_frame(AVFilterContext *filter, uint8_t *frame, int size,
+                             int64_t ch_layout, int sample_fmt, int planar, int64_t pts);
+

_______________________________________________
FFmpeg-soc mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-soc

Reply via email to