af_earwax.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/audioconvert.h"
  31. #include "avfilter.h"
  32. #define NUMTAPS 64
  33. static const int8_t filt[NUMTAPS] = {
  34. /* 30° 330° */
  35. 4, -6, /* 32 tap stereo FIR filter. */
  36. 4, -11, /* One side filters as if the */
  37. -1, -5, /* signal was from 30 degrees */
  38. 3, 3, /* from the ear, the other as */
  39. -2, 5, /* if 330 degrees. */
  40. -5, 0,
  41. 9, 1,
  42. 6, 3, /* Input */
  43. -4, -1, /* Left Right */
  44. -5, -3, /* __________ __________ */
  45. -2, -5, /* | | | | */
  46. -7, 1, /* .---| Hh,0(f) | | Hh,0(f) |---. */
  47. 6, -7, /* / |__________| |__________| \ */
  48. 30, -29, /* / \ / \ */
  49. 12, -3, /* / X \ */
  50. -11, 4, /* / / \ \ */
  51. -3, 7, /* ____V_____ __________V V__________ _____V____ */
  52. -20, 23, /* | | | | | | | | */
  53. 2, 0, /* | Hh,30(f) | | Hh,330(f)| | Hh,330(f)| | Hh,30(f) | */
  54. 1, -6, /* |__________| |__________| |__________| |__________| */
  55. -14, -5, /* \ ___ / \ ___ / */
  56. 15, -18, /* \ / \ / _____ \ / \ / */
  57. 6, 7, /* `->| + |<--' / \ `-->| + |<-' */
  58. 15, -10, /* \___/ _/ \_ \___/ */
  59. -14, 22, /* \ / \ / \ / */
  60. -7, -2, /* `--->| | | |<---' */
  61. -4, 9, /* \_/ \_/ */
  62. 6, -12, /* */
  63. 6, -6, /* Headphones */
  64. 0, -11,
  65. 0, -5,
  66. 4, 0};
  67. typedef struct {
  68. int16_t taps[NUMTAPS * 2];
  69. } EarwaxContext;
  70. static int query_formats(AVFilterContext *ctx)
  71. {
  72. AVFilterFormats *formats = NULL;
  73. avfilter_add_format(&formats, AV_SAMPLE_FMT_S16);
  74. avfilter_set_common_sample_formats(ctx, formats);
  75. formats = NULL;
  76. avfilter_add_format(&formats, AV_CH_LAYOUT_STEREO);
  77. avfilter_set_common_channel_layouts(ctx, formats);
  78. formats = NULL;
  79. avfilter_add_format(&formats, AVFILTER_PACKED);
  80. avfilter_set_common_packing_formats(ctx, formats);
  81. return 0;
  82. }
  83. static int config_input(AVFilterLink *inlink)
  84. {
  85. if (inlink->sample_rate != 44100) {
  86. av_log(inlink->dst, AV_LOG_ERROR,
  87. "The earwax filter only works for 44.1kHz audio. Insert "
  88. "a resample filter before this\n");
  89. return AVERROR(EINVAL);
  90. }
  91. return 0;
  92. }
  93. //FIXME: replace with DSPContext.scalarproduct_int16
  94. static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, int16_t *out)
  95. {
  96. int32_t sample;
  97. int16_t j;
  98. while (in < endin) {
  99. sample = 32;
  100. for (j = 0; j < NUMTAPS; j++)
  101. sample += in[j] * filt[j];
  102. *out = sample >> 6;
  103. out++;
  104. in++;
  105. }
  106. return out;
  107. }
  108. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  109. {
  110. AVFilterLink *outlink = inlink->dst->outputs[0];
  111. int16_t *taps, *endin, *in, *out;
  112. AVFilterBufferRef *outsamples =
  113. avfilter_get_audio_buffer(inlink, AV_PERM_WRITE,
  114. insamples->audio->nb_samples);
  115. avfilter_copy_buffer_ref_props(outsamples, insamples);
  116. taps = ((EarwaxContext *)inlink->dst->priv)->taps;
  117. out = (int16_t *)outsamples->data[0];
  118. in = (int16_t *)insamples ->data[0];
  119. // copy part of new input and process with saved input
  120. memcpy(taps+NUMTAPS, in, NUMTAPS * sizeof(*taps));
  121. out = scalarproduct(taps, taps + NUMTAPS, out);
  122. // process current input
  123. endin = in + insamples->audio->nb_samples * 2 - NUMTAPS;
  124. out = scalarproduct(in, endin, out);
  125. // save part of input for next round
  126. memcpy(taps, endin, NUMTAPS * sizeof(*taps));
  127. avfilter_filter_samples(outlink, outsamples);
  128. avfilter_unref_buffer(insamples);
  129. }
  130. AVFilter avfilter_af_earwax = {
  131. .name = "earwax",
  132. .description = NULL_IF_CONFIG_SMALL("Widen the stereo image."),
  133. .query_formats = query_formats,
  134. .priv_size = sizeof(EarwaxContext),
  135. .inputs = (const AVFilterPad[]) {{ .name = "default",
  136. .type = AVMEDIA_TYPE_AUDIO,
  137. .filter_samples = filter_samples,
  138. .config_props = config_input,
  139. .min_perms = AV_PERM_READ, },
  140. { .name = NULL}},
  141. .outputs = (const AVFilterPad[]) {{ .name = "default",
  142. .type = AVMEDIA_TYPE_AUDIO, },
  143. { .name = NULL}},
  144. };