GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavfilter/af_aecho.c Lines: 118 155 76.1 %
Date: 2020-09-25 14:59:26 Branches: 61 127 48.0 %

Line Branch Exec Source
1
/*
2
 * Copyright (c) 2013 Paul B Mahol
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#include "libavutil/avassert.h"
22
#include "libavutil/avstring.h"
23
#include "libavutil/opt.h"
24
#include "libavutil/samplefmt.h"
25
#include "avfilter.h"
26
#include "audio.h"
27
#include "filters.h"
28
#include "internal.h"
29
30
typedef struct AudioEchoContext {
31
    const AVClass *class;
32
    float in_gain, out_gain;
33
    char *delays, *decays;
34
    float *delay, *decay;
35
    int nb_echoes;
36
    int delay_index;
37
    uint8_t **delayptrs;
38
    int max_samples, fade_out;
39
    int *samples;
40
    int eof;
41
    int64_t next_pts;
42
43
    void (*echo_samples)(struct AudioEchoContext *ctx, uint8_t **delayptrs,
44
                         uint8_t * const *src, uint8_t **dst,
45
                         int nb_samples, int channels);
46
} AudioEchoContext;
47
48
#define OFFSET(x) offsetof(AudioEchoContext, x)
49
#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50
51
static const AVOption aecho_options[] = {
52
    { "in_gain",  "set signal input gain",  OFFSET(in_gain),  AV_OPT_TYPE_FLOAT,  {.dbl=0.6}, 0, 1, A },
53
    { "out_gain", "set signal output gain", OFFSET(out_gain), AV_OPT_TYPE_FLOAT,  {.dbl=0.3}, 0, 1, A },
54
    { "delays",   "set list of signal delays", OFFSET(delays), AV_OPT_TYPE_STRING, {.str="1000"}, 0, 0, A },
55
    { "decays",   "set list of signal decays", OFFSET(decays), AV_OPT_TYPE_STRING, {.str="0.5"}, 0, 0, A },
56
    { NULL }
57
};
58
59
AVFILTER_DEFINE_CLASS(aecho);
60
61
2
static void count_items(char *item_str, int *nb_items)
62
{
63
    char *p;
64
65
2
    *nb_items = 1;
66
7
    for (p = item_str; *p; p++) {
67
5
        if (*p == '|')
68
            (*nb_items)++;
69
    }
70
71
2
}
72
73
2
static void fill_items(char *item_str, int *nb_items, float *items)
74
{
75
2
    char *p, *saveptr = NULL;
76
2
    int i, new_nb_items = 0;
77
78
2
    p = item_str;
79
4
    for (i = 0; i < *nb_items; i++) {
80
2
        char *tstr = av_strtok(p, "|", &saveptr);
81
2
        p = NULL;
82
2
        if (tstr)
83
2
            new_nb_items += av_sscanf(tstr, "%f", &items[new_nb_items]) == 1;
84
    }
85
86
2
    *nb_items = new_nb_items;
87
2
}
88
89
1
static av_cold void uninit(AVFilterContext *ctx)
90
{
91
1
    AudioEchoContext *s = ctx->priv;
92
93
1
    av_freep(&s->delay);
94
1
    av_freep(&s->decay);
95
1
    av_freep(&s->samples);
96
97
1
    if (s->delayptrs)
98
1
        av_freep(&s->delayptrs[0]);
99
1
    av_freep(&s->delayptrs);
100
1
}
101
102
1
static av_cold int init(AVFilterContext *ctx)
103
{
104
1
    AudioEchoContext *s = ctx->priv;
105
    int nb_delays, nb_decays, i;
106
107

1
    if (!s->delays || !s->decays) {
108
        av_log(ctx, AV_LOG_ERROR, "Missing delays and/or decays.\n");
109
        return AVERROR(EINVAL);
110
    }
111
112
1
    count_items(s->delays, &nb_delays);
113
1
    count_items(s->decays, &nb_decays);
114
115
1
    s->delay = av_realloc_f(s->delay, nb_delays, sizeof(*s->delay));
116
1
    s->decay = av_realloc_f(s->decay, nb_decays, sizeof(*s->decay));
117

1
    if (!s->delay || !s->decay)
118
        return AVERROR(ENOMEM);
119
120
1
    fill_items(s->delays, &nb_delays, s->delay);
121
1
    fill_items(s->decays, &nb_decays, s->decay);
122
123
1
    if (nb_delays != nb_decays) {
124
        av_log(ctx, AV_LOG_ERROR, "Number of delays %d differs from number of decays %d.\n", nb_delays, nb_decays);
125
        return AVERROR(EINVAL);
126
    }
127
128
1
    s->nb_echoes = nb_delays;
129
1
    if (!s->nb_echoes) {
130
        av_log(ctx, AV_LOG_ERROR, "At least one decay & delay must be set.\n");
131
        return AVERROR(EINVAL);
132
    }
133
134
1
    s->samples = av_realloc_f(s->samples, nb_delays, sizeof(*s->samples));
135
1
    if (!s->samples)
136
        return AVERROR(ENOMEM);
137
138
2
    for (i = 0; i < nb_delays; i++) {
139

1
        if (s->delay[i] <= 0 || s->delay[i] > 90000) {
140
            av_log(ctx, AV_LOG_ERROR, "delay[%d]: %f is out of allowed range: (0, 90000]\n", i, s->delay[i]);
141
            return AVERROR(EINVAL);
142
        }
143

1
        if (s->decay[i] <= 0 || s->decay[i] > 1) {
144
            av_log(ctx, AV_LOG_ERROR, "decay[%d]: %f is out of allowed range: (0, 1]\n", i, s->decay[i]);
145
            return AVERROR(EINVAL);
146
        }
147
    }
148
149
1
    s->next_pts = AV_NOPTS_VALUE;
150
151
1
    av_log(ctx, AV_LOG_DEBUG, "nb_echoes:%d\n", s->nb_echoes);
152
1
    return 0;
153
}
154
155
1
static int query_formats(AVFilterContext *ctx)
156
{
157
    AVFilterChannelLayouts *layouts;
158
    AVFilterFormats *formats;
159
    static const enum AVSampleFormat sample_fmts[] = {
160
        AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
161
        AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
162
        AV_SAMPLE_FMT_NONE
163
    };
164
    int ret;
165
166
1
    layouts = ff_all_channel_counts();
167
1
    if (!layouts)
168
        return AVERROR(ENOMEM);
169
1
    ret = ff_set_common_channel_layouts(ctx, layouts);
170
1
    if (ret < 0)
171
        return ret;
172
173
1
    formats = ff_make_format_list(sample_fmts);
174
1
    if (!formats)
175
        return AVERROR(ENOMEM);
176
1
    ret = ff_set_common_formats(ctx, formats);
177
1
    if (ret < 0)
178
        return ret;
179
180
1
    formats = ff_all_samplerates();
181
1
    if (!formats)
182
        return AVERROR(ENOMEM);
183
1
    return ff_set_common_samplerates(ctx, formats);
184
}
185
186
#define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
187
188
#define ECHO(name, type, min, max)                                          \
189
static void echo_samples_## name ##p(AudioEchoContext *ctx,                 \
190
                                     uint8_t **delayptrs,                   \
191
                                     uint8_t * const *src, uint8_t **dst,   \
192
                                     int nb_samples, int channels)          \
193
{                                                                           \
194
    const double out_gain = ctx->out_gain;                                  \
195
    const double in_gain = ctx->in_gain;                                    \
196
    const int nb_echoes = ctx->nb_echoes;                                   \
197
    const int max_samples = ctx->max_samples;                               \
198
    int i, j, chan, av_uninit(index);                                       \
199
                                                                            \
200
    av_assert1(channels > 0); /* would corrupt delay_index */               \
