af_aphaser.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (c) 2013 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * phaser audio filter
  23. */
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/opt.h"
  26. #include "audio.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. #include "generate_wave_table.h"
  30. typedef struct AudioPhaserContext {
  31. const AVClass *class;
  32. double in_gain, out_gain;
  33. double delay;
  34. double decay;
  35. double speed;
  36. int type;
  37. int delay_buffer_length;
  38. double *delay_buffer;
  39. int modulation_buffer_length;
  40. int32_t *modulation_buffer;
  41. int delay_pos, modulation_pos;
  42. void (*phaser)(struct AudioPhaserContext *s,
  43. uint8_t * const *src, uint8_t **dst,
  44. int nb_samples, int channels);
  45. } AudioPhaserContext;
  46. #define OFFSET(x) offsetof(AudioPhaserContext, x)
  47. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  48. static const AVOption aphaser_options[] = {
  49. { "in_gain", "set input gain", OFFSET(in_gain), AV_OPT_TYPE_DOUBLE, {.dbl=.4}, 0, 1, FLAGS },
  50. { "out_gain", "set output gain", OFFSET(out_gain), AV_OPT_TYPE_DOUBLE, {.dbl=.74}, 0, 1e9, FLAGS },
  51. { "delay", "set delay in milliseconds", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=3.}, 0, 5, FLAGS },
  52. { "decay", "set decay", OFFSET(decay), AV_OPT_TYPE_DOUBLE, {.dbl=.4}, 0, .99, FLAGS },
  53. { "speed", "set modulation speed", OFFSET(speed), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, .1, 2, FLAGS },
  54. { "type", "set modulation type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=WAVE_TRI}, 0, WAVE_NB-1, FLAGS, "type" },
  55. { "triangular", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, FLAGS, "type" },
  56. { "t", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, FLAGS, "type" },
  57. { "sinusoidal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, FLAGS, "type" },
  58. { "s", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, FLAGS, "type" },
  59. { NULL }
  60. };
  61. AVFILTER_DEFINE_CLASS(aphaser);
  62. static av_cold int init(AVFilterContext *ctx)
  63. {
  64. AudioPhaserContext *s = ctx->priv;
  65. if (s->in_gain > (1 - s->decay * s->decay))
  66. av_log(ctx, AV_LOG_WARNING, "in_gain may cause clipping\n");
  67. if (s->in_gain / (1 - s->decay) > 1 / s->out_gain)
  68. av_log(ctx, AV_LOG_WARNING, "out_gain may cause clipping\n");
  69. return 0;
  70. }
  71. static int query_formats(AVFilterContext *ctx)
  72. {
  73. AVFilterFormats *formats;
  74. AVFilterChannelLayouts *layouts;
  75. static const enum AVSampleFormat sample_fmts[] = {
  76. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
  77. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
  78. AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
  79. AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
  80. AV_SAMPLE_FMT_NONE
  81. };
  82. int ret;
  83. layouts = ff_all_channel_counts();
  84. if (!layouts)
  85. return AVERROR(ENOMEM);
  86. ret = ff_set_common_channel_layouts(ctx, layouts);
  87. if (ret < 0)
  88. return ret;
  89. formats = ff_make_format_list(sample_fmts);
  90. if (!formats)
  91. return AVERROR(ENOMEM);
  92. ret = ff_set_common_formats(ctx, formats);
  93. if (ret < 0)
  94. return ret;
  95. formats = ff_all_samplerates();
  96. if (!formats)
  97. return AVERROR(ENOMEM);
  98. return ff_set_common_samplerates(ctx, formats);
  99. }
  100. #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
  101. #define PHASER_PLANAR(name, type) \
  102. static void phaser_## name ##p(AudioPhaserContext *s, \
  103. uint8_t * const *ssrc, uint8_t **ddst, \
  104. int nb_samples, int channels) \
  105. { \
  106. int i, c, delay_pos, modulation_pos; \
  107. \
  108. av_assert0(channels > 0); \
  109. for (c = 0; c < channels; c++) { \
  110. type *src = (type *)ssrc[c]; \
  111. type *dst = (type *)ddst[c]; \
  112. double *buffer = s->delay_buffer + \
  113. c * s->delay_buffer_length; \
  114. \
  115. delay_pos = s->delay_pos; \
  116. modulation_pos = s->modulation_pos; \
  117. \
  118. for (i = 0; i < nb_samples; i++, src++, dst++) { \
  119. double v = *src * s->in_gain + buffer[ \
  120. MOD(delay_pos + s->modulation_buffer[ \
  121. modulation_pos], \
  122. s->delay_buffer_length)] * s->decay; \
  123. \
  124. modulation_pos = MOD(modulation_pos + 1, \
  125. s->modulation_buffer_length); \
  126. delay_pos = MOD(delay_pos + 1, s->delay_buffer_length); \
  127. buffer[delay_pos] = v; \
  128. \
  129. *dst = v * s->out_gain; \
  130. } \
  131. } \
  132. \
  133. s->delay_pos = delay_pos; \
  134. s->modulation_pos = modulation_pos; \
  135. }
  136. #define PHASER(name, type) \
  137. static void phaser_## name (AudioPhaserContext *s, \
  138. uint8_t * const *ssrc, uint8_t **ddst, \
  139. int nb_samples, int channels) \
  140. { \
  141. int i, c, delay_pos, modulation_pos; \
  142. type *src = (type *)ssrc[0]; \
  143. type *dst = (type *)ddst[0]; \
  144. double *buffer = s->delay_buffer; \
  145. \
  146. delay_pos = s->delay_pos; \
  147. modulation_pos = s->modulation_pos; \
  148. \
  149. for (i = 0; i < nb_samples; i++) { \
  150. int pos = MOD(delay_pos + s->modulation_buffer[modulation_pos], \
  151. s->delay_buffer_length) * channels; \
  152. int npos; \
  153. \
  154. delay_pos = MOD(delay_pos + 1, s->delay_buffer_length); \
  155. npos = delay_pos * channels; \
  156. for (c = 0; c < channels; c++, src++, dst++) { \
  157. double v = *src * s->in_gain + buffer[pos + c] * s->decay; \
  158. \
  159. buffer[npos + c] = v; \
  160. \
  161. *dst = v * s->out_gain; \
  162. } \
  163. \
  164. modulation_pos = MOD(modulation_pos + 1, \
  165. s->modulation_buffer_length); \
  166. } \
  167. \
  168. s->delay_pos = delay_pos; \
  169. s->modulation_pos = modulation_pos; \
  170. }
  171. PHASER_PLANAR(dbl, double)
  172. PHASER_PLANAR(flt, float)
  173. PHASER_PLANAR(s16, int16_t)
  174. PHASER_PLANAR(s32, int32_t)
  175. PHASER(dbl, double)
  176. PHASER(flt, float)
  177. PHASER(s16, int16_t)
  178. PHASER(s32, int32_t)
  179. static int config_output(AVFilterLink *outlink)
  180. {
  181. AudioPhaserContext *s = outlink->src->priv;
  182. AVFilterLink *inlink = outlink->src->inputs[0];
  183. s->delay_buffer_length = s->delay * 0.001 * inlink->sample_rate + 0.5;
  184. if (s->delay_buffer_length <= 0) {
  185. av_log(outlink->src, AV_LOG_ERROR, "delay is too small\n");
  186. return AVERROR(EINVAL);
  187. }
  188. s->delay_buffer = av_calloc(s->delay_buffer_length, sizeof(*s->delay_buffer) * inlink->channels);
  189. s->modulation_buffer_length = inlink->sample_rate / s->speed + 0.5;
  190. s->modulation_buffer = av_malloc_array(s->modulation_buffer_length, sizeof(*s->modulation_buffer));
  191. if (!s->modulation_buffer || !s->delay_buffer)
  192. return AVERROR(ENOMEM);
  193. ff_generate_wave_table(s->type, AV_SAMPLE_FMT_S32,
  194. s->modulation_buffer, s->modulation_buffer_length,
  195. 1., s->delay_buffer_length, M_PI / 2.0);
  196. s->delay_pos = s->modulation_pos = 0;
  197. switch (inlink->format) {
  198. case AV_SAMPLE_FMT_DBL: s->phaser = phaser_dbl; break;
  199. case AV_SAMPLE_FMT_DBLP: s->phaser = phaser_dblp; break;
  200. case AV_SAMPLE_FMT_FLT: s->phaser = phaser_flt; break;
  201. case AV_SAMPLE_FMT_FLTP: s->phaser = phaser_fltp; break;
  202. case AV_SAMPLE_FMT_S16: s->phaser = phaser_s16; break;
  203. case AV_SAMPLE_FMT_S16P: s->phaser = phaser_s16p; break;
  204. case AV_SAMPLE_FMT_S32: s->phaser = phaser_s32; break;
  205. case AV_SAMPLE_FMT_S32P: s->phaser = phaser_s32p; break;
  206. default: av_assert0(0);
  207. }
  208. return 0;
  209. }
  210. static int filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
  211. {
  212. AudioPhaserContext *s = inlink->dst->priv;
  213. AVFilterLink *outlink = inlink->dst->outputs[0];
  214. AVFrame *outbuf;
  215. if (av_frame_is_writable(inbuf)) {
  216. outbuf = inbuf;
  217. } else {
  218. outbuf = ff_get_audio_buffer(inlink, inbuf->nb_samples);
  219. if (!outbuf)
  220. return AVERROR(ENOMEM);
  221. av_frame_copy_props(outbuf, inbuf);
  222. }
  223. s->phaser(s, inbuf->extended_data, outbuf->extended_data,
  224. outbuf->nb_samples, av_frame_get_channels(outbuf));
  225. if (inbuf != outbuf)
  226. av_frame_free(&inbuf);
  227. return ff_filter_frame(outlink, outbuf);
  228. }
  229. static av_cold void uninit(AVFilterContext *ctx)
  230. {
  231. AudioPhaserContext *s = ctx->priv;
  232. av_freep(&s->delay_buffer);
  233. av_freep(&s->modulation_buffer);
  234. }
  235. static const AVFilterPad aphaser_inputs[] = {
  236. {
  237. .name = "default",
  238. .type = AVMEDIA_TYPE_AUDIO,
  239. .filter_frame = filter_frame,
  240. },
  241. { NULL }
  242. };
  243. static const AVFilterPad aphaser_outputs[] = {
  244. {
  245. .name = "default",
  246. .type = AVMEDIA_TYPE_AUDIO,
  247. .config_props = config_output,
  248. },
  249. { NULL }
  250. };
  251. AVFilter ff_af_aphaser = {
  252. .name = "aphaser",
  253. .description = NULL_IF_CONFIG_SMALL("Add a phasing effect to the audio."),
  254. .query_formats = query_formats,
  255. .priv_size = sizeof(AudioPhaserContext),
  256. .init = init,
  257. .uninit = uninit,
  258. .inputs = aphaser_inputs,
  259. .outputs = aphaser_outputs,
  260. .priv_class = &aphaser_class,
  261. };