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.

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