[FFmpeg-devel] [ffmpeg-devel] [PATCH 1/4] lavfi: add asrc_abuffer - audio source buffer filter

Stefano Sabatini stefano.sabatini-lala at poste.it
Tue Aug 2 11:48:47 CEST 2011


On date Monday 2011-08-01 12:36:26 +0300, Mina Nagy Zaki encoded:
> Docs were misplaced, here's an updated patch
> 

> From d464fc54179c9d96b74ea6f4e90d9fecc5a78b88 Mon Sep 17 00:00:00 2001
> From: Mina Nagy Zaki <mnzaki at gmail.com>
> Date: Mon, 1 Aug 2011 11:33:26 +0300
> Subject: [PATCH 02/11] lavfi: add asrc_abuffer - audio source buffer filter
> 
> Originally based on code by Stefano Sabatini and S. N. Hemanth
> ---
>  doc/filters.texi           |   48 ++++++
>  libavfilter/Makefile       |    2 +
>  libavfilter/allfilters.c   |    1 +
>  libavfilter/asrc_abuffer.c |  377 ++++++++++++++++++++++++++++++++++++++++++++
>  libavfilter/asrc_abuffer.h |   81 ++++++++++
>  5 files changed, 509 insertions(+), 0 deletions(-)
>  create mode 100644 libavfilter/asrc_abuffer.c
>  create mode 100644 libavfilter/asrc_abuffer.h
> 
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 87f6c6a..dd1a1ee 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -128,6 +128,54 @@ Pass the audio source unchanged to the output.
>  
>  Below is a description of the currently available audio sources.
>  
> + at section abuffer
> +
> +Buffer audio frames, and make them available to the filter chain.
> +
> +This source is mainly intended for a programmatic use, in particular
> +through the interface defined in @file{libavfilter/asrc_abuffer.h}.
> +
> +It accepts the following mandatory parameters:
> + at var{sample_rate}:@var{sample_fmt}:@var{channel_layout}:@var{packing}
> +
> + at table @option
> +
> + at item sample_rate
> +
> +The sample rate of the incoming audio buffers.
> +
> + at item sample_fmt
> +
> +The sample format of the incoming audio buffers.
> +Either a sample format name or its corresponging integer representation from
> +the enum AVSampleFormat in @file{libavutil/samplefmt.h}
> +
> + at item channel_layout
> +
> +The channel layout of the incoming audio buffers.
> +Either a channel layout name from channel_layout_map in
> + at file{libavutil/audioconvert.c} or its corresponding integer representation
> +from the AV_CH_LAYOUT_* macros in @file{libavutil/audioconvert.h}

Note: somehow unpretty (we should list them in the manual and/or list
through some switch).

