2 * Copyright (c) 2013 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/avassert.h"
22 #include "libavutil/avstring.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/samplefmt.h"
29 typedef struct AudioEchoContext
{
31 float in_gain
, out_gain
;
32 char *delays
, *decays
;
37 int max_samples
, fade_out
;
41 void (*echo_samples
)(struct AudioEchoContext
*ctx
, uint8_t **delayptrs
,
42 uint8_t * const *src
, uint8_t **dst
,
43 int nb_samples
, int channels
);
46 #define OFFSET(x) offsetof(AudioEchoContext, x)
47 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
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
},
57 AVFILTER_DEFINE_CLASS(aecho
);
59 static void count_items(char *item_str
, int *nb_items
)
64 for (p
= item_str
; *p
; p
++) {
71 static void fill_items(char *item_str
, int *nb_items
, float *items
)
73 char *p
, *saveptr
= NULL
;
74 int i
, new_nb_items
= 0;
77 for (i
= 0; i
< *nb_items
; i
++) {
78 char *tstr
= av_strtok(p
, "|", &saveptr
);
80 new_nb_items
+= sscanf(tstr
, "%f", &items
[i
]) == 1;
83 *nb_items
= new_nb_items
;
86 static av_cold
void uninit(AVFilterContext
*ctx
)
88 AudioEchoContext
*s
= ctx
->priv
;
92 av_freep(&s
->samples
);
95 av_freep(&s
->delayptrs
[0]);
96 av_freep(&s
->delayptrs
);
99 static av_cold
int init(AVFilterContext
*ctx
)
101 AudioEchoContext
*s
= ctx
->priv
;
102 int nb_delays
, nb_decays
, i
;
104 if (!s
->delays
|| !s
->decays
) {
105 av_log(ctx
, AV_LOG_ERROR
, "Missing delays and/or decays.\n");
106 return AVERROR(EINVAL
);
109 count_items(s
->delays
, &nb_delays
);
110 count_items(s
->decays
, &nb_decays
);
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
);
117 fill_items(s
->delays
, &nb_delays
, s
->delay
);
118 fill_items(s
->decays
, &nb_decays
, s
->decay
);
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
);
125 s
->nb_echoes
= nb_delays
;
127 av_log(ctx
, AV_LOG_ERROR
, "At least one decay & delay must be set.\n");
128 return AVERROR(EINVAL
);
131 s
->samples
= av_realloc_f(s
->samples
, nb_delays
, sizeof(*s
->samples
));
133 return AVERROR(ENOMEM
);
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
);
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
);
146 s
->next_pts
= AV_NOPTS_VALUE
;
148 av_log(ctx
, AV_LOG_DEBUG
, "nb_echoes:%d\n", s
->nb_echoes
);
152 static int query_formats(AVFilterContext
*ctx
)
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
,
162 layouts
= ff_all_channel_layouts();
164 return AVERROR(ENOMEM
);
165 ff_set_common_channel_layouts(ctx
, layouts
);
167 formats
= ff_make_format_list(sample_fmts
);
169 return AVERROR(ENOMEM
);
170 ff_set_common_formats(ctx
, formats
);
172 formats
= ff_all_samplerates();
174 return AVERROR(ENOMEM
);
175 ff_set_common_samplerates(ctx
, formats
);
180 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
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) \
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, av_uninit(index); \
194 av_assert1(channels > 0); /* would corrupt delay_index */ \
196 for (chan = 0; chan < channels; chan++) { \
197 const type *s = (type *)src[chan]; \
198 type *d = (type *)dst[chan]; \
199 type *dbuf = (type *)delayptrs[chan]; \
201 index = ctx->delay_index; \
202 for (i = 0; i < nb_samples; i++, s++, d++) { \
206 out = in * in_gain; \
207 for (j = 0; j < nb_echoes; j++) { \
208 int ix = index + max_samples - ctx->samples[j]; \
209 ix = MOD(ix, max_samples); \
210 out += dbuf[ix] * ctx->decay[j]; \
214 *d = av_clipd(out, min, max); \
217 index = MOD(index + 1, max_samples); \
220 ctx->delay_index = index; \
223 ECHO(dbl
, double, -1.0, 1.0 )
224 ECHO(flt
, float, -1.0, 1.0 )
225 ECHO(s16
, int16_t, INT16_MIN
, INT16_MAX
)
226 ECHO(s32
, int32_t, INT32_MIN
, INT32_MAX
)
228 static int config_output(AVFilterLink
*outlink
)
230 AVFilterContext
*ctx
= outlink
->src
;
231 AudioEchoContext
*s
= ctx
->priv
;
235 for (i
= 0; i
< s
->nb_echoes
; i
++) {
236 s
->samples
[i
] = s
->delay
[i
] * outlink
->sample_rate
/ 1000.0;
237 s
->max_samples
= FFMAX(s
->max_samples
, s
->samples
[i
]);
238 volume
+= s
->decay
[i
];
241 if (s
->max_samples
<= 0) {
242 av_log(ctx
, AV_LOG_ERROR
, "Nothing to echo - missing delay samples.\n");
243 return AVERROR(EINVAL
);
245 s
->fade_out
= s
->max_samples
;
247 if (volume
* s
->in_gain
* s
->out_gain
> 1.0)
248 av_log(ctx
, AV_LOG_WARNING
,
249 "out_gain %f can cause saturation of output\n", s
->out_gain
);
251 switch (outlink
->format
) {
252 case AV_SAMPLE_FMT_DBLP
: s
->echo_samples
= echo_samples_dblp
; break;
253 case AV_SAMPLE_FMT_FLTP
: s
->echo_samples
= echo_samples_fltp
; break;
254 case AV_SAMPLE_FMT_S16P
: s
->echo_samples
= echo_samples_s16p
; break;
255 case AV_SAMPLE_FMT_S32P
: s
->echo_samples
= echo_samples_s32p
; break;
260 av_freep(&s
->delayptrs
[0]);
261 av_freep(&s
->delayptrs
);
263 return av_samples_alloc_array_and_samples(&s
->delayptrs
, NULL
,
269 static int filter_frame(AVFilterLink
*inlink
, AVFrame
*frame
)
271 AVFilterContext
*ctx
= inlink
->dst
;
272 AudioEchoContext
*s
= ctx
->priv
;
275 if (av_frame_is_writable(frame
)) {
278 out_frame
= ff_get_audio_buffer(inlink
, frame
->nb_samples
);
280 return AVERROR(ENOMEM
);
281 av_frame_copy_props(out_frame
, frame
);
284 s
->echo_samples(s
, s
->delayptrs
, frame
->extended_data
, out_frame
->extended_data
,
285 frame
->nb_samples
, inlink
->channels
);
287 s
->next_pts
= frame
->pts
+ av_rescale_q(frame
->nb_samples
, (AVRational
){1, inlink
->sample_rate
}, inlink
->time_base
);
289 if (frame
!= out_frame
)
290 av_frame_free(&frame
);
292 return ff_filter_frame(ctx
->outputs
[0], out_frame
);
295 static int request_frame(AVFilterLink
*outlink
)
297 AVFilterContext
*ctx
= outlink
->src
;
298 AudioEchoContext
*s
= ctx
->priv
;
301 ret
= ff_request_frame(ctx
->inputs
[0]);
303 if (ret
== AVERROR_EOF
&& !ctx
->is_disabled
&& s
->fade_out
) {
304 int nb_samples
= FFMIN(s
->fade_out
, 2048);
307 frame
= ff_get_audio_buffer(outlink
, nb_samples
);
309 return AVERROR(ENOMEM
);
310 s
->fade_out
-= nb_samples
;
312 av_samples_set_silence(frame
->extended_data
, 0,
317 s
->echo_samples(s
, s
->delayptrs
, frame
->extended_data
, frame
->extended_data
,
318 frame
->nb_samples
, outlink
->channels
);
320 frame
->pts
= s
->next_pts
;
321 if (s
->next_pts
!= AV_NOPTS_VALUE
)
322 s
->next_pts
+= av_rescale_q(nb_samples
, (AVRational
){1, outlink
->sample_rate
}, outlink
->time_base
);
324 return ff_filter_frame(outlink
, frame
);
330 static const AVFilterPad aecho_inputs
[] = {
333 .type
= AVMEDIA_TYPE_AUDIO
,
334 .filter_frame
= filter_frame
,
339 static const AVFilterPad aecho_outputs
[] = {
342 .request_frame
= request_frame
,
343 .config_props
= config_output
,
344 .type
= AVMEDIA_TYPE_AUDIO
,
349 AVFilter ff_af_aecho
= {
351 .description
= NULL_IF_CONFIG_SMALL("Add echoing to the audio."),
352 .query_formats
= query_formats
,
353 .priv_size
= sizeof(AudioEchoContext
),
354 .priv_class
= &aecho_class
,
357 .inputs
= aecho_inputs
,
358 .outputs
= aecho_outputs
,