af_adeclick.c 23 KB

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