2 * Copyright (c) 2018 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/audio_fifo.h"
22 #include "libavutil/opt.h"
29 typedef struct DeclickChannel {
32 double *acoefficients;
48 typedef struct AudioDeclickContext {
77 uint64_t detected_errors;
83 double *window_func_lut;
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;
91 #define OFFSET(x) offsetof(AudioDeclickContext, x)
92 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
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" },
114 AVFILTER_DEFINE_CLASS(adeclick);
116 static int query_formats(AVFilterContext *ctx)
118 AVFilterFormats *formats = NULL;
119 AVFilterChannelLayouts *layouts = NULL;
120 static const enum AVSampleFormat sample_fmts[] = {
126 formats = ff_make_format_list(sample_fmts);
128 return AVERROR(ENOMEM);
129 ret = ff_set_common_formats(ctx, formats);
133 layouts = ff_all_channel_counts();
135 return AVERROR(ENOMEM);
137 ret = ff_set_common_channel_layouts(ctx, layouts);
141 formats = ff_all_samplerates();
142 return ff_set_common_samplerates(ctx, formats);
145 static int config_input(AVFilterLink *inlink)
147 AVFilterContext *ctx = inlink->dst;
148 AudioDeclickContext *s = ctx->priv;
151 s->pts = AV_NOPTS_VALUE;
152 s->window_size = inlink->sample_rate * s->w / 1000.;
153 if (s->window_size < 100)
154 return AVERROR(EINVAL);
155 s->ar_order = FFMAX(s->window_size * s->ar / 100., 1);
156 s->nb_burst_samples = s->window_size * s->burst / 1000.;
157 s->hop_size = s->window_size * (1. - (s->overlap / 100.));
159 return AVERROR(EINVAL);
161 s->window_func_lut = av_calloc(s->window_size, sizeof(*s->window_func_lut));
162 if (!s->window_func_lut)
163 return AVERROR(ENOMEM);
164 for (i = 0; i < s->window_size; i++)
165 s->window_func_lut[i] = sin(M_PI * i / s->window_size) *
166 (1. - (s->overlap / 100.)) * M_PI_2;
168 av_frame_free(&s->in);
169 av_frame_free(&s->out);
170 av_frame_free(&s->buffer);
171 av_frame_free(&s->is);
172 s->enabled = ff_get_audio_buffer(inlink, s->window_size);
173 s->in = ff_get_audio_buffer(inlink, s->window_size);
174 s->out = ff_get_audio_buffer(inlink, s->window_size);
175 s->buffer = ff_get_audio_buffer(inlink, s->window_size * 2);
176 s->is = ff_get_audio_buffer(inlink, s->window_size);
177 if (!s->in || !s->out || !s->buffer || !s->is || !s->enabled)
178 return AVERROR(ENOMEM);
180 s->efifo = av_audio_fifo_alloc(inlink->format, 1, s->window_size);
182 return AVERROR(ENOMEM);
183 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->window_size);
185 return AVERROR(ENOMEM);
186 s->overlap_skip = s->method ? (s->window_size - s->hop_size) / 2 : 0;
187 if (s->overlap_skip > 0) {
188 av_audio_fifo_write(s->fifo, (void **)s->in->extended_data,
192 s->nb_channels = inlink->channels;
193 s->chan = av_calloc(inlink->channels, sizeof(*s->chan));
195 return AVERROR(ENOMEM);
197 for (i = 0; i < inlink->channels; i++) {
198 DeclickChannel *c = &s->chan[i];
200 c->detection = av_calloc(s->window_size, sizeof(*c->detection));
201 c->auxiliary = av_calloc(s->ar_order + 1, sizeof(*c->auxiliary));
202 c->acoefficients = av_calloc(s->ar_order + 1, sizeof(*c->acoefficients));
203 c->acorrelation = av_calloc(s->ar_order + 1, sizeof(*c->acorrelation));
204 c->tmp = av_calloc(s->ar_order, sizeof(*c->tmp));
205 c->click = av_calloc(s->window_size, sizeof(*c->click));
206 c->index = av_calloc(s->window_size, sizeof(*c->index));
207 c->interpolated = av_calloc(s->window_size, sizeof(*c->interpolated));
208 if (!c->auxiliary || !c->acoefficients || !c->detection || !c->click ||
209 !c->index || !c->interpolated || !c->acorrelation || !c->tmp)
210 return AVERROR(ENOMEM);
216 static void autocorrelation(const double *input, int order, int size,
217 double *output, double scale)
221 for (i = 0; i <= order; i++) {
224 for (j = i; j < size; j++)
225 value += input[j] * input[j - i];
227 output[i] = value * scale;
231 static double autoregression(const double *samples, int ar_order,
232 int nb_samples, double *k, double *r, double *a)
237 memset(a, 0, ar_order * sizeof(*a));
239 autocorrelation(samples, ar_order, nb_samples, r, 1. / nb_samples);
241 /* Levinson-Durbin algorithm */
242 k[0] = a[0] = -r[1] / r[0];
243 alpha = r[0] * (1. - k[0] * k[0]);
244 for (i = 1; i < ar_order; i++) {
247 for (j = 0; j < i; j++)
248 epsilon += a[j] * r[i - j];
251 k[i] = -epsilon / alpha;
252 alpha *= (1. - k[i] * k[i]);
253 for (j = i - 1; j >= 0; j--)
254 k[j] = a[j] + k[i] * a[i - j - 1];
255 for (j = 0; j <= i; j++)
260 for (i = 1; i <= ar_order; i++)
266 static int isfinite_array(double *samples, int nb_samples)
270 for (i = 0; i < nb_samples; i++)
271 if (!isfinite(samples[i]))
277 static int find_index(int *index, int value, int size)
281 if ((value < index[0]) || (value > index[size - 1]))
287 while (start <= end) {
288 i = (end + start) / 2;
289 if (index[i] == value)
291 if (value < index[i])
293 if (value > index[i])
300 static int factorization(double *matrix, int n)
304 for (i = 0; i < n; i++) {
305 const int in = i * n;
308 value = matrix[in + i];
309 for (j = 0; j < i; j++)
310 value -= matrix[j * n + j] * matrix[in + j] * matrix[in + j];
316 matrix[in + i] = value;
317 for (j = i + 1; j < n; j++) {
318 const int jn = j * n;
322 for (k = 0; k < i; k++)
323 x -= matrix[k * n + k] * matrix[in + k] * matrix[jn + k];
324 matrix[jn + i] = x / matrix[in + i];
331 static int do_interpolation(DeclickChannel *c, double *matrix,
332 double *vector, int n, double *out)
337 ret = factorization(matrix, n);
341 av_fast_malloc(&c->y, &c->y_size, n * sizeof(*c->y));
344 return AVERROR(ENOMEM);
346 for (i = 0; i < n; i++) {
347 const int in = i * n;
351 for (j = 0; j < i; j++)
352 value -= matrix[in + j] * y[j];
356 for (i = n - 1; i >= 0; i--) {
357 out[i] = y[i] / matrix[i * n + i];
358 for (j = i + 1; j < n; j++)
359 out[i] -= matrix[j * n + i] * out[j];
365 static int interpolation(DeclickChannel *c, const double *src, int ar_order,
366 double *acoefficients, int *index, int nb_errors,
367 double *auxiliary, double *interpolated)
369 double *vector, *matrix;
372 av_fast_malloc(&c->matrix, &c->matrix_size, nb_errors * nb_errors * sizeof(*c->matrix));
375 return AVERROR(ENOMEM);
377 av_fast_malloc(&c->vector, &c->vector_size, nb_errors * sizeof(*c->vector));
380 return AVERROR(ENOMEM);
382 autocorrelation(acoefficients, ar_order, ar_order + 1, auxiliary, 1.);
384 for (i = 0; i < nb_errors; i++) {
385 const int im = i * nb_errors;
387 for (j = i; j < nb_errors; j++) {
388 if (abs(index[j] - index[i]) <= ar_order) {
389 matrix[j * nb_errors + i] = matrix[im + j] = auxiliary[abs(index[j] - index[i])];
391 matrix[j * nb_errors + i] = matrix[im + j] = 0;
396 for (i = 0; i < nb_errors; i++) {
399 for (j = -ar_order; j <= ar_order; j++)
400 if (find_index(index, index[i] - j, nb_errors))
401 value -= src[index[i] - j] * auxiliary[abs(j)];
406 return do_interpolation(c, matrix, vector, nb_errors, interpolated);
409 static int detect_clips(AudioDeclickContext *s, DeclickChannel *c,
411 double *unused1, double *unused2,
412 uint8_t *clip, int *index,
413 const double *src, double *dst)
415 const double threshold = s->threshold;
416 double max_amplitude = 0;
420 av_fast_malloc(&c->histogram, &c->histogram_size, s->nb_hbins * sizeof(*c->histogram));
422 return AVERROR(ENOMEM);
423 histogram = c->histogram;
424 memset(histogram, 0, sizeof(*histogram) * s->nb_hbins);
426 for (i = 0; i < s->window_size; i++) {
427 const unsigned index = fmin(fabs(src[i]), 1) * (s->nb_hbins - 1);
434 for (i = s->nb_hbins - 1; i > 1; i--) {
436 if (histogram[i] / (double)FFMAX(histogram[i - 1], 1) > threshold) {
437 max_amplitude = i / (double)s->nb_hbins;
443 if (max_amplitude > 0.) {
444 for (i = 0; i < s->window_size; i++) {
445 clip[i] = fabs(src[i]) >= max_amplitude;
449 memset(clip, 0, s->ar_order * sizeof(*clip));
450 memset(clip + (s->window_size - s->ar_order), 0, s->ar_order * sizeof(*clip));
452 for (i = s->ar_order; i < s->window_size - s->ar_order; i++)
454 index[nb_clips++] = i;
459 static int detect_clicks(AudioDeclickContext *s, DeclickChannel *c,
461 double *detection, double *acoefficients,
462 uint8_t *click, int *index,
463 const double *src, double *dst)
465 const double threshold = s->threshold;
466 int i, j, nb_clicks = 0, prev = -1;
468 memset(detection, 0, s->window_size * sizeof(*detection));
470 for (i = s->ar_order; i < s->window_size; i++) {
471 for (j = 0; j <= s->ar_order; j++) {
472 detection[i] += acoefficients[j] * src[i - j];
476 for (i = 0; i < s->window_size; i++) {
477 click[i] = fabs(detection[i]) > sigmae * threshold;
481 for (i = 0; i < s->window_size; i++) {
485 if (prev >= 0 && (i > prev + 1) && (i <= s->nb_burst_samples + prev))
486 for (j = prev + 1; j < i; j++)
491 memset(click, 0, s->ar_order * sizeof(*click));
492 memset(click + (s->window_size - s->ar_order), 0, s->ar_order * sizeof(*click));
494 for (i = s->ar_order; i < s->window_size - s->ar_order; i++)
496 index[nb_clicks++] = i;
501 typedef struct ThreadData {
505 static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
507 AudioDeclickContext *s = ctx->priv;
508 ThreadData *td = arg;
509 AVFrame *out = td->out;
510 const double *src = (const double *)s->in->extended_data[ch];
511 double *is = (double *)s->is->extended_data[ch];
512 double *dst = (double *)s->out->extended_data[ch];
513 double *ptr = (double *)out->extended_data[ch];
514 double *buf = (double *)s->buffer->extended_data[ch];
515 const double *w = s->window_func_lut;
516 DeclickChannel *c = &s->chan[ch];
520 sigmae = autoregression(src, s->ar_order, s->window_size, c->acoefficients, c->acorrelation, c->tmp);
522 if (isfinite_array(c->acoefficients, s->ar_order + 1)) {
523 double *interpolated = c->interpolated;
524 int *index = c->index;
527 nb_errors = s->detector(s, c, sigmae, c->detection, c->acoefficients,
528 c->click, index, src, dst);
530 double *enabled = (double *)s->enabled->extended_data[0];
532 ret = interpolation(c, src, s->ar_order, c->acoefficients, index,
533 nb_errors, c->auxiliary, interpolated);
537 av_audio_fifo_peek(s->efifo, (void**)s->enabled->extended_data, s->window_size);
539 for (j = 0; j < nb_errors; j++) {
540 if (enabled[index[j]]) {
541 dst[index[j]] = interpolated[j];
547 memcpy(dst, src, s->window_size * sizeof(*dst));
550 if (s->method == 0) {
551 for (j = 0; j < s->window_size; j++)
552 buf[j] += dst[j] * w[j];
554 const int skip = s->overlap_skip;
556 for (j = 0; j < s->hop_size; j++)
557 buf[j] = dst[skip + j];
559 for (j = 0; j < s->hop_size; j++)
562 memmove(buf, buf + s->hop_size, (s->window_size * 2 - s->hop_size) * sizeof(*buf));
563 memmove(is, is + s->hop_size, (s->window_size - s->hop_size) * sizeof(*is));
564 memset(buf + s->window_size * 2 - s->hop_size, 0, s->hop_size * sizeof(*buf));
565 memset(is + s->window_size - s->hop_size, 0, s->hop_size * sizeof(*is));
570 static int filter_frame(AVFilterLink *inlink)
572 AVFilterContext *ctx = inlink->dst;
573 AVFilterLink *outlink = ctx->outputs[0];
574 AudioDeclickContext *s = ctx->priv;
576 int ret = 0, j, ch, detected_errors = 0;
579 out = ff_get_audio_buffer(outlink, s->hop_size);
581 return AVERROR(ENOMEM);
583 ret = av_audio_fifo_peek(s->fifo, (void **)s->in->extended_data,
589 ret = ctx->internal->execute(ctx, filter_channel, &td, NULL, inlink->channels);
593 for (ch = 0; ch < s->in->channels; ch++) {
594 double *is = (double *)s->is->extended_data[ch];
596 for (j = 0; j < s->hop_size; j++) {
602 av_audio_fifo_drain(s->fifo, s->hop_size);
603 av_audio_fifo_drain(s->efifo, s->hop_size);
605 if (s->samples_left > 0)
606 out->nb_samples = FFMIN(s->hop_size, s->samples_left);
609 s->pts += av_rescale_q(s->hop_size, (AVRational){1, outlink->sample_rate}, outlink->time_base);
611 s->detected_errors += detected_errors;
612 s->nb_samples += out->nb_samples * inlink->channels;
614 ret = ff_filter_frame(outlink, out);
618 if (s->samples_left > 0) {
619 s->samples_left -= s->hop_size;
620 if (s->samples_left <= 0)
621 av_audio_fifo_drain(s->fifo, av_audio_fifo_size(s->fifo));
630 static int activate(AVFilterContext *ctx)
632 AVFilterLink *inlink = ctx->inputs[0];
633 AVFilterLink *outlink = ctx->outputs[0];
634 AudioDeclickContext *s = ctx->priv;
639 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
641 ret = ff_inlink_consume_samples(inlink, s->window_size, s->window_size, &in);
645 double *e = (double *)s->enabled->extended_data[0];
647 if (s->pts == AV_NOPTS_VALUE)
650 ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
652 for (int i = 0; i < in->nb_samples; i++)
653 e[i] = !ctx->is_disabled;
655 av_audio_fifo_write(s->efifo, (void**)s->enabled->extended_data, in->nb_samples);
661 if (av_audio_fifo_size(s->fifo) >= s->window_size ||
663 return filter_frame(inlink);
665 if (av_audio_fifo_size(s->fifo) >= s->window_size) {
666 ff_filter_set_ready(ctx, 100);
670 if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
671 if (status == AVERROR_EOF) {
673 s->samples_left = av_audio_fifo_size(s->fifo) - s->overlap_skip;
674 ff_filter_set_ready(ctx, 100);
679 if (s->eof && s->samples_left <= 0) {
680 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
685 FF_FILTER_FORWARD_WANTED(outlink, inlink);
687 return FFERROR_NOT_READY;
690 static av_cold int init(AVFilterContext *ctx)
692 AudioDeclickContext *s = ctx->priv;
694 s->is_declip = !strcmp(ctx->filter->name, "adeclip");
696 s->detector = detect_clips;
698 s->detector = detect_clicks;
704 static av_cold void uninit(AVFilterContext *ctx)
706 AudioDeclickContext *s = ctx->priv;
709 av_log(ctx, AV_LOG_INFO, "Detected %s in %"PRId64" of %"PRId64" samples (%g%%).\n",
710 s->is_declip ? "clips" : "clicks", s->detected_errors,
711 s->nb_samples, 100. * s->detected_errors / s->nb_samples);
713 av_audio_fifo_free(s->fifo);
714 av_audio_fifo_free(s->efifo);
715 av_freep(&s->window_func_lut);
716 av_frame_free(&s->enabled);
717 av_frame_free(&s->in);
718 av_frame_free(&s->out);
719 av_frame_free(&s->buffer);
720 av_frame_free(&s->is);
723 for (i = 0; i < s->nb_channels; i++) {
724 DeclickChannel *c = &s->chan[i];
726 av_freep(&c->detection);
727 av_freep(&c->auxiliary);
728 av_freep(&c->acoefficients);
729 av_freep(&c->acorrelation);
733 av_freep(&c->interpolated);
734 av_freep(&c->matrix);
736 av_freep(&c->histogram);
737 c->histogram_size = 0;
738 av_freep(&c->vector);
748 static const AVFilterPad inputs[] = {
751 .type = AVMEDIA_TYPE_AUDIO,
752 .config_props = config_input,
757 static const AVFilterPad outputs[] = {
760 .type = AVMEDIA_TYPE_AUDIO,
765 AVFilter ff_af_adeclick = {
767 .description = NULL_IF_CONFIG_SMALL("Remove impulsive noise from input audio."),
768 .query_formats = query_formats,
769 .priv_size = sizeof(AudioDeclickContext),
770 .priv_class = &adeclick_class,
772 .activate = activate,
776 .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
779 static const AVOption adeclip_options[] = {
780 { "window", "set window size", OFFSET(w), AV_OPT_TYPE_DOUBLE, {.dbl=55}, 10, 100, AF },
781 { "w", "set window size", OFFSET(w), AV_OPT_TYPE_DOUBLE, {.dbl=55}, 10, 100, AF },
782 { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_DOUBLE, {.dbl=75}, 50, 95, AF },
783 { "o", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_DOUBLE, {.dbl=75}, 50, 95, AF },
784 { "arorder", "set autoregression order", OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=8}, 0, 25, AF },
785 { "a", "set autoregression order", OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=8}, 0, 25, AF },
786 { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=10}, 1, 100, AF },
787 { "t", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=10}, 1, 100, AF },
788 { "hsize", "set histogram size", OFFSET(nb_hbins), AV_OPT_TYPE_INT, {.i64=1000}, 100, 9999, AF },
789 { "n", "set histogram size", OFFSET(nb_hbins), AV_OPT_TYPE_INT, {.i64=1000}, 100, 9999, AF },
790 { "method", "set overlap method", OFFSET(method), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AF, "m" },
791 { "m", "set overlap method", OFFSET(method), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AF, "m" },
792 { "add", "overlap-add", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "m" },
793 { "a", "overlap-add", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "m" },
794 { "save", "overlap-save", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "m" },
795 { "s", "overlap-save", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "m" },
799 AVFILTER_DEFINE_CLASS(adeclip);
801 AVFilter ff_af_adeclip = {
803 .description = NULL_IF_CONFIG_SMALL("Remove clipping from input audio."),
804 .query_formats = query_formats,
805 .priv_size = sizeof(AudioDeclickContext),
806 .priv_class = &adeclip_class,
808 .activate = activate,
812 .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,