Browse code

lavfi: add aecho filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2013/07/08 22:44:35
Showing 6 changed files
... ...
@@ -3,6 +3,8 @@ releases are sorted from youngest to oldest.
3 3
 
4 4
 version <next>
5 5
 
6
+- aecho filter
7
+
6 8
 
7 9
 version 2.0:
8 10
 
... ...
@@ -347,6 +347,66 @@ aconvert=u8:auto
347 347
 @end example
348 348
 @end itemize
349 349
 
350
+@section aecho
351
+
352
+Apply echoing to the input audio.
353
+
354
+Echoes are reflected sound and can occur naturally amongst mountains
355
+(and sometimes large buildings) when talking or shouting; digital echo
356
+effects emulate this behaviour and are often used to help fill out the
357
+sound of a single instrument or vocal. The time difference between the
358
+original signal and the reflection is the @code{delay}, and the
359
+loudness of the reflected signal is the @code{decay}.
360
+Multiple echoes can have different delays and decays.
361
+
362
+A description of the accepted parameters follows.
363
+
364
+@table @option
365
+@item in_gain
366
+Set input gain of reflected signal. Default is @code{0.6}.
367
+
368
+@item out_gain
369
+Set output gain of reflected signal. Default is @code{0.3}.
370
+
371
+@item delays
372
+Set list of time intervals in milliseconds between original signal and reflections
373
+separated by '|'. Allowed range for each @code{delay} is @code{(0 - 90000.0]}.
374
+Default is @code{1000}.
375
+
376
+@item decays
377
+Set list of loudnesses of reflected signals separated by '|'.
378
+Allowed range for each @code{decay} is @code{(0 - 1.0]}.
379
+Default is @code{0.5}.
380
+@end table
381
+
382
+@subsection Examples
383
+
384
+@itemize
385
+@item
386
+Make it sound as if there are twice as many instruments as are actually playing:
387
+@example
388
+aecho=0.8:0.88:60:0.4
389
+@end example
390
+
391
+@item
392
+If delay is very short, then it sound like a (metallic) robot playing music:
393
+@example
394
+aecho=0.8:0.88:6:0.4
395
+@end example
396
+
397
+@item
398
+A longer delay will sound like an open air concert in the mountains:
399
+@example
400
+aecho=0.8:0.9:1000:0.3
401
+@end example
402
+
403
+@item
404
+Same as above but with one more mountain:
405
+@example
406
+aecho=0.8:0.9:1000|1800:0.3|0.25
407
+@end example
408
+@end itemize
409
+
350 410
 @section afade
351 411
 
352 412
 Apply fade-in/out effect to input audio.
... ...
@@ -52,6 +52,7 @@ OBJS-$(CONFIG_AVFORMAT)                      += lavfutils.o
52 52
 OBJS-$(CONFIG_SWSCALE)                       += lswsutils.o
53 53
 
54 54
 OBJS-$(CONFIG_ACONVERT_FILTER)               += af_aconvert.o
55
+OBJS-$(CONFIG_AECHO_FILTER)                  += af_aecho.o
55 56
 OBJS-$(CONFIG_AFADE_FILTER)                  += af_afade.o
56 57
 OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
57 58
 OBJS-$(CONFIG_AINTERLEAVE_FILTER)            += f_interleave.o