201
                                                                            \
202
    for (chan = 0; chan < channels; chan++) {                               \
203
        const type *s = (type *)src[chan];                                  \
204
        type *d = (type *)dst[chan];                                        \
205
        type *dbuf = (type *)delayptrs[chan];                               \
206
                                                                            \
207
        index = ctx->delay_index;                                           \
208
        for (i = 0; i < nb_samples; i++, s++, d++) {                        \
209
            double out, in;                                                 \
210
                                                                            \
211
            in = *s;                                                        \
212
            out = in * in_gain;                                             \
213
            for (j = 0; j < nb_echoes; j++) {                               \
214
                int ix = index + max_samples - ctx->samples[j];             \
215
                ix = MOD(ix, max_samples);                                  \
216
                out += dbuf[ix] * ctx->decay[j];                            \
217
            }                                                               \
218
            out *= out_gain;                                                \
219
                                                                            \
220
            *d = av_clipd(out, min, max);                                   \
221
            dbuf[index] = in;                                               \
222
                                                                            \
223
            index = MOD(index + 1, max_samples);                            \
224
        }                                                                   \
225
    }                                                                       \
226
    ctx->delay_index = index;                                               \
227
}
228
229
ECHO(dbl, double,  -1.0,      1.0      )
230
ECHO(flt, float,   -1.0,      1.0      )
231


