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"
28 typedef struct ChanDelay {
35 typedef struct AudioDelayContext {
44 void (*delay_channel)(ChanDelay *d, int nb_samples,
45 const uint8_t *src, uint8_t *dst);
48 #define OFFSET(x) offsetof(AudioDelayContext, x)
49 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51 static const AVOption adelay_options[] = {
52 { "delays", "set list of delays for each channel", OFFSET(delays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
56 AVFILTER_DEFINE_CLASS(adelay);
58 static int query_formats(AVFilterContext *ctx)
60 AVFilterChannelLayouts *layouts;
61 AVFilterFormats *formats;
62 static const enum AVSampleFormat sample_fmts[] = {
63 AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
64 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
69 layouts = ff_all_channel_counts();
71 return AVERROR(ENOMEM);
72 ret = ff_set_common_channel_layouts(ctx, layouts);
76 formats = ff_make_format_list(sample_fmts);
78 return AVERROR(ENOMEM);
79 ret = ff_set_common_formats(ctx, formats);
83 formats = ff_all_samplerates();
85 return AVERROR(ENOMEM);
86 return ff_set_common_samplerates(ctx, formats);
89 #define DELAY(name, type, fill) \
90 static void delay_channel_## name ##p(ChanDelay *d, int nb_samples, \
91 const uint8_t *ssrc, uint8_t *ddst) \
93 const type *src = (type *)ssrc; \
94 type *dst = (type *)ddst; \
95 type *samples = (type *)d->samples; \
97 while (nb_samples) { \
98 if (d->delay_index < d->delay) { \
99 const int len = FFMIN(nb_samples, d->delay - d->delay_index); \
101 memcpy(&samples[d->delay_index], src, len * sizeof(type)); \
102 memset(dst, fill, len * sizeof(type)); \
103 d->delay_index += len; \
108 *dst = samples[d->index]; \
109 samples[d->index] = *src; \
113 d->index = d->index >= d->delay ? 0 : d->index; \
118 DELAY(u8, uint8_t, 0x80)
119 DELAY(s16, int16_t, 0)
120 DELAY(s32, int32_t, 0)
122 DELAY(dbl, double, 0)
124 static int config_input(AVFilterLink *inlink)
126 AVFilterContext *ctx = inlink->dst;
127 AudioDelayContext *s = ctx->priv;
128 char *p, *arg, *saveptr = NULL;
131 s->chandelay = av_calloc(inlink->channels, sizeof(*s->chandelay));
133 return AVERROR(ENOMEM);
134 s->nb_delays = inlink->channels;
135 s->block_align = av_get_bytes_per_sample(inlink->format);
138 for (i = 0; i < s->nb_delays; i++) {
139 ChanDelay *d = &s->chandelay[i];
144 if (!(arg = av_strtok(p, "|", &saveptr)))
149 ret = sscanf(arg, "%d%c", &d->delay, &type);
150 if (ret != 2 || type != 'S') {
151 sscanf(arg, "%f", &delay);
152 d->delay = delay * inlink->sample_rate / 1000.0;
156 av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n");
157 return AVERROR(EINVAL);
161 for (i = 0; i < s->nb_delays; i++) {
162 ChanDelay *d = &s->chandelay[i];
167 d->samples = av_malloc_array(d->delay, s->block_align);
169 return AVERROR(ENOMEM);
171 s->max_delay = FFMAX(s->max_delay, d->delay);
174 switch (inlink->format) {
175 case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ; break;
176 case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p; break;
177 case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p; break;
178 case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp; break;
179 case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp; break;
185 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
187 AVFilterContext *ctx = inlink->dst;
188 AudioDelayContext *s = ctx->priv;
192 if (ctx->is_disabled || !s->delays)
193 return ff_filter_frame(ctx->outputs[0], frame);
195 out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
197 av_frame_free(&frame);
198 return AVERROR(ENOMEM);
200 av_frame_copy_props(out_frame, frame);
202 for (i = 0; i < s->nb_delays; i++) {
203 ChanDelay *d = &s->chandelay[i];
204 const uint8_t *src = frame->extended_data[i];
205 uint8_t *dst = out_frame->extended_data[i];
208 memcpy(dst, src, frame->nb_samples * s->block_align);
210 s->delay_channel(d, frame->nb_samples, src, dst);
213 s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
214 av_frame_free(&frame);
215 return ff_filter_frame(ctx->outputs[0], out_frame);
218 static int request_frame(AVFilterLink *outlink)
220 AVFilterContext *ctx = outlink->src;
221 AudioDelayContext *s = ctx->priv;
224 ret = ff_request_frame(ctx->inputs[0]);
225 if (ret == AVERROR_EOF && !ctx->is_disabled && s->max_delay) {
226 int nb_samples = FFMIN(s->max_delay, 2048);
229 frame = ff_get_audio_buffer(outlink, nb_samples);
231 return AVERROR(ENOMEM);
232 s->max_delay -= nb_samples;
234 av_samples_set_silence(frame->extended_data, 0,
239 frame->pts = s->next_pts;
240 if (s->next_pts != AV_NOPTS_VALUE)
241 s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
243 ret = filter_frame(ctx->inputs[0], frame);
249 static av_cold void uninit(AVFilterContext *ctx)
251 AudioDelayContext *s = ctx->priv;
254 for (i = 0; i < s->nb_delays; i++)
255 av_freep(&s->chandelay[i].samples);
256 av_freep(&s->chandelay);
259 static const AVFilterPad adelay_inputs[] = {
262 .type = AVMEDIA_TYPE_AUDIO,
263 .config_props = config_input,
264 .filter_frame = filter_frame,
269 static const AVFilterPad adelay_outputs[] = {
272 .request_frame = request_frame,
273 .type = AVMEDIA_TYPE_AUDIO,
278 AVFilter ff_af_adelay = {
280 .description = NULL_IF_CONFIG_SMALL("Delay one or more audio channels."),
281 .query_formats = query_formats,
282 .priv_size = sizeof(AudioDelayContext),
283 .priv_class = &adelay_class,
285 .inputs = adelay_inputs,
286 .outputs = adelay_outputs,
287 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,