vaf_spectrumsynth.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, 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. /**
  21. * @file
  22. * SpectrumSynth filter
  23. * @todo support float pixel format
  24. */
  25. #include "libavcodec/avfft.h"
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/channel_layout.h"
  28. #include "libavutil/ffmath.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/parseutils.h"
  31. #include "avfilter.h"
  32. #include "formats.h"
  33. #include "audio.h"
  34. #include "video.h"
  35. #include "internal.h"
  36. #include "window_func.h"
  37. enum MagnitudeScale { LINEAR, LOG, NB_SCALES };
  38. enum SlideMode { REPLACE, SCROLL, FULLFRAME, RSCROLL, NB_SLIDES };
  39. enum Orientation { VERTICAL, HORIZONTAL, NB_ORIENTATIONS };
  40. typedef struct SpectrumSynthContext {
  41. const AVClass *class;
  42. int sample_rate;
  43. int channels;
  44. int scale;
  45. int sliding;
  46. int win_func;
  47. float overlap;
  48. int orientation;
  49. AVFrame *magnitude, *phase;
  50. FFTContext *fft; ///< Fast Fourier Transform context
  51. int fft_bits; ///< number of bits (FFT window size = 1<<fft_bits)
  52. FFTComplex **fft_data; ///< bins holder for each (displayed) channels
  53. int win_size;
  54. int size;
  55. int nb_freq;
  56. int hop_size;
  57. int start, end;
  58. int xpos;
  59. int xend;
  60. int64_t pts;
  61. float factor;
  62. AVFrame *buffer;
  63. float *window_func_lut; ///< Window function LUT
  64. } SpectrumSynthContext;
  65. #define OFFSET(x) offsetof(SpectrumSynthContext, x)
  66. #define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  67. #define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  68. static const AVOption spectrumsynth_options[] = {
  69. { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 44100}, 15, INT_MAX, A },
  70. { "channels", "set channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 1}, 1, 8, A },
  71. { "scale", "set input amplitude scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64 = LOG}, 0, NB_SCALES-1, V, "scale" },
  72. { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, V, "scale" },
  73. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, V, "scale" },
  74. { "slide", "set input sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = FULLFRAME}, 0, NB_SLIDES-1, V, "slide" },
  75. { "replace", "consume old columns with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, V, "slide" },
  76. { "scroll", "consume only most right column", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, V, "slide" },
  77. { "fullframe", "consume full frames", 0, AV_OPT_TYPE_CONST, {.i64=FULLFRAME}, 0, 0, V, "slide" },
  78. { "rscroll", "consume only most left column", 0, AV_OPT_TYPE_CONST, {.i64=RSCROLL}, 0, 0, V, "slide" },
  79. { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_WFUNC-1, A, "win_func" },
  80. { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, A, "win_func" },
  81. { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, A, "win_func" },
  82. { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, A, "win_func" },
  83. { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, A, "win_func" },
  84. { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, A, "win_func" },
  85. { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, A, "win_func" },
  86. { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, A },
  87. { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, V, "orientation" },
  88. { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, V, "orientation" },
  89. { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, V, "orientation" },
  90. { NULL }
  91. };
  92. AVFILTER_DEFINE_CLASS(spectrumsynth);
  93. static int query_formats(AVFilterContext *ctx)
  94. {
  95. SpectrumSynthContext *s = ctx->priv;
  96. AVFilterFormats *formats = NULL;
  97. AVFilterChannelLayouts *layout = NULL;
  98. AVFilterLink *magnitude = ctx->inputs[0];
  99. AVFilterLink *phase = ctx->inputs[1];
  100. AVFilterLink *outlink = ctx->outputs[0];
  101. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
  102. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
  103. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  104. AV_PIX_FMT_YUV444P16, AV_PIX_FMT_NONE };
  105. int ret, sample_rates[] = { 48000, -1 };
  106. formats = ff_make_format_list(sample_fmts);
  107. if ((ret = ff_formats_ref (formats, &outlink->in_formats )) < 0 ||
  108. (ret = ff_add_channel_layout (&layout, FF_COUNT2LAYOUT(s->channels))) < 0 ||
  109. (ret = ff_channel_layouts_ref (layout , &outlink->in_channel_layouts)) < 0)
  110. return ret;
  111. sample_rates[0] = s->sample_rate;
  112. formats = ff_make_format_list(sample_rates);
  113. if (!formats)
  114. return AVERROR(ENOMEM);
  115. if ((ret = ff_formats_ref(formats, &outlink->in_samplerates)) < 0)
  116. return ret;
  117. formats = ff_make_format_list(pix_fmts);
  118. if (!formats)
  119. return AVERROR(ENOMEM);
  120. if ((ret = ff_formats_ref(formats, &magnitude->out_formats)) < 0)
  121. return ret;
  122. formats = ff_make_format_list(pix_fmts);
  123. if (!formats)
  124. return AVERROR(ENOMEM);
  125. if ((ret = ff_formats_ref(formats, &phase->out_formats)) < 0)
  126. return ret;
  127. return 0;
  128. }
  129. static int config_output(AVFilterLink *outlink)
  130. {
  131. AVFilterContext *ctx = outlink->src;
  132. SpectrumSynthContext *s = ctx->priv;
  133. int width = ctx->inputs[0]->w;
  134. int height = ctx->inputs[0]->h;
  135. AVRational time_base = ctx->inputs[0]->time_base;
  136. AVRational frame_rate = ctx->inputs[0]->frame_rate;
  137. int i, ch, fft_bits;
  138. float factor, overlap;
  139. outlink->sample_rate = s->sample_rate;
  140. outlink->time_base = (AVRational){1, s->sample_rate};
  141. if (width != ctx->inputs[1]->w ||
  142. height != ctx->inputs[1]->h) {
  143. av_log(ctx, AV_LOG_ERROR,
  144. "Magnitude and Phase sizes differ (%dx%d vs %dx%d).\n",
  145. width, height,
  146. ctx->inputs[1]->w, ctx->inputs[1]->h);
  147. return AVERROR_INVALIDDATA;
  148. } else if (av_cmp_q(time_base, ctx->inputs[1]->time_base) != 0) {
  149. av_log(ctx, AV_LOG_ERROR,
  150. "Magnitude and Phase time bases differ (%d/%d vs %d/%d).\n",
  151. time_base.num, time_base.den,
  152. ctx->inputs[1]->time_base.num,
  153. ctx->inputs[1]->time_base.den);
  154. return AVERROR_INVALIDDATA;
  155. } else if (av_cmp_q(frame_rate, ctx->inputs[1]->frame_rate) != 0) {
  156. av_log(ctx, AV_LOG_ERROR,
  157. "Magnitude and Phase framerates differ (%d/%d vs %d/%d).\n",
  158. frame_rate.num, frame_rate.den,
  159. ctx->inputs[1]->frame_rate.num,
  160. ctx->inputs[1]->frame_rate.den);
  161. return AVERROR_INVALIDDATA;
  162. }
  163. s->size = s->orientation == VERTICAL ? height / s->channels : width / s->channels;
  164. s->xend = s->orientation == VERTICAL ? width : height;
  165. for (fft_bits = 1; 1 << fft_bits < 2 * s->size; fft_bits++);
  166. s->win_size = 1 << fft_bits;
  167. s->nb_freq = 1 << (fft_bits - 1);
  168. s->fft = av_fft_init(fft_bits, 1);
  169. if (!s->fft) {
  170. av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
  171. "The window size might be too high.\n");
  172. return AVERROR(EINVAL);
  173. }
  174. s->fft_data = av_calloc(s->channels, sizeof(*s->fft_data));
  175. if (!s->fft_data)
  176. return AVERROR(ENOMEM);
  177. for (ch = 0; ch < s->channels; ch++) {
  178. s->fft_data[ch] = av_calloc(s->win_size, sizeof(**s->fft_data));
  179. if (!s->fft_data[ch])
  180. return AVERROR(ENOMEM);
  181. }
  182. s->buffer = ff_get_audio_buffer(outlink, s->win_size * 2);
  183. if (!s->buffer)
  184. return AVERROR(ENOMEM);
  185. /* pre-calc windowing function */
  186. s->window_func_lut = av_realloc_f(s->window_func_lut, s->win_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->win_size, s->win_func, &overlap);
  191. if (s->overlap == 1)
  192. s->overlap = overlap;
  193. s->hop_size = (1 - s->overlap) * s->win_size;
  194. for (factor = 0, i = 0; i < s->win_size; i++) {
  195. factor += s->window_func_lut[i] * s->window_func_lut[i];
  196. }
  197. s->factor = (factor / s->win_size) / FFMAX(1 / (1 - s->overlap) - 1, 1);
  198. return 0;
  199. }
  200. static int request_frame(AVFilterLink *outlink)
  201. {
  202. AVFilterContext *ctx = outlink->src;
  203. SpectrumSynthContext *s = ctx->priv;
  204. int ret;
  205. if (!s->magnitude) {
  206. ret = ff_request_frame(ctx->inputs[0]);
  207. if (ret < 0)
  208. return ret;
  209. }
  210. if (!s->phase) {
  211. ret = ff_request_frame(ctx->inputs[1]);
  212. if (ret < 0)
  213. return ret;
  214. }
  215. return 0;
  216. }
  217. static void read16_fft_bin(SpectrumSynthContext *s,
  218. int x, int y, int f, int ch)
  219. {
  220. const int m_linesize = s->magnitude->linesize[0];
  221. const int p_linesize = s->phase->linesize[0];
  222. const uint16_t *m = (uint16_t *)(s->magnitude->data[0] + y * m_linesize);
  223. const uint16_t *p = (uint16_t *)(s->phase->data[0] + y * p_linesize);
  224. float magnitude, phase;
  225. switch (s->scale) {
  226. case LINEAR:
  227. magnitude = m[x] / (double)UINT16_MAX;
  228. break;
  229. case LOG:
  230. magnitude = ff_exp10(((m[x] / (double)UINT16_MAX) - 1.) * 6.);
  231. break;
  232. default:
  233. av_assert0(0);
  234. }
  235. phase = ((p[x] / (double)UINT16_MAX) * 2. - 1.) * M_PI;
  236. s->fft_data[ch][f].re = magnitude * cos(phase);
  237. s->fft_data[ch][f].im = magnitude * sin(phase);
  238. }
  239. static void read8_fft_bin(SpectrumSynthContext *s,
  240. int x, int y, int f, int ch)
  241. {
  242. const int m_linesize = s->magnitude->linesize[0];
  243. const int p_linesize = s->phase->linesize[0];
  244. const uint8_t *m = (uint8_t *)(s->magnitude->data[0] + y * m_linesize);
  245. const uint8_t *p = (uint8_t *)(s->phase->data[0] + y * p_linesize);
  246. float magnitude, phase;
  247. switch (s->scale) {
  248. case LINEAR:
  249. magnitude = m[x] / (double)UINT8_MAX;
  250. break;
  251. case LOG:
  252. magnitude = ff_exp10(((m[x] / (double)UINT8_MAX) - 1.) * 6.);
  253. break;
  254. default:
  255. av_assert0(0);
  256. }
  257. phase = ((p[x] / (double)UINT8_MAX) * 2. - 1.) * M_PI;
  258. s->fft_data[ch][f].re = magnitude * cos(phase);
  259. s->fft_data[ch][f].im = magnitude * sin(phase);
  260. }
  261. static void read_fft_data(AVFilterContext *ctx, int x, int h, int ch)
  262. {
  263. SpectrumSynthContext *s = ctx->priv;
  264. AVFilterLink *inlink = ctx->inputs[0];
  265. int start = h * (s->channels - ch) - 1;
  266. int end = h * (s->channels - ch - 1);
  267. int y, f;
  268. switch (s->orientation) {
  269. case VERTICAL:
  270. switch (inlink->format) {
  271. case AV_PIX_FMT_YUV444P16:
  272. case AV_PIX_FMT_GRAY16:
  273. for (y = start, f = 0; y >= end; y--, f++) {
  274. read16_fft_bin(s, x, y, f, ch);
  275. }
  276. break;
  277. case AV_PIX_FMT_YUVJ444P:
  278. case AV_PIX_FMT_YUV444P:
  279. case AV_PIX_FMT_GRAY8:
  280. for (y = start, f = 0; y >= end; y--, f++) {
  281. read8_fft_bin(s, x, y, f, ch);
  282. }
  283. break;
  284. }
  285. break;
  286. case HORIZONTAL:
  287. switch (inlink->format) {
  288. case AV_PIX_FMT_YUV444P16:
  289. case AV_PIX_FMT_GRAY16:
  290. for (y = end, f = 0; y <= start; y++, f++) {
  291. read16_fft_bin(s, y, x, f, ch);
  292. }
  293. break;
  294. case AV_PIX_FMT_YUVJ444P:
  295. case AV_PIX_FMT_YUV444P:
  296. case AV_PIX_FMT_GRAY8:
  297. for (y = end, f = 0; y <= start; y++, f++) {
  298. read8_fft_bin(s, y, x, f, ch);
  299. }
  300. break;
  301. }
  302. break;
  303. }
  304. }
  305. static void synth_window(AVFilterContext *ctx, int x)
  306. {
  307. SpectrumSynthContext *s = ctx->priv;
  308. const int h = s->size;
  309. int nb = s->win_size;
  310. int y, f, ch;
  311. for (ch = 0; ch < s->channels; ch++) {
  312. read_fft_data(ctx, x, h, ch);
  313. for (y = h; y <= s->nb_freq; y++) {
  314. s->fft_data[ch][y].re = 0;
  315. s->fft_data[ch][y].im = 0;
  316. }
  317. for (y = s->nb_freq + 1, f = s->nb_freq - 1; y < nb; y++, f--) {
  318. s->fft_data[ch][y].re = s->fft_data[ch][f].re;
  319. s->fft_data[ch][y].im = -s->fft_data[ch][f].im;
  320. }
  321. av_fft_permute(s->fft, s->fft_data[ch]);
  322. av_fft_calc(s->fft, s->fft_data[ch]);
  323. }
  324. }
  325. static int try_push_frame(AVFilterContext *ctx, int x)
  326. {
  327. SpectrumSynthContext *s = ctx->priv;
  328. AVFilterLink *outlink = ctx->outputs[0];
  329. const float factor = s->factor;
  330. int ch, n, i, ret;
  331. int start, end;
  332. AVFrame *out;
  333. synth_window(ctx, x);
  334. for (ch = 0; ch < s->channels; ch++) {
  335. float *buf = (float *)s->buffer->extended_data[ch];
  336. int j, k;
  337. start = s->start;
  338. end = s->end;
  339. k = end;
  340. for (i = 0, j = start; j < k && i < s->win_size; i++, j++) {
  341. buf[j] += s->fft_data[ch][i].re;
  342. }
  343. for (; i < s->win_size; i++, j++) {
  344. buf[j] = s->fft_data[ch][i].re;
  345. }
  346. start += s->hop_size;
  347. end = j;
  348. if (start >= s->win_size) {
  349. start -= s->win_size;
  350. end -= s->win_size;
  351. if (ch == s->channels - 1) {
  352. float *dst;
  353. int c;
  354. out = ff_get_audio_buffer(outlink, s->win_size);
  355. if (!out) {
  356. av_frame_free(&s->magnitude);
  357. av_frame_free(&s->phase);
  358. return AVERROR(ENOMEM);
  359. }
  360. out->pts = s->pts;
  361. s->pts += s->win_size;
  362. for (c = 0; c < s->channels; c++) {
  363. dst = (float *)out->extended_data[c];
  364. buf = (float *)s->buffer->extended_data[c];
  365. for (n = 0; n < s->win_size; n++) {
  366. dst[n] = buf[n] * factor;
  367. }
  368. memmove(buf, buf + s->win_size, s->win_size * 4);
  369. }
  370. ret = ff_filter_frame(outlink, out);
  371. if (ret < 0)
  372. return ret;
  373. }
  374. }
  375. }
  376. s->start = start;
  377. s->end = end;
  378. return 0;
  379. }
  380. static int try_push_frames(AVFilterContext *ctx)
  381. {
  382. SpectrumSynthContext *s = ctx->priv;
  383. int ret, x;
  384. if (!(s->magnitude && s->phase))
  385. return 0;
  386. switch (s->sliding) {
  387. case REPLACE:
  388. ret = try_push_frame(ctx, s->xpos);
  389. s->xpos++;
  390. if (s->xpos >= s->xend)
  391. s->xpos = 0;
  392. break;
  393. case SCROLL:
  394. s->xpos = s->xend - 1;
  395. ret = try_push_frame(ctx, s->xpos);
  396. break;
  397. case RSCROLL:
  398. s->xpos = 0;
  399. ret = try_push_frame(ctx, s->xpos);
  400. break;
  401. case FULLFRAME:
  402. for (x = 0; x < s->xend; x++) {
  403. ret = try_push_frame(ctx, x);
  404. if (ret < 0)
  405. break;
  406. }
  407. break;
  408. default:
  409. av_assert0(0);
  410. }
  411. av_frame_free(&s->magnitude);
  412. av_frame_free(&s->phase);
  413. return ret;
  414. }
  415. static int filter_frame_magnitude(AVFilterLink *inlink, AVFrame *magnitude)
  416. {
  417. AVFilterContext *ctx = inlink->dst;
  418. SpectrumSynthContext *s = ctx->priv;
  419. s->magnitude = magnitude;
  420. return try_push_frames(ctx);
  421. }
  422. static int filter_frame_phase(AVFilterLink *inlink, AVFrame *phase)
  423. {
  424. AVFilterContext *ctx = inlink->dst;
  425. SpectrumSynthContext *s = ctx->priv;
  426. s->phase = phase;
  427. return try_push_frames(ctx);
  428. }
  429. static av_cold void uninit(AVFilterContext *ctx)
  430. {
  431. SpectrumSynthContext *s = ctx->priv;
  432. int i;
  433. av_frame_free(&s->magnitude);
  434. av_frame_free(&s->phase);
  435. av_frame_free(&s->buffer);
  436. av_fft_end(s->fft);
  437. if (s->fft_data) {
  438. for (i = 0; i < s->channels; i++)
  439. av_freep(&s->fft_data[i]);
  440. }
  441. av_freep(&s->fft_data);
  442. av_freep(&s->window_func_lut);
  443. }
  444. static const AVFilterPad spectrumsynth_inputs[] = {
  445. {
  446. .name = "magnitude",
  447. .type = AVMEDIA_TYPE_VIDEO,
  448. .filter_frame = filter_frame_magnitude,
  449. .needs_fifo = 1,
  450. },
  451. {
  452. .name = "phase",
  453. .type = AVMEDIA_TYPE_VIDEO,
  454. .filter_frame = filter_frame_phase,
  455. .needs_fifo = 1,
  456. },
  457. { NULL }
  458. };
  459. static const AVFilterPad spectrumsynth_outputs[] = {
  460. {
  461. .name = "default",
  462. .type = AVMEDIA_TYPE_AUDIO,
  463. .config_props = config_output,
  464. .request_frame = request_frame,
  465. },
  466. { NULL }
  467. };
  468. AVFilter ff_vaf_spectrumsynth = {
  469. .name = "spectrumsynth",
  470. .description = NULL_IF_CONFIG_SMALL("Convert input spectrum videos to audio output."),
  471. .uninit = uninit,
  472. .query_formats = query_formats,
  473. .priv_size = sizeof(SpectrumSynthContext),
  474. .inputs = spectrumsynth_inputs,
  475. .outputs = spectrumsynth_outputs,
  476. .priv_class = &spectrumsynth_class,
  477. };