af_anlmdn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Copyright (c) 2019 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 <float.h>
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/audio_fifo.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/opt.h"
  25. #include "avfilter.h"
  26. #include "audio.h"
  27. #include "formats.h"
  28. #include "af_anlmdndsp.h"
  29. #define WEIGHT_LUT_NBITS 20
  30. #define WEIGHT_LUT_SIZE (1<<WEIGHT_LUT_NBITS)
  31. #define SQR(x) ((x) * (x))
  32. typedef struct AudioNLMeansContext {
  33. const AVClass *class;
  34. float a;
  35. int64_t pd;
  36. int64_t rd;
  37. float m;
  38. int om;
  39. float pdiff_lut_scale;
  40. float weight_lut[WEIGHT_LUT_SIZE];
  41. int K;
  42. int S;
  43. int N;
  44. int H;
  45. int offset;
  46. AVFrame *in;
  47. AVFrame *cache;
  48. int64_t pts;
  49. AVAudioFifo *fifo;
  50. int eof_left;
  51. AudioNLMDNDSPContext dsp;
  52. } AudioNLMeansContext;
  53. enum OutModes {
  54. IN_MODE,
  55. OUT_MODE,
  56. NOISE_MODE,
  57. NB_MODES
  58. };
  59. #define OFFSET(x) offsetof(AudioNLMeansContext, x)
  60. #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  61. static const AVOption anlmdn_options[] = {
  62. { "s", "set denoising strength", OFFSET(a), AV_OPT_TYPE_FLOAT, {.dbl=0.00001},0.00001, 10, AF },
  63. { "p", "set patch duration", OFFSET(pd), AV_OPT_TYPE_DURATION, {.i64=2000}, 1000, 100000, AF },
  64. { "r", "set research duration", OFFSET(rd), AV_OPT_TYPE_DURATION, {.i64=6000}, 2000, 300000, AF },
  65. { "o", "set output mode", OFFSET(om), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_MODES-1, AF, "mode" },
  66. { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AF, "mode" },
  67. { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AF, "mode" },
  68. { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE},0, 0, AF, "mode" },
  69. { "m", "set smooth factor", OFFSET(m), AV_OPT_TYPE_FLOAT, {.dbl=11.}, 1, 15, AF },
  70. { NULL }
  71. };
  72. AVFILTER_DEFINE_CLASS(anlmdn);
  73. static int query_formats(AVFilterContext *ctx)
  74. {
  75. AVFilterFormats *formats = NULL;
  76. AVFilterChannelLayouts *layouts = NULL;
  77. static const enum AVSampleFormat sample_fmts[] = {
  78. AV_SAMPLE_FMT_FLTP,
  79. AV_SAMPLE_FMT_NONE
  80. };
  81. int ret;
  82. formats = ff_make_format_list(sample_fmts);
  83. if (!formats)
  84. return AVERROR(ENOMEM);
  85. ret = ff_set_common_formats(ctx, formats);
  86. if (ret < 0)
  87. return ret;
  88. layouts = ff_all_channel_counts();
  89. if (!layouts)
  90. return AVERROR(ENOMEM);
  91. ret = ff_set_common_channel_layouts(ctx, layouts);
  92. if (ret < 0)
  93. return ret;
  94. formats = ff_all_samplerates();
  95. return ff_set_common_samplerates(ctx, formats);
  96. }
  97. static float compute_distance_ssd_c(const float *f1, const float *f2, ptrdiff_t K)
  98. {
  99. float distance = 0.;
  100. for (int k = -K; k <= K; k++)
  101. distance += SQR(f1[k] - f2[k]);
  102. return distance;
  103. }
  104. static void compute_cache_c(float *cache, const float *f,
  105. ptrdiff_t S, ptrdiff_t K,
  106. ptrdiff_t i, ptrdiff_t jj)
  107. {
  108. int v = 0;
  109. for (int j = jj; j < jj + S; j++, v++)
  110. cache[v] += -SQR(f[i - K - 1] - f[j - K - 1]) + SQR(f[i + K] - f[j + K]);
  111. }
  112. void ff_anlmdn_init(AudioNLMDNDSPContext *dsp)
  113. {
  114. dsp->compute_distance_ssd = compute_distance_ssd_c;
  115. dsp->compute_cache = compute_cache_c;
  116. if (ARCH_X86)
  117. ff_anlmdn_init_x86(dsp);
  118. }
  119. static int config_output(AVFilterLink *outlink)
  120. {
  121. AVFilterContext *ctx = outlink->src;
  122. AudioNLMeansContext *s = ctx->priv;
  123. int ret;
  124. s->K = av_rescale(s->pd, outlink->sample_rate, AV_TIME_BASE);
  125. s->S = av_rescale(s->rd, outlink->sample_rate, AV_TIME_BASE);
  126. s->eof_left = -1;
  127. s->pts = AV_NOPTS_VALUE;
  128. s->H = s->K * 2 + 1;
  129. s->N = s->H + (s->K + s->S) * 2;
  130. av_log(ctx, AV_LOG_DEBUG, "K:%d S:%d H:%d N:%d\n", s->K, s->S, s->H, s->N);
  131. av_frame_free(&s->in);
  132. av_frame_free(&s->cache);
  133. s->in = ff_get_audio_buffer(outlink, s->N);
  134. if (!s->in)
  135. return AVERROR(ENOMEM);
  136. s->cache = ff_get_audio_buffer(outlink, s->S * 2);
  137. if (!s->cache)
  138. return AVERROR(ENOMEM);
  139. s->fifo = av_audio_fifo_alloc(outlink->format, outlink->channels, s->N);
  140. if (!s->fifo)
  141. return AVERROR(ENOMEM);
  142. ret = av_audio_fifo_write(s->fifo, (void **)s->in->extended_data, s->K + s->S);
  143. if (ret < 0)
  144. return ret;
  145. s->pdiff_lut_scale = 1.f / s->m * WEIGHT_LUT_SIZE;
  146. for (int i = 0; i < WEIGHT_LUT_SIZE; i++) {
  147. float w = -i / s->pdiff_lut_scale;
  148. s->weight_lut[i] = expf(w);
  149. }
  150. ff_anlmdn_init(&s->dsp);
  151. return 0;
  152. }
  153. static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
  154. {
  155. AudioNLMeansContext *s = ctx->priv;
  156. AVFrame *out = arg;
  157. const int S = s->S;
  158. const int K = s->K;
  159. const int om = s->om;
  160. const float *f = (const float *)(s->in->extended_data[ch]) + K;
  161. float *cache = (float *)s->cache->extended_data[ch];
  162. const float sw = (65536.f / (4 * K + 2)) / sqrtf(s->a);
  163. float *dst = (float *)out->extended_data[ch] + s->offset;
  164. const float smooth = s->m;
  165. for (int i = S; i < s->H + S; i++) {
  166. float P = 0.f, Q = 0.f;
  167. int v = 0;
  168. if (i == S) {
  169. for (int j = i - S; j <= i + S; j++) {
  170. if (i == j)
  171. continue;
  172. cache[v++] = s->dsp.compute_distance_ssd(f + i, f + j, K);
  173. }
  174. } else {
  175. s->dsp.compute_cache(cache, f, S, K, i, i - S);
  176. s->dsp.compute_cache(cache + S, f, S, K, i, i + 1);
  177. }
  178. for (int j = 0; j < 2 * S && !ctx->is_disabled; j++) {
  179. const float distance = cache[j];
  180. unsigned weight_lut_idx;
  181. float w;
  182. if (distance < 0.f) {
  183. cache[j] = 0.f;
  184. continue;
  185. }
  186. w = distance * sw;
  187. if (w >= smooth)
  188. continue;
  189. weight_lut_idx = w * s->pdiff_lut_scale;
  190. av_assert2(weight_lut_idx < WEIGHT_LUT_SIZE);
  191. w = s->weight_lut[weight_lut_idx];
  192. P += w * f[i - S + j + (j >= S)];
  193. Q += w;
  194. }
  195. P += f[i];
  196. Q += 1;
  197. switch (om) {
  198. case IN_MODE: dst[i - S] = f[i]; break;
  199. case OUT_MODE: dst[i - S] = P / Q; break;
  200. case NOISE_MODE: dst[i - S] = f[i] - (P / Q); break;
  201. }
  202. }
  203. return 0;
  204. }
  205. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  206. {
  207. AVFilterContext *ctx = inlink->dst;
  208. AVFilterLink *outlink = ctx->outputs[0];
  209. AudioNLMeansContext *s = ctx->priv;
  210. AVFrame *out = NULL;
  211. int available, wanted, ret;
  212. if (s->pts == AV_NOPTS_VALUE)
  213. s->pts = in->pts;
  214. ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
  215. in->nb_samples);
  216. av_frame_free(&in);
  217. s->offset = 0;
  218. available = av_audio_fifo_size(s->fifo);
  219. wanted = (available / s->H) * s->H;
  220. if (wanted >= s->H && available >= s->N) {
  221. out = ff_get_audio_buffer(outlink, wanted);
  222. if (!out)
  223. return AVERROR(ENOMEM);
  224. }
  225. while (available >= s->N) {
  226. ret = av_audio_fifo_peek(s->fifo, (void **)s->in->extended_data, s->N);
  227. if (ret < 0)
  228. break;
  229. ctx->internal->execute(ctx, filter_channel, out, NULL, inlink->channels);
  230. av_audio_fifo_drain(s->fifo, s->H);
  231. s->offset += s->H;
  232. available -= s->H;
  233. }
  234. if (out) {
  235. out->pts = s->pts;
  236. out->nb_samples = s->offset;
  237. if (s->eof_left >= 0) {
  238. out->nb_samples = FFMIN(s->eof_left, s->offset);
  239. s->eof_left -= out->nb_samples;
  240. }
  241. s->pts += s->offset;
  242. return ff_filter_frame(outlink, out);
  243. }
  244. return ret;
  245. }
  246. static int request_frame(AVFilterLink *outlink)
  247. {
  248. AVFilterContext *ctx = outlink->src;
  249. AudioNLMeansContext *s = ctx->priv;
  250. int ret;
  251. ret = ff_request_frame(ctx->inputs[0]);
  252. if (ret == AVERROR_EOF && s->eof_left != 0) {
  253. AVFrame *in;
  254. if (s->eof_left < 0)
  255. s->eof_left = av_audio_fifo_size(s->fifo) - (s->S + s->K);
  256. if (s->eof_left <= 0)
  257. return AVERROR_EOF;
  258. in = ff_get_audio_buffer(outlink, s->H);
  259. if (!in)
  260. return AVERROR(ENOMEM);
  261. return filter_frame(ctx->inputs[0], in);
  262. }
  263. return ret;
  264. }
  265. static av_cold void uninit(AVFilterContext *ctx)
  266. {
  267. AudioNLMeansContext *s = ctx->priv;
  268. av_audio_fifo_free(s->fifo);
  269. av_frame_free(&s->in);
  270. av_frame_free(&s->cache);
  271. }
  272. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  273. char *res, int res_len, int flags)
  274. {
  275. AudioNLMeansContext *s = ctx->priv;
  276. if (!strcmp(cmd, "s")) {
  277. float a;
  278. if (av_sscanf(args, "%f", &a) == 1)
  279. s->a = av_clipf(a, 0.00001, 10);
  280. } else if (!strcmp(cmd, "o")) {
  281. if (!strcmp(args, "i")) {
  282. s->om = IN_MODE;
  283. } else if (!strcmp(args, "o")) {
  284. s->om = OUT_MODE;
  285. } else if (!strcmp(args, "n")) {
  286. s->om = NOISE_MODE;
  287. }
  288. }
  289. return 0;
  290. }
  291. static const AVFilterPad inputs[] = {
  292. {
  293. .name = "default",
  294. .type = AVMEDIA_TYPE_AUDIO,
  295. .filter_frame = filter_frame,
  296. },
  297. { NULL }
  298. };
  299. static const AVFilterPad outputs[] = {
  300. {
  301. .name = "default",
  302. .type = AVMEDIA_TYPE_AUDIO,
  303. .config_props = config_output,
  304. .request_frame = request_frame,
  305. },
  306. { NULL }
  307. };
  308. AVFilter ff_af_anlmdn = {
  309. .name = "anlmdn",
  310. .description = NULL_IF_CONFIG_SMALL("Reduce broadband noise from stream using Non-Local Means."),
  311. .query_formats = query_formats,
  312. .priv_size = sizeof(AudioNLMeansContext),
  313. .priv_class = &anlmdn_class,
  314. .uninit = uninit,
  315. .inputs = inputs,
  316. .outputs = outputs,
  317. .process_command = process_command,
  318. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
  319. AVFILTER_FLAG_SLICE_THREADS,
  320. };