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/eval.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/samplefmt.h"
30 typedef struct ChanDelay {
37 typedef struct AudioDelayContext {
48 void (*delay_channel)(ChanDelay *d, int nb_samples,
49 const uint8_t *src, uint8_t *dst);
52 #define OFFSET(x) offsetof(AudioDelayContext, x)
53 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
55 static const AVOption adelay_options[] = {
56 { "delays", "set list of delays for each channel", OFFSET(delays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
60 AVFILTER_DEFINE_CLASS(adelay);
62 static int query_formats(AVFilterContext *ctx)
64 AVFilterChannelLayouts *layouts;
65 AVFilterFormats *formats;
66 static const enum AVSampleFormat sample_fmts[] = {
67 AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
68 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
73 layouts = ff_all_channel_counts();
75 return AVERROR(ENOMEM);
76 ret = ff_set_common_channel_layouts(ctx, layouts);
80 formats = ff_make_format_list(sample_fmts);
82 return AVERROR(ENOMEM);
83 ret = ff_set_common_formats(ctx, formats);
87 formats = ff_all_samplerates();
89 return AVERROR(ENOMEM);
90 return ff_set_common_samplerates(ctx, formats);
93 #define DELAY(name, type, fill) \
94 static void delay_channel_## name ##p(ChanDelay *d, int nb_samples, \
95 const uint8_t *ssrc, uint8_t *ddst) \
97 const type *src = (type *)ssrc; \
98 type *dst = (type *)ddst; \
99 type *samples = (type *)d->samples; \
101 while (nb_samples) { \
102 if (d->delay_index < d->delay) { \
103 const int len = FFMIN(nb_samples, d->delay - d->delay_index); \
105 memcpy(&samples[d->delay_index], src, len * sizeof(type)); \
106 memset(dst, fill, len * sizeof(type)); \
107 d->delay_index += len; \
112 *dst = samples[d->index]; \
113 samples[d->index] = *src; \
117 d->index = d->index >= d->delay ? 0 : d->index; \
122 DELAY(u8, uint8_t, 0x80)
123 DELAY(s16, int16_t, 0)
124 DELAY(s32, int32_t, 0)
126 DELAY(dbl, double, 0)
128 static int config_input(AVFilterLink *inlink)
130 AVFilterContext *ctx = inlink->dst;
131 AudioDelayContext *s = ctx->priv;
132 char *p, *arg, *saveptr = NULL;
135 s->chandelay = av_calloc(inlink->channels, sizeof(*s->chandelay));
137 return AVERROR(ENOMEM);
138 s->nb_delays = inlink->channels;
139 s->block_align = av_get_bytes_per_sample(inlink->format);
142 for (i = 0; i < s->nb_delays; i++) {
143 ChanDelay *d = &s->chandelay[i];
148 if (!(arg = av_strtok(p, "|", &saveptr)))
153 ret = av_sscanf(arg, "%d%c", &d->delay, &type);
154 if (ret != 2 || type != 'S') {
155 div = type == 's' ? 1.0 : 1000.0;
156 av_sscanf(arg, "%f", &delay);
157 d->delay = delay * inlink->sample_rate / div;
161 av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n");
162 return AVERROR(EINVAL);
166 s->padding = s->chandelay[0].delay;
167 for (i = 1; i < s->nb_delays; i++) {
168 ChanDelay *d = &s->chandelay[i];
170 s->padding = FFMIN(s->padding, d->delay);
174 for (i = 0; i < s->nb_delays; i++) {
175 ChanDelay *d = &s->chandelay[i];
177 d->delay -= s->padding;
181 for (i = 0; i < s->nb_delays; i++) {
182 ChanDelay *d = &s->chandelay[i];
187 d->samples = av_malloc_array(d->delay, s->block_align);
189 return AVERROR(ENOMEM);
191 s->max_delay = FFMAX(s->max_delay, d->delay);
194 switch (inlink->format) {
195 case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ; break;
196 case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p; break;
197 case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p; break;
198 case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp; break;
199 case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp; break;
205 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
207 AVFilterContext *ctx = inlink->dst;
208 AudioDelayContext *s = ctx->priv;
212 if (ctx->is_disabled || !s->delays)
213 return ff_filter_frame(ctx->outputs[0], frame);
215 out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
217 av_frame_free(&frame);
218 return AVERROR(ENOMEM);
220 av_frame_copy_props(out_frame, frame);
222 for (i = 0; i < s->nb_delays; i++) {
223 ChanDelay *d = &s->chandelay[i];
224 const uint8_t *src = frame->extended_data[i];
225 uint8_t *dst = out_frame->extended_data[i];
228 memcpy(dst, src, frame->nb_samples * s->block_align);
230 s->delay_channel(d, frame->nb_samples, src, dst);
233 out_frame->pts = s->next_pts;
234 s->next_pts += av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
235 av_frame_free(&frame);
236 return ff_filter_frame(ctx->outputs[0], out_frame);
239 static int activate(AVFilterContext *ctx)
241 AVFilterLink *inlink = ctx->inputs[0];
242 AVFilterLink *outlink = ctx->outputs[0];
243 AudioDelayContext *s = ctx->priv;
244 AVFrame *frame = NULL;
248 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
251 int nb_samples = FFMIN(s->padding, 2048);
253 frame = ff_get_audio_buffer(outlink, nb_samples);
255 return AVERROR(ENOMEM);
256 s->padding -= nb_samples;
258 av_samples_set_silence(frame->extended_data, 0,
263 frame->pts = s->next_pts;
264 if (s->next_pts != AV_NOPTS_VALUE)
265 s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
267 return ff_filter_frame(outlink, frame);
270 ret = ff_inlink_consume_frame(inlink, &frame);
275 return filter_frame(inlink, frame);
277 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
278 if (status == AVERROR_EOF)
282 if (s->eof && s->max_delay) {
283 int nb_samples = FFMIN(s->max_delay, 2048);
285 frame = ff_get_audio_buffer(outlink, nb_samples);
287 return AVERROR(ENOMEM);
288 s->max_delay -= nb_samples;
290 av_samples_set_silence(frame->extended_data, 0,
295 frame->pts = s->next_pts;
296 return filter_frame(inlink, frame);
299 if (s->eof && s->max_delay == 0) {
300 ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
305 FF_FILTER_FORWARD_WANTED(outlink, inlink);
307 return FFERROR_NOT_READY;
310 static av_cold void uninit(AVFilterContext *ctx)
312 AudioDelayContext *s = ctx->priv;
315 for (int i = 0; i < s->nb_delays; i++)
316 av_freep(&s->chandelay[i].samples);
318 av_freep(&s->chandelay);
321 static const AVFilterPad adelay_inputs[] = {
324 .type = AVMEDIA_TYPE_AUDIO,
325 .config_props = config_input,
330 static const AVFilterPad adelay_outputs[] = {
333 .type = AVMEDIA_TYPE_AUDIO,
338 AVFilter ff_af_adelay = {
340 .description = NULL_IF_CONFIG_SMALL("Delay one or more audio channels."),
341 .query_formats = query_formats,
342 .priv_size = sizeof(AudioDelayContext),
343 .priv_class = &adelay_class,
344 .activate = activate,
346 .inputs = adelay_inputs,
347 .outputs = adelay_outputs,
348 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,