]> git.sesse.net Git - ffmpeg/blob - libavfilter/avf_showspectrum.c
Merge commit '4521645b1aee9e9ad8f5cea7b2392cd5f6ffcd26'
[ffmpeg] / libavfilter / avf_showspectrum.c
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"
30 #include "libavutil/audioconvert.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;
38     AVFilterBufferRef *outpicref;
39     int req_fullfilled;
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
48 } ShowSpectrumContext;
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
86 static int query_formats(AVFilterContext *ctx)
87 {
88     AVFilterFormats *formats = NULL;
89     AVFilterChannelLayouts *layouts = NULL;
90     AVFilterLink *inlink = ctx->inputs[0];
91     AVFilterLink *outlink = ctx->outputs[0];
92     static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16P, -1 };
93     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGB24, -1 };
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 =
165             ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_PRESERVE|AV_PERM_REUSE2,
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_start_frame(outlink, avfilter_ref_buffer(showspectrum->outpicref, ~AV_PERM_WRITE));
192     ff_draw_slice(outlink, 0, outlink->h, 1);
193     ff_end_frame(outlink);
194 }
195
196 static int request_frame(AVFilterLink *outlink)
197 {
198     ShowSpectrumContext *showspectrum = outlink->src->priv;
199     AVFilterLink *inlink = outlink->src->inputs[0];
200     int ret;
201
202     showspectrum->req_fullfilled = 0;
203     do {
204         ret = ff_request_frame(inlink);
205     } while (!showspectrum->req_fullfilled && ret >= 0);
206
207     if (ret == AVERROR_EOF && showspectrum->outpicref)
208         push_frame(outlink);
209     return ret;
210 }
211
212 static int plot_spectrum_column(AVFilterLink *inlink, AVFilterBufferRef *insamples, int nb_samples)
213 {
214     AVFilterContext *ctx = inlink->dst;
215     AVFilterLink *outlink = ctx->outputs[0];
216     ShowSpectrumContext *showspectrum = ctx->priv;
217     AVFilterBufferRef *outpicref = showspectrum->outpicref;
218     const int nb_channels = av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
219
220     /* nb_freq contains the power of two superior or equal to the output image
221      * height (or half the RDFT window size) */
222     const int nb_freq = 1 << (showspectrum->rdft_bits - 1);
223     const int win_size = nb_freq << 1;
224
225     int ch, n, y;
226     FFTSample *data[2];
227     const int nb_display_channels = FFMIN(nb_channels, 2);
228     const int start = showspectrum->filled;
229     const int add_samples = FFMIN(win_size - start, nb_samples);
230
231     /* fill RDFT input with the number of samples available */
232     for (ch = 0; ch < nb_display_channels; ch++) {
233         const int16_t *p = (int16_t *)insamples->extended_data[ch];
234
235         p += showspectrum->consumed;
236         data[ch] = showspectrum->rdft_data + win_size * ch; // select channel buffer
237         for (n = 0; n < add_samples; n++)
238             data[ch][start + n] = p[n] * showspectrum->window_func_lut[start + n];
239     }
240     showspectrum->filled += add_samples;
241
242     /* complete RDFT window size? */
243     if (showspectrum->filled == win_size) {
244
245         /* run RDFT on each samples set */
246         for (ch = 0; ch < nb_display_channels; ch++)
247             av_rdft_calc(showspectrum->rdft, data[ch]);
248
249         /* fill a new spectrum column */
250 #define RE(ch) data[ch][2*y + 0]
251 #define IM(ch) data[ch][2*y + 1]
252 #define MAGNITUDE(re, im) sqrt((re)*(re) + (im)*(im))
253
254         for (y = 0; y < outlink->h; y++) {
255             // FIXME: bin[0] contains first and last bins
256             uint8_t *p = outpicref->data[0] + (outlink->h - y - 1) * outpicref->linesize[0];
257             const double w = 1. / sqrt(nb_freq);
258             int a =                           sqrt(w * MAGNITUDE(RE(0), IM(0)));
259             int b = nb_display_channels > 1 ? sqrt(w * MAGNITUDE(RE(1), IM(1))) : a;
260
261             if (showspectrum->sliding) {
262                 memmove(p, p + 3, (outlink->w - 1) * 3);
263                 p += (outlink->w - 1) * 3;
264             } else {
265                 p += showspectrum->xpos * 3;
266             }
267
268             a = FFMIN(a, 255);
269             b = FFMIN(b, 255);
270             p[0] = a;
271             p[1] = b;
272             p[2] = (a + b) / 2;
273         }
274         outpicref->pts = insamples->pts +
275             av_rescale_q(showspectrum->consumed,
276                          (AVRational){ 1, inlink->sample_rate },
277                          outlink->time_base);
278         push_frame(outlink);
279     }
280
281     return add_samples;
282 }
283
284 static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
285 {
286     AVFilterContext *ctx = inlink->dst;
287     ShowSpectrumContext *showspectrum = ctx->priv;
288     int left_samples = insamples->audio->nb_samples;
289
290     showspectrum->consumed = 0;
291     while (left_samples) {
292         const int added_samples = plot_spectrum_column(inlink, insamples, left_samples);
293         showspectrum->consumed += added_samples;
294         left_samples -= added_samples;
295     }
296
297     avfilter_unref_buffer(insamples);
298     return 0;
299 }
300
301 AVFilter avfilter_avf_showspectrum = {
302     .name           = "showspectrum",
303     .description    = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
304     .init           = init,
305     .uninit         = uninit,
306     .query_formats  = query_formats,
307     .priv_size      = sizeof(ShowSpectrumContext),
308
309     .inputs  = (const AVFilterPad[]) {
310         {
311             .name           = "default",
312             .type           = AVMEDIA_TYPE_AUDIO,
313             .filter_samples = filter_samples,
314             .min_perms      = AV_PERM_READ,
315         },
316         { .name = NULL }
317     },
318
319     .outputs = (const AVFilterPad[]) {
320         {
321             .name           = "default",
322             .type           = AVMEDIA_TYPE_VIDEO,
323             .config_props   = config_output,
324             .request_frame  = request_frame,
325         },
326         { .name = NULL }
327     },
328
329     .priv_class = &showspectrum_class,
330 };