You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
8.1KB

  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 32
  35. static const int8_t filt[NUMTAPS * 2] = {
  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 EarwaxContext {
  70. int16_t filter[2][NUMTAPS];
  71. int16_t taps[4][NUMTAPS * 2];
  72. AVFrame *frame[2];
  73. } EarwaxContext;
  74. static int query_formats(AVFilterContext *ctx)
  75. {
  76. static const int sample_rates[] = { 44100, -1 };
  77. int ret;
  78. AVFilterFormats *formats = NULL;
  79. AVFilterChannelLayouts *layout = NULL;
  80. if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_S16P )) < 0 ||
  81. (ret = ff_set_common_formats (ctx , formats )) < 0 ||
  82. (ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO )) < 0 ||
  83. (ret = ff_set_common_channel_layouts (ctx , layout )) < 0 ||
  84. (ret = ff_set_common_samplerates (ctx , ff_make_format_list(sample_rates) )) < 0)
  85. return ret;
  86. return 0;
  87. }
  88. //FIXME: replace with DSPContext.scalarproduct_int16
  89. static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin,
  90. const int16_t *filt, int16_t *out)
  91. {
  92. int32_t sample;
  93. int16_t j;
  94. while (in < endin) {
  95. sample = 0;
  96. for (j = 0; j < NUMTAPS; j++)
  97. sample += in[j] * filt[j];
  98. *out = av_clip_int16(sample >> 7);
  99. out++;
  100. in++;
  101. }
  102. return out;
  103. }
  104. static int config_input(AVFilterLink *inlink)
  105. {
  106. EarwaxContext *s = inlink->dst->priv;
  107. for (int i = 0; i < NUMTAPS; i++) {
  108. s->filter[0][i] = filt[i * 2];
  109. s->filter[1][i] = filt[i * 2 + 1];
  110. }
  111. return 0;
  112. }
  113. static void convolve(AVFilterContext *ctx, AVFrame *in,
  114. int input_ch, int output_ch,
  115. int filter_ch, int tap_ch)
  116. {
  117. EarwaxContext *s = ctx->priv;
  118. int16_t *taps, *endin, *dst, *src;
  119. int len;
  120. taps = s->taps[tap_ch];
  121. dst = (int16_t *)s->frame[input_ch]->data[output_ch];
  122. src = (int16_t *)in->data[input_ch];
  123. len = FFMIN(NUMTAPS, in->nb_samples);
  124. // copy part of new input and process with saved input
  125. memcpy(taps+NUMTAPS, src, len * sizeof(*taps));
  126. dst = scalarproduct(taps, taps + len, s->filter[filter_ch], dst);
  127. // process current input
  128. if (in->nb_samples >= NUMTAPS) {
  129. endin = src + in->nb_samples - NUMTAPS;
  130. scalarproduct(src, endin, s->filter[filter_ch], dst);
  131. // save part of input for next round
  132. memcpy(taps, endin, NUMTAPS * sizeof(*taps));
  133. } else {
  134. memmove(taps, taps + in->nb_samples, NUMTAPS * sizeof(*taps));
  135. }
  136. }
  137. static void mix(AVFilterContext *ctx, AVFrame *out,
  138. int output_ch, int f0, int f1, int i0, int i1)
  139. {
  140. EarwaxContext *s = ctx->priv;
  141. const int16_t *srcl = (const int16_t *)s->frame[f0]->data[i0];
  142. const int16_t *srcr = (const int16_t *)s->frame[f1]->data[i1];
  143. int16_t *dst = (int16_t *)out->data[output_ch];
  144. for (int n = 0; n < out->nb_samples; n++)
  145. dst[n] = av_clip_int16(srcl[n] + srcr[n]);
  146. }
  147. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  148. {
  149. AVFilterContext *ctx = inlink->dst;
  150. EarwaxContext *s = ctx->priv;
  151. AVFilterLink *outlink = ctx->outputs[0];
  152. AVFrame *out = ff_get_audio_buffer(outlink, in->nb_samples);
  153. for (int ch = 0; ch < 2; ch++) {
  154. if (!s->frame[ch] || s->frame[ch]->nb_samples < in->nb_samples) {
  155. av_frame_free(&s->frame[ch]);
  156. s->frame[ch] = ff_get_audio_buffer(outlink, in->nb_samples);
  157. if (!s->frame[ch]) {
  158. av_frame_free(&in);
  159. av_frame_free(&out);
  160. return AVERROR(ENOMEM);
  161. }
  162. }
  163. }
  164. if (!out) {
  165. av_frame_free(&in);
  166. return AVERROR(ENOMEM);
  167. }
  168. av_frame_copy_props(out, in);
  169. convolve(ctx, in, 0, 0, 0, 0);
  170. convolve(ctx, in, 0, 1, 1, 1);
  171. convolve(ctx, in, 1, 0, 0, 2);
  172. convolve(ctx, in, 1, 1, 1, 3);
  173. mix(ctx, out, 0, 0, 1, 1, 0);
  174. mix(ctx, out, 1, 0, 1, 0, 1);
  175. av_frame_free(&in);
  176. return ff_filter_frame(outlink, out);
  177. }
  178. static av_cold void uninit(AVFilterContext *ctx)
  179. {
  180. EarwaxContext *s = ctx->priv;
  181. av_frame_free(&s->frame[0]);
  182. av_frame_free(&s->frame[1]);
  183. }
  184. static const AVFilterPad earwax_inputs[] = {
  185. {
  186. .name = "default",
  187. .type = AVMEDIA_TYPE_AUDIO,
  188. .filter_frame = filter_frame,
  189. .config_props = config_input,
  190. },
  191. { NULL }
  192. };
  193. static const AVFilterPad earwax_outputs[] = {
  194. {
  195. .name = "default",
  196. .type = AVMEDIA_TYPE_AUDIO,
  197. },
  198. { NULL }
  199. };
  200. AVFilter ff_af_earwax = {
  201. .name = "earwax",
  202. .description = NULL_IF_CONFIG_SMALL("Widen the stereo image."),
  203. .query_formats = query_formats,
  204. .priv_size = sizeof(EarwaxContext),
  205. .uninit = uninit,
  206. .inputs = earwax_inputs,
  207. .outputs = earwax_outputs,
  208. };