58 59
new file mode 100644
... ...
@@ -0,0 +1,357 @@
0
+/*
1
+ * Copyright (c) 2013 Paul B Mahol
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ *
19
+ */
20
+
21
+#include "libavutil/avstring.h"
22
+#include "libavutil/opt.h"
23
+#include "libavutil/samplefmt.h"
24
+#include "libavutil/avassert.h"
25
+#include "avfilter.h"
26
+#include "audio.h"
27
+#include "internal.h"
28
+
29
+typedef struct AudioEchoContext {
30
+    const AVClass *class;
31
+    float in_gain, out_gain;
32
+    char *delays, *decays;
33
+    float *delay, *decay;
34
+    int nb_echoes;
35
+    int delay_index;
36
+    uint8_t **delayptrs;
37
+    int max_samples, fade_out;
38
+    int *samples;
39
+    int64_t next_pts;
40
+
41
+    void (*echo_samples)(struct AudioEchoContext *ctx, uint8_t **delayptrs,
42
+                         uint8_t * const *src, uint8_t **dst,
43
+                         int nb_samples, int channels);
44
+} AudioEchoContext;
45
+
46
+#define OFFSET(x) offsetof(AudioEchoContext, x)
47
+#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
48
+
49
+static const AVOption aecho_options[] = {
50
+    { "in_gain",  "set signal input gain",  OFFSET(in_gain),  AV_OPT_TYPE_FLOAT,  {.dbl=0.6}, 0, 1, A },
51
+    { "out_gain", "set signal output gain", OFFSET(out_gain), AV_OPT_TYPE_FLOAT,  {.dbl=0.3}, 0, 1, A },
52
+    { "delays",   "set list of signal delays", OFFSET(delays), AV_OPT_TYPE_STRING, {.str="1000"}, 0, 0, A },
53
+    { "decays",   "set list of signal decays", OFFSET(decays), AV_OPT_TYPE_STRING, {.str="0.5"}, 0, 0, A },
54
+    { NULL },
55
+};
56
+
57
+AVFILTER_DEFINE_CLASS(aecho);
58
+
59
+static void count_items(char *item_str, int *nb_items)
60
+{
61
+    char *p;
62
+
63
+    *nb_items = 1;
64
+    for (p = item_str; *p; p++) {
65
+        if (*p == '|')
66
+            (*nb_items)++;
67
+    }
68
+
69
+}
70
+
71
+static void fill_items(char *item_str, int *nb_items, float *items)
72
+{
73
+    char *p, *saveptr = NULL;
74
+    int i, new_nb_items = 0;
75
+
76
+    p = item_str;
77
+    for (i = 0; i < *nb_items; i++) {
78
+        char *tstr = av_strtok(p, "|", &saveptr);
79
+        p = NULL;
80
+        new_nb_items += sscanf(tstr, "%f", &items[i]) == 1;
81
+    }
82
+
83
+    *nb_items = new_nb_items;
84
+}
85
+
86
+static av_cold void uninit(AVFilterContext *ctx)
87
+{
88
+    AudioEchoContext *s = ctx->priv;
89
+
90
+    av_freep(&s->delay);
91
+    av_freep(&s->decay);
92
+    av_freep(&s->samples);
93
+
94
+    if (s->delayptrs)
95
+        av_freep(s->delayptrs[0]);
96
+    av_freep(&s->delayptrs);
97
+}
98
+
99
+static av_cold int init(AVFilterContext *ctx)
100
+{
101
+    AudioEchoContext *s = ctx->priv;
102
+    int nb_delays, nb_decays, i;
103
+
104
+    if (!s->delays || !s->decays) {
105
+        av_log(ctx, AV_LOG_ERROR, "Missing delays and/or decays.\n");
106
+        return AVERROR(EINVAL);
107
+    }
108
+
109
+    count_items(s->delays, &nb_delays);
110
+    count_items(s->decays, &nb_decays);
111
+
112
+    s->delay = av_realloc_f(s->delay, nb_delays, sizeof(*s->delay));
113
+    s->decay = av_realloc_f(s->decay, nb_decays, sizeof(*s->decay));
114
+    if (!s->delay || !s->decay)
115
+        return AVERROR(ENOMEM);
116
+
117
+    fill_items(s->delays, &nb_delays, s->delay);
118
+    fill_items(s->decays, &nb_decays, s->decay);
119
+
120
+    if (nb_delays != nb_decays) {
121
+        av_log(ctx, AV_LOG_ERROR, "Number of delays %d differs from number of decays %d.\n", nb_delays, nb_decays);
122
+        return AVERROR(EINVAL);
123
+    }
124
+
125
+    s->nb_echoes = nb_delays;
126
+    if (!s->nb_echoes) {
127
+        av_log(ctx, AV_LOG_ERROR, "At least one decay & delay must be set.\n");
128
+        return AVERROR(EINVAL);
129
+    }
130
+
131
+    s->samples = av_realloc_f(s->samples, nb_delays, sizeof(*s->samples));
132
+    if (!s->samples)
133
+        return AVERROR(ENOMEM);
134
+
135
+    for (i = 0; i < nb_delays; i++) {
136
+        if (s->delay[i] <= 0 || s->delay[i] > 90000) {
137
+            av_log(ctx, AV_LOG_ERROR, "delay[%d]: %f is out of allowed range: (0, 90000]\n", i, s->delay[i]);
138
+            return AVERROR(EINVAL);
139
+        }
140
+        if (s->decay[i] <= 0 || s->decay[i] > 1) {
141
+            av_log(ctx, AV_LOG_ERROR, "decay[%d]: %f is out of allowed range: (0, 1]\n", i, s->decay[i]);
142
+            return AVERROR(EINVAL);
143
+        }
144
+    }
145
+
146
+    s->next_pts = AV_NOPTS_VALUE;
147
+
148
+    av_log(ctx, AV_LOG_DEBUG, "nb_echoes:%d\n", s->nb_echoes);
149
+    return 0;
150
+}
151
+
152
+static int query_formats(AVFilterContext *ctx)
153
+{
154
+    AVFilterChannelLayouts *layouts;
155
+    AVFilterFormats *formats;
156
+    static const enum AVSampleFormat sample_fmts[] = {
157
+        AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
158
+        AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
159
+        AV_SAMPLE_FMT_NONE
160
+    };
161
+
162
+    layouts = ff_all_channel_layouts();
163
+    if (!layouts)
164
+        return AVERROR(ENOMEM);
165
+    ff_set_common_channel_layouts(ctx, layouts);
166
+
167
+    formats = ff_make_format_list(sample_fmts);
168
+    if (!formats)
169
+        return AVERROR(ENOMEM);
170
+    ff_set_common_formats(ctx, formats);
171
+
172
+    formats = ff_all_samplerates();
173
+    if (!formats)
174
+        return AVERROR(ENOMEM);
175
+    ff_set_common_samplerates(ctx, formats);
176
+
177
+    return 0;
178
+}
179
+
180
+#define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
181
+
182
+#define ECHO(name, type, min, max)                                          \
183
+static void echo_samples_## name ##p(AudioEchoContext *ctx,                 \
184
+                                     uint8_t **delayptrs,                   \
185
+                                     uint8_t * const *src, uint8_t **dst,   \
186
+                                     int nb_samples, int channels)          \
187
+{                                                                           \
188
+    const double out_gain = ctx->out_gain;                                  \
189
+    const double in_gain = ctx->in_gain;                                    \
190
+    const int nb_echoes = ctx->nb_echoes;                                   \
191
+    const int max_samples = ctx->max_samples;                               \
192
+    int i, j, chan, index;                                                  \
193
+                                                                            \
194
+    for (chan = 0; chan < channels; chan++) {                               \
195
+        const type *s = (type *)src[chan];                                  \
196
+        type *d = (type *)dst[chan];                                        \
197
+        type *dbuf = (type *)delayptrs[chan];                               \
198
+                                                                            \
199
+        index = ctx->delay_index;                                           \
200
+        for (i = 0; i < nb_samples; i++, s++, d++) {                        \
201
+            double out, in;                                                 \
202
+                                                                            \
203
+            in = *s;                                                        \
204
+            out = in * in_gain;                                             \
205
+            for (j = 0; j < nb_echoes; j++) {                               \
206
+                int ix = index + max_samples - ctx->samples[j];             \
207
+                ix = MOD(ix, max_samples);                                  \
208
+                out += dbuf[ix] * ctx->decay[j];                            \
209
+            }                                                               \
210
+            out *= out_gain;                                                \
211
+                                                                            \
212
+            *d = av_clipd(out, min, max);                                   \
213
+            dbuf[index] = in;                                               \
214
+                                                                            \
215
+            index = MOD(index + 1, max_samples);                            \
216
+        }                                                                   \
217
+    }                                                                       \
218
+    ctx->delay_index = index;                                               \
219
+}
220
+
221
+ECHO(dbl, double,  -1.0,      1.0      )
222
+ECHO(flt, float,   -1.0,      1.0      )
223
+ECHO(s16, int16_t, INT16_MIN, INT16_MAX)
224
+ECHO(s32, int32_t, INT32_MIN, INT32_MAX)
225
+
226
+static int config_output(AVFilterLink *outlink)
227
+{
228
+    AVFilterContext *ctx = outlink->src;
229
+    AudioEchoContext *s = ctx->priv;
230
+    float volume = 1.0;
231
+    int i;
232
+
233
+    for (i = 0; i < s->nb_echoes; i++) {
234
+        s->samples[i] = s->delay[i] * outlink->sample_rate / 1000.0;
235
+        s->max_samples = FFMAX(s->max_samples, s->samples[i]);
236
+        volume += s->decay[i];
237
+    }
238
+
239
+    if (s->max_samples <= 0) {
240
+        av_log(ctx, AV_LOG_ERROR, "Nothing to echo - missing delay samples.\n");
241
+        return AVERROR(EINVAL);
242
+    }
243
+    s->fade_out = s->max_samples;
244
+
245
+    if (volume * s->in_gain * s->out_gain > 1.0)
246
+        av_log(ctx, AV_LOG_WARNING,
247
+               "out_gain %f can cause saturation of output\n", s->out_gain);
248
+
249
+    switch (outlink->format) {
250
+    case AV_SAMPLE_FMT_DBLP: s->echo_samples = echo_samples_dblp; break;
251
+    case AV_SAMPLE_FMT_FLTP: s->echo_samples = echo_samples_fltp; break;
252
+    case AV_SAMPLE_FMT_S16P: s->echo_samples = echo_samples_s16p; break;
253
+    case AV_SAMPLE_FMT_S32P: s->echo_samples = echo_samples_s32p; break;
254
+    }
255
+
256
+
257
+    if (s->delayptrs)
258
+        av_freep(s->delayptrs[0]);
259
+    av_freep(&s->delayptrs);
260
+
261
+    return av_samples_alloc_array_and_samples(&s->delayptrs, NULL,
262
+                                              outlink->channels,
263
+                                              s->max_samples,
264
+                                              outlink->format, 0);
265
+}
266
+
267
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
268
+{
269
+    AVFilterContext *ctx = inlink->dst;
270
+    AudioEchoContext *s = ctx->priv;
271
+    AVFrame *out_frame;
272
+
273
+    if (av_frame_is_writable(frame)) {
274
+        out_frame = frame;
275
+    } else {
276
+        out_frame = ff_get_audio_buffer(inlink, frame->nb_samples);
277
+        if (!out_frame)
278
+            return AVERROR(ENOMEM);
279
+        av_frame_copy_props(out_frame, frame);
280
+    }
281
+
282
+    s->echo_samples(s, s->delayptrs, frame->data, out_frame->data,
283
+                    frame->nb_samples, inlink->channels);
284
+
285
+    if (frame != out_frame)
286
+        av_frame_free(&frame);
287
+
288
+    s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
289
+    return ff_filter_frame(ctx->outputs[0], out_frame);
290
+}
291
+
292
+static int request_frame(AVFilterLink *outlink)
293
+{
294
+    AVFilterContext *ctx = outlink->src;
295
+    AudioEchoContext *s = ctx->priv;
296
+    int ret;
297
+
298
+    ret = ff_request_frame(ctx->inputs[0]);
299
+
300
+    if (ret == AVERROR_EOF && !ctx->is_disabled && s->fade_out) {
301
+        int nb_samples = FFMIN(s->fade_out, 2048);
302
+        AVFrame *frame;
303
+
304
+        frame = ff_get_audio_buffer(outlink, nb_samples);
305
+        if (!frame)
306
+            return AVERROR(ENOMEM);
307
+        s->fade_out -= nb_samples;
308
+
309
+        av_samples_set_silence(frame->extended_data, 0,
310
+                               frame->nb_samples,
311
+                               outlink->channels,
312
+                               frame->format);
313
+
314
+        s->echo_samples(s, s->delayptrs, frame->data, frame->data,
315
+                        frame->nb_samples, outlink->channels);
316
+
317
+        frame->pts = s->next_pts;
318
+        if (s->next_pts != AV_NOPTS_VALUE)
319
+            s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
320
+
321
+        return ff_filter_frame(outlink, frame);
322
+    }
323
+
324
+    return ret;
325
+}
326
+
327
+static const AVFilterPad aecho_inputs[] = {
328
+    {
329
+        .name         = "default",
330
+        .type         = AVMEDIA_TYPE_AUDIO,
331
+        .filter_frame = filter_frame,
332
+    },
333
+    { NULL },
334
+};
335
+
336
+static const AVFilterPad aecho_outputs[] = {
337
+    {
338
+        .name          = "default",
339
+        .request_frame = request_frame,
340
+        .config_props  = config_output,
341
+        .type          = AVMEDIA_TYPE_AUDIO,
342
+    },
343
+    { NULL },
344
+};
345
+
346
+AVFilter avfilter_af_aecho = {
347
+    .name          = "aecho",
348
+    .description   = NULL_IF_CONFIG_SMALL("Add echoing to the audio."),
349
+    .query_formats = query_formats,
350
+    .priv_size     = sizeof(AudioEchoContext),
351
+    .priv_class    = &aecho_class,
352
+    .init          = init,
353
+    .uninit        = uninit,
354
+    .inputs        = aecho_inputs,
355
+    .outputs       = aecho_outputs,
356
+};
... ...
@@ -48,6 +48,7 @@ void avfilter_register_all(void)
48 48
 #if FF_API_ACONVERT_FILTER
49 49
     REGISTER_FILTER(ACONVERT,       aconvert,       af);
50 50
 #endif
51
+    REGISTER_FILTER(AECHO,          aecho,          af);
51 52
     REGISTER_FILTER(AFADE,          afade,          af);
52 53
     REGISTER_FILTER(AFORMAT,        aformat,        af);
53 54
     REGISTER_FILTER(AINTERLEAVE,    ainterleave,    af);
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR  3
33
-#define LIBAVFILTER_VERSION_MINOR  79
34
-#define LIBAVFILTER_VERSION_MICRO 101
33
+#define LIBAVFILTER_VERSION_MINOR  80
34
+#define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
                                                LIBAVFILTER_VERSION_MINOR, \