FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_adeclick.c
Date: 2024-02-29 09:57:37
Exec Total Coverage
Lines: 0 403 0.0%
Functions: 0 15 0.0%
Branches: 0 260 0.0%

Line Branch Exec Source
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
21 #include "libavutil/audio_fifo.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/tx.h"
24 #include "avfilter.h"
25 #include "audio.h"
26 #include "filters.h"
27 #include "internal.h"
28
29 typedef struct DeclickChannel {
30 double *auxiliary;
31 double *detection;
32 double *acoefficients;
33 double *acorrelation;
34 double *tmp;
35 double *interpolated;
36 double *matrix;
37 int matrix_size;
38 double *vector;
39 int vector_size;
40 double *y;
41 int y_size;
42 uint8_t *click;
43 int *index;
44 unsigned *histogram;
45 int histogram_size;
46 } DeclickChannel;
47
48 typedef 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;
57 int nb_hbins;
58
59 int is_declip;
60 int ar_order;
61 int nb_burst_samples;
62 int window_size;
63 int hop_size;
64 int overlap_skip;
65
66 AVFrame *enabled;
67 AVFrame *in;
68 AVFrame *out;
69 AVFrame *buffer;
70 AVFrame *is;
71
72 DeclickChannel *chan;
73
74 int64_t pts;
75 int nb_channels;
76 uint64_t nb_samples;
77 uint64_t detected_errors;
78 int samples_left;
79 int eof;
80
81 AVAudioFifo *efifo;
82 AVAudioFifo *fifo;
83 double *window_func_lut;
84
85 int (*detector)(struct AudioDeclickContext *s, DeclickChannel *c,
86 double sigmae, double *detection,
87 double *acoefficients, uint8_t *click, int *index,
88 const double *src, double *dst);
89 } AudioDeclickContext;
90
91 #define OFFSET(x) offsetof(AudioDeclickContext, x)
92 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
93
94 static 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
114 AVFILTER_DEFINE_CLASS(adeclick);
115
116 static 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], *tx_out[2];
134 AVTXContext *tx, *itx;
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 return ret;
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 return ret;
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 return AVERROR(ENOMEM);
157
158 for (int n = 0; n < s->window_size - s->hop_size; n++)
159 tx_in[0][n] = 1.0;
160
161 for (int n = 0; n < s->hop_size; n++)
162 tx_in[1][n] = 1.0;
163
164 tx_fn(tx, tx_out[0], tx_in[0], sizeof(double));
165 tx_fn(tx, tx_out[1], tx_in[1], sizeof(double));
166
167 for (int n = 0; n <= tx_size/2; n++) {
168 double re0 = tx_out[0][2*n];
169 double im0 = tx_out[0][2*n+1];
170 double re1 = tx_out[1][2*n];
171 double im1 = tx_out[1][2*n+1];
172
173 tx_in[0][2*n] = re0 * re1 - im0 * im1;
174 tx_in[0][2*n+1] = re0 * im1 + re1 * im0;
175 }
176
177 itx_fn(itx, tx_out[0], tx_in[0], sizeof(AVComplexDouble));
178
179 scale = 1.0 / (s->window_size - s->hop_size);
180 for (int n = 0; n < s->window_size; n++)
181 s->window_func_lut[n] = tx_out[0][n] * scale;
182
183 av_tx_uninit(&tx);
184 av_tx_uninit(&itx);
185
186 av_freep(&tx_in[0]);
187 av_freep(&tx_in[1]);
188 av_freep(&tx_out[0]);
189 av_freep(&tx_out[1]);
190 }
191
192 av_frame_free(&s->in);
193 av_frame_free(&s->out);
194 av_frame_free(&s->buffer);
195 av_frame_free(&s->is);
196 s->enabled = ff_get_audio_buffer(inlink, s->window_size);
197 s->in = ff_get_audio_buffer(inlink, s->window_size);
198 s->out = ff_get_audio_buffer(inlink, s->window_size);
199 s->buffer = ff_get_audio_buffer(inlink, s->window_size * 2);
200 s->is = ff_get_audio_buffer(inlink, s->window_size);
201 if (!s->in || !s->out || !s->buffer || !s->is || !s->enabled)
202 return AVERROR(ENOMEM);
203
204 s->efifo = av_audio_fifo_alloc(inlink->format, 1, s->window_size);
205 if (!s->efifo)
206 return AVERROR(ENOMEM);
207 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->ch_layout.nb_channels, s->window_size);
208 if (!s->fifo)
209 return AVERROR(ENOMEM);
210 s->overlap_skip = s->method ? (s->window_size - s->hop_size) / 2 : 0;
211 if (s->overlap_skip > 0) {
212 av_audio_fifo_write(s->fifo, (void **)s->in->extended_data,
213 s->overlap_skip);
214 }
215
216 s->nb_channels = inlink->ch_layout.nb_channels;
217 s->chan = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->chan));
218 if (!s->chan)
219 return AVERROR(ENOMEM);
220
221 for (i = 0; i < inlink->ch_layout.nb_channels; i++) {
222 DeclickChannel *c = &s->chan[i];
223
224 c->detection = av_calloc(s->window_size, sizeof(*c->detection));
225 c->auxiliary = av_calloc(s->ar_order + 1, sizeof(*c->auxiliary));
226 c->acoefficients = av_calloc(s->ar_order + 1, sizeof(*c->acoefficients));
227 c->acorrelation = av_calloc(s->ar_order + 1, sizeof(*c->acorrelation));
228 c->tmp = av_calloc(s->ar_order, sizeof(*c->tmp));
229 c->click = av_calloc(s->window_size, sizeof(*c->click));
230 c->index = av_calloc(s->window_size, sizeof(*c->index));
231 c->interpolated = av_calloc(s->window_size, sizeof(*c->interpolated));
232 if (!c->auxiliary || !c->acoefficients || !c->detection || !c->click ||
233 !c->index || !c->interpolated || !c->acorrelation || !c->tmp)
234 return AVERROR(ENOMEM);
235 }
236
237 return 0;
238 }
239
240 static void autocorrelation(const double *input, int order, int size,
241 double *output, double scale)
242 {
243 int i, j;
244
245 for (i = 0; i <= order; i++) {
246 double value = 0.;
247
248 for (j = i; j < size; j++)
249 value += input[j] * input[j - i];
250
251 output[i] = value * scale;
252 }
253 }
254
255 static double autoregression(const double *samples, int ar_order,
256 int nb_samples, double *k, double *r, double *a)
257 {
258 double alpha;
259 int i, j;
260
261 memset(a, 0, ar_order * sizeof(*a));
262
263 autocorrelation(samples, ar_order, nb_samples, r, 1. / nb_samples);
264
265 /* Levinson-Durbin algorithm */
266 k[0] = a[0] = -r[1] / r[0];
267 alpha = r[0] * (1. - k[0] * k[0]);
268 for (i = 1; i < ar_order; i++) {
269 double epsilon = 0.;
270
271 for (j = 0; j < i; j++)
272 epsilon += a[j] * r[i - j];
273 epsilon += r[i + 1];
274
275 k[i] = -epsilon / alpha;
276 alpha *= (1. - k[i] * k[i]);
277 for (j = i - 1; j >= 0; j--)
278 k[j] = a[j] + k[i] * a[i - j - 1];
279 for (j = 0; j <= i; j++)
280 a[j] = k[j];
281 }
282
283 k[0] = 1.;
284 for (i = 1; i <= ar_order; i++)
285 k[i] = a[i - 1];
286
287 return sqrt(alpha);
288 }
289
290 static int isfinite_array(double *samples, int nb_samples)
291 {
292 int i;
293
294 for (i = 0; i < nb_samples; i++)
295 if (!isfinite(samples[i]))
296 return 0;
297
298 return 1;
299 }
300
301 static int find_index(int *index, int value, int size)
302 {
303 int i, start, end;
304
305 if ((value < index[0]) || (value > index[size - 1]))
306 return 1;
307
308 i = start = 0;
309 end = size - 1;
310
311 while (start <= end) {
312 i = (end + start) / 2;
313 if (index[i] == value)
314 return 0;
315 if (value < index[i])
316 end = i - 1;
317 if (value > index[i])
318 start = i + 1;
319 }
320
321 return 1;
322 }
323
324 static int factorization(double *matrix, int n)
325 {
326 int i, j, k;
327
328 for (i = 0; i < n; i++) {
329 const int in = i * n;
330 double value;
331
332 value = matrix[in + i];
333 for (j = 0; j < i; j++)
334 value -= matrix[j * n + j] * matrix[in + j] * matrix[in + j];
335
336 if (value == 0.) {
337 return -1;
338 }
339
340 matrix[in + i] = value;
341 for (j = i + 1; j < n; j++) {
342 const int jn = j * n;
343 double x;
344
345 x = matrix[jn + i];
346 for (k = 0; k < i; k++)
347 x -= matrix[k * n + k] * matrix[in + k] * matrix[jn + k];
348 matrix[jn + i] = x / matrix[in + i];
349 }
350 }
351
352 return 0;
353 }
354
355 static int do_interpolation(DeclickChannel *c, double *matrix,
356 double *vector, int n, double *out)
357 {
358 int i, j, ret;
359 double *y;
360
361 ret = factorization(matrix, n);
362 if (ret < 0)
363 return ret;
364
365 av_fast_malloc(&c->y, &c->y_size, n * sizeof(*c->y));
366 y = c->y;
367 if (!y)
368 return AVERROR(ENOMEM);
369
370 for (i = 0; i < n; i++) {
371 const int in = i * n;
372 double value;
373
374 value = vector[i];
375 for (j = 0; j < i; j++)
376 value -= matrix[in + j] * y[j];
377 y[i] = value;
378 }
379
380 for (i = n - 1; i >= 0; i--) {
381 out[i] = y[i] / matrix[i * n + i];
382 for (j = i + 1; j < n; j++)
383 out[i] -= matrix[j * n + i] * out[j];
384 }
385
386 return 0;
387 }
388
389 static int interpolation(DeclickChannel *c, const double *src, int ar_order,
390 double *acoefficients, int *index, int nb_errors,
391 double *auxiliary, double *interpolated)
392 {
393 double *vector, *matrix;
394 int i, j;
395
396 av_fast_malloc(&c->matrix, &c->matrix_size, nb_errors * nb_errors * sizeof(*c->matrix));
397 matrix = c->matrix;
398 if (!matrix)
399 return AVERROR(ENOMEM);
400
401 av_fast_malloc(&c->vector, &c->vector_size, nb_errors * sizeof(*c->vector));
402 vector = c->vector;
403 if (!vector)
404 return AVERROR(ENOMEM);
405
406 autocorrelation(acoefficients, ar_order, ar_order + 1, auxiliary, 1.);
407
408 for (i = 0; i < nb_errors; i++) {
409 const int im = i * nb_errors;
410
411 for (j = i; j < nb_errors; j++) {
412 if (abs(index[j] - index[i]) <= ar_order) {
413 matrix[j * nb_errors + i] = matrix[im + j] = auxiliary[abs(index[j] - index[i])];
414 } else {
415 matrix[j * nb_errors + i] = matrix[im + j] = 0;
416 }
417 }
418 }
419
420 for (i = 0; i < nb_errors; i++) {
421 double value = 0.;
422
423 for (j = -ar_order; j <= ar_order; j++)
424 if (find_index(index, index[i] - j, nb_errors))
425 value -= src[index[i] - j] * auxiliary[abs(j)];
426
427 vector[i] = value;
428 }
429
430 return do_interpolation(c, matrix, vector, nb_errors, interpolated);
431 }
432
433 static int detect_clips(AudioDeclickContext *s, DeclickChannel *c,
434 double unused0,
435 double *unused1, double *unused2,
436 uint8_t *clip, int *index,
437 const double *src, double *dst)
438 {
439 const double threshold = s->threshold;
440 double max_amplitude = 0;
441 unsigned *histogram;
442 int i, nb_clips = 0;
443
444 av_fast_malloc(&c->histogram, &c->histogram_size, s->nb_hbins * sizeof(*c->histogram));
445 if (!c->histogram)
446 return AVERROR(ENOMEM);
447 histogram = c->histogram;
448 memset(histogram, 0, sizeof(*histogram) * s->nb_hbins);
449
450 for (i = 0; i < s->window_size; i++) {
451 const unsigned index = fmin(fabs(src[i]), 1) * (s->nb_hbins - 1);
452
453 histogram[index]++;
454 dst[i] = src[i];
455 clip[i] = 0;
456 }
457
458 for (i = s->nb_hbins - 1; i > 1; i--) {
459 if (histogram[i]) {
460 if (histogram[i] / (double)FFMAX(histogram[i - 1], 1) > threshold) {
461 max_amplitude = i / (double)s->nb_hbins;
462 }
463 break;
464 }
465 }
466
467 if (max_amplitude > 0.) {
468 for (i = 0; i < s->window_size; i++) {
469 clip[i] = fabs(src[i]) >= max_amplitude;
470 }
471 }
472
473 memset(clip, 0, s->ar_order * sizeof(*clip));
474 memset(clip + (s->window_size - s->ar_order), 0, s->ar_order * sizeof(*clip));
475
476 for (i = s->ar_order; i < s->window_size - s->ar_order; i++)
477 if (clip[i])
478 index[nb_clips++] = i;
479
480 return nb_clips;
481 }
482
483 static int detect_clicks(AudioDeclickContext *s, DeclickChannel *c,
484 double sigmae,
485 double *detection, double *acoefficients,
486 uint8_t *click, int *index,
487 const double *src, double *dst)
488 {
489 const double threshold = s->threshold;
490 int i, j, nb_clicks = 0, prev = -1;
491
492 memset(detection, 0, s->window_size * sizeof(*detection));
493
494 for (i = s->ar_order; i < s->window_size; i++) {
495 for (j = 0; j <= s->ar_order; j++) {
496 detection[i] += acoefficients[j] * src[i - j];
497 }
498 }
499
500 for (i = 0; i < s->window_size; i++) {
501 click[i] = fabs(detection[i]) > sigmae * threshold;
502 dst[i] = src[i];
503 }
504
505 for (i = 0; i < s->window_size; i++) {
506 if (!click[i])
507 continue;
508
509 if (prev >= 0 && (i > prev + 1) && (i <= s->nb_burst_samples + prev))
510 for (j = prev + 1; j < i; j++)
511 click[j] = 1;
512 prev = i;
513 }
514
515 memset(click, 0, s->ar_order * sizeof(*click));
516 memset(click + (s->window_size - s->ar_order), 0, s->ar_order * sizeof(*click));
517
518 for (i = s->ar_order; i < s->window_size - s->ar_order; i++)
519 if (click[i])
520 index[nb_clicks++] = i;
521
522 return nb_clicks;
523 }
524
525 typedef struct ThreadData {
526 AVFrame *out;
527 } ThreadData;
528
529 static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
530 {
531 AudioDeclickContext *s = ctx->priv;
532 ThreadData *td = arg;
533 AVFrame *out = td->out;
534 const double *src = (const double *)s->in->extended_data[ch];
535 double *is = (double *)s->is->extended_data[ch];
536 double *dst = (double *)s->out->extended_data[ch];
537 double *ptr = (double *)out->extended_data[ch];
538 double *buf = (double *)s->buffer->extended_data[ch];
539 const double *w = s->window_func_lut;
540 DeclickChannel *c = &s->chan[ch];
541 double sigmae;
542 int j, ret;
543
544 sigmae = autoregression(src, s->ar_order, s->window_size, c->acoefficients, c->acorrelation, c->tmp);
545
546 if (isfinite_array(c->acoefficients, s->ar_order + 1)) {
547 double *interpolated = c->interpolated;
548 int *index = c->index;
549 int nb_errors;
550
551 nb_errors = s->detector(s, c, sigmae, c->detection, c->acoefficients,
552 c->click, index, src, dst);
553 if (nb_errors > 0) {
554 double *enabled = (double *)s->enabled->extended_data[0];
555
556 ret = interpolation(c, src, s->ar_order, c->acoefficients, index,
557 nb_errors, c->auxiliary, interpolated);
558 if (ret < 0)
559 return ret;
560
561 av_audio_fifo_peek(s->efifo, (void**)s->enabled->extended_data, s->window_size);
562
563 for (j = 0; j < nb_errors; j++) {
564 if (enabled[index[j]]) {
565 dst[index[j]] = interpolated[j];
566 is[index[j]] = 1;
567 }
568 }
569 }
570 } else {
571 memcpy(dst, src, s->window_size * sizeof(*dst));
572 }
573
574 if (s->method == 0) {
575 for (j = 0; j < s->window_size; j++)
576 buf[j] += dst[j] * w[j];
577 } else {
578 const int skip = s->overlap_skip;
579
580 for (j = 0; j < s->hop_size; j++)
581 buf[j] = dst[skip + j];
582 }
583 for (j = 0; j < s->hop_size; j++)
584 ptr[j] = buf[j];
585
586 memmove(buf, buf + s->hop_size, (s->window_size * 2 - s->hop_size) * sizeof(*buf));
587 memmove(is, is + s->hop_size, (s->window_size - s->hop_size) * sizeof(*is));
588 memset(buf + s->window_size * 2 - s->hop_size, 0, s->hop_size * sizeof(*buf));
589 memset(is + s->window_size - s->hop_size, 0, s->hop_size * sizeof(*is));
590
591 return 0;
592 }
593
594 static int filter_frame(AVFilterLink *inlink)
595 {
596 AVFilterContext *ctx = inlink->dst;
597 AVFilterLink *outlink = ctx->outputs[0];
598 AudioDeclickContext *s = ctx->priv;
599 AVFrame *out = NULL;
600 int ret = 0, j, ch, detected_errors = 0;
601 ThreadData td;
602
603 out = ff_get_audio_buffer(outlink, s->hop_size);
604 if (!out)
605 return AVERROR(ENOMEM);
606
607 ret = av_audio_fifo_peek(s->fifo, (void **)s->in->extended_data,
608 s->window_size);
609 if (ret < 0)
610 goto fail;
611
612 td.out = out;
613 ret = ff_filter_execute(ctx, filter_channel, &td, NULL, inlink->ch_layout.nb_channels);
614 if (ret < 0)
615 goto fail;
616
617 for (ch = 0; ch < s->in->ch_layout.nb_channels; ch++) {
618 double *is = (double *)s->is->extended_data[ch];
619
620 for (j = 0; j < s->hop_size; j++) {
621 if (is[j])
622 detected_errors++;
623 }
624 }
625
626 av_audio_fifo_drain(s->fifo, s->hop_size);
627 av_audio_fifo_drain(s->efifo, s->hop_size);
628
629 if (s->samples_left > 0)
630 out->nb_samples = FFMIN(s->hop_size, s->samples_left);
631
632 out->pts = s->pts;
633 s->pts += av_rescale_q(s->hop_size, (AVRational){1, outlink->sample_rate}, outlink->time_base);
634
635 s->detected_errors += detected_errors;
636 s->nb_samples += out->nb_samples * inlink->ch_layout.nb_channels;
637
638 ret = ff_filter_frame(outlink, out);
639 if (ret < 0)
640 return ret;
641
642 if (s->samples_left > 0) {
643 s->samples_left -= s->hop_size;
644 if (s->samples_left <= 0)
645 av_audio_fifo_drain(s->fifo, av_audio_fifo_size(s->fifo));
646 }
647
648 fail:
649 if (ret < 0)
650 av_frame_free(&out);
651 return ret;
652 }
653
654 static int activate(AVFilterContext *ctx)
655 {
656 AVFilterLink *inlink = ctx->inputs[0];
657 AVFilterLink *outlink = ctx->outputs[0];
658 AudioDeclickContext *s = ctx->priv;
659 AVFrame *in;
660 int ret, status;
661 int64_t pts;
662
663 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
664
665 ret = ff_inlink_consume_samples(inlink, s->window_size, s->window_size, &in);
666 if (ret < 0)
667 return ret;
668 if (ret > 0) {
669 double *e = (double *)s->enabled->extended_data[0];
670
671 if (s->pts == AV_NOPTS_VALUE)
672 s->pts = in->pts;
673
674 ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
675 in->nb_samples);
676 for (int i = 0; i < in->nb_samples; i++)
677 e[i] = !ctx->is_disabled;
678
679 av_audio_fifo_write(s->efifo, (void**)s->enabled->extended_data, in->nb_samples);
680 av_frame_free(&in);
681 if (ret < 0)
682 return ret;
683 }
684
685 if (av_audio_fifo_size(s->fifo) >= s->window_size ||
686 s->samples_left > 0)
687 return filter_frame(inlink);
688
689 if (av_audio_fifo_size(s->fifo) >= s->window_size) {
690 ff_filter_set_ready(ctx, 100);
691 return 0;
692 }
693
694 if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
695 if (status == AVERROR_EOF) {
696 s->eof = 1;
697 s->samples_left = av_audio_fifo_size(s->fifo) - s->overlap_skip;
698 ff_filter_set_ready(ctx, 100);
699 return 0;
700 }
701 }
702
703 if (s->eof && s->samples_left <= 0) {
704 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
705 return 0;
706 }
707
708 if (!s->eof)
709 FF_FILTER_FORWARD_WANTED(outlink, inlink);
710
711 return FFERROR_NOT_READY;
712 }
713
714 static av_cold int init(AVFilterContext *ctx)
715 {
716 AudioDeclickContext *s = ctx->priv;
717
718 s->is_declip = !strcmp(ctx->filter->name, "adeclip");
719 if (s->is_declip) {
720 s->detector = detect_clips;
721 } else {
722 s->detector = detect_clicks;
723 }
724
725 return 0;
726 }
727
728 static av_cold void uninit(AVFilterContext *ctx)
729 {
730 AudioDeclickContext *s = ctx->priv;
731 int i;
732
733 if (s->nb_samples > 0)
734 av_log(ctx, AV_LOG_INFO, "Detected %s in %"PRId64" of %"PRId64" samples (%g%%).\n",
735 s->is_declip ? "clips" : "clicks", s->detected_errors,
736 s->nb_samples, 100. * s->detected_errors / s->nb_samples);
737
738 av_audio_fifo_free(s->fifo);
739 av_audio_fifo_free(s->efifo);
740 av_freep(&s->window_func_lut);
741 av_frame_free(&s->enabled);
742 av_frame_free(&s->in);
743 av_frame_free(&s->out);
744 av_frame_free(&s->buffer);
745 av_frame_free(&s->is);
746
747 if (s->chan) {
748 for (i = 0; i < s->nb_channels; i++) {
749 DeclickChannel *c = &s->chan[i];
750
751 av_freep(&c->detection);
752 av_freep(&c->auxiliary);
753 av_freep(&c->acoefficients);
754 av_freep(&c->acorrelation);
755 av_freep(&c->tmp);
756 av_freep(&c->click);
757 av_freep(&c->index);
758 av_freep(&c->interpolated);
759 av_freep(&c->matrix);
760 c->matrix_size = 0;
761 av_freep(&c->histogram);
762 c->histogram_size = 0;
763 av_freep(&c->vector);
764 c->vector_size = 0;
765 av_freep(&c->y);
766 c->y_size = 0;
767 }
768 }
769 av_freep(&s->chan);
770 s->nb_channels = 0;
771 }
772
773 static const AVFilterPad inputs[] = {
774 {
775 .name = "default",
776 .type = AVMEDIA_TYPE_AUDIO,
777 .config_props = config_input,
778 },
779 };
780
781 const AVFilter ff_af_adeclick = {
782 .name = "adeclick",
783 .description = NULL_IF_CONFIG_SMALL("Remove impulsive noise from input audio."),
784 .priv_size = sizeof(AudioDeclickContext),
785 .priv_class = &adeclick_class,
786 .init = init,
787 .activate = activate,
788 .uninit = uninit,
789 FILTER_INPUTS(inputs),
790 FILTER_OUTPUTS(ff_audio_default_filterpad),
791 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBLP),
792 .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
793 };
794
795 static const AVOption adeclip_options[] = {
796 { "window", "set window size", OFFSET(w), AV_OPT_TYPE_DOUBLE, {.dbl=55}, 10, 100, AF },
797 { "w", "set window size", OFFSET(w), AV_OPT_TYPE_DOUBLE, {.dbl=55}, 10, 100, AF },
798 { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_DOUBLE, {.dbl=75}, 50, 95, AF },
799 { "o", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_DOUBLE, {.dbl=75}, 50, 95, AF },
800 { "arorder", "set autoregression order", OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=8}, 0, 25, AF },
801 { "a", "set autoregression order", OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=8}, 0, 25, AF },
802 { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=10}, 1, 100, AF },
803 { "t", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=10}, 1, 100, AF },
804 { "hsize", "set histogram size", OFFSET(nb_hbins), AV_OPT_TYPE_INT, {.i64=1000}, 100, 9999, AF },
805 { "n", "set histogram size", OFFSET(nb_hbins), AV_OPT_TYPE_INT, {.i64=1000}, 100, 9999, AF },
806 { "method", "set overlap method", OFFSET(method), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AF, .unit = "m" },
807 { "m", "set overlap method", OFFSET(method), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AF, .unit = "m" },
808 { "add", "overlap-add", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "m" },
809 { "a", "overlap-add", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "m" },
810 { "save", "overlap-save", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "m" },
811 { "s", "overlap-save", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "m" },
812 { NULL }
813 };
814
815 AVFILTER_DEFINE_CLASS(adeclip);
816
817 const AVFilter ff_af_adeclip = {
818 .name = "adeclip",
819 .description = NULL_IF_CONFIG_SMALL("Remove clipping from input audio."),
820 .priv_size = sizeof(AudioDeclickContext),
821 .priv_class = &adeclip_class,
822 .init = init,
823 .activate = activate,
824 .uninit = uninit,
825 FILTER_INPUTS(inputs),
826 FILTER_OUTPUTS(ff_audio_default_filterpad),
827 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBLP),
828 .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
829 };
830