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