[FFmpeg-devel] [PATCH] avfilter: add audio noise source

Nicolas George george at nsup.org
Fri Oct 30 18:37:00 CET 2015


Le nonidi 9 brumaire, an CCXXIV, Kyle Swanson a écrit :
> Signed-off-by: Kyle Swanson <k at ylo.ph>
> ---
>  Changelog                |   1 +
>  doc/filters.texi         |  30 ++++++++++
>  libavfilter/Makefile     |   1 +
>  libavfilter/allfilters.c |   1 +
>  libavfilter/asrc_noise.c | 141 +++++++++++++++++++++++++++++++++++++++++++++++
>  libavfilter/version.h    |   2 +-
>  6 files changed, 175 insertions(+), 1 deletion(-)
>  create mode 100644 libavfilter/asrc_noise.c
> 
> diff --git a/Changelog b/Changelog
> index c49e383..d2ea2e1 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -29,6 +29,7 @@ version <next>:
>  - vibrato filter
>  - innoHeim/Rsupport Screen Capture Codec decoder
>  - ADPCM AICA decoder
> +- noise audio source

Thanks for the patch. See comments below.

>  version 2.8:
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 15ea77a..0d901cc 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -3193,6 +3193,36 @@ ffplay -f lavfi flite=text='No more be grieved for which that thou hast done.'
>  For more information about libflite, check:
>  @url{http://www.speech.cs.cmu.edu/flite/}
>  
> + at section noise
> +
> +Generate a white noise audio signal.
> +
> +The filter accepts the following options:
> +
> + at table @option
> +
> + at item sample_rate, r
> +Specify the sample rate. Default value is 48000 Hz.
> +
> + at item duration, d
> +Specify the duration of the generated audio stream. Default value is 10 seconds.
> +
> + at item amplitude, a
> +Specify the amplitude (0.0 - 1.0) of the generated audio stream. Default value is 1.0.
> +
> + at end table
> +
> + at subsection Examples
> +
> + at itemize
> +
> + at item
> +Generate 60 seconds of white noise, with a 44.1 kHz sampling rate and an amplitude of 0.5:
> + at example
> +noise=d=60:r=44100:a=0.5
> + at end example
> + at end itemize
> +
>  @section sine
>  
>  Generate an audio signal made of a sine wave with amplitude 1/8.
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index c5819d4..a0b2232 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -93,6 +93,7 @@ OBJS-$(CONFIG_VOLUMEDETECT_FILTER)           += af_volumedetect.o
>  OBJS-$(CONFIG_AEVALSRC_FILTER)               += aeval.o
>  OBJS-$(CONFIG_ANULLSRC_FILTER)               += asrc_anullsrc.o
>  OBJS-$(CONFIG_FLITE_FILTER)                  += asrc_flite.o
> +OBJS-$(CONFIG_NOISE_FILTER)                  += asrc_noise.o
>  OBJS-$(CONFIG_SINE_FILTER)                   += asrc_sine.o
>  
>  OBJS-$(CONFIG_ANULLSINK_FILTER)              += asink_anullsink.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index a538b81..f820441 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -115,6 +115,7 @@ void avfilter_register_all(void)
>      REGISTER_FILTER(AEVALSRC,       aevalsrc,       asrc);
>      REGISTER_FILTER(ANULLSRC,       anullsrc,       asrc);
>      REGISTER_FILTER(FLITE,          flite,          asrc);
> +    REGISTER_FILTER(NOISE,          noise,          asrc);
>      REGISTER_FILTER(SINE,           sine,           asrc);
>  
>      REGISTER_FILTER(ANULLSINK,      anullsink,      asink);
> diff --git a/libavfilter/asrc_noise.c b/libavfilter/asrc_noise.c
> new file mode 100644
> index 0000000..00370a6
> --- /dev/null
> +++ b/libavfilter/asrc_noise.c
> @@ -0,0 +1,141 @@
> +/*
> + * Copyright (c) 2015 Kyle Swanson <k at ylo.ph>.
> + *
> + * 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
> + */
> +
> +#include <float.h>
> +
> +#include "libavutil/opt.h"
> +#include "audio.h"
> +#include "avfilter.h"
> +#include "internal.h"
> +#include "libavutil/lfg.h"
> +
> +typedef struct {
> +    const AVClass *class;
> +    int sample_rate;
> +    double amplitude;
> +    double dur_sec;
> +    int64_t dur_samp;

> +    AVLFG c;

The doxy for it says:

 * Please also consider a simple LCG like state= state*1664525+1013904223,
 * it may be good enough and faster for your specific use case.

Did you test in terms of speed and noise quality? I used this exact LCG for
the pink noise and it sounds fine. The LCG has an additional bonus on top of
speed: it is seekable; it may come in handy at a later time.

> +} NoiseContext;
> +
> +#define OFFSET(x) offsetof(NoiseContext, x)
> +#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
> +
> +static const AVOption noise_options[] = {
> +    { "sample_rate", "set sample rate",    OFFSET(sample_rate),    AV_OPT_TYPE_INT,     {.i64 = 48000}, 15,   INT_MAX,  FLAGS },
> +    { "r",           "set sample rate",    OFFSET(sample_rate),    AV_OPT_TYPE_INT,     {.i64 = 48000}, 15,   INT_MAX,  FLAGS },
> +    { "amplitude",   "set amplitude",      OFFSET(amplitude),      AV_OPT_TYPE_DOUBLE,  {.dbl = 1.},    0.,   1.,       FLAGS },
> +    { "a",           "set amplitude",      OFFSET(amplitude),      AV_OPT_TYPE_DOUBLE,  {.dbl = 1.},    0.,   1.,       FLAGS },

> +    { "duration",    "set duration",       OFFSET(dur_sec),        AV_OPT_TYPE_DOUBLE,  {.dbl = 10.},   0.,   DBL_MAX,  FLAGS },
> +    { "d",           "set duration",       OFFSET(dur_sec),        AV_OPT_TYPE_DOUBLE,  {.dbl = 10.},   0.,   DBL_MAX,  FLAGS },

AV_OPT_TYPE_DURATION would be better for this option.

> +    {NULL}
> +};
> +
> +AVFILTER_DEFINE_CLASS(noise);
> +
> +static av_cold int query_formats(AVFilterContext *ctx)
> +{
> +    NoiseContext *s = ctx->priv;
> +    static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 };
> +    int sample_rates[] = { s->sample_rate, -1 };
> +    static const enum AVSampleFormat sample_fmts[] = {

> +        AV_SAMPLE_FMT_DBL,

Floating point code does not yield the exact same result on different
hardware, and thus is harder to test, better use integers whenever possible.
In this case, I suspect S32, or even possibly S16, would be perfectly fine.

> +        AV_SAMPLE_FMT_NONE
> +    };
> +
> +    AVFilterFormats *formats;
> +    AVFilterChannelLayouts *layouts;
> +    int ret;
> +
> +    formats = ff_make_format_list(sample_fmts);
> +    if (!formats)
> +        return AVERROR(ENOMEM);
> +    ret = ff_set_common_formats (ctx, formats);
> +    if (ret < 0)
> +        return ret;
> +
> +    layouts = avfilter_make_format64_list(chlayouts);
> +    if (!layouts)
> +        return AVERROR(ENOMEM);
> +    ret = ff_set_common_channel_layouts(ctx, layouts);
> +    if (ret < 0)
> +        return ret;
> +
> +    formats = ff_make_format_list(sample_rates);
> +    if (!formats)
> +        return AVERROR(ENOMEM);
> +    return ff_set_common_samplerates(ctx, formats);
> +}
> +
> +static av_cold int config_props(AVFilterLink *outlink)
> +{
> +    AVFilterContext *ctx = outlink->src;
> +    NoiseContext *s = ctx->priv;
> +    s->dur_samp = s->dur_sec * s->sample_rate;
> +    return 0;
> +}
> +
> +static int request_frame(AVFilterLink *outlink) {
> +    AVFilterContext *ctx = outlink->src;
> +    NoiseContext *s = ctx->priv;
> +    AVFrame *frame;
> +    int nb_samples, i;
> +    double *dst;
> +
> +    nb_samples = 1024;
> +    if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
> +        return AVERROR(ENOMEM);
> +    dst = (double *)frame->data[0];
> +
> +    for (i = 0; i < nb_samples; i++) {
> +        dst[i]= s->amplitude * ((2 * ((double) av_lfg_get(&s->c) / 0xffffffff)) - 1);
> +        s->dur_samp--;
> +        if (s->dur_samp <= 0)
> +            return AVERROR_EOF;
> +    }
> +    return ff_filter_frame(outlink, frame);
> +}
> +
> +static av_cold int init(AVFilterContext *ctx) {
> +    NoiseContext *s = ctx->priv;
> +    av_lfg_init(&s->c, 0);
> +    return 0;
> +}
> +
> +static const AVFilterPad noise_outputs[] = {
> +    {
> +        .name          = "default",
> +        .type          = AVMEDIA_TYPE_AUDIO,
> +        .request_frame = request_frame,
> +        .config_props  = config_props,
> +    },
> +    { NULL }
> +};
> +
> +AVFilter ff_asrc_noise = {
> +    .name          = "noise",
> +    .description   = NULL_IF_CONFIG_SMALL("Generate white noise audio signal."),
> +    .init          = init,
> +    .query_formats = query_formats,
> +    .priv_size     = sizeof(NoiseContext),
> +    .inputs        = NULL,
> +    .outputs       = noise_outputs,
> +    .priv_class    = &noise_class,
> +};
> diff --git a/libavfilter/version.h b/libavfilter/version.h
> index c3ecf91..05b0735 100644
> --- a/libavfilter/version.h
> +++ b/libavfilter/version.h
> @@ -30,7 +30,7 @@
>  #include "libavutil/version.h"
>  
>  #define LIBAVFILTER_VERSION_MAJOR   6
> -#define LIBAVFILTER_VERSION_MINOR  14
> +#define LIBAVFILTER_VERSION_MINOR  15

>  #define LIBAVFILTER_VERSION_MICRO 101

Micro should be reset to 100 after minor bump.

>  
>  #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \

Regards,

-- 
  Nicolas George
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20151030/c55c3bf0/attachment.sig>


More information about the ffmpeg-devel mailing list