[FFmpeg-devel] [PATCH] Add 'asetframesize' audiofilter

Andrey Utkin andrey.krieger.utkin at gmail.com
Thu Feb 23 16:49:10 CET 2012


Filter that changes number of samples on single output operation
---
 libavfilter/Makefile           |    1 +
 libavfilter/af_asetframesize.c |  191 ++++++++++++++++++++++++++++++++++++++++
 libavfilter/allfilters.c       |    1 +
 3 files changed, 193 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/af_asetframesize.c

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 9f5fdcb..db7397b 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -38,6 +38,7 @@ OBJS-$(CONFIG_EARWAX_FILTER)                 += af_earwax.o
 OBJS-$(CONFIG_PAN_FILTER)                    += af_pan.o
 OBJS-$(CONFIG_SILENCEDETECT_FILTER)          += af_silencedetect.o
 OBJS-$(CONFIG_VOLUME_FILTER)                 += af_volume.o
+OBJS-$(CONFIG_ASETFRAMESIZE_FILTER)          += af_asetframesize.o
 
 OBJS-$(CONFIG_ABUFFER_FILTER)                += asrc_abuffer.o
 OBJS-$(CONFIG_AEVALSRC_FILTER)               += asrc_aevalsrc.o
diff --git a/libavfilter/af_asetframesize.c b/libavfilter/af_asetframesize.c
new file mode 100644
index 0000000..dd391fc
--- /dev/null
+++ b/libavfilter/af_asetframesize.c
@@ -0,0 +1,191 @@
+/*
+ * Copyright (c) 2012 Andrey Utkin
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * Filter that changes number of samples on single output operation
+ */
+
+#include "avfilter.h"
+#include "libavutil/fifo.h"
+
+typedef struct {
+    unsigned int framesize; // how many samples to output
+    AVFifoBuffer *fifo; // samples are queued here
+    int64_t next_out_pts;
+} ASFSContext;
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    ASFSContext *asfs = ctx->priv;
+    if (!args) {
+        av_log(ctx, AV_LOG_ERROR, "parameter is required\n");
+        return AVERROR(EINVAL);
+    }
+    asfs->framesize = atoi(args);
+    if (!asfs->framesize) {
+        av_log(ctx, AV_LOG_ERROR, "invalind framesize %d (from '%s')\n", asfs->framesize, args);
+        return AVERROR(EINVAL);
+    }
+
+    asfs->fifo = av_fifo_alloc(2 * AVCODEC_MAX_AUDIO_FRAME_SIZE);
+    if (!asfs->fifo)
+        return AVERROR(ENOMEM);
+
+    asfs->next_out_pts = AV_NOPTS_VALUE;
+    return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    ASFSContext *asfs = ctx->priv;
+    av_fifo_free(asfs->fifo);
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    /*
+     * State that we support:
+     * - every sample format
+     * - every channels layout
+     * - only PACKED packing format
+     */
+    AVFilterFormats *formats = NULL;
+    int packing_fmts[] = { AVFILTER_PACKED, -1 };
+
+    formats = avfilter_make_all_channel_layouts();
+    if (!formats)
+        return AVERROR(ENOMEM);
+    avfilter_set_common_channel_layouts(ctx, formats);
+
+    formats = avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO);
+    if (!formats)
+        return AVERROR(ENOMEM);
+    avfilter_set_common_sample_formats(ctx, formats);
+
+    formats = avfilter_make_format_list(packing_fmts);
+    if (!formats)
+        return AVERROR(ENOMEM);
+    avfilter_set_common_packing_formats(ctx, formats);
+
+    return 0;
+}
+
+static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
+{
+    int r;
+    ASFSContext *asfs = inlink->dst->priv;
+    int nb_samples = insamples->audio->nb_samples;
+    int nb_channels =
+        av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
+    int bytes_per_sample_1ch = av_get_bytes_per_sample(insamples->format);
+    int nb_bytes_in  = nb_channels * bytes_per_sample_1ch * nb_samples;
+
+    if (av_fifo_space(asfs->fifo) < nb_bytes_in) {
+        av_log(inlink->dst, AV_LOG_DEBUG, "no space for %d bytes, stretching fifo\n", nb_bytes_in);
+        r = av_fifo_realloc2(asfs->fifo, av_fifo_size(asfs->fifo) + nb_bytes_in);
+        if (r < 0) {
+            av_log(inlink->dst, AV_LOG_ERROR,
+                    "stretching fifo fail, discarded %d samples\n", nb_samples);
+            return;
+        }
+    }
+    av_fifo_generic_write(asfs->fifo, insamples->data[0], nb_bytes_in, NULL);
+
+    if (asfs->next_out_pts == AV_NOPTS_VALUE)
+        asfs->next_out_pts = insamples->pts;
+    avfilter_unref_buffer(insamples);
+}
+
+static int poll_frame(AVFilterLink *outlink)
+{
+    ASFSContext *asfs = outlink->src->priv;
+    int r;
+    int nb_channels =
+        av_get_channel_layout_nb_channels(outlink->channel_layout);
+    int bytes_per_sample_1ch = av_get_bytes_per_sample(outlink->format);
+    int bytes_per_sample = nb_channels * bytes_per_sample_1ch;
+    int bytes_per_outframe = asfs->framesize * bytes_per_sample;
+    if (av_fifo_size(asfs->fifo) / bytes_per_outframe >= 1)
+        return 1;
+    if (!avfilter_poll_frame(outlink->src->inputs[0]))
+        return 0;
+    r = avfilter_request_frame(outlink->src->inputs[0]);
+    if (r < 0) {
+        av_log(outlink->src, AV_LOG_ERROR, "requesting frame from source fail, error %d\n", r);
+        return r;
+    }
+    if (av_fifo_size(asfs->fifo) / bytes_per_outframe >= 1)
+        return 1;
+    else
+        return 0;
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    ASFSContext *asfs = outlink->src->priv;
+    int nb_channels =
+        av_get_channel_layout_nb_channels(outlink->channel_layout);
+    int bytes_per_sample_1ch = av_get_bytes_per_sample(outlink->format);
+    int bytes_per_sample = nb_channels * bytes_per_sample_1ch;
+    int bytes_per_outframe = asfs->framesize * bytes_per_sample;
+    AVFilterBufferRef *outsamples;
+
+    if (av_fifo_size(asfs->fifo) < bytes_per_outframe) {
+        av_log(outlink->src, AV_LOG_ERROR, "requested frame, but having none\n");
+        return AVERROR(EINVAL);
+    }
+
+    outsamples = avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, asfs->framesize);
+    assert(outsamples);
+    av_fifo_generic_read(asfs->fifo, outsamples->data[0], bytes_per_outframe, NULL);
+
+    outsamples->audio->channel_layout = outlink->channel_layout;
+    outsamples->audio->nb_samples = asfs->framesize;
+    outsamples->audio->sample_rate = outlink->sample_rate;
+    outsamples->pts = asfs->next_out_pts;
+    if (asfs->next_out_pts != AV_NOPTS_VALUE)
+        asfs->next_out_pts += asfs->framesize;
+
+    avfilter_filter_samples(outlink, outsamples);
+    return 0;
+}
+
+AVFilter avfilter_af_asetframesize = {
+    .name           = "asetframesize",
+    .description    = NULL_IF_CONFIG_SMALL(
+            "Set number of samples to be output from filtergraph at once"),
+    .query_formats  = query_formats,
+    .priv_size      = sizeof(ASFSContext),
+    .init           = init,
+    .uninit         = uninit,
+
+    .inputs  = (const AVFilterPad[])  {{ .name     = "default",
+                                   .type           = AVMEDIA_TYPE_AUDIO,
+                                   .filter_samples = filter_samples,
+                                   .min_perms      = AV_PERM_READ|AV_PERM_WRITE},
+                                 { .name = NULL}},
+
+    .outputs = (const AVFilterPad[])  {{ .name     = "default",
+                                   .type           = AVMEDIA_TYPE_AUDIO,
+                                   .request_frame  = request_frame,
+                                   .poll_frame     = poll_frame, },
+                                 { .name = NULL}},
+};
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 487738a..339ef8d 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -46,6 +46,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (PAN,         pan,         af);
     REGISTER_FILTER (SILENCEDETECT, silencedetect, af);
     REGISTER_FILTER (VOLUME,      volume,      af);
+    REGISTER_FILTER (ASETFRAMESIZE, asetframesize, af);
 
     REGISTER_FILTER (ABUFFER,     abuffer,     asrc);
     REGISTER_FILTER (AEVALSRC,    aevalsrc,    asrc);
-- 
1.7.7



More information about the ffmpeg-devel mailing list