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.

541 lines
18KB

  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. default:
  232. av_assert0(0);
  233. }
  234. phase = ((p[x] / (double)UINT16_MAX) * 2. - 1.) * M_PI;
  235. s->fft_data[ch][f].re = magnitude * cos(phase);
  236. s->fft_data[ch][f].im = magnitude * sin(phase);
  237. }
  238. static void read8_fft_bin(SpectrumSynthContext *s,
  239. int x, int y, int f, int ch)
  240. {
  241. const int m_linesize = s->magnitude->linesize[0];
  242. const int p_linesize = s->phase->linesize[0];
  243. const uint8_t *m = (uint8_t *)(s->magnitude->data[0] + y * m_linesize);
  244. const uint8_t *p = (uint8_t *)(s->phase->data[0] + y * p_linesize);
  245. float magnitude, phase;
  246. switch (s->scale) {
  247. case LINEAR:
  248. magnitude = m[x] / (double)UINT8_MAX;
  249. break;
  250. case LOG:
  251. magnitude = ff_exp10(((m[x] / (double)UINT8_MAX) - 1.) * 6.);
  252. break;
  253. default:
  254. av_assert0(0);
  255. }
  256. phase = ((p[x] / (double)UINT8_MAX) * 2. - 1.) * M_PI;
  257. s->fft_data[ch][f].re = magnitude * cos(phase);
  258. s->fft_data[ch][f].im = magnitude * sin(phase);
  259. }
  260. static void read_fft_data(AVFilterContext *ctx, int x, int h, int ch)
  261. {
  262. SpectrumSynthContext *s = ctx->priv;
  263. AVFilterLink *inlink = ctx->inputs[0];
  264. int start = h * (s->channels - ch) - 1;
  265. int end = h * (s->channels - ch - 1);
  266. int y, f;
  267. switch (s->orientation) {
  268. case VERTICAL:
  269. switch (inlink->format) {
  270. case AV_PIX_FMT_YUV444P16:
  271. case AV_PIX_FMT_GRAY16:
  272. for (y = start, f = 0; y >= end; y--, f++) {
  273. read16_fft_bin(s, x, y, f, ch);
  274. }
  275. break;
  276. case AV_PIX_FMT_YUVJ444P:
  277. case AV_PIX_FMT_YUV444P:
  278. case AV_PIX_FMT_GRAY8:
  279. for (y = start, f = 0; y >= end; y--, f++) {
  280. read8_fft_bin(s, x, y, f, ch);
  281. }
  282. break;
  283. }
  284. break;
  285. case HORIZONTAL:
  286. switch (inlink->format) {
  287. case AV_PIX_FMT_YUV444P16:
  288. case AV_PIX_FMT_GRAY16:
  289. for (y = end, f = 0; y <= start; y++, f++) {
  290. read16_fft_bin(s, y, x, f, ch);
  291. }
  292. break;
  293. case AV_PIX_FMT_YUVJ444P:
  294. case AV_PIX_FMT_YUV444P:
  295. case AV_PIX_FMT_GRAY8:
  296. for (y = end, f = 0; y <= start; y++, f++) {
  297. read8_fft_bin(s, y, x, f, ch);
  298. }
  299. break;
  300. }
  301. break;
  302. }
  303. }
  304. static void synth_window(AVFilterContext *ctx, int x)
  305. {
  306. SpectrumSynthContext *s = ctx->priv;
  307. const int h = s->size;
  308. int nb = s->win_size;
  309. int y, f, ch;
  310. for (ch = 0; ch < s->channels; ch++) {
  311. read_fft_data(ctx, x, h, ch);
  312. for (y = h; y <= s->nb_freq; y++) {
  313. s->fft_data[ch][y].re = 0;
  314. s->fft_data[ch][y].im = 0;
  315. }
  316. for (y = s->nb_freq + 1, f = s->nb_freq - 1; y < nb; y++, f--) {
  317. s->fft_data[ch][y].re = s->fft_data[ch][f].re;
  318. s->fft_data[ch][y].im = -s->fft_data[ch][f].im;
  319. }
  320. av_fft_permute(s->fft, s->fft_data[ch]);
  321. av_fft_calc(s->fft, s->fft_data[ch]);
  322. }
  323. }
  324. static int try_push_frame(AVFilterContext *ctx, int x)
  325. {
  326. SpectrumSynthContext *s = ctx->priv;
  327. AVFilterLink *outlink = ctx->outputs[0];
  328. const float factor = s->factor;
  329. int ch, n, i, ret;
  330. int start, end;
  331. AVFrame *out;
  332. synth_window(ctx, x);
  333. for (ch = 0; ch < s->channels; ch++) {
  334. float *buf = (float *)s->buffer->extended_data[ch];
  335. int j, k;
  336. start = s->start;
  337. end = s->end;
  338. k = end;
  339. for (i = 0, j = start; j < k && i < s->win_size; i++, j++) {
  340. buf[j] += s->fft_data[ch][i].re;
  341. }
  342. for (; i < s->win_size; i++, j++) {
  343. buf[j] = s->fft_data[ch][i].re;
  344. }
  345. start += s->hop_size;
  346. end = j;
  347. if (start >= s->win_size) {
  348. start -= s->win_size;
  349. end -= s->win_size;
  350. if (ch == s->channels - 1) {
  351. float *dst;
  352. int c;
  353. out = ff_get_audio_buffer(outlink, s->win_size);
  354. if (!out) {
  355. av_frame_free(&s->magnitude);
  356. av_frame_free(&s->phase);
  357. return AVERROR(ENOMEM);
  358. }
  359. out->pts = s->pts;
  360. s->pts += s->win_size;
  361. for (c = 0; c < s->channels; c++) {
  362. dst = (float *)out->extended_data[c];
  363. buf = (float *)s->buffer->extended_data[c];
  364. for (n = 0; n < s->win_size; n++) {
  365. dst[n] = buf[n] * factor;
  366. }
  367. memmove(buf, buf + s->win_size, s->win_size * 4);
  368. }
  369. ret = ff_filter_frame(outlink, out);
  370. }
  371. }
  372. }
  373. s->start = start;
  374. s->end = end;
  375. return 0;
  376. }
  377. static int try_push_frames(AVFilterContext *ctx)
  378. {
  379. SpectrumSynthContext *s = ctx->priv;
  380. int ret, x;
  381. if (!(s->magnitude && s->phase))
  382. return 0;
  383. switch (s->sliding) {
  384. case REPLACE:
  385. ret = try_push_frame(ctx, s->xpos);
  386. s->xpos++;
  387. if (s->xpos >= s->xend)
  388. s->xpos = 0;
  389. break;
  390. case SCROLL:
  391. s->xpos = s->xend - 1;
  392. ret = try_push_frame(ctx, s->xpos);
  393. break;
  394. case RSCROLL:
  395. s->xpos = 0;
  396. ret = try_push_frame(ctx, s->xpos);
  397. break;
  398. case FULLFRAME:
  399. for (x = 0; x < s->xend; x++) {
  400. ret = try_push_frame(ctx, x);
  401. if (ret < 0)
  402. break;
  403. }
  404. break;
  405. default:
  406. av_assert0(0);
  407. }
  408. av_frame_free(&s->magnitude);
  409. av_frame_free(&s->phase);
  410. return ret;
  411. }
  412. static int filter_frame_magnitude(AVFilterLink *inlink, AVFrame *magnitude)
  413. {
  414. AVFilterContext *ctx = inlink->dst;
  415. SpectrumSynthContext *s = ctx->priv;
  416. s->magnitude = magnitude;
  417. return try_push_frames(ctx);
  418. }
  419. static int filter_frame_phase(AVFilterLink *inlink, AVFrame *phase)
  420. {
  421. AVFilterContext *ctx = inlink->dst;
  422. SpectrumSynthContext *s = ctx->priv;
  423. s->phase = phase;
  424. return try_push_frames(ctx);
  425. }
  426. static av_cold void uninit(AVFilterContext *ctx)
  427. {
  428. SpectrumSynthContext *s = ctx->priv;
  429. int i;
  430. av_frame_free(&s->magnitude);
  431. av_frame_free(&s->phase);
  432. av_frame_free(&s->buffer);
  433. av_fft_end(s->fft);
  434. if (s->fft_data) {
  435. for (i = 0; i < s->channels; i++)
  436. av_freep(&s->fft_data[i]);
  437. }
  438. av_freep(&s->fft_data);
  439. av_freep(&s->window_func_lut);
  440. }
  441. static const AVFilterPad spectrumsynth_inputs[] = {
  442. {
  443. .name = "magnitude",
  444. .type = AVMEDIA_TYPE_VIDEO,
  445. .filter_frame = filter_frame_magnitude,
  446. .needs_fifo = 1,
  447. },
  448. {
  449. .name = "phase",
  450. .type = AVMEDIA_TYPE_VIDEO,
  451. .filter_frame = filter_frame_phase,
  452. .needs_fifo = 1,
  453. },
  454. { NULL }
  455. };
  456. static const AVFilterPad spectrumsynth_outputs[] = {
  457. {
  458. .name = "default",
  459. .type = AVMEDIA_TYPE_AUDIO,
  460. .config_props = config_output,
  461. .request_frame = request_frame,
  462. },
  463. { NULL }
  464. };
  465. AVFilter ff_vaf_spectrumsynth = {
  466. .name = "spectrumsynth",
  467. .description = NULL_IF_CONFIG_SMALL("Convert input spectrum videos to audio output."),
  468. .uninit = uninit,
  469. .query_formats = query_formats,
  470. .priv_size = sizeof(SpectrumSynthContext),
  471. .inputs = spectrumsynth_inputs,
  472. .outputs = spectrumsynth_outputs,
  473. .priv_class = &spectrumsynth_class,
  474. };