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.

347 lines
8.9KB

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