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.

424 lines
11KB

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