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

Paul B Mahol onemda at gmail.com
Wed Jul 10 13:44:04 CEST 2013


On 7/10/13, Stefano Sabatini <stefasab at gmail.com> wrote:
> On date Tuesday 2013-07-09 23:13:59 +0000, Paul B Mahol encoded:
>> Signed-off-by: Paul B Mahol <onemda at gmail.com>
>> ---
>>  doc/filters.texi         |  60 ++++++++
>>  libavfilter/Makefile     |   1 +
>>  libavfilter/af_aecho.c   | 357
>> +++++++++++++++++++++++++++++++++++++++++++++++
>>  libavfilter/allfilters.c |   1 +
>>  4 files changed, 419 insertions(+)
>>  create mode 100644 libavfilter/af_aecho.c
>>
>> diff --git a/doc/filters.texi b/doc/filters.texi
>> index 33436ad..92f8612 100644
>> --- a/doc/filters.texi
>> +++ b/doc/filters.texi
>> @@ -347,6 +347,66 @@ aconvert=u8:auto
>>  @end example
>>  @end itemize
>>
>> + at section aecho
>> +
>> +Apply echoing to the input audio.
>> +
>> +Echoes are reflected sound and can occur naturally amongst mountains
>> +(and sometimes large buildings) when talking or shouting; digital echo
>> +effects emulate this behaviour and are often used to help fill out the
>> +sound of a single instrument or vocal. The time difference between the
>> +original signal and the reflection is the @code{delay}, and the
>> +loudness of the reflected signal is the @code{decay}.
>> +Multiple echoes can have different delays and decays.
>> +
>> +A description of the accepted parameters follows.
>> +
>> + at table @option
>
>> + at item in_gain
>> +Set input gain of reflected signal. Default is @code{0.6}.
>> +
>> + at item out_gain
>> +Set output gain of reflected signal. Default is @code{0.3}.
>
> This could be more explicit.
>
>> +
>> + at item delays
>> +Set list of time intervals in milliseconds between original signal and
>> reflections
>> +separated by '|'. Allowed range for each @code{delay} is @code{(0 -
>> 90000.0]}.
>> +Default is @code{1000}.
>> +
>> + at item decays
>> +Set list of loudnesses of reflected signals separated by '|'.
>> +Allowed range for each @code{decay} is @code{(0 - 1.0]}.
>> +Default is @code{0.5}.
>> + at end table
>> +
>> + at subsection Examples
>> +
>> + at itemize
>> + at item
>> +Make it sound as if there are twice as many instruments as are actually
>> playing:
>> + at example
>> +aecho=0.8:0.88:60:0.4
>> + at end example
>> +
>> + at item
>> +If delay is very short, then it sound like a (metallic) robot playing
>> music:
>> + at example
>> +aecho=0.8:0.88:6:0.4
>> + at end example
>> +
>> + at item
>> +A longer delay will sound like an open air concert in the mountains:
>> + at example
>> +aecho=0.8:0.9:1000:0.3
>> + at end example
>> +
>> + at item
>> +Same as above but with one more mountain:
>> + at example
>> +aecho=0.8:0.9:1000|1800:0.3|0.25
>> + at end example
>> + at end itemize
>> +
>>  @section afade
>>
>>  Apply fade-in/out effect to input audio.
>> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
>> index cf76ee1..306b24c 100644
>> --- a/libavfilter/Makefile
>> +++ b/libavfilter/Makefile
>> @@ -52,6 +52,7 @@ OBJS-$(CONFIG_AVFORMAT)                      +=
>> lavfutils.o
>>  OBJS-$(CONFIG_SWSCALE)                       += lswsutils.o
>>
>>  OBJS-$(CONFIG_ACONVERT_FILTER)               += af_aconvert.o
>> +OBJS-$(CONFIG_AECHO_FILTER)                  += af_aecho.o
>>  OBJS-$(CONFIG_AFADE_FILTER)                  += af_afade.o
>>  OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
>>  OBJS-$(CONFIG_AINTERLEAVE_FILTER)            += f_interleave.o
>> diff --git a/libavfilter/af_aecho.c b/libavfilter/af_aecho.c
>> new file mode 100644
>> index 0000000..23158f7
>> --- /dev/null
>> +++ b/libavfilter/af_aecho.c
>> @@ -0,0 +1,357 @@
>> +/*
>> + * Copyright (c) 2013 Paul B Mahol
>> + *
>> + * 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 "libavutil/avstring.h"
>> +#include "libavutil/opt.h"
>> +#include "libavutil/samplefmt.h"
>> +#include "libavutil/avassert.h"
>> +#include "avfilter.h"
>> +#include "audio.h"
>> +#include "internal.h"
>> +
>> +typedef struct AudioEchoContext {
>> +    const AVClass *class;
>> +    float in_gain, out_gain;
>> +    char *delays, *decays;
>> +    float *delay, *decay;
>> +    int nb_echoes;
>> +    int delay_index;
>> +    uint8_t **delayptrs;
>> +    int max_samples, fade_out;
>> +    int *samples;
>> +    int64_t next_pts;
>> +
>> +    void (*echo_samples)(struct AudioEchoContext *ctx, uint8_t
>> **delayptrs,
>> +                         uint8_t * const *src, uint8_t **dst,
>> +                         int nb_samples, int channels);
>> +} AudioEchoContext;
>> +
>> +#define OFFSET(x) offsetof(AudioEchoContext, x)
>> +#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
>> +
>> +static const AVOption aecho_options[] = {
>> +    { "in_gain",  "set signal input gain",  OFFSET(in_gain),
>> AV_OPT_TYPE_FLOAT,  {.dbl=0.6}, 0, 1, A },
>> +    { "out_gain", "set signal output gain", OFFSET(out_gain),
>> AV_OPT_TYPE_FLOAT,  {.dbl=0.3}, 0, 1, A },
>> +    { "delays",   "set list of signal delays", OFFSET(delays),
>> AV_OPT_TYPE_STRING, {.str="1000"}, 0, 0, A },
>> +    { "decays",   "set list of signal decays", OFFSET(decays),
>> AV_OPT_TYPE_STRING, {.str="0.5"}, 0, 0, A },
>> +    { NULL },
>> +};
>> +
>> +AVFILTER_DEFINE_CLASS(aecho);
>> +
>> +static void count_items(char *item_str, int *nb_items)
>> +{
>> +    char *p;
>> +
>> +    *nb_items = 1;
>> +    for (p = item_str; *p; p++) {
>> +        if (*p == '|')
>> +            (*nb_items)++;
>> +    }
>> +
>> +}
>> +
>> +static void fill_items(char *item_str, int *nb_items, float *items)
>> +{
>> +    char *p, *saveptr = NULL;
>> +    int i, new_nb_items = 0;
>> +
>> +    p = item_str;
>> +    for (i = 0; i < *nb_items; i++) {
>> +        char *tstr = av_strtok(p, "|", &saveptr);
>> +        p = NULL;
>
>> +        new_nb_items += sscanf(tstr, "%f", &items[i]) == 1;
>
> av_strtod()?

Really not.

>
>> +    }
>> +
>> +    *nb_items = new_nb_items;
>> +}
>> +
>> +static av_cold void uninit(AVFilterContext *ctx)
>> +{
>> +    AudioEchoContext *s = ctx->priv;
>> +
>> +    av_freep(&s->delay);
>> +    av_freep(&s->decay);
>> +    av_freep(&s->samples);
>> +
>> +    if (s->delayptrs)
>> +        av_freep(s->delayptrs[0]);
>> +    av_freep(&s->delayptrs);
>> +}
>> +
>> +static av_cold int init(AVFilterContext *ctx)
>> +{
>> +    AudioEchoContext *s = ctx->priv;
>> +    int nb_delays, nb_decays, i;
>> +
>> +    if (!s->delays || !s->decays) {
>
>> +        av_log(ctx, AV_LOG_ERROR, "missing delays and/or decays\n");
>
> Nit: Missing
>
> First char is usually Capitalized, same below.

Done.
>
>> +        return AVERROR(EINVAL);
>> +    }
>> +
>> +    count_items(s->delays, &nb_delays);
>> +    count_items(s->decays, &nb_decays);
>> +
>> +    s->delay = av_realloc_f(s->delay, nb_delays, sizeof(*s->delay));
>> +    s->decay = av_realloc_f(s->decay, nb_decays, sizeof(*s->decay));
>> +    if (!s->delay || !s->decay)
>> +        return AVERROR(ENOMEM);
>> +
>> +    fill_items(s->delays, &nb_delays, s->delay);
>> +    fill_items(s->decays, &nb_decays, s->decay);
>> +
>> +    if (nb_delays != nb_decays) {
>> +        av_log(ctx, AV_LOG_ERROR, "number of delays differs from number
>> of decays\n");
>
> Number ... of delays %d differs from number of decays %d

Done.
>
> Alternatively you can adopt a syntax which eliminates the problem, for
> example:
> 12%34|56%78|...

Ugly.

>
>> +        return AVERROR(EINVAL);
>> +    }
>> +
>> +    s->nb_echoes = nb_delays;
>
>> +    if (!s->nb_echoes) {
>> +        av_log(ctx, AV_LOG_ERROR, "at least one decay & delay must be
>> set\n");
>> +        return AVERROR(EINVAL);
>> +    }
>
> Shouldn't work as a null filter in this case? You could just return a
> warning (unless there are compelling reasons for not doing it).

If you want null filter use null filter.

>
> [...]
>> +AVFilter avfilter_af_aecho = {
>> +    .name          = "aecho",
>> +    .description   = NULL_IF_CONFIG_SMALL("Add echoing to the audio."),
>> +    .query_formats = query_formats,
>> +    .priv_size     = sizeof(AudioEchoContext),
>> +    .priv_class    = &aecho_class,
>> +    .init          = init,
>> +    .uninit        = uninit,
>> +    .inputs        = aecho_inputs,
>> +    .outputs       = aecho_outputs,
>
> No timeline support?

Not possible, you would get many funny results I don't consider feature.

Applied.

> --
> FFmpeg = Fabulous and Fast Maxi Patchable Elitist Gem
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>


More information about the ffmpeg-devel mailing list