[FFmpeg-devel] [PATCH] Add 'asetframesize' audiofilter
Andrey Utkin
andrey.krieger.utkin at gmail.com
Tue Mar 13 16:32:39 CET 2012
This patch is resubmit of the one already reviewed by S. Sabatini
http://ffmpeg.org/pipermail/ffmpeg-devel/2012-February/121381.html
I've fixed what i could.
I found the following issues pointless or disappointing:
1. Is there any reason for which you can't set a default value? (for framesize)
2. AVCODEC_MAX_AUDIO_FRAME_SIZE non-availability
3. Possible int overflow (at av_fifo_realloc2)
I request your attention and help to tricky moment of the algorithm: this
filter does not call avfilter_filter_samples() from its .filter_samples() proc;
but all other audiofilters do. Is that wrong, or ok?
---8<---
Filter that changes number of samples on single output operation
---
libavfilter/Makefile | 1 +
libavfilter/af_asetframesize.c | 199 ++++++++++++++++++++++++++++++++++++++++
libavfilter/allfilters.c | 1 +
3 files changed, 201 insertions(+), 0 deletions(-)
create mode 100644 libavfilter/af_asetframesize.c
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index e9c9a4b..1938312 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -31,6 +31,7 @@ OBJS-$(CONFIG_AFORMAT_FILTER) += af_aformat.o
OBJS-$(CONFIG_AMERGE_FILTER) += af_amerge.o
OBJS-$(CONFIG_ANULL_FILTER) += af_anull.o
OBJS-$(CONFIG_ARESAMPLE_FILTER) += af_aresample.o
+OBJS-$(CONFIG_ASETFRAMESIZE_FILTER) += af_asetframesize.o
OBJS-$(CONFIG_ASHOWINFO_FILTER) += af_ashowinfo.o
OBJS-$(CONFIG_ASPLIT_FILTER) += af_asplit.o
OBJS-$(CONFIG_ASTREAMSYNC_FILTER) += af_astreamsync.o
diff --git a/libavfilter/af_asetframesize.c b/libavfilter/af_asetframesize.c
new file mode 100644
index 0000000..8f13893
--- /dev/null
+++ b/libavfilter/af_asetframesize.c
@@ -0,0 +1,199 @@
+/*
+ * 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 nb_out_samples; ///< how many samples to output at once
+ 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;
+ int ret;
+ if (!args) {
+ av_log(ctx, AV_LOG_ERROR, "Parameter is required\n");
+ return AVERROR(EINVAL);
+ }
+ ret = sscanf(args, "%u", &asfs->nb_out_samples);
+ if ((ret != 1) || (!asfs->nb_out_samples)) {
+ av_log(ctx, AV_LOG_ERROR, "Invalid specified framesize '%s'\n", 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 ret;
+ 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);
+ ret = av_fifo_realloc2(asfs->fifo, av_fifo_size(asfs->fifo) + nb_bytes_in);
+ if (ret < 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 ret;
+ 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->nb_out_samples * bytes_per_sample;
+ if (av_fifo_size(asfs->fifo) >= bytes_per_outframe)
+ return 1;
+ if (!avfilter_poll_frame(outlink->src->inputs[0]))
+ return 0;
+ ret = avfilter_request_frame(outlink->src->inputs[0]);
+ if (ret < 0) {
+ av_log(outlink->src, AV_LOG_ERROR, "Error occurred when requesting frame from source\n");
+ return ret;
+ }
+ 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->nb_out_samples * 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->nb_out_samples);
+ if (!outsamples) {
+ av_log(outlink->src, AV_LOG_ERROR, "avfilter_get_audio_buffer fail\n");
+ return AVERROR(ENOMEM);
+ }
+ 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->nb_out_samples;
+ 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->nb_out_samples;
+
+ avfilter_filter_samples(outlink, outsamples);
+ avfilter_unref_buffer(outsamples);
+ return 0;
+}
+
+AVFilter avfilter_af_asetframesize = {
+ .name = "asetframesize",
+ .description = NULL_IF_CONFIG_SMALL(
+ "Set the number of samples for the output audio frames"),
+ .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 f0a7f8b..0ed44dc 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -39,6 +39,7 @@ void avfilter_register_all(void)
REGISTER_FILTER (AMERGE, amerge, af);
REGISTER_FILTER (ANULL, anull, af);
REGISTER_FILTER (ARESAMPLE, aresample, af);
+ REGISTER_FILTER (ASETFRAMESIZE, asetframesize, af);
REGISTER_FILTER (ASHOWINFO, ashowinfo, af);
REGISTER_FILTER (ASPLIT, asplit, af);
REGISTER_FILTER (ASTREAMSYNC, astreamsync, af);
--
1.7.7
More information about the ffmpeg-devel
mailing list