af_afftfilt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Copyright (c) 2016 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU Lesser General Public License as published
  8. * by the Free Software Foundation; either version 2.1 of the License,
  9. * 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. #include "libavutil/audio_fifo.h"
  21. #include "libavutil/avstring.h"
  22. #include "libavfilter/internal.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/opt.h"
  25. #include "libavcodec/avfft.h"
  26. #include "libavutil/eval.h"
  27. #include "audio.h"
  28. #include "window_func.h"
  29. typedef struct AFFTFiltContext {
  30. const AVClass *class;
  31. char *real_str;
  32. char *img_str;
  33. int fft_bits;
  34. FFTContext *fft, *ifft;
  35. FFTComplex **fft_data;
  36. int nb_exprs;
  37. int window_size;
  38. AVExpr **real;
  39. AVExpr **imag;
  40. AVAudioFifo *fifo;
  41. int64_t pts;
  42. int hop_size;
  43. float overlap;
  44. AVFrame *buffer;
  45. int start, end;
  46. int win_func;
  47. float win_scale;
  48. float *window_func_lut;
  49. } AFFTFiltContext;
  50. static const char *const var_names[] = { "sr", "b", "nb", "ch", "chs", "pts", NULL };
  51. enum { VAR_SAMPLE_RATE, VAR_BIN, VAR_NBBINS, VAR_CHANNEL, VAR_CHANNELS, VAR_PTS, VAR_VARS_NB };
  52. #define OFFSET(x) offsetof(AFFTFiltContext, x)
  53. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  54. static const AVOption afftfilt_options[] = {
  55. { "real", "set channels real expressions", OFFSET(real_str), AV_OPT_TYPE_STRING, {.str = "1" }, 0, 0, A },
  56. { "imag", "set channels imaginary expressions", OFFSET(img_str), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, A },
  57. { "win_size", "set window size", OFFSET(fft_bits), AV_OPT_TYPE_INT, {.i64=12}, 4, 17, A, "fft" },
  58. { "w16", 0, 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, A, "fft" },
  59. { "w32", 0, 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, A, "fft" },
  60. { "w64", 0, 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, A, "fft" },
  61. { "w128", 0, 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, A, "fft" },
  62. { "w256", 0, 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, A, "fft" },
  63. { "w512", 0, 0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, A, "fft" },
  64. { "w1024", 0, 0, AV_OPT_TYPE_CONST, {.i64=10}, 0, 0, A, "fft" },
  65. { "w2048", 0, 0, AV_OPT_TYPE_CONST, {.i64=11}, 0, 0, A, "fft" },
  66. { "w4096", 0, 0, AV_OPT_TYPE_CONST, {.i64=12}, 0, 0, A, "fft" },
  67. { "w8192", 0, 0, AV_OPT_TYPE_CONST, {.i64=13}, 0, 0, A, "fft" },
  68. { "w16384", 0, 0, AV_OPT_TYPE_CONST, {.i64=14}, 0, 0, A, "fft" },
  69. { "w32768", 0, 0, AV_OPT_TYPE_CONST, {.i64=15}, 0, 0, A, "fft" },
  70. { "w65536", 0, 0, AV_OPT_TYPE_CONST, {.i64=16}, 0, 0, A, "fft" },
  71. { "w131072",0, 0, AV_OPT_TYPE_CONST, {.i64=17}, 0, 0, A, "fft" },
  72. { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, A, "win_func" },
  73. { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, A, "win_func" },
  74. { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, A, "win_func" },
  75. { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, A, "win_func" },
  76. { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, A, "win_func" },
  77. { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, A, "win_func" },
  78. { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, A, "win_func" },
  79. { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, A },
  80. { NULL },
  81. };
  82. AVFILTER_DEFINE_CLASS(afftfilt);
  83. static int config_input(AVFilterLink *inlink)
  84. {
  85. AVFilterContext *ctx = inlink->dst;
  86. AFFTFiltContext *s = ctx->priv;
  87. char *saveptr = NULL;
  88. int ret = 0, ch, i;
  89. float overlap;
  90. char *args;
  91. const char *last_expr = "1";
  92. s->fft = av_fft_init(s->fft_bits, 0);
  93. s->ifft = av_fft_init(s->fft_bits, 1);
  94. if (!s->fft || !s->ifft)
  95. return AVERROR(ENOMEM);
  96. s->window_size = 1 << s->fft_bits;
  97. s->fft_data = av_calloc(inlink->channels, sizeof(*s->fft_data));
  98. if (!s->fft_data)
  99. return AVERROR(ENOMEM);
  100. for (ch = 0; ch < inlink->channels; ch++) {
  101. s->fft_data[ch] = av_calloc(s->window_size, sizeof(**s->fft_data));
  102. if (!s->fft_data[ch])
  103. return AVERROR(ENOMEM);
  104. }
  105. s->real = av_calloc(inlink->channels, sizeof(*s->real));
  106. if (!s->real)
  107. return AVERROR(ENOMEM);
  108. s->imag = av_calloc(inlink->channels, sizeof(*s->imag));
  109. if (!s->imag)
  110. return AVERROR(ENOMEM);
  111. args = av_strdup(s->real_str);
  112. if (!args)
  113. return AVERROR(ENOMEM);
  114. for (ch = 0; ch < inlink->channels; ch++) {
  115. char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr);
  116. ret = av_expr_parse(&s->real[ch], arg ? arg : last_expr, var_names,
  117. NULL, NULL, NULL, NULL, 0, ctx);
  118. if (ret < 0)
  119. break;
  120. if (arg)
  121. last_expr = arg;
  122. s->nb_exprs++;
  123. }
  124. av_free(args);
  125. args = av_strdup(s->img_str ? s->img_str : s->real_str);
  126. if (!args)
  127. return AVERROR(ENOMEM);
  128. for (ch = 0; ch < inlink->channels; ch++) {
  129. char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr);
  130. ret = av_expr_parse(&s->imag[ch], arg ? arg : last_expr, var_names,
  131. NULL, NULL, NULL, NULL, 0, ctx);
  132. if (ret < 0)
  133. break;
  134. if (arg)
  135. last_expr = arg;
  136. }
  137. av_free(args);
  138. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->window_size);
  139. if (!s->fifo)
  140. return AVERROR(ENOMEM);
  141. s->window_func_lut = av_realloc_f(s->window_func_lut, s->window_size,
  142. sizeof(*s->window_func_lut));
  143. if (!s->window_func_lut)
  144. return AVERROR(ENOMEM);
  145. ff_generate_window_func(s->window_func_lut, s->window_size, s->win_func, &overlap);
  146. if (s->overlap == 1)
  147. s->overlap = overlap;
  148. for (s->win_scale = 0, i = 0; i < s->window_size; i++) {
  149. s->win_scale += s->window_func_lut[i] * s->window_func_lut[i];
  150. }
  151. s->hop_size = s->window_size * (1 - s->overlap);
  152. if (s->hop_size <= 0)
  153. return AVERROR(EINVAL);
  154. s->buffer = ff_get_audio_buffer(inlink, s->window_size * 2);
  155. if (!s->buffer)
  156. return AVERROR(ENOMEM);
  157. return ret;
  158. }
  159. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  160. {
  161. AVFilterContext *ctx = inlink->dst;
  162. AVFilterLink *outlink = ctx->outputs[0];
  163. AFFTFiltContext *s = ctx->priv;
  164. const int window_size = s->window_size;
  165. const float f = 1. / s->win_scale;
  166. double values[VAR_VARS_NB];
  167. AVFrame *out, *in = NULL;
  168. int ch, n, ret, i, j, k;
  169. int start = s->start, end = s->end;
  170. av_audio_fifo_write(s->fifo, (void **)frame->extended_data, frame->nb_samples);
  171. av_frame_free(&frame);
  172. while (av_audio_fifo_size(s->fifo) >= window_size) {
  173. if (!in) {
  174. in = ff_get_audio_buffer(outlink, window_size);
  175. if (!in)
  176. return AVERROR(ENOMEM);
  177. }
  178. ret = av_audio_fifo_peek(s->fifo, (void **)in->extended_data, window_size);
  179. if (ret < 0)
  180. break;
  181. for (ch = 0; ch < inlink->channels; ch++) {
  182. const float *src = (float *)in->extended_data[ch];
  183. FFTComplex *fft_data = s->fft_data[ch];
  184. for (n = 0; n < in->nb_samples; n++) {
  185. fft_data[n].re = src[n] * s->window_func_lut[n];
  186. fft_data[n].im = 0;
  187. }
  188. for (; n < window_size; n++) {
  189. fft_data[n].re = 0;
  190. fft_data[n].im = 0;
  191. }
  192. }
  193. values[VAR_PTS] = s->pts;
  194. values[VAR_SAMPLE_RATE] = inlink->sample_rate;
  195. values[VAR_NBBINS] = window_size / 2;
  196. values[VAR_CHANNELS] = inlink->channels;
  197. for (ch = 0; ch < inlink->channels; ch++) {
  198. FFTComplex *fft_data = s->fft_data[ch];
  199. float *buf = (float *)s->buffer->extended_data[ch];
  200. int x;
  201. values[VAR_CHANNEL] = ch;
  202. av_fft_permute(s->fft, fft_data);
  203. av_fft_calc(s->fft, fft_data);
  204. for (n = 0; n < window_size / 2; n++) {
  205. float fr, fi;
  206. values[VAR_BIN] = n;
  207. fr = av_expr_eval(s->real[ch], values, s);
  208. fi = av_expr_eval(s->imag[ch], values, s);
  209. fft_data[n].re *= fr;
  210. fft_data[n].im *= fi;
  211. }
  212. for (n = window_size / 2 + 1, x = window_size / 2 - 1; n < window_size; n++, x--) {
  213. fft_data[n].re = fft_data[x].re;
  214. fft_data[n].im = -fft_data[x].im;
  215. }
  216. av_fft_permute(s->ifft, fft_data);
  217. av_fft_calc(s->ifft, fft_data);
  218. start = s->start;
  219. end = s->end;
  220. k = end;
  221. for (i = 0, j = start; j < k && i < window_size; i++, j++) {
  222. buf[j] += s->fft_data[ch][i].re * f;
  223. }
  224. for (; i < window_size; i++, j++) {
  225. buf[j] = s->fft_data[ch][i].re * f;
  226. }
  227. start += s->hop_size;
  228. end = j;
  229. }
  230. s->start = start;
  231. s->end = end;
  232. if (start >= window_size) {
  233. float *dst, *buf;
  234. start -= window_size;
  235. end -= window_size;
  236. s->start = start;
  237. s->end = end;
  238. out = ff_get_audio_buffer(outlink, window_size);
  239. if (!out) {
  240. ret = AVERROR(ENOMEM);
  241. break;
  242. }
  243. out->pts = s->pts;
  244. s->pts += window_size;
  245. for (ch = 0; ch < inlink->channels; ch++) {
  246. dst = (float *)out->extended_data[ch];
  247. buf = (float *)s->buffer->extended_data[ch];
  248. for (n = 0; n < window_size; n++) {
  249. dst[n] = buf[n] * (1 - s->overlap);
  250. }
  251. memmove(buf, buf + window_size, window_size * 4);
  252. }
  253. ret = ff_filter_frame(outlink, out);
  254. if (ret < 0)
  255. break;
  256. }
  257. av_audio_fifo_drain(s->fifo, s->hop_size);
  258. }
  259. av_frame_free(&in);
  260. return ret;
  261. }
  262. static int query_formats(AVFilterContext *ctx)
  263. {
  264. AVFilterFormats *formats;
  265. AVFilterChannelLayouts *layouts;
  266. static const enum AVSampleFormat sample_fmts[] = {
  267. AV_SAMPLE_FMT_FLTP,
  268. AV_SAMPLE_FMT_NONE
  269. };
  270. int ret;
  271. layouts = ff_all_channel_counts();
  272. if (!layouts)
  273. return AVERROR(ENOMEM);
  274. ret = ff_set_common_channel_layouts(ctx, layouts);
  275. if (ret < 0)
  276. return ret;
  277. formats = ff_make_format_list(sample_fmts);
  278. if (!formats)
  279. return AVERROR(ENOMEM);
  280. ret = ff_set_common_formats(ctx, formats);
  281. if (ret < 0)
  282. return ret;
  283. formats = ff_all_samplerates();
  284. if (!formats)
  285. return AVERROR(ENOMEM);
  286. return ff_set_common_samplerates(ctx, formats);
  287. }
  288. static av_cold void uninit(AVFilterContext *ctx)
  289. {
  290. AFFTFiltContext *s = ctx->priv;
  291. int i;
  292. av_fft_end(s->fft);
  293. av_fft_end(s->ifft);
  294. for (i = 0; i < s->nb_exprs; i++) {
  295. if (s->fft_data)
  296. av_freep(&s->fft_data[i]);
  297. }
  298. av_freep(&s->fft_data);
  299. for (i = 0; i < s->nb_exprs; i++) {
  300. av_expr_free(s->real[i]);
  301. av_expr_free(s->imag[i]);
  302. }
  303. av_freep(&s->real);
  304. av_freep(&s->imag);
  305. av_frame_free(&s->buffer);
  306. }
  307. static const AVFilterPad inputs[] = {
  308. {
  309. .name = "default",
  310. .type = AVMEDIA_TYPE_AUDIO,
  311. .config_props = config_input,
  312. .filter_frame = filter_frame,
  313. },
  314. { NULL }
  315. };
  316. static const AVFilterPad outputs[] = {
  317. {
  318. .name = "default",
  319. .type = AVMEDIA_TYPE_AUDIO,
  320. },
  321. { NULL }
  322. };
  323. AVFilter ff_af_afftfilt = {
  324. .name = "afftfilt",
  325. .description = NULL_IF_CONFIG_SMALL("Apply arbitrary expressions to samples in frequency domain."),
  326. .priv_size = sizeof(AFFTFiltContext),
  327. .priv_class = &afftfilt_class,
  328. .inputs = inputs,
  329. .outputs = outputs,
  330. .query_formats = query_formats,
  331. .uninit = uninit,
  332. };