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.

233 lines
6.6KB

  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/channel_layout.h"
  19. #include "libavutil/ffmath.h"
  20. #include "libavutil/opt.h"
  21. #include "avfilter.h"
  22. #include "audio.h"
  23. #include "formats.h"
  24. typedef struct ASubBoostContext {
  25. const AVClass *class;
  26. double dry_gain;
  27. double wet_gain;
  28. double feedback;
  29. double decay;
  30. double delay;
  31. double cutoff;
  32. double slope;
  33. double a0, a1, a2;
  34. double b0, b1, b2;
  35. int write_pos;
  36. int buffer_samples;
  37. AVFrame *w;
  38. AVFrame *buffer;
  39. } ASubBoostContext;
  40. static int query_formats(AVFilterContext *ctx)
  41. {
  42. AVFilterFormats *formats = NULL;
  43. AVFilterChannelLayouts *layouts = NULL;
  44. static const enum AVSampleFormat sample_fmts[] = {
  45. AV_SAMPLE_FMT_DBLP,
  46. AV_SAMPLE_FMT_NONE
  47. };
  48. int ret;
  49. formats = ff_make_format_list(sample_fmts);
  50. if (!formats)
  51. return AVERROR(ENOMEM);
  52. ret = ff_set_common_formats(ctx, formats);
  53. if (ret < 0)
  54. return ret;
  55. layouts = ff_all_channel_counts();
  56. if (!layouts)
  57. return AVERROR(ENOMEM);
  58. ret = ff_set_common_channel_layouts(ctx, layouts);
  59. if (ret < 0)
  60. return ret;
  61. formats = ff_all_samplerates();
  62. return ff_set_common_samplerates(ctx, formats);
  63. }
  64. static int get_coeffs(AVFilterContext *ctx)
  65. {
  66. ASubBoostContext *s = ctx->priv;
  67. AVFilterLink *inlink = ctx->inputs[0];
  68. double w0 = 2 * M_PI * s->cutoff / inlink->sample_rate;
  69. double alpha = sin(w0) / 2 * sqrt(2. * (1. / s->slope - 1.) + 2.);
  70. s->a0 = 1 + alpha;
  71. s->a1 = -2 * cos(w0);
  72. s->a2 = 1 - alpha;
  73. s->b0 = (1 - cos(w0)) / 2;
  74. s->b1 = 1 - cos(w0);
  75. s->b2 = (1 - cos(w0)) / 2;
  76. s->a1 /= s->a0;
  77. s->a2 /= s->a0;
  78. s->b0 /= s->a0;
  79. s->b1 /= s->a0;
  80. s->b2 /= s->a0;
  81. s->buffer_samples = inlink->sample_rate * s->delay / 1000;
  82. return 0;
  83. }
  84. static int config_input(AVFilterLink *inlink)
  85. {
  86. AVFilterContext *ctx = inlink->dst;
  87. ASubBoostContext *s = ctx->priv;
  88. s->buffer = ff_get_audio_buffer(inlink, inlink->sample_rate / 10);
  89. s->w = ff_get_audio_buffer(inlink, 2);
  90. if (!s->buffer || !s->w)
  91. return AVERROR(ENOMEM);
  92. return get_coeffs(ctx);
  93. }
  94. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  95. {
  96. AVFilterContext *ctx = inlink->dst;
  97. AVFilterLink *outlink = ctx->outputs[0];
  98. ASubBoostContext *s = ctx->priv;
  99. const double wet = s->wet_gain, dry = s->dry_gain, feedback = s->feedback, decay = s->decay;
  100. const double b0 = s->b0;
  101. const double b1 = s->b1;
  102. const double b2 = s->b2;
  103. const double a1 = -s->a1;
  104. const double a2 = -s->a2;
  105. int write_pos;
  106. AVFrame *out;
  107. if (av_frame_is_writable(in)) {
  108. out = in;
  109. } else {
  110. out = ff_get_audio_buffer(outlink, in->nb_samples);
  111. if (!out) {
  112. av_frame_free(&in);
  113. return AVERROR(ENOMEM);
  114. }
  115. av_frame_copy_props(out, in);
  116. }
  117. for (int ch = 0; ch < in->channels; ch++) {
  118. const double *src = (const double *)in->extended_data[ch];
  119. double *dst = (double *)out->extended_data[ch];
  120. double *buffer = (double *)s->buffer->extended_data[ch];
  121. double *w = (double *)s->w->extended_data[ch];
  122. write_pos = s->write_pos;
  123. for (int n = 0; n < in->nb_samples; n++) {
  124. double out_sample;
  125. out_sample = src[n] * b0 + w[0];
  126. w[0] = b1 * src[n] + w[1] + a1 * out_sample;
  127. w[1] = b2 * src[n] + a2 * out_sample;
  128. buffer[write_pos] = buffer[write_pos] * decay + out_sample * feedback;
  129. dst[n] = src[n] * dry + buffer[write_pos] * wet;
  130. if (++write_pos >= s->buffer_samples)
  131. write_pos = 0;
  132. }
  133. }
  134. s->write_pos = write_pos;
  135. if (out != in)
  136. av_frame_free(&in);
  137. return ff_filter_frame(outlink, out);
  138. }
  139. static av_cold void uninit(AVFilterContext *ctx)
  140. {
  141. ASubBoostContext *s = ctx->priv;
  142. av_frame_free(&s->buffer);
  143. av_frame_free(&s->w);
  144. }
  145. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  146. char *res, int res_len, int flags)
  147. {
  148. int ret;
  149. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  150. if (ret < 0)
  151. return ret;
  152. return get_coeffs(ctx);
  153. }
  154. #define OFFSET(x) offsetof(ASubBoostContext, x)
  155. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  156. static const AVOption asubboost_options[] = {
  157. { "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, FLAGS },
  158. { "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_DOUBLE, {.dbl=0.8}, 0, 1, FLAGS },
  159. { "decay", "set decay", OFFSET(decay), AV_OPT_TYPE_DOUBLE, {.dbl=0.7}, 0, 1, FLAGS },
  160. { "feedback", "set feedback", OFFSET(feedback), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, FLAGS },
  161. { "cutoff", "set cutoff", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 50, 900, FLAGS },
  162. { "slope", "set slope", OFFSET(slope), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0.0001, 1, FLAGS },
  163. { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 1, 100, FLAGS },
  164. { NULL }
  165. };
  166. AVFILTER_DEFINE_CLASS(asubboost);
  167. static const AVFilterPad inputs[] = {
  168. {
  169. .name = "default",
  170. .type = AVMEDIA_TYPE_AUDIO,
  171. .filter_frame = filter_frame,
  172. .config_props = config_input,
  173. },
  174. { NULL }
  175. };
  176. static const AVFilterPad outputs[] = {
  177. {
  178. .name = "default",
  179. .type = AVMEDIA_TYPE_AUDIO,
  180. },
  181. { NULL }
  182. };
  183. AVFilter ff_af_asubboost = {
  184. .name = "asubboost",
  185. .description = NULL_IF_CONFIG_SMALL("Boost subwoofer frequencies."),
  186. .query_formats = query_formats,
  187. .priv_size = sizeof(ASubBoostContext),
  188. .priv_class = &asubboost_class,
  189. .uninit = uninit,
  190. .inputs = inputs,
  191. .outputs = outputs,
  192. .process_command = process_command,
  193. };