af_afftfilt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 "filters.h"
  29. #include "window_func.h"
  30. typedef struct AFFTFiltContext {
  31. const AVClass *class;
  32. char *real_str;
  33. char *img_str;
  34. int fft_size;
  35. int fft_bits;
  36. FFTContext *fft, *ifft;
  37. FFTComplex **fft_data;
  38. FFTComplex **fft_temp;
  39. int nb_exprs;
  40. int window_size;
  41. AVExpr **real;
  42. AVExpr **imag;
  43. AVAudioFifo *fifo;
  44. int64_t pts;
  45. int hop_size;
  46. float overlap;
  47. AVFrame *buffer;
  48. int eof;
  49. int win_func;
  50. float *window_func_lut;
  51. } AFFTFiltContext;
  52. static const char *const var_names[] = { "sr", "b", "nb", "ch", "chs", "pts", "re", "im", NULL };
  53. enum { VAR_SAMPLE_RATE, VAR_BIN, VAR_NBBINS, VAR_CHANNEL, VAR_CHANNELS, VAR_PTS, VAR_REAL, VAR_IMAG, VAR_VARS_NB };
  54. #define OFFSET(x) offsetof(AFFTFiltContext, x)
  55. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  56. static const AVOption afftfilt_options[] = {
  57. { "real", "set channels real expressions", OFFSET(real_str), AV_OPT_TYPE_STRING, {.str = "re" }, 0, 0, A },
  58. { "imag", "set channels imaginary expressions", OFFSET(img_str), AV_OPT_TYPE_STRING, {.str = "im" }, 0, 0, A },
  59. { "win_size", "set window size", OFFSET(fft_size), AV_OPT_TYPE_INT, {.i64=4096}, 16, 131072, A },
  60. { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, A, "win_func" },
  61. { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, A, "win_func" },
  62. { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, A, "win_func" },
  63. { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, A, "win_func" },
  64. { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, A, "win_func" },
  65. { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, A, "win_func" },
  66. { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, A, "win_func" },
  67. { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, A, "win_func" },
  68. { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, A, "win_func" },
  69. { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, A, "win_func" },
  70. { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, A, "win_func" },
  71. { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, A, "win_func" },
  72. { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, A, "win_func" },
  73. { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, A, "win_func" },
  74. { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, A, "win_func" },
  75. { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, A, "win_func" },
  76. { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, A, "win_func" },
  77. { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, A, "win_func" },
  78. { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, A, "win_func" },
  79. { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, A, "win_func" },
  80. { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, A, "win_func" },
  81. { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, A, "win_func" },
  82. { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, A },
  83. { NULL },
  84. };
  85. AVFILTER_DEFINE_CLASS(afftfilt);
  86. static inline double getreal(void *priv, double x, double ch)
  87. {
  88. AFFTFiltContext *s = priv;
  89. int ich, ix;
  90. ich = av_clip(ch, 0, s->nb_exprs - 1);
  91. ix = av_clip(x, 0, s->window_size / 2);
  92. return s->fft_data[ich][ix].re;
  93. }
  94. static inline double getimag(void *priv, double x, double ch)
  95. {
  96. AFFTFiltContext *s = priv;
  97. int ich, ix;
  98. ich = av_clip(ch, 0, s->nb_exprs - 1);
  99. ix = av_clip(x, 0, s->window_size / 2);
  100. return s->fft_data[ich][ix].im;
  101. }
  102. static double realf(void *priv, double x, double ch) { return getreal(priv, x, ch); }
  103. static double imagf(void *priv, double x, double ch) { return getimag(priv, x, ch); }
  104. static const char *const func2_names[] = { "real", "imag", NULL };
  105. double (*func2[])(void *, double, double) = { realf, imagf, NULL };
  106. static int config_input(AVFilterLink *inlink)
  107. {
  108. AVFilterContext *ctx = inlink->dst;
  109. AFFTFiltContext *s = ctx->priv;
  110. char *saveptr = NULL;
  111. int ret = 0, ch;
  112. float overlap;
  113. char *args;
  114. const char *last_expr = "1";
  115. s->pts = AV_NOPTS_VALUE;
  116. s->fft_bits = av_log2(s->fft_size);
  117. s->fft = av_fft_init(s->fft_bits, 0);
  118. s->ifft = av_fft_init(s->fft_bits, 1);
  119. if (!s->fft || !s->ifft)
  120. return AVERROR(ENOMEM);
  121. s->window_size = 1 << s->fft_bits;
  122. s->fft_data = av_calloc(inlink->channels, sizeof(*s->fft_data));
  123. if (!s->fft_data)
  124. return AVERROR(ENOMEM);
  125. s->fft_temp = av_calloc(inlink->channels, sizeof(*s->fft_temp));
  126. if (!s->fft_temp)
  127. return AVERROR(ENOMEM);
  128. for (ch = 0; ch < inlink->channels; ch++) {
  129. s->fft_data[ch] = av_calloc(s->window_size, sizeof(**s->fft_data));
  130. if (!s->fft_data[ch])
  131. return AVERROR(ENOMEM);
  132. }
  133. for (ch = 0; ch < inlink->channels; ch++) {
  134. s->fft_temp[ch] = av_calloc(s->window_size, sizeof(**s->fft_temp));
  135. if (!s->fft_temp[ch])
  136. return AVERROR(ENOMEM);
  137. }
  138. s->real = av_calloc(inlink->channels, sizeof(*s->real));
  139. if (!s->real)
  140. return AVERROR(ENOMEM);
  141. s->imag = av_calloc(inlink->channels, sizeof(*s->imag));
  142. if (!s->imag)
  143. return AVERROR(ENOMEM);
  144. args = av_strdup(s->real_str);
  145. if (!args)
  146. return AVERROR(ENOMEM);
  147. for (ch = 0; ch < inlink->channels; ch++) {
  148. char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr);
  149. ret = av_expr_parse(&s->real[ch], arg ? arg : last_expr, var_names,
  150. NULL, NULL, func2_names, func2, 0, ctx);
  151. if (ret < 0)
  152. break;
  153. if (arg)
  154. last_expr = arg;
  155. s->nb_exprs++;
  156. }
  157. av_free(args);
  158. args = av_strdup(s->img_str ? s->img_str : s->real_str);
  159. if (!args)
  160. return AVERROR(ENOMEM);
  161. for (ch = 0; ch < inlink->channels; ch++) {
  162. char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr);
  163. ret = av_expr_parse(&s->imag[ch], arg ? arg : last_expr, var_names,
  164. NULL, NULL, func2_names, func2, 0, ctx);
  165. if (ret < 0)
  166. break;
  167. if (arg)
  168. last_expr = arg;
  169. }
  170. av_free(args);
  171. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->window_size);
  172. if (!s->fifo)
  173. return AVERROR(ENOMEM);
  174. s->window_func_lut = av_realloc_f(s->window_func_lut, s->window_size,
  175. sizeof(*s->window_func_lut));
  176. if (!s->window_func_lut)
  177. return AVERROR(ENOMEM);
  178. generate_window_func(s->window_func_lut, s->window_size, s->win_func, &overlap);
  179. if (s->overlap == 1)
  180. s->overlap = overlap;
  181. s->hop_size = s->window_size * (1 - s->overlap);
  182. if (s->hop_size <= 0)
  183. return AVERROR(EINVAL);
  184. s->buffer = ff_get_audio_buffer(inlink, s->window_size * 2);
  185. if (!s->buffer)
  186. return AVERROR(ENOMEM);
  187. return ret;
  188. }
  189. static int filter_frame(AVFilterLink *inlink)
  190. {
  191. AVFilterContext *ctx = inlink->dst;
  192. AVFilterLink *outlink = ctx->outputs[0];
  193. AFFTFiltContext *s = ctx->priv;
  194. const int window_size = s->window_size;
  195. const float f = 1. / (s->window_size / 2);
  196. double values[VAR_VARS_NB];
  197. AVFrame *out, *in = NULL;
  198. int ch, n, ret, i;
  199. if (!in) {
  200. in = ff_get_audio_buffer(outlink, window_size);
  201. if (!in)
  202. return AVERROR(ENOMEM);
  203. }
  204. ret = av_audio_fifo_peek(s->fifo, (void **)in->extended_data, window_size);
  205. if (ret < 0)
  206. goto fail;
  207. for (ch = 0; ch < inlink->channels; ch++) {
  208. const float *src = (float *)in->extended_data[ch];
  209. FFTComplex *fft_data = s->fft_data[ch];
  210. for (n = 0; n < in->nb_samples; n++) {
  211. fft_data[n].re = src[n] * s->window_func_lut[n];
  212. fft_data[n].im = 0;
  213. }
  214. for (; n < window_size; n++) {
  215. fft_data[n].re = 0;
  216. fft_data[n].im = 0;
  217. }
  218. }
  219. values[VAR_PTS] = s->pts;
  220. values[VAR_SAMPLE_RATE] = inlink->sample_rate;
  221. values[VAR_NBBINS] = window_size / 2;
  222. values[VAR_CHANNELS] = inlink->channels;
  223. for (ch = 0; ch < inlink->channels; ch++) {
  224. FFTComplex *fft_data = s->fft_data[ch];
  225. av_fft_permute(s->fft, fft_data);
  226. av_fft_calc(s->fft, fft_data);
  227. }
  228. for (ch = 0; ch < inlink->channels; ch++) {
  229. FFTComplex *fft_data = s->fft_data[ch];
  230. FFTComplex *fft_temp = s->fft_temp[ch];
  231. float *buf = (float *)s->buffer->extended_data[ch];
  232. int x;
  233. values[VAR_CHANNEL] = ch;
  234. for (n = 0; n <= window_size / 2; n++) {
  235. float fr, fi;
  236. values[VAR_BIN] = n;
  237. values[VAR_REAL] = fft_data[n].re;
  238. values[VAR_IMAG] = fft_data[n].im;
  239. fr = av_expr_eval(s->real[ch], values, s);
  240. fi = av_expr_eval(s->imag[ch], values, s);
  241. fft_temp[n].re = fr;
  242. fft_temp[n].im = fi;
  243. }
  244. for (n = window_size / 2 + 1, x = window_size / 2 - 1; n < window_size; n++, x--) {
  245. fft_temp[n].re = fft_temp[x].re;
  246. fft_temp[n].im = -fft_temp[x].im;
  247. }
  248. av_fft_permute(s->ifft, fft_temp);
  249. av_fft_calc(s->ifft, fft_temp);
  250. for (i = 0; i < window_size; i++) {
  251. buf[i] += s->fft_temp[ch][i].re * f;
  252. }
  253. }
  254. out = ff_get_audio_buffer(outlink, s->hop_size);
  255. if (!out) {
  256. ret = AVERROR(ENOMEM);
  257. goto fail;
  258. }
  259. out->pts = s->pts;
  260. s->pts += s->hop_size;
  261. for (ch = 0; ch < inlink->channels; ch++) {
  262. float *dst = (float *)out->extended_data[ch];
  263. float *buf = (float *)s->buffer->extended_data[ch];
  264. for (n = 0; n < s->hop_size; n++)
  265. dst[n] = buf[n] * (1.f - s->overlap);
  266. memmove(buf, buf + s->hop_size, window_size * 4);
  267. }
  268. ret = ff_filter_frame(outlink, out);
  269. if (ret < 0)
  270. goto fail;
  271. av_audio_fifo_drain(s->fifo, s->hop_size);
  272. fail:
  273. av_frame_free(&in);
  274. return ret < 0 ? ret : 0;
  275. }
  276. static int activate(AVFilterContext *ctx)
  277. {
  278. AVFilterLink *inlink = ctx->inputs[0];
  279. AVFilterLink *outlink = ctx->outputs[0];
  280. AFFTFiltContext *s = ctx->priv;
  281. AVFrame *in = NULL;
  282. int ret = 0, status;
  283. int64_t pts;
  284. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  285. if (!s->eof && av_audio_fifo_size(s->fifo) < s->window_size) {
  286. ret = ff_inlink_consume_frame(inlink, &in);
  287. if (ret < 0)
  288. return ret;
  289. if (ret > 0) {
  290. ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
  291. in->nb_samples);
  292. if (ret >= 0 && s->pts == AV_NOPTS_VALUE)
  293. s->pts = in->pts;
  294. av_frame_free(&in);
  295. if (ret < 0)
  296. return ret;
  297. }
  298. }
  299. if ((av_audio_fifo_size(s->fifo) >= s->window_size) ||
  300. (av_audio_fifo_size(s->fifo) > 0 && s->eof)) {
  301. ret = filter_frame(inlink);
  302. if (av_audio_fifo_size(s->fifo) >= s->window_size)
  303. ff_filter_set_ready(ctx, 100);
  304. return ret;
  305. }
  306. if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
  307. if (status == AVERROR_EOF) {
  308. s->eof = 1;
  309. if (av_audio_fifo_size(s->fifo) >= 0) {
  310. ff_filter_set_ready(ctx, 100);
  311. return 0;
  312. }
  313. }
  314. }
  315. if (s->eof && av_audio_fifo_size(s->fifo) <= 0) {
  316. ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
  317. return 0;
  318. }
  319. if (!s->eof)
  320. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  321. return FFERROR_NOT_READY;
  322. }
  323. static int query_formats(AVFilterContext *ctx)
  324. {
  325. AVFilterFormats *formats;
  326. AVFilterChannelLayouts *layouts;
  327. static const enum AVSampleFormat sample_fmts[] = {
  328. AV_SAMPLE_FMT_FLTP,
  329. AV_SAMPLE_FMT_NONE
  330. };
  331. int ret;
  332. layouts = ff_all_channel_counts();
  333. if (!layouts)
  334. return AVERROR(ENOMEM);
  335. ret = ff_set_common_channel_layouts(ctx, layouts);
  336. if (ret < 0)
  337. return ret;
  338. formats = ff_make_format_list(sample_fmts);
  339. if (!formats)
  340. return AVERROR(ENOMEM);
  341. ret = ff_set_common_formats(ctx, formats);
  342. if (ret < 0)
  343. return ret;
  344. formats = ff_all_samplerates();
  345. if (!formats)
  346. return AVERROR(ENOMEM);
  347. return ff_set_common_samplerates(ctx, formats);
  348. }
  349. static av_cold void uninit(AVFilterContext *ctx)
  350. {
  351. AFFTFiltContext *s = ctx->priv;
  352. int i;
  353. av_fft_end(s->fft);
  354. av_fft_end(s->ifft);
  355. for (i = 0; i < s->nb_exprs; i++) {
  356. if (s->fft_data)
  357. av_freep(&s->fft_data[i]);
  358. if (s->fft_temp)
  359. av_freep(&s->fft_temp[i]);
  360. }
  361. av_freep(&s->fft_data);
  362. av_freep(&s->fft_temp);
  363. for (i = 0; i < s->nb_exprs; i++) {
  364. av_expr_free(s->real[i]);
  365. av_expr_free(s->imag[i]);
  366. }
  367. av_freep(&s->real);
  368. av_freep(&s->imag);
  369. av_frame_free(&s->buffer);
  370. av_freep(&s->window_func_lut);
  371. av_audio_fifo_free(s->fifo);
  372. }
  373. static const AVFilterPad inputs[] = {
  374. {
  375. .name = "default",
  376. .type = AVMEDIA_TYPE_AUDIO,
  377. .config_props = config_input,
  378. },
  379. { NULL }
  380. };
  381. static const AVFilterPad outputs[] = {
  382. {
  383. .name = "default",
  384. .type = AVMEDIA_TYPE_AUDIO,
  385. },
  386. { NULL }
  387. };
  388. AVFilter ff_af_afftfilt = {
  389. .name = "afftfilt",
  390. .description = NULL_IF_CONFIG_SMALL("Apply arbitrary expressions to samples in frequency domain."),
  391. .priv_size = sizeof(AFFTFiltContext),
  392. .priv_class = &afftfilt_class,
  393. .inputs = inputs,
  394. .outputs = outputs,
  395. .activate = activate,
  396. .query_formats = query_formats,
  397. .uninit = uninit,
  398. };