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.7KB

  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 *i, *o;
  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->i = ff_get_audio_buffer(inlink, 2);
  90. s->o = ff_get_audio_buffer(inlink, 2);
  91. if (!s->buffer || !s->i || !s->o)
  92. return AVERROR(ENOMEM);
  93. return get_coeffs(ctx);
  94. }
  95. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  96. {
  97. AVFilterContext *ctx = inlink->dst;
  98. AVFilterLink *outlink = ctx->outputs[0];
  99. ASubBoostContext *s = ctx->priv;
  100. const float wet = s->wet_gain, dry = s->dry_gain, feedback = s->feedback, decay = s->decay;
  101. int write_pos;
  102. AVFrame *out;
  103. if (av_frame_is_writable(in)) {
  104. out = in;
  105. } else {
  106. out = ff_get_audio_buffer(outlink, in->nb_samples);
  107. if (!out) {
  108. av_frame_free(&in);
  109. return AVERROR(ENOMEM);
  110. }
  111. av_frame_copy_props(out, in);
  112. }
  113. for (int ch = 0; ch < in->channels; ch++) {
  114. const double *src = (const double *)in->extended_data[ch];
  115. double *dst = (double *)out->extended_data[ch];
  116. double *buffer = (double *)s->buffer->extended_data[ch];
  117. double *ix = (double *)s->i->extended_data[ch];
  118. double *ox = (double *)s->o->extended_data[ch];
  119. write_pos = s->write_pos;
  120. for (int n = 0; n < in->nb_samples; n++) {
  121. double out_sample;
  122. out_sample = src[n] * s->b0 + ix[0] * s->b1 + ix[1] * s->b2 - ox[0] * s->a1 - ox[1] * s->a2;
  123. ix[1] = ix[0];
  124. ix[0] = src[n];
  125. ox[1] = ox[0];
  126. ox[0] = out_sample;
  127. buffer[write_pos] = buffer[write_pos] * decay + out_sample * feedback;
  128. dst[n] = src[n] * dry + buffer[write_pos] * wet;
  129. if (++write_pos >= s->buffer_samples)
  130. write_pos = 0;
  131. }
  132. }
  133. s->write_pos = write_pos;
  134. if (out != in)
  135. av_frame_free(&in);
  136. return ff_filter_frame(outlink, out);
  137. }
  138. static av_cold void uninit(AVFilterContext *ctx)
  139. {
  140. ASubBoostContext *s = ctx->priv;
  141. av_frame_free(&s->buffer);
  142. av_frame_free(&s->i);
  143. av_frame_free(&s->o);
  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. };