af_aphaser.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. enum WaveType {
  30. WAVE_SIN,
  31. WAVE_TRI,
  32. WAVE_NB,
  33. };
  34. typedef struct AudioPhaserContext {
  35. const AVClass *class;
  36. double in_gain, out_gain;
  37. double delay;
  38. double decay;
  39. double speed;
  40. enum WaveType type;
  41. int delay_buffer_length;
  42. double *delay_buffer;
  43. int modulation_buffer_length;
  44. int32_t *modulation_buffer;
  45. int delay_pos, modulation_pos;
  46. void (*phaser)(struct AudioPhaserContext *p,
  47. uint8_t * const *src, uint8_t **dst,
  48. int nb_samples, int channels);
  49. } AudioPhaserContext;
  50. #define OFFSET(x) offsetof(AudioPhaserContext, x)
  51. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  52. static const AVOption aphaser_options[] = {
  53. { "in_gain", "set input gain", OFFSET(in_gain), AV_OPT_TYPE_DOUBLE, {.dbl=.4}, 0, 1, FLAGS },
  54. { "out_gain", "set output gain", OFFSET(out_gain), AV_OPT_TYPE_DOUBLE, {.dbl=.74}, 0, 1e9, FLAGS },
  55. { "delay", "set delay in milliseconds", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=3.}, 0, 5, FLAGS },
  56. { "decay", "set decay", OFFSET(decay), AV_OPT_TYPE_DOUBLE, {.dbl=.4}, 0, .99, FLAGS },
  57. { "speed", "set modulation speed", OFFSET(speed), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, .1, 2, FLAGS },
  58. { "type", "set modulation type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=WAVE_TRI}, 0, WAVE_NB-1, FLAGS, "type" },
  59. { "triangular", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, FLAGS, "type" },
  60. { "t", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, FLAGS, "type" },
  61. { "sinusoidal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, FLAGS, "type" },
  62. { "s", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, FLAGS, "type" },
  63. { NULL }
  64. };
  65. AVFILTER_DEFINE_CLASS(aphaser);
  66. static av_cold int init(AVFilterContext *ctx)
  67. {
  68. AudioPhaserContext *p = ctx->priv;
  69. if (p->in_gain > (1 - p->decay * p->decay))
  70. av_log(ctx, AV_LOG_WARNING, "in_gain may cause clipping\n");
  71. if (p->in_gain / (1 - p->decay) > 1 / p->out_gain)
  72. av_log(ctx, AV_LOG_WARNING, "out_gain may cause clipping\n");
  73. return 0;
  74. }
  75. static int query_formats(AVFilterContext *ctx)
  76. {
  77. AVFilterFormats *formats;
  78. AVFilterChannelLayouts *layouts;
  79. static const enum AVSampleFormat sample_fmts[] = {
  80. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
  81. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
  82. AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
  83. AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
  84. AV_SAMPLE_FMT_NONE
  85. };
  86. layouts = ff_all_channel_layouts();
  87. if (!layouts)
  88. return AVERROR(ENOMEM);
  89. ff_set_common_channel_layouts(ctx, layouts);
  90. formats = ff_make_format_list(sample_fmts);
  91. if (!formats)
  92. return AVERROR(ENOMEM);
  93. ff_set_common_formats(ctx, formats);
  94. formats = ff_all_samplerates();
  95. if (!formats)
  96. return AVERROR(ENOMEM);
  97. ff_set_common_samplerates(ctx, formats);
  98. return 0;
  99. }
  100. static void generate_wave_table(enum WaveType wave_type, enum AVSampleFormat sample_fmt,
  101. void *table, int table_size,
  102. double min, double max, double phase)
  103. {
  104. uint32_t i, phase_offset = phase / M_PI / 2 * table_size + 0.5;
  105. for (i = 0; i < table_size; i++) {
  106. uint32_t point = (i + phase_offset) % table_size;
  107. double d;
  108. switch (wave_type) {
  109. case WAVE_SIN:
  110. d = (sin((double)point / table_size * 2 * M_PI) + 1) / 2;
  111. break;
  112. case WAVE_TRI:
  113. d = (double)point * 2 / table_size;
  114. switch (4 * point / table_size) {
  115. case 0: d = d + 0.5; break;
  116. case 1:
  117. case 2: d = 1.5 - d; break;
  118. case 3: d = d - 1.5; break;
  119. }
  120. break;
  121. default:
  122. av_assert0(0);
  123. }
  124. d = d * (max - min) + min;
  125. switch (sample_fmt) {
  126. case AV_SAMPLE_FMT_FLT: {
  127. float *fp = (float *)table;
  128. *fp++ = (float)d;
  129. table = fp;
  130. continue; }
  131. case AV_SAMPLE_FMT_DBL: {
  132. double *dp = (double *)table;
  133. *dp++ = d;
  134. table = dp;
  135. continue; }
  136. }
  137. d += d < 0 ? -0.5 : 0.5;
  138. switch (sample_fmt) {
  139. case AV_SAMPLE_FMT_S16: {
  140. int16_t *sp = table;
  141. *sp++ = (int16_t)d;
  142. table = sp;
  143. continue; }
  144. case AV_SAMPLE_FMT_S32: {
  145. int32_t *ip = table;
  146. *ip++ = (int32_t)d;
  147. table = ip;
  148. continue; }
  149. default:
  150. av_assert0(0);
  151. }
  152. }
  153. }
  154. #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
  155. #define PHASER_PLANAR(name, type) \
  156. static void phaser_## name ##p(AudioPhaserContext *p, \
  157. uint8_t * const *src, uint8_t **dst, \
  158. int nb_samples, int channels) \
  159. { \
  160. int i, c, delay_pos, modulation_pos; \
  161. \
  162. av_assert0(channels > 0); \
  163. for (c = 0; c < channels; c++) { \
  164. type *s = (type *)src[c]; \
  165. type *d = (type *)dst[c]; \
  166. double *buffer = p->delay_buffer + \
  167. c * p->delay_buffer_length; \
  168. \
  169. delay_pos = p->delay_pos; \
  170. modulation_pos = p->modulation_pos; \
  171. \
  172. for (i = 0; i < nb_samples; i++, s++, d++) { \
  173. double v = *s * p->in_gain + buffer[ \
  174. MOD(delay_pos + p->modulation_buffer[ \
  175. modulation_pos], \
  176. p->delay_buffer_length)] * p->decay; \
  177. \
  178. modulation_pos = MOD(modulation_pos + 1, \
  179. p->modulation_buffer_length); \
  180. delay_pos = MOD(delay_pos + 1, p->delay_buffer_length); \
  181. buffer[delay_pos] = v; \
  182. \
  183. *d = v * p->out_gain; \
  184. } \
  185. } \
  186. \
  187. p->delay_pos = delay_pos; \
  188. p->modulation_pos = modulation_pos; \
  189. }
  190. #define PHASER(name, type) \
  191. static void phaser_## name (AudioPhaserContext *p, \
  192. uint8_t * const *src, uint8_t **dst, \
  193. int nb_samples, int channels) \
  194. { \
  195. int i, c, delay_pos, modulation_pos; \
  196. type *s = (type *)src[0]; \
  197. type *d = (type *)dst[0]; \
  198. double *buffer = p->delay_buffer; \
  199. \
  200. delay_pos = p->delay_pos; \
  201. modulation_pos = p->modulation_pos; \
  202. \
  203. for (i = 0; i < nb_samples; i++) { \
  204. int pos = MOD(delay_pos + p->modulation_buffer[modulation_pos], \
  205. p->delay_buffer_length) * channels; \
  206. int npos; \
  207. \
  208. delay_pos = MOD(delay_pos + 1, p->delay_buffer_length); \
  209. npos = delay_pos * channels; \
  210. for (c = 0; c < channels; c++, s++, d++) { \
  211. double v = *s * p->in_gain + buffer[pos + c] * p->decay; \
  212. \
  213. buffer[npos + c] = v; \
  214. \
  215. *d = v * p->out_gain; \
  216. } \
  217. \
  218. modulation_pos = MOD(modulation_pos + 1, \
  219. p->modulation_buffer_length); \
  220. } \
  221. \
  222. p->delay_pos = delay_pos; \
  223. p->modulation_pos = modulation_pos; \
  224. }
  225. PHASER_PLANAR(dbl, double)
  226. PHASER_PLANAR(flt, float)
  227. PHASER_PLANAR(s16, int16_t)
  228. PHASER_PLANAR(s32, int32_t)
  229. PHASER(dbl, double)
  230. PHASER(flt, float)
  231. PHASER(s16, int16_t)
  232. PHASER(s32, int32_t)
  233. static int config_output(AVFilterLink *outlink)
  234. {
  235. AudioPhaserContext *p = outlink->src->priv;
  236. AVFilterLink *inlink = outlink->src->inputs[0];
  237. p->delay_buffer_length = p->delay * 0.001 * inlink->sample_rate + 0.5;
  238. p->delay_buffer = av_calloc(p->delay_buffer_length, sizeof(*p->delay_buffer) * inlink->channels);
  239. p->modulation_buffer_length = inlink->sample_rate / p->speed + 0.5;
  240. p->modulation_buffer = av_malloc(p->modulation_buffer_length * sizeof(*p->modulation_buffer));
  241. if (!p->modulation_buffer || !p->delay_buffer)
  242. return AVERROR(ENOMEM);
  243. generate_wave_table(p->type, AV_SAMPLE_FMT_S32,
  244. p->modulation_buffer, p->modulation_buffer_length,
  245. 1., p->delay_buffer_length, M_PI / 2.0);
  246. p->delay_pos = p->modulation_pos = 0;
  247. switch (inlink->format) {
  248. case AV_SAMPLE_FMT_DBL: p->phaser = phaser_dbl; break;
  249. case AV_SAMPLE_FMT_DBLP: p->phaser = phaser_dblp; break;
  250. case AV_SAMPLE_FMT_FLT: p->phaser = phaser_flt; break;
  251. case AV_SAMPLE_FMT_FLTP: p->phaser = phaser_fltp; break;
  252. case AV_SAMPLE_FMT_S16: p->phaser = phaser_s16; break;
  253. case AV_SAMPLE_FMT_S16P: p->phaser = phaser_s16p; break;
  254. case AV_SAMPLE_FMT_S32: p->phaser = phaser_s32; break;
  255. case AV_SAMPLE_FMT_S32P: p->phaser = phaser_s32p; break;
  256. default: av_assert0(0);
  257. }
  258. return 0;
  259. }
  260. static int filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
  261. {
  262. AudioPhaserContext *p = inlink->dst->priv;
  263. AVFilterLink *outlink = inlink->dst->outputs[0];
  264. AVFrame *outbuf;
  265. if (av_frame_is_writable(inbuf)) {
  266. outbuf = inbuf;
  267. } else {
  268. outbuf = ff_get_audio_buffer(inlink, inbuf->nb_samples);
  269. if (!outbuf)
  270. return AVERROR(ENOMEM);
  271. av_frame_copy_props(outbuf, inbuf);
  272. }
  273. p->phaser(p, inbuf->extended_data, outbuf->extended_data,
  274. outbuf->nb_samples, av_frame_get_channels(outbuf));
  275. if (inbuf != outbuf)
  276. av_frame_free(&inbuf);
  277. return ff_filter_frame(outlink, outbuf);
  278. }
  279. static av_cold void uninit(AVFilterContext *ctx)
  280. {
  281. AudioPhaserContext *p = ctx->priv;
  282. av_freep(&p->delay_buffer);
  283. av_freep(&p->modulation_buffer);
  284. }
  285. static const AVFilterPad aphaser_inputs[] = {
  286. {
  287. .name = "default",
  288. .type = AVMEDIA_TYPE_AUDIO,
  289. .filter_frame = filter_frame,
  290. },
  291. { NULL }
  292. };
  293. static const AVFilterPad aphaser_outputs[] = {
  294. {
  295. .name = "default",
  296. .type = AVMEDIA_TYPE_AUDIO,
  297. .config_props = config_output,
  298. },
  299. { NULL }
  300. };
  301. AVFilter ff_af_aphaser = {
  302. .name = "aphaser",
  303. .description = NULL_IF_CONFIG_SMALL("Add a phasing effect to the audio."),
  304. .query_formats = query_formats,
  305. .priv_size = sizeof(AudioPhaserContext),
  306. .init = init,
  307. .uninit = uninit,
  308. .inputs = aphaser_inputs,
  309. .outputs = aphaser_outputs,
  310. .priv_class = &aphaser_class,
  311. };