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.

535 lines
17KB

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