FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
avf_showspectrum.c
Go to the documentation of this file.
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 /**
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 
27 #include <math.h>
28 
29 #include "libavcodec/avfft.h"
31 #include "libavutil/opt.h"
32 #include "avfilter.h"
33 #include "internal.h"
34 
35 typedef struct {
36  const AVClass *class;
37  int w, h;
40  int sliding; ///< 1 if sliding mode, 0 otherwise
41  int xpos; ///< x position (current column)
42  RDFTContext *rdft; ///< Real Discrete Fourier Transform context
43  int rdft_bits; ///< number of bits (RDFT window size = 1<<rdft_bits)
44  FFTSample *rdft_data; ///< bins holder for each (displayed) channels
45  int filled; ///< number of samples (per channel) filled in current rdft_buffer
46  int consumed; ///< number of samples (per channel) consumed from the input frame
47  float *window_func_lut; ///< Window function LUT
49 
50 #define OFFSET(x) offsetof(ShowSpectrumContext, x)
51 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
52 
53 static const AVOption showspectrum_options[] = {
54  { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x480"}, 0, 0, FLAGS },
55  { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x480"}, 0, 0, FLAGS },
56  { "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
57  { NULL },
58 };
59 
60 AVFILTER_DEFINE_CLASS(showspectrum);
61 
62 static av_cold int init(AVFilterContext *ctx, const char *args)
63 {
64  ShowSpectrumContext *showspectrum = ctx->priv;
65  int err;
66 
67  showspectrum->class = &showspectrum_class;
68  av_opt_set_defaults(showspectrum);
69 
70  if ((err = av_set_options_string(showspectrum, args, "=", ":")) < 0)
71  return err;
72 
73  return 0;
74 }
75 
76 static av_cold void uninit(AVFilterContext *ctx)
77 {
78  ShowSpectrumContext *showspectrum = ctx->priv;
79 
80  av_rdft_end(showspectrum->rdft);
81  av_freep(&showspectrum->rdft_data);
82  av_freep(&showspectrum->window_func_lut);
83  avfilter_unref_bufferp(&showspectrum->outpicref);
84 }
85 
87 {
90  AVFilterLink *inlink = ctx->inputs[0];
91  AVFilterLink *outlink = ctx->outputs[0];
93  static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE };
94 
95  /* set input audio formats */
96  formats = ff_make_format_list(sample_fmts);
97  if (!formats)
98  return AVERROR(ENOMEM);
99  ff_formats_ref(formats, &inlink->out_formats);
100 
101  layouts = ff_all_channel_layouts();
102  if (!layouts)
103  return AVERROR(ENOMEM);
104  ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
105 
106  formats = ff_all_samplerates();
107  if (!formats)
108  return AVERROR(ENOMEM);
109  ff_formats_ref(formats, &inlink->out_samplerates);
110 
111  /* set output video format */
112  formats = ff_make_format_list(pix_fmts);
113  if (!formats)
114  return AVERROR(ENOMEM);
115  ff_formats_ref(formats, &outlink->in_formats);
116 
117  return 0;
118 }
119 
120 static int config_output(AVFilterLink *outlink)
121 {
122  AVFilterContext *ctx = outlink->src;
123  ShowSpectrumContext *showspectrum = ctx->priv;
124  int i, rdft_bits, win_size;
125 
126  outlink->w = showspectrum->w;
127  outlink->h = showspectrum->h;
128 
129  /* RDFT window size (precision) according to the requested output frame height */
130  for (rdft_bits = 1; 1<<rdft_bits < 2*outlink->h; rdft_bits++);
131  win_size = 1 << rdft_bits;
132 
133  /* (re-)configuration if the video output changed (or first init) */
134  if (rdft_bits != showspectrum->rdft_bits) {
135  size_t rdft_size;
136  AVFilterBufferRef *outpicref;
137 
138  av_rdft_end(showspectrum->rdft);
139  showspectrum->rdft = av_rdft_init(rdft_bits, DFT_R2C);
140  showspectrum->rdft_bits = rdft_bits;
141 
142  /* RDFT buffers: x2 for each (display) channel buffer.
143  * Note: we use free and malloc instead of a realloc-like function to
144  * make sure the buffer is aligned in memory for the FFT functions. */
145  av_freep(&showspectrum->rdft_data);
146  if (av_size_mult(sizeof(*showspectrum->rdft_data), 2 * win_size, &rdft_size) < 0)
147  return AVERROR(EINVAL);
148  showspectrum->rdft_data = av_malloc(rdft_size);
149  if (!showspectrum->rdft_data)
150  return AVERROR(ENOMEM);
151  showspectrum->filled = 0;
152 
153  /* pre-calc windowing function (hann here) */
154  showspectrum->window_func_lut =
155  av_realloc_f(showspectrum->window_func_lut, win_size,
156  sizeof(*showspectrum->window_func_lut));
157  if (!showspectrum->window_func_lut)
158  return AVERROR(ENOMEM);
159  for (i = 0; i < win_size; i++)
160  showspectrum->window_func_lut[i] = .5f * (1 - cos(2*M_PI*i / (win_size-1)));
161 
162  /* prepare the initial picref buffer (black frame) */
163  avfilter_unref_bufferp(&showspectrum->outpicref);
164  showspectrum->outpicref = outpicref =
166  outlink->w, outlink->h);
167  if (!outpicref)
168  return AVERROR(ENOMEM);
169  outlink->sample_aspect_ratio = (AVRational){1,1};
170  memset(outpicref->data[0], 0, outlink->h * outpicref->linesize[0]);
171  }
172 
173  if (showspectrum->xpos >= outlink->w)
174  showspectrum->xpos = 0;
175 
176  av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d RDFT window size:%d\n",
177  showspectrum->w, showspectrum->h, win_size);
178  return 0;
179 }
180 
181 inline static void push_frame(AVFilterLink *outlink)
182 {
183  ShowSpectrumContext *showspectrum = outlink->src->priv;
184 
185  showspectrum->xpos++;
186  if (showspectrum->xpos >= outlink->w)
187  showspectrum->xpos = 0;
188  showspectrum->filled = 0;
189  showspectrum->req_fullfilled = 1;
190 
191  ff_filter_frame(outlink, avfilter_ref_buffer(showspectrum->outpicref, ~AV_PERM_WRITE));
192 }
193 
194 static int request_frame(AVFilterLink *outlink)
195 {
196  ShowSpectrumContext *showspectrum = outlink->src->priv;
197  AVFilterLink *inlink = outlink->src->inputs[0];
198  int ret;
199 
200  showspectrum->req_fullfilled = 0;
201  do {
202  ret = ff_request_frame(inlink);
203  } while (!showspectrum->req_fullfilled && ret >= 0);
204 
205  if (ret == AVERROR_EOF && showspectrum->outpicref)
206  push_frame(outlink);
207  return ret;
208 }
209 
210 static int plot_spectrum_column(AVFilterLink *inlink, AVFilterBufferRef *insamples, int nb_samples)
211 {
212  AVFilterContext *ctx = inlink->dst;
213  AVFilterLink *outlink = ctx->outputs[0];
214  ShowSpectrumContext *showspectrum = ctx->priv;
215  AVFilterBufferRef *outpicref = showspectrum->outpicref;
217 
218  /* nb_freq contains the power of two superior or equal to the output image
219  * height (or half the RDFT window size) */
220  const int nb_freq = 1 << (showspectrum->rdft_bits - 1);
221  const int win_size = nb_freq << 1;
222 
223  int ch, n, y;
224  FFTSample *data[2];
225  const int nb_display_channels = FFMIN(nb_channels, 2);
226  const int start = showspectrum->filled;
227  const int add_samples = FFMIN(win_size - start, nb_samples);
228 
229  /* fill RDFT input with the number of samples available */
230  for (ch = 0; ch < nb_display_channels; ch++) {
231  const int16_t *p = (int16_t *)insamples->extended_data[ch];
232 
233  p += showspectrum->consumed;
234  data[ch] = showspectrum->rdft_data + win_size * ch; // select channel buffer
235  for (n = 0; n < add_samples; n++)
236  data[ch][start + n] = p[n] * showspectrum->window_func_lut[start + n];
237  }
238  showspectrum->filled += add_samples;
239 
240  /* complete RDFT window size? */
241  if (showspectrum->filled == win_size) {
242 
243  /* run RDFT on each samples set */
244  for (ch = 0; ch < nb_display_channels; ch++)
245  av_rdft_calc(showspectrum->rdft, data[ch]);
246 
247  /* fill a new spectrum column */
248 #define RE(ch) data[ch][2*y + 0]
249 #define IM(ch) data[ch][2*y + 1]
250 #define MAGNITUDE(re, im) sqrt((re)*(re) + (im)*(im))
251 
252  for (y = 0; y < outlink->h; y++) {
253  // FIXME: bin[0] contains first and last bins
254  uint8_t *p = outpicref->data[0] + (outlink->h - y - 1) * outpicref->linesize[0];
255  const double w = 1. / sqrt(nb_freq);
256  int a = sqrt(w * MAGNITUDE(RE(0), IM(0)));
257  int b = nb_display_channels > 1 ? sqrt(w * MAGNITUDE(RE(1), IM(1))) : a;
258 
259  if (showspectrum->sliding) {
260  memmove(p, p + 3, (outlink->w - 1) * 3);
261  p += (outlink->w - 1) * 3;
262  } else {
263  p += showspectrum->xpos * 3;
264  }
265 
266  a = FFMIN(a, 255);
267  b = FFMIN(b, 255);
268  p[0] = a;
269  p[1] = b;
270  p[2] = (a + b) / 2;
271  }
272  outpicref->pts = insamples->pts +
273  av_rescale_q(showspectrum->consumed,
274  (AVRational){ 1, inlink->sample_rate },
275  outlink->time_base);
276  push_frame(outlink);
277  }
278 
279  return add_samples;
280 }
281 
282 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
283 {
284  AVFilterContext *ctx = inlink->dst;
285  ShowSpectrumContext *showspectrum = ctx->priv;
286  int left_samples = insamples->audio->nb_samples;
287 
288  showspectrum->consumed = 0;
289  while (left_samples) {
290  const int added_samples = plot_spectrum_column(inlink, insamples, left_samples);
291  showspectrum->consumed += added_samples;
292  left_samples -= added_samples;
293  }
294 
295  avfilter_unref_buffer(insamples);
296  return 0;
297 }
298 
300  {
301  .name = "default",
302  .type = AVMEDIA_TYPE_AUDIO,
303  .filter_frame = filter_frame,
304  .min_perms = AV_PERM_READ,
305  },
306  { NULL }
307 };
308 
310  {
311  .name = "default",
312  .type = AVMEDIA_TYPE_VIDEO,
313  .config_props = config_output,
314  .request_frame = request_frame,
315  },
316  { NULL }
317 };
318 
320  .name = "showspectrum",
321  .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
322  .init = init,
323  .uninit = uninit,
324  .query_formats = query_formats,
325  .priv_size = sizeof(ShowSpectrumContext),
326  .inputs = showspectrum_inputs,
327  .outputs = showspectrum_outputs,
328  .priv_class = &showspectrum_class,
329 };