[FFmpeg-devel] [PATCH 1/2] lavfi: add astreamsync audio filter.

Stefano Sabatini stefasab at gmail.com
Wed Dec 28 15:09:00 CET 2011


On date Saturday 2011-12-24 17:57:52 +0100, Nicolas George encoded:
> 
> Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
> ---
>  Changelog                    |    1 +
>  doc/filters.texi             |   29 ++++++
>  libavfilter/Makefile         |    1 +
>  libavfilter/af_astreamsync.c |  209 ++++++++++++++++++++++++++++++++++++++++++
>  libavfilter/allfilters.c     |    1 +
>  5 files changed, 241 insertions(+), 0 deletions(-)
>  create mode 100644 libavfilter/af_astreamsync.c
> 
> diff --git a/Changelog b/Changelog
> index b4e7b58..7044b4c 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -7,6 +7,7 @@ version next:
>  - SBaGen (SBG) binaural beats script demuxer
>  - OpenMG Audio muxer
>  - SMJPEG demuxer
> +- astreamsync audio filter
>  
>  
>  version 0.9:
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 699e0c1..ce4a74f 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -224,6 +224,35 @@ expressed in the form "[@var{c0} @var{c1} @var{c2} @var{c3} @var{c4} @var{c5}
>  @var{c6} @var{c7}]"
>  @end table
>  
> + at section astreamsync
> +
> +Forward two audio streams and control the order the buffers are forwarded.
> +
> +The argument to the filter is an expression deciding which stream should be
> +forwarded next: if the result is negative, the first stream is forwarded; if
> +the result is positive or zero, the second stream is forwarded. It can use
> +the following variables:
> +
> + at table @var
> + at item b1 b2
> +number of buffers forwarded so far on each stream
> + at item s1 s2
> +number of samples forwarded so far on each stream
> + at item t1 t2
> +current timestamp of each stream
> + at end table
> +
> +The default value is @code{t1-t2}, which means to always forward the stream
> +that has a smaller timestamp.
> +
> +Example: stress-test @code{amerge} by randomly sending buffers on the wrong
> +input, while avoiding too much of a desynchronization:
> + at example
> +amovie=file.ogg [a] ; amovie=file.mp3 [b] ;
> +[a] [b] astreamsync=(2*random(1))-1+tanh(5*(t1-t2)) [a2] [b2] ;
> +[a2] [b2] amerge
> + at end example
> +
>  @section earwax
>  
>  Make audio easier to listen to on headphones.
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 9977753..555767b 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -29,6 +29,7 @@ OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
>  OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
>  OBJS-$(CONFIG_ARESAMPLE_FILTER)              += af_aresample.o
>  OBJS-$(CONFIG_ASHOWINFO_FILTER)              += af_ashowinfo.o
> +OBJS-$(CONFIG_ASTREAMSYNC_FILTER)            += af_astreamsync.o
>  OBJS-$(CONFIG_EARWAX_FILTER)                 += af_earwax.o
>  OBJS-$(CONFIG_PAN_FILTER)                    += af_pan.o
>  OBJS-$(CONFIG_VOLUME_FILTER)                 += af_volume.o
> diff --git a/libavfilter/af_astreamsync.c b/libavfilter/af_astreamsync.c
> new file mode 100644
> index 0000000..ce5dcbf
> --- /dev/null
> +++ b/libavfilter/af_astreamsync.c
> @@ -0,0 +1,209 @@
> +/*
> + * Copyright (c) 2011 Nicolas George <nicolas.george at normalesup.org>
> + *
> + * 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 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
> + * Stream (de)synchronization filter
> + */
> +
> +#include "libavutil/eval.h"
> +#include "avfilter.h"
> +#include "internal.h"
> +
> +#define QUEUE_SIZE 16
> +
> +static const char * const var_names[] = {
> +    "b1", "b2",
> +    "s1", "s2",
> +    "t1", "t2",
> +    NULL
> +};
> +
> +enum var_name {
> +    VAR_B1, VAR_B2,
> +    VAR_S1, VAR_S2,
> +    VAR_T1, VAR_T2,
> +    VAR_NB
> +};
> +
> +typedef struct {
> +    AVExpr *expr;
> +    double var_values[VAR_NB];
> +    struct buf_queue {
> +        AVFilterBufferRef *buf[QUEUE_SIZE];
> +        unsigned tail, nb;
> +        /* buf[tail] is the oldest,
> +           buf[(tail + nb) % QUEUE_SIZE] is where the next is added */
> +    } queue[2];
> +    int req[2];
> +    int next_out;
> +    int eof; /* bitmask, one bit for each stream */
> +} AStreamSyncContext;
> +
> +static const char *default_expr = "t1-t2";
> +
> +static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
> +{
> +    AStreamSyncContext *as = ctx->priv;
> +    const char *expr = args0 ? args0 : default_expr;
> +    int r, i;
> +
> +    r = av_expr_parse(&as->expr, expr, var_names,
> +                      NULL, NULL, NULL, NULL, 0, ctx);
> +    if (r < 0) {
> +        av_log(ctx, AV_LOG_ERROR, "Error in expression \"%s\"\n", expr);
> +        return r;
> +    }
> +    for (i = 0; i < 42; i++)
> +        av_expr_eval(as->expr, as->var_values, NULL); /* exercize prng */
> +    return 0;

Note: do we have a way to initialize the seed of the random()
expression?
>From docs:
|@item random(x)
|Return a pseudo random value between 0.0 and 1.0. @var{x} is the index of the
|internal variable which will be used to save the seed/state.

so I suppose we may use one of the internal variables and initialize
it to an user defined seed value (not necessarily in this patch if you
don't mind).

[...]

Patch looks good to me otherwise.
-- 
FFmpeg = Frightening Fancy Merciless Patchable Esoteric Gadget


More information about the ffmpeg-devel mailing list