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/avstring.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/samplefmt.h"
29 typedef struct ChanDelay {
36 typedef struct AudioDelayContext {
47 void (*delay_channel)(ChanDelay *d, int nb_samples,
48 const uint8_t *src, uint8_t *dst);
51 #define OFFSET(x) offsetof(AudioDelayContext, x)
52 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
54 static const AVOption adelay_options[] = {
55 { "delays", "set list of delays for each channel", OFFSET(delays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
59 AVFILTER_DEFINE_CLASS(adelay);
61 static int query_formats(AVFilterContext *ctx)
63 AVFilterChannelLayouts *layouts;
64 AVFilterFormats *formats;
65 static const enum AVSampleFormat sample_fmts[] = {
66 AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
67 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
72 layouts = ff_all_channel_counts();
74 return AVERROR(ENOMEM);
75 ret = ff_set_common_channel_layouts(ctx, layouts);
79 formats = ff_make_format_list(sample_fmts);
81 return AVERROR(ENOMEM);
82 ret = ff_set_common_formats(ctx, formats);
86 formats = ff_all_samplerates();
88 return AVERROR(ENOMEM);
89 return ff_set_common_samplerates(ctx, formats);
92 #define DELAY(name, type, fill) \
93 static void delay_channel_## name ##p(ChanDelay *d, int nb_samples, \
94 const uint8_t *ssrc, uint8_t *ddst) \
96 const type *src = (type *)ssrc; \
97 type *dst = (type *)ddst; \
98 type *samples = (type *)d->samples; \
100 while (nb_samples) { \
101 if (d->delay_index < d->delay) { \
102 const int len = FFMIN(nb_samples, d->delay - d->delay_index); \
104 memcpy(&samples[d->delay_index], src, len * sizeof(type)); \
105 memset(dst, fill, len * sizeof(type)); \
106 d->delay_index += len; \
111 *dst = samples[d->index]; \
112 samples[d->index] = *src; \
116 d->index = d->index >= d->delay ? 0 : d->index; \
121 DELAY(u8, uint8_t, 0x80)
122 DELAY(s16, int16_t, 0)
123 DELAY(s32, int32_t, 0)
125 DELAY(dbl, double, 0)
127 static int config_input(AVFilterLink *inlink)
129 AVFilterContext *ctx = inlink->dst;
130 AudioDelayContext *s = ctx->priv;
131 char *p, *arg, *saveptr = NULL;
134 s->chandelay = av_calloc(inlink->channels, sizeof(*s->chandelay));
136 return AVERROR(ENOMEM);
137 s->nb_delays = inlink->channels;
138 s->block_align = av_get_bytes_per_sample(inlink->format);
141 for (i = 0; i < s->nb_delays; i++) {
142 ChanDelay *d = &s->chandelay[i];
147 if (!(arg = av_strtok(p, "|", &saveptr)))
152 ret = sscanf(arg, "%d%c", &d->delay, &type);
153 if (ret != 2 || type != 'S') {
154 sscanf(arg, "%f", &delay);
155 d->delay = delay * inlink->sample_rate / 1000.0;
159 av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n");
160 return AVERROR(EINVAL);
164 s->padding = s->chandelay[0].delay;
165 for (i = 1; i < s->nb_delays; i++) {
166 ChanDelay *d = &s->chandelay[i];
168 s->padding = FFMIN(s->padding, d->delay);
172 for (i = 0; i < s->nb_delays; i++) {
173 ChanDelay *d = &s->chandelay[i];
175 d->delay -= s->padding;
179 for (i = 0; i < s->nb_delays; i++) {
180 ChanDelay *d = &s->chandelay[i];
185 d->samples = av_malloc_array(d->delay, s->block_align);
187 return AVERROR(ENOMEM);
189 s->max_delay = FFMAX(s->max_delay, d->delay);
192 switch (inlink->format) {
193 case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ; break;
194 case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p; break;
195 case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p; break;
196 case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp; break;
197 case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp; break;
203 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
205 AVFilterContext *ctx = inlink->dst;
206 AudioDelayContext *s = ctx->priv;
210 if (ctx->is_disabled || !s->delays)
211 return ff_filter_frame(ctx->outputs[0], frame);
213 out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
215 av_frame_free(&frame);
216 return AVERROR(ENOMEM);
218 av_frame_copy_props(out_frame, frame);
220 for (i = 0; i < s->nb_delays; i++) {
221 ChanDelay *d = &s->chandelay[i];
222 const uint8_t *src = frame->extended_data[i];
223 uint8_t *dst = out_frame->extended_data[i];
226 memcpy(dst, src, frame->nb_samples * s->block_align);
228 s->delay_channel(d, frame->nb_samples, src, dst);
231 out_frame->pts = s->next_pts;
232 s->next_pts += av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
233 av_frame_free(&frame);
234 return ff_filter_frame(ctx->outputs[0], out_frame);
237 static int activate(AVFilterContext *ctx)
239 AVFilterLink *inlink = ctx->inputs[0];
240 AVFilterLink *outlink = ctx->outputs[0];
241 AudioDelayContext *s = ctx->priv;
242 AVFrame *frame = NULL;
246 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
249 int nb_samples = FFMIN(s->padding, 2048);
251 frame = ff_get_audio_buffer(outlink, nb_samples);
253 return AVERROR(ENOMEM);
254 s->padding -= nb_samples;
256 av_samples_set_silence(frame->extended_data, 0,
261 frame->pts = s->next_pts;
262 if (s->next_pts != AV_NOPTS_VALUE)
263 s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
265 return ff_filter_frame(outlink, frame);
268 ret = ff_inlink_consume_frame(inlink, &frame);
273 return filter_frame(inlink, frame);
275 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
276 if (status == AVERROR_EOF)
280 if (s->eof && s->max_delay) {
281 int nb_samples = FFMIN(s->max_delay, 2048);
283 frame = ff_get_audio_buffer(outlink, nb_samples);
285 return AVERROR(ENOMEM);
286 s->max_delay -= nb_samples;
288 av_samples_set_silence(frame->extended_data, 0,
293 frame->pts = s->next_pts;
294 return filter_frame(inlink, frame);
297 if (s->eof && s->max_delay == 0) {
298 ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
303 FF_FILTER_FORWARD_WANTED(outlink, inlink);
305 return FFERROR_NOT_READY;
308 static av_cold void uninit(AVFilterContext *ctx)
310 AudioDelayContext *s = ctx->priv;
313 for (int i = 0; i < s->nb_delays; i++)
314 av_freep(&s->chandelay[i].samples);
316 av_freep(&s->chandelay);
319 static const AVFilterPad adelay_inputs[] = {
322 .type = AVMEDIA_TYPE_AUDIO,
323 .config_props = config_input,
328 static const AVFilterPad adelay_outputs[] = {
331 .type = AVMEDIA_TYPE_AUDIO,
336 AVFilter ff_af_adelay = {
338 .description = NULL_IF_CONFIG_SMALL("Delay one or more audio channels."),
339 .query_formats = query_formats,
340 .priv_size = sizeof(AudioDelayContext),
341 .priv_class = &adelay_class,
342 .activate = activate,
344 .inputs = adelay_inputs,
345 .outputs = adelay_outputs,
346 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,