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.

327 lines
11KB

  1. /*
  2. * Copyright (c) 2012 Clément Bœsch
  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. * audio to spectrum (video) transmedia filter, based on ffplay rdft showmode
  23. * (by Michael Niedermayer) and lavfi/avf_showwaves (by Stefano Sabatini).
  24. */
  25. #include <math.h>
  26. #include "libavcodec/avfft.h"
  27. #include "libavutil/audioconvert.h"
  28. #include "libavutil/opt.h"
  29. #include "avfilter.h"
  30. #include "internal.h"
  31. typedef struct {
  32. const AVClass *class;
  33. int w, h;
  34. AVFilterBufferRef *outpicref;
  35. int req_fullfilled;
  36. int sliding; ///< 1 if sliding mode, 0 otherwise
  37. int xpos; ///< x position (current column)
  38. RDFTContext *rdft; ///< Real Discrete Fourier Transform context
  39. int rdft_bits; ///< number of bits (RDFT window size = 1<<rdft_bits)
  40. FFTSample *rdft_data; ///< bins holder for each (displayed) channels
  41. int filled; ///< number of samples (per channel) filled in current rdft_buffer
  42. int consumed; ///< number of samples (per channel) consumed from the input frame
  43. float *window_func_lut; ///< Window function LUT
  44. } ShowSpectrumContext;
  45. #define OFFSET(x) offsetof(ShowSpectrumContext, x)
  46. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  47. static const AVOption showspectrum_options[] = {
  48. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x480"}, 0, 0, FLAGS },
  49. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x480"}, 0, 0, FLAGS },
  50. { "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
  51. { NULL },
  52. };
  53. AVFILTER_DEFINE_CLASS(showspectrum);
  54. static av_cold int init(AVFilterContext *ctx, const char *args)
  55. {
  56. ShowSpectrumContext *showspectrum = ctx->priv;
  57. int err;
  58. showspectrum->class = &showspectrum_class;
  59. av_opt_set_defaults(showspectrum);
  60. if ((err = av_set_options_string(showspectrum, args, "=", ":")) < 0)
  61. return err;
  62. return 0;
  63. }
  64. static av_cold void uninit(AVFilterContext *ctx)
  65. {
  66. ShowSpectrumContext *showspectrum = ctx->priv;
  67. av_rdft_end(showspectrum->rdft);
  68. av_freep(&showspectrum->rdft_data);
  69. av_freep(&showspectrum->window_func_lut);
  70. avfilter_unref_bufferp(&showspectrum->outpicref);
  71. }
  72. static int query_formats(AVFilterContext *ctx)
  73. {
  74. AVFilterFormats *formats = NULL;
  75. AVFilterChannelLayouts *layouts = NULL;
  76. AVFilterLink *inlink = ctx->inputs[0];
  77. AVFilterLink *outlink = ctx->outputs[0];
  78. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16P, -1 };
  79. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGB24, -1 };
  80. /* set input audio formats */
  81. formats = ff_make_format_list(sample_fmts);
  82. if (!formats)
  83. return AVERROR(ENOMEM);
  84. ff_formats_ref(formats, &inlink->out_formats);
  85. layouts = ff_all_channel_layouts();
  86. if (!layouts)
  87. return AVERROR(ENOMEM);
  88. ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
  89. formats = ff_all_samplerates();
  90. if (!formats)
  91. return AVERROR(ENOMEM);
  92. ff_formats_ref(formats, &inlink->out_samplerates);
  93. /* set output video format */
  94. formats = ff_make_format_list(pix_fmts);
  95. if (!formats)
  96. return AVERROR(ENOMEM);
  97. ff_formats_ref(formats, &outlink->in_formats);
  98. return 0;
  99. }
  100. static int config_output(AVFilterLink *outlink)
  101. {
  102. AVFilterContext *ctx = outlink->src;
  103. ShowSpectrumContext *showspectrum = ctx->priv;
  104. int i, rdft_bits, win_size;
  105. outlink->w = showspectrum->w;
  106. outlink->h = showspectrum->h;
  107. /* RDFT window size (precision) according to the requested output frame height */
  108. for (rdft_bits = 1; 1<<rdft_bits < 2*outlink->h; rdft_bits++);
  109. win_size = 1 << rdft_bits;
  110. /* (re-)configuration if the video output changed (or first init) */
  111. if (rdft_bits != showspectrum->rdft_bits) {
  112. AVFilterBufferRef *outpicref;
  113. av_rdft_end(showspectrum->rdft);
  114. showspectrum->rdft = av_rdft_init(rdft_bits, DFT_R2C);
  115. showspectrum->rdft_bits = rdft_bits;
  116. /* RDFT buffers: x2 for each (display) channel buffer */
  117. showspectrum->rdft_data =
  118. av_realloc_f(showspectrum->rdft_data, 2 * win_size,
  119. sizeof(*showspectrum->rdft_data));
  120. if (!showspectrum->rdft_data)
  121. return AVERROR(ENOMEM);
  122. showspectrum->filled = 0;
  123. /* pre-calc windowing function (hann here) */
  124. showspectrum->window_func_lut =
  125. av_realloc_f(showspectrum->window_func_lut, win_size,
  126. sizeof(*showspectrum->window_func_lut));
  127. if (!showspectrum->window_func_lut)
  128. return AVERROR(ENOMEM);
  129. for (i = 0; i < win_size; i++)
  130. showspectrum->window_func_lut[i] = .5f * (1 - cos(2*M_PI*i / (win_size-1)));
  131. /* prepare the initial picref buffer (black frame) */
  132. avfilter_unref_bufferp(&showspectrum->outpicref);
  133. showspectrum->outpicref = outpicref =
  134. ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_PRESERVE|AV_PERM_REUSE2,
  135. outlink->w, outlink->h);
  136. if (!outpicref)
  137. return AVERROR(ENOMEM);
  138. outlink->sample_aspect_ratio = (AVRational){1,1};
  139. memset(outpicref->data[0], 0, outlink->h * outpicref->linesize[0]);
  140. }
  141. if (showspectrum->xpos >= outlink->w)
  142. showspectrum->xpos = 0;
  143. av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d RDFT window size:%d\n",
  144. showspectrum->w, showspectrum->h, win_size);
  145. return 0;
  146. }
  147. inline static void push_frame(AVFilterLink *outlink)
  148. {
  149. ShowSpectrumContext *showspectrum = outlink->src->priv;
  150. showspectrum->xpos++;
  151. if (showspectrum->xpos >= outlink->w)
  152. showspectrum->xpos = 0;
  153. showspectrum->filled = 0;
  154. showspectrum->req_fullfilled = 1;
  155. ff_start_frame(outlink, avfilter_ref_buffer(showspectrum->outpicref, ~AV_PERM_WRITE));
  156. ff_draw_slice(outlink, 0, outlink->h, 1);
  157. ff_end_frame(outlink);
  158. }
  159. static int request_frame(AVFilterLink *outlink)
  160. {
  161. ShowSpectrumContext *showspectrum = outlink->src->priv;
  162. AVFilterLink *inlink = outlink->src->inputs[0];
  163. int ret;
  164. showspectrum->req_fullfilled = 0;
  165. do {
  166. ret = ff_request_frame(inlink);
  167. } while (!showspectrum->req_fullfilled && ret >= 0);
  168. if (ret == AVERROR_EOF && showspectrum->outpicref)
  169. push_frame(outlink);
  170. return ret;
  171. }
  172. static int plot_spectrum_column(AVFilterLink *inlink, AVFilterBufferRef *insamples, int nb_samples)
  173. {
  174. AVFilterContext *ctx = inlink->dst;
  175. AVFilterLink *outlink = ctx->outputs[0];
  176. ShowSpectrumContext *showspectrum = ctx->priv;
  177. AVFilterBufferRef *outpicref = showspectrum->outpicref;
  178. const int nb_channels = av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
  179. /* nb_freq contains the power of two superior or equal to the output image
  180. * height (or half the RDFT window size) */
  181. const int nb_freq = 1 << (showspectrum->rdft_bits - 1);
  182. const int win_size = nb_freq << 1;
  183. int ch, n, y;
  184. FFTSample *data[2];
  185. const int nb_display_channels = FFMIN(nb_channels, 2);
  186. const int start = showspectrum->filled;
  187. const int add_samples = FFMIN(win_size - start, nb_samples);
  188. /* fill RDFT input with the number of samples available */
  189. for (ch = 0; ch < nb_display_channels; ch++) {
  190. const int16_t *p = (int16_t *)insamples->extended_data[ch];
  191. p += showspectrum->consumed;
  192. data[ch] = showspectrum->rdft_data + win_size * ch; // select channel buffer
  193. for (n = 0; n < add_samples; n++)
  194. data[ch][start + n] = p[n] * showspectrum->window_func_lut[start + n];
  195. }
  196. showspectrum->filled += add_samples;
  197. /* complete RDFT window size? */
  198. if (showspectrum->filled == win_size) {
  199. /* run RDFT on each samples set */
  200. for (ch = 0; ch < nb_display_channels; ch++)
  201. av_rdft_calc(showspectrum->rdft, data[ch]);
  202. /* fill a new spectrum column */
  203. #define RE(ch) data[ch][2*y + 0]
  204. #define IM(ch) data[ch][2*y + 1]
  205. #define MAGNITUDE(re, im) sqrt((re)*(re) + (im)*(im))
  206. for (y = 0; y < outlink->h; y++) {
  207. // FIXME: bin[0] contains first and last bins
  208. uint8_t *p = outpicref->data[0] + (outlink->h - y - 1) * outpicref->linesize[0];
  209. const double w = 1. / sqrt(nb_freq);
  210. int a = sqrt(w * MAGNITUDE(RE(0), IM(0)));
  211. int b = nb_display_channels > 1 ? sqrt(w * MAGNITUDE(RE(1), IM(1))) : a;
  212. if (showspectrum->sliding) {
  213. memmove(p, p + 3, (outlink->w - 1) * 3);
  214. p += (outlink->w - 1) * 3;
  215. } else {
  216. p += showspectrum->xpos * 3;
  217. }
  218. a = FFMIN(a, 255);
  219. b = FFMIN(b, 255);
  220. p[0] = a;
  221. p[1] = b;
  222. p[2] = (a + b) / 2;
  223. }
  224. outpicref->pts = insamples->pts +
  225. av_rescale_q(showspectrum->consumed,
  226. (AVRational){ 1, inlink->sample_rate },
  227. outlink->time_base);
  228. push_frame(outlink);
  229. }
  230. return add_samples;
  231. }
  232. static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  233. {
  234. AVFilterContext *ctx = inlink->dst;
  235. ShowSpectrumContext *showspectrum = ctx->priv;
  236. int left_samples = insamples->audio->nb_samples;
  237. showspectrum->consumed = 0;
  238. while (left_samples) {
  239. const int added_samples = plot_spectrum_column(inlink, insamples, left_samples);
  240. showspectrum->consumed += added_samples;
  241. left_samples -= added_samples;
  242. }
  243. avfilter_unref_buffer(insamples);
  244. return 0;
  245. }
  246. AVFilter avfilter_avf_showspectrum = {
  247. .name = "showspectrum",
  248. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
  249. .init = init,
  250. .uninit = uninit,
  251. .query_formats = query_formats,
  252. .priv_size = sizeof(ShowSpectrumContext),
  253. .inputs = (const AVFilterPad[]) {
  254. {
  255. .name = "default",
  256. .type = AVMEDIA_TYPE_AUDIO,
  257. .filter_samples = filter_samples,
  258. .min_perms = AV_PERM_READ,
  259. },
  260. { .name = NULL }
  261. },
  262. .outputs = (const AVFilterPad[]) {
  263. {
  264. .name = "default",
  265. .type = AVMEDIA_TYPE_VIDEO,
  266. .config_props = config_output,
  267. .request_frame = request_frame,
  268. },
  269. { .name = NULL }
  270. },
  271. .priv_class = &showspectrum_class,
  272. };