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.

594 lines
24KB

  1. /*
  2. * Copyright (c) 2012-2013 Clément Bœsch
  3. * Copyright (c) 2013 Rudolf Polzer <divverent@xonotic.org>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * audio to spectrum (video) transmedia filter, based on ffplay rdft showmode
  24. * (by Michael Niedermayer) and lavfi/avf_showwaves (by Stefano Sabatini).
  25. */
  26. #include <math.h>
  27. #include "libavcodec/avfft.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/channel_layout.h"
  30. #include "libavutil/opt.h"
  31. #include "avfilter.h"
  32. #include "internal.h"
  33. #include "window_func.h"
  34. enum DisplayMode { COMBINED, SEPARATE, NB_MODES };
  35. enum DisplayScale { LINEAR, SQRT, CBRT, LOG, NB_SCALES };
  36. enum ColorMode { CHANNEL, INTENSITY, NB_CLMODES };
  37. enum SlideMode { REPLACE, SCROLL, FULLFRAME, RSCROLL, NB_SLIDES };
  38. enum Orientation { VERTICAL, HORIZONTAL, NB_ORIENTATIONS };
  39. typedef struct {
  40. const AVClass *class;
  41. int w, h;
  42. AVFrame *outpicref;
  43. int nb_display_channels;
  44. int orientation;
  45. int channel_width;
  46. int channel_height;
  47. int sliding; ///< 1 if sliding mode, 0 otherwise
  48. int mode; ///< channel display mode
  49. int color_mode; ///< display color scheme
  50. int scale;
  51. float saturation; ///< color saturation multiplier
  52. int xpos; ///< x position (current column)
  53. RDFTContext *rdft; ///< Real Discrete Fourier Transform context
  54. int rdft_bits; ///< number of bits (RDFT window size = 1<<rdft_bits)
  55. FFTSample **rdft_data; ///< bins holder for each (displayed) channels
  56. float *window_func_lut; ///< Window function LUT
  57. int win_func;
  58. double win_scale;
  59. float overlap;
  60. float *combine_buffer; ///< color combining buffer (3 * h items)
  61. } ShowSpectrumContext;
  62. #define OFFSET(x) offsetof(ShowSpectrumContext, x)
  63. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  64. static const AVOption showspectrum_options[] = {
  65. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
  66. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
  67. { "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_SLIDES-1, FLAGS, "slide" },
  68. { "replace", "replace old columns with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, FLAGS, "slide" },
  69. { "scroll", "scroll from right to left", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" },
  70. { "rscroll", "scroll from left to right", 0, AV_OPT_TYPE_CONST, {.i64=RSCROLL}, 0, 0, FLAGS, "slide" },
  71. { "fullframe", "return full frames", 0, AV_OPT_TYPE_CONST, {.i64=FULLFRAME}, 0, 0, FLAGS, "slide" },
  72. { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, COMBINED, NB_MODES-1, FLAGS, "mode" },
  73. { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
  74. { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
  75. { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=CHANNEL}, CHANNEL, NB_CLMODES-1, FLAGS, "color" },
  76. { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
  77. { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
  78. { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=SQRT}, LINEAR, NB_SCALES-1, FLAGS, "scale" },
  79. { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
  80. { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
  81. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
  82. { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
  83. { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
  84. { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
  85. { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
  86. { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
  87. { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
  88. { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
  89. { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
  90. { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
  91. { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
  92. { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
  93. { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
  94. { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
  95. { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
  96. { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
  97. { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
  98. { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
  99. { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
  100. { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
  101. { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
  102. { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
  103. { NULL }
  104. };
  105. AVFILTER_DEFINE_CLASS(showspectrum);
  106. static const struct {
  107. float a, y, u, v;
  108. } intensity_color_table[] = {
  109. { 0, 0, 0, 0 },
  110. { 0.13, .03587126228984074, .1573300977624594, -.02548747583751842 },
  111. { 0.30, .18572281794568020, .1772436246393981, .17475554840414750 },
  112. { 0.60, .28184980583656130, -.1593064119945782, .47132074554608920 },
  113. { 0.73, .65830621175547810, -.3716070802232764, .24352759331252930 },
  114. { 0.78, .76318535758242900, -.4307467689263783, .16866496622310430 },
  115. { 0.91, .95336363636363640, -.2045454545454546, .03313636363636363 },
  116. { 1, 1, 0, 0 }
  117. };
  118. static av_cold void uninit(AVFilterContext *ctx)
  119. {
  120. ShowSpectrumContext *s = ctx->priv;
  121. int i;
  122. av_freep(&s->combine_buffer);
  123. av_rdft_end(s->rdft);
  124. for (i = 0; i < s->nb_display_channels; i++)
  125. av_freep(&s->rdft_data[i]);
  126. av_freep(&s->rdft_data);
  127. av_freep(&s->window_func_lut);
  128. av_frame_free(&s->outpicref);
  129. }
  130. static int query_formats(AVFilterContext *ctx)
  131. {
  132. AVFilterFormats *formats = NULL;
  133. AVFilterChannelLayouts *layouts = NULL;
  134. AVFilterLink *inlink = ctx->inputs[0];
  135. AVFilterLink *outlink = ctx->outputs[0];
  136. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_NONE };
  137. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE };
  138. int ret;
  139. /* set input audio formats */
  140. formats = ff_make_format_list(sample_fmts);
  141. if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
  142. return ret;
  143. layouts = ff_all_channel_layouts();
  144. if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
  145. return ret;
  146. formats = ff_all_samplerates();
  147. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  148. return ret;
  149. /* set output video format */
  150. formats = ff_make_format_list(pix_fmts);
  151. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  152. return ret;
  153. return 0;
  154. }
  155. static int config_output(AVFilterLink *outlink)
  156. {
  157. AVFilterContext *ctx = outlink->src;
  158. AVFilterLink *inlink = ctx->inputs[0];
  159. ShowSpectrumContext *s = ctx->priv;
  160. int i, rdft_bits, win_size, h, w;
  161. outlink->w = s->w;
  162. outlink->h = s->h;
  163. h = (s->mode == COMBINED || s->orientation == HORIZONTAL) ? outlink->h : outlink->h / inlink->channels;
  164. w = (s->mode == COMBINED || s->orientation == VERTICAL) ? outlink->w : outlink->w / inlink->channels;
  165. s->channel_height = h;
  166. s->channel_width = w;
  167. if (s->orientation == VERTICAL) {
  168. /* RDFT window size (precision) according to the requested output frame height */
  169. for (rdft_bits = 1; 1 << rdft_bits < 2 * h; rdft_bits++);
  170. } else {
  171. /* RDFT window size (precision) according to the requested output frame width */
  172. for (rdft_bits = 1; 1 << rdft_bits < 2 * w; rdft_bits++);
  173. }
  174. win_size = 1 << rdft_bits;
  175. /* (re-)configuration if the video output changed (or first init) */
  176. if (rdft_bits != s->rdft_bits) {
  177. AVFrame *outpicref;
  178. av_rdft_end(s->rdft);
  179. s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
  180. if (!s->rdft) {
  181. av_log(ctx, AV_LOG_ERROR, "Unable to create RDFT context. "
  182. "The window size might be too high.\n");
  183. return AVERROR(EINVAL);
  184. }
  185. s->rdft_bits = rdft_bits;
  186. /* RDFT buffers: x2 for each (display) channel buffer.
  187. * Note: we use free and malloc instead of a realloc-like function to
  188. * make sure the buffer is aligned in memory for the FFT functions. */
  189. for (i = 0; i < s->nb_display_channels; i++)
  190. av_freep(&s->rdft_data[i]);
  191. av_freep(&s->rdft_data);
  192. s->nb_display_channels = inlink->channels;
  193. s->rdft_data = av_calloc(s->nb_display_channels, sizeof(*s->rdft_data));
  194. if (!s->rdft_data)
  195. return AVERROR(ENOMEM);
  196. for (i = 0; i < s->nb_display_channels; i++) {
  197. s->rdft_data[i] = av_calloc(win_size, sizeof(**s->rdft_data));
  198. if (!s->rdft_data[i])
  199. return AVERROR(ENOMEM);
  200. }
  201. /* pre-calc windowing function */
  202. s->window_func_lut =
  203. av_realloc_f(s->window_func_lut, win_size,
  204. sizeof(*s->window_func_lut));
  205. if (!s->window_func_lut)
  206. return AVERROR(ENOMEM);
  207. ff_generate_window_func(s->window_func_lut, win_size, s->win_func, &s->overlap);
  208. for (s->win_scale = 0, i = 0; i < win_size; i++) {
  209. s->win_scale += s->window_func_lut[i] * s->window_func_lut[i];
  210. }
  211. s->win_scale = 1. / (sqrt(s->win_scale) * 32768.);
  212. /* prepare the initial picref buffer (black frame) */
  213. av_frame_free(&s->outpicref);
  214. s->outpicref = outpicref =
  215. ff_get_video_buffer(outlink, outlink->w, outlink->h);
  216. if (!outpicref)
  217. return AVERROR(ENOMEM);
  218. outlink->sample_aspect_ratio = (AVRational){1,1};
  219. for (i = 0; i < outlink->h; i++) {
  220. memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w);
  221. memset(outpicref->data[1] + i * outpicref->linesize[1], 128, outlink->w);
  222. memset(outpicref->data[2] + i * outpicref->linesize[2], 128, outlink->w);
  223. }
  224. }
  225. if ((s->orientation == VERTICAL && s->xpos >= outlink->w) ||
  226. (s->orientation == HORIZONTAL && s->xpos >= outlink->h))
  227. s->xpos = 0;
  228. outlink->frame_rate = av_make_q(inlink->sample_rate, win_size);
  229. if (s->orientation == VERTICAL && s->sliding == FULLFRAME)
  230. outlink->frame_rate.den *= outlink->w;
  231. if (s->orientation == HORIZONTAL && s->sliding == FULLFRAME)
  232. outlink->frame_rate.den *= outlink->h;
  233. inlink->min_samples = inlink->max_samples = inlink->partial_buf_size =
  234. win_size;
  235. if (s->orientation == VERTICAL) {
  236. s->combine_buffer =
  237. av_realloc_f(s->combine_buffer, outlink->h * 3,
  238. sizeof(*s->combine_buffer));
  239. } else {
  240. s->combine_buffer =
  241. av_realloc_f(s->combine_buffer, outlink->w * 3,
  242. sizeof(*s->combine_buffer));
  243. }
  244. av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d RDFT window size:%d\n",
  245. s->w, s->h, win_size);
  246. return 0;
  247. }
  248. static int request_frame(AVFilterLink *outlink)
  249. {
  250. ShowSpectrumContext *s = outlink->src->priv;
  251. AVFilterLink *inlink = outlink->src->inputs[0];
  252. unsigned i;
  253. int ret;
  254. ret = ff_request_frame(inlink);
  255. if (ret == AVERROR_EOF && s->sliding == FULLFRAME && s->xpos > 0 &&
  256. s->outpicref) {
  257. if (s->orientation == VERTICAL) {
  258. for (i = 0; i < outlink->h; i++) {
  259. memset(s->outpicref->data[0] + i * s->outpicref->linesize[0] + s->xpos, 0, outlink->w - s->xpos);
  260. memset(s->outpicref->data[1] + i * s->outpicref->linesize[1] + s->xpos, 128, outlink->w - s->xpos);
  261. memset(s->outpicref->data[2] + i * s->outpicref->linesize[2] + s->xpos, 128, outlink->w - s->xpos);
  262. }
  263. } else {
  264. for (i = s->xpos; i < outlink->h; i++) {
  265. memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w);
  266. memset(s->outpicref->data[1] + i * s->outpicref->linesize[1], 128, outlink->w);
  267. memset(s->outpicref->data[2] + i * s->outpicref->linesize[2], 128, outlink->w);
  268. }
  269. }
  270. ret = ff_filter_frame(outlink, s->outpicref);
  271. s->outpicref = NULL;
  272. }
  273. return ret;
  274. }
  275. static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
  276. {
  277. int ret;
  278. AVFilterContext *ctx = inlink->dst;
  279. AVFilterLink *outlink = ctx->outputs[0];
  280. ShowSpectrumContext *s = ctx->priv;
  281. AVFrame *outpicref = s->outpicref;
  282. /* nb_freq contains the power of two superior or equal to the output image
  283. * height (or half the RDFT window size) */
  284. const int nb_freq = 1 << (s->rdft_bits - 1);
  285. const int win_size = nb_freq << 1;
  286. const double w = s->win_scale;
  287. int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
  288. int ch, plane, n, x, y;
  289. av_assert0(insamples->nb_samples == win_size);
  290. /* fill RDFT input with the number of samples available */
  291. for (ch = 0; ch < s->nb_display_channels; ch++) {
  292. const int16_t *p = (int16_t *)insamples->extended_data[ch];
  293. for (n = 0; n < win_size; n++)
  294. s->rdft_data[ch][n] = p[n] * s->window_func_lut[n];
  295. }
  296. /* run RDFT on each samples set */
  297. for (ch = 0; ch < s->nb_display_channels; ch++)
  298. av_rdft_calc(s->rdft, s->rdft_data[ch]);
  299. /* fill a new spectrum column */
  300. #define RE(y, ch) s->rdft_data[ch][2 * (y) + 0]
  301. #define IM(y, ch) s->rdft_data[ch][2 * (y) + 1]
  302. #define MAGNITUDE(y, ch) hypot(RE(y, ch), IM(y, ch))
  303. /* initialize buffer for combining to black */
  304. if (s->orientation == VERTICAL) {
  305. for (y = 0; y < outlink->h; y++) {
  306. s->combine_buffer[3 * y ] = 0;
  307. s->combine_buffer[3 * y + 1] = 127.5;
  308. s->combine_buffer[3 * y + 2] = 127.5;
  309. }
  310. } else {
  311. for (y = 0; y < outlink->w; y++) {
  312. s->combine_buffer[3 * y ] = 0;
  313. s->combine_buffer[3 * y + 1] = 127.5;
  314. s->combine_buffer[3 * y + 2] = 127.5;
  315. }
  316. }
  317. for (ch = 0; ch < s->nb_display_channels; ch++) {
  318. float yf, uf, vf;
  319. /* decide color range */
  320. switch (s->mode) {
  321. case COMBINED:
  322. // reduce range by channel count
  323. yf = 256.0f / s->nb_display_channels;
  324. switch (s->color_mode) {
  325. case INTENSITY:
  326. uf = yf;
  327. vf = yf;
  328. break;
  329. case CHANNEL:
  330. /* adjust saturation for mixed UV coloring */
  331. /* this factor is correct for infinite channels, an approximation otherwise */
  332. uf = yf * M_PI;
  333. vf = yf * M_PI;
  334. break;
  335. default:
  336. av_assert0(0);
  337. }
  338. break;
  339. case SEPARATE:
  340. // full range
  341. yf = 256.0f;
  342. uf = 256.0f;
  343. vf = 256.0f;
  344. break;
  345. default:
  346. av_assert0(0);
  347. }
  348. if (s->color_mode == CHANNEL) {
  349. if (s->nb_display_channels > 1) {
  350. uf *= 0.5 * sin((2 * M_PI * ch) / s->nb_display_channels);
  351. vf *= 0.5 * cos((2 * M_PI * ch) / s->nb_display_channels);
  352. } else {
  353. uf = 0.0f;
  354. vf = 0.0f;
  355. }
  356. }
  357. uf *= s->saturation;
  358. vf *= s->saturation;
  359. /* draw the channel */
  360. for (y = 0; y < h; y++) {
  361. int row = (s->mode == COMBINED) ? y : ch * h + y;
  362. float *out = &s->combine_buffer[3 * row];
  363. /* get magnitude */
  364. float a = w * MAGNITUDE(y, ch);
  365. /* apply scale */
  366. switch (s->scale) {
  367. case LINEAR:
  368. break;
  369. case SQRT:
  370. a = sqrt(a);
  371. break;
  372. case CBRT:
  373. a = cbrt(a);
  374. break;
  375. case LOG:
  376. a = 1 + log10(FFMAX(FFMIN(1, a), 1e-6)) / 5; // zero = -120dBFS
  377. break;
  378. default:
  379. av_assert0(0);
  380. }
  381. if (s->color_mode == INTENSITY) {
  382. float y, u, v;
  383. int i;
  384. for (i = 1; i < FF_ARRAY_ELEMS(intensity_color_table) - 1; i++)
  385. if (intensity_color_table[i].a >= a)
  386. break;
  387. // i now is the first item >= the color
  388. // now we know to interpolate between item i - 1 and i
  389. if (a <= intensity_color_table[i - 1].a) {
  390. y = intensity_color_table[i - 1].y;
  391. u = intensity_color_table[i - 1].u;
  392. v = intensity_color_table[i - 1].v;
  393. } else if (a >= intensity_color_table[i].a) {
  394. y = intensity_color_table[i].y;
  395. u = intensity_color_table[i].u;
  396. v = intensity_color_table[i].v;
  397. } else {
  398. float start = intensity_color_table[i - 1].a;
  399. float end = intensity_color_table[i].a;
  400. float lerpfrac = (a - start) / (end - start);
  401. y = intensity_color_table[i - 1].y * (1.0f - lerpfrac)
  402. + intensity_color_table[i].y * lerpfrac;
  403. u = intensity_color_table[i - 1].u * (1.0f - lerpfrac)
  404. + intensity_color_table[i].u * lerpfrac;
  405. v = intensity_color_table[i - 1].v * (1.0f - lerpfrac)
  406. + intensity_color_table[i].v * lerpfrac;
  407. }
  408. out[0] += y * yf;
  409. out[1] += u * uf;
  410. out[2] += v * vf;
  411. } else {
  412. out[0] += a * yf;
  413. out[1] += a * uf;
  414. out[2] += a * vf;
  415. }
  416. }
  417. }
  418. /* copy to output */
  419. if (s->orientation == VERTICAL) {
  420. if (s->sliding == SCROLL) {
  421. for (plane = 0; plane < 3; plane++) {
  422. for (y = 0; y < outlink->h; y++) {
  423. uint8_t *p = outpicref->data[plane] +
  424. y * outpicref->linesize[plane];
  425. memmove(p, p + 1, outlink->w - 1);
  426. }
  427. }
  428. s->xpos = outlink->w - 1;
  429. } else if (s->sliding == RSCROLL) {
  430. for (plane = 0; plane < 3; plane++) {
  431. for (y = 0; y < outlink->h; y++) {
  432. uint8_t *p = outpicref->data[plane] +
  433. y * outpicref->linesize[plane];
  434. memmove(p + 1, p, outlink->w - 1);
  435. }
  436. }
  437. s->xpos = 0;
  438. }
  439. for (plane = 0; plane < 3; plane++) {
  440. uint8_t *p = outpicref->data[plane] +
  441. (outlink->h - 1) * outpicref->linesize[plane] +
  442. s->xpos;
  443. for (y = 0; y < outlink->h; y++) {
  444. *p = lrint(FFMAX(0, FFMIN(s->combine_buffer[3 * y + plane], 255)));
  445. p -= outpicref->linesize[plane];
  446. }
  447. }
  448. } else {
  449. if (s->sliding == SCROLL) {
  450. for (plane = 0; plane < 3; plane++) {
  451. for (y = 1; y < outlink->h; y++) {
  452. memmove(outpicref->data[plane] + (y-1) * outpicref->linesize[plane],
  453. outpicref->data[plane] + (y ) * outpicref->linesize[plane],
  454. outlink->w);
  455. }
  456. }
  457. s->xpos = outlink->h - 1;
  458. } else if (s->sliding == RSCROLL) {
  459. for (plane = 0; plane < 3; plane++) {
  460. for (y = outlink->h - 1; y >= 1; y--) {
  461. memmove(outpicref->data[plane] + (y ) * outpicref->linesize[plane],
  462. outpicref->data[plane] + (y-1) * outpicref->linesize[plane],
  463. outlink->w);
  464. }
  465. }
  466. s->xpos = 0;
  467. }
  468. for (plane = 0; plane < 3; plane++) {
  469. uint8_t *p = outpicref->data[plane] +
  470. s->xpos * outpicref->linesize[plane];
  471. for (x = 0; x < outlink->w; x++) {
  472. *p = lrint(FFMAX(0, FFMIN(s->combine_buffer[3 * x + plane], 255)));
  473. p++;
  474. }
  475. }
  476. }
  477. if (s->sliding != FULLFRAME || s->xpos == 0)
  478. outpicref->pts = insamples->pts;
  479. s->xpos++;
  480. if (s->orientation == VERTICAL && s->xpos >= outlink->w)
  481. s->xpos = 0;
  482. if (s->orientation == HORIZONTAL && s->xpos >= outlink->h)
  483. s->xpos = 0;
  484. if (s->sliding != FULLFRAME || s->xpos == 0) {
  485. ret = ff_filter_frame(outlink, av_frame_clone(s->outpicref));
  486. if (ret < 0)
  487. return ret;
  488. }
  489. return win_size;
  490. }
  491. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  492. {
  493. AVFilterContext *ctx = inlink->dst;
  494. ShowSpectrumContext *s = ctx->priv;
  495. unsigned win_size = 1 << s->rdft_bits;
  496. int ret = 0;
  497. av_assert0(insamples->nb_samples <= win_size);
  498. if (insamples->nb_samples == win_size)
  499. ret = plot_spectrum_column(inlink, insamples);
  500. av_frame_free(&insamples);
  501. return ret;
  502. }
  503. static const AVFilterPad showspectrum_inputs[] = {
  504. {
  505. .name = "default",
  506. .type = AVMEDIA_TYPE_AUDIO,
  507. .filter_frame = filter_frame,
  508. },
  509. { NULL }
  510. };
  511. static const AVFilterPad showspectrum_outputs[] = {
  512. {
  513. .name = "default",
  514. .type = AVMEDIA_TYPE_VIDEO,
  515. .config_props = config_output,
  516. .request_frame = request_frame,
  517. },
  518. { NULL }
  519. };
  520. AVFilter ff_avf_showspectrum = {
  521. .name = "showspectrum",
  522. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
  523. .uninit = uninit,
  524. .query_formats = query_formats,
  525. .priv_size = sizeof(ShowSpectrumContext),
  526. .inputs = showspectrum_inputs,
  527. .outputs = showspectrum_outputs,
  528. .priv_class = &showspectrum_class,
  529. };