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 {
49 void (*delay_channel)(ChanDelay *d, int nb_samples,
50 const uint8_t *src, uint8_t *dst);
53 #define OFFSET(x) offsetof(AudioDelayContext, x)
54 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
56 static const AVOption adelay_options[] = {
57 { "delays", "set list of delays for each channel", OFFSET(delays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
58 { "all", "use last available delay for remained channels", OFFSET(all), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
62 AVFILTER_DEFINE_CLASS(adelay);
64 static int query_formats(AVFilterContext *ctx)
66 AVFilterChannelLayouts *layouts;
67 AVFilterFormats *formats;
68 static const enum AVSampleFormat sample_fmts[] = {
69 AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
70 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
75 layouts = ff_all_channel_counts();
77 return AVERROR(ENOMEM);
78 ret = ff_set_common_channel_layouts(ctx, layouts);
82 formats = ff_make_format_list(sample_fmts);
84 return AVERROR(ENOMEM);
85 ret = ff_set_common_formats(ctx, formats);
89 formats = ff_all_samplerates();
91 return AVERROR(ENOMEM);
92 return ff_set_common_samplerates(ctx, formats);
95 #define DELAY(name, type, fill) \
96 static void delay_channel_## name ##p(ChanDelay *d, int nb_samples, \
97 const uint8_t *ssrc, uint8_t *ddst) \
99 const type *src = (type *)ssrc; \
100 type *dst = (type *)ddst; \
101 type *samples = (type *)d->samples; \
103 while (nb_samples) { \
104 if (d->delay_index < d->delay) { \
105 const int len = FFMIN(nb_samples, d->delay - d->delay_index); \
107 memcpy(&samples[d->delay_index], src, len * sizeof(type)); \
108 memset(dst, fill, len * sizeof(type)); \
109 d->delay_index += len; \
114 *dst = samples[d->index]; \
115 samples[d->index] = *src; \
119 d->index = d->index >= d->delay ? 0 : d->index; \
124 DELAY(u8, uint8_t, 0x80)
125 DELAY(s16, int16_t, 0)
126 DELAY(s32, int32_t, 0)
128 DELAY(dbl, double, 0)
130 static int config_input(AVFilterLink *inlink)
132 AVFilterContext *ctx = inlink->dst;
133 AudioDelayContext *s = ctx->priv;
134 char *p, *arg, *saveptr = NULL;
137 s->chandelay = av_calloc(inlink->channels, sizeof(*s->chandelay));
139 return AVERROR(ENOMEM);
140 s->nb_delays = inlink->channels;
141 s->block_align = av_get_bytes_per_sample(inlink->format);
144 for (i = 0; i < s->nb_delays; i++) {
145 ChanDelay *d = &s->chandelay[i];
150 if (!(arg = av_strtok(p, "|", &saveptr)))
155 ret = av_sscanf(arg, "%d%c", &d->delay, &type);
156 if (ret != 2 || type != 'S') {
157 div = type == 's' ? 1.0 : 1000.0;
158 av_sscanf(arg, "%f", &delay);
159 d->delay = delay * inlink->sample_rate / div;
163 av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n");
164 return AVERROR(EINVAL);
169 for (int j = i; j < s->nb_delays; j++)
170 s->chandelay[j].delay = s->chandelay[i-1].delay;
173 s->padding = s->chandelay[0].delay;
174 for (i = 1; i < s->nb_delays; i++) {
175 ChanDelay *d = &s->chandelay[i];
177 s->padding = FFMIN(s->padding, d->delay);
181 for (i = 0; i < s->nb_delays; i++) {
182 ChanDelay *d = &s->chandelay[i];
184 d->delay -= s->padding;
188 for (i = 0; i < s->nb_delays; i++) {
189 ChanDelay *d = &s->chandelay[i];
194 d->samples = av_malloc_array(d->delay, s->block_align);
196 return AVERROR(ENOMEM);
198 s->max_delay = FFMAX(s->max_delay, d->delay);
201 switch (inlink->format) {
202 case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ; break;
203 case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p; break;
204 case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p; break;
205 case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp; break;
206 case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp; break;
212 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
214 AVFilterContext *ctx = inlink->dst;
215 AudioDelayContext *s = ctx->priv;
219 if (ctx->is_disabled || !s->delays)
220 return ff_filter_frame(ctx->outputs[0], frame);
222 out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
224 av_frame_free(&frame);
225 return AVERROR(ENOMEM);
227 av_frame_copy_props(out_frame, frame);
229 for (i = 0; i < s->nb_delays; i++) {
230 ChanDelay *d = &s->chandelay[i];
231 const uint8_t *src = frame->extended_data[i];
232 uint8_t *dst = out_frame->extended_data[i];
235 memcpy(dst, src, frame->nb_samples * s->block_align);
237 s->delay_channel(d, frame->nb_samples, src, dst);
240 out_frame->pts = s->next_pts;
241 s->next_pts += av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
242 av_frame_free(&frame);
243 return ff_filter_frame(ctx->outputs[0], out_frame);
246 static int activate(AVFilterContext *ctx)
248 AVFilterLink *inlink = ctx->inputs[0];
249 AVFilterLink *outlink = ctx->outputs[0];
250 AudioDelayContext *s = ctx->priv;
251 AVFrame *frame = NULL;
255 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
258 int nb_samples = FFMIN(s->padding, 2048);
260 frame = ff_get_audio_buffer(outlink, nb_samples);
262 return AVERROR(ENOMEM);
263 s->padding -= nb_samples;
265 av_samples_set_silence(frame->extended_data, 0,
270 frame->pts = s->next_pts;
271 if (s->next_pts != AV_NOPTS_VALUE)
272 s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
274 return ff_filter_frame(outlink, frame);
277 ret = ff_inlink_consume_frame(inlink, &frame);
282 return filter_frame(inlink, frame);
284 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
285 if (status == AVERROR_EOF)
289 if (s->eof && s->max_delay) {
290 int nb_samples = FFMIN(s->max_delay, 2048);
292 frame = ff_get_audio_buffer(outlink, nb_samples);
294 return AVERROR(ENOMEM);
295 s->max_delay -= nb_samples;
297 av_samples_set_silence(frame->extended_data, 0,
302 frame->pts = s->next_pts;
303 return filter_frame(inlink, frame);
306 if (s->eof && s->max_delay == 0) {
307 ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
312 FF_FILTER_FORWARD_WANTED(outlink, inlink);
314 return FFERROR_NOT_READY;
317 static av_cold void uninit(AVFilterContext *ctx)
319 AudioDelayContext *s = ctx->priv;
322 for (int i = 0; i < s->nb_delays; i++)
323 av_freep(&s->chandelay[i].samples);
325 av_freep(&s->chandelay);
328 static const AVFilterPad adelay_inputs[] = {
331 .type = AVMEDIA_TYPE_AUDIO,
332 .config_props = config_input,
337 static const AVFilterPad adelay_outputs[] = {
340 .type = AVMEDIA_TYPE_AUDIO,
345 AVFilter ff_af_adelay = {
347 .description = NULL_IF_CONFIG_SMALL("Delay one or more audio channels."),
348 .query_formats = query_formats,
349 .priv_size = sizeof(AudioDelayContext),
350 .priv_class = &adelay_class,
351 .activate = activate,
353 .inputs = adelay_inputs,
354 .outputs = adelay_outputs,
355 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,