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.

372 lines
10KB

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