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.

192 lines
5.9KB

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