af_afftdn.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  1. /*
  2. * Copyright (c) 2018 The FFmpeg Project
  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 <float.h>
  21. #include "libavutil/audio_fifo.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/channel_layout.h"
  24. #include "libavutil/opt.h"
  25. #include "libavcodec/avfft.h"
  26. #include "avfilter.h"
  27. #include "audio.h"
  28. #include "formats.h"
  29. #include "filters.h"
  30. #define C (M_LN10 * 0.1)
  31. #define RATIO 0.98
  32. #define RRATIO (1.0 - RATIO)
  33. enum OutModes {
  34. IN_MODE,
  35. OUT_MODE,
  36. NOISE_MODE,
  37. NB_MODES
  38. };
  39. enum NoiseType {
  40. WHITE_NOISE,
  41. VINYL_NOISE,
  42. SHELLAC_NOISE,
  43. CUSTOM_NOISE,
  44. NB_NOISE
  45. };
  46. typedef struct DeNoiseChannel {
  47. int band_noise[15];
  48. double noise_band_auto_var[15];
  49. double noise_band_sample[15];
  50. double *amt;
  51. double *band_amt;
  52. double *band_excit;
  53. double *gain;
  54. double *prior;
  55. double *prior_band_excit;
  56. double *clean_data;
  57. double *noisy_data;
  58. double *out_samples;
  59. double *spread_function;
  60. double *abs_var;
  61. double *rel_var;
  62. double *min_abs_var;
  63. FFTComplex *fft_data;
  64. FFTContext *fft, *ifft;
  65. double noise_band_norm[15];
  66. double noise_band_avr[15];
  67. double noise_band_avi[15];
  68. double noise_band_var[15];
  69. double sfm_threshold;
  70. double sfm_alpha;
  71. double sfm_results[3];
  72. int sfm_fail_flags[512];
  73. int sfm_fail_total;
  74. } DeNoiseChannel;
  75. typedef struct AudioFFTDeNoiseContext {
  76. const AVClass *class;
  77. float noise_reduction;
  78. float noise_floor;
  79. int noise_type;
  80. char *band_noise_str;
  81. float residual_floor;
  82. int track_noise;
  83. int track_residual;
  84. int output_mode;
  85. float last_residual_floor;
  86. float last_noise_floor;
  87. float last_noise_reduction;
  88. float last_noise_balance;
  89. int64_t block_count;
  90. int64_t pts;
  91. int channels;
  92. int sample_noise;
  93. int sample_noise_start;
  94. int sample_noise_end;
  95. float sample_rate;
  96. int buffer_length;
  97. int fft_length;
  98. int fft_length2;
  99. int bin_count;
  100. int window_length;
  101. int sample_advance;
  102. int number_of_bands;
  103. int band_centre[15];
  104. int *bin2band;
  105. double *window;
  106. double *band_alpha;
  107. double *band_beta;
  108. DeNoiseChannel *dnch;
  109. double max_gain;
  110. double max_var;
  111. double gain_scale;
  112. double window_weight;
  113. double floor;
  114. double sample_floor;
  115. double auto_floor;
  116. int noise_band_edge[17];
  117. int noise_band_count;
  118. double matrix_a[25];
  119. double vector_b[5];
  120. double matrix_b[75];
  121. double matrix_c[75];
  122. AVAudioFifo *fifo;
  123. } AudioFFTDeNoiseContext;
  124. #define OFFSET(x) offsetof(AudioFFTDeNoiseContext, x)
  125. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  126. static const AVOption afftdn_options[] = {
  127. { "nr", "set the noise reduction", OFFSET(noise_reduction), AV_OPT_TYPE_FLOAT, {.dbl = 12}, .01, 97, A },
  128. { "nf", "set the noise floor", OFFSET(noise_floor), AV_OPT_TYPE_FLOAT, {.dbl =-50}, -80,-20, A },
  129. { "nt", "set the noise type", OFFSET(noise_type), AV_OPT_TYPE_INT, {.i64 = WHITE_NOISE}, WHITE_NOISE, NB_NOISE-1, A, "type" },
  130. { "w", "white noise", 0, AV_OPT_TYPE_CONST, {.i64 = WHITE_NOISE}, 0, 0, A, "type" },
  131. { "v", "vinyl noise", 0, AV_OPT_TYPE_CONST, {.i64 = VINYL_NOISE}, 0, 0, A, "type" },
  132. { "s", "shellac noise", 0, AV_OPT_TYPE_CONST, {.i64 = SHELLAC_NOISE}, 0, 0, A, "type" },
  133. { "c", "custom noise", 0, AV_OPT_TYPE_CONST, {.i64 = CUSTOM_NOISE}, 0, 0, A, "type" },
  134. { "bn", "set the custom bands noise", OFFSET(band_noise_str), AV_OPT_TYPE_STRING, {.str = 0}, 0, 0, A },
  135. { "rf", "set the residual floor", OFFSET(residual_floor), AV_OPT_TYPE_FLOAT, {.dbl =-38}, -80,-20, A },
  136. { "tn", "track noise", OFFSET(track_noise), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, A },
  137. { "tr", "track residual", OFFSET(track_residual), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, A },
  138. { "om", "set output mode", OFFSET(output_mode), AV_OPT_TYPE_INT, {.i64 = OUT_MODE}, 0, NB_MODES-1, A, "mode" },
  139. { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64 = IN_MODE}, 0, 0, A, "mode" },
  140. { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64 = OUT_MODE}, 0, 0, A, "mode" },
  141. { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64 = NOISE_MODE}, 0, 0, A, "mode" },
  142. { NULL }
  143. };
  144. AVFILTER_DEFINE_CLASS(afftdn);
  145. static int get_band_noise(AudioFFTDeNoiseContext *s,
  146. int band, double a,
  147. double b, double c)
  148. {
  149. double d1, d2, d3;
  150. d1 = a / s->band_centre[band];
  151. d1 = 10.0 * log(1.0 + d1 * d1) / M_LN10;
  152. d2 = b / s->band_centre[band];
  153. d2 = 10.0 * log(1.0 + d2 * d2) / M_LN10;
  154. d3 = s->band_centre[band] / c;
  155. d3 = 10.0 * log(1.0 + d3 * d3) / M_LN10;
  156. return lrint(-d1 + d2 - d3);
  157. }
  158. static void factor(double *array, int size)
  159. {
  160. for (int i = 0; i < size - 1; i++) {
  161. for (int j = i + 1; j < size; j++) {
  162. double d = array[j + i * size] / array[i + i * size];
  163. array[j + i * size] = d;
  164. for (int k = i + 1; k < size; k++) {
  165. array[j + k * size] -= d * array[i + k * size];
  166. }
  167. }
  168. }
  169. }
  170. static void solve(double *matrix, double *vector, int size)
  171. {
  172. for (int i = 0; i < size - 1; i++) {
  173. for (int j = i + 1; j < size; j++) {
  174. double d = matrix[j + i * size];
  175. vector[j] -= d * vector[i];
  176. }
  177. }
  178. vector[size - 1] /= matrix[size * size - 1];
  179. for (int i = size - 2; i >= 0; i--) {
  180. double d = vector[i];
  181. for (int j = i + 1; j < size; j++)
  182. d -= matrix[i + j * size] * vector[j];
  183. vector[i] = d / matrix[i + i * size];
  184. }
  185. }
  186. static int process_get_band_noise(AudioFFTDeNoiseContext *s,
  187. DeNoiseChannel *dnch,
  188. int band)
  189. {
  190. double product, sum, f;
  191. int i = 0;
  192. if (band < 15)
  193. return dnch->band_noise[band];
  194. for (int j = 0; j < 5; j++) {
  195. sum = 0.0;
  196. for (int k = 0; k < 15; k++)
  197. sum += s->matrix_b[i++] * dnch->band_noise[k];
  198. s->vector_b[j] = sum;
  199. }
  200. solve(s->matrix_a, s->vector_b, 5);
  201. f = (0.5 * s->sample_rate) / s->band_centre[14];
  202. f = 15.0 + log(f / 1.5) / log(1.5);
  203. sum = 0.0;
  204. product = 1.0;
  205. for (int j = 0; j < 5; j++) {
  206. sum += product * s->vector_b[j];
  207. product *= f;
  208. }
  209. return lrint(sum);
  210. }
  211. static void calculate_sfm(AudioFFTDeNoiseContext *s,
  212. DeNoiseChannel *dnch,
  213. int start, int end)
  214. {
  215. double d1 = 0.0, d2 = 1.0;
  216. int i = 0, j = 0;
  217. for (int k = start; k < end; k++) {
  218. if (dnch->noisy_data[k] > s->sample_floor) {
  219. j++;
  220. d1 += dnch->noisy_data[k];
  221. d2 *= dnch->noisy_data[k];
  222. if (d2 > 1.0E100) {
  223. d2 *= 1.0E-100;
  224. i++;
  225. } else if (d2 < 1.0E-100) {
  226. d2 *= 1.0E100;
  227. i--;
  228. }
  229. }
  230. }
  231. if (j > 1) {
  232. d1 /= j;
  233. dnch->sfm_results[0] = d1;
  234. d2 = log(d2) + 230.2585 * i;
  235. d2 /= j;
  236. d1 = log(d1);
  237. dnch->sfm_results[1] = d1;
  238. dnch->sfm_results[2] = d1 - d2;
  239. } else {
  240. dnch->sfm_results[0] = s->auto_floor;
  241. dnch->sfm_results[1] = dnch->sfm_threshold;
  242. dnch->sfm_results[2] = dnch->sfm_threshold;
  243. }
  244. }
  245. static double limit_gain(double a, double b)
  246. {
  247. if (a > 1.0)
  248. return (b * a - 1.0) / (b + a - 2.0);
  249. if (a < 1.0)
  250. return (b * a - 2.0 * a + 1.0) / (b - a);
  251. return 1.0;
  252. }
  253. static void process_frame(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch,
  254. FFTComplex *fft_data,
  255. double *prior, double *prior_band_excit, int track_noise)
  256. {
  257. double d1, d2, d3, gain;
  258. int n, i1;
  259. d1 = fft_data[0].re * fft_data[0].re;
  260. dnch->noisy_data[0] = d1;
  261. d2 = d1 / dnch->abs_var[0];
  262. d3 = RATIO * prior[0] + RRATIO * fmax(d2 - 1.0, 0.0);
  263. gain = d3 / (1.0 + d3);
  264. gain *= (gain + M_PI_4 / fmax(d2, 1.0E-6));
  265. prior[0] = (d2 * gain);
  266. dnch->clean_data[0] = (d1 * gain);
  267. gain = sqrt(gain);
  268. dnch->gain[0] = gain;
  269. n = 0;
  270. for (int i = 1; i < s->fft_length2; i++) {
  271. d1 = fft_data[i].re * fft_data[i].re + fft_data[i].im * fft_data[i].im;
  272. if (d1 > s->sample_floor)
  273. n = i;
  274. dnch->noisy_data[i] = d1;
  275. d2 = d1 / dnch->abs_var[i];
  276. d3 = RATIO * prior[i] + RRATIO * fmax(d2 - 1.0, 0.0);
  277. gain = d3 / (1.0 + d3);
  278. gain *= (gain + M_PI_4 / fmax(d2, 1.0E-6));
  279. prior[i] = d2 * gain;
  280. dnch->clean_data[i] = d1 * gain;
  281. gain = sqrt(gain);
  282. dnch->gain[i] = gain;
  283. }
  284. d1 = fft_data[0].im * fft_data[0].im;
  285. if (d1 > s->sample_floor)
  286. n = s->fft_length2;
  287. dnch->noisy_data[s->fft_length2] = d1;
  288. d2 = d1 / dnch->abs_var[s->fft_length2];
  289. d3 = RATIO * prior[s->fft_length2] + RRATIO * fmax(d2 - 1.0, 0.0);
  290. gain = d3 / (1.0 + d3);
  291. gain *= gain + M_PI_4 / fmax(d2, 1.0E-6);
  292. prior[s->fft_length2] = d2 * gain;
  293. dnch->clean_data[s->fft_length2] = d1 * gain;
  294. gain = sqrt(gain);
  295. dnch->gain[s->fft_length2] = gain;
  296. if (n > s->fft_length2 - 2) {
  297. n = s->bin_count;
  298. i1 = s->noise_band_count;
  299. } else {
  300. i1 = 0;
  301. for (int i = 0; i <= s->noise_band_count; i++) {
  302. if (n > 1.1 * s->noise_band_edge[i]) {
  303. i1 = i;
  304. }
  305. }
  306. }
  307. if (track_noise && (i1 > s->noise_band_count / 2)) {
  308. int j = FFMIN(n, s->noise_band_edge[i1]);
  309. int m = 3, k;
  310. for (k = i1 - 1; k >= 0; k--) {
  311. int i = s->noise_band_edge[k];
  312. calculate_sfm(s, dnch, i, j);
  313. dnch->noise_band_sample[k] = dnch->sfm_results[0];
  314. if (dnch->sfm_results[2] + 0.013 * m * fmax(0.0, dnch->sfm_results[1] - 20.53) >= dnch->sfm_threshold) {
  315. break;
  316. }
  317. j = i;
  318. m++;
  319. }
  320. if (k < i1 - 1) {
  321. double sum = 0.0, min, max;
  322. int i;
  323. for (i = i1 - 1; i > k; i--) {
  324. min = log(dnch->noise_band_sample[i] / dnch->noise_band_auto_var[i]);
  325. sum += min;
  326. }
  327. i = i1 - k - 1;
  328. if (i < 5) {
  329. min = 3.0E-4 * i * i;
  330. } else {
  331. min = 3.0E-4 * (8 * i - 16);
  332. }
  333. if (i < 3) {
  334. max = 2.0E-4 * i * i;
  335. } else {
  336. max = 2.0E-4 * (4 * i - 4);
  337. }
  338. if (s->track_residual) {
  339. if (s->last_noise_floor > s->last_residual_floor + 9) {
  340. min *= 0.5;
  341. max *= 0.75;
  342. } else if (s->last_noise_floor > s->last_residual_floor + 6) {
  343. min *= 0.4;
  344. max *= 1.0;
  345. } else if (s->last_noise_floor > s->last_residual_floor + 4) {
  346. min *= 0.3;
  347. max *= 1.3;
  348. } else if (s->last_noise_floor > s->last_residual_floor + 2) {
  349. min *= 0.2;
  350. max *= 1.6;
  351. } else if (s->last_noise_floor > s->last_residual_floor) {
  352. min *= 0.1;
  353. max *= 2.0;
  354. } else {
  355. min = 0.0;
  356. max *= 2.5;
  357. }
  358. }
  359. sum = av_clipd(sum, -min, max);
  360. sum = exp(sum);
  361. for (int i = 0; i < 15; i++)
  362. dnch->noise_band_auto_var[i] *= sum;
  363. } else if (dnch->sfm_results[2] >= dnch->sfm_threshold) {
  364. dnch->sfm_fail_flags[s->block_count & 0x1FF] = 1;
  365. dnch->sfm_fail_total += 1;
  366. }
  367. }
  368. for (int i = 0; i < s->number_of_bands; i++) {
  369. dnch->band_excit[i] = 0.0;
  370. dnch->band_amt[i] = 0.0;
  371. }
  372. for (int i = 0; i < s->bin_count; i++) {
  373. dnch->band_excit[s->bin2band[i]] += dnch->clean_data[i];
  374. }
  375. for (int i = 0; i < s->number_of_bands; i++) {
  376. dnch->band_excit[i] = fmax(dnch->band_excit[i],
  377. s->band_alpha[i] * dnch->band_excit[i] +
  378. s->band_beta[i] * prior_band_excit[i]);
  379. prior_band_excit[i] = dnch->band_excit[i];
  380. }
  381. for (int j = 0, i = 0; j < s->number_of_bands; j++) {
  382. for (int k = 0; k < s->number_of_bands; k++) {
  383. dnch->band_amt[j] += dnch->spread_function[i++] * dnch->band_excit[k];
  384. }
  385. }
  386. for (int i = 0; i < s->bin_count; i++)
  387. dnch->amt[i] = dnch->band_amt[s->bin2band[i]];
  388. if (dnch->amt[0] > dnch->abs_var[0]) {
  389. dnch->gain[0] = 1.0;
  390. } else if (dnch->amt[0] > dnch->min_abs_var[0]) {
  391. double limit = sqrt(dnch->abs_var[0] / dnch->amt[0]);
  392. dnch->gain[0] = limit_gain(dnch->gain[0], limit);
  393. } else {
  394. dnch->gain[0] = limit_gain(dnch->gain[0], s->max_gain);
  395. }
  396. if (dnch->amt[s->fft_length2] > dnch->abs_var[s->fft_length2]) {
  397. dnch->gain[s->fft_length2] = 1.0;
  398. } else if (dnch->amt[s->fft_length2] > dnch->min_abs_var[s->fft_length2]) {
  399. double limit = sqrt(dnch->abs_var[s->fft_length2] / dnch->amt[s->fft_length2]);
  400. dnch->gain[s->fft_length2] = limit_gain(dnch->gain[s->fft_length2], limit);
  401. } else {
  402. dnch->gain[s->fft_length2] = limit_gain(dnch->gain[s->fft_length2], s->max_gain);
  403. }
  404. for (int i = 1; i < s->fft_length2; i++) {
  405. if (dnch->amt[i] > dnch->abs_var[i]) {
  406. dnch->gain[i] = 1.0;
  407. } else if (dnch->amt[i] > dnch->min_abs_var[i]) {
  408. double limit = sqrt(dnch->abs_var[i] / dnch->amt[i]);
  409. dnch->gain[i] = limit_gain(dnch->gain[i], limit);
  410. } else {
  411. dnch->gain[i] = limit_gain(dnch->gain[i], s->max_gain);
  412. }
  413. }
  414. gain = dnch->gain[0];
  415. dnch->clean_data[0] = (gain * gain * dnch->noisy_data[0]);
  416. fft_data[0].re *= gain;
  417. gain = dnch->gain[s->fft_length2];
  418. dnch->clean_data[s->fft_length2] = (gain * gain * dnch->noisy_data[s->fft_length2]);
  419. fft_data[0].im *= gain;
  420. for (int i = 1; i < s->fft_length2; i++) {
  421. gain = dnch->gain[i];
  422. dnch->clean_data[i] = (gain * gain * dnch->noisy_data[i]);
  423. fft_data[i].re *= gain;
  424. fft_data[i].im *= gain;
  425. }
  426. }
  427. static double freq2bark(double x)
  428. {
  429. double d = x / 7500.0;
  430. return 13.0 * atan(7.6E-4 * x) + 3.5 * atan(d * d);
  431. }
  432. static int get_band_centre(AudioFFTDeNoiseContext *s, int band)
  433. {
  434. if (band == -1)
  435. return lrint(s->band_centre[0] / 1.5);
  436. return s->band_centre[band];
  437. }
  438. static int get_band_edge(AudioFFTDeNoiseContext *s, int band)
  439. {
  440. int i;
  441. if (band == 15) {
  442. i = lrint(s->band_centre[14] * 1.224745);
  443. } else {
  444. i = lrint(s->band_centre[band] / 1.224745);
  445. }
  446. return FFMIN(i, s->sample_rate / 2);
  447. }
  448. static void set_band_parameters(AudioFFTDeNoiseContext *s,
  449. DeNoiseChannel *dnch)
  450. {
  451. double band_noise, d2, d3, d4, d5;
  452. int i = 0, j = 0, k = 0;
  453. d5 = 0.0;
  454. band_noise = process_get_band_noise(s, dnch, 0);
  455. for (int m = j; m <= s->fft_length2; m++) {
  456. if (m == j) {
  457. i = j;
  458. d5 = band_noise;
  459. if (k == 15) {
  460. j = s->bin_count;
  461. } else {
  462. j = s->fft_length * get_band_centre(s, k) / s->sample_rate;
  463. }
  464. d2 = j - i;
  465. band_noise = process_get_band_noise(s, dnch, k);
  466. k++;
  467. }
  468. d3 = (j - m) / d2;
  469. d4 = (m - i) / d2;
  470. dnch->rel_var[m] = exp((d5 * d3 + band_noise * d4) * C);
  471. }
  472. dnch->rel_var[s->fft_length2] = exp(band_noise * C);
  473. for (i = 0; i < 15; i++)
  474. dnch->noise_band_auto_var[i] = s->max_var * exp((process_get_band_noise(s, dnch, i) - 2.0) * C);
  475. for (i = 0; i <= s->fft_length2; i++) {
  476. dnch->abs_var[i] = fmax(s->max_var * dnch->rel_var[i], 1.0);
  477. dnch->min_abs_var[i] = s->gain_scale * dnch->abs_var[i];
  478. }
  479. }
  480. static void read_custom_noise(AudioFFTDeNoiseContext *s, int ch)
  481. {
  482. DeNoiseChannel *dnch = &s->dnch[ch];
  483. char *p, *arg, *saveptr = NULL;
  484. int i, ret, band_noise[15] = { 0 };
  485. if (!s->band_noise_str)
  486. return;
  487. p = av_strdup(s->band_noise_str);
  488. if (!p)
  489. return;
  490. for (i = 0; i < 15; i++) {
  491. if (!(arg = av_strtok(p, "| ", &saveptr)))
  492. break;
  493. p = NULL;
  494. ret = av_sscanf(arg, "%d", &band_noise[i]);
  495. if (ret != 1) {
  496. av_log(s, AV_LOG_ERROR, "Custom band noise must be integer.\n");
  497. break;
  498. }
  499. band_noise[i] = av_clip(band_noise[i], -24, 24);
  500. }
  501. av_free(p);
  502. memcpy(dnch->band_noise, band_noise, sizeof(band_noise));
  503. }
  504. static void set_parameters(AudioFFTDeNoiseContext *s)
  505. {
  506. if (s->last_noise_floor != s->noise_floor)
  507. s->last_noise_floor = s->noise_floor;
  508. if (s->track_residual)
  509. s->last_noise_floor = fmaxf(s->last_noise_floor, s->residual_floor);
  510. s->max_var = s->floor * exp((100.0 + s->last_noise_floor) * C);
  511. if (s->track_residual) {
  512. s->last_residual_floor = s->residual_floor;
  513. s->last_noise_reduction = fmax(s->last_noise_floor - s->last_residual_floor, 0);
  514. s->max_gain = exp(s->last_noise_reduction * (0.5 * C));
  515. } else if (s->noise_reduction != s->last_noise_reduction) {
  516. s->last_noise_reduction = s->noise_reduction;
  517. s->last_residual_floor = av_clipf(s->last_noise_floor - s->last_noise_reduction, -80, -20);
  518. s->max_gain = exp(s->last_noise_reduction * (0.5 * C));
  519. }
  520. s->gain_scale = 1.0 / (s->max_gain * s->max_gain);
  521. for (int ch = 0; ch < s->channels; ch++) {
  522. DeNoiseChannel *dnch = &s->dnch[ch];
  523. set_band_parameters(s, dnch);
  524. }
  525. }
  526. static int config_input(AVFilterLink *inlink)
  527. {
  528. AVFilterContext *ctx = inlink->dst;
  529. AudioFFTDeNoiseContext *s = ctx->priv;
  530. double wscale, sar, sum, sdiv;
  531. int i, j, k, m, n;
  532. s->dnch = av_calloc(inlink->channels, sizeof(*s->dnch));
  533. if (!s->dnch)
  534. return AVERROR(ENOMEM);
  535. s->pts = AV_NOPTS_VALUE;
  536. s->channels = inlink->channels;
  537. s->sample_rate = inlink->sample_rate;
  538. s->sample_advance = s->sample_rate / 80;
  539. s->window_length = 3 * s->sample_advance;
  540. s->fft_length2 = 1 << (32 - ff_clz(s->window_length));
  541. s->fft_length = s->fft_length2 * 2;
  542. s->buffer_length = s->fft_length * 2;
  543. s->bin_count = s->fft_length2 + 1;
  544. s->band_centre[0] = 80;
  545. for (i = 1; i < 15; i++) {
  546. s->band_centre[i] = lrint(1.5 * s->band_centre[i - 1] + 5.0);
  547. if (s->band_centre[i] < 1000) {
  548. s->band_centre[i] = 10 * (s->band_centre[i] / 10);
  549. } else if (s->band_centre[i] < 5000) {
  550. s->band_centre[i] = 50 * ((s->band_centre[i] + 20) / 50);
  551. } else if (s->band_centre[i] < 15000) {
  552. s->band_centre[i] = 100 * ((s->band_centre[i] + 45) / 100);
  553. } else {
  554. s->band_centre[i] = 1000 * ((s->band_centre[i] + 495) / 1000);
  555. }
  556. }
  557. for (j = 0; j < 5; j++) {
  558. for (k = 0; k < 5; k++) {
  559. s->matrix_a[j + k * 5] = 0.0;
  560. for (m = 0; m < 15; m++)
  561. s->matrix_a[j + k * 5] += pow(m, j + k);
  562. }
  563. }
  564. factor(s->matrix_a, 5);
  565. i = 0;
  566. for (j = 0; j < 5; j++)
  567. for (k = 0; k < 15; k++)
  568. s->matrix_b[i++] = pow(k, j);
  569. i = 0;
  570. for (j = 0; j < 15; j++)
  571. for (k = 0; k < 5; k++)
  572. s->matrix_c[i++] = pow(j, k);
  573. s->window = av_calloc(s->window_length, sizeof(*s->window));
  574. s->bin2band = av_calloc(s->bin_count, sizeof(*s->bin2band));
  575. if (!s->window || !s->bin2band)
  576. return AVERROR(ENOMEM);
  577. sdiv = s->sample_rate / 17640.0;
  578. for (i = 0; i <= s->fft_length2; i++)
  579. s->bin2band[i] = lrint(sdiv * freq2bark((0.5 * i * s->sample_rate) / s->fft_length2));
  580. s->number_of_bands = s->bin2band[s->fft_length2] + 1;
  581. s->band_alpha = av_calloc(s->number_of_bands, sizeof(*s->band_alpha));
  582. s->band_beta = av_calloc(s->number_of_bands, sizeof(*s->band_beta));
  583. if (!s->band_alpha || !s->band_beta)
  584. return AVERROR(ENOMEM);
  585. for (int ch = 0; ch < inlink->channels; ch++) {
  586. DeNoiseChannel *dnch = &s->dnch[ch];
  587. switch (s->noise_type) {
  588. case WHITE_NOISE:
  589. for (i = 0; i < 15; i++)
  590. dnch->band_noise[i] = 0;
  591. break;
  592. case VINYL_NOISE:
  593. for (i = 0; i < 15; i++)
  594. dnch->band_noise[i] = get_band_noise(s, i, 50.0, 500.5, 2125.0) + FFMAX(i - 7, 0);
  595. break;
  596. case SHELLAC_NOISE:
  597. for (i = 0; i < 15; i++)
  598. dnch->band_noise[i] = get_band_noise(s, i, 1.0, 500.0, 1.0E10) + FFMAX(i - 12, -5);
  599. break;
  600. case CUSTOM_NOISE:
  601. read_custom_noise(s, ch);
  602. break;
  603. default:
  604. return AVERROR_BUG;
  605. }
  606. dnch->sfm_threshold = 0.8;
  607. dnch->sfm_alpha = 0.05;
  608. for (i = 0; i < 512; i++)
  609. dnch->sfm_fail_flags[i] = 0;
  610. dnch->sfm_fail_total = 0;
  611. j = FFMAX((int)(10.0 * (1.3 - dnch->sfm_threshold)), 1);
  612. for (i = 0; i < 512; i += j) {
  613. dnch->sfm_fail_flags[i] = 1;
  614. dnch->sfm_fail_total += 1;
  615. }
  616. dnch->amt = av_calloc(s->bin_count, sizeof(*dnch->amt));
  617. dnch->band_amt = av_calloc(s->number_of_bands, sizeof(*dnch->band_amt));
  618. dnch->band_excit = av_calloc(s->number_of_bands, sizeof(*dnch->band_excit));
  619. dnch->gain = av_calloc(s->bin_count, sizeof(*dnch->gain));
  620. dnch->prior = av_calloc(s->bin_count, sizeof(*dnch->prior));
  621. dnch->prior_band_excit = av_calloc(s->number_of_bands, sizeof(*dnch->prior_band_excit));
  622. dnch->clean_data = av_calloc(s->bin_count, sizeof(*dnch->clean_data));
  623. dnch->noisy_data = av_calloc(s->bin_count, sizeof(*dnch->noisy_data));
  624. dnch->out_samples = av_calloc(s->buffer_length, sizeof(*dnch->out_samples));
  625. dnch->abs_var = av_calloc(s->bin_count, sizeof(*dnch->abs_var));
  626. dnch->rel_var = av_calloc(s->bin_count, sizeof(*dnch->rel_var));
  627. dnch->min_abs_var = av_calloc(s->bin_count, sizeof(*dnch->min_abs_var));
  628. dnch->fft_data = av_calloc(s->fft_length2 + 1, sizeof(*dnch->fft_data));
  629. dnch->fft = av_fft_init(av_log2(s->fft_length2), 0);
  630. dnch->ifft = av_fft_init(av_log2(s->fft_length2), 1);
  631. dnch->spread_function = av_calloc(s->number_of_bands * s->number_of_bands,
  632. sizeof(*dnch->spread_function));
  633. if (!dnch->amt ||
  634. !dnch->band_amt ||
  635. !dnch->band_excit ||
  636. !dnch->gain ||
  637. !dnch->prior ||
  638. !dnch->prior_band_excit ||
  639. !dnch->clean_data ||
  640. !dnch->noisy_data ||
  641. !dnch->out_samples ||
  642. !dnch->fft_data ||
  643. !dnch->abs_var ||
  644. !dnch->rel_var ||
  645. !dnch->min_abs_var ||
  646. !dnch->spread_function ||
  647. !dnch->fft ||
  648. !dnch->ifft)
  649. return AVERROR(ENOMEM);
  650. }
  651. for (int ch = 0; ch < inlink->channels; ch++) {
  652. DeNoiseChannel *dnch = &s->dnch[ch];
  653. double *prior_band_excit = dnch->prior_band_excit;
  654. double *prior = dnch->prior;
  655. double min, max;
  656. double p1, p2;
  657. p1 = pow(0.1, 2.5 / sdiv);
  658. p2 = pow(0.1, 1.0 / sdiv);
  659. j = 0;
  660. for (m = 0; m < s->number_of_bands; m++) {
  661. for (n = 0; n < s->number_of_bands; n++) {
  662. if (n < m) {
  663. dnch->spread_function[j++] = pow(p2, m - n);
  664. } else if (n > m) {
  665. dnch->spread_function[j++] = pow(p1, n - m);
  666. } else {
  667. dnch->spread_function[j++] = 1.0;
  668. }
  669. }
  670. }
  671. for (m = 0; m < s->number_of_bands; m++) {
  672. dnch->band_excit[m] = 0.0;
  673. prior_band_excit[m] = 0.0;
  674. }
  675. for (m = 0; m <= s->fft_length2; m++)
  676. dnch->band_excit[s->bin2band[m]] += 1.0;
  677. j = 0;
  678. for (m = 0; m < s->number_of_bands; m++) {
  679. for (n = 0; n < s->number_of_bands; n++)
  680. prior_band_excit[m] += dnch->spread_function[j++] * dnch->band_excit[n];
  681. }
  682. min = pow(0.1, 2.5);
  683. max = pow(0.1, 1.0);
  684. for (int i = 0; i < s->number_of_bands; i++) {
  685. if (i < lrint(12.0 * sdiv)) {
  686. dnch->band_excit[i] = pow(0.1, 1.45 + 0.1 * i / sdiv);
  687. } else {
  688. dnch->band_excit[i] = pow(0.1, 2.5 - 0.2 * (i / sdiv - 14.0));
  689. }
  690. dnch->band_excit[i] = av_clipd(dnch->band_excit[i], min, max);
  691. }
  692. for (int i = 0; i <= s->fft_length2; i++)
  693. prior[i] = RRATIO;
  694. for (int i = 0; i < s->buffer_length; i++)
  695. dnch->out_samples[i] = 0;
  696. j = 0;
  697. for (int i = 0; i < s->number_of_bands; i++)
  698. for (int k = 0; k < s->number_of_bands; k++)
  699. dnch->spread_function[j++] *= dnch->band_excit[i] / prior_band_excit[i];
  700. }
  701. j = 0;
  702. sar = s->sample_advance / s->sample_rate;
  703. for (int i = 0; i <= s->fft_length2; i++) {
  704. if ((i == s->fft_length2) || (s->bin2band[i] > j)) {
  705. double d6 = (i - 1) * s->sample_rate / s->fft_length;
  706. double d7 = fmin(0.008 + 2.2 / d6, 0.03);
  707. s->band_alpha[j] = exp(-sar / d7);
  708. s->band_beta[j] = 1.0 - s->band_alpha[j];
  709. j = s->bin2band[i];
  710. }
  711. }
  712. wscale = sqrt(16.0 / (9.0 * s->fft_length));
  713. sum = 0.0;
  714. for (int i = 0; i < s->window_length; i++) {
  715. double d10 = sin(i * M_PI / s->window_length);
  716. d10 *= wscale * d10;
  717. s->window[i] = d10;
  718. sum += d10 * d10;
  719. }
  720. s->window_weight = 0.5 * sum;
  721. s->floor = (1LL << 48) * exp(-23.025558369790467) * s->window_weight;
  722. s->sample_floor = s->floor * exp(4.144600506562284);
  723. s->auto_floor = s->floor * exp(6.907667510937141);
  724. set_parameters(s);
  725. s->noise_band_edge[0] = FFMIN(s->fft_length2, s->fft_length * get_band_edge(s, 0) / s->sample_rate);
  726. i = 0;
  727. for (int j = 1; j < 16; j++) {
  728. s->noise_band_edge[j] = FFMIN(s->fft_length2, s->fft_length * get_band_edge(s, j) / s->sample_rate);
  729. if (s->noise_band_edge[j] > lrint(1.1 * s->noise_band_edge[j - 1]))
  730. i++;
  731. s->noise_band_edge[16] = i;
  732. }
  733. s->noise_band_count = s->noise_band_edge[16];
  734. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->fft_length);
  735. if (!s->fifo)
  736. return AVERROR(ENOMEM);
  737. return 0;
  738. }
  739. static void preprocess(FFTComplex *in, int len)
  740. {
  741. double d1, d2, d3, d4, d5, d6, d7, d8, d9, d10;
  742. int n, i, k;
  743. d5 = 2.0 * M_PI / len;
  744. d8 = sin(0.5 * d5);
  745. d8 = -2.0 * d8 * d8;
  746. d7 = sin(d5);
  747. d9 = 1.0 + d8;
  748. d6 = d7;
  749. n = len / 2;
  750. for (i = 1; i < len / 4; i++) {
  751. k = n - i;
  752. d2 = 0.5 * (in[i].re + in[k].re);
  753. d1 = 0.5 * (in[i].im - in[k].im);
  754. d4 = 0.5 * (in[i].im + in[k].im);
  755. d3 = 0.5 * (in[k].re - in[i].re);
  756. in[i].re = d2 + d9 * d4 + d6 * d3;
  757. in[i].im = d1 + d9 * d3 - d6 * d4;
  758. in[k].re = d2 - d9 * d4 - d6 * d3;
  759. in[k].im = -d1 + d9 * d3 - d6 * d4;
  760. d10 = d9;
  761. d9 += d9 * d8 - d6 * d7;
  762. d6 += d6 * d8 + d10 * d7;
  763. }
  764. d2 = in[0].re;
  765. in[0].re = d2 + in[0].im;
  766. in[0].im = d2 - in[0].im;
  767. }
  768. static void postprocess(FFTComplex *in, int len)
  769. {
  770. double d1, d2, d3, d4, d5, d6, d7, d8, d9, d10;
  771. int n, i, k;
  772. d5 = 2.0 * M_PI / len;
  773. d8 = sin(0.5 * d5);
  774. d8 = -2.0 * d8 * d8;
  775. d7 = sin(d5);
  776. d9 = 1.0 + d8;
  777. d6 = d7;
  778. n = len / 2;
  779. for (i = 1; i < len / 4; i++) {
  780. k = n - i;
  781. d2 = 0.5 * (in[i].re + in[k].re);
  782. d1 = 0.5 * (in[i].im - in[k].im);
  783. d4 = 0.5 * (in[i].re - in[k].re);
  784. d3 = 0.5 * (in[i].im + in[k].im);
  785. in[i].re = d2 - d9 * d3 - d6 * d4;
  786. in[i].im = d1 + d9 * d4 - d6 * d3;
  787. in[k].re = d2 + d9 * d3 + d6 * d4;
  788. in[k].im = -d1 + d9 * d4 - d6 * d3;
  789. d10 = d9;
  790. d9 += d9 * d8 - d6 * d7;
  791. d6 += d6 * d8 + d10 * d7;
  792. }
  793. d2 = in[0].re;
  794. in[0].re = 0.5 * (d2 + in[0].im);
  795. in[0].im = 0.5 * (d2 - in[0].im);
  796. }
  797. static void init_sample_noise(DeNoiseChannel *dnch)
  798. {
  799. for (int i = 0; i < 15; i++) {
  800. dnch->noise_band_norm[i] = 0.0;
  801. dnch->noise_band_avr[i] = 0.0;
  802. dnch->noise_band_avi[i] = 0.0;
  803. dnch->noise_band_var[i] = 0.0;
  804. }
  805. }
  806. static void sample_noise_block(AudioFFTDeNoiseContext *s,
  807. DeNoiseChannel *dnch,
  808. AVFrame *in, int ch)
  809. {
  810. float *src = (float *)in->extended_data[ch];
  811. double mag2, var = 0.0, avr = 0.0, avi = 0.0;
  812. int edge, j, k, n, edgemax;
  813. for (int i = 0; i < s->window_length; i++) {
  814. dnch->fft_data[i].re = s->window[i] * src[i] * (1LL << 24);
  815. dnch->fft_data[i].im = 0.0;
  816. }
  817. for (int i = s->window_length; i < s->fft_length2; i++) {
  818. dnch->fft_data[i].re = 0.0;
  819. dnch->fft_data[i].im = 0.0;
  820. }
  821. av_fft_permute(dnch->fft, dnch->fft_data);
  822. av_fft_calc(dnch->fft, dnch->fft_data);
  823. preprocess(dnch->fft_data, s->fft_length);
  824. edge = s->noise_band_edge[0];
  825. j = edge;
  826. k = 0;
  827. n = j;
  828. edgemax = fmin(s->fft_length2, s->noise_band_edge[15]);
  829. dnch->fft_data[s->fft_length2].re = dnch->fft_data[0].im;
  830. dnch->fft_data[0].im = 0.0;
  831. dnch->fft_data[s->fft_length2].im = 0.0;
  832. for (int i = j; i <= edgemax; i++) {
  833. if ((i == j) && (i < edgemax)) {
  834. if (j > edge) {
  835. dnch->noise_band_norm[k - 1] += j - edge;
  836. dnch->noise_band_avr[k - 1] += avr;
  837. dnch->noise_band_avi[k - 1] += avi;
  838. dnch->noise_band_var[k - 1] += var;
  839. }
  840. k++;
  841. edge = j;
  842. j = s->noise_band_edge[k];
  843. if (k == 15) {
  844. j++;
  845. }
  846. var = 0.0;
  847. avr = 0.0;
  848. avi = 0.0;
  849. }
  850. avr += dnch->fft_data[n].re;
  851. avi += dnch->fft_data[n].im;
  852. mag2 = dnch->fft_data[n].re * dnch->fft_data[n].re +
  853. dnch->fft_data[n].im * dnch->fft_data[n].im;
  854. mag2 = fmax(mag2, s->sample_floor);
  855. dnch->noisy_data[i] = mag2;
  856. var += mag2;
  857. n++;
  858. }
  859. dnch->noise_band_norm[k - 1] += j - edge;
  860. dnch->noise_band_avr[k - 1] += avr;
  861. dnch->noise_band_avi[k - 1] += avi;
  862. dnch->noise_band_var[k - 1] += var;
  863. }
  864. static void finish_sample_noise(AudioFFTDeNoiseContext *s,
  865. DeNoiseChannel *dnch,
  866. double *sample_noise)
  867. {
  868. for (int i = 0; i < s->noise_band_count; i++) {
  869. dnch->noise_band_avr[i] /= dnch->noise_band_norm[i];
  870. dnch->noise_band_avi[i] /= dnch->noise_band_norm[i];
  871. dnch->noise_band_var[i] /= dnch->noise_band_norm[i];
  872. dnch->noise_band_var[i] -= dnch->noise_band_avr[i] * dnch->noise_band_avr[i] +
  873. dnch->noise_band_avi[i] * dnch->noise_band_avi[i];
  874. dnch->noise_band_auto_var[i] = dnch->noise_band_var[i];
  875. sample_noise[i] = (1.0 / C) * log(dnch->noise_band_var[i] / s->floor) - 100.0;
  876. }
  877. if (s->noise_band_count < 15) {
  878. for (int i = s->noise_band_count; i < 15; i++)
  879. sample_noise[i] = sample_noise[i - 1];
  880. }
  881. }
  882. static void set_noise_profile(AudioFFTDeNoiseContext *s,
  883. DeNoiseChannel *dnch,
  884. double *sample_noise,
  885. int new_profile)
  886. {
  887. int new_band_noise[15];
  888. double temp[15];
  889. double sum = 0.0, d1;
  890. float new_noise_floor;
  891. int i, n;
  892. for (int m = 0; m < 15; m++)
  893. temp[m] = sample_noise[m];
  894. if (new_profile) {
  895. i = 0;
  896. for (int m = 0; m < 5; m++) {
  897. sum = 0.0;
  898. for (n = 0; n < 15; n++)
  899. sum += s->matrix_b[i++] * temp[n];
  900. s->vector_b[m] = sum;
  901. }
  902. solve(s->matrix_a, s->vector_b, 5);
  903. i = 0;
  904. for (int m = 0; m < 15; m++) {
  905. sum = 0.0;
  906. for (n = 0; n < 5; n++)
  907. sum += s->matrix_c[i++] * s->vector_b[n];
  908. temp[m] = sum;
  909. }
  910. }
  911. sum = 0.0;
  912. for (int m = 0; m < 15; m++)
  913. sum += temp[m];
  914. d1 = (int)(sum / 15.0 - 0.5);
  915. if (!new_profile)
  916. i = lrint(temp[7] - d1);
  917. for (d1 -= dnch->band_noise[7] - i; d1 > -20.0; d1 -= 1.0)
  918. ;
  919. for (int m = 0; m < 15; m++)
  920. temp[m] -= d1;
  921. new_noise_floor = d1 + 2.5;
  922. if (new_profile) {
  923. av_log(s, AV_LOG_INFO, "bn=");
  924. for (int m = 0; m < 15; m++) {
  925. new_band_noise[m] = lrint(temp[m]);
  926. new_band_noise[m] = av_clip(new_band_noise[m], -24, 24);
  927. av_log(s, AV_LOG_INFO, "%d ", new_band_noise[m]);
  928. }
  929. av_log(s, AV_LOG_INFO, "\n");
  930. memcpy(dnch->band_noise, new_band_noise, sizeof(new_band_noise));
  931. }
  932. if (s->track_noise)
  933. s->noise_floor = new_noise_floor;
  934. }
  935. typedef struct ThreadData {
  936. AVFrame *in;
  937. } ThreadData;
  938. static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  939. {
  940. AudioFFTDeNoiseContext *s = ctx->priv;
  941. ThreadData *td = arg;
  942. AVFrame *in = td->in;
  943. const int start = (in->channels * jobnr) / nb_jobs;
  944. const int end = (in->channels * (jobnr+1)) / nb_jobs;
  945. for (int ch = start; ch < end; ch++) {
  946. DeNoiseChannel *dnch = &s->dnch[ch];
  947. const float *src = (const float *)in->extended_data[ch];
  948. double *dst = dnch->out_samples;
  949. if (s->track_noise) {
  950. int i = s->block_count & 0x1FF;
  951. if (dnch->sfm_fail_flags[i])
  952. dnch->sfm_fail_total--;
  953. dnch->sfm_fail_flags[i] = 0;
  954. dnch->sfm_threshold *= 1.0 - dnch->sfm_alpha;
  955. dnch->sfm_threshold += dnch->sfm_alpha * (0.5 + (1.0 / 640) * dnch->sfm_fail_total);
  956. }
  957. for (int m = 0; m < s->window_length; m++) {
  958. dnch->fft_data[m].re = s->window[m] * src[m] * (1LL << 24);
  959. dnch->fft_data[m].im = 0;
  960. }
  961. for (int m = s->window_length; m < s->fft_length2; m++) {
  962. dnch->fft_data[m].re = 0;
  963. dnch->fft_data[m].im = 0;
  964. }
  965. av_fft_permute(dnch->fft, dnch->fft_data);
  966. av_fft_calc(dnch->fft, dnch->fft_data);
  967. preprocess(dnch->fft_data, s->fft_length);
  968. process_frame(s, dnch, dnch->fft_data,
  969. dnch->prior,
  970. dnch->prior_band_excit,
  971. s->track_noise);
  972. postprocess(dnch->fft_data, s->fft_length);
  973. av_fft_permute(dnch->ifft, dnch->fft_data);
  974. av_fft_calc(dnch->ifft, dnch->fft_data);
  975. for (int m = 0; m < s->window_length; m++)
  976. dst[m] += s->window[m] * dnch->fft_data[m].re / (1LL << 24);
  977. }
  978. return 0;
  979. }
  980. static void get_auto_noise_levels(AudioFFTDeNoiseContext *s,
  981. DeNoiseChannel *dnch,
  982. double *levels)
  983. {
  984. if (s->noise_band_count > 0) {
  985. for (int i = 0; i < s->noise_band_count; i++) {
  986. levels[i] = (1.0 / C) * log(dnch->noise_band_auto_var[i] / s->floor) - 100.0;
  987. }
  988. if (s->noise_band_count < 15) {
  989. for (int i = s->noise_band_count; i < 15; i++)
  990. levels[i] = levels[i - 1];
  991. }
  992. } else {
  993. for (int i = 0; i < 15; i++) {
  994. levels[i] = -100.0;
  995. }
  996. }
  997. }
  998. static int output_frame(AVFilterLink *inlink)
  999. {
  1000. AVFilterContext *ctx = inlink->dst;
  1001. AVFilterLink *outlink = ctx->outputs[0];
  1002. AudioFFTDeNoiseContext *s = ctx->priv;
  1003. AVFrame *out = NULL, *in = NULL;
  1004. ThreadData td;
  1005. int ret = 0;
  1006. in = ff_get_audio_buffer(outlink, s->window_length);
  1007. if (!in)
  1008. return AVERROR(ENOMEM);
  1009. ret = av_audio_fifo_peek(s->fifo, (void **)in->extended_data, s->window_length);
  1010. if (ret < 0)
  1011. goto end;
  1012. if (s->track_noise) {
  1013. for (int ch = 0; ch < inlink->channels; ch++) {
  1014. DeNoiseChannel *dnch = &s->dnch[ch];
  1015. double levels[15];
  1016. get_auto_noise_levels(s, dnch, levels);
  1017. set_noise_profile(s, dnch, levels, 0);
  1018. }
  1019. if (s->noise_floor != s->last_noise_floor)
  1020. set_parameters(s);
  1021. }
  1022. if (s->sample_noise_start) {
  1023. for (int ch = 0; ch < inlink->channels; ch++) {
  1024. DeNoiseChannel *dnch = &s->dnch[ch];
  1025. init_sample_noise(dnch);
  1026. }
  1027. s->sample_noise_start = 0;
  1028. s->sample_noise = 1;
  1029. }
  1030. if (s->sample_noise) {
  1031. for (int ch = 0; ch < inlink->channels; ch++) {
  1032. DeNoiseChannel *dnch = &s->dnch[ch];
  1033. sample_noise_block(s, dnch, in, ch);
  1034. }
  1035. }
  1036. if (s->sample_noise_end) {
  1037. for (int ch = 0; ch < inlink->channels; ch++) {
  1038. DeNoiseChannel *dnch = &s->dnch[ch];
  1039. double sample_noise[15];
  1040. finish_sample_noise(s, dnch, sample_noise);
  1041. set_noise_profile(s, dnch, sample_noise, 1);
  1042. set_band_parameters(s, dnch);
  1043. }
  1044. s->sample_noise = 0;
  1045. s->sample_noise_end = 0;
  1046. }
  1047. s->block_count++;
  1048. td.in = in;
  1049. ctx->internal->execute(ctx, filter_channel, &td, NULL,
  1050. FFMIN(outlink->channels, ff_filter_get_nb_threads(ctx)));
  1051. out = ff_get_audio_buffer(outlink, s->sample_advance);
  1052. if (!out) {
  1053. ret = AVERROR(ENOMEM);
  1054. goto end;
  1055. }
  1056. for (int ch = 0; ch < inlink->channels; ch++) {
  1057. DeNoiseChannel *dnch = &s->dnch[ch];
  1058. double *src = dnch->out_samples;
  1059. float *orig = (float *)in->extended_data[ch];
  1060. float *dst = (float *)out->extended_data[ch];
  1061. switch (s->output_mode) {
  1062. case IN_MODE:
  1063. for (int m = 0; m < s->sample_advance; m++)
  1064. dst[m] = orig[m];
  1065. break;
  1066. case OUT_MODE:
  1067. for (int m = 0; m < s->sample_advance; m++)
  1068. dst[m] = src[m];
  1069. break;
  1070. case NOISE_MODE:
  1071. for (int m = 0; m < s->sample_advance; m++)
  1072. dst[m] = orig[m] - src[m];
  1073. break;
  1074. default:
  1075. av_frame_free(&out);
  1076. ret = AVERROR_BUG;
  1077. goto end;
  1078. }
  1079. memmove(src, src + s->sample_advance, (s->window_length - s->sample_advance) * sizeof(*src));
  1080. memset(src + (s->window_length - s->sample_advance), 0, s->sample_advance * sizeof(*src));
  1081. }
  1082. av_audio_fifo_drain(s->fifo, s->sample_advance);
  1083. out->pts = s->pts;
  1084. ret = ff_filter_frame(outlink, out);
  1085. if (ret < 0)
  1086. goto end;
  1087. s->pts += s->sample_advance;
  1088. end:
  1089. av_frame_free(&in);
  1090. return ret;
  1091. }
  1092. static int activate(AVFilterContext *ctx)
  1093. {
  1094. AVFilterLink *inlink = ctx->inputs[0];
  1095. AVFilterLink *outlink = ctx->outputs[0];
  1096. AudioFFTDeNoiseContext *s = ctx->priv;
  1097. AVFrame *frame = NULL;
  1098. int ret;
  1099. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  1100. ret = ff_inlink_consume_frame(inlink, &frame);
  1101. if (ret < 0)
  1102. return ret;
  1103. if (ret > 0) {
  1104. if (s->pts == AV_NOPTS_VALUE)
  1105. s->pts = frame->pts;
  1106. ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, frame->nb_samples);
  1107. av_frame_free(&frame);
  1108. if (ret < 0)
  1109. return ret;
  1110. }
  1111. if (av_audio_fifo_size(s->fifo) >= s->window_length)
  1112. return output_frame(inlink);
  1113. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  1114. if (ff_outlink_frame_wanted(outlink) &&
  1115. av_audio_fifo_size(s->fifo) < s->window_length) {
  1116. ff_inlink_request_frame(inlink);
  1117. return 0;
  1118. }
  1119. return FFERROR_NOT_READY;
  1120. }
  1121. static av_cold void uninit(AVFilterContext *ctx)
  1122. {
  1123. AudioFFTDeNoiseContext *s = ctx->priv;
  1124. av_freep(&s->window);
  1125. av_freep(&s->bin2band);
  1126. av_freep(&s->band_alpha);
  1127. av_freep(&s->band_beta);
  1128. if (s->dnch) {
  1129. for (int ch = 0; ch < s->channels; ch++) {
  1130. DeNoiseChannel *dnch = &s->dnch[ch];
  1131. av_freep(&dnch->amt);
  1132. av_freep(&dnch->band_amt);
  1133. av_freep(&dnch->band_excit);
  1134. av_freep(&dnch->gain);
  1135. av_freep(&dnch->prior);
  1136. av_freep(&dnch->prior_band_excit);
  1137. av_freep(&dnch->clean_data);
  1138. av_freep(&dnch->noisy_data);
  1139. av_freep(&dnch->out_samples);
  1140. av_freep(&dnch->spread_function);
  1141. av_freep(&dnch->abs_var);
  1142. av_freep(&dnch->rel_var);
  1143. av_freep(&dnch->min_abs_var);
  1144. av_freep(&dnch->fft_data);
  1145. av_fft_end(dnch->fft);
  1146. dnch->fft = NULL;
  1147. av_fft_end(dnch->ifft);
  1148. dnch->ifft = NULL;
  1149. }
  1150. av_freep(&s->dnch);
  1151. }
  1152. av_audio_fifo_free(s->fifo);
  1153. }
  1154. static int query_formats(AVFilterContext *ctx)
  1155. {
  1156. AVFilterFormats *formats = NULL;
  1157. AVFilterChannelLayouts *layouts = NULL;
  1158. static const enum AVSampleFormat sample_fmts[] = {
  1159. AV_SAMPLE_FMT_FLTP,
  1160. AV_SAMPLE_FMT_NONE
  1161. };
  1162. int ret;
  1163. formats = ff_make_format_list(sample_fmts);
  1164. if (!formats)
  1165. return AVERROR(ENOMEM);
  1166. ret = ff_set_common_formats(ctx, formats);
  1167. if (ret < 0)
  1168. return ret;
  1169. layouts = ff_all_channel_counts();
  1170. if (!layouts)
  1171. return AVERROR(ENOMEM);
  1172. ret = ff_set_common_channel_layouts(ctx, layouts);
  1173. if (ret < 0)
  1174. return ret;
  1175. formats = ff_all_samplerates();
  1176. return ff_set_common_samplerates(ctx, formats);
  1177. }
  1178. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  1179. char *res, int res_len, int flags)
  1180. {
  1181. AudioFFTDeNoiseContext *s = ctx->priv;
  1182. int need_reset = 0;
  1183. if (!strcmp(cmd, "sample_noise") ||
  1184. !strcmp(cmd, "sn")) {
  1185. if (!strcmp(args, "start")) {
  1186. s->sample_noise_start = 1;
  1187. s->sample_noise_end = 0;
  1188. } else if (!strcmp(args, "end") ||
  1189. !strcmp(args, "stop")) {
  1190. s->sample_noise_start = 0;
  1191. s->sample_noise_end = 1;
  1192. }
  1193. } else if (!strcmp(cmd, "nr") ||
  1194. !strcmp(cmd, "noise_reduction")) {
  1195. float nr;
  1196. if (av_sscanf(args, "%f", &nr) == 1) {
  1197. s->noise_reduction = av_clipf(nr, 0.01, 97);
  1198. need_reset = 1;
  1199. }
  1200. } else if (!strcmp(cmd, "nf") ||
  1201. !strcmp(cmd, "noise_floor")) {
  1202. float nf;
  1203. if (av_sscanf(args, "%f", &nf) == 1) {
  1204. s->noise_floor = av_clipf(nf, -80, -20);
  1205. need_reset = 1;
  1206. }
  1207. } else if (!strcmp(cmd, "output_mode") ||
  1208. !strcmp(cmd, "om")) {
  1209. if (!strcmp(args, "i")) {
  1210. s->output_mode = IN_MODE;
  1211. } else if (!strcmp(args, "o")) {
  1212. s->output_mode = OUT_MODE;
  1213. } else if (!strcmp(args, "n")) {
  1214. s->output_mode = NOISE_MODE;
  1215. }
  1216. }
  1217. if (need_reset)
  1218. set_parameters(s);
  1219. return 0;
  1220. }
  1221. static const AVFilterPad inputs[] = {
  1222. {
  1223. .name = "default",
  1224. .type = AVMEDIA_TYPE_AUDIO,
  1225. .config_props = config_input,
  1226. },
  1227. { NULL }
  1228. };
  1229. static const AVFilterPad outputs[] = {
  1230. {
  1231. .name = "default",
  1232. .type = AVMEDIA_TYPE_AUDIO,
  1233. },
  1234. { NULL }
  1235. };
  1236. AVFilter ff_af_afftdn = {
  1237. .name = "afftdn",
  1238. .description = NULL_IF_CONFIG_SMALL("Denoise audio samples using FFT."),
  1239. .query_formats = query_formats,
  1240. .priv_size = sizeof(AudioFFTDeNoiseContext),
  1241. .priv_class = &afftdn_class,
  1242. .activate = activate,
  1243. .uninit = uninit,
  1244. .inputs = inputs,
  1245. .outputs = outputs,
  1246. .process_command = process_command,
  1247. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
  1248. AVFILTER_FLAG_SLICE_THREADS,
  1249. };