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.

403 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;
  90. const char *last_expr = "1";
  91. s->fft = av_fft_init(s->fft_bits, 0);
  92. s->ifft = av_fft_init(s->fft_bits, 1);
  93. if (!s->fft || !s->ifft)
  94. return AVERROR(ENOMEM);
  95. s->window_size = 1 << s->fft_bits;
  96. s->fft_data = av_calloc(inlink->channels, sizeof(*s->fft_data));
  97. if (!s->fft_data)
  98. return AVERROR(ENOMEM);
  99. for (ch = 0; ch < inlink->channels; ch++) {
  100. s->fft_data[ch] = av_calloc(s->window_size, sizeof(**s->fft_data));
  101. if (!s->fft_data[ch])
  102. return AVERROR(ENOMEM);
  103. }
  104. s->real = av_calloc(inlink->channels, sizeof(*s->real));
  105. if (!s->real)
  106. return AVERROR(ENOMEM);
  107. s->imag = av_calloc(inlink->channels, sizeof(*s->imag));
  108. if (!s->imag)
  109. return AVERROR(ENOMEM);
  110. args = av_strdup(s->real_str);
  111. if (!args)
  112. return AVERROR(ENOMEM);
  113. for (ch = 0; ch < inlink->channels; ch++) {
  114. char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr);
  115. ret = av_expr_parse(&s->real[ch], arg ? arg : last_expr, var_names,
  116. NULL, NULL, NULL, NULL, 0, ctx);
  117. if (ret < 0)
  118. break;
  119. if (arg)
  120. last_expr = arg;
  121. s->nb_exprs++;
  122. }
  123. av_free(args);
  124. args = av_strdup(s->img_str ? s->img_str : s->real_str);
  125. if (!args)
  126. return AVERROR(ENOMEM);
  127. for (ch = 0; ch < inlink->channels; ch++) {
  128. char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr);
  129. ret = av_expr_parse(&s->imag[ch], arg ? arg : last_expr, var_names,
  130. NULL, NULL, NULL, NULL, 0, ctx);
  131. if (ret < 0)
  132. break;
  133. if (arg)
  134. last_expr = arg;
  135. }
  136. av_free(args);
  137. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->window_size);
  138. if (!s->fifo)
  139. return AVERROR(ENOMEM);
  140. s->window_func_lut = av_realloc_f(s->window_func_lut, s->window_size,
  141. sizeof(*s->window_func_lut));
  142. if (!s->window_func_lut)
  143. return AVERROR(ENOMEM);
  144. ff_generate_window_func(s->window_func_lut, s->window_size, s->win_func, &overlap);
  145. if (s->overlap == 1)
  146. s->overlap = overlap;
  147. for (s->win_scale = 0, i = 0; i < s->window_size; i++) {
  148. s->win_scale += s->window_func_lut[i] * s->window_func_lut[i];
  149. }
  150. s->hop_size = s->window_size * (1 - s->overlap);
  151. if (s->hop_size <= 0)
  152. return AVERROR(EINVAL);
  153. s->buffer = ff_get_audio_buffer(inlink, s->window_size * 2);
  154. if (!s->buffer)
  155. return AVERROR(ENOMEM);
  156. return ret;
  157. }
  158. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  159. {
  160. AVFilterContext *ctx = inlink->dst;
  161. AVFilterLink *outlink = ctx->outputs[0];
  162. AFFTFiltContext *s = ctx->priv;
  163. const int window_size = s->window_size;
  164. const float f = 1. / s->win_scale;
  165. double values[VAR_VARS_NB];
  166. AVFrame *out, *in = NULL;
  167. int ch, n, ret, i, j, k;
  168. int start = s->start, end = s->end;
  169. av_audio_fifo_write(s->fifo, (void **)frame->extended_data, frame->nb_samples);
  170. av_frame_free(&frame);
  171. while (av_audio_fifo_size(s->fifo) >= window_size) {
  172. if (!in) {
  173. in = ff_get_audio_buffer(outlink, window_size);
  174. if (!in)
  175. return AVERROR(ENOMEM);
  176. }
  177. ret = av_audio_fifo_peek(s->fifo, (void **)in->extended_data, window_size);
  178. if (ret < 0)
  179. break;
  180. for (ch = 0; ch < inlink->channels; ch++) {
  181. const float *src = (float *)in->extended_data[ch];
  182. FFTComplex *fft_data = s->fft_data[ch];
  183. for (n = 0; n < in->nb_samples; n++) {
  184. fft_data[n].re = src[n] * s->window_func_lut[n];
  185. fft_data[n].im = 0;
  186. }
  187. for (; n < window_size; n++) {
  188. fft_data[n].re = 0;
  189. fft_data[n].im = 0;
  190. }
  191. }
  192. values[VAR_PTS] = s->pts;
  193. values[VAR_SAMPLE_RATE] = inlink->sample_rate;
  194. values[VAR_NBBINS] = window_size / 2;
  195. values[VAR_CHANNELS] = inlink->channels;
  196. for (ch = 0; ch < inlink->channels; ch++) {
  197. FFTComplex *fft_data = s->fft_data[ch];
  198. float *buf = (float *)s->buffer->extended_data[ch];
  199. int x;
  200. values[VAR_CHANNEL] = ch;
  201. av_fft_permute(s->fft, fft_data);
  202. av_fft_calc(s->fft, fft_data);
  203. for (n = 0; n < window_size / 2; n++) {
  204. float fr, fi;
  205. values[VAR_BIN] = n;
  206. fr = av_expr_eval(s->real[ch], values, s);
  207. fi = av_expr_eval(s->imag[ch], values, s);
  208. fft_data[n].re *= fr;
  209. fft_data[n].im *= fi;
  210. }
  211. for (n = window_size / 2 + 1, x = window_size / 2 - 1; n < window_size; n++, x--) {
  212. fft_data[n].re = fft_data[x].re;
  213. fft_data[n].im = -fft_data[x].im;
  214. }
  215. av_fft_permute(s->ifft, fft_data);
  216. av_fft_calc(s->ifft, fft_data);
  217. start = s->start;
  218. end = s->end;
  219. k = end;
  220. for (i = 0, j = start; j < k && i < window_size; i++, j++) {
  221. buf[j] += s->fft_data[ch][i].re * f;
  222. }
  223. for (; i < window_size; i++, j++) {
  224. buf[j] = s->fft_data[ch][i].re * f;
  225. }
  226. start += s->hop_size;
  227. end = j;
  228. }
  229. s->start = start;
  230. s->end = end;
  231. if (start >= window_size) {
  232. float *dst, *buf;
  233. start -= window_size;
  234. end -= window_size;
  235. s->start = start;
  236. s->end = end;
  237. out = ff_get_audio_buffer(outlink, window_size);
  238. if (!out) {
  239. ret = AVERROR(ENOMEM);
  240. break;
  241. }
  242. out->pts = s->pts;
  243. s->pts += window_size;
  244. for (ch = 0; ch < inlink->channels; ch++) {
  245. dst = (float *)out->extended_data[ch];
  246. buf = (float *)s->buffer->extended_data[ch];
  247. for (n = 0; n < window_size; n++) {
  248. dst[n] = buf[n] * (1 - s->overlap);
  249. }
  250. memmove(buf, buf + window_size, window_size * 4);
  251. }
  252. ret = ff_filter_frame(outlink, out);
  253. if (ret < 0)
  254. break;
  255. }
  256. av_audio_fifo_drain(s->fifo, s->hop_size);
  257. }
  258. av_frame_free(&in);
  259. return ret;
  260. }
  261. static int query_formats(AVFilterContext *ctx)
  262. {
  263. AVFilterFormats *formats;
  264. AVFilterChannelLayouts *layouts;
  265. static const enum AVSampleFormat sample_fmts[] = {
  266. AV_SAMPLE_FMT_FLTP,
  267. AV_SAMPLE_FMT_NONE
  268. };
  269. int ret;
  270. layouts = ff_all_channel_counts();
  271. if (!layouts)
  272. return AVERROR(ENOMEM);
  273. ret = ff_set_common_channel_layouts(ctx, layouts);
  274. if (ret < 0)
  275. return ret;
  276. formats = ff_make_format_list(sample_fmts);
  277. if (!formats)
  278. return AVERROR(ENOMEM);
  279. ret = ff_set_common_formats(ctx, formats);
  280. if (ret < 0)
  281. return ret;
  282. formats = ff_all_samplerates();
  283. if (!formats)
  284. return AVERROR(ENOMEM);
  285. return ff_set_common_samplerates(ctx, formats);
  286. }
  287. static av_cold void uninit(AVFilterContext *ctx)
  288. {
  289. AFFTFiltContext *s = ctx->priv;
  290. int i;
  291. av_fft_end(s->fft);
  292. av_fft_end(s->ifft);
  293. for (i = 0; i < s->nb_exprs; i++) {
  294. if (s->fft_data)
  295. av_freep(&s->fft_data[i]);
  296. }
  297. av_freep(&s->fft_data);
  298. for (i = 0; i < s->nb_exprs; i++) {
  299. av_expr_free(s->real[i]);
  300. av_expr_free(s->imag[i]);
  301. }
  302. av_freep(&s->real);
  303. av_freep(&s->imag);
  304. av_frame_free(&s->buffer);
  305. }
  306. static const AVFilterPad inputs[] = {
  307. {
  308. .name = "default",
  309. .type = AVMEDIA_TYPE_AUDIO,
  310. .config_props = config_input,
  311. .filter_frame = filter_frame,
  312. },
  313. { NULL }
  314. };
  315. static const AVFilterPad outputs[] = {
  316. {
  317. .name = "default",
  318. .type = AVMEDIA_TYPE_AUDIO,
  319. },
  320. { NULL }
  321. };
  322. AVFilter ff_af_afftfilt = {
  323. .name = "afftfilt",
  324. .description = NULL_IF_CONFIG_SMALL("Apply arbitrary expressions to samples in frequency domain."),
  325. .priv_size = sizeof(AFFTFiltContext),
  326. .priv_class = &afftfilt_class,
  327. .inputs = inputs,
  328. .outputs = outputs,
  329. .query_formats = query_formats,
  330. .uninit = uninit,
  331. };