af_earwax.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. static int config_input(AVFilterLink *inlink)
  85. {
  86. if (inlink->sample_rate != 44100) {
  87. av_log(inlink->dst, AV_LOG_ERROR,
  88. "The earwax filter only works for 44.1kHz audio. Insert "
  89. "a resample filter before this\n");
  90. return AVERROR(EINVAL);
  91. }
  92. return 0;
  93. }
  94. //FIXME: replace with DSPContext.scalarproduct_int16
  95. static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, int16_t *out)
  96. {
  97. int32_t sample;
  98. int16_t j;
  99. while (in < endin) {
  100. sample = 32;
  101. for (j = 0; j < NUMTAPS; j++)
  102. sample += in[j] * filt[j];
  103. *out = sample >> 6;
  104. out++;
  105. in++;
  106. }
  107. return out;
  108. }
  109. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  110. {
  111. AVFilterLink *outlink = inlink->dst->outputs[0];
  112. int16_t *taps, *endin, *in, *out;
  113. AVFilterBufferRef *outsamples =
  114. ff_get_audio_buffer(inlink, AV_PERM_WRITE,
  115. insamples->audio->nb_samples);
  116. int ret;
  117. if (!outsamples)
  118. return AVERROR(ENOMEM);
  119. avfilter_copy_buffer_ref_props(outsamples, insamples);
  120. taps = ((EarwaxContext *)inlink->dst->priv)->taps;
  121. out = (int16_t *)outsamples->data[0];
  122. in = (int16_t *)insamples ->data[0];
  123. // copy part of new input and process with saved input
  124. memcpy(taps+NUMTAPS, in, NUMTAPS * sizeof(*taps));
  125. out = scalarproduct(taps, taps + NUMTAPS, out);
  126. // process current input
  127. endin = in + insamples->audio->nb_samples * 2 - NUMTAPS;
  128. scalarproduct(in, endin, out);
  129. // save part of input for next round
  130. memcpy(taps, endin, NUMTAPS * sizeof(*taps));
  131. ret = ff_filter_frame(outlink, outsamples);
  132. avfilter_unref_buffer(insamples);
  133. return ret;
  134. }
  135. static const AVFilterPad earwax_inputs[] = {
  136. {
  137. .name = "default",
  138. .type = AVMEDIA_TYPE_AUDIO,
  139. .filter_frame = filter_frame,
  140. .config_props = config_input,
  141. .min_perms = AV_PERM_READ,
  142. },
  143. { NULL }
  144. };
  145. static const AVFilterPad earwax_outputs[] = {
  146. {
  147. .name = "default",
  148. .type = AVMEDIA_TYPE_AUDIO,
  149. },
  150. { NULL }
  151. };
  152. AVFilter avfilter_af_earwax = {
  153. .name = "earwax",
  154. .description = NULL_IF_CONFIG_SMALL("Widen the stereo image."),
  155. .query_formats = query_formats,
  156. .priv_size = sizeof(EarwaxContext),
  157. .inputs = earwax_inputs,
  158. .outputs = earwax_outputs,
  159. };