[FFmpeg-devel] [PATCH] avfilter: add apad filter

Clément Bœsch ubitux at gmail.com
Tue Dec 18 12:52:45 CET 2012


On Tue, Dec 18, 2012 at 05:23:46AM +0100, Michael Niedermayer wrote:
> This filter pads an audio stream with silence
> It can together with -shortest be used to extend audio streams to
> the same length as video.
> 
> Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
> ---
>  libavfilter/Makefile     |    1 +
>  libavfilter/af_apad.c    |  116 ++++++++++++++++++++++++++++++++++++++++++++++
>  libavfilter/allfilters.c |    1 +
>  3 files changed, 118 insertions(+)
>  create mode 100644 libavfilter/af_apad.c
> 

Would be nice to have an entry + example in doc/filters.texi.

> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 58f8954..1145aeb 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -53,6 +53,7 @@ OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
>  OBJS-$(CONFIG_AMERGE_FILTER)                 += af_amerge.o
>  OBJS-$(CONFIG_AMIX_FILTER)                   += af_amix.o
>  OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
> +OBJS-$(CONFIG_APAD_FILTER)                   += af_apad.o
>  OBJS-$(CONFIG_ARESAMPLE_FILTER)              += af_aresample.o
>  OBJS-$(CONFIG_ASELECT_FILTER)                += f_select.o
>  OBJS-$(CONFIG_ASENDCMD_FILTER)               += f_sendcmd.o
> diff --git a/libavfilter/af_apad.c b/libavfilter/af_apad.c
> new file mode 100644
> index 0000000..e39a33e
> --- /dev/null
> +++ b/libavfilter/af_apad.c
> @@ -0,0 +1,116 @@
> +/*
> + * Copyright (c) 2012 Michael Niedermayer
> + *
> + * 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
> + *
> + * Based on af_aresample.c

nit: move to @file below?

> + */
> +
> +/**
> + * @file
> + * audio pad filter
> + */
> +
> +#include "libavutil/avstring.h"
> +#include "libavutil/channel_layout.h"
> +#include "libavutil/opt.h"
> +#include "libavutil/samplefmt.h"
> +#include "libavutil/avassert.h"
> +#include "avfilter.h"
> +#include "audio.h"
> +#include "internal.h"
> +
> +typedef struct {
> +    int64_t next_pts;
> +} APadContext;
> +
> +static av_cold int init(AVFilterContext *ctx, const char *args)
> +{
> +    APadContext *apad = ctx->priv;
> +
> +    apad->next_pts = AV_NOPTS_VALUE;
> +
> +    return 0;
> +}
> +
> +static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
> +{
> +    AVFilterContext *ctx = inlink->dst;
> +    APadContext *apad = ctx->priv;
> +    apad->next_pts = frame->pts + av_rescale_q(frame->audio->nb_samples, (AVRational){1 ,inlink->sample_rate}, inlink->time_base);

style: ", " instead of " ,"

> +    return ff_filter_frame(ctx->outputs[0], frame);
> +}
> +
> +static int request_frame(AVFilterLink *outlink)
> +{
> +    AVFilterContext *ctx = outlink->src;
> +    APadContext *apad = ctx->priv;
> +    int ret;
> +
> +    ret = ff_request_frame(ctx->inputs[0]);
> +
> +    if (ret == AVERROR_EOF) {
> +        int n_out = 4096;

We should consider making it an option, but can be done later.

> +        AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
> +        if (!outsamplesref)
> +            return AVERROR(ENOMEM);
> +
> +        av_assert0(outsamplesref->audio->sample_rate == outlink->sample_rate);
> +        av_assert0(outsamplesref->audio->nb_samples  == n_out);
> +
> +        av_samples_set_silence(outsamplesref->extended_data, 0,
> +                               n_out,
> +                               outsamplesref->audio->channels,
> +                               outsamplesref->format);
> +
> +        outsamplesref->pts = apad->next_pts;
> +        if(apad->next_pts != AV_NOPTS_VALUE)

style: I don't mind whatever style is selected, but please be consistent
with it within the file; "if(" or "if (", but same everywhere please.

> +            apad->next_pts += av_rescale_q(n_out, (AVRational){1 ,outlink->sample_rate}, outlink->time_base);

ditto: ", "

> +
> +        ff_filter_frame(outlink, outsamplesref);
> +        return 0;

return ff_filter_frame()?

> +    }
> +    return ret;
> +}
> +
> +static const AVFilterPad apad_inputs[] = {
> +    {
> +        .name         = "default",
> +        .type         = AVMEDIA_TYPE_AUDIO,
> +        .filter_frame = filter_frame,
> +        .min_perms    = AV_PERM_READ,
> +    },
> +    { NULL },
> +};
> +
> +static const AVFilterPad apad_outputs[] = {
> +    {
> +        .name          = "default",
> +        .request_frame = request_frame,
> +        .type          = AVMEDIA_TYPE_AUDIO,
> +    },
> +    { NULL },
> +};
> +
> +AVFilter avfilter_af_apad = {
> +    .name          = "apad",
> +    .description   = NULL_IF_CONFIG_SMALL("Pad audio."),

"Pad audio with silence."?

> +    .init          = init,
> +    .priv_size     = sizeof(APadContext),
> +    .inputs        = apad_inputs,
> +    .outputs       = apad_outputs,
> +};
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 8c8f1fc..fc96809 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -45,6 +45,7 @@ void avfilter_register_all(void)
>      REGISTER_FILTER (AMERGE,      amerge,      af);
>      REGISTER_FILTER (AMIX,        amix,        af);
>      REGISTER_FILTER (ANULL,       anull,       af);
> +    REGISTER_FILTER (APAD,        apad,        af);
>      REGISTER_FILTER (ARESAMPLE,   aresample,   af);
>      REGISTER_FILTER (ASELECT,     aselect,     af);
>      REGISTER_FILTER (ASENDCMD,    asendcmd,    af);

LGTM otherwise.

Note that this is likely achievable with a concat + aevalsrc=0, so maybe
it could be worth mentioning the filter in the documentation example of
aevalsrc=0.

-- 
Clément B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20121218/2c6bdcbd/attachment.asc>


More information about the ffmpeg-devel mailing list