af_crossfeed.c 5.4 KB

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