af_adynamicsmooth.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/ffmath.h"
  19. #include "libavutil/opt.h"
  20. #include "avfilter.h"
  21. #include "audio.h"
  22. #include "formats.h"
  23. typedef struct AudioDynamicSmoothContext {
  24. const AVClass *class;
  25. double sensitivity;
  26. double basefreq;
  27. AVFrame *coeffs;
  28. } AudioDynamicSmoothContext;
  29. static int config_input(AVFilterLink *inlink)
  30. {
  31. AVFilterContext *ctx = inlink->dst;
  32. AudioDynamicSmoothContext *s = ctx->priv;
  33. s->coeffs = ff_get_audio_buffer(inlink, 3);
  34. if (!s->coeffs)
  35. return AVERROR(ENOMEM);
  36. return 0;
  37. }
  38. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  39. {
  40. AVFilterContext *ctx = inlink->dst;
  41. AVFilterLink *outlink = ctx->outputs[0];
  42. AudioDynamicSmoothContext *s = ctx->priv;
  43. const double sensitivity = s->sensitivity;
  44. const double wc = s->basefreq / in->sample_rate;
  45. AVFrame *out;
  46. if (av_frame_is_writable(in)) {
  47. out = in;
  48. } else {
  49. out = ff_get_audio_buffer(outlink, in->nb_samples);
  50. if (!out) {
  51. av_frame_free(&in);
  52. return AVERROR(ENOMEM);
  53. }
  54. av_frame_copy_props(out, in);
  55. }
  56. for (int ch = 0; ch < out->channels; ch++) {
  57. const double *src = (const double *)in->extended_data[ch];
  58. double *dst = (double *)out->extended_data[ch];
  59. double *coeffs = (double *)s->coeffs->extended_data[ch];
  60. double low1 = coeffs[0];
  61. double low2 = coeffs[1];
  62. double inz = coeffs[2];
  63. for (int n = 0; n < out->nb_samples; n++) {
  64. double low1z = low1;
  65. double low2z = low2;
  66. double bandz = low2z - low1z;
  67. double wd = wc + sensitivity * fabs(bandz);
  68. double g = fmin(1., wd * (5.9948827 + wd * (-11.969296 + wd * 15.959062)));
  69. low1 = low1z + g * (0.5 * (src[n] + inz) - low1z);
  70. low2 = low2z + g * (0.5 * (low1 + low1z) - low2z);
  71. inz = src[n];
  72. dst[n] = ctx->is_disabled ? src[n] : low2;
  73. }
  74. coeffs[0] = low1;
  75. coeffs[1] = low2;
  76. coeffs[2] = inz;
  77. }
  78. if (out != in)
  79. av_frame_free(&in);
  80. return ff_filter_frame(outlink, out);
  81. }
  82. static av_cold void uninit(AVFilterContext *ctx)
  83. {
  84. AudioDynamicSmoothContext *s = ctx->priv;
  85. av_frame_free(&s->coeffs);
  86. }
  87. #define OFFSET(x) offsetof(AudioDynamicSmoothContext, x)
  88. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  89. static const AVOption adynamicsmooth_options[] = {
  90. { "sensitivity", "set smooth sensitivity", OFFSET(sensitivity), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, 1000000, FLAGS },
  91. { "basefreq", "set base frequency", OFFSET(basefreq), AV_OPT_TYPE_DOUBLE, {.dbl=22050}, 2, 1000000, FLAGS },
  92. { NULL }
  93. };
  94. AVFILTER_DEFINE_CLASS(adynamicsmooth);
  95. static const AVFilterPad inputs[] = {
  96. {
  97. .name = "default",
  98. .type = AVMEDIA_TYPE_AUDIO,
  99. .filter_frame = filter_frame,
  100. .config_props = config_input,
  101. },
  102. };
  103. static const AVFilterPad outputs[] = {
  104. {
  105. .name = "default",
  106. .type = AVMEDIA_TYPE_AUDIO,
  107. },
  108. };
  109. const AVFilter ff_af_adynamicsmooth = {
  110. .name = "adynamicsmooth",
  111. .description = NULL_IF_CONFIG_SMALL("Apply Dynamic Smoothing of input audio."),
  112. .priv_size = sizeof(AudioDynamicSmoothContext),
  113. .priv_class = &adynamicsmooth_class,
  114. .uninit = uninit,
  115. FILTER_INPUTS(inputs),
  116. FILTER_OUTPUTS(outputs),
  117. FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBLP),
  118. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  119. .process_command = ff_filter_process_command,
  120. };