You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

798 lines
24KB

  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. #include "libavutil/audio_fifo.h"
  21. #include "libavutil/opt.h"
  22. #include "avfilter.h"
  23. #include "audio.h"
  24. #include "filters.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. typedef struct DeclickChannel {
  28. double *auxiliary;
  29. double *detection;
  30. double *acoefficients;
  31. double *acorrelation;
  32. double *tmp;
  33. double *interpolated;
  34. double *matrix;
  35. int matrix_size;
  36. double *vector;
  37. int vector_size;
  38. double *y;
  39. int y_size;
  40. uint8_t *click;
  41. int *index;
  42. unsigned *histogram;
  43. int histogram_size;
  44. } DeclickChannel;
  45. typedef struct AudioDeclickContext {
  46. const AVClass *class;
  47. double w;
  48. double overlap;
  49. double threshold;
  50. double ar;
  51. double burst;
  52. int method;
  53. int nb_hbins;
  54. int is_declip;
  55. int ar_order;
  56. int nb_burst_samples;
  57. int window_size;
  58. int hop_size;
  59. int overlap_skip;
  60. AVFrame *enabled;
  61. AVFrame *in;
  62. AVFrame *out;
  63. AVFrame *buffer;
  64. AVFrame *is;
  65. DeclickChannel *chan;
  66. int64_t pts;
  67. int nb_channels;
  68. uint64_t nb_samples;
  69. uint64_t detected_errors;
  70. int samples_left;
  71. int eof;
  72. AVAudioFifo *efifo;
  73. AVAudioFifo *fifo;
  74. double *window_func_lut;
  75. int (*detector)(struct AudioDeclickContext *s, DeclickChannel *c,
  76. double sigmae, double *detection,
  77. double *acoefficients, uint8_t *click, int *index,
  78. const double *src, double *dst);
  79. } AudioDeclickContext;
  80. #define OFFSET(x) offsetof(AudioDeclickContext, x)
  81. #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  82. static const AVOption adeclick_options[] = {
  83. { "w", "set window size", OFFSET(w), AV_OPT_TYPE_DOUBLE, {.dbl=55}, 10, 100, AF },
  84. { "o", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_DOUBLE, {.dbl=75}, 50, 95, AF },
  85. { "a", "set autoregression order", OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, 25, AF },
  86. { "t", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 100, AF },
  87. { "b", "set burst fusion", OFFSET(burst), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, 10, AF },
  88. { "m", "set overlap method", OFFSET(method), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AF, "m" },
  89. { "a", "overlap-add", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "m" },
  90. { "s", "overlap-save", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "m" },
  91. { NULL }
  92. };
  93. AVFILTER_DEFINE_CLASS(adeclick);
  94. static int query_formats(AVFilterContext *ctx)
  95. {
  96. AVFilterFormats *formats = NULL;
  97. AVFilterChannelLayouts *layouts = NULL;
  98. static const enum AVSampleFormat sample_fmts[] = {
  99. AV_SAMPLE_FMT_DBLP,
  100. AV_SAMPLE_FMT_NONE
  101. };
  102. int ret;
  103. formats = ff_make_format_list(sample_fmts);
  104. if (!formats)
  105. return AVERROR(ENOMEM);
  106. ret = ff_set_common_formats(ctx, formats);
  107. if (ret < 0)
  108. return ret;
  109. layouts = ff_all_channel_counts();
  110. if (!layouts)
  111. return AVERROR(ENOMEM);
  112. ret = ff_set_common_channel_layouts(ctx, layouts);
  113. if (ret < 0)
  114. return ret;
  115. formats = ff_all_samplerates();
  116. return ff_set_common_samplerates(ctx, formats);
  117. }
  118. static int config_input(AVFilterLink *inlink)
  119. {
  120. AVFilterContext *ctx = inlink->dst;
  121. AudioDeclickContext *s = ctx->priv;
  122. int i;
  123. s->pts = AV_NOPTS_VALUE;
  124. s->window_size = inlink->sample_rate * s->w / 1000.;
  125. if (s->window_size < 100)
  126. return AVERROR(EINVAL);
  127. s->ar_order = FFMAX(s->window_size * s->ar / 100., 1);
  128. s->nb_burst_samples = s->window_size * s->burst / 1000.;
  129. s->hop_size = s->window_size * (1. - (s->overlap / 100.));
  130. if (s->hop_size < 1)
  131. return AVERROR(EINVAL);
  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. av_frame_free(&s->in);
  139. av_frame_free(&s->out);
  140. av_frame_free(&s->buffer);
  141. av_frame_free(&s->is);
  142. s->enabled = ff_get_audio_buffer(inlink, s->window_size);
  143. s->in = ff_get_audio_buffer(inlink, s->window_size);
  144. s->out = ff_get_audio_buffer(inlink, s->window_size);
  145. s->buffer = ff_get_audio_buffer(inlink, s->window_size * 2);
  146. s->is = ff_get_audio_buffer(inlink, s->window_size);
  147. if (!s->in || !s->out || !s->buffer || !s->is || !s->enabled)
  148. return AVERROR(ENOMEM);
  149. s->efifo = av_audio_fifo_alloc(inlink->format, 1, s->window_size);
  150. if (!s->efifo)
  151. return AVERROR(ENOMEM);
  152. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->window_size);
  153. if (!s->fifo)
  154. return AVERROR(ENOMEM);
  155. s->overlap_skip = s->method ? (s->window_size - s->hop_size) / 2 : 0;
  156. if (s->overlap_skip > 0) {
  157. av_audio_fifo_write(s->fifo, (void **)s->in->extended_data,
  158. s->overlap_skip);
  159. }
  160. s->nb_channels = inlink->channels;
  161. s->chan = av_calloc(inlink->channels, sizeof(*s->chan));
  162. if (!s->chan)
  163. return AVERROR(ENOMEM);
  164. for (i = 0; i < inlink->channels; i++) {
  165. DeclickChannel *c = &s->chan[i];
  166. c->detection = av_calloc(s->window_size, sizeof(*c->detection));
  167. c->auxiliary = av_calloc(s->ar_order + 1, sizeof(*c->auxiliary));
  168. c->acoefficients = av_calloc(s->ar_order + 1, sizeof(*c->acoefficients));
  169. c->acorrelation = av_calloc(s->ar_order + 1, sizeof(*c->acorrelation));
  170. c->tmp = av_calloc(s->ar_order, sizeof(*c->tmp));
  171. c->click = av_calloc(s->window_size, sizeof(*c->click));
  172. c->index = av_calloc(s->window_size, sizeof(*c->index));
  173. c->interpolated = av_calloc(s->window_size, sizeof(*c->interpolated));
  174. if (!c->auxiliary || !c->acoefficients || !c->detection || !c->click ||
  175. !c->index || !c->interpolated || !c->acorrelation || !c->tmp)
  176. return AVERROR(ENOMEM);
  177. }
  178. return 0;
  179. }
  180. static void autocorrelation(const double *input, int order, int size,
  181. double *output, double scale)
  182. {
  183. int i, j;
  184. for (i = 0; i <= order; i++) {
  185. double value = 0.;
  186. for (j = i; j < size; j++)
  187. value += input[j] * input[j - i];
  188. output[i] = value * scale;
  189. }
  190. }
  191. static double autoregression(const double *samples, int ar_order,
  192. int nb_samples, double *k, double *r, double *a)
  193. {
  194. double alpha;
  195. int i, j;
  196. memset(a, 0, ar_order * sizeof(*a));
  197. autocorrelation(samples, ar_order, nb_samples, r, 1. / nb_samples);
  198. /* Levinson-Durbin algorithm */
  199. k[0] = a[0] = -r[1] / r[0];
  200. alpha = r[0] * (1. - k[0] * k[0]);
  201. for (i = 1; i < ar_order; i++) {
  202. double epsilon = 0.;
  203. for (j = 0; j < i; j++)
  204. epsilon += a[j] * r[i - j];
  205. epsilon += r[i + 1];
  206. k[i] = -epsilon / alpha;
  207. alpha *= (1. - k[i] * k[i]);
  208. for (j = i - 1; j >= 0; j--)
  209. k[j] = a[j] + k[i] * a[i - j - 1];
  210. for (j = 0; j <= i; j++)
  211. a[j] = k[j];
  212. }
  213. k[0] = 1.;
  214. for (i = 1; i <= ar_order; i++)
  215. k[i] = a[i - 1];
  216. return sqrt(alpha);
  217. }
  218. static int isfinite_array(double *samples, int nb_samples)
  219. {
  220. int i;
  221. for (i = 0; i < nb_samples; i++)
  222. if (!isfinite(samples[i]))
  223. return 0;
  224. return 1;
  225. }
  226. static int find_index(int *index, int value, int size)
  227. {
  228. int i, start, end;
  229. if ((value < index[0]) || (value > index[size - 1]))
  230. return 1;
  231. i = start = 0;
  232. end = size - 1;
  233. while (start <= end) {
  234. i = (end + start) / 2;
  235. if (index[i] == value)
  236. return 0;
  237. if (value < index[i])
  238. end = i - 1;
  239. if (value > index[i])
  240. start = i + 1;
  241. }
  242. return 1;
  243. }
  244. static int factorization(double *matrix, int n)
  245. {
  246. int i, j, k;
  247. for (i = 0; i < n; i++) {
  248. const int in = i * n;
  249. double value;
  250. value = matrix[in + i];
  251. for (j = 0; j < i; j++)
  252. value -= matrix[j * n + j] * matrix[in + j] * matrix[in + j];
  253. if (value == 0.) {
  254. return -1;
  255. }
  256. matrix[in + i] = value;
  257. for (j = i + 1; j < n; j++) {
  258. const int jn = j * n;
  259. double x;
  260. x = matrix[jn + i];
  261. for (k = 0; k < i; k++)
  262. x -= matrix[k * n + k] * matrix[in + k] * matrix[jn + k];
  263. matrix[jn + i] = x / matrix[in + i];
  264. }
  265. }
  266. return 0;
  267. }
  268. static int do_interpolation(DeclickChannel *c, double *matrix,
  269. double *vector, int n, double *out)
  270. {
  271. int i, j, ret;
  272. double *y;
  273. ret = factorization(matrix, n);
  274. if (ret < 0)
  275. return ret;
  276. av_fast_malloc(&c->y, &c->y_size, n * sizeof(*c->y));
  277. y = c->y;
  278. if (!y)
  279. return AVERROR(ENOMEM);
  280. for (i = 0; i < n; i++) {
  281. const int in = i * n;
  282. double value;
  283. value = vector[i];
  284. for (j = 0; j < i; j++)
  285. value -= matrix[in + j] * y[j];
  286. y[i] = value;
  287. }
  288. for (i = n - 1; i >= 0; i--) {
  289. out[i] = y[i] / matrix[i * n + i];
  290. for (j = i + 1; j < n; j++)
  291. out[i] -= matrix[j * n + i] * out[j];
  292. }
  293. return 0;
  294. }
  295. static int interpolation(DeclickChannel *c, const double *src, int ar_order,
  296. double *acoefficients, int *index, int nb_errors,
  297. double *auxiliary, double *interpolated)
  298. {
  299. double *vector, *matrix;
  300. int i, j;
  301. av_fast_malloc(&c->matrix, &c->matrix_size, nb_errors * nb_errors * sizeof(*c->matrix));
  302. matrix = c->matrix;
  303. if (!matrix)
  304. return AVERROR(ENOMEM);
  305. av_fast_malloc(&c->vector, &c->vector_size, nb_errors * sizeof(*c->vector));
  306. vector = c->vector;
  307. if (!vector)
  308. return AVERROR(ENOMEM);
  309. autocorrelation(acoefficients, ar_order, ar_order + 1, auxiliary, 1.);
  310. for (i = 0; i < nb_errors; i++) {
  311. const int im = i * nb_errors;
  312. for (j = i; j < nb_errors; j++) {
  313. if (abs(index[j] - index[i]) <= ar_order) {
  314. matrix[j * nb_errors + i] = matrix[im + j] = auxiliary[abs(index[j] - index[i])];
  315. } else {
  316. matrix[j * nb_errors + i] = matrix[im + j] = 0;
  317. }
  318. }
  319. }
  320. for (i = 0; i < nb_errors; i++) {
  321. double value = 0.;
  322. for (j = -ar_order; j <= ar_order; j++)
  323. if (find_index(index, index[i] - j, nb_errors))
  324. value -= src[index[i] - j] * auxiliary[abs(j)];
  325. vector[i] = value;
  326. }
  327. return do_interpolation(c, matrix, vector, nb_errors, interpolated);
  328. }
  329. static int detect_clips(AudioDeclickContext *s, DeclickChannel *c,
  330. double unused0,
  331. double *unused1, double *unused2,
  332. uint8_t *clip, int *index,
  333. const double *src, double *dst)
  334. {
  335. const double threshold = s->threshold;
  336. double max_amplitude = 0;
  337. unsigned *histogram;
  338. int i, nb_clips = 0;
  339. av_fast_malloc(&c->histogram, &c->histogram_size, s->nb_hbins * sizeof(*c->histogram));
  340. if (!c->histogram)
  341. return AVERROR(ENOMEM);
  342. histogram = c->histogram;
  343. memset(histogram, 0, sizeof(*histogram) * s->nb_hbins);
  344. for (i = 0; i < s->window_size; i++) {
  345. const unsigned index = fmin(fabs(src[i]), 1) * (s->nb_hbins - 1);
  346. histogram[index]++;
  347. dst[i] = src[i];
  348. clip[i] = 0;
  349. }
  350. for (i = s->nb_hbins - 1; i > 1; i--) {
  351. if (histogram[i]) {
  352. if (histogram[i] / (double)FFMAX(histogram[i - 1], 1) > threshold) {
  353. max_amplitude = i / (double)s->nb_hbins;
  354. }
  355. break;
  356. }
  357. }
  358. if (max_amplitude > 0.) {
  359. for (i = 0; i < s->window_size; i++) {
  360. clip[i] = fabs(src[i]) >= max_amplitude;
  361. }
  362. }
  363. memset(clip, 0, s->ar_order * sizeof(*clip));
  364. memset(clip + (s->window_size - s->ar_order), 0, s->ar_order * sizeof(*clip));
  365. for (i = s->ar_order; i < s->window_size - s->ar_order; i++)
  366. if (clip[i])
  367. index[nb_clips++] = i;
  368. return nb_clips;
  369. }
  370. static int detect_clicks(AudioDeclickContext *s, DeclickChannel *c,
  371. double sigmae,
  372. double *detection, double *acoefficients,
  373. uint8_t *click, int *index,
  374. const double *src, double *dst)
  375. {
  376. const double threshold = s->threshold;
  377. int i, j, nb_clicks = 0, prev = -1;
  378. memset(detection, 0, s->window_size * sizeof(*detection));
  379. for (i = s->ar_order; i < s->window_size; i++) {
  380. for (j = 0; j <= s->ar_order; j++) {
  381. detection[i] += acoefficients[j] * src[i - j];
  382. }
  383. }
  384. for (i = 0; i < s->window_size; i++) {
  385. click[i] = fabs(detection[i]) > sigmae * threshold;
  386. dst[i] = src[i];
  387. }
  388. for (i = 0; i < s->window_size; i++) {
  389. if (!click[i])
  390. continue;
  391. if (prev >= 0 && (i > prev + 1) && (i <= s->nb_burst_samples + prev))
  392. for (j = prev + 1; j < i; j++)
  393. click[j] = 1;
  394. prev = i;
  395. }
  396. memset(click, 0, s->ar_order * sizeof(*click));
  397. memset(click + (s->window_size - s->ar_order), 0, s->ar_order * sizeof(*click));
  398. for (i = s->ar_order; i < s->window_size - s->ar_order; i++)
  399. if (click[i])
  400. index[nb_clicks++] = i;
  401. return nb_clicks;
  402. }
  403. typedef struct ThreadData {
  404. AVFrame *out;
  405. } ThreadData;
  406. static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
  407. {
  408. AudioDeclickContext *s = ctx->priv;
  409. ThreadData *td = arg;
  410. AVFrame *out = td->out;
  411. const double *src = (const double *)s->in->extended_data[ch];
  412. double *is = (double *)s->is->extended_data[ch];
  413. double *dst = (double *)s->out->extended_data[ch];
  414. double *ptr = (double *)out->extended_data[ch];
  415. double *buf = (double *)s->buffer->extended_data[ch];
  416. const double *w = s->window_func_lut;
  417. DeclickChannel *c = &s->chan[ch];
  418. double sigmae;
  419. int j, ret;
  420. sigmae = autoregression(src, s->ar_order, s->window_size, c->acoefficients, c->acorrelation, c->tmp);
  421. if (isfinite_array(c->acoefficients, s->ar_order + 1)) {
  422. double *interpolated = c->interpolated;
  423. int *index = c->index;
  424. int nb_errors;
  425. nb_errors = s->detector(s, c, sigmae, c->detection, c->acoefficients,
  426. c->click, index, src, dst);
  427. if (nb_errors > 0) {
  428. double *enabled = (double *)s->enabled->extended_data[0];
  429. ret = interpolation(c, src, s->ar_order, c->acoefficients, index,
  430. nb_errors, c->auxiliary, interpolated);
  431. if (ret < 0)
  432. return ret;
  433. av_audio_fifo_peek(s->efifo, (void**)s->enabled->extended_data, s->window_size);
  434. for (j = 0; j < nb_errors; j++) {
  435. if (enabled[index[j]]) {
  436. dst[index[j]] = interpolated[j];
  437. is[index[j]] = 1;
  438. }
  439. }
  440. }
  441. } else {
  442. memcpy(dst, src, s->window_size * sizeof(*dst));
  443. }
  444. if (s->method == 0) {
  445. for (j = 0; j < s->window_size; j++)
  446. buf[j] += dst[j] * w[j];
  447. } else {
  448. const int skip = s->overlap_skip;
  449. for (j = 0; j < s->hop_size; j++)
  450. buf[j] = dst[skip + j];
  451. }
  452. for (j = 0; j < s->hop_size; j++)
  453. ptr[j] = buf[j];
  454. memmove(buf, buf + s->hop_size, (s->window_size * 2 - s->hop_size) * sizeof(*buf));
  455. memmove(is, is + s->hop_size, (s->window_size - s->hop_size) * sizeof(*is));
  456. memset(buf + s->window_size * 2 - s->hop_size, 0, s->hop_size * sizeof(*buf));
  457. memset(is + s->window_size - s->hop_size, 0, s->hop_size * sizeof(*is));
  458. return 0;
  459. }
  460. static int filter_frame(AVFilterLink *inlink)
  461. {
  462. AVFilterContext *ctx = inlink->dst;
  463. AVFilterLink *outlink = ctx->outputs[0];
  464. AudioDeclickContext *s = ctx->priv;
  465. AVFrame *out = NULL;
  466. int ret = 0, j, ch, detected_errors = 0;
  467. ThreadData td;
  468. out = ff_get_audio_buffer(outlink, s->hop_size);
  469. if (!out)
  470. return AVERROR(ENOMEM);
  471. ret = av_audio_fifo_peek(s->fifo, (void **)s->in->extended_data,
  472. s->window_size);
  473. if (ret < 0)
  474. goto fail;
  475. td.out = out;
  476. ret = ctx->internal->execute(ctx, filter_channel, &td, NULL, inlink->channels);
  477. if (ret < 0)
  478. goto fail;
  479. for (ch = 0; ch < s->in->channels; ch++) {
  480. double *is = (double *)s->is->extended_data[ch];
  481. for (j = 0; j < s->hop_size; j++) {
  482. if (is[j])
  483. detected_errors++;
  484. }
  485. }
  486. av_audio_fifo_drain(s->fifo, s->hop_size);
  487. av_audio_fifo_drain(s->efifo, s->hop_size);
  488. if (s->samples_left > 0)
  489. out->nb_samples = FFMIN(s->hop_size, s->samples_left);
  490. out->pts = s->pts;
  491. s->pts += av_rescale_q(s->hop_size, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  492. s->detected_errors += detected_errors;
  493. s->nb_samples += out->nb_samples * inlink->channels;
  494. ret = ff_filter_frame(outlink, out);
  495. if (ret < 0)
  496. return ret;
  497. if (s->samples_left > 0) {
  498. s->samples_left -= s->hop_size;
  499. if (s->samples_left <= 0)
  500. av_audio_fifo_drain(s->fifo, av_audio_fifo_size(s->fifo));
  501. }
  502. fail:
  503. if (ret < 0)
  504. av_frame_free(&out);
  505. return ret;
  506. }
  507. static int activate(AVFilterContext *ctx)
  508. {
  509. AVFilterLink *inlink = ctx->inputs[0];
  510. AVFilterLink *outlink = ctx->outputs[0];
  511. AudioDeclickContext *s = ctx->priv;
  512. AVFrame *in;
  513. int ret, status;
  514. int64_t pts;
  515. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  516. ret = ff_inlink_consume_samples(inlink, s->window_size, s->window_size, &in);
  517. if (ret < 0)
  518. return ret;
  519. if (ret > 0) {
  520. double *e = (double *)s->enabled->extended_data[0];
  521. if (s->pts == AV_NOPTS_VALUE)
  522. s->pts = in->pts;
  523. ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
  524. in->nb_samples);
  525. for (int i = 0; i < in->nb_samples; i++)
  526. e[i] = !ctx->is_disabled;
  527. av_audio_fifo_write(s->efifo, (void**)s->enabled->extended_data, in->nb_samples);
  528. av_frame_free(&in);
  529. if (ret < 0)
  530. return ret;
  531. }
  532. if (av_audio_fifo_size(s->fifo) >= s->window_size ||
  533. s->samples_left > 0)
  534. return filter_frame(inlink);
  535. if (av_audio_fifo_size(s->fifo) >= s->window_size) {
  536. ff_filter_set_ready(ctx, 100);
  537. return 0;
  538. }
  539. if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
  540. if (status == AVERROR_EOF) {
  541. s->eof = 1;
  542. s->samples_left = av_audio_fifo_size(s->fifo) - s->overlap_skip;
  543. ff_filter_set_ready(ctx, 100);
  544. return 0;
  545. }
  546. }
  547. if (s->eof && s->samples_left <= 0) {
  548. ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
  549. return 0;
  550. }
  551. if (!s->eof)
  552. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  553. return FFERROR_NOT_READY;
  554. }
  555. static av_cold int init(AVFilterContext *ctx)
  556. {
  557. AudioDeclickContext *s = ctx->priv;
  558. s->is_declip = !strcmp(ctx->filter->name, "adeclip");
  559. if (s->is_declip) {
  560. s->detector = detect_clips;
  561. } else {
  562. s->detector = detect_clicks;
  563. }
  564. return 0;
  565. }
  566. static av_cold void uninit(AVFilterContext *ctx)
  567. {
  568. AudioDeclickContext *s = ctx->priv;
  569. int i;
  570. av_log(ctx, AV_LOG_INFO, "Detected %s in %"PRId64" of %"PRId64" samples (%g%%).\n",
  571. s->is_declip ? "clips" : "clicks", s->detected_errors,
  572. s->nb_samples, 100. * s->detected_errors / s->nb_samples);
  573. av_audio_fifo_free(s->fifo);
  574. av_audio_fifo_free(s->efifo);
  575. av_freep(&s->window_func_lut);
  576. av_frame_free(&s->enabled);
  577. av_frame_free(&s->in);
  578. av_frame_free(&s->out);
  579. av_frame_free(&s->buffer);
  580. av_frame_free(&s->is);
  581. if (s->chan) {
  582. for (i = 0; i < s->nb_channels; i++) {
  583. DeclickChannel *c = &s->chan[i];
  584. av_freep(&c->detection);
  585. av_freep(&c->auxiliary);
  586. av_freep(&c->acoefficients);
  587. av_freep(&c->acorrelation);
  588. av_freep(&c->tmp);
  589. av_freep(&c->click);
  590. av_freep(&c->index);
  591. av_freep(&c->interpolated);
  592. av_freep(&c->matrix);
  593. c->matrix_size = 0;
  594. av_freep(&c->histogram);
  595. c->histogram_size = 0;
  596. av_freep(&c->vector);
  597. c->vector_size = 0;
  598. av_freep(&c->y);
  599. c->y_size = 0;
  600. }
  601. }
  602. av_freep(&s->chan);
  603. s->nb_channels = 0;
  604. }
  605. static const AVFilterPad inputs[] = {
  606. {
  607. .name = "default",
  608. .type = AVMEDIA_TYPE_AUDIO,
  609. .config_props = config_input,
  610. },
  611. { NULL }
  612. };
  613. static const AVFilterPad outputs[] = {
  614. {
  615. .name = "default",
  616. .type = AVMEDIA_TYPE_AUDIO,
  617. },
  618. { NULL }
  619. };
  620. AVFilter ff_af_adeclick = {
  621. .name = "adeclick",
  622. .description = NULL_IF_CONFIG_SMALL("Remove impulsive noise from input audio."),
  623. .query_formats = query_formats,
  624. .priv_size = sizeof(AudioDeclickContext),
  625. .priv_class = &adeclick_class,
  626. .init = init,
  627. .activate = activate,
  628. .uninit = uninit,
  629. .inputs = inputs,
  630. .outputs = outputs,
  631. .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  632. };
  633. static const AVOption adeclip_options[] = {
  634. { "w", "set window size", OFFSET(w), AV_OPT_TYPE_DOUBLE, {.dbl=55}, 10, 100, AF },
  635. { "o", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_DOUBLE, {.dbl=75}, 50, 95, AF },
  636. { "a", "set autoregression order", OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=8}, 0, 25, AF },
  637. { "t", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=10}, 1, 100, AF },
  638. { "n", "set histogram size", OFFSET(nb_hbins), AV_OPT_TYPE_INT, {.i64=1000}, 100, 9999, AF },
  639. { "m", "set overlap method", OFFSET(method), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AF, "m" },
  640. { "a", "overlap-add", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "m" },
  641. { "s", "overlap-save", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "m" },
  642. { NULL }
  643. };
  644. AVFILTER_DEFINE_CLASS(adeclip);
  645. AVFilter ff_af_adeclip = {
  646. .name = "adeclip",
  647. .description = NULL_IF_CONFIG_SMALL("Remove clipping from input audio."),
  648. .query_formats = query_formats,
  649. .priv_size = sizeof(AudioDeclickContext),
  650. .priv_class = &adeclip_class,
  651. .init = init,
  652. .activate = activate,
  653. .uninit = uninit,
  654. .inputs = inputs,
  655. .outputs = outputs,
  656. .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  657. };