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.

317 lines
8.2KB

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