af_extrastereo.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2015 The FFmpeg Project
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/channel_layout.h"
  21. #include "libavutil/opt.h"
  22. #include "avfilter.h"
  23. #include "audio.h"
  24. #include "formats.h"
  25. typedef struct ExtraStereoContext {
  26. const AVClass *class;
  27. float mult;
  28. int clip;
  29. } ExtraStereoContext;
  30. #define OFFSET(x) offsetof(ExtraStereoContext, x)
  31. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  32. static const AVOption extrastereo_options[] = {
  33. { "m", "set the difference coefficient", OFFSET(mult), AV_OPT_TYPE_FLOAT, {.dbl=2.5}, -10, 10, A },
  34. { "c", "enable clipping", OFFSET(clip), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, A },
  35. { NULL }
  36. };
  37. AVFILTER_DEFINE_CLASS(extrastereo);
  38. static int query_formats(AVFilterContext *ctx)
  39. {
  40. AVFilterFormats *formats = NULL;
  41. AVFilterChannelLayouts *layout = NULL;
  42. int ret;
  43. if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_FLT )) < 0 ||
  44. (ret = ff_set_common_formats (ctx , formats )) < 0 ||
  45. (ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO)) < 0 ||
  46. (ret = ff_set_common_channel_layouts (ctx , layout )) < 0)
  47. return ret;
  48. formats = ff_all_samplerates();
  49. return ff_set_common_samplerates(ctx, formats);
  50. }
  51. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  52. {
  53. AVFilterContext *ctx = inlink->dst;
  54. AVFilterLink *outlink = ctx->outputs[0];
  55. ExtraStereoContext *s = ctx->priv;
  56. const float *src = (const float *)in->data[0];
  57. const float mult = s->mult;
  58. AVFrame *out;
  59. float *dst;
  60. int n;
  61. if (av_frame_is_writable(in)) {
  62. out = in;
  63. } else {
  64. out = ff_get_audio_buffer(outlink, in->nb_samples);
  65. if (!out) {
  66. av_frame_free(&in);
  67. return AVERROR(ENOMEM);
  68. }
  69. av_frame_copy_props(out, in);
  70. }
  71. dst = (float *)out->data[0];
  72. for (n = 0; n < in->nb_samples; n++) {
  73. float average, left, right;
  74. left = src[n * 2 ];
  75. right = src[n * 2 + 1];
  76. average = (left + right) / 2.;
  77. left = average + mult * (left - average);
  78. right = average + mult * (right - average);
  79. if (s->clip) {
  80. left = av_clipf(left, -1, 1);
  81. right = av_clipf(right, -1, 1);
  82. }
  83. dst[n * 2 ] = left;
  84. dst[n * 2 + 1] = right;
  85. }
  86. if (out != in)
  87. av_frame_free(&in);
  88. return ff_filter_frame(outlink, out);
  89. }
  90. static const AVFilterPad inputs[] = {
  91. {
  92. .name = "default",
  93. .type = AVMEDIA_TYPE_AUDIO,
  94. .filter_frame = filter_frame,
  95. },
  96. { NULL }
  97. };
  98. static const AVFilterPad outputs[] = {
  99. {
  100. .name = "default",
  101. .type = AVMEDIA_TYPE_AUDIO,
  102. },
  103. { NULL }
  104. };
  105. AVFilter ff_af_extrastereo = {
  106. .name = "extrastereo",
  107. .description = NULL_IF_CONFIG_SMALL("Increase difference between stereo audio channels."),
  108. .query_formats = query_formats,
  109. .priv_size = sizeof(ExtraStereoContext),
  110. .priv_class = &extrastereo_class,
  111. .inputs = inputs,
  112. .outputs = outputs,
  113. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  114. };