[FFmpeg-devel] [PATCH] lavfi: add aecho filter
Michael Niedermayer
michaelni at gmx.at
Tue Jul 9 20:10:18 CEST 2013
On Tue, Jul 09, 2013 at 02:53:14PM +0000, Paul B Mahol wrote:
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
> doc/filters.texi | 29 +++++
> libavfilter/Makefile | 1 +
> libavfilter/af_aecho.c | 297 +++++++++++++++++++++++++++++++++++++++++++++++
> libavfilter/allfilters.c | 1 +
> 4 files changed, 328 insertions(+)
> create mode 100644 libavfilter/af_aecho.c
>
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 234ff2e..9472675 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -347,6 +347,35 @@ 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 volume of reflected signal.
> +
> + at item out_gain
> +Set output volume of reflected signal.
> +
> + at item delay1..7
> +Set time interval in miliseconds between original signal and reflection.
> +
> + at item decay1..7
> +Loudness of reflected signal.
> +
> + at end table
> +
> @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..9d4010d
> --- /dev/null
> +++ b/libavfilter/af_aecho.c
> @@ -0,0 +1,297 @@
> +/*
> + * 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"
> +
> +#define MAX_ECHOS 7
> +
> +typedef struct AudioEchoContext {
> + const AVClass *class;
> + float in_gain, out_gain;
> + float delay[MAX_ECHOS], decay[MAX_ECHOS];
> + int nb_echos;
> + int delay_index;
> + uint8_t **delayptrs;
> + int max_samples, fade_out;
> + int samples[MAX_ECHOS];
> + 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", "", OFFSET(in_gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, A },
> + { "out_gain", "", OFFSET(out_gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, A },
> + { "delay1", "", OFFSET(delay[0]), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 90000, A },
> + { "decay1", "", OFFSET(decay[0]), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, A },
> + { "delay2", "", OFFSET(delay[1]), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 90000, A },
> + { "decay2", "", OFFSET(decay[1]), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, A },
> + { "delay3", "", OFFSET(delay[2]), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 90000, A },
> + { "decay3", "", OFFSET(decay[2]), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, A },
> + { "delay4", "", OFFSET(delay[3]), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 90000, A },
> + { "decay4", "", OFFSET(decay[3]), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, A },
> + { "delay5", "", OFFSET(delay[4]), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 90000, A },
> + { "decay5", "", OFFSET(decay[4]), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, A },
> + { "delay6", "", OFFSET(delay[5]), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 90000, A },
> + { "decay6", "", OFFSET(decay[5]), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, A },
> + { "delay7", "", OFFSET(delay[6]), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 90000, A },
> + { "decay7", "", OFFSET(decay[6]), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, A },
> + { NULL },
> +};
> +
> +AVFILTER_DEFINE_CLASS(aecho);
> +
> +static av_cold int init(AVFilterContext *ctx)
> +{
> + AudioEchoContext *s = ctx->priv;
> + int i;
> +
> + for (i = 0; i < MAX_ECHOS; i++) {
> + if (!s->delay[i] || !s->decay[i])
> + break;
> + }
> + s->nb_echos = i;
> + if (!s->nb_echos) {
> + av_log(ctx, AV_LOG_ERROR, "at least one decay & delay must be set");
> + return AVERROR(EINVAL);
> + }
> +
> + s->next_pts = AV_NOPTS_VALUE;
> +
> + return 0;
> +}
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> + AVFilterChannelLayouts *layouts;
> + AVFilterFormats *formats;
> + static const enum AVSampleFormat sample_fmts[] = {
> + AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
> + AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
> + AV_SAMPLE_FMT_NONE
> + };
> +
> + layouts = ff_all_channel_layouts();
> + if (!layouts)
> + return AVERROR(ENOMEM);
> + ff_set_common_channel_layouts(ctx, layouts);
> +
> + formats = ff_make_format_list(sample_fmts);
> + if (!formats)
> + return AVERROR(ENOMEM);
> + ff_set_common_formats(ctx, formats);
> +
> + formats = ff_all_samplerates();
> + if (!formats)
> + return AVERROR(ENOMEM);
> + ff_set_common_samplerates(ctx, formats);
> +
> + return 0;
> +}
> +
> +static av_cold void uninit(AVFilterContext *ctx)
> +{
> + AudioEchoContext *s = ctx->priv;
> +
> + if (s->delayptrs)
> + av_freep(s->delayptrs[0]);
> + av_freep(&s->delayptrs);
> +}
> +
> +#define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
> +
> +#define ECHO(name, type, min, max) \
> +static void echo_samples_## name ##p(AudioEchoContext *ctx, \
> + uint8_t **delayptrs, \
> + uint8_t * const *src, uint8_t **dst, \
> + int nb_samples, int channels) \
> +{ \
> + const double out_gain = ctx->out_gain; \
> + const double in_gain = ctx->in_gain; \
> + const int nb_echos = ctx->nb_echos; \
> + const int max_samples = ctx->max_samples; \
> + int i, j, chan, index; \
> + \
> + for (chan = 0; chan < channels; chan++) { \
> + const type *s = (type *)src[chan]; \
> + type *d = (type *)dst[chan]; \
> + type *dbuf = (type *)delayptrs[chan]; \
> + \
> + index = ctx->delay_index; \
> + for (i = 0; i < nb_samples; i++, s++, d++) { \
> + double out, in; \
> + \
> + in = *s; \
> + out = in * in_gain; \
> + for (j = 0; j < nb_echos; j++) { \
> + int ix = index + max_samples - ctx->samples[j]; \
> + ix = MOD(ix, max_samples); \
> + out += dbuf[ix] * ctx->decay[j]; \
> + } \
for large number of echos implementing this with an FFT would be
faster
also the inner loop can be optimized quite a bit i suspect
these can of course be done seperately from this patch
end of comments from me / should be ok if noone else has comments
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
Frequently ignored answer#1 FFmpeg bugs should be sent to our bugtracker. User
questions about the command line tools should be sent to the ffmpeg-user ML.
And questions about how to use libav* should be sent to the libav-user ML.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20130709/f12a7165/attachment.asc>
More information about the ffmpeg-devel
mailing list