af_aphaser.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 *p,
  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 *p = ctx->priv;
  65. if (p->in_gain > (1 - p->decay * p->decay))
  66. av_log(ctx, AV_LOG_WARNING, "in_gain may cause clipping\n");
  67. if (p->in_gain / (1 - p->decay) > 1 / p->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. layouts = ff_all_channel_layouts();
  83. if (!layouts)
  84. return AVERROR(ENOMEM);
  85. ff_set_common_channel_layouts(ctx, layouts);
  86. formats = ff_make_format_list(sample_fmts);
  87. if (!formats)
  88. return AVERROR(ENOMEM);
  89. ff_set_common_formats(ctx, formats);
  90. formats = ff_all_samplerates();
  91. if (!formats)
  92. return AVERROR(ENOMEM);
  93. ff_set_common_samplerates(ctx, formats);
  94. return 0;
  95. }
  96. #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
  97. #define PHASER_PLANAR(name, type) \
  98. static void phaser_## name ##p(AudioPhaserContext *p, \
  99. uint8_t * const *src, uint8_t **dst, \
  100. int nb_samples, int channels) \
  101. { \
  102. int i, c, delay_pos, modulation_pos; \
  103. \
  104. av_assert0(channels > 0); \
  105. for (c = 0; c < channels; c++) { \
  106. type *s = (type *)src[c]; \
  107. type *d = (type *)dst[c]; \
  108. double *buffer = p->delay_buffer + \
  109. c * p->delay_buffer_length; \
  110. \
  111. delay_pos = p->delay_pos; \
  112. modulation_pos = p->modulation_pos; \
  113. \
  114. for (i = 0; i < nb_samples; i++, s++, d++) { \
  115. double v = *s * p->in_gain + buffer[ \
  116. MOD(delay_pos + p->modulation_buffer[ \
  117. modulation_pos], \
  118. p->delay_buffer_length)] * p->decay; \
  119. \
  120. modulation_pos = MOD(modulation_pos + 1, \
  121. p->modulation_buffer_length); \
  122. delay_pos = MOD(delay_pos + 1, p->delay_buffer_length); \
  123. buffer[delay_pos] = v; \
  124. \
  125. *d = v * p->out_gain; \
  126. } \
  127. } \
  128. \
  129. p->delay_pos = delay_pos; \
  130. p->modulation_pos = modulation_pos; \
  131. }
  132. #define PHASER(name, type) \
  133. static void phaser_## name (AudioPhaserContext *p, \
  134. uint8_t * const *src, uint8_t **dst, \
  135. int nb_samples, int channels) \
  136. { \
  137. int i, c, delay_pos, modulation_pos; \
  138. type *s = (type *)src[0]; \
  139. type *d = (type *)dst[0]; \
  140. double *buffer = p->delay_buffer; \
  141. \
  142. delay_pos = p->delay_pos; \
  143. modulation_pos = p->modulation_pos; \
  144. \
  145. for (i = 0; i < nb_samples; i++) { \
  146. int pos = MOD(delay_pos + p->modulation_buffer[modulation_pos], \
  147. p->delay_buffer_length) * channels; \
  148. int npos; \
  149. \
  150. delay_pos = MOD(delay_pos + 1, p->delay_buffer_length); \
  151. npos = delay_pos * channels; \
  152. for (c = 0; c < channels; c++, s++, d++) { \
  153. double v = *s * p->in_gain + buffer[pos + c] * p->decay; \
  154. \
  155. buffer[npos + c] = v; \
  156. \
  157. *d = v * p->out_gain; \
  158. } \
  159. \
  160. modulation_pos = MOD(modulation_pos + 1, \
  161. p->modulation_buffer_length); \
  162. } \
  163. \
  164. p->delay_pos = delay_pos; \
  165. p->modulation_pos = modulation_pos; \
  166. }
  167. PHASER_PLANAR(dbl, double)
  168. PHASER_PLANAR(flt, float)
  169. PHASER_PLANAR(s16, int16_t)
  170. PHASER_PLANAR(s32, int32_t)
  171. PHASER(dbl, double)
  172. PHASER(flt, float)
  173. PHASER(s16, int16_t)
  174. PHASER(s32, int32_t)
  175. static int config_output(AVFilterLink *outlink)
  176. {
  177. AudioPhaserContext *p = outlink->src->priv;
  178. AVFilterLink *inlink = outlink->src->inputs[0];
  179. p->delay_buffer_length = p->delay * 0.001 * inlink->sample_rate + 0.5;
  180. p->delay_buffer = av_calloc(p->delay_buffer_length, sizeof(*p->delay_buffer) * inlink->channels);
  181. p->modulation_buffer_length = inlink->sample_rate / p->speed + 0.5;
  182. p->modulation_buffer = av_malloc_array(p->modulation_buffer_length, sizeof(*p->modulation_buffer));
  183. if (!p->modulation_buffer || !p->delay_buffer)
  184. return AVERROR(ENOMEM);
  185. ff_generate_wave_table(p->type, AV_SAMPLE_FMT_S32,
  186. p->modulation_buffer, p->modulation_buffer_length,
  187. 1., p->delay_buffer_length, M_PI / 2.0);
  188. p->delay_pos = p->modulation_pos = 0;
  189. switch (inlink->format) {
  190. case AV_SAMPLE_FMT_DBL: p->phaser = phaser_dbl; break;
  191. case AV_SAMPLE_FMT_DBLP: p->phaser = phaser_dblp; break;
  192. case AV_SAMPLE_FMT_FLT: p->phaser = phaser_flt; break;
  193. case AV_SAMPLE_FMT_FLTP: p->phaser = phaser_fltp; break;
  194. case AV_SAMPLE_FMT_S16: p->phaser = phaser_s16; break;
  195. case AV_SAMPLE_FMT_S16P: p->phaser = phaser_s16p; break;
  196. case AV_SAMPLE_FMT_S32: p->phaser = phaser_s32; break;
  197. case AV_SAMPLE_FMT_S32P: p->phaser = phaser_s32p; break;
  198. default: av_assert0(0);
  199. }
  200. return 0;
  201. }
  202. static int filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
  203. {
  204. AudioPhaserContext *p = inlink->dst->priv;
  205. AVFilterLink *outlink = inlink->dst->outputs[0];
  206. AVFrame *outbuf;
  207. if (av_frame_is_writable(inbuf)) {
  208. outbuf = inbuf;
  209. } else {
  210. outbuf = ff_get_audio_buffer(inlink, inbuf->nb_samples);
  211. if (!outbuf)
  212. return AVERROR(ENOMEM);
  213. av_frame_copy_props(outbuf, inbuf);
  214. }
  215. p->phaser(p, inbuf->extended_data, outbuf->extended_data,
  216. outbuf->nb_samples, av_frame_get_channels(outbuf));
  217. if (inbuf != outbuf)
  218. av_frame_free(&inbuf);
  219. return ff_filter_frame(outlink, outbuf);
  220. }
  221. static av_cold void uninit(AVFilterContext *ctx)
  222. {
  223. AudioPhaserContext *p = ctx->priv;
  224. av_freep(&p->delay_buffer);
  225. av_freep(&p->modulation_buffer);
  226. }
  227. static const AVFilterPad aphaser_inputs[] = {
  228. {
  229. .name = "default",
  230. .type = AVMEDIA_TYPE_AUDIO,
  231. .filter_frame = filter_frame,
  232. },
  233. { NULL }
  234. };
  235. static const AVFilterPad aphaser_outputs[] = {
  236. {
  237. .name = "default",
  238. .type = AVMEDIA_TYPE_AUDIO,
  239. .config_props = config_output,
  240. },
  241. { NULL }
  242. };
  243. AVFilter ff_af_aphaser = {
  244. .name = "aphaser",
  245. .description = NULL_IF_CONFIG_SMALL("Add a phasing effect to the audio."),
  246. .query_formats = query_formats,
  247. .priv_size = sizeof(AudioPhaserContext),
  248. .init = init,
  249. .uninit = uninit,
  250. .inputs = aphaser_inputs,
  251. .outputs = aphaser_outputs,
  252. .priv_class = &aphaser_class,
  253. };