af_earwax.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. int ret;
  76. AVFilterFormats *formats = NULL;
  77. AVFilterChannelLayouts *layout = NULL;
  78. if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_S16 )) < 0 ||
  79. (ret = ff_set_common_formats (ctx , formats )) < 0 ||
  80. (ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO )) < 0 ||
  81. (ret = ff_set_common_channel_layouts (ctx , layout )) < 0 ||
  82. (ret = ff_set_common_samplerates (ctx , ff_make_format_list(sample_rates) )) < 0)
  83. return ret;
  84. return 0;
  85. }
  86. //FIXME: replace with DSPContext.scalarproduct_int16
  87. static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, int16_t *out)
  88. {
  89. int32_t sample;
  90. int16_t j;
  91. while (in < endin) {
  92. sample = 0;
  93. for (j = 0; j < NUMTAPS; j++)
  94. sample += in[j] * filt[j];
  95. *out = av_clip_int16(sample >> 6);
  96. out++;
  97. in++;
  98. }
  99. return out;
  100. }
  101. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  102. {
  103. AVFilterLink *outlink = inlink->dst->outputs[0];
  104. int16_t *taps, *endin, *in, *out;
  105. AVFrame *outsamples = ff_get_audio_buffer(inlink, insamples->nb_samples);
  106. int len;
  107. if (!outsamples) {
  108. av_frame_free(&insamples);
  109. return AVERROR(ENOMEM);
  110. }
  111. av_frame_copy_props(outsamples, insamples);
  112. taps = ((EarwaxContext *)inlink->dst->priv)->taps;
  113. out = (int16_t *)outsamples->data[0];
  114. in = (int16_t *)insamples ->data[0];
  115. len = FFMIN(NUMTAPS, 2*insamples->nb_samples);
  116. // copy part of new input and process with saved input
  117. memcpy(taps+NUMTAPS, in, len * sizeof(*taps));
  118. out = scalarproduct(taps, taps + len, out);
  119. // process current input
  120. if (2*insamples->nb_samples >= NUMTAPS ){
  121. endin = in + insamples->nb_samples * 2 - NUMTAPS;
  122. scalarproduct(in, endin, out);
  123. // save part of input for next round
  124. memcpy(taps, endin, NUMTAPS * sizeof(*taps));
  125. } else
  126. memmove(taps, taps + 2*insamples->nb_samples, NUMTAPS * sizeof(*taps));
  127. av_frame_free(&insamples);
  128. return ff_filter_frame(outlink, outsamples);
  129. }
  130. static const AVFilterPad earwax_inputs[] = {
  131. {
  132. .name = "default",
  133. .type = AVMEDIA_TYPE_AUDIO,
  134. .filter_frame = filter_frame,
  135. },
  136. { NULL }
  137. };
  138. static const AVFilterPad earwax_outputs[] = {
  139. {
  140. .name = "default",
  141. .type = AVMEDIA_TYPE_AUDIO,
  142. },
  143. { NULL }
  144. };
  145. AVFilter ff_af_earwax = {
  146. .name = "earwax",
  147. .description = NULL_IF_CONFIG_SMALL("Widen the stereo image."),
  148. .query_formats = query_formats,
  149. .priv_size = sizeof(EarwaxContext),
  150. .inputs = earwax_inputs,
  151. .outputs = earwax_outputs,
  152. };