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.

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