[FFmpeg-devel] [PATCH] lavfi: add aformat filter

Stefano Sabatini stefano.sabatini-lala at poste.it
Tue Aug 2 12:44:42 CEST 2011


On date Monday 2011-08-01 12:39:13 +0300, Mina Nagy Zaki encoded:
> ---
>  doc/filters.texi         |   18 +++++++
>  libavfilter/Makefile     |    1 +
>  libavfilter/af_aformat.c |  114 ++++++++++++++++++++++++++++++++++++++++++++++
>  libavfilter/allfilters.c |    1 +
>  4 files changed, 134 insertions(+), 0 deletions(-)
>  create mode 100644 libavfilter/af_aformat.c
> 
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 2747402..87f6c6a 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -99,6 +99,24 @@ build.
>  
>  Below is a description of the currently available audio filters.
>  
> + at section aformat
> +
> +Convert the input audio to one of the specified formats.
> +The framework will negotiate the most apropriate format to minimize conversions.

typo: apPropriate

> +
> +The filter accepts three lists of formats, separated by ":",
> +"sample_formats:channel_layouts:packing"
> +
> +Elements in the list are seperated by "," which has to be escaped in the

sepArated

> +filtergraph specification.

Specify semantics for "all".

> +
> +Some examples follow:
> + at example
> +aformat=u8\\,s16:mono:packed
> +
> +aformat=s16:mono\\,stereo:packed\\,planar
> + at end example
> +
>  @section anull
>  
>  Pass the audio source unchanged to the output.
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 6007fed..865ba1e 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -18,6 +18,7 @@ OBJS = allfilters.o                                                     \
>  
>  OBJS-$(CONFIG_AVCODEC)                       += avcodec.o
>  
> +OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
>  OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
>  
>  OBJS-$(CONFIG_ANULLSRC_FILTER)               += asrc_anullsrc.o
> diff --git a/libavfilter/af_aformat.c b/libavfilter/af_aformat.c
> new file mode 100644
> index 0000000..03b915c
> --- /dev/null
> +++ b/libavfilter/af_aformat.c
> @@ -0,0 +1,114 @@
> +/*
> + * 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
> + * format and noformat audio filters
> + */
> +

> +#include "libavcodec/audioconvert.h"

libavcodec? 

> +#include "avfilter.h"
> +
> +typedef struct {
> +    AVFilterFormats *formats, *chlayouts, *packing;
> +} AFormatContext;
> +
> +static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
> +{
> +    AFormatContext * const aformat = ctx->priv;
> +    char *arg, *fmt_str;
> +    int fmt;
> +

> +    if (!args) return AVERROR(EINVAL);

complain before exit.

> +
> +    arg = strsep(&args, ":");
> +    while (fmt_str = strsep(&arg, ",")) {
> +        if (!strcmp(fmt_str, "all")) {
> +            aformat->formats = avfilter_all_formats(AVMEDIA_TYPE_AUDIO);
> +            break;
> +        }
> +        fmt = av_get_sample_fmt(fmt_str);

> +        if (fmt == -1) {

Nit: AV_SAMPLE_FMT_NONE

> +            av_log(ctx, AV_LOG_ERROR, "Bad sample format '%s'\n", fmt_str);
> +            return AVERROR(EINVAL);
> +        }
> +        avfilter_add_format(&aformat->formats, fmt);
> +    }
> +
> +    arg = strsep(&args, ":");
> +    while (fmt_str = strsep(&arg, ",")) {
> +        if (!strcmp(fmt_str, "all")) {
> +            aformat->chlayouts = avfilter_all_channel_layouts();
> +            break;
> +        }
> +        fmt = av_get_channel_layout(fmt_str);
> +        if (fmt == -1) {
> +            av_log(ctx, AV_LOG_ERROR, "Bad channel layout '%s'\n", fmt_str);
> +            return AVERROR(EINVAL);
> +        }
> +        avfilter_add_format(&aformat->chlayouts, fmt);
> +    }
> +
> +    arg = strsep(&args, ":");
> +    while (fmt_str = strsep(&arg, ",")) {
> +        if (!strcmp(fmt_str, "all")) {
> +            aformat->packing = avfilter_all_packing_formats();
> +            break;
> +        }
> +        char *tail;
> +        fmt = strtol(fmt_str, &tail, 10);
> +        if (*tail) fmt = strcmp(fmt_str, "packed");
> +        avfilter_add_format(&aformat->packing,
> +                            fmt ? AVFILTER_PLANAR : AVFILTER_PACKED);
> +    }
> +
> +    return 0;
> +}
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> +    AFormatContext * const aformat = ctx->priv;
> +
> +    avfilter_set_common_sample_formats (ctx, aformat->formats);
> +    avfilter_set_common_channel_layouts(ctx, aformat->chlayouts);
> +    avfilter_set_common_packing_formats(ctx, aformat->packing);
> +    return 0;
> +}
> +
> +static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
> +{
> +    avfilter_filter_samples(inlink->dst->outputs[0], insamplesref);
> +}
> +
> +AVFilter avfilter_af_aformat = {
> +    .name          = "aformat",

> +    .description   = NULL_IF_CONFIG_SMALL(""),

Missing description.

> +    .init          = init,
> +    .query_formats = query_formats,
> +    .priv_size     = sizeof(AFormatContext),
> +
> +    .inputs        = (AVFilterPad[]) {{ .name            = "default",
> +                                        .type            = AVMEDIA_TYPE_AUDIO,
> +                                        .filter_samples  = filter_samples},
> +                                      { .name = NULL}},
> +    .outputs       = (AVFilterPad[]) {{ .name            = "default",
> +                                        .type            = AVMEDIA_TYPE_AUDIO},
> +                                      { .name = NULL}},
> +};
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 99f7bc4..56baa50 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -34,6 +34,7 @@ void avfilter_register_all(void)
>          return;
>      initialized = 1;
>  
> +    REGISTER_FILTER (AFORMAT,     aformat,     af);
>      REGISTER_FILTER (ANULL,       anull,       af);
>  
>      REGISTER_FILTER (ANULLSRC,    anullsrc,    asrc);

Looks fine otherwise.
-- 
FFmpeg = Faithful and Faithful Mean Ponderous Erudite Guru


More information about the ffmpeg-devel mailing list