]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_adelay.c
avfilter/af_adelay: use av_sscanf()
[ffmpeg] / libavfilter / af_adelay.c
1 /*
2  * Copyright (c) 2013 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "libavutil/avstring.h"
22 #include "libavutil/eval.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/samplefmt.h"
25 #include "avfilter.h"
26 #include "audio.h"
27 #include "filters.h"
28 #include "internal.h"
29
30 typedef struct ChanDelay {
31     int delay;
32     unsigned delay_index;
33     unsigned index;
34     uint8_t *samples;
35 } ChanDelay;
36
37 typedef struct AudioDelayContext {
38     const AVClass *class;
39     char *delays;
40     ChanDelay *chandelay;
41     int nb_delays;
42     int block_align;
43     int64_t padding;
44     int64_t max_delay;
45     int64_t next_pts;
46     int eof;
47
48     void (*delay_channel)(ChanDelay *d, int nb_samples,
49                           const uint8_t *src, uint8_t *dst);
50 } AudioDelayContext;
51
52 #define OFFSET(x) offsetof(AudioDelayContext, x)
53 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
54
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 },
57     { NULL }
58 };
59
60 AVFILTER_DEFINE_CLASS(adelay);
61
62 static int query_formats(AVFilterContext *ctx)
63 {
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,
69         AV_SAMPLE_FMT_NONE
70     };
71     int ret;
72
73     layouts = ff_all_channel_counts();
74     if (!layouts)
75         return AVERROR(ENOMEM);
76     ret = ff_set_common_channel_layouts(ctx, layouts);
77     if (ret < 0)
78         return ret;
79
80     formats = ff_make_format_list(sample_fmts);
81     if (!formats)
82         return AVERROR(ENOMEM);
83     ret = ff_set_common_formats(ctx, formats);
84     if (ret < 0)
85         return ret;
86
87     formats = ff_all_samplerates();
88     if (!formats)
89         return AVERROR(ENOMEM);
90     return ff_set_common_samplerates(ctx, formats);
91 }
92
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) \
96 {                                                                         \
97     const type *src = (type *)ssrc;                                       \
98     type *dst = (type *)ddst;                                             \
99     type *samples = (type *)d->samples;                                   \
100                                                                           \
101     while (nb_samples) {                                                  \
102         if (d->delay_index < d->delay) {                                  \
103             const int len = FFMIN(nb_samples, d->delay - d->delay_index); \
104                                                                           \
105             memcpy(&samples[d->delay_index], src, len * sizeof(type));    \
106             memset(dst, fill, len * sizeof(type));                        \
107             d->delay_index += len;                                        \
108             src += len;                                                   \
109             dst += len;                                                   \
110             nb_samples -= len;                                            \
111         } else {                                                          \
112             *dst = samples[d->index];                                     \
113             samples[d->index] = *src;                                     \
114             nb_samples--;                                                 \
115             d->index++;                                                   \
116             src++, dst++;                                                 \
117             d->index = d->index >= d->delay ? 0 : d->index;               \
118         }                                                                 \
119     }                                                                     \
120 }
121
122 DELAY(u8,  uint8_t, 0x80)
123 DELAY(s16, int16_t, 0)
124 DELAY(s32, int32_t, 0)
125 DELAY(flt, float,   0)
126 DELAY(dbl, double,  0)
127
128 static int config_input(AVFilterLink *inlink)
129 {
130     AVFilterContext *ctx = inlink->dst;
131     AudioDelayContext *s = ctx->priv;
132     char *p, *arg, *saveptr = NULL;
133     int i;
134
135     s->chandelay = av_calloc(inlink->channels, sizeof(*s->chandelay));
136     if (!s->chandelay)
137         return AVERROR(ENOMEM);
138     s->nb_delays = inlink->channels;
139     s->block_align = av_get_bytes_per_sample(inlink->format);
140
141     p = s->delays;
142     for (i = 0; i < s->nb_delays; i++) {
143         ChanDelay *d = &s->chandelay[i];
144         float delay;
145         char type = 0;
146         int ret;
147
148         if (!(arg = av_strtok(p, "|", &saveptr)))
149             break;
150
151         p = NULL;
152
153         ret = av_sscanf(arg, "%d%c", &d->delay, &type);
154         if (ret != 2 || type != 'S') {
155             av_sscanf(arg, "%f", &delay);
156             d->delay = delay * inlink->sample_rate / 1000.0;
157         }
158
159         if (d->delay < 0) {
160             av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n");
161             return AVERROR(EINVAL);
162         }
163     }
164
165     s->padding = s->chandelay[0].delay;
166     for (i = 1; i < s->nb_delays; i++) {
167         ChanDelay *d = &s->chandelay[i];
168
169         s->padding = FFMIN(s->padding, d->delay);
170     }
171
172     if (s->padding) {
173         for (i = 0; i < s->nb_delays; i++) {
174             ChanDelay *d = &s->chandelay[i];
175
176             d->delay -= s->padding;
177         }
178     }
179
180     for (i = 0; i < s->nb_delays; i++) {
181         ChanDelay *d = &s->chandelay[i];
182
183         if (!d->delay)
184             continue;
185
186         d->samples = av_malloc_array(d->delay, s->block_align);
187         if (!d->samples)
188             return AVERROR(ENOMEM);
189
190         s->max_delay = FFMAX(s->max_delay, d->delay);
191     }
192
193     switch (inlink->format) {
194     case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ; break;
195     case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p; break;
196     case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p; break;
197     case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp; break;
198     case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp; break;
199     }
200
201     return 0;
202 }
203
204 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
205 {
206     AVFilterContext *ctx = inlink->dst;
207     AudioDelayContext *s = ctx->priv;
208     AVFrame *out_frame;
209     int i;
210
211     if (ctx->is_disabled || !s->delays)
212         return ff_filter_frame(ctx->outputs[0], frame);
213
214     out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
215     if (!out_frame) {
216         av_frame_free(&frame);
217         return AVERROR(ENOMEM);
218     }
219     av_frame_copy_props(out_frame, frame);
220
221     for (i = 0; i < s->nb_delays; i++) {
222         ChanDelay *d = &s->chandelay[i];
223         const uint8_t *src = frame->extended_data[i];
224         uint8_t *dst = out_frame->extended_data[i];
225
226         if (!d->delay)
227             memcpy(dst, src, frame->nb_samples * s->block_align);
228         else
229             s->delay_channel(d, frame->nb_samples, src, dst);
230     }
231
232     out_frame->pts = s->next_pts;
233     s->next_pts += av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
234     av_frame_free(&frame);
235     return ff_filter_frame(ctx->outputs[0], out_frame);
236 }
237
238 static int activate(AVFilterContext *ctx)
239 {
240     AVFilterLink *inlink = ctx->inputs[0];
241     AVFilterLink *outlink = ctx->outputs[0];
242     AudioDelayContext *s = ctx->priv;
243     AVFrame *frame = NULL;
244     int ret, status;
245     int64_t pts;
246
247     FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
248
249     if (s->padding) {
250         int nb_samples = FFMIN(s->padding, 2048);
251
252         frame = ff_get_audio_buffer(outlink, nb_samples);
253         if (!frame)
254             return AVERROR(ENOMEM);
255         s->padding -= nb_samples;
256
257         av_samples_set_silence(frame->extended_data, 0,
258                                frame->nb_samples,
259                                outlink->channels,
260                                frame->format);
261
262         frame->pts = s->next_pts;
263         if (s->next_pts != AV_NOPTS_VALUE)
264             s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
265
266         return ff_filter_frame(outlink, frame);
267     }
268
269     ret = ff_inlink_consume_frame(inlink, &frame);
270     if (ret < 0)
271         return ret;
272
273     if (ret > 0)
274         return filter_frame(inlink, frame);
275
276     if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
277         if (status == AVERROR_EOF)
278             s->eof = 1;
279     }
280
281     if (s->eof && s->max_delay) {
282         int nb_samples = FFMIN(s->max_delay, 2048);
283
284         frame = ff_get_audio_buffer(outlink, nb_samples);
285         if (!frame)
286             return AVERROR(ENOMEM);
287         s->max_delay -= nb_samples;
288
289         av_samples_set_silence(frame->extended_data, 0,
290                                frame->nb_samples,
291                                outlink->channels,
292                                frame->format);
293
294         frame->pts = s->next_pts;
295         return filter_frame(inlink, frame);
296     }
297
298     if (s->eof && s->max_delay == 0) {
299         ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
300         return 0;
301     }
302
303     if (!s->eof)
304         FF_FILTER_FORWARD_WANTED(outlink, inlink);
305
306     return FFERROR_NOT_READY;
307 }
308
309 static av_cold void uninit(AVFilterContext *ctx)
310 {
311     AudioDelayContext *s = ctx->priv;
312
313     if (s->chandelay) {
314         for (int i = 0; i < s->nb_delays; i++)
315             av_freep(&s->chandelay[i].samples);
316     }
317     av_freep(&s->chandelay);
318 }
319
320 static const AVFilterPad adelay_inputs[] = {
321     {
322         .name         = "default",
323         .type         = AVMEDIA_TYPE_AUDIO,
324         .config_props = config_input,
325     },
326     { NULL }
327 };
328
329 static const AVFilterPad adelay_outputs[] = {
330     {
331         .name = "default",
332         .type = AVMEDIA_TYPE_AUDIO,
333     },
334     { NULL }
335 };
336
337 AVFilter ff_af_adelay = {
338     .name          = "adelay",
339     .description   = NULL_IF_CONFIG_SMALL("Delay one or more audio channels."),
340     .query_formats = query_formats,
341     .priv_size     = sizeof(AudioDelayContext),
342     .priv_class    = &adelay_class,
343     .activate      = activate,
344     .uninit        = uninit,
345     .inputs        = adelay_inputs,
346     .outputs       = adelay_outputs,
347     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
348 };