> +
> + at item packing
> +
> +Either "packed" or "planar", or their integer representation: 0 or 1
> +respectively.
> +
> + at end table
> +
> +For example:
> + at example
> +abuffer=44100:s16:stereo:planar
> + at end example
> +
> +will instruct the source to accept planar 16bit signed stereo at 44100Hz.
> +Since the sample format with name "s16" corresponds to the number
> +1 and the "stereo" channel layout corresponds to the value 3
> + at example
> +abuffer=44100:1:3:1
> + at end example
> +
>  @section anullsrc
>  
>  Null audio source, never return audio frames. It is mainly useful as a
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 865ba1e..686fd30 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -65,6 +65,8 @@ OBJS-$(CONFIG_UNSHARP_FILTER)                += vf_unsharp.o
>  OBJS-$(CONFIG_VFLIP_FILTER)                  += vf_vflip.o
>  OBJS-$(CONFIG_YADIF_FILTER)                  += vf_yadif.o
>  
> +OBJS-$(CONFIG_ABUFFER_FILTER)                += asrc_abuffer.o
> +
>  OBJS-$(CONFIG_BUFFER_FILTER)                 += vsrc_buffer.o
>  OBJS-$(CONFIG_COLOR_FILTER)                  += vsrc_color.o
>  OBJS-$(CONFIG_FREI0R_SRC_FILTER)             += vf_frei0r.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 56baa50..49be7b7 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -37,6 +37,7 @@ void avfilter_register_all(void)
>      REGISTER_FILTER (AFORMAT,     aformat,     af);
>      REGISTER_FILTER (ANULL,       anull,       af);
>  
> +    REGISTER_FILTER (ABUFFER,     abuffer,     asrc);
>      REGISTER_FILTER (ANULLSRC,    anullsrc,    asrc);
>  
>      REGISTER_FILTER (ANULLSINK,   anullsink,   asink);
> diff --git a/libavfilter/asrc_abuffer.c b/libavfilter/asrc_abuffer.c
> new file mode 100644
> index 0000000..3375b28
> --- /dev/null
> +++ b/libavfilter/asrc_abuffer.c
> @@ -0,0 +1,377 @@
> +/*
> + * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram
> + * Copyright (c) 2011 Mina Nagy Zaki
> + *
> + * 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
> + * Memory buffer source filter for audio
> + */
> +
> +#include "libavutil/audioconvert.h"
> +#include "libavutil/eval.h"
> +#include "asrc_abuffer.h"
> +
> +typedef struct {
> +    // Audio format of incoming buffers
> +    int sample_rate;
> +    unsigned int sample_fmt;
> +    int64_t channel_layout;
> +    int planar;
> +    // FIFO buffer of audio frame pointers
> +    AVFifoBuffer *fifo;
> +    // Normalization filters
> +    AVFilterContext *aconvert;
> +    AVFilterContext *aresample;
> +} ABufferSourceContext;
> +
> +#define FIFO_SIZE 8
> +
> +static void buf_free(AVFilterBuffer *ptr)
> +{
> +    av_free(ptr);
> +    return;
> +}
> +
> +static inline void setup_link(ABufferSourceContext *abuffer, AVFilterLink *link)
> +{
> +    link->format         = abuffer->sample_fmt;
> +    link->channel_layout = abuffer->channel_layout;
> +    link->planar         = abuffer->planar;
> +    link->sample_rate    = abuffer->sample_rate;
> +}
> +
> +static int insert_filter(ABufferSourceContext *abuffer,

> +                         AVFilterLink *link, AVFilterContext **filter,

for avoiding horrible confusion please filter -> filter_ctx, or
filt_ctx or filctx.

> +                         const char *filt_name, const char *args)
> +{
> +    int ret;
> +
> +    if ((ret = avfilter_open(filter, avfilter_get_by_name(filt_name),
> +                            "audio filter")) < 0)
> +        return ret;
> +

> +    link->src->outputs[0] = NULL;
> +    if ((ret = avfilter_link(link->src, 0, *filter, 0)) < 0) {
> +        link->src->outputs[0] = link;
> +        return ret;
> +    }
> +
> +    link->src             = *filter;
> +    link->srcpad          = &((*filter)->output_pads[0]);
> +    (*filter)->outputs[0] = link;

avfilter_insert_filter()?

> +
> +    setup_link(abuffer, (*filter)->inputs[0]);
> +

> +    if ((ret = avfilter_init_filter(*filter, args,  NULL)) < 0)
> +    {

Nit++: if (...) {

> +        avfilter_free(*filter);
> +        return ret;
> +    }
> +

> +    (*filter)->outputs[0]->srcpad->config_props((*filter)->outputs[0]);

missing check

        link = (*filter)->outputs[0];
        return link->srcpad->config_props(link);

looks (much) more readable

> +
> +    return 0;
> +}
> +
> +static int remove_filter(AVFilterContext *ctx, AVFilterContext **filter)
> +{
> +    int ret;
> +    AVFilterContext *dst = (*filter)->outputs[0]->dst;
> +    unsigned dstpad_idx = (*filter)->outputs[0]->dstpad - dst->input_pads;
> +
> +    avfilter_free(*filter);
> +    *filter = NULL;
> +
> +    if ((ret = avfilter_link(ctx, 0, dst, dstpad_idx)) < 0)
> +        return ret;
> +
> +    setup_link(ctx->priv, ctx->outputs[0]);
> +
> +    return 0;
> +}
> +

> +static int reconfigure_filter(AVFilterContext *filter)

filter -> filt_ctx or the equivalent

> +{
> +    int ret;
> +
> +    filter->filter->uninit(filter);
> +    if ((ret = filter->filter->init(filter, NULL, NULL)) < 0)
> +        return ret;

> +    if ((ret = filter->outputs[0]->srcpad->config_props(
> +                                               filter->outputs[0])) < 0)
> +        return ret;
> +    if ((ret = filter->inputs[0]->srcpad->config_props(
> +                                               filter->inputs[0])) < 0)
> +        return ret;

Nit: outlink/inlink vars may help readability.

> +
> +    return 0;
> +}
> +
> +int av_asrc_buffer_add_audio_buffer_ref(AVFilterContext *ctx,
> +                                        AVFilterBufferRef *samplesref,
> +                                        int av_unused flags)
> +{
> +    ABufferSourceContext *abuffer = ctx->priv;
> +    AVFilterLink *link;
> +    int ret;
> +
> +    if (av_fifo_space(abuffer->fifo) < sizeof(samplesref)) {
> +        av_log(ctx, AV_LOG_ERROR,
> +               "Buffering limit reached. Please consume some available frames "
> +               "before adding new ones.\n");

> +        return AVERROR(ENOMEM);

Nit: technically this could be considered an EINVAL, there was no
memory problem in the sense than no malloc/realloc failed, but we
simply asked for more space than it was available in the buffer.

> +    }
> +
> +    // Normalize input
> +

> +    link = ctx->outputs[0];

this can be done in the var init, save a line

> +    if (!link->sample_rate)
> +        abuffer->sample_rate = link->sample_rate = samplesref->audio->sample_rate;

why this check? sample_rate should be always set in the link, or some
configuration error happened.

> +    if (samplesref->audio->sample_rate != link->sample_rate) {
> +        av_log(ctx, AV_LOG_INFO, "Audio sample rate changed, normalizing\n");

For debugging purposes, I ask you to add indications of src and dst
sample rates, that's extremely helpful when debugging such things (or
spotting a problem from reading an user report).

> +        if (!abuffer->aresample) {
> +            char args[16];
> +            snprintf(args, sizeof(args), "%i", abuffer->sample_rate);
> +            ret = insert_filter(abuffer, link, &abuffer->aresample, "aresample", args);
> +            if (ret < 0) return ret;
> +        } else {
> +            link = abuffer->aresample->outputs[0];
> +            if (samplesref->audio->sample_rate == link->sample_rate)
> +                remove_filter(ctx, &abuffer->aresample);
> +            else
> +                reconfigure_filter(abuffer->aresample);
> +        }
> +    }
> +
> +    link = ctx->outputs[0];
> +    if (samplesref->format                != link->format         ||
> +        samplesref->audio->channel_layout != link->channel_layout ||
> +        samplesref->audio->planar         != link->planar) {
> +
> +        abuffer->sample_fmt     = samplesref->format;
> +        abuffer->channel_layout = samplesref->audio->channel_layout;
> +        abuffer->planar         = samplesref->audio->planar;
> +

> +        av_log(ctx, AV_LOG_INFO, "Audio input format changed, normalizing\n");

Ditto, specify what is changing to what.

> +
> +        if (!abuffer->aconvert) {
> +            ret = insert_filter(abuffer, link, &abuffer->aconvert, "aconvert", NULL);
> +            if (ret < 0) return ret;
> +        } else {
> +            link = abuffer->aconvert->outputs[0];
> +            if (samplesref->format                == link->format         &&
> +                samplesref->audio->channel_layout == link->channel_layout &&
> +                samplesref->audio->planar         == link->planar
> +               )
> +                remove_filter(ctx, &abuffer->aconvert);
> +            else
> +                reconfigure_filter(abuffer->aconvert);
> +        }
> +    }
> +
> +    if (sizeof(samplesref) != av_fifo_generic_write(abuffer->fifo, &samplesref,
> +                                                    sizeof(samplesref), NULL))
> +        return AVERROR(ENOMEM);

a log may be useful (same consideration on EINVAL - but that's somehow
subjective and i have no strong argument for preferring one over the other)

[...]

-- 
FFmpeg = Fundamental Fundamental Mysterious Peaceless Eccentric Goblin


More information about the ffmpeg-devel mailing list