FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_adelay.c
Date: 2026-09-18 15:12:08
Exec Total Coverage
Lines: 111 202 55.0%
Functions: 6 16 37.5%
Branches: 63 220 28.6%

Line Branch Exec Source
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
29 typedef struct ChanDelay {
30 int64_t delay;
31 size_t delay_index;
32 size_t index;
33 unsigned int samples_size;
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 offset;
47 int64_t next_pts;
48 int eof;
49
50 AVFrame *input;
51
52 void (*delay_channel)(ChanDelay *d, int nb_samples,
53 const uint8_t *src, uint8_t *dst);
54 int (*resize_channel_samples)(ChanDelay *d, int64_t new_delay);
55 } AudioDelayContext;
56
57 #define OFFSET(x) offsetof(AudioDelayContext, x)
58 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
59
60 static 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
66 AVFILTER_DEFINE_CLASS(adelay);
67
68 #define DELAY(name, type, fill) \
69 static 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
97 DELAY(u8, uint8_t, 0x80)
98
6/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 264600 times.
✓ Branch 2 taken 264458 times.
✓ Branch 3 taken 142 times.
✓ Branch 4 taken 264601 times.
✓ Branch 5 taken 66 times.
264667 DELAY(s16, int16_t, 0)
99 DELAY(s32, int32_t, 0)
100 DELAY(flt, float, 0)
101 DELAY(dbl, double, 0)
102
103 #define CHANGE_DELAY(name, type, fill) \
104 static 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
153 CHANGE_DELAY(u8, uint8_t, 0x80)
154 CHANGE_DELAY(s16, int16_t, 0)
155 CHANGE_DELAY(s32, int32_t, 0)
156 CHANGE_DELAY(flt, float, 0)
157 CHANGE_DELAY(dbl, double, 0)
158
159 2 static int parse_delays(char *p, char **saveptr, int64_t *result, AVFilterContext *ctx, int sample_rate) {
160 double delay, div;
161 char *arg;
162 char suffix;
163
164
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 if (!(arg = av_strtok(p, "|", saveptr)))
165 1 return 1;
166
167 1 suffix = arg[strlen(arg) - 1];
168
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (suffix != 'S') {
169 double delay_samples;
170
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 div = suffix == 's' ? 1.0 : 1000.0;
172
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (av_sscanf(arg, "%lf", &delay) != 1) {
173 av_log(ctx, AV_LOG_ERROR, "Invalid syntax for delay.\n");
174 return AVERROR(EINVAL);
175 }
176 1 delay_samples = delay * sample_rate / div;
177
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if (!isfinite(delay_samples) || delay_samples < -0x1p63 || delay_samples >= 0x1p63) {
178 av_log(ctx, AV_LOG_ERROR, "Delay is out of range.\n");
179 return AVERROR(EINVAL);
180 }
181 1 *result = delay_samples;
182 } else {
183 char type;
184
185 if (av_sscanf(arg, "%"SCNd64"%c", result, &type) != 2 || type != 'S') {
186 av_log(ctx, AV_LOG_ERROR, "Invalid syntax for delay.\n");
187 return AVERROR(EINVAL);
188 }
189 }
190
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (*result < 0) {
192 av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n");
193 return AVERROR(EINVAL);
194 }
195 1 return 0;
196 }
197
198 1 static int config_input(AVFilterLink *inlink)
199 {
200 1 AVFilterContext *ctx = inlink->dst;
201 1 AudioDelayContext *s = ctx->priv;
202 1 char *p, *saveptr = NULL;
203 int i;
204
205 1 s->next_pts = AV_NOPTS_VALUE;
206 1 s->chandelay = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->chandelay));
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->chandelay)
208 return AVERROR(ENOMEM);
209 1 s->nb_delays = inlink->ch_layout.nb_channels;
210 1 s->block_align = av_get_bytes_per_sample(inlink->format);
211
212 1 p = s->delays;
213
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 for (i = 0; i < s->nb_delays; i++) {
214 2 ChanDelay *d = &s->chandelay[i];
215 int ret;
216
217 2 ret = parse_delays(p, &saveptr, &d->delay, ctx, inlink->sample_rate);
218
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (ret == 1)
219 1 break;
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 else if (ret < 0)
221 return ret;
222 1 p = NULL;
223 }
224
225
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (s->all && i) {
226 for (int j = i; j < s->nb_delays; j++)
227 s->chandelay[j].delay = s->chandelay[i-1].delay;
228 }
229
230 1 s->padding = s->chandelay[0].delay;
231
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (i = 1; i < s->nb_delays; i++) {
232 1 ChanDelay *d = &s->chandelay[i];
233
234 1 s->padding = FFMIN(s->padding, d->delay);
235 }
236
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (s->padding) {
238 for (i = 0; i < s->nb_delays; i++) {
239 ChanDelay *d = &s->chandelay[i];
240
241 d->delay -= s->padding;
242 }
243
244 s->offset = av_rescale_q(s->padding,
245 av_make_q(1, inlink->sample_rate),
246 inlink->time_base);
247 }
248
249
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (i = 0; i < s->nb_delays; i++) {
250 2 ChanDelay *d = &s->chandelay[i];
251
252
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!d->delay)
253 1 continue;
254
255 if (d->delay > SIZE_MAX) {
256 av_log(ctx, AV_LOG_ERROR, "Requested delay is too big.\n");
257 return AVERROR(EINVAL);
258 }
259
260 1 d->samples = av_malloc_array(d->delay, s->block_align);
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!d->samples)
262 return AVERROR(ENOMEM);
263 1 d->samples_size = d->delay * s->block_align;
264
265 1 s->max_delay = FFMAX(s->max_delay, d->delay);
266 }
267
268
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1 switch (inlink->format) {
269 case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ;
270 s->resize_channel_samples = resize_samples_u8p; break;
271 1 case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p;
272 1 s->resize_channel_samples = resize_samples_s16p; break;
273 case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p;
274 s->resize_channel_samples = resize_samples_s32p; break;
275 case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp;
276 s->resize_channel_samples = resize_samples_fltp; break;
277 case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp;
278 s->resize_channel_samples = resize_samples_dblp; break;
279 }
280
281 1 return 0;
282 }
283
284 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
285 char *res, int res_len, int flags)
286 {
287 int ret = AVERROR(ENOSYS);
288 AVFilterLink *inlink = ctx->inputs[0];
289 AudioDelayContext *s = ctx->priv;
290
291 if (!strcmp(cmd, "delays")) {
292 int64_t delay;
293 char *p, *saveptr = NULL;
294 int64_t all_delay = -1;
295 int64_t max_delay = 0;
296 char *args_cpy = av_strdup(args);
297 if (args_cpy == NULL) {
298 return AVERROR(ENOMEM);
299 }
300
301 ret = 0;
302 p = args_cpy;
303
304 if (!strncmp(args, "all:", 4)) {
305 p = &args_cpy[4];
306 ret = parse_delays(p, &saveptr, &all_delay, ctx, inlink->sample_rate);
307 if (ret == 1)
308 ret = AVERROR(EINVAL);
309 else if (ret == 0)
310 delay = all_delay;
311 }
312
313 if (!ret) {
314 for (int i = 0; i < s->nb_delays; i++) {
315 ChanDelay *d = &s->chandelay[i];
316
317 if (all_delay < 0) {
318 ret = parse_delays(p, &saveptr, &delay, ctx, inlink->sample_rate);
319 if (ret != 0) {
320 ret = 0;
321 break;
322 }
323 p = NULL;
324 }
325
326 ret = s->resize_channel_samples(d, delay);
327 if (ret)
328 break;
329 max_delay = FFMAX(max_delay, d->delay);
330 }
331 s->max_delay = FFMAX(s->max_delay, max_delay);
332 }
333 av_freep(&args_cpy);
334 }
335 return ret;
336 }
337
338 66 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
339 {
340 66 AVFilterContext *ctx = inlink->dst;
341 66 AVFilterLink *outlink = ctx->outputs[0];
342 66 AudioDelayContext *s = ctx->priv;
343 AVFrame *out_frame;
344 int i;
345
346
2/4
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 66 times.
66 if (ctx->is_disabled || !s->delays) {
347 s->input = NULL;
348 return ff_filter_frame(outlink, frame);
349 }
350
351 66 s->next_pts = av_rescale_q(frame->pts, inlink->time_base, outlink->time_base);
352
353 66 out_frame = ff_get_audio_buffer(outlink, frame->nb_samples);
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (!out_frame) {
355 s->input = NULL;
356 av_frame_free(&frame);
357 return AVERROR(ENOMEM);
358 }
359 66 av_frame_copy_props(out_frame, frame);
360
361
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 66 times.
198 for (i = 0; i < s->nb_delays; i++) {
362 132 ChanDelay *d = &s->chandelay[i];
363 132 const uint8_t *src = frame->extended_data[i];
364 132 uint8_t *dst = out_frame->extended_data[i];
365
366
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 66 times.
132 if (!d->delay)
367 66 memcpy(dst, src, frame->nb_samples * s->block_align);
368 else
369 66 s->delay_channel(d, frame->nb_samples, src, dst);
370 }
371
372 66 out_frame->pts = s->next_pts + s->offset;
373 66 out_frame->duration = av_rescale_q(out_frame->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
374 66 s->next_pts += out_frame->duration;
375 66 av_frame_free(&frame);
376 66 s->input = NULL;
377 66 return ff_filter_frame(outlink, out_frame);
378 }
379
380 132 static int activate(AVFilterContext *ctx)
381 {
382 132 AVFilterLink *inlink = ctx->inputs[0];
383 132 AVFilterLink *outlink = ctx->outputs[0];
384 132 AudioDelayContext *s = ctx->priv;
385 132 AVFrame *frame = NULL;
386 int ret, status;
387 int64_t pts;
388
389
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 132 times.
132 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
390
391
1/2
✓ Branch 0 taken 132 times.
✗ Branch 1 not taken.
132 if (!s->input) {
392 132 ret = ff_inlink_consume_frame(inlink, &s->input);
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 if (ret < 0)
394 return ret;
395 }
396
397
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 130 times.
132 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
398
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (status == AVERROR_EOF)
399 2 s->eof = 1;
400 }
401
402
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 131 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
132 if (s->next_pts == AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE)
403 1 s->next_pts = av_rescale_q(pts, inlink->time_base, outlink->time_base);
404
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 if (s->padding) {
406 int nb_samples = FFMIN(s->padding, 2048);
407
408 frame = ff_get_audio_buffer(outlink, nb_samples);
409 if (!frame)
410 return AVERROR(ENOMEM);
411 s->padding -= nb_samples;
412
413 av_samples_set_silence(frame->extended_data, 0,
414 frame->nb_samples,
415 outlink->ch_layout.nb_channels,
416 frame->format);
417
418 frame->duration = av_rescale_q(frame->nb_samples,
419 (AVRational){1, outlink->sample_rate},
420 outlink->time_base);
421 frame->pts = s->next_pts;
422 s->next_pts += frame->duration;
423
424 return ff_filter_frame(outlink, frame);
425 }
426
427
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 67 times.
132 if (s->input)
428 65 return filter_frame(inlink, s->input);
429
430
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
67 if (s->eof && s->max_delay) {
431 1 int nb_samples = FFMIN(s->max_delay, 2048);
432
433 1 frame = ff_get_audio_buffer(outlink, nb_samples);
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!frame)
435 return AVERROR(ENOMEM);
436 1 s->max_delay -= nb_samples;
437
438 1 av_samples_set_silence(frame->extended_data, 0,
439 frame->nb_samples,
440 outlink->ch_layout.nb_channels,
441 1 frame->format);
442
443 1 frame->duration = av_rescale_q(frame->nb_samples,
444 1 (AVRational){1, outlink->sample_rate},
445 outlink->time_base);
446 1 frame->pts = s->next_pts;
447 1 s->next_pts += frame->duration;
448 1 return filter_frame(inlink, frame);
449 }
450
451
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
66 if (s->eof && s->max_delay == 0) {
452 1 ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
453 1 return 0;
454 }
455
456
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 if (!s->eof)
457
1/2
✓ Branch 1 taken 65 times.
✗ Branch 2 not taken.
65 FF_FILTER_FORWARD_WANTED(outlink, inlink);
458
459 return FFERROR_NOT_READY;
460 }
461
462 2 static av_cold void uninit(AVFilterContext *ctx)
463 {
464 2 AudioDelayContext *s = ctx->priv;
465
466
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (s->chandelay) {
467
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i = 0; i < s->nb_delays; i++)
468 2 av_freep(&s->chandelay[i].samples);
469 }
470 2 av_freep(&s->chandelay);
471 2 }
472
473 static const AVFilterPad adelay_inputs[] = {
474 {
475 .name = "default",
476 .type = AVMEDIA_TYPE_AUDIO,
477 .config_props = config_input,
478 },
479 };
480
481 const FFFilter ff_af_adelay = {
482 .p.name = "adelay",
483 .p.description = NULL_IF_CONFIG_SMALL("Delay one or more audio channels."),
484 .p.priv_class = &adelay_class,
485 .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
486 .priv_size = sizeof(AudioDelayContext),
487 .activate = activate,
488 .uninit = uninit,
489 FILTER_INPUTS(adelay_inputs),
490 FILTER_OUTPUTS(ff_audio_default_filterpad),
491 FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
492 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP),
493 .process_command = process_command,
494 };
495