FFmpeg
Loading...
Searching...
No Matches
af_adeclick.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 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
22#include "libavutil/mem.h"
23#include "libavutil/opt.h"
24#include "libavutil/tx.h"
25#include "avfilter.h"
26#include "audio.h"
27#include "filters.h"
28
29typedef struct DeclickChannel {
30 double *auxiliary;
31 double *detection;
33 double *acorrelation;
34 double *tmp;
35 double *interpolated;
36 double *matrix;
38 double *vector;
40 double *y;
41 int y_size;
42 uint8_t *click;
43 int *index;
44 unsigned *histogram;
47
48typedef struct AudioDeclickContext {
49 const AVClass *class;
50
51 double w;
52 double overlap;
53 double threshold;
54 double ar;
55 double burst;
56 int method;
58
65
71
73
76 uint64_t nb_samples;
79 int eof;
80
84
86 double sigmae, double *detection,
87 double *acoefficients, uint8_t *click, int *index,
88 const double *src, double *dst);
90
91#define OFFSET(x) offsetof(AudioDeclickContext, x)
92#define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
93
94static const AVOption adeclick_options[] = {
95 { "window", "set window size", OFFSET(w), AV_OPT_TYPE_DOUBLE, {.dbl=55}, 10, 100, AF },
96 { "w", "set window size", OFFSET(w), AV_OPT_TYPE_DOUBLE, {.dbl=55}, 10, 100, AF },
97 { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_DOUBLE, {.dbl=75}, 50, 95, AF },
98 { "o", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_DOUBLE, {.dbl=75}, 50, 95, AF },
99 { "arorder", "set autoregression order", OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, 25, AF },
100 { "a", "set autoregression order", OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, 25, AF },
101 { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 100, AF },
102 { "t", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 100, AF },
103 { "burst", "set burst fusion", OFFSET(burst), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, 10, AF },
104 { "b", "set burst fusion", OFFSET(burst), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, 10, AF },
105 { "method", "set overlap method", OFFSET(method), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AF, .unit = "m" },
106 { "m", "set overlap method", OFFSET(method), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AF, .unit = "m" },
107 { "add", "overlap-add", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "m" },
108 { "a", "overlap-add", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "m" },
109 { "save", "overlap-save", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "m" },
110 { "s", "overlap-save", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "m" },
111 { NULL }
112};
113
115
116static int config_input(AVFilterLink *inlink)
117{
118 AVFilterContext *ctx = inlink->dst;
119 AudioDeclickContext *s = ctx->priv;
120 int i;
121
122 s->pts = AV_NOPTS_VALUE;
123 s->window_size = FFMAX(100, inlink->sample_rate * s->w / 1000.);
124 s->ar_order = FFMAX(s->window_size * s->ar / 100., 1);
125 s->nb_burst_samples = s->window_size * s->burst / 1000.;
126 s->hop_size = FFMAX(1, s->window_size * (1. - (s->overlap / 100.)));
127
128 s->window_func_lut = av_calloc(s->window_size, sizeof(*s->window_func_lut));
129 if (!s->window_func_lut)
130 return AVERROR(ENOMEM);
131
132 {
133 double *tx_in[2] = { NULL }, *tx_out[2] = { NULL };
134 AVTXContext *tx = NULL, *itx = NULL;
135 av_tx_fn tx_fn, itx_fn;
136 int ret, tx_size;
137 double scale;
138
139 tx_size = 1 << (32 - ff_clz(s->window_size));
140
141 scale = 1.0;
142 ret = av_tx_init(&tx, &tx_fn, AV_TX_DOUBLE_RDFT, 0, tx_size, &scale, 0);
143 if (ret < 0)
144 goto tx_end;
145
146 scale = 1.0 / tx_size;
147 ret = av_tx_init(&itx, &itx_fn, AV_TX_DOUBLE_RDFT, 1, tx_size, &scale, 0);
148 if (ret < 0)
149 goto tx_end;
150
151 tx_in[0] = av_calloc(tx_size + 2, sizeof(*tx_in[0]));
152 tx_in[1] = av_calloc(tx_size + 2, sizeof(*tx_in[1]));
153 tx_out[0] = av_calloc(tx_size + 2, sizeof(*tx_out[0]));
154 tx_out[1] = av_calloc(tx_size + 2, sizeof(*tx_out[1]));
155 if (!tx_in[0] || !tx_in[1] || !tx_out[0] || !tx_out[1]) {
156 ret = AVERROR(ENOMEM);
157 goto tx_end;
158 }
159
160 for (int n = 0; n < s->window_size - s->hop_size; n++)
161 tx_in[0][n] = 1.0;
162
163 for (int n = 0; n < s->hop_size; n++)
164 tx_in[1][n] = 1.0;
165
166 tx_fn(tx, tx_out[0], tx_in[0], sizeof(double));
167 tx_fn(tx, tx_out[1], tx_in[1], sizeof(double));
168
169 for (int n = 0; n <= tx_size/2; n++) {
170 double re0 = tx_out[0][2*n];
171 double im0 = tx_out[0][2*n+1];
172 double re1 = tx_out[1][2*n];
173 double im1 = tx_out[1][2*n+1];
174
175 tx_in[0][2*n] = re0 * re1 - im0 * im1;
176 tx_in[0][2*n+1] = re0 * im1 + re1 * im0;
177 }
178
179 itx_fn(itx, tx_out[0], tx_in[0], sizeof(AVComplexDouble));
180
181 scale = 1.0 / (s->window_size - s->hop_size);
182 for (int n = 0; n < s->window_size; n++)
183 s->window_func_lut[n] = tx_out[0][n] * scale;
184
185tx_end:
186 av_tx_uninit(&tx);
187 av_tx_uninit(&itx);
188
189 av_freep(&tx_in[0]);
190 av_freep(&tx_in[1]);
191 av_freep(&tx_out[0]);
192 av_freep(&tx_out[1]);
193
194 if (ret < 0)
195 return ret;
196 }
197
198 av_frame_free(&s->in);
199 av_frame_free(&s->out);
200 av_frame_free(&s->buffer);
201 av_frame_free(&s->is);
202 s->enabled = ff_get_audio_buffer(inlink, s->window_size);
203 s->in = ff_get_audio_buffer(inlink, s->window_size);
204 s->out = ff_get_audio_buffer(inlink, s->window_size);
205 s->buffer = ff_get_audio_buffer(inlink, s->window_size * 2);
206 s->is = ff_get_audio_buffer(inlink, s->window_size);
207 if (!s->in || !s->out || !s->buffer || !s->is || !s->enabled)
208 return AVERROR(ENOMEM);
209
210 s->efifo = av_audio_fifo_alloc(inlink->format, 1, s->window_size);
211 if (!s->efifo)
212 return AVERROR(ENOMEM);
213 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->ch_layout.nb_channels, s->window_size);
214 if (!s->fifo)
215 return AVERROR(ENOMEM);
216 s->overlap_skip = s->method ? (s->window_size - s->hop_size) / 2 : 0;
217 if (s->overlap_skip > 0) {
218 av_audio_fifo_write(s->fifo, (void **)s->in->extended_data,
219 s->overlap_skip);
220 }
221
222 s->nb_channels = inlink->ch_layout.nb_channels;
223 s->chan = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->chan));
224 if (!s->chan)
225 return AVERROR(ENOMEM);
226
227 for (i = 0; i < inlink->ch_layout.nb_channels; i++) {
228 DeclickChannel *c = &s->chan[i];
229
230 c->detection = av_calloc(s->window_size, sizeof(*c->detection));
231 c->auxiliary = av_calloc(s->ar_order + 1, sizeof(*c->auxiliary));
232 c->acoefficients = av_calloc(s->ar_order + 1, sizeof(*c->acoefficients));
233 c->acorrelation = av_calloc(s->ar_order + 1, sizeof(*c->acorrelation));
234 c->tmp = av_calloc(s->ar_order, sizeof(*c->tmp));
235 c->click = av_calloc(s->window_size, sizeof(*c->click));
236 c->index = av_calloc(s->window_size, sizeof(*c->index));
237 c->interpolated = av_calloc(s->window_size, sizeof(*c->interpolated));
238 if (!c->auxiliary || !c->acoefficients || !c->detection || !c->click ||
239 !c->index || !c->interpolated || !c->acorrelation || !c->tmp)
240 return AVERROR(ENOMEM);
241 }
242
243 return 0;
244}
245
246static void autocorrelation(const double *input, int order, int size,
247 double *output, double scale)
248{
249 int i, j;
250
251 for (i = 0; i <= order; i++) {
252 double value = 0.;
253
254 for (j = i; j < size; j++)
255 value += input[j] * input[j - i];
256
257 output[i] = value * scale;
258 }
259}
260
261static double autoregression(const double *samples, int ar_order,
262 int nb_samples, double *k, double *r, double *a)
263{
264 double alpha;
265 int i, j;
266
267 memset(a, 0, ar_order * sizeof(*a));
268
270
271 /* Levinson-Durbin algorithm */
272 k[0] = a[0] = -r[1] / r[0];
273 alpha = r[0] * (1. - k[0] * k[0]);
274 for (i = 1; i < ar_order; i++) {
275 double epsilon = 0.;
276
277 for (j = 0; j < i; j++)
278 epsilon += a[j] * r[i - j];
279 epsilon += r[i + 1];
280
281 k[i] = -epsilon / alpha;
282 alpha *= (1. - k[i] * k[i]);
283 for (j = i - 1; j >= 0; j--)
284 k[j] = a[j] + k[i] * a[i - j - 1];
285 for (j = 0; j <= i; j++)
286 a[j] = k[j];
287 }
288
289 k[0] = 1.;
290 for (i = 1; i <= ar_order; i++)
291 k[i] = a[i - 1];
292
293 return sqrt(alpha);
294}
295
296static int isfinite_array(double *samples, int nb_samples)
297{
298 int i;
299
300 for (i = 0; i < nb_samples; i++)
301 if (!isfinite(samples[i]))
302 return 0;
303
304 return 1;
305}
306
307static int find_index(int *index, int value, int size)
308{
309 int i, start, end;
310
311 if ((value < index[0]) || (value > index[size - 1]))
312 return 1;
313
314 i = start = 0;
315 end = size - 1;
316
317 while (start <= end) {
318 i = (end + start) / 2;
319 if (index[i] == value)
320 return 0;
321 if (value < index[i])
322 end = i - 1;
323 if (value > index[i])
324 start = i + 1;
325 }
326
327 return 1;
328}
329
330static int factorization(double *matrix, int n)
331{
332 int i, j, k;
333
334 for (i = 0; i < n; i++) {
335 const int in = i * n;
336 double value;
337
338 value = matrix[in + i];
339 for (j = 0; j < i; j++)
340 value -= matrix[j * n + j] * matrix[in + j] * matrix[in + j];
341
342 if (value == 0.) {
343 return -1;
344 }
345
346 matrix[in + i] = value;
347 for (j = i + 1; j < n; j++) {
348 const int jn = j * n;
349 double x;
350
351 x = matrix[jn + i];
352 for (k = 0; k < i; k++)
353 x -= matrix[k * n + k] * matrix[in + k] * matrix[jn + k];
354 matrix[jn + i] = x / matrix[in + i];
355 }
356 }
357
358 return 0;
359}
360
362 double *vector, int n, double *out)
363{
364 int i, j, ret;
365 double *y;
366
367 ret = factorization(matrix, n);
368 if (ret < 0)
369 return ret;
370
371 av_fast_malloc(&c->y, &c->y_size, n * sizeof(*c->y));
372 y = c->y;
373 if (!y)
374 return AVERROR(ENOMEM);
375
376 for (i = 0; i < n; i++) {
377 const int in = i * n;
378 double value;
379
380 value = vector[i];
381 for (j = 0; j < i; j++)
382 value -= matrix[in + j] * y[j];
383 y[i] = value;
384 }
385
386 for (i = n - 1; i >= 0; i--) {
387 out[i] = y[i] / matrix[i * n + i];
388 for (j = i + 1; j < n; j++)
389 out[i] -= matrix[j * n + i] * out[j];
390 }
391
392 return 0;
393}
394
395static int interpolation(DeclickChannel *c, const double *src, int ar_order,
396 double *acoefficients, int *index, int nb_errors,
397 double *auxiliary, double *interpolated)
398{
399 double *vector, *matrix;
400 int i, j;
401
402 av_fast_malloc(&c->matrix, &c->matrix_size, nb_errors * nb_errors * sizeof(*c->matrix));
403 matrix = c->matrix;
404 if (!matrix)
405 return AVERROR(ENOMEM);
406
407 av_fast_malloc(&c->vector, &c->vector_size, nb_errors * sizeof(*c->vector));
408 vector = c->vector;
409 if (!vector)
410 return AVERROR(ENOMEM);
411
412 autocorrelation(acoefficients, ar_order, ar_order + 1, auxiliary, 1.);
413
414 for (i = 0; i < nb_errors; i++) {
415 const int im = i * nb_errors;
416
417 for (j = i; j < nb_errors; j++) {
418 if (abs(index[j] - index[i]) <= ar_order) {
419 matrix[j * nb_errors + i] = matrix[im + j] = auxiliary[abs(index[j] - index[i])];
420 } else {
421 matrix[j * nb_errors + i] = matrix[im + j] = 0;
422 }
423 }
424 }
425
426 for (i = 0; i < nb_errors; i++) {
427 double value = 0.;
428
429 for (j = -ar_order; j <= ar_order; j++)
430 if (find_index(index, index[i] - j, nb_errors))
431 value -= src[index[i] - j] * auxiliary[abs(j)];
432
433 vector[i] = value;
434 }
435
436 return do_interpolation(c, matrix, vector, nb_errors, interpolated);
437}
438
440 double unused0,
441 double *unused1, double *unused2,
442 uint8_t *clip, int *index,
443 const double *src, double *dst)
444{
445 const double threshold = s->threshold;
446 double max_amplitude = 0;
447 unsigned *histogram;
448 int i, nb_clips = 0;
449
450 av_fast_malloc(&c->histogram, &c->histogram_size, s->nb_hbins * sizeof(*c->histogram));
451 if (!c->histogram)
452 return AVERROR(ENOMEM);
453 histogram = c->histogram;
454 memset(histogram, 0, sizeof(*histogram) * s->nb_hbins);
455
456 for (i = 0; i < s->window_size; i++) {
457 const unsigned index = fmin(fabs(src[i]), 1) * (s->nb_hbins - 1);
458
459 histogram[index]++;
460 dst[i] = src[i];
461 clip[i] = 0;
462 }
463
464 for (i = s->nb_hbins - 1; i > 1; i--) {
465 if (histogram[i]) {
466 if (histogram[i] / (double)FFMAX(histogram[i - 1], 1) > threshold) {
467 max_amplitude = i / (double)s->nb_hbins;
468 }
469 break;
470 }
471 }
472
473 if (max_amplitude > 0.) {
474 for (i = 0; i < s->window_size; i++) {
475 clip[i] = fabs(src[i]) >= max_amplitude;
476 }
477 }
478
479 memset(clip, 0, s->ar_order * sizeof(*clip));
480 memset(clip + (s->window_size - s->ar_order), 0, s->ar_order * sizeof(*clip));
481
482 for (i = s->ar_order; i < s->window_size - s->ar_order; i++)
483 if (clip[i])
484 index[nb_clips++] = i;
485
486 return nb_clips;
487}
488
490 double sigmae,
491 double *detection, double *acoefficients,
492 uint8_t *click, int *index,
493 const double *src, double *dst)
494{
495 const double threshold = s->threshold;
496 int i, j, nb_clicks = 0, prev = -1;
497
498 memset(detection, 0, s->window_size * sizeof(*detection));
499
500 for (i = s->ar_order; i < s->window_size; i++) {
501 for (j = 0; j <= s->ar_order; j++) {
502 detection[i] += acoefficients[j] * src[i - j];
503 }
504 }
505
506 for (i = 0; i < s->window_size; i++) {
507 click[i] = fabs(detection[i]) > sigmae * threshold;
508 dst[i] = src[i];
509 }
510
511 for (i = 0; i < s->window_size; i++) {
512 if (!click[i])
513 continue;
514
515 if (prev >= 0 && (i > prev + 1) && (i <= s->nb_burst_samples + prev))
516 for (j = prev + 1; j < i; j++)
517 click[j] = 1;
518 prev = i;
519 }
520
521 memset(click, 0, s->ar_order * sizeof(*click));
522 memset(click + (s->window_size - s->ar_order), 0, s->ar_order * sizeof(*click));
523
524 for (i = s->ar_order; i < s->window_size - s->ar_order; i++)
525 if (click[i])
526 index[nb_clicks++] = i;
527
528 return nb_clicks;
529}
530
531typedef struct ThreadData {
533} ThreadData;
534
535static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
536{
537 AudioDeclickContext *s = ctx->priv;
538 ThreadData *td = arg;
539 AVFrame *out = td->out;
540 const double *src = (const double *)s->in->extended_data[ch];
541 double *is = (double *)s->is->extended_data[ch];
542 double *dst = (double *)s->out->extended_data[ch];
543 double *ptr = (double *)out->extended_data[ch];
544 double *buf = (double *)s->buffer->extended_data[ch];
545 const double *w = s->window_func_lut;
546 DeclickChannel *c = &s->chan[ch];
547 double sigmae;
548 int j, ret;
549
550 sigmae = autoregression(src, s->ar_order, s->window_size, c->acoefficients, c->acorrelation, c->tmp);
551
552 if (isfinite_array(c->acoefficients, s->ar_order + 1)) {
553 double *interpolated = c->interpolated;
554 int *index = c->index;
555 int nb_errors;
556
557 nb_errors = s->detector(s, c, sigmae, c->detection, c->acoefficients,
558 c->click, index, src, dst);
559 if (nb_errors > 0) {
560 double *enabled = (double *)s->enabled->extended_data[0];
561
562 ret = interpolation(c, src, s->ar_order, c->acoefficients, index,
563 nb_errors, c->auxiliary, interpolated);
564 if (ret < 0)
565 return ret;
566
567 av_audio_fifo_peek(s->efifo, (void**)s->enabled->extended_data, s->window_size);
568
569 for (j = 0; j < nb_errors; j++) {
570 if (enabled[index[j]]) {
571 dst[index[j]] = interpolated[j];
572 is[index[j]] = 1;
573 }
574 }
575 }
576 } else {
577 memcpy(dst, src, s->window_size * sizeof(*dst));
578 }
579
580 if (s->method == 0) {
581 for (j = 0; j < s->window_size; j++)
582 buf[j] += dst[j] * w[j];
583 } else {
584 const int skip = s->overlap_skip;
585
586 for (j = 0; j < s->hop_size; j++)
587 buf[j] = dst[skip + j];
588 }
589 for (j = 0; j < s->hop_size; j++)
590 ptr[j] = buf[j];
591
592 memmove(buf, buf + s->hop_size, (s->window_size * 2 - s->hop_size) * sizeof(*buf));
593 memmove(is, is + s->hop_size, (s->window_size - s->hop_size) * sizeof(*is));
594 memset(buf + s->window_size * 2 - s->hop_size, 0, s->hop_size * sizeof(*buf));
595 memset(is + s->window_size - s->hop_size, 0, s->hop_size * sizeof(*is));
596
597 return 0;
598}
599
600static int filter_frame(AVFilterLink *inlink)
601{
602 AVFilterContext *ctx = inlink->dst;
603 AVFilterLink *outlink = ctx->outputs[0];
604 AudioDeclickContext *s = ctx->priv;
605 AVFrame *out = NULL;
606 int ret = 0, j, ch, detected_errors = 0;
607 ThreadData td;
608
609 out = ff_get_audio_buffer(outlink, s->hop_size);
610 if (!out)
611 return AVERROR(ENOMEM);
612
613 ret = av_audio_fifo_peek(s->fifo, (void **)s->in->extended_data,
614 s->window_size);
615 if (ret < 0)
616 goto fail;
617
618 td.out = out;
620 if (ret < 0)
621 goto fail;
622
623 for (ch = 0; ch < s->in->ch_layout.nb_channels; ch++) {
624 double *is = (double *)s->is->extended_data[ch];
625
626 for (j = 0; j < s->hop_size; j++) {
627 if (is[j])
628 detected_errors++;
629 }
630 }
631
632 av_audio_fifo_drain(s->fifo, s->hop_size);
633 av_audio_fifo_drain(s->efifo, s->hop_size);
634
635 if (s->samples_left > 0)
636 out->nb_samples = FFMIN(s->hop_size, s->samples_left);
637
638 out->pts = s->pts;
639 s->pts += av_rescale_q(s->hop_size, (AVRational){1, outlink->sample_rate}, outlink->time_base);
640
641 s->detected_errors += detected_errors;
642 s->nb_samples += out->nb_samples * inlink->ch_layout.nb_channels;
643
644 ret = ff_filter_frame(outlink, out);
645 if (ret < 0)
646 return ret;
647
648 if (s->samples_left > 0) {
649 s->samples_left -= s->hop_size;
650 if (s->samples_left <= 0)
652 }
653
654fail:
655 if (ret < 0)
657 return ret;
658}
659
661{
662 AVFilterLink *inlink = ctx->inputs[0];
663 AVFilterLink *outlink = ctx->outputs[0];
664 AudioDeclickContext *s = ctx->priv;
665 AVFrame *in;
666 int ret, status;
667 int64_t pts;
668
669 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
670
671 ret = ff_inlink_consume_samples(inlink, s->window_size, s->window_size, &in);
672 if (ret < 0)
673 return ret;
674 if (ret > 0) {
675 double *e = (double *)s->enabled->extended_data[0];
676
677 if (s->pts == AV_NOPTS_VALUE)
678 s->pts = in->pts;
679
680 ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
681 in->nb_samples);
682 for (int i = 0; i < in->nb_samples; i++)
683 e[i] = !ctx->is_disabled;
684
685 av_audio_fifo_write(s->efifo, (void**)s->enabled->extended_data, in->nb_samples);
686 av_frame_free(&in);
687 if (ret < 0)
688 return ret;
689 }
690
691 if (av_audio_fifo_size(s->fifo) >= s->window_size ||
692 s->samples_left > 0)
693 return filter_frame(inlink);
694
695 if (av_audio_fifo_size(s->fifo) >= s->window_size) {
697 return 0;
698 }
699
700 if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
701 if (status == AVERROR_EOF) {
702 s->eof = 1;
703 s->samples_left = av_audio_fifo_size(s->fifo) - s->overlap_skip;
705 return 0;
706 }
707 }
708
709 if (s->eof && s->samples_left <= 0) {
710 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
711 return 0;
712 }
713
714 if (!s->eof)
715 FF_FILTER_FORWARD_WANTED(outlink, inlink);
716
717 return FFERROR_NOT_READY;
718}
719
721{
722 AudioDeclickContext *s = ctx->priv;
723
724 s->is_declip = !strcmp(ctx->filter->name, "adeclip");
725 if (s->is_declip) {
726 s->detector = detect_clips;
727 } else {
728 s->detector = detect_clicks;
729 }
730
731 return 0;
732}
733
735{
736 AudioDeclickContext *s = ctx->priv;
737 int i;
738
739 if (s->nb_samples > 0)
740 av_log(ctx, AV_LOG_INFO, "Detected %s in %"PRId64" of %"PRId64" samples (%g%%).\n",
741 s->is_declip ? "clips" : "clicks", s->detected_errors,
742 s->nb_samples, 100. * s->detected_errors / s->nb_samples);
743
744 av_audio_fifo_free(s->fifo);
745 av_audio_fifo_free(s->efifo);
746 av_freep(&s->window_func_lut);
747 av_frame_free(&s->enabled);
748 av_frame_free(&s->in);
749 av_frame_free(&s->out);
750 av_frame_free(&s->buffer);
751 av_frame_free(&s->is);
752
753 if (s->chan) {
754 for (i = 0; i < s->nb_channels; i++) {
755 DeclickChannel *c = &s->chan[i];
756
757 av_freep(&c->detection);
758 av_freep(&c->auxiliary);
759 av_freep(&c->acoefficients);
760 av_freep(&c->acorrelation);
761 av_freep(&c->tmp);
762 av_freep(&c->click);
763 av_freep(&c->index);
764 av_freep(&c->interpolated);
765 av_freep(&c->matrix);
766 c->matrix_size = 0;
767 av_freep(&c->histogram);
768 c->histogram_size = 0;
769 av_freep(&c->vector);
770 c->vector_size = 0;
771 av_freep(&c->y);
772 c->y_size = 0;
773 }
774 }
775 av_freep(&s->chan);
776 s->nb_channels = 0;
777}
778
779static const AVFilterPad inputs[] = {
780 {
781 .name = "default",
782 .type = AVMEDIA_TYPE_AUDIO,
783 .config_props = config_input,
784 },
785};
786
788 .p.name = "adeclick",
789 .p.description = NULL_IF_CONFIG_SMALL("Remove impulsive noise from input audio."),
790 .p.priv_class = &adeclick_class,
792 .priv_size = sizeof(AudioDeclickContext),
793 .init = init,
795 .uninit = uninit,
799};
800
801static const AVOption adeclip_options[] = {
802 { "window", "set window size", OFFSET(w), AV_OPT_TYPE_DOUBLE, {.dbl=55}, 10, 100, AF },
803 { "w", "set window size", OFFSET(w), AV_OPT_TYPE_DOUBLE, {.dbl=55}, 10, 100, AF },
804 { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_DOUBLE, {.dbl=75}, 50, 95, AF },
805 { "o", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_DOUBLE, {.dbl=75}, 50, 95, AF },
806 { "arorder", "set autoregression order", OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=8}, 0, 25, AF },
807 { "a", "set autoregression order", OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=8}, 0, 25, AF },
808 { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=10}, 1, 100, AF },
809 { "t", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=10}, 1, 100, AF },
810 { "hsize", "set histogram size", OFFSET(nb_hbins), AV_OPT_TYPE_INT, {.i64=1000}, 100, 9999, AF },
811 { "n", "set histogram size", OFFSET(nb_hbins), AV_OPT_TYPE_INT, {.i64=1000}, 100, 9999, AF },
812 { "method", "set overlap method", OFFSET(method), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AF, .unit = "m" },
813 { "m", "set overlap method", OFFSET(method), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AF, .unit = "m" },
814 { "add", "overlap-add", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "m" },
815 { "a", "overlap-add", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "m" },
816 { "save", "overlap-save", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "m" },
817 { "s", "overlap-save", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "m" },
818 { NULL }
819};
820
822
824 .p.name = "adeclip",
825 .p.description = NULL_IF_CONFIG_SMALL("Remove clipping from input audio."),
826 .p.priv_class = &adeclip_class,
828 .priv_size = sizeof(AudioDeclickContext),
829 .init = init,
831 .uninit = uninit,
835};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static const AVFilterPad inputs[]
Definition af_aap.c:299
static int config_input(AVFilterLink *inlink)
#define AF
static void autocorrelation(const double *input, int order, int size, double *output, double scale)
static int detect_clips(AudioDeclickContext *s, DeclickChannel *c, double unused0, double *unused1, double *unused2, uint8_t *clip, int *index, const double *src, double *dst)
static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
static int find_index(int *index, int value, int size)
static int isfinite_array(double *samples, int nb_samples)
static int factorization(double *matrix, int n)
const FFFilter ff_af_adeclick
static int config_input(AVFilterLink *inlink)
static double autoregression(const double *samples, int ar_order, int nb_samples, double *k, double *r, double *a)
static int filter_frame(AVFilterLink *inlink)
static const AVOption adeclip_options[]
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static int detect_clicks(AudioDeclickContext *s, DeclickChannel *c, double sigmae, double *detection, double *acoefficients, uint8_t *click, int *index, const double *src, double *dst)
static int do_interpolation(DeclickChannel *c, double *matrix, double *vector, int n, double *out)
#define OFFSET(x)
Definition af_adeclick.c:91
static int interpolation(DeclickChannel *c, const double *src, int ar_order, double *acoefficients, int *index, int nb_errors, double *auxiliary, double *interpolated)
const FFFilter ff_af_adeclip
static const AVOption adeclick_options[]
Definition af_adeclick.c:94
static FILE * out
static AVFormatContext * ctx
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
Audio FIFO Buffer.
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_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
Definition avfilter.c:1540
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition avfilter.c:229
Main libavfilter public API header.
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
#define is(width, name, range_min, range_max, subs,...)
Definition cbs_h264.c:78
#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
#define abs(x)
static __device__ float fabs(float a)
double fmin(double, double)
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
double value
Definition eval.c:102
#define fail
Definition test.h:479
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#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
int av_audio_fifo_peek(const AVAudioFifo *af, void *const *data, int nb_samples)
Peek data from an AVAudioFifo.
Definition audio_fifo.c:145
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition audio_fifo.c:62
int av_audio_fifo_write(AVAudioFifo *af, void *const *data, int nb_samples)
Write data to an AVAudioFifo.
Definition audio_fifo.c:119
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition audio_fifo.c:48
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition audio_fifo.c:222
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition audio_fifo.c:195
#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
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define ff_clz
Definition intmath.h:141
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition mem.c:555
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition samplefmt.h:67
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
int index
Definition gxfenc.c:90
int a
static const int16_t alpha[]
Definition ilbcdata.h:55
#define r
Definition input.c:42
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
const char * arg
Definition jacosubdec.c:65
static void filter_channel(MLPDecodeContext *m, unsigned int substr, unsigned int channel)
Generate PCM samples using the prediction filters and residual values read from the data stream,...
Definition mlpdec.c:972
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#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 FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition filters.h:257
#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 isfinite(x)
Definition libm.h:361
uint8_t w
Definition llvidencdsp.c:39
#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.
AVOptions.
Context for an Audio FIFO Buffer.
Definition audio_fifo.c:37
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
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
AVAudioFifo * efifo
Definition af_adeclick.c:81
uint64_t detected_errors
Definition af_adeclick.c:77
AVAudioFifo * fifo
Definition af_adeclick.c:82
DeclickChannel * chan
Definition af_adeclick.c:72
double * window_func_lut
Definition af_adeclick.c:83
int(* detector)(struct AudioDeclickContext *s, DeclickChannel *c, double sigmae, double *detection, double *acoefficients, uint8_t *click, int *index, const double *src, double *dst)
Definition af_adeclick.c:85
double * auxiliary
Definition af_adeclick.c:30
double * vector
Definition af_adeclick.c:38
double * acorrelation
Definition af_adeclick.c:33
double * detection
Definition af_adeclick.c:31
double * matrix
Definition af_adeclick.c:36
uint8_t * click
Definition af_adeclick.c:42
double * acoefficients
Definition af_adeclick.c:32
unsigned * histogram
Definition af_adeclick.c:44
double * interpolated
Definition af_adeclick.c:35
Used for passing data between threads.
Definition dsddec.c:71
AVFrame * out
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static int64_t pts
int size
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition tx.c:295
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition tx.c:903
@ AV_TX_DOUBLE_RDFT
Definition tx.h:91
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition tx.h:151
static double c[64]