af_earwax.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2011 Mina Nagy Zaki
  3. * Copyright (c) 2000 Edward Beingessner And Sundry Contributors.
  4. * This source code is freely redistributable and may be used for any purpose.
  5. * This copyright notice must be maintained. Edward Beingessner And Sundry
  6. * Contributors are not responsible for the consequences of using this
  7. * software.
  8. *
  9. * This file is part of FFmpeg.
  10. *
  11. * FFmpeg is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * FFmpeg is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with FFmpeg; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. /**
  26. * @file
  27. * Stereo Widening Effect. Adds audio cues to move stereo image in
  28. * front of the listener. Adapted from the libsox earwax effect.
  29. */
  30. #include "libavutil/channel_layout.h"
  31. #include "avfilter.h"
  32. #include "audio.h"
  33. #include "formats.h"
  34. #define NUMTAPS 64
  35. static const int8_t filt[NUMTAPS] = {
  36. /* 30° 330° */
  37. 4, -6, /* 32 tap stereo FIR filter. */
  38. 4, -11, /* One side filters as if the */
  39. -1, -5, /* signal was from 30 degrees */
  40. 3, 3, /* from the ear, the other as */
  41. -2, 5, /* if 330 degrees. */
  42. -5, 0,
  43. 9, 1,
  44. 6, 3, /* Input */
  45. -4, -1, /* Left Right */
  46. -5, -3, /* __________ __________ */
  47. -2, -5, /* | | | | */
  48. -7, 1, /* .---| Hh,0(f) | | Hh,0(f) |---. */
  49. 6, -7, /* / |__________| |__________| \ */
  50. 30, -29, /* / \ / \ */
  51. 12, -3, /* / X \ */
  52. -11, 4, /* / / \ \ */
  53. -3, 7, /* ____V_____ __________V V__________ _____V____ */
  54. -20, 23, /* | | | | | | | | */
  55. 2, 0, /* | Hh,30(f) | | Hh,330(f)| | Hh,330(f)| | Hh,30(f) | */
  56. 1, -6, /* |__________| |__________| |__________| |__________| */
  57. -14, -5, /* \ ___ / \ ___ / */
  58. 15, -18, /* \ / \ / _____ \ / \ / */
  59. 6, 7, /* `->| + |<--' / \ `-->| + |<-' */
  60. 15, -10, /* \___/ _/ \_ \___/ */
  61. -14, 22, /* \ / \ / \ / */
  62. -7, -2, /* `--->| | | |<---' */
  63. -4, 9, /* \_/ \_/ */
  64. 6, -12, /* */
  65. 6, -6, /* Headphones */
  66. 0, -11,
  67. 0, -5,
  68. 4, 0};
  69. typedef struct {
  70. int16_t taps[NUMTAPS * 2];
  71. } EarwaxContext;
  72. static int query_formats(AVFilterContext *ctx)
  73. {
  74. static const int sample_rates[] = { 44100, -1 };
  75. AVFilterFormats *formats = NULL;
  76. AVFilterChannelLayouts *layout = NULL;
  77. ff_add_format(&formats, AV_SAMPLE_FMT_S16);
  78. ff_set_common_formats(ctx, formats);
  79. ff_add_channel_layout(&layout, AV_CH_LAYOUT_STEREO);
  80. ff_set_common_channel_layouts(ctx, layout);
  81. ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
  82. return 0;
  83. }
  84. //FIXME: replace with DSPContext.scalarproduct_int16
  85. static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, int16_t *out)
  86. {
  87. int32_t sample;
  88. int16_t j;
  89. while (in < endin) {
  90. sample = 0;
  91. for (j = 0; j < NUMTAPS; j++)
  92. sample += in[j] * filt[j];
  93. *out = av_clip_int16(sample >> 6);
  94. out++;
  95. in++;
  96. }
  97. return out;
  98. }
  99. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  100. {
  101. AVFilterLink *outlink = inlink->dst->outputs[0];
  102. int16_t *taps, *endin, *in, *out;
  103. AVFrame *outsamples = ff_get_audio_buffer(inlink, insamples->nb_samples);
  104. int len;
  105. if (!outsamples) {
  106. av_frame_free(&insamples);
  107. return AVERROR(ENOMEM);
  108. }
  109. av_frame_copy_props(outsamples, insamples);
  110. taps = ((EarwaxContext *)inlink->dst->priv)->taps;
  111. out = (int16_t *)outsamples->data[0];
  112. in = (int16_t *)insamples ->data[0];
  113. len = FFMIN(NUMTAPS, 2*insamples->nb_samples);
  114. // copy part of new input and process with saved input
  115. memcpy(taps+NUMTAPS, in, len * sizeof(*taps));
  116. out = scalarproduct(taps, taps + len, out);
  117. // process current input
  118. if (2*insamples->nb_samples >= NUMTAPS ){
  119. endin = in + insamples->nb_samples * 2 - NUMTAPS;
  120. scalarproduct(in, endin, out);
  121. // save part of input for next round
  122. memcpy(taps, endin, NUMTAPS * sizeof(*taps));
  123. } else
  124. memmove(taps, taps + 2*insamples->nb_samples, NUMTAPS * sizeof(*taps));
  125. av_frame_free(&insamples);
  126. return ff_filter_frame(outlink, outsamples);
  127. }
  128. static const AVFilterPad earwax_inputs[] = {
  129. {
  130. .name = "default",
  131. .type = AVMEDIA_TYPE_AUDIO,
  132. .filter_frame = filter_frame,
  133. },
  134. { NULL }
  135. };
  136. static const AVFilterPad earwax_outputs[] = {
  137. {
  138. .name = "default",
  139. .type = AVMEDIA_TYPE_AUDIO,
  140. },
  141. { NULL }
  142. };
  143. AVFilter ff_af_earwax = {
  144. .name = "earwax",
  145. .description = NULL_IF_CONFIG_SMALL("Widen the stereo image."),
  146. .query_formats = query_formats,
  147. .priv_size = sizeof(EarwaxContext),
  148. .inputs = earwax_inputs,
  149. .outputs = earwax_outputs,
  150. };