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.

367 lines
9.8KB

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