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
22 #include "libavutil/avstring.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/samplefmt.h"
29 typedef struct ChanDelay {
36 typedef struct AudioDelayContext {
45 void (*delay_channel)(ChanDelay *d, int nb_samples,
46 const uint8_t *src, uint8_t *dst);
49 #define OFFSET(x) offsetof(AudioDelayContext, x)
50 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
52 static const AVOption adelay_options[] = {
53 { "delays", "set list of delays for each channel", OFFSET(delays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
57 AVFILTER_DEFINE_CLASS(adelay);
59 static int query_formats(AVFilterContext *ctx)
61 AVFilterChannelLayouts *layouts;
62 AVFilterFormats *formats;
63 static const enum AVSampleFormat sample_fmts[] = {
64 AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
65 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
69 layouts = ff_all_channel_layouts();
71 return AVERROR(ENOMEM);
72 ff_set_common_channel_layouts(ctx, layouts);
74 formats = ff_make_format_list(sample_fmts);
76 return AVERROR(ENOMEM);
77 ff_set_common_formats(ctx, formats);
79 formats = ff_all_samplerates();
81 return AVERROR(ENOMEM);
82 ff_set_common_samplerates(ctx, formats);
87 #define DELAY(name, type, fill) \
88 static void delay_channel_## name ##p(ChanDelay *d, int nb_samples, \
89 const uint8_t *ssrc, uint8_t *ddst) \
91 const type *src = (type *)ssrc; \
92 type *dst = (type *)ddst; \
93 type *samples = (type *)d->samples; \
95 while (nb_samples) { \
96 if (d->delay_index < d->delay) { \
97 const int len = FFMIN(nb_samples, d->delay - d->delay_index); \
99 memcpy(&samples[d->delay_index], src, len * sizeof(type)); \
100 memset(dst, fill, len * sizeof(type)); \
101 d->delay_index += len; \
106 *dst = samples[d->index]; \
107 samples[d->index] = *src; \
111 d->index = d->index >= d->delay ? 0 : d->index; \
116 DELAY(u8, uint8_t, 0x80)
117 DELAY(s16, int16_t, 0)
118 DELAY(s32, int32_t, 0)
120 DELAY(dbl, double, 0)
122 static int config_input(AVFilterLink *inlink)
124 AVFilterContext *ctx = inlink->dst;
125 AudioDelayContext *s = ctx->priv;
126 char *p, *arg, *saveptr = NULL;
129 s->chandelay = av_calloc(inlink->channels, sizeof(*s->chandelay));
131 return AVERROR(ENOMEM);
132 s->nb_delays = inlink->channels;
133 s->block_align = av_get_bytes_per_sample(inlink->format);
136 for (i = 0; i < s->nb_delays; i++) {
137 ChanDelay *d = &s->chandelay[i];
140 if (!(arg = av_strtok(p, "|", &saveptr)))
144 sscanf(arg, "%f", &delay);
146 d->delay = delay * inlink->sample_rate / 1000.0;
148 av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n");
149 return AVERROR(EINVAL);
153 for (i = 0; i < s->nb_delays; i++) {
154 ChanDelay *d = &s->chandelay[i];
159 d->samples = av_malloc_array(d->delay, s->block_align);
161 return AVERROR(ENOMEM);
163 s->max_delay = FFMAX(s->max_delay, d->delay);
167 av_log(ctx, AV_LOG_ERROR, "At least one delay >0 must be specified.\n");
168 return AVERROR(EINVAL);
171 switch (inlink->format) {
172 case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ; break;
173 case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p; break;
174 case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p; break;
175 case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp; break;
176 case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp; break;
182 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
184 AVFilterContext *ctx = inlink->dst;
185 AudioDelayContext *s = ctx->priv;
189 if (ctx->is_disabled || !s->delays)
190 return ff_filter_frame(ctx->outputs[0], frame);
192 out_frame = ff_get_audio_buffer(inlink, frame->nb_samples);
194 return AVERROR(ENOMEM);
195 av_frame_copy_props(out_frame, frame);
197 for (i = 0; i < s->nb_delays; i++) {
198 ChanDelay *d = &s->chandelay[i];
199 const uint8_t *src = frame->extended_data[i];
200 uint8_t *dst = out_frame->extended_data[i];
203 memcpy(dst, src, frame->nb_samples * s->block_align);
205 s->delay_channel(d, frame->nb_samples, src, dst);
208 s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
209 av_frame_free(&frame);
210 return ff_filter_frame(ctx->outputs[0], out_frame);
213 static int request_frame(AVFilterLink *outlink)
215 AVFilterContext *ctx = outlink->src;
216 AudioDelayContext *s = ctx->priv;
219 ret = ff_request_frame(ctx->inputs[0]);
220 if (ret == AVERROR_EOF && !ctx->is_disabled && s->max_delay) {
221 int nb_samples = FFMIN(s->max_delay, 2048);
224 frame = ff_get_audio_buffer(outlink, nb_samples);
226 return AVERROR(ENOMEM);
227 s->max_delay -= nb_samples;
229 av_samples_set_silence(frame->extended_data, 0,
234 frame->pts = s->next_pts;
235 if (s->next_pts != AV_NOPTS_VALUE)
236 s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
238 ret = filter_frame(ctx->inputs[0], frame);
244 static av_cold void uninit(AVFilterContext *ctx)
246 AudioDelayContext *s = ctx->priv;
249 for (i = 0; i < s->nb_delays; i++)
250 av_free(s->chandelay[i].samples);
251 av_freep(&s->chandelay);
254 static const AVFilterPad adelay_inputs[] = {
257 .type = AVMEDIA_TYPE_AUDIO,
258 .config_props = config_input,
259 .filter_frame = filter_frame,
264 static const AVFilterPad adelay_outputs[] = {
267 .request_frame = request_frame,
268 .type = AVMEDIA_TYPE_AUDIO,
273 AVFilter avfilter_af_adelay = {
275 .description = NULL_IF_CONFIG_SMALL("Delay one or more audio channels."),
276 .query_formats = query_formats,
277 .priv_size = sizeof(AudioDelayContext),
278 .priv_class = &adelay_class,
280 .inputs = adelay_inputs,
281 .outputs = adelay_outputs,
282 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,