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