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.

485 lines
15KB

  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. saveptr = NULL;
  162. last_expr = "1";
  163. for (ch = 0; ch < inlink->channels; ch++) {
  164. char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr);
  165. ret = av_expr_parse(&s->imag[ch], arg ? arg : last_expr, var_names,
  166. NULL, NULL, func2_names, func2, 0, ctx);
  167. if (ret < 0)
  168. break;
  169. if (arg)
  170. last_expr = arg;
  171. }
  172. av_free(args);
  173. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->window_size);
  174. if (!s->fifo)
  175. return AVERROR(ENOMEM);
  176. s->window_func_lut = av_realloc_f(s->window_func_lut, s->window_size,
  177. sizeof(*s->window_func_lut));
  178. if (!s->window_func_lut)
  179. return AVERROR(ENOMEM);
  180. generate_window_func(s->window_func_lut, s->window_size, s->win_func, &overlap);
  181. if (s->overlap == 1)
  182. s->overlap = overlap;
  183. s->hop_size = s->window_size * (1 - s->overlap);
  184. if (s->hop_size <= 0)
  185. return AVERROR(EINVAL);
  186. s->buffer = ff_get_audio_buffer(inlink, s->window_size * 2);
  187. if (!s->buffer)
  188. return AVERROR(ENOMEM);
  189. return ret;
  190. }
  191. static int filter_frame(AVFilterLink *inlink)
  192. {
  193. AVFilterContext *ctx = inlink->dst;
  194. AVFilterLink *outlink = ctx->outputs[0];
  195. AFFTFiltContext *s = ctx->priv;
  196. const int window_size = s->window_size;
  197. const float f = 1. / (s->window_size / 2);
  198. double values[VAR_VARS_NB];
  199. AVFrame *out, *in = NULL;
  200. int ch, n, ret, i;
  201. if (!in) {
  202. in = ff_get_audio_buffer(outlink, window_size);
  203. if (!in)
  204. return AVERROR(ENOMEM);
  205. }
  206. ret = av_audio_fifo_peek(s->fifo, (void **)in->extended_data, window_size);
  207. if (ret < 0)
  208. goto fail;
  209. for (ch = 0; ch < inlink->channels; ch++) {
  210. const float *src = (float *)in->extended_data[ch];
  211. FFTComplex *fft_data = s->fft_data[ch];
  212. for (n = 0; n < in->nb_samples; n++) {
  213. fft_data[n].re = src[n] * s->window_func_lut[n];
  214. fft_data[n].im = 0;
  215. }
  216. for (; n < window_size; n++) {
  217. fft_data[n].re = 0;
  218. fft_data[n].im = 0;
  219. }
  220. }
  221. values[VAR_PTS] = s->pts;
  222. values[VAR_SAMPLE_RATE] = inlink->sample_rate;
  223. values[VAR_NBBINS] = window_size / 2;
  224. values[VAR_CHANNELS] = inlink->channels;
  225. for (ch = 0; ch < inlink->channels; ch++) {
  226. FFTComplex *fft_data = s->fft_data[ch];
  227. av_fft_permute(s->fft, fft_data);
  228. av_fft_calc(s->fft, fft_data);
  229. }
  230. for (ch = 0; ch < inlink->channels; ch++) {
  231. FFTComplex *fft_data = s->fft_data[ch];
  232. FFTComplex *fft_temp = s->fft_temp[ch];
  233. float *buf = (float *)s->buffer->extended_data[ch];
  234. int x;
  235. values[VAR_CHANNEL] = ch;
  236. for (n = 0; n <= window_size / 2; n++) {
  237. float fr, fi;
  238. values[VAR_BIN] = n;
  239. values[VAR_REAL] = fft_data[n].re;
  240. values[VAR_IMAG] = fft_data[n].im;
  241. fr = av_expr_eval(s->real[ch], values, s);
  242. fi = av_expr_eval(s->imag[ch], values, s);
  243. fft_temp[n].re = fr;
  244. fft_temp[n].im = fi;
  245. }
  246. for (n = window_size / 2 + 1, x = window_size / 2 - 1; n < window_size; n++, x--) {
  247. fft_temp[n].re = fft_temp[x].re;
  248. fft_temp[n].im = -fft_temp[x].im;
  249. }
  250. av_fft_permute(s->ifft, fft_temp);
  251. av_fft_calc(s->ifft, fft_temp);
  252. for (i = 0; i < window_size; i++) {
  253. buf[i] += s->fft_temp[ch][i].re * f;
  254. }
  255. }
  256. out = ff_get_audio_buffer(outlink, s->hop_size);
  257. if (!out) {
  258. ret = AVERROR(ENOMEM);
  259. goto fail;
  260. }
  261. out->pts = s->pts;
  262. s->pts += s->hop_size;
  263. for (ch = 0; ch < inlink->channels; ch++) {
  264. float *dst = (float *)out->extended_data[ch];
  265. float *buf = (float *)s->buffer->extended_data[ch];
  266. for (n = 0; n < s->hop_size; n++)
  267. dst[n] = buf[n] * (1.f - s->overlap);
  268. memmove(buf, buf + s->hop_size, window_size * 4);
  269. }
  270. ret = ff_filter_frame(outlink, out);
  271. if (ret < 0)
  272. goto fail;
  273. av_audio_fifo_drain(s->fifo, s->hop_size);
  274. fail:
  275. av_frame_free(&in);
  276. return ret < 0 ? ret : 0;
  277. }
  278. static int activate(AVFilterContext *ctx)
  279. {
  280. AVFilterLink *inlink = ctx->inputs[0];
  281. AVFilterLink *outlink = ctx->outputs[0];
  282. AFFTFiltContext *s = ctx->priv;
  283. AVFrame *in = NULL;
  284. int ret = 0, status;
  285. int64_t pts;
  286. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  287. if (!s->eof && av_audio_fifo_size(s->fifo) < s->window_size) {
  288. ret = ff_inlink_consume_frame(inlink, &in);
  289. if (ret < 0)
  290. return ret;
  291. if (ret > 0) {
  292. ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
  293. in->nb_samples);
  294. if (ret >= 0 && s->pts == AV_NOPTS_VALUE)
  295. s->pts = in->pts;
  296. av_frame_free(&in);
  297. if (ret < 0)
  298. return ret;
  299. }
  300. }
  301. if ((av_audio_fifo_size(s->fifo) >= s->window_size) ||
  302. (av_audio_fifo_size(s->fifo) > 0 && s->eof)) {
  303. ret = filter_frame(inlink);
  304. if (av_audio_fifo_size(s->fifo) >= s->window_size)
  305. ff_filter_set_ready(ctx, 100);
  306. return ret;
  307. }
  308. if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
  309. if (status == AVERROR_EOF) {
  310. s->eof = 1;
  311. if (av_audio_fifo_size(s->fifo) >= 0) {
  312. ff_filter_set_ready(ctx, 100);
  313. return 0;
  314. }
  315. }
  316. }
  317. if (s->eof && av_audio_fifo_size(s->fifo) <= 0) {
  318. ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
  319. return 0;
  320. }
  321. if (!s->eof)
  322. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  323. return FFERROR_NOT_READY;
  324. }
  325. static int query_formats(AVFilterContext *ctx)
  326. {
  327. AVFilterFormats *formats;
  328. AVFilterChannelLayouts *layouts;
  329. static const enum AVSampleFormat sample_fmts[] = {
  330. AV_SAMPLE_FMT_FLTP,
  331. AV_SAMPLE_FMT_NONE
  332. };
  333. int ret;
  334. layouts = ff_all_channel_counts();
  335. if (!layouts)
  336. return AVERROR(ENOMEM);
  337. ret = ff_set_common_channel_layouts(ctx, layouts);
  338. if (ret < 0)
  339. return ret;
  340. formats = ff_make_format_list(sample_fmts);
  341. if (!formats)
  342. return AVERROR(ENOMEM);
  343. ret = ff_set_common_formats(ctx, formats);
  344. if (ret < 0)
  345. return ret;
  346. formats = ff_all_samplerates();
  347. if (!formats)
  348. return AVERROR(ENOMEM);
  349. return ff_set_common_samplerates(ctx, formats);
  350. }
  351. static av_cold void uninit(AVFilterContext *ctx)
  352. {
  353. AFFTFiltContext *s = ctx->priv;
  354. int i;
  355. av_fft_end(s->fft);
  356. av_fft_end(s->ifft);
  357. for (i = 0; i < s->nb_exprs; i++) {
  358. if (s->fft_data)
  359. av_freep(&s->fft_data[i]);
  360. if (s->fft_temp)
  361. av_freep(&s->fft_temp[i]);
  362. }
  363. av_freep(&s->fft_data);
  364. av_freep(&s->fft_temp);
  365. for (i = 0; i < s->nb_exprs; i++) {
  366. av_expr_free(s->real[i]);
  367. av_expr_free(s->imag[i]);
  368. }
  369. av_freep(&s->real);
  370. av_freep(&s->imag);
  371. av_frame_free(&s->buffer);
  372. av_freep(&s->window_func_lut);
  373. av_audio_fifo_free(s->fifo);
  374. }
  375. static const AVFilterPad inputs[] = {
  376. {
  377. .name = "default",
  378. .type = AVMEDIA_TYPE_AUDIO,
  379. .config_props = config_input,
  380. },
  381. { NULL }
  382. };
  383. static const AVFilterPad outputs[] = {
  384. {
  385. .name = "default",
  386. .type = AVMEDIA_TYPE_AUDIO,
  387. },
  388. { NULL }
  389. };
  390. AVFilter ff_af_afftfilt = {
  391. .name = "afftfilt",
  392. .description = NULL_IF_CONFIG_SMALL("Apply arbitrary expressions to samples in frequency domain."),
  393. .priv_size = sizeof(AFFTFiltContext),
  394. .priv_class = &afftfilt_class,
  395. .inputs = inputs,
  396. .outputs = outputs,
  397. .activate = activate,
  398. .query_formats = query_formats,
  399. .uninit = uninit,
  400. };