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.

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