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.

814 lines
25KB

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