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.

402 lines
13KB

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