You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

500 lines
16KB

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