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/mem.h" | ||
24 | #include "libavutil/opt.h" | ||
25 | #include "libavutil/samplefmt.h" | ||
26 | #include "avfilter.h" | ||
27 | #include "audio.h" | ||
28 | #include "filters.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 | 4 | static void count_items(char *item_str, int *nb_items) | |
62 | { | ||
63 | char *p; | ||
64 | |||
65 | 4 | *nb_items = 1; | |
66 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 4 times.
|
14 | for (p = item_str; *p; p++) { |
67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (*p == '|') |
68 | ✗ | (*nb_items)++; | |
69 | } | ||
70 | |||
71 | 4 | } | |
72 | |||
73 | 4 | static void fill_items(char *item_str, int *nb_items, float *items) | |
74 | { | ||
75 | 4 | char *p, *saveptr = NULL; | |
76 | 4 | int i, new_nb_items = 0; | |
77 | |||
78 | 4 | p = item_str; | |
79 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | for (i = 0; i < *nb_items; i++) { |
80 | 4 | char *tstr = av_strtok(p, "|", &saveptr); | |
81 | 4 | p = NULL; | |
82 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (tstr) |
83 | 4 | new_nb_items += av_sscanf(tstr, "%f", &items[new_nb_items]) == 1; | |
84 | } | ||
85 | |||
86 | 4 | *nb_items = new_nb_items; | |
87 | 4 | } | |
88 | |||
89 | 2 | static av_cold void uninit(AVFilterContext *ctx) | |
90 | { | ||
91 | 2 | AudioEchoContext *s = ctx->priv; | |
92 | |||
93 | 2 | av_freep(&s->delay); | |
94 | 2 | av_freep(&s->decay); | |
95 | 2 | av_freep(&s->samples); | |
96 | |||
97 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (s->delayptrs) |
98 | 1 | av_freep(&s->delayptrs[0]); | |
99 | 2 | av_freep(&s->delayptrs); | |
100 | 2 | } | |
101 | |||
102 | 2 | static av_cold int init(AVFilterContext *ctx) | |
103 | { | ||
104 | 2 | AudioEchoContext *s = ctx->priv; | |
105 | int nb_delays, nb_decays, i; | ||
106 | |||
107 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | 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 | 2 | count_items(s->delays, &nb_delays); | |
113 | 2 | count_items(s->decays, &nb_decays); | |
114 | |||
115 | 2 | s->delay = av_realloc_f(s->delay, nb_delays, sizeof(*s->delay)); | |
116 | 2 | s->decay = av_realloc_f(s->decay, nb_decays, sizeof(*s->decay)); | |
117 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if (!s->delay || !s->decay) |
118 | ✗ | return AVERROR(ENOMEM); | |
119 | |||
120 | 2 | fill_items(s->delays, &nb_delays, s->delay); | |
121 | 2 | fill_items(s->decays, &nb_decays, s->decay); | |
122 | |||
123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | 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 | 2 | s->nb_echoes = nb_delays; | |
129 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | 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 | 2 | s->samples = av_realloc_f(s->samples, nb_delays, sizeof(*s->samples)); | |
135 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!s->samples) |
136 | ✗ | return AVERROR(ENOMEM); | |
137 | |||
138 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | for (i = 0; i < nb_delays; i++) { |
139 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | 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 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | 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 | 2 | s->next_pts = AV_NOPTS_VALUE; | |
150 | |||
151 | 2 | av_log(ctx, AV_LOG_DEBUG, "nb_echoes:%d\n", s->nb_echoes); | |
152 | 2 | return 0; | |
153 | } | ||
154 | |||
155 | #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a)) | ||
156 | |||
157 | #define ECHO(name, type, min, max) \ | ||
158 | static void echo_samples_## name ##p(AudioEchoContext *ctx, \ | ||
159 | uint8_t **delayptrs, \ | ||
160 | uint8_t * const *src, uint8_t **dst, \ | ||
161 | int nb_samples, int channels) \ | ||
162 | { \ | ||
163 | const double out_gain = ctx->out_gain; \ | ||
164 | const double in_gain = ctx->in_gain; \ | ||
165 | const int nb_echoes = ctx->nb_echoes; \ | ||
166 | const int max_samples = ctx->max_samples; \ | ||
167 | int i, j, chan, av_uninit(index); \ | ||
168 | \ | ||
169 | av_assert1(channels > 0); /* would corrupt delay_index */ \ | ||
170 | \ | ||
171 | for (chan = 0; chan < channels; chan++) { \ | ||
172 | const type *s = (type *)src[chan]; \ | ||
173 | type *d = (type *)dst[chan]; \ | ||
174 | type *dbuf = (type *)delayptrs[chan]; \ | ||
175 | \ | ||
176 | index = ctx->delay_index; \ | ||
177 | for (i = 0; i < nb_samples; i++, s++, d++) { \ | ||
178 | double out, in; \ | ||
179 | \ | ||
180 | in = *s; \ | ||
181 | out = in * in_gain; \ | ||
182 | for (j = 0; j < nb_echoes; j++) { \ | ||
183 | int ix = index + max_samples - ctx->samples[j]; \ | ||
184 | ix = MOD(ix, max_samples); \ | ||
185 | out += dbuf[ix] * ctx->decay[j]; \ | ||
186 | } \ | ||
187 | out *= out_gain; \ | ||
188 | \ | ||
189 | *d = av_clipd(out, min, max); \ | ||
190 | dbuf[index] = in; \ | ||
191 | \ | ||
192 | index = MOD(index + 1, max_samples); \ | ||
193 | } \ | ||
194 | } \ | ||
195 | ctx->delay_index = index; \ | ||
196 | } | ||
197 | |||
198 | ✗ | ECHO(dbl, double, -1.0, 1.0 ) | |
199 | ✗ | ECHO(flt, float, -1.0, 1.0 ) | |
200 |
9/10✗ Branch 0 not taken.
✓ Branch 1 taken 532022 times.
✓ Branch 2 taken 532022 times.
✓ Branch 3 taken 532022 times.
✓ Branch 4 taken 376 times.
✓ Branch 5 taken 531646 times.
✓ Branch 6 taken 532022 times.
✓ Branch 7 taken 132 times.
✓ Branch 8 taken 132 times.
✓ Branch 9 taken 66 times.
|
1064242 | ECHO(s16, int16_t, INT16_MIN, INT16_MAX) |
201 | ✗ | ECHO(s32, int32_t, INT32_MIN, INT32_MAX) | |
202 | |||
203 | 1 | static int config_output(AVFilterLink *outlink) | |
204 | { | ||
205 | 1 | AVFilterContext *ctx = outlink->src; | |
206 | 1 | AudioEchoContext *s = ctx->priv; | |
207 | 1 | float volume = 1.0; | |
208 | int i; | ||
209 | |||
210 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for (i = 0; i < s->nb_echoes; i++) { |
211 | 1 | s->samples[i] = s->delay[i] * outlink->sample_rate / 1000.0; | |
212 | 1 | s->max_samples = FFMAX(s->max_samples, s->samples[i]); | |
213 | 1 | volume += s->decay[i]; | |
214 | } | ||
215 | |||
216 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (s->max_samples <= 0) { |
217 | ✗ | av_log(ctx, AV_LOG_ERROR, "Nothing to echo - missing delay samples.\n"); | |
218 | ✗ | return AVERROR(EINVAL); | |
219 | } | ||
220 | 1 | s->fade_out = s->max_samples; | |
221 | |||
222 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (volume * s->in_gain * s->out_gain > 1.0) |
223 | ✗ | av_log(ctx, AV_LOG_WARNING, | |
224 | ✗ | "out_gain %f can cause saturation of output\n", s->out_gain); | |
225 | |||
226 |
1/5✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | switch (outlink->format) { |
227 | ✗ | case AV_SAMPLE_FMT_DBLP: s->echo_samples = echo_samples_dblp; break; | |
228 | ✗ | case AV_SAMPLE_FMT_FLTP: s->echo_samples = echo_samples_fltp; break; | |
229 | 1 | case AV_SAMPLE_FMT_S16P: s->echo_samples = echo_samples_s16p; break; | |
230 | ✗ | case AV_SAMPLE_FMT_S32P: s->echo_samples = echo_samples_s32p; break; | |
231 | } | ||
232 | |||
233 | |||
234 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (s->delayptrs) |
235 | ✗ | av_freep(&s->delayptrs[0]); | |
236 | 1 | av_freep(&s->delayptrs); | |
237 | |||
238 | 1 | return av_samples_alloc_array_and_samples(&s->delayptrs, NULL, | |
239 | outlink->ch_layout.nb_channels, | ||
240 | s->max_samples, | ||
241 | 1 | outlink->format, 0); | |
242 | } | ||
243 | |||
244 | 65 | static int filter_frame(AVFilterLink *inlink, AVFrame *frame) | |
245 | { | ||
246 | 65 | AVFilterContext *ctx = inlink->dst; | |
247 | 65 | AudioEchoContext *s = ctx->priv; | |
248 | AVFrame *out_frame; | ||
249 | |||
250 |
1/2✓ Branch 1 taken 65 times.
✗ Branch 2 not taken.
|
65 | if (av_frame_is_writable(frame)) { |
251 | 65 | out_frame = frame; | |
252 | } else { | ||
253 | ✗ | out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples); | |
254 | ✗ | if (!out_frame) { | |
255 | ✗ | av_frame_free(&frame); | |
256 | ✗ | return AVERROR(ENOMEM); | |
257 | } | ||
258 | ✗ | av_frame_copy_props(out_frame, frame); | |
259 | } | ||
260 | |||
261 | 65 | s->echo_samples(s, s->delayptrs, frame->extended_data, out_frame->extended_data, | |
262 | 65 | frame->nb_samples, inlink->ch_layout.nb_channels); | |
263 | |||
264 | 65 | s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base); | |
265 | |||
266 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (frame != out_frame) |
267 | ✗ | av_frame_free(&frame); | |
268 | |||
269 | 65 | return ff_filter_frame(ctx->outputs[0], out_frame); | |
270 | } | ||
271 | |||
272 | 1 | static int request_frame(AVFilterLink *outlink) | |
273 | { | ||
274 | 1 | AVFilterContext *ctx = outlink->src; | |
275 | 1 | AudioEchoContext *s = ctx->priv; | |
276 | 1 | int nb_samples = FFMIN(s->fade_out, 2048); | |
277 | 1 | AVFrame *frame = ff_get_audio_buffer(outlink, nb_samples); | |
278 | |||
279 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!frame) |
280 | ✗ | return AVERROR(ENOMEM); | |
281 | 1 | s->fade_out -= nb_samples; | |
282 | |||
283 | 1 | av_samples_set_silence(frame->extended_data, 0, | |
284 | frame->nb_samples, | ||
285 | outlink->ch_layout.nb_channels, | ||
286 | 1 | frame->format); | |
287 | |||
288 | 1 | s->echo_samples(s, s->delayptrs, frame->extended_data, frame->extended_data, | |
289 | frame->nb_samples, outlink->ch_layout.nb_channels); | ||
290 | |||
291 | 1 | frame->pts = s->next_pts; | |
292 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (s->next_pts != AV_NOPTS_VALUE) |
293 | 1 | s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base); | |
294 | |||
295 | 1 | return ff_filter_frame(outlink, frame); | |
296 | } | ||
297 | |||
298 | 132 | static int activate(AVFilterContext *ctx) | |
299 | { | ||
300 | 132 | AVFilterLink *inlink = ctx->inputs[0]; | |
301 | 132 | AVFilterLink *outlink = ctx->outputs[0]; | |
302 | 132 | AudioEchoContext *s = ctx->priv; | |
303 | AVFrame *in; | ||
304 | int ret, status; | ||
305 | int64_t pts; | ||
306 | |||
307 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 132 times.
|
132 | FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); |
308 | |||
309 | 132 | ret = ff_inlink_consume_frame(inlink, &in); | |
310 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
|
132 | if (ret < 0) |
311 | ✗ | return ret; | |
312 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 67 times.
|
132 | if (ret > 0) |
313 | 65 | return filter_frame(inlink, in); | |
314 | |||
315 |
4/4✓ Branch 0 taken 66 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 65 times.
|
67 | if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) { |
316 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (status == AVERROR_EOF) |
317 | 1 | s->eof = 1; | |
318 | } | ||
319 | |||
320 |
4/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
|
67 | if (s->eof && s->fade_out <= 0) { |
321 | 1 | ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts); | |
322 | 1 | return 0; | |
323 | } | ||
324 | |||
325 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 1 times.
|
66 | if (!s->eof) |
326 |
1/2✓ Branch 1 taken 65 times.
✗ Branch 2 not taken.
|
65 | FF_FILTER_FORWARD_WANTED(outlink, inlink); |
327 | |||
328 | 1 | return request_frame(outlink); | |
329 | } | ||
330 | |||
331 | static const AVFilterPad aecho_outputs[] = { | ||
332 | { | ||
333 | .name = "default", | ||
334 | .config_props = config_output, | ||
335 | .type = AVMEDIA_TYPE_AUDIO, | ||
336 | }, | ||
337 | }; | ||
338 | |||
339 | const AVFilter ff_af_aecho = { | ||
340 | .name = "aecho", | ||
341 | .description = NULL_IF_CONFIG_SMALL("Add echoing to the audio."), | ||
342 | .priv_size = sizeof(AudioEchoContext), | ||
343 | .priv_class = &aecho_class, | ||
344 | .init = init, | ||
345 | .activate = activate, | ||
346 | .uninit = uninit, | ||
347 | FILTER_INPUTS(ff_audio_default_filterpad), | ||
348 | FILTER_OUTPUTS(aecho_outputs), | ||
349 | FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P, | ||
350 | AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP), | ||
351 | }; | ||
352 |