FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_aecho.c
Date: 2024-04-25 05:10:44
Exec Total Coverage
Lines: 106 138 76.8%
Functions: 9 12 75.0%
Branches: 56 117 47.9%

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 #include "internal.h"
30
31 typedef struct AudioEchoContext {
32 const AVClass *class;
33 float in_gain, out_gain;
34 char *delays, *decays;
35 float *delay, *decay;
36 int nb_echoes;
37 int delay_index;
38 uint8_t **delayptrs;
39 int max_samples, fade_out;
40 int *samples;
41 int eof;
42 int64_t next_pts;
43
44 void (*echo_samples)(struct AudioEchoContext *ctx, uint8_t **delayptrs,
45 uint8_t * const *src, uint8_t **dst,
46 int nb_samples, int channels);
47 } AudioEchoContext;
48
49 #define OFFSET(x) offsetof(AudioEchoContext, x)
50 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51
52 static const AVOption aecho_options[] = {
53 { "in_gain", "set signal input gain", OFFSET(in_gain), AV_OPT_TYPE_FLOAT, {.dbl=0.6}, 0, 1, A },
54 { "out_gain", "set signal output gain", OFFSET(out_gain), AV_OPT_TYPE_FLOAT, {.dbl=0.3}, 0, 1, A },
55 { "delays", "set list of signal delays", OFFSET(delays), AV_OPT_TYPE_STRING, {.str="1000"}, 0, 0, A },
56 { "decays", "set list of signal decays", OFFSET(decays), AV_OPT_TYPE_STRING, {.str="0.5"}, 0, 0, A },
57 { NULL }
58 };
59
60 AVFILTER_DEFINE_CLASS(aecho);
61
62 4 static void count_items(char *item_str, int *nb_items)
63 {
64 char *p;
65
66 4 *nb_items = 1;
67
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 4 times.
14 for (p = item_str; *p; p++) {
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (*p == '|')
69 (*nb_items)++;
70 }
71
72 4 }
73
74 4 static void fill_items(char *item_str, int *nb_items, float *items)
75 {
76 4 char *p, *saveptr = NULL;
77 4 int i, new_nb_items = 0;
78
79 4 p = item_str;
80
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for (i = 0; i < *nb_items; i++) {
81 4 char *tstr = av_strtok(p, "|", &saveptr);
82 4 p = NULL;
83
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (tstr)
84 4 new_nb_items += av_sscanf(tstr, "%f", &items[new_nb_items]) == 1;
85 }
86
87 4 *nb_items = new_nb_items;
88 4 }
89
90 2 static av_cold void uninit(AVFilterContext *ctx)
91 {
92 2 AudioEchoContext *s = ctx->priv;
93
94 2 av_freep(&s->delay);
95 2 av_freep(&s->decay);
96 2 av_freep(&s->samples);
97
98
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (s->delayptrs)
99 1 av_freep(&s->delayptrs[0]);
100 2 av_freep(&s->delayptrs);
101 2 }
102
103 2 static av_cold int init(AVFilterContext *ctx)
104 {
105 2 AudioEchoContext *s = ctx->priv;
106 int nb_delays, nb_decays, i;
107
108
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) {
109 av_log(ctx, AV_LOG_ERROR, "Missing delays and/or decays.\n");
110 return AVERROR(EINVAL);
111 }
112
113 2 count_items(s->delays, &nb_delays);
114 2 count_items(s->decays, &nb_decays);
115
116 2 s->delay = av_realloc_f(s->delay, nb_delays, sizeof(*s->delay));
117 2 s->decay = av_realloc_f(s->decay, nb_decays, sizeof(*s->decay));
118
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)
119 return AVERROR(ENOMEM);
120
121 2 fill_items(s->delays, &nb_delays, s->delay);
122 2 fill_items(s->decays, &nb_decays, s->decay);
123
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (nb_delays != nb_decays) {
125 av_log(ctx, AV_LOG_ERROR, "Number of delays %d differs from number of decays %d.\n", nb_delays, nb_decays);
126 return AVERROR(EINVAL);
127 }
128
129 2 s->nb_echoes = nb_delays;
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->nb_echoes) {
131 av_log(ctx, AV_LOG_ERROR, "At least one decay & delay must be set.\n");
132 return AVERROR(EINVAL);
133 }
134
135 2 s->samples = av_realloc_f(s->samples, nb_delays, sizeof(*s->samples));
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->samples)
137 return AVERROR(ENOMEM);
138
139
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (i = 0; i < nb_delays; i++) {
140
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) {
141 av_log(ctx, AV_LOG_ERROR, "delay[%d]: %f is out of allowed range: (0, 90000]\n", i, s->delay[i]);
142 return AVERROR(EINVAL);
143 }
144
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) {
145 av_log(ctx, AV_LOG_ERROR, "decay[%d]: %f is out of allowed range: (0, 1]\n", i, s->decay[i]);
146 return AVERROR(EINVAL);
147 }
148 }
149
150 2 s->next_pts = AV_NOPTS_VALUE;
151
152 2 av_log(ctx, AV_LOG_DEBUG, "nb_echoes:%d\n", s->nb_echoes);
153 2 return 0;
154 }
155
156 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
157
158 #define ECHO(name, type, min, max) \
159 static void echo_samples_## name ##p(AudioEchoContext *ctx, \
160 uint8_t **delayptrs, \
161 uint8_t * const *src, uint8_t **dst, \
162 int nb_samples, int channels) \
163 { \
164 const double out_gain = ctx->out_gain; \
165 const double in_gain = ctx->in_gain; \
166 const int nb_echoes = ctx->nb_echoes; \
167 const int max_samples = ctx->max_samples; \
168 int i, j, chan, av_uninit(index); \
169 \
170 av_assert1(channels > 0); /* would corrupt delay_index */ \
171 \
172 for (chan = 0; chan < channels; chan++) { \
173 const type *s = (type *)src[chan]; \
174 type *d = (type *)dst[chan]; \
175 type *dbuf = (type *)delayptrs[chan]; \
176 \
177 index = ctx->delay_index; \
178 for (i = 0; i < nb_samples; i++, s++, d++) { \
179 double out, in; \
180 \
181 in = *s; \
182 out = in * in_gain; \
183 for (j = 0; j < nb_echoes; j++) { \
184 int ix = index + max_samples - ctx->samples[j]; \
185 ix = MOD(ix, max_samples); \
186 out += dbuf[ix] * ctx->decay[j]; \
187 } \
188 out *= out_gain; \
189 \
190 *d = av_clipd(out, min, max); \
191 dbuf[index] = in; \
192 \
193 index = MOD(index + 1, max_samples); \
194 } \
195 } \
196 ctx->delay_index = index; \
197 }
198
199 ECHO(dbl, double, -1.0, 1.0 )
200 ECHO(flt, float, -1.0, 1.0 )
201
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)
202 ECHO(s32, int32_t, INT32_MIN, INT32_MAX)
203
204 1 static int config_output(AVFilterLink *outlink)
205 {
206 1 AVFilterContext *ctx = outlink->src;
207 1 AudioEchoContext *s = ctx->priv;
208 1 float volume = 1.0;
209 int i;
210
211
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (i = 0; i < s->nb_echoes; i++) {
212 1 s->samples[i] = s->delay[i] * outlink->sample_rate / 1000.0;
213 1 s->max_samples = FFMAX(s->max_samples, s->samples[i]);
214 1 volume += s->decay[i];
215 }
216
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (s->max_samples <= 0) {
218 av_log(ctx, AV_LOG_ERROR, "Nothing to echo - missing delay samples.\n");
219 return AVERROR(EINVAL);
220 }
221 1 s->fade_out = s->max_samples;
222
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (volume * s->in_gain * s->out_gain > 1.0)
224 av_log(ctx, AV_LOG_WARNING,
225 "out_gain %f can cause saturation of output\n", s->out_gain);
226
227
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) {
228 case AV_SAMPLE_FMT_DBLP: s->echo_samples = echo_samples_dblp; break;
229 case AV_SAMPLE_FMT_FLTP: s->echo_samples = echo_samples_fltp; break;
230 1 case AV_SAMPLE_FMT_S16P: s->echo_samples = echo_samples_s16p; break;
231 case AV_SAMPLE_FMT_S32P: s->echo_samples = echo_samples_s32p; break;
232 }
233
234
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (s->delayptrs)
236 av_freep(&s->delayptrs[0]);
237 1 av_freep(&s->delayptrs);
238
239 1 return av_samples_alloc_array_and_samples(&s->delayptrs, NULL,
240 outlink->ch_layout.nb_channels,
241 s->max_samples,
242 1 outlink->format, 0);
243 }
244
245 65 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
246 {
247 65 AVFilterContext *ctx = inlink->dst;
248 65 AudioEchoContext *s = ctx->priv;
249 AVFrame *out_frame;
250
251
1/2
✓ Branch 1 taken 65 times.
✗ Branch 2 not taken.
65 if (av_frame_is_writable(frame)) {
252 65 out_frame = frame;
253 } else {
254 out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
255 if (!out_frame) {
256 av_frame_free(&frame);
257 return AVERROR(ENOMEM);
258 }
259 av_frame_copy_props(out_frame, frame);
260 }
261
262 65 s->echo_samples(s, s->delayptrs, frame->extended_data, out_frame->extended_data,
263 65 frame->nb_samples, inlink->ch_layout.nb_channels);
264
265 65 s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
266
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (frame != out_frame)
268 av_frame_free(&frame);
269
270 65 return ff_filter_frame(ctx->outputs[0], out_frame);
271 }
272
273 1 static int request_frame(AVFilterLink *outlink)
274 {
275 1 AVFilterContext *ctx = outlink->src;
276 1 AudioEchoContext *s = ctx->priv;
277 1 int nb_samples = FFMIN(s->fade_out, 2048);
278 1 AVFrame *frame = ff_get_audio_buffer(outlink, nb_samples);
279
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!frame)
281 return AVERROR(ENOMEM);
282 1 s->fade_out -= nb_samples;
283
284 1 av_samples_set_silence(frame->extended_data, 0,
285 frame->nb_samples,
286 outlink->ch_layout.nb_channels,
287 1 frame->format);
288
289 1 s->echo_samples(s, s->delayptrs, frame->extended_data, frame->extended_data,
290 frame->nb_samples, outlink->ch_layout.nb_channels);
291
292 1 frame->pts = s->next_pts;
293
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (s->next_pts != AV_NOPTS_VALUE)
294 1 s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
295
296 1 return ff_filter_frame(outlink, frame);
297 }
298
299 132 static int activate(AVFilterContext *ctx)
300 {
301 132 AVFilterLink *inlink = ctx->inputs[0];
302 132 AVFilterLink *outlink = ctx->outputs[0];
303 132 AudioEchoContext *s = ctx->priv;
304 AVFrame *in;
305 int ret, status;
306 int64_t pts;
307
308
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 132 times.
132 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
309
310 132 ret = ff_inlink_consume_frame(inlink, &in);
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 if (ret < 0)
312 return ret;
313
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 67 times.
132 if (ret > 0)
314 65 return filter_frame(inlink, in);
315
316
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)) {
317
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (status == AVERROR_EOF)
318 1 s->eof = 1;
319 }
320
321
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) {
322 1 ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
323 1 return 0;
324 }
325
326
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 1 times.
66 if (!s->eof)
327
1/2
✓ Branch 1 taken 65 times.
✗ Branch 2 not taken.
65 FF_FILTER_FORWARD_WANTED(outlink, inlink);
328
329 1 return request_frame(outlink);
330 }
331
332 static const AVFilterPad aecho_outputs[] = {
333 {
334 .name = "default",
335 .config_props = config_output,
336 .type = AVMEDIA_TYPE_AUDIO,
337 },
338 };
339
340 const AVFilter ff_af_aecho = {
341 .name = "aecho",
342 .description = NULL_IF_CONFIG_SMALL("Add echoing to the audio."),
343 .priv_size = sizeof(AudioEchoContext),
344 .priv_class = &aecho_class,
345 .init = init,
346 .activate = activate,
347 .uninit = uninit,
348 FILTER_INPUTS(ff_audio_default_filterpad),
349 FILTER_OUTPUTS(aecho_outputs),
350 FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
351 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP),
352 };
353