1064824
ECHO(s16, int16_t, INT16_MIN, INT16_MAX)
232
ECHO(s32, int32_t, INT32_MIN, INT32_MAX)
233
234
1
static int config_output(AVFilterLink *outlink)
235
{
236
1
    AVFilterContext *ctx = outlink->src;
237
1
    AudioEchoContext *s = ctx->priv;
238
1
    float volume = 1.0;
239
    int i;
240
241
2
    for (i = 0; i < s->nb_echoes; i++) {
242
1
        s->samples[i] = s->delay[i] * outlink->sample_rate / 1000.0;
243
1
        s->max_samples = FFMAX(s->max_samples, s->samples[i]);
244
1
        volume += s->decay[i];
245
    }
246
247
1
    if (s->max_samples <= 0) {
248
        av_log(ctx, AV_LOG_ERROR, "Nothing to echo - missing delay samples.\n");
249
        return AVERROR(EINVAL);
250
    }
251
1
    s->fade_out = s->max_samples;
252
253
1
    if (volume * s->in_gain * s->out_gain > 1.0)
254
        av_log(ctx, AV_LOG_WARNING,
255
               "out_gain %f can cause saturation of output\n", s->out_gain);
256
257

1
    switch (outlink->format) {
258
    case AV_SAMPLE_FMT_DBLP: s->echo_samples = echo_samples_dblp; break;
259
    case AV_SAMPLE_FMT_FLTP: s->echo_samples = echo_samples_fltp; break;
260
1
    case AV_SAMPLE_FMT_S16P: s->echo_samples = echo_samples_s16p; break;
261
    case AV_SAMPLE_FMT_S32P: s->echo_samples = echo_samples_s32p; break;
262
    }
263
264
265
1
    if (s->delayptrs)
266
        av_freep(&s->delayptrs[0]);
267
1
    av_freep(&s->delayptrs);
268
269
1
    return av_samples_alloc_array_and_samples(&s->delayptrs, NULL,
270
                                              outlink->channels,
271
                                              s->max_samples,
272
1
                                              outlink->format, 0);
273
}
274
275
259
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
276
{
277
259
    AVFilterContext *ctx = inlink->dst;
278
259
    AudioEchoContext *s = ctx->priv;
279
    AVFrame *out_frame;
280
281
259
    if (av_frame_is_writable(frame)) {
282
259
        out_frame = frame;
283
    } else {
284
        out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
285
        if (!out_frame) {
286
            av_frame_free(&frame);
287
            return AVERROR(ENOMEM);
288
        }
289
        av_frame_copy_props(out_frame, frame);
290
    }
291
292
259
    s->echo_samples(s, s->delayptrs, frame->extended_data, out_frame->extended_data,
293
259
                    frame->nb_samples, inlink->channels);
294
295
259
    s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
296
297
259
    if (frame != out_frame)
298
        av_frame_free(&frame);
299
300
259
    return ff_filter_frame(ctx->outputs[0], out_frame);
301
}
302
303
1
static int request_frame(AVFilterLink *outlink)
304
{
305
1
    AVFilterContext *ctx = outlink->src;
306
1
    AudioEchoContext *s = ctx->priv;
307
1
    int nb_samples = FFMIN(s->fade_out, 2048);
308
1
    AVFrame *frame = ff_get_audio_buffer(outlink, nb_samples);
309
310
1
    if (!frame)
311
        return AVERROR(ENOMEM);
312
1
    s->fade_out -= nb_samples;
313
314
1
    av_samples_set_silence(frame->extended_data, 0,
315
                           frame->nb_samples,
316
                           outlink->channels,
317
1
                           frame->format);
318
319
1
    s->echo_samples(s, s->delayptrs, frame->extended_data, frame->extended_data,
320
                    frame->nb_samples, outlink->channels);
321
322
1
    frame->pts = s->next_pts;
323
1
    if (s->next_pts != AV_NOPTS_VALUE)
324
1
        s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
325
326
1
    return ff_filter_frame(outlink, frame);
327
}
328
329
521
static int activate(AVFilterContext *ctx)
330
{
331
521
    AVFilterLink *inlink = ctx->inputs[0];
332
521
    AVFilterLink *outlink = ctx->outputs[0];
333
521
    AudioEchoContext *s = ctx->priv;
334
    AVFrame *in;
335
    int ret, status;
336
    int64_t pts;
337
338
521
    FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
339
340
520
    ret = ff_inlink_consume_frame(inlink, &in);
341
520
    if (ret < 0)
342
        return ret;
343
520
    if (ret > 0)
344
259
        return filter_frame(inlink, in);
345
346

261
    if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
347
1
        if (status == AVERROR_EOF)
348
1
            s->eof = 1;
349
    }
350
351

261
    if (s->eof && s->fade_out <= 0) {
352
1
        ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
353
1
        return 0;
354
    }
355
356
260
    if (!s->eof)
357
259
        FF_FILTER_FORWARD_WANTED(outlink, inlink);
358
359
1
    return request_frame(outlink);
360
}
361
362
static const AVFilterPad aecho_inputs[] = {
363
    {
364
        .name         = "default",
365
        .type         = AVMEDIA_TYPE_AUDIO,
366
    },
367
    { NULL }
368
};
369
370
static const AVFilterPad aecho_outputs[] = {
371
    {
372
        .name          = "default",
373
        .config_props  = config_output,
374
        .type          = AVMEDIA_TYPE_AUDIO,
375
    },
376
    { NULL }
377
};
378
379
AVFilter ff_af_aecho = {
380
    .name          = "aecho",
381
    .description   = NULL_IF_CONFIG_SMALL("Add echoing to the audio."),
382
    .query_formats = query_formats,
383
    .priv_size     = sizeof(AudioEchoContext),
384
    .priv_class    = &aecho_class,
385
    .init          = init,
386
    .activate      = activate,
387
    .uninit        = uninit,
388
    .inputs        = aecho_inputs,
389
    .outputs       = aecho_outputs,
390
};