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.

170 lines
5.2KB

  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/opt.h"
  20. #include "avfilter.h"
  21. #include "audio.h"
  22. #include "formats.h"
  23. typedef struct CrossfeedContext {
  24. const AVClass *class;
  25. double range;
  26. double strength;
  27. double level_in;
  28. double level_out;
  29. double a0, a1, a2;
  30. double b0, b1, b2;
  31. double i1, i2;
  32. double o1, o2;
  33. } CrossfeedContext;
  34. static int query_formats(AVFilterContext *ctx)
  35. {
  36. AVFilterFormats *formats = NULL;
  37. AVFilterChannelLayouts *layout = NULL;
  38. int ret;
  39. if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_DBL )) < 0 ||
  40. (ret = ff_set_common_formats (ctx , formats )) < 0 ||
  41. (ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO)) < 0 ||
  42. (ret = ff_set_common_channel_layouts (ctx , layout )) < 0 ||
  43. (ret = ff_set_common_samplerates (ctx , ff_all_samplerates())) < 0)
  44. return ret;
  45. return 0;
  46. }
  47. static int config_input(AVFilterLink *inlink)
  48. {
  49. AVFilterContext *ctx = inlink->dst;
  50. CrossfeedContext *s = ctx->priv;
  51. double A = exp(s->strength * -30 / 40 * log(10.));
  52. double w0 = 2 * M_PI * (1. - s->range) * 2100 / inlink->sample_rate;
  53. double alpha;
  54. alpha = sin(w0) / 2 * sqrt(2 * (1 / 0.5 - 1) + 2);
  55. s->a0 = (A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
  56. s->a1 = -2 * ((A - 1) + (A + 1) * cos(w0));
  57. s->a2 = (A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
  58. s->b0 = A * ((A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
  59. s->b1 = 2 * A * ((A - 1) - (A + 1) * cos(w0));
  60. s->b2 = A * ((A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
  61. s->a1 /= s->a0;
  62. s->a2 /= s->a0;
  63. s->b0 /= s->a0;
  64. s->b1 /= s->a0;
  65. s->b2 /= s->a0;
  66. return 0;
  67. }
  68. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  69. {
  70. AVFilterContext *ctx = inlink->dst;
  71. AVFilterLink *outlink = ctx->outputs[0];
  72. CrossfeedContext *s = ctx->priv;
  73. const double *src = (const double *)in->data[0];
  74. const double level_in = s->level_in;
  75. const double level_out = s->level_out;
  76. const double b0 = s->b0;
  77. const double b1 = s->b1;
  78. const double b2 = s->b2;
  79. const double a1 = s->a1;
  80. const double a2 = s->a2;
  81. AVFrame *out;
  82. double *dst;
  83. int n;
  84. if (av_frame_is_writable(in)) {
  85. out = in;
  86. } else {
  87. out = ff_get_audio_buffer(outlink, in->nb_samples);
  88. if (!out) {
  89. av_frame_free(&in);
  90. return AVERROR(ENOMEM);
  91. }
  92. av_frame_copy_props(out, in);
  93. }
  94. dst = (double *)out->data[0];
  95. for (n = 0; n < out->nb_samples; n++, src += 2, dst += 2) {
  96. double mid = (src[0] + src[1]) * level_in * .5;
  97. double side = (src[0] - src[1]) * level_in * .5;
  98. double oside = side * b0 + s->i1 * b1 + s->i2 * b2 - s->o1 * a1 - s->o2 * a2;
  99. s->i2 = s->i1;
  100. s->i1 = side;
  101. s->o2 = s->o1;
  102. s->o1 = oside;
  103. dst[0] = (mid + oside) * level_out;
  104. dst[1] = (mid - oside) * level_out;
  105. }
  106. if (out != in)
  107. av_frame_free(&in);
  108. return ff_filter_frame(outlink, out);
  109. }
  110. #define OFFSET(x) offsetof(CrossfeedContext, x)
  111. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  112. static const AVOption crossfeed_options[] = {
  113. { "strength", "set crossfeed strength", OFFSET(strength), AV_OPT_TYPE_DOUBLE, {.dbl=.2}, 0, 1, FLAGS },
  114. { "range", "set soundstage wideness", OFFSET(range), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, 0, 1, FLAGS },
  115. { "level_in", "set level in", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=.9}, 0, 1, FLAGS },
  116. { "level_out", "set level out", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0, 1, FLAGS },
  117. { NULL }
  118. };
  119. AVFILTER_DEFINE_CLASS(crossfeed);
  120. static const AVFilterPad inputs[] = {
  121. {
  122. .name = "default",
  123. .type = AVMEDIA_TYPE_AUDIO,
  124. .filter_frame = filter_frame,
  125. .config_props = config_input,
  126. },
  127. { NULL }
  128. };
  129. static const AVFilterPad outputs[] = {
  130. {
  131. .name = "default",
  132. .type = AVMEDIA_TYPE_AUDIO,
  133. },
  134. { NULL }
  135. };
  136. AVFilter ff_af_crossfeed = {
  137. .name = "crossfeed",
  138. .description = NULL_IF_CONFIG_SMALL("Apply headphone crossfeed filter."),
  139. .query_formats = query_formats,
  140. .priv_size = sizeof(CrossfeedContext),
  141. .priv_class = &crossfeed_class,
  142. .inputs = inputs,
  143. .outputs = outputs,
  144. };