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.

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