FFmpeg
Loading...
Searching...
No Matches
af_adelay.c
Go to the documentation of this file.
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/mem.h"
23#include "libavutil/opt.h"
24#include "libavutil/samplefmt.h"
25#include "avfilter.h"
26#include "audio.h"
27#include "filters.h"
28
29typedef struct ChanDelay {
32 size_t index;
33 unsigned int samples_size;
34 uint8_t *samples;
35} ChanDelay;
36
37typedef struct AudioDelayContext {
38 const AVClass *class;
39 int all;
40 char *delays;
48 int eof;
49
51
52 void (*delay_channel)(ChanDelay *d, int nb_samples,
53 const uint8_t *src, uint8_t *dst);
56
57#define OFFSET(x) offsetof(AudioDelayContext, x)
58#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
59
60static const AVOption adelay_options[] = {
61 { "delays", "set list of delays for each channel", OFFSET(delays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A | AV_OPT_FLAG_RUNTIME_PARAM },
62 { "all", "use last available delay for remained channels", OFFSET(all), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
63 { NULL }
64};
65
67
68#define DELAY(name, type, fill) \
69static void delay_channel_## name ##p(ChanDelay *d, int nb_samples, \
70 const uint8_t *ssrc, uint8_t *ddst) \
71{ \
72 const type *src = (type *)ssrc; \
73 type *dst = (type *)ddst; \
74 type *samples = (type *)d->samples; \
75 \
76 while (nb_samples) { \
77 if (d->delay_index < d->delay) { \
78 const int len = FFMIN(nb_samples, d->delay - d->delay_index); \
79 \
80 memcpy(&samples[d->delay_index], src, len * sizeof(type)); \
81 memset(dst, fill, len * sizeof(type)); \
82 d->delay_index += len; \
83 src += len; \
84 dst += len; \
85 nb_samples -= len; \
86 } else { \
87 *dst = samples[d->index]; \
88 samples[d->index] = *src; \
89 nb_samples--; \
90 d->index++; \
91 src++, dst++; \
92 d->index = d->index >= d->delay ? 0 : d->index; \
93 } \
94 } \
95}
96
97DELAY(u8, uint8_t, 0x80)
98DELAY(s16, int16_t, 0)
99DELAY(s32, int32_t, 0)
100DELAY(flt, float, 0)
101DELAY(dbl, double, 0)
102
103#define CHANGE_DELAY(name, type, fill) \
104static int resize_samples_## name ##p(ChanDelay *d, int64_t new_delay) \
105{ \
106 type *samples; \
107 \
108 if (new_delay == d->delay) { \
109 return 0; \
110 } \
111 \
112 if (new_delay == 0) { \
113 av_freep(&d->samples); \
114 d->samples_size = 0; \
115 d->delay = 0; \
116 d->index = 0; \
117 d->delay_index = 0; \
118 return 0; \
119 } \
120 \
121 samples = (type *) av_fast_realloc(d->samples, &d->samples_size, new_delay * sizeof(type)); \
122 if (!samples) { \
123 return AVERROR(ENOMEM); \
124 } \
125 \
126 if (new_delay < d->delay) { \
127 if (d->index > new_delay) { \
128 d->index -= new_delay; \
129 memmove(samples, &samples[new_delay], d->index * sizeof(type)); \
130 d->delay_index = new_delay; \
131 } else if (d->delay_index > d->index) { \
132 memmove(&samples[d->index], &samples[d->index+(d->delay-new_delay)], \
133 (new_delay - d->index) * sizeof(type)); \
134 d->delay_index -= d->delay - new_delay; \
135 } \
136 } else { \
137 size_t block_size; \
138 if (d->delay_index >= d->delay) { \
139 block_size = (d->delay - d->index) * sizeof(type); \
140 memmove(&samples[d->index+(new_delay - d->delay)], &samples[d->index], block_size); \
141 d->delay_index = new_delay; \
142 } else { \
143 d->delay_index += new_delay - d->delay; \
144 } \
145 block_size = (new_delay - d->delay) * sizeof(type); \
146 memset(&samples[d->index], fill, block_size); \
147 } \
148 d->delay = new_delay; \
149 d->samples = (void *) samples; \
150 return 0; \
151}
152
153CHANGE_DELAY(u8, uint8_t, 0x80)
154CHANGE_DELAY(s16, int16_t, 0)
155CHANGE_DELAY(s32, int32_t, 0)
156CHANGE_DELAY(flt, float, 0)
157CHANGE_DELAY(dbl, double, 0)
158
159static int parse_delays(char *p, char **saveptr, int64_t *result, AVFilterContext *ctx, int sample_rate) {
160 float delay, div;
161 int ret;
162 char *arg;
163 char type = 0;
164
165 if (!(arg = av_strtok(p, "|", saveptr)))
166 return 1;
167
168 ret = av_sscanf(arg, "%"SCNd64"%c", result, &type);
169 if (ret != 2 || type != 'S') {
170 div = type == 's' ? 1.0 : 1000.0;
171 if (av_sscanf(arg, "%f", &delay) != 1) {
172 av_log(ctx, AV_LOG_ERROR, "Invalid syntax for delay.\n");
173 return AVERROR(EINVAL);
174 }
175 *result = delay * sample_rate / div;
176 }
177
178 if (*result < 0) {
179 av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n");
180 return AVERROR(EINVAL);
181 }
182 return 0;
183}
184
185static int config_input(AVFilterLink *inlink)
186{
187 AVFilterContext *ctx = inlink->dst;
188 AudioDelayContext *s = ctx->priv;
189 char *p, *saveptr = NULL;
190 int i;
191
192 s->next_pts = AV_NOPTS_VALUE;
193 s->chandelay = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->chandelay));
194 if (!s->chandelay)
195 return AVERROR(ENOMEM);
196 s->nb_delays = inlink->ch_layout.nb_channels;
197 s->block_align = av_get_bytes_per_sample(inlink->format);
198
199 p = s->delays;
200 for (i = 0; i < s->nb_delays; i++) {
201 ChanDelay *d = &s->chandelay[i];
202 int ret;
203
204 ret = parse_delays(p, &saveptr, &d->delay, ctx, inlink->sample_rate);
205 if (ret == 1)
206 break;
207 else if (ret < 0)
208 return ret;
209 p = NULL;
210 }
211
212 if (s->all && i) {
213 for (int j = i; j < s->nb_delays; j++)
214 s->chandelay[j].delay = s->chandelay[i-1].delay;
215 }
216
217 s->padding = s->chandelay[0].delay;
218 for (i = 1; i < s->nb_delays; i++) {
219 ChanDelay *d = &s->chandelay[i];
220
221 s->padding = FFMIN(s->padding, d->delay);
222 }
223
224 if (s->padding) {
225 for (i = 0; i < s->nb_delays; i++) {
226 ChanDelay *d = &s->chandelay[i];
227
228 d->delay -= s->padding;
229 }
230
231 s->offset = av_rescale_q(s->padding,
232 av_make_q(1, inlink->sample_rate),
233 inlink->time_base);
234 }
235
236 for (i = 0; i < s->nb_delays; i++) {
237 ChanDelay *d = &s->chandelay[i];
238
239 if (!d->delay)
240 continue;
241
242 if (d->delay > SIZE_MAX) {
243 av_log(ctx, AV_LOG_ERROR, "Requested delay is too big.\n");
244 return AVERROR(EINVAL);
245 }
246
247 d->samples = av_malloc_array(d->delay, s->block_align);
248 if (!d->samples)
249 return AVERROR(ENOMEM);
250 d->samples_size = d->delay * s->block_align;
251
252 s->max_delay = FFMAX(s->max_delay, d->delay);
253 }
254
255 switch (inlink->format) {
256 case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ;
257 s->resize_channel_samples = resize_samples_u8p; break;
258 case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p;
259 s->resize_channel_samples = resize_samples_s16p; break;
260 case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p;
261 s->resize_channel_samples = resize_samples_s32p; break;
262 case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp;
263 s->resize_channel_samples = resize_samples_fltp; break;
264 case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp;
265 s->resize_channel_samples = resize_samples_dblp; break;
266 }
267
268 return 0;
269}
270
271static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
272 char *res, int res_len, int flags)
273{
274 int ret = AVERROR(ENOSYS);
275 AVFilterLink *inlink = ctx->inputs[0];
276 AudioDelayContext *s = ctx->priv;
277
278 if (!strcmp(cmd, "delays")) {
279 int64_t delay;
280 char *p, *saveptr = NULL;
281 int64_t all_delay = -1;
282 int64_t max_delay = 0;
283 char *args_cpy = av_strdup(args);
284 if (args_cpy == NULL) {
285 return AVERROR(ENOMEM);
286 }
287
288 ret = 0;
289 p = args_cpy;
290
291 if (!strncmp(args, "all:", 4)) {
292 p = &args_cpy[4];
293 ret = parse_delays(p, &saveptr, &all_delay, ctx, inlink->sample_rate);
294 if (ret == 1)
295 ret = AVERROR(EINVAL);
296 else if (ret == 0)
297 delay = all_delay;
298 }
299
300 if (!ret) {
301 for (int i = 0; i < s->nb_delays; i++) {
302 ChanDelay *d = &s->chandelay[i];
303
304 if (all_delay < 0) {
305 ret = parse_delays(p, &saveptr, &delay, ctx, inlink->sample_rate);
306 if (ret != 0) {
307 ret = 0;
308 break;
309 }
310 p = NULL;
311 }
312
313 ret = s->resize_channel_samples(d, delay);
314 if (ret)
315 break;
316 max_delay = FFMAX(max_delay, d->delay);
317 }
318 s->max_delay = FFMAX(s->max_delay, max_delay);
319 }
320 av_freep(&args_cpy);
321 }
322 return ret;
323}
324
326{
327 AVFilterContext *ctx = inlink->dst;
328 AVFilterLink *outlink = ctx->outputs[0];
329 AudioDelayContext *s = ctx->priv;
330 AVFrame *out_frame;
331 int i;
332
333 if (ctx->is_disabled || !s->delays) {
334 s->input = NULL;
335 return ff_filter_frame(outlink, frame);
336 }
337
338 s->next_pts = av_rescale_q(frame->pts, inlink->time_base, outlink->time_base);
339
340 out_frame = ff_get_audio_buffer(outlink, frame->nb_samples);
341 if (!out_frame) {
342 s->input = NULL;
344 return AVERROR(ENOMEM);
345 }
346 av_frame_copy_props(out_frame, frame);
347
348 for (i = 0; i < s->nb_delays; i++) {
349 ChanDelay *d = &s->chandelay[i];
350 const uint8_t *src = frame->extended_data[i];
351 uint8_t *dst = out_frame->extended_data[i];
352
353 if (!d->delay)
354 memcpy(dst, src, frame->nb_samples * s->block_align);
355 else
356 s->delay_channel(d, frame->nb_samples, src, dst);
357 }
358
359 out_frame->pts = s->next_pts + s->offset;
360 out_frame->duration = av_rescale_q(out_frame->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
361 s->next_pts += out_frame->duration;
363 s->input = NULL;
364 return ff_filter_frame(outlink, out_frame);
365}
366
368{
369 AVFilterLink *inlink = ctx->inputs[0];
370 AVFilterLink *outlink = ctx->outputs[0];
371 AudioDelayContext *s = ctx->priv;
372 AVFrame *frame = NULL;
373 int ret, status;
374 int64_t pts;
375
376 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
377
378 if (!s->input) {
379 ret = ff_inlink_consume_frame(inlink, &s->input);
380 if (ret < 0)
381 return ret;
382 }
383
384 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
385 if (status == AVERROR_EOF)
386 s->eof = 1;
387 }
388
389 if (s->next_pts == AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE)
390 s->next_pts = av_rescale_q(pts, inlink->time_base, outlink->time_base);
391
392 if (s->padding) {
393 int nb_samples = FFMIN(s->padding, 2048);
394
395 frame = ff_get_audio_buffer(outlink, nb_samples);
396 if (!frame)
397 return AVERROR(ENOMEM);
398 s->padding -= nb_samples;
399
400 av_samples_set_silence(frame->extended_data, 0,
401 frame->nb_samples,
402 outlink->ch_layout.nb_channels,
403 frame->format);
404
405 frame->duration = av_rescale_q(frame->nb_samples,
406 (AVRational){1, outlink->sample_rate},
407 outlink->time_base);
408 frame->pts = s->next_pts;
409 s->next_pts += frame->duration;
410
411 return ff_filter_frame(outlink, frame);
412 }
413
414 if (s->input)
415 return filter_frame(inlink, s->input);
416
417 if (s->eof && s->max_delay) {
418 int nb_samples = FFMIN(s->max_delay, 2048);
419
420 frame = ff_get_audio_buffer(outlink, nb_samples);
421 if (!frame)
422 return AVERROR(ENOMEM);
423 s->max_delay -= nb_samples;
424
425 av_samples_set_silence(frame->extended_data, 0,
426 frame->nb_samples,
427 outlink->ch_layout.nb_channels,
428 frame->format);
429
430 frame->duration = av_rescale_q(frame->nb_samples,
431 (AVRational){1, outlink->sample_rate},
432 outlink->time_base);
433 frame->pts = s->next_pts;
434 s->next_pts += frame->duration;
435 return filter_frame(inlink, frame);
436 }
437
438 if (s->eof && s->max_delay == 0) {
439 ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
440 return 0;
441 }
442
443 if (!s->eof)
444 FF_FILTER_FORWARD_WANTED(outlink, inlink);
445
446 return FFERROR_NOT_READY;
447}
448
450{
451 AudioDelayContext *s = ctx->priv;
452
453 if (s->chandelay) {
454 for (int i = 0; i < s->nb_delays; i++)
455 av_freep(&s->chandelay[i].samples);
456 }
457 av_freep(&s->chandelay);
458}
459
460static const AVFilterPad adelay_inputs[] = {
461 {
462 .name = "default",
463 .type = AVMEDIA_TYPE_AUDIO,
464 .config_props = config_input,
465 },
466};
467
469 .p.name = "adelay",
470 .p.description = NULL_IF_CONFIG_SMALL("Delay one or more audio channels."),
471 .p.priv_class = &adelay_class,
473 .priv_size = sizeof(AudioDelayContext),
475 .uninit = uninit,
480 .process_command = process_command,
481};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int config_input(AVFilterLink *inlink)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
#define DELAY(name, type, fill)
Definition af_adelay.c:68
static const AVOption adelay_options[]
Definition af_adelay.c:60
static const AVFilterPad adelay_inputs[]
Definition af_adelay.c:460
const FFFilter ff_af_adelay
Definition af_adelay.c:468
static int config_input(AVFilterLink *inlink)
Definition af_adelay.c:185
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition af_adelay.c:325
#define CHANGE_DELAY(name, type, fill)
Definition af_adelay.c:103
static int parse_delays(char *p, char **saveptr, int64_t *result, AVFilterContext *ctx, int sample_rate)
Definition af_adelay.c:159
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition af_adelay.c:271
static int activate(AVFilterContext *ctx)
Definition af_adelay.c:367
static av_cold void uninit(AVFilterContext *ctx)
Definition af_adelay.c:449
#define OFFSET(x)
Definition af_adelay.c:57
#define A(x)
Definition vpx_arith.h:28
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
int32_t
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition avfilter.c:1467
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition avfilter.c:1520
Main libavfilter public API header.
int av_sscanf(const char *string, const char *format,...)
Definition avsscanf.c:962
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVFrame * frame
#define AV_OPT_FLAG_RUNTIME_PARAM
A generic parameter which can be set by the user at runtime.
Definition opt.h:376
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition avfilter.h:204
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition rational.h:71
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition samplefmt.c:108
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition samplefmt.h:64
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition samplefmt.h:63
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition samplefmt.h:65
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition samplefmt.h:67
int av_samples_set_silence(uint8_t *const *audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition samplefmt.c:246
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition avstring.c:179
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
cl_device_type type
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_SAMPLEFMTS(...)
Definition filters.h:252
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition filters.h:694
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition filters.h:629
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition filters.h:639
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
#define av_strdup(s)
Definition ops_static.c:55
AVOptions.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
int64_t duration
Duration of the frame, in the same units as pts.
Definition frame.h:820
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:533
AVOption.
Definition opt.h:428
Rational number (pair of numerator and denominator).
Definition rational.h:58
ChanDelay * chandelay
Definition af_adelay.c:41
int(* resize_channel_samples)(ChanDelay *d, int64_t new_delay)
Definition af_adelay.c:54
AVFrame * input
Definition af_adelay.c:50
int64_t max_delay
Definition af_adelay.c:45
void(* delay_channel)(ChanDelay *d, int nb_samples, const uint8_t *src, uint8_t *dst)
Definition af_adelay.c:52
size_t delay_index
Definition af_adelay.c:31
int64_t delay
Definition af_adelay.c:30
size_t index
Definition af_adelay.c:32
unsigned int samples_size
Definition af_adelay.c:33
uint8_t * samples
Definition af_adelay.c:34
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static AVFormatContext * ctx
Definition movenc.c:49
static int64_t pts