blob: 09b5a2a51fa462f446d361f86d2374bd1ac71c77
1 | /* |
2 | * Copyright (c) 2012-2013 Clément Bœsch |
3 | * Copyright (c) 2013 Rudolf Polzer <divverent@xonotic.org> |
4 | * Copyright (c) 2015 Paul B Mahol |
5 | * |
6 | * This file is part of FFmpeg. |
7 | * |
8 | * FFmpeg is free software; you can redistribute it and/or |
9 | * modify it under the terms of the GNU Lesser General Public |
10 | * License as published by the Free Software Foundation; either |
11 | * version 2.1 of the License, or (at your option) any later version. |
12 | * |
13 | * FFmpeg is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | * Lesser General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU Lesser General Public |
19 | * License along with FFmpeg; if not, write to the Free Software |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | */ |
22 | |
23 | /** |
24 | * @file |
25 | * audio to spectrum (video) transmedia filter, based on ffplay rdft showmode |
26 | * (by Michael Niedermayer) and lavfi/avf_showwaves (by Stefano Sabatini). |
27 | */ |
28 | |
29 | #include <math.h> |
30 | |
31 | #include "libavcodec/avfft.h" |
32 | #include "libavutil/audio_fifo.h" |
33 | #include "libavutil/avassert.h" |
34 | #include "libavutil/avstring.h" |
35 | #include "libavutil/channel_layout.h" |
36 | #include "libavutil/opt.h" |
37 | #include "libavutil/xga_font_data.h" |
38 | #include "audio.h" |
39 | #include "video.h" |
40 | #include "avfilter.h" |
41 | #include "internal.h" |
42 | #include "window_func.h" |
43 | |
44 | enum DisplayMode { COMBINED, SEPARATE, NB_MODES }; |
45 | enum DataMode { D_MAGNITUDE, D_PHASE, NB_DMODES }; |
46 | enum DisplayScale { LINEAR, SQRT, CBRT, LOG, FOURTHRT, FIFTHRT, NB_SCALES }; |
47 | enum ColorMode { CHANNEL, INTENSITY, RAINBOW, MORELAND, NEBULAE, FIRE, FIERY, FRUIT, COOL, NB_CLMODES }; |
48 | enum SlideMode { REPLACE, SCROLL, FULLFRAME, RSCROLL, NB_SLIDES }; |
49 | enum Orientation { VERTICAL, HORIZONTAL, NB_ORIENTATIONS }; |
50 | |
51 | typedef struct { |
52 | const AVClass *class; |
53 | int w, h; |
54 | AVFrame *outpicref; |
55 | int nb_display_channels; |
56 | int orientation; |
57 | int channel_width; |
58 | int channel_height; |
59 | int sliding; ///< 1 if sliding mode, 0 otherwise |
60 | int mode; ///< channel display mode |
61 | int color_mode; ///< display color scheme |
62 | int scale; |
63 | float saturation; ///< color saturation multiplier |
64 | float rotation; ///< color rotation |
65 | int data; |
66 | int xpos; ///< x position (current column) |
67 | FFTContext **fft; ///< Fast Fourier Transform context |
68 | int fft_bits; ///< number of bits (FFT window size = 1<<fft_bits) |
69 | FFTComplex **fft_data; ///< bins holder for each (displayed) channels |
70 | float *window_func_lut; ///< Window function LUT |
71 | float **magnitudes; |
72 | float **phases; |
73 | int win_func; |
74 | int win_size; |
75 | double win_scale; |
76 | float overlap; |
77 | float gain; |
78 | int hop_size; |
79 | float *combine_buffer; ///< color combining buffer (3 * h items) |
80 | float **color_buffer; ///< color buffer (3 * h * ch items) |
81 | AVAudioFifo *fifo; |
82 | int64_t pts; |
83 | int single_pic; |
84 | int legend; |
85 | int start_x, start_y; |
86 | } ShowSpectrumContext; |
87 | |
88 | #define OFFSET(x) offsetof(ShowSpectrumContext, x) |
89 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM |
90 | |
91 | static const AVOption showspectrum_options[] = { |
92 | { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS }, |
93 | { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS }, |
94 | { "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_SLIDES-1, FLAGS, "slide" }, |
95 | { "replace", "replace old columns with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, FLAGS, "slide" }, |
96 | { "scroll", "scroll from right to left", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" }, |
97 | { "fullframe", "return full frames", 0, AV_OPT_TYPE_CONST, {.i64=FULLFRAME}, 0, 0, FLAGS, "slide" }, |
98 | { "rscroll", "scroll from left to right", 0, AV_OPT_TYPE_CONST, {.i64=RSCROLL}, 0, 0, FLAGS, "slide" }, |
99 | { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, COMBINED, NB_MODES-1, FLAGS, "mode" }, |
100 | { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" }, |
101 | { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" }, |
102 | { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=CHANNEL}, CHANNEL, NB_CLMODES-1, FLAGS, "color" }, |
103 | { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" }, |
104 | { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" }, |
105 | { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" }, |
106 | { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" }, |
107 | { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" }, |
108 | { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" }, |
109 | { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" }, |
110 | { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" }, |
111 | { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" }, |
112 | { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=SQRT}, LINEAR, NB_SCALES-1, FLAGS, "scale" }, |
113 | { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" }, |
114 | { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" }, |
115 | { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" }, |
116 | { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" }, |
117 | { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" }, |
118 | { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" }, |
119 | { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS }, |
120 | { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" }, |
121 | { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" }, |
122 | { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" }, |
123 | { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" }, |
124 | { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" }, |
125 | { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" }, |
126 | { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" }, |
127 | { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" }, |
128 | { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" }, |
129 | { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" }, |
130 | { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" }, |
131 | { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" }, |
132 | { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" }, |
133 | { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" }, |
134 | { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" }, |
135 | { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" }, |
136 | { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" }, |
137 | { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" }, |
138 | { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" }, |
139 | { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" }, |
140 | { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" }, |
141 | { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" }, |
142 | { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" }, |
143 | { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" }, |
144 | { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl = 0}, 0, 1, FLAGS }, |
145 | { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS }, |
146 | { "data", "set data mode", OFFSET(data), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_DMODES-1, FLAGS, "data" }, |
147 | { "magnitude", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_MAGNITUDE}, 0, 0, FLAGS, "data" }, |
148 | { "phase", NULL, 0, AV_OPT_TYPE_CONST, {.i64=D_PHASE}, 0, 0, FLAGS, "data" }, |
149 | { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS }, |
150 | { NULL } |
151 | }; |
152 | |
153 | AVFILTER_DEFINE_CLASS(showspectrum); |
154 | |
155 | static const struct ColorTable { |
156 | float a, y, u, v; |
157 | } color_table[][8] = { |
158 | [INTENSITY] = { |
159 | { 0, 0, 0, 0 }, |
160 | { 0.13, .03587126228984074, .1573300977624594, -.02548747583751842 }, |
161 | { 0.30, .18572281794568020, .1772436246393981, .17475554840414750 }, |
162 | { 0.60, .28184980583656130, -.1593064119945782, .47132074554608920 }, |
163 | { 0.73, .65830621175547810, -.3716070802232764, .24352759331252930 }, |
164 | { 0.78, .76318535758242900, -.4307467689263783, .16866496622310430 }, |
165 | { 0.91, .95336363636363640, -.2045454545454546, .03313636363636363 }, |
166 | { 1, 1, 0, 0 }}, |
167 | [RAINBOW] = { |
168 | { 0, 0, 0, 0 }, |
169 | { 0.13, 44/256., (189-128)/256., (138-128)/256. }, |
170 | { 0.25, 29/256., (186-128)/256., (119-128)/256. }, |
171 | { 0.38, 119/256., (194-128)/256., (53-128)/256. }, |
172 | { 0.60, 111/256., (73-128)/256., (59-128)/256. }, |
173 | { 0.73, 205/256., (19-128)/256., (149-128)/256. }, |
174 | { 0.86, 135/256., (83-128)/256., (200-128)/256. }, |
175 | { 1, 73/256., (95-128)/256., (225-128)/256. }}, |
176 | [MORELAND] = { |
177 | { 0, 44/256., (181-128)/256., (112-128)/256. }, |
178 | { 0.13, 126/256., (177-128)/256., (106-128)/256. }, |
179 | { 0.25, 164/256., (163-128)/256., (109-128)/256. }, |
180 | { 0.38, 200/256., (140-128)/256., (120-128)/256. }, |
181 | { 0.60, 201/256., (117-128)/256., (141-128)/256. }, |
182 | { 0.73, 177/256., (103-128)/256., (165-128)/256. }, |
183 | { 0.86, 136/256., (100-128)/256., (183-128)/256. }, |
184 | { 1, 68/256., (117-128)/256., (203-128)/256. }}, |
185 | [NEBULAE] = { |
186 | { 0, 10/256., (134-128)/256., (132-128)/256. }, |
187 | { 0.23, 21/256., (137-128)/256., (130-128)/256. }, |
188 | { 0.45, 35/256., (134-128)/256., (134-128)/256. }, |
189 | { 0.57, 51/256., (130-128)/256., (139-128)/256. }, |
190 | { 0.67, 104/256., (116-128)/256., (162-128)/256. }, |
191 | { 0.77, 120/256., (105-128)/256., (188-128)/256. }, |
192 | { 0.87, 140/256., (105-128)/256., (188-128)/256. }, |
193 | { 1, 1, 0, 0 }}, |
194 | [FIRE] = { |
195 | { 0, 0, 0, 0 }, |
196 | { 0.23, 44/256., (132-128)/256., (127-128)/256. }, |
197 | { 0.45, 62/256., (116-128)/256., (140-128)/256. }, |
198 | { 0.57, 75/256., (105-128)/256., (152-128)/256. }, |
199 | { 0.67, 95/256., (91-128)/256., (166-128)/256. }, |
200 | { 0.77, 126/256., (74-128)/256., (172-128)/256. }, |
201 | { 0.87, 164/256., (73-128)/256., (162-128)/256. }, |
202 | { 1, 1, 0, 0 }}, |
203 | [FIERY] = { |
204 | { 0, 0, 0, 0 }, |
205 | { 0.23, 36/256., (116-128)/256., (163-128)/256. }, |
206 | { 0.45, 52/256., (102-128)/256., (200-128)/256. }, |
207 | { 0.57, 116/256., (84-128)/256., (196-128)/256. }, |
208 | { 0.67, 157/256., (67-128)/256., (181-128)/256. }, |
209 | { 0.77, 193/256., (40-128)/256., (155-128)/256. }, |
210 | { 0.87, 221/256., (101-128)/256., (134-128)/256. }, |
211 | { 1, 1, 0, 0 }}, |
212 | [FRUIT] = { |
213 | { 0, 0, 0, 0 }, |
214 | { 0.20, 29/256., (136-128)/256., (119-128)/256. }, |
215 | { 0.30, 60/256., (119-128)/256., (90-128)/256. }, |
216 | { 0.40, 85/256., (91-128)/256., (85-128)/256. }, |
217 | { 0.50, 116/256., (70-128)/256., (105-128)/256. }, |
218 | { 0.60, 151/256., (50-128)/256., (146-128)/256. }, |
219 | { 0.70, 191/256., (63-128)/256., (178-128)/256. }, |
220 | { 1, 98/256., (80-128)/256., (221-128)/256. }}, |
221 | [COOL] = { |
222 | { 0, 0, 0, 0 }, |
223 | { .15, 0, .5, -.5 }, |
224 | { 1, 1, -.5, .5 }}, |
225 | }; |
226 | |
227 | static av_cold void uninit(AVFilterContext *ctx) |
228 | { |
229 | ShowSpectrumContext *s = ctx->priv; |
230 | int i; |
231 | |
232 | av_freep(&s->combine_buffer); |
233 | if (s->fft) { |
234 | for (i = 0; i < s->nb_display_channels; i++) |
235 | av_fft_end(s->fft[i]); |
236 | } |
237 | av_freep(&s->fft); |
238 | if (s->fft_data) { |
239 | for (i = 0; i < s->nb_display_channels; i++) |
240 | av_freep(&s->fft_data[i]); |
241 | } |
242 | av_freep(&s->fft_data); |
243 | if (s->color_buffer) { |
244 | for (i = 0; i < s->nb_display_channels; i++) |
245 | av_freep(&s->color_buffer[i]); |
246 | } |
247 | av_freep(&s->color_buffer); |
248 | av_freep(&s->window_func_lut); |
249 | if (s->magnitudes) { |
250 | for (i = 0; i < s->nb_display_channels; i++) |
251 | av_freep(&s->magnitudes[i]); |
252 | } |
253 | av_freep(&s->magnitudes); |
254 | av_frame_free(&s->outpicref); |
255 | av_audio_fifo_free(s->fifo); |
256 | if (s->phases) { |
257 | for (i = 0; i < s->nb_display_channels; i++) |
258 | av_freep(&s->phases[i]); |
259 | } |
260 | av_freep(&s->phases); |
261 | } |
262 | |
263 | static int query_formats(AVFilterContext *ctx) |
264 | { |
265 | AVFilterFormats *formats = NULL; |
266 | AVFilterChannelLayouts *layouts = NULL; |
267 | AVFilterLink *inlink = ctx->inputs[0]; |
268 | AVFilterLink *outlink = ctx->outputs[0]; |
269 | static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE }; |
270 | static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE }; |
271 | int ret; |
272 | |
273 | /* set input audio formats */ |
274 | formats = ff_make_format_list(sample_fmts); |
275 | if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0) |
276 | return ret; |
277 | |
278 | layouts = ff_all_channel_layouts(); |
279 | if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0) |
280 | return ret; |
281 | |
282 | formats = ff_all_samplerates(); |
283 | if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0) |
284 | return ret; |
285 | |
286 | /* set output video format */ |
287 | formats = ff_make_format_list(pix_fmts); |
288 | if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0) |
289 | return ret; |
290 | |
291 | return 0; |
292 | } |
293 | |
294 | static int config_output(AVFilterLink *outlink) |
295 | { |
296 | AVFilterContext *ctx = outlink->src; |
297 | AVFilterLink *inlink = ctx->inputs[0]; |
298 | ShowSpectrumContext *s = ctx->priv; |
299 | int i, fft_bits, h, w; |
300 | float overlap; |
301 | |
302 | if (!strcmp(ctx->filter->name, "showspectrumpic")) |
303 | s->single_pic = 1; |
304 | |
305 | outlink->w = s->w; |
306 | outlink->h = s->h; |
307 | |
308 | if (s->legend) { |
309 | s->start_x = log10(inlink->sample_rate) * 25; |
310 | s->start_y = 64; |
311 | outlink->w += s->start_x * 2; |
312 | outlink->h += s->start_y * 2; |
313 | } |
314 | |
315 | h = (s->mode == COMBINED || s->orientation == HORIZONTAL) ? s->h : s->h / inlink->channels; |
316 | w = (s->mode == COMBINED || s->orientation == VERTICAL) ? s->w : s->w / inlink->channels; |
317 | s->channel_height = h; |
318 | s->channel_width = w; |
319 | |
320 | if (s->orientation == VERTICAL) { |
321 | /* FFT window size (precision) according to the requested output frame height */ |
322 | for (fft_bits = 1; 1 << fft_bits < 2 * h; fft_bits++); |
323 | } else { |
324 | /* FFT window size (precision) according to the requested output frame width */ |
325 | for (fft_bits = 1; 1 << fft_bits < 2 * w; fft_bits++); |
326 | } |
327 | s->win_size = 1 << fft_bits; |
328 | |
329 | if (!s->fft) { |
330 | s->fft = av_calloc(inlink->channels, sizeof(*s->fft)); |
331 | if (!s->fft) |
332 | return AVERROR(ENOMEM); |
333 | } |
334 | |
335 | /* (re-)configuration if the video output changed (or first init) */ |
336 | if (fft_bits != s->fft_bits) { |
337 | AVFrame *outpicref; |
338 | |
339 | s->fft_bits = fft_bits; |
340 | |
341 | /* FFT buffers: x2 for each (display) channel buffer. |
342 | * Note: we use free and malloc instead of a realloc-like function to |
343 | * make sure the buffer is aligned in memory for the FFT functions. */ |
344 | for (i = 0; i < s->nb_display_channels; i++) { |
345 | av_fft_end(s->fft[i]); |
346 | av_freep(&s->fft_data[i]); |
347 | } |
348 | av_freep(&s->fft_data); |
349 | |
350 | s->nb_display_channels = inlink->channels; |
351 | for (i = 0; i < s->nb_display_channels; i++) { |
352 | s->fft[i] = av_fft_init(fft_bits, 0); |
353 | if (!s->fft[i]) { |
354 | av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. " |
355 | "The window size might be too high.\n"); |
356 | return AVERROR(EINVAL); |
357 | } |
358 | } |
359 | |
360 | s->magnitudes = av_calloc(s->nb_display_channels, sizeof(*s->magnitudes)); |
361 | if (!s->magnitudes) |
362 | return AVERROR(ENOMEM); |
363 | for (i = 0; i < s->nb_display_channels; i++) { |
364 | s->magnitudes[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->magnitudes)); |
365 | if (!s->magnitudes[i]) |
366 | return AVERROR(ENOMEM); |
367 | } |
368 | |
369 | s->phases = av_calloc(s->nb_display_channels, sizeof(*s->phases)); |
370 | if (!s->phases) |
371 | return AVERROR(ENOMEM); |
372 | for (i = 0; i < s->nb_display_channels; i++) { |
373 | s->phases[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->phases)); |
374 | if (!s->phases[i]) |
375 | return AVERROR(ENOMEM); |
376 | } |
377 | |
378 | av_freep(&s->color_buffer); |
379 | s->color_buffer = av_calloc(s->nb_display_channels, sizeof(*s->color_buffer)); |
380 | if (!s->color_buffer) |
381 | return AVERROR(ENOMEM); |
382 | for (i = 0; i < s->nb_display_channels; i++) { |
383 | s->color_buffer[i] = av_calloc(s->orientation == VERTICAL ? s->h * 3 : s->w * 3, sizeof(**s->color_buffer)); |
384 | if (!s->color_buffer[i]) |
385 | return AVERROR(ENOMEM); |
386 | } |
387 | |
388 | s->fft_data = av_calloc(s->nb_display_channels, sizeof(*s->fft_data)); |
389 | if (!s->fft_data) |
390 | return AVERROR(ENOMEM); |
391 | for (i = 0; i < s->nb_display_channels; i++) { |
392 | s->fft_data[i] = av_calloc(s->win_size, sizeof(**s->fft_data)); |
393 | if (!s->fft_data[i]) |
394 | return AVERROR(ENOMEM); |
395 | } |
396 | |
397 | /* pre-calc windowing function */ |
398 | s->window_func_lut = |
399 | av_realloc_f(s->window_func_lut, s->win_size, |
400 | sizeof(*s->window_func_lut)); |
401 | if (!s->window_func_lut) |
402 | return AVERROR(ENOMEM); |
403 | ff_generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap); |
404 | if (s->overlap == 1) |
405 | s->overlap = overlap; |
406 | s->hop_size = (1. - s->overlap) * s->win_size; |
407 | if (s->hop_size < 1) { |
408 | av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap); |
409 | return AVERROR(EINVAL); |
410 | } |
411 | |
412 | for (s->win_scale = 0, i = 0; i < s->win_size; i++) { |
413 | s->win_scale += s->window_func_lut[i] * s->window_func_lut[i]; |
414 | } |
415 | s->win_scale = 1. / sqrt(s->win_scale); |
416 | |
417 | /* prepare the initial picref buffer (black frame) */ |
418 | av_frame_free(&s->outpicref); |
419 | s->outpicref = outpicref = |
420 | ff_get_video_buffer(outlink, outlink->w, outlink->h); |
421 | if (!outpicref) |
422 | return AVERROR(ENOMEM); |
423 | outlink->sample_aspect_ratio = (AVRational){1,1}; |
424 | for (i = 0; i < outlink->h; i++) { |
425 | memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w); |
426 | memset(outpicref->data[1] + i * outpicref->linesize[1], 128, outlink->w); |
427 | memset(outpicref->data[2] + i * outpicref->linesize[2], 128, outlink->w); |
428 | } |
429 | av_frame_set_color_range(outpicref, AVCOL_RANGE_JPEG); |
430 | } |
431 | |
432 | if ((s->orientation == VERTICAL && s->xpos >= s->w) || |
433 | (s->orientation == HORIZONTAL && s->xpos >= s->h)) |
434 | s->xpos = 0; |
435 | |
436 | outlink->frame_rate = av_make_q(inlink->sample_rate, s->win_size * (1.-s->overlap)); |
437 | if (s->orientation == VERTICAL && s->sliding == FULLFRAME) |
438 | outlink->frame_rate.den *= s->w; |
439 | if (s->orientation == HORIZONTAL && s->sliding == FULLFRAME) |
440 | outlink->frame_rate.den *= s->h; |
441 | |
442 | if (s->orientation == VERTICAL) { |
443 | s->combine_buffer = |
444 | av_realloc_f(s->combine_buffer, s->h * 3, |
445 | sizeof(*s->combine_buffer)); |
446 | } else { |
447 | s->combine_buffer = |
448 | av_realloc_f(s->combine_buffer, s->w * 3, |
449 | sizeof(*s->combine_buffer)); |
450 | } |
451 | |
452 | av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d FFT window size:%d\n", |
453 | s->w, s->h, s->win_size); |
454 | |
455 | av_audio_fifo_free(s->fifo); |
456 | s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size); |
457 | if (!s->fifo) |
458 | return AVERROR(ENOMEM); |
459 | return 0; |
460 | } |
461 | |
462 | static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
463 | { |
464 | ShowSpectrumContext *s = ctx->priv; |
465 | const float *window_func_lut = s->window_func_lut; |
466 | AVFrame *fin = arg; |
467 | const int ch = jobnr; |
468 | int n; |
469 | |
470 | /* fill FFT input with the number of samples available */ |
471 | const float *p = (float *)fin->extended_data[ch]; |
472 | |
473 | for (n = 0; n < s->win_size; n++) { |
474 | s->fft_data[ch][n].re = p[n] * window_func_lut[n]; |
475 | s->fft_data[ch][n].im = 0; |
476 | } |
477 | |
478 | /* run FFT on each samples set */ |
479 | av_fft_permute(s->fft[ch], s->fft_data[ch]); |
480 | av_fft_calc(s->fft[ch], s->fft_data[ch]); |
481 | |
482 | return 0; |
483 | } |
484 | |
485 | #define RE(y, ch) s->fft_data[ch][y].re |
486 | #define IM(y, ch) s->fft_data[ch][y].im |
487 | #define MAGNITUDE(y, ch) hypot(RE(y, ch), IM(y, ch)) |
488 | #define PHASE(y, ch) atan2(IM(y, ch), RE(y, ch)) |
489 | |
490 | static int calc_channel_magnitudes(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
491 | { |
492 | ShowSpectrumContext *s = ctx->priv; |
493 | const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1); |
494 | int y, h = s->orientation == VERTICAL ? s->h : s->w; |
495 | const float f = s->gain * w; |
496 | const int ch = jobnr; |
497 | float *magnitudes = s->magnitudes[ch]; |
498 | |
499 | for (y = 0; y < h; y++) |
500 | magnitudes[y] = MAGNITUDE(y, ch) * f; |
501 | |
502 | return 0; |
503 | } |
504 | |
505 | static int calc_channel_phases(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
506 | { |
507 | ShowSpectrumContext *s = ctx->priv; |
508 | const int h = s->orientation == VERTICAL ? s->h : s->w; |
509 | const int ch = jobnr; |
510 | float *phases = s->phases[ch]; |
511 | int y; |
512 | |
513 | for (y = 0; y < h; y++) |
514 | phases[y] = (PHASE(y, ch) / M_PI + 1) / 2; |
515 | |
516 | return 0; |
517 | } |
518 | |
519 | static void acalc_magnitudes(ShowSpectrumContext *s) |
520 | { |
521 | const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1); |
522 | int ch, y, h = s->orientation == VERTICAL ? s->h : s->w; |
523 | const float f = s->gain * w; |
524 | |
525 | for (ch = 0; ch < s->nb_display_channels; ch++) { |
526 | float *magnitudes = s->magnitudes[ch]; |
527 | |
528 | for (y = 0; y < h; y++) |
529 | magnitudes[y] += MAGNITUDE(y, ch) * f; |
530 | } |
531 | } |
532 | |
533 | static void scale_magnitudes(ShowSpectrumContext *s, float scale) |
534 | { |
535 | int ch, y, h = s->orientation == VERTICAL ? s->h : s->w; |
536 | |
537 | for (ch = 0; ch < s->nb_display_channels; ch++) { |
538 | float *magnitudes = s->magnitudes[ch]; |
539 | |
540 | for (y = 0; y < h; y++) |
541 | magnitudes[y] *= scale; |
542 | } |
543 | } |
544 | |
545 | static void color_range(ShowSpectrumContext *s, int ch, |
546 | float *yf, float *uf, float *vf) |
547 | { |
548 | switch (s->mode) { |
549 | case COMBINED: |
550 | // reduce range by channel count |
551 | *yf = 256.0f / s->nb_display_channels; |
552 | switch (s->color_mode) { |
553 | case RAINBOW: |
554 | case MORELAND: |
555 | case NEBULAE: |
556 | case FIRE: |
557 | case FIERY: |
558 | case FRUIT: |
559 | case COOL: |
560 | case INTENSITY: |
561 | *uf = *yf; |
562 | *vf = *yf; |
563 | break; |
564 | case CHANNEL: |
565 | /* adjust saturation for mixed UV coloring */ |
566 | /* this factor is correct for infinite channels, an approximation otherwise */ |
567 | *uf = *yf * M_PI; |
568 | *vf = *yf * M_PI; |
569 | break; |
570 | default: |
571 | av_assert0(0); |
572 | } |
573 | break; |
574 | case SEPARATE: |
575 | // full range |
576 | *yf = 256.0f; |
577 | *uf = 256.0f; |
578 | *vf = 256.0f; |
579 | break; |
580 | default: |
581 | av_assert0(0); |
582 | } |
583 | |
584 | if (s->color_mode == CHANNEL) { |
585 | if (s->nb_display_channels > 1) { |
586 | *uf *= 0.5 * sin((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation); |
587 | *vf *= 0.5 * cos((2 * M_PI * ch) / s->nb_display_channels + M_PI * s->rotation); |
588 | } else { |
589 | *uf *= 0.5 * sin(M_PI * s->rotation); |
590 | *vf *= 0.5 * cos(M_PI * s->rotation + M_PI_2); |
591 | } |
592 | } else { |
593 | *uf += *uf * sin(M_PI * s->rotation); |
594 | *vf += *vf * cos(M_PI * s->rotation + M_PI_2); |
595 | } |
596 | |
597 | *uf *= s->saturation; |
598 | *vf *= s->saturation; |
599 | } |
600 | |
601 | static void pick_color(ShowSpectrumContext *s, |
602 | float yf, float uf, float vf, |
603 | float a, float *out) |
604 | { |
605 | if (s->color_mode > CHANNEL) { |
606 | const int cm = s->color_mode; |
607 | float y, u, v; |
608 | int i; |
609 | |
610 | for (i = 1; i < FF_ARRAY_ELEMS(color_table[cm]) - 1; i++) |
611 | if (color_table[cm][i].a >= a) |
612 | break; |
613 | // i now is the first item >= the color |
614 | // now we know to interpolate between item i - 1 and i |
615 | if (a <= color_table[cm][i - 1].a) { |
616 | y = color_table[cm][i - 1].y; |
617 | u = color_table[cm][i - 1].u; |
618 | v = color_table[cm][i - 1].v; |
619 | } else if (a >= color_table[cm][i].a) { |
620 | y = color_table[cm][i].y; |
621 | u = color_table[cm][i].u; |
622 | v = color_table[cm][i].v; |
623 | } else { |
624 | float start = color_table[cm][i - 1].a; |
625 | float end = color_table[cm][i].a; |
626 | float lerpfrac = (a - start) / (end - start); |
627 | y = color_table[cm][i - 1].y * (1.0f - lerpfrac) |
628 | + color_table[cm][i].y * lerpfrac; |
629 | u = color_table[cm][i - 1].u * (1.0f - lerpfrac) |
630 | + color_table[cm][i].u * lerpfrac; |
631 | v = color_table[cm][i - 1].v * (1.0f - lerpfrac) |
632 | + color_table[cm][i].v * lerpfrac; |
633 | } |
634 | |
635 | out[0] = y * yf; |
636 | out[1] = u * uf; |
637 | out[2] = v * vf; |
638 | } else { |
639 | out[0] = a * yf; |
640 | out[1] = a * uf; |
641 | out[2] = a * vf; |
642 | } |
643 | } |
644 | |
645 | static void clear_combine_buffer(ShowSpectrumContext *s, int size) |
646 | { |
647 | int y; |
648 | |
649 | for (y = 0; y < size; y++) { |
650 | s->combine_buffer[3 * y ] = 0; |
651 | s->combine_buffer[3 * y + 1] = 127.5; |
652 | s->combine_buffer[3 * y + 2] = 127.5; |
653 | } |
654 | } |
655 | |
656 | static int plot_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
657 | { |
658 | ShowSpectrumContext *s = ctx->priv; |
659 | const int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width; |
660 | const int ch = jobnr; |
661 | float *magnitudes = s->magnitudes[ch]; |
662 | float *phases = s->phases[ch]; |
663 | float yf, uf, vf; |
664 | int y; |
665 | |
666 | /* decide color range */ |
667 | color_range(s, ch, &yf, &uf, &vf); |
668 | |
669 | /* draw the channel */ |
670 | for (y = 0; y < h; y++) { |
671 | int row = (s->mode == COMBINED) ? y : ch * h + y; |
672 | float *out = &s->color_buffer[ch][3 * row]; |
673 | float a; |
674 | |
675 | switch (s->data) { |
676 | case D_MAGNITUDE: |
677 | /* get magnitude */ |
678 | a = magnitudes[y]; |
679 | break; |
680 | case D_PHASE: |
681 | /* get phase */ |
682 | a = phases[y]; |
683 | break; |
684 | default: |
685 | av_assert0(0); |
686 | } |
687 | |
688 | /* apply scale */ |
689 | switch (s->scale) { |
690 | case LINEAR: |
691 | a = av_clipf(a, 0, 1); |
692 | break; |
693 | case SQRT: |
694 | a = av_clipf(sqrt(a), 0, 1); |
695 | break; |
696 | case CBRT: |
697 | a = av_clipf(cbrt(a), 0, 1); |
698 | break; |
699 | case FOURTHRT: |
700 | a = av_clipf(sqrt(sqrt(a)), 0, 1); |
701 | break; |
702 | case FIFTHRT: |
703 | a = av_clipf(pow(a, 0.20), 0, 1); |
704 | break; |
705 | case LOG: |
706 | a = 1 + log10(av_clipd(a, 1e-6, 1)) / 6; // zero = -120dBFS |
707 | break; |
708 | default: |
709 | av_assert0(0); |
710 | } |
711 | |
712 | pick_color(s, yf, uf, vf, a, out); |
713 | } |
714 | |
715 | return 0; |
716 | } |
717 | |
718 | static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples) |
719 | { |
720 | AVFilterContext *ctx = inlink->dst; |
721 | AVFilterLink *outlink = ctx->outputs[0]; |
722 | ShowSpectrumContext *s = ctx->priv; |
723 | AVFrame *outpicref = s->outpicref; |
724 | int ret, plane, x, y, z = s->orientation == VERTICAL ? s->h : s->w; |
725 | |
726 | /* fill a new spectrum column */ |
727 | /* initialize buffer for combining to black */ |
728 | clear_combine_buffer(s, z); |
729 | |
730 | ctx->internal->execute(ctx, plot_channel, NULL, NULL, s->nb_display_channels); |
731 | |
732 | for (y = 0; y < z * 3; y++) { |
733 | for (x = 0; x < s->nb_display_channels; x++) { |
734 | s->combine_buffer[y] += s->color_buffer[x][y]; |
735 | } |
736 | } |
737 | |
738 | av_frame_make_writable(s->outpicref); |
739 | /* copy to output */ |
740 | if (s->orientation == VERTICAL) { |
741 | if (s->sliding == SCROLL) { |
742 | for (plane = 0; plane < 3; plane++) { |
743 | for (y = 0; y < s->h; y++) { |
744 | uint8_t *p = outpicref->data[plane] + |
745 | y * outpicref->linesize[plane]; |
746 | memmove(p, p + 1, s->w - 1); |
747 | } |
748 | } |
749 | s->xpos = s->w - 1; |
750 | } else if (s->sliding == RSCROLL) { |
751 | for (plane = 0; plane < 3; plane++) { |
752 | for (y = 0; y < s->h; y++) { |
753 | uint8_t *p = outpicref->data[plane] + |
754 | y * outpicref->linesize[plane]; |
755 | memmove(p + 1, p, s->w - 1); |
756 | } |
757 | } |
758 | s->xpos = 0; |
759 | } |
760 | for (plane = 0; plane < 3; plane++) { |
761 | uint8_t *p = outpicref->data[plane] + s->start_x + |
762 | (outlink->h - 1 - s->start_y) * outpicref->linesize[plane] + |
763 | s->xpos; |
764 | for (y = 0; y < s->h; y++) { |
765 | *p = lrintf(av_clipf(s->combine_buffer[3 * y + plane], 0, 255)); |
766 | p -= outpicref->linesize[plane]; |
767 | } |
768 | } |
769 | } else { |
770 | if (s->sliding == SCROLL) { |
771 | for (plane = 0; plane < 3; plane++) { |
772 | for (y = 1; y < s->h; y++) { |
773 | memmove(outpicref->data[plane] + (y-1) * outpicref->linesize[plane], |
774 | outpicref->data[plane] + (y ) * outpicref->linesize[plane], |
775 | s->w); |
776 | } |
777 | } |
778 | s->xpos = s->h - 1; |
779 | } else if (s->sliding == RSCROLL) { |
780 | for (plane = 0; plane < 3; plane++) { |
781 | for (y = s->h - 1; y >= 1; y--) { |
782 | memmove(outpicref->data[plane] + (y ) * outpicref->linesize[plane], |
783 | outpicref->data[plane] + (y-1) * outpicref->linesize[plane], |
784 | s->w); |
785 | } |
786 | } |
787 | s->xpos = 0; |
788 | } |
789 | for (plane = 0; plane < 3; plane++) { |
790 | uint8_t *p = outpicref->data[plane] + s->start_x + |
791 | (s->xpos + s->start_y) * outpicref->linesize[plane]; |
792 | for (x = 0; x < s->w; x++) { |
793 | *p = lrintf(av_clipf(s->combine_buffer[3 * x + plane], 0, 255)); |
794 | p++; |
795 | } |
796 | } |
797 | } |
798 | |
799 | if (s->sliding != FULLFRAME || s->xpos == 0) |
800 | outpicref->pts = insamples->pts; |
801 | |
802 | s->xpos++; |
803 | if (s->orientation == VERTICAL && s->xpos >= s->w) |
804 | s->xpos = 0; |
805 | if (s->orientation == HORIZONTAL && s->xpos >= s->h) |
806 | s->xpos = 0; |
807 | if (!s->single_pic && (s->sliding != FULLFRAME || s->xpos == 0)) { |
808 | ret = ff_filter_frame(outlink, av_frame_clone(s->outpicref)); |
809 | if (ret < 0) |
810 | return ret; |
811 | } |
812 | |
813 | return s->win_size; |
814 | } |
815 | |
816 | #if CONFIG_SHOWSPECTRUM_FILTER |
817 | |
818 | static int request_frame(AVFilterLink *outlink) |
819 | { |
820 | ShowSpectrumContext *s = outlink->src->priv; |
821 | AVFilterLink *inlink = outlink->src->inputs[0]; |
822 | unsigned i; |
823 | int ret; |
824 | |
825 | ret = ff_request_frame(inlink); |
826 | if (ret == AVERROR_EOF && s->sliding == FULLFRAME && s->xpos > 0 && |
827 | s->outpicref) { |
828 | if (s->orientation == VERTICAL) { |
829 | for (i = 0; i < outlink->h; i++) { |
830 | memset(s->outpicref->data[0] + i * s->outpicref->linesize[0] + s->xpos, 0, outlink->w - s->xpos); |
831 | memset(s->outpicref->data[1] + i * s->outpicref->linesize[1] + s->xpos, 128, outlink->w - s->xpos); |
832 | memset(s->outpicref->data[2] + i * s->outpicref->linesize[2] + s->xpos, 128, outlink->w - s->xpos); |
833 | } |
834 | } else { |
835 | for (i = s->xpos; i < outlink->h; i++) { |
836 | memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w); |
837 | memset(s->outpicref->data[1] + i * s->outpicref->linesize[1], 128, outlink->w); |
838 | memset(s->outpicref->data[2] + i * s->outpicref->linesize[2], 128, outlink->w); |
839 | } |
840 | } |
841 | ret = ff_filter_frame(outlink, s->outpicref); |
842 | s->outpicref = NULL; |
843 | } |
844 | |
845 | return ret; |
846 | } |
847 | |
848 | static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) |
849 | { |
850 | AVFilterContext *ctx = inlink->dst; |
851 | ShowSpectrumContext *s = ctx->priv; |
852 | AVFrame *fin = NULL; |
853 | int ret = 0, consumed = 0; |
854 | |
855 | if (s->pts == AV_NOPTS_VALUE) |
856 | s->pts = insamples->pts - av_audio_fifo_size(s->fifo); |
857 | |
858 | av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples); |
859 | av_frame_free(&insamples); |
860 | while (av_audio_fifo_size(s->fifo) >= s->win_size) { |
861 | fin = ff_get_audio_buffer(inlink, s->win_size); |
862 | if (!fin) { |
863 | ret = AVERROR(ENOMEM); |
864 | goto fail; |
865 | } |
866 | |
867 | fin->pts = s->pts + consumed; |
868 | consumed += s->hop_size; |
869 | ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size); |
870 | if (ret < 0) |
871 | goto fail; |
872 | |
873 | av_assert0(fin->nb_samples == s->win_size); |
874 | |
875 | ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels); |
876 | |
877 | if (s->data == D_MAGNITUDE) |
878 | ctx->internal->execute(ctx, calc_channel_magnitudes, NULL, NULL, s->nb_display_channels); |
879 | |
880 | if (s->data == D_PHASE) |
881 | ctx->internal->execute(ctx, calc_channel_phases, NULL, NULL, s->nb_display_channels); |
882 | |
883 | ret = plot_spectrum_column(inlink, fin); |
884 | av_frame_free(&fin); |
885 | av_audio_fifo_drain(s->fifo, s->hop_size); |
886 | if (ret < 0) |
887 | goto fail; |
888 | } |
889 | |
890 | fail: |
891 | s->pts = AV_NOPTS_VALUE; |
892 | av_frame_free(&fin); |
893 | return ret; |
894 | } |
895 | |
896 | static const AVFilterPad showspectrum_inputs[] = { |
897 | { |
898 | .name = "default", |
899 | .type = AVMEDIA_TYPE_AUDIO, |
900 | .filter_frame = filter_frame, |
901 | }, |
902 | { NULL } |
903 | }; |
904 | |
905 | static const AVFilterPad showspectrum_outputs[] = { |
906 | { |
907 | .name = "default", |
908 | .type = AVMEDIA_TYPE_VIDEO, |
909 | .config_props = config_output, |
910 | .request_frame = request_frame, |
911 | }, |
912 | { NULL } |
913 | }; |
914 | |
915 | AVFilter ff_avf_showspectrum = { |
916 | .name = "showspectrum", |
917 | .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."), |
918 | .uninit = uninit, |
919 | .query_formats = query_formats, |
920 | .priv_size = sizeof(ShowSpectrumContext), |
921 | .inputs = showspectrum_inputs, |
922 | .outputs = showspectrum_outputs, |
923 | .priv_class = &showspectrum_class, |
924 | .flags = AVFILTER_FLAG_SLICE_THREADS, |
925 | }; |
926 | #endif // CONFIG_SHOWSPECTRUM_FILTER |
927 | |
928 | #if CONFIG_SHOWSPECTRUMPIC_FILTER |
929 | |
930 | static const AVOption showspectrumpic_options[] = { |
931 | { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS }, |
932 | { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS }, |
933 | { "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_MODES-1, FLAGS, "mode" }, |
934 | { "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" }, |
935 | { "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" }, |
936 | { "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=INTENSITY}, 0, NB_CLMODES-1, FLAGS, "color" }, |
937 | { "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" }, |
938 | { "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" }, |
939 | { "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" }, |
940 | { "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" }, |
941 | { "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" }, |
942 | { "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" }, |
943 | { "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" }, |
944 | { "fruit", "fruit based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FRUIT}, 0, 0, FLAGS, "color" }, |
945 | { "cool", "cool based coloring", 0, AV_OPT_TYPE_CONST, {.i64=COOL}, 0, 0, FLAGS, "color" }, |
946 | { "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LOG}, 0, NB_SCALES-1, FLAGS, "scale" }, |
947 | { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" }, |
948 | { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" }, |
949 | { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" }, |
950 | { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" }, |
951 | { "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" }, |
952 | { "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" }, |
953 | { "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS }, |
954 | { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" }, |
955 | { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" }, |
956 | { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" }, |
957 | { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" }, |
958 | { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" }, |
959 | { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" }, |
960 | { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" }, |
961 | { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" }, |
962 | { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" }, |
963 | { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" }, |
964 | { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" }, |
965 | { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" }, |
966 | { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" }, |
967 | { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" }, |
968 | { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" }, |
969 | { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" }, |
970 | { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" }, |
971 | { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" }, |
972 | { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" }, |
973 | { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" }, |
974 | { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" }, |
975 | { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" }, |
976 | { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" }, |
977 | { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" }, |
978 | { "gain", "set scale gain", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl = 1}, 0, 128, FLAGS }, |
979 | { "legend", "draw legend", OFFSET(legend), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS }, |
980 | { "rotation", "color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS }, |
981 | { NULL } |
982 | }; |
983 | |
984 | AVFILTER_DEFINE_CLASS(showspectrumpic); |
985 | |
986 | static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o) |
987 | { |
988 | const uint8_t *font; |
989 | int font_height; |
990 | int i; |
991 | |
992 | font = avpriv_cga_font, font_height = 8; |
993 | |
994 | for (i = 0; txt[i]; i++) { |
995 | int char_y, mask; |
996 | |
997 | if (o) { |
998 | for (char_y = font_height - 1; char_y >= 0; char_y--) { |
999 | uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x; |
1000 | for (mask = 0x80; mask; mask >>= 1) { |
1001 | if (font[txt[i] * font_height + font_height - 1 - char_y] & mask) |
1002 | p[char_y] = ~p[char_y]; |
1003 | p += pic->linesize[0]; |
1004 | } |
1005 | } |
1006 | } else { |
1007 | uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8); |
1008 | for (char_y = 0; char_y < font_height; char_y++) { |
1009 | for (mask = 0x80; mask; mask >>= 1) { |
1010 | if (font[txt[i] * font_height + char_y] & mask) |
1011 | *p = ~(*p); |
1012 | p++; |
1013 | } |
1014 | p += pic->linesize[0] - 8; |
1015 | } |
1016 | } |
1017 | } |
1018 | } |
1019 | |
1020 | static int showspectrumpic_request_frame(AVFilterLink *outlink) |
1021 | { |
1022 | AVFilterContext *ctx = outlink->src; |
1023 | ShowSpectrumContext *s = ctx->priv; |
1024 | AVFilterLink *inlink = ctx->inputs[0]; |
1025 | int ret, samples; |
1026 | |
1027 | ret = ff_request_frame(inlink); |
1028 | samples = av_audio_fifo_size(s->fifo); |
1029 | if (ret == AVERROR_EOF && s->outpicref && samples > 0) { |
1030 | int consumed = 0; |
1031 | int y, x = 0, sz = s->orientation == VERTICAL ? s->w : s->h; |
1032 | int ch, spf, spb; |
1033 | AVFrame *fin; |
1034 | |
1035 | spf = s->win_size * (samples / ((s->win_size * sz) * ceil(samples / (float)(s->win_size * sz)))); |
1036 | spf = FFMAX(1, spf); |
1037 | |
1038 | spb = (samples / (spf * sz)) * spf; |
1039 | |
1040 | fin = ff_get_audio_buffer(inlink, s->win_size); |
1041 | if (!fin) |
1042 | return AVERROR(ENOMEM); |
1043 | |
1044 | while (x < sz) { |
1045 | ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size); |
1046 | if (ret < 0) { |
1047 | av_frame_free(&fin); |
1048 | return ret; |
1049 | } |
1050 | |
1051 | av_audio_fifo_drain(s->fifo, spf); |
1052 | |
1053 | if (ret < s->win_size) { |
1054 | for (ch = 0; ch < s->nb_display_channels; ch++) { |
1055 | memset(fin->extended_data[ch] + ret * sizeof(float), 0, |
1056 | (s->win_size - ret) * sizeof(float)); |
1057 | } |
1058 | } |
1059 | |
1060 | ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels); |
1061 | acalc_magnitudes(s); |
1062 | |
1063 | consumed += spf; |
1064 | if (consumed >= spb) { |
1065 | int h = s->orientation == VERTICAL ? s->h : s->w; |
1066 | |
1067 | scale_magnitudes(s, 1. / (consumed / spf)); |
1068 | plot_spectrum_column(inlink, fin); |
1069 | consumed = 0; |
1070 | x++; |
1071 | for (ch = 0; ch < s->nb_display_channels; ch++) |
1072 | memset(s->magnitudes[ch], 0, h * sizeof(float)); |
1073 | } |
1074 | } |
1075 | |
1076 | av_frame_free(&fin); |
1077 | s->outpicref->pts = 0; |
1078 | |
1079 | if (s->legend) { |
1080 | int multi = (s->mode == SEPARATE && s->color_mode == CHANNEL); |
1081 | float spp = samples / (float)sz; |
1082 | uint8_t *dst; |
1083 | |
1084 | drawtext(s->outpicref, 2, outlink->h - 10, "CREATED BY LIBAVFILTER", 0); |
1085 | |
1086 | dst = s->outpicref->data[0] + (s->start_y - 1) * s->outpicref->linesize[0] + s->start_x - 1; |
1087 | for (x = 0; x < s->w + 1; x++) |
1088 | dst[x] = 200; |
1089 | dst = s->outpicref->data[0] + (s->start_y + s->h) * s->outpicref->linesize[0] + s->start_x - 1; |
1090 | for (x = 0; x < s->w + 1; x++) |
1091 | dst[x] = 200; |
1092 | for (y = 0; y < s->h + 2; y++) { |
1093 | dst = s->outpicref->data[0] + (y + s->start_y - 1) * s->outpicref->linesize[0]; |
1094 | dst[s->start_x - 1] = 200; |
1095 | dst[s->start_x + s->w] = 200; |
1096 | } |
1097 | if (s->orientation == VERTICAL) { |
1098 | int h = s->mode == SEPARATE ? s->h / s->nb_display_channels : s->h; |
1099 | for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) { |
1100 | for (y = 0; y < h; y += 20) { |
1101 | dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[0]; |
1102 | dst[s->start_x - 2] = 200; |
1103 | dst[s->start_x + s->w + 1] = 200; |
1104 | } |
1105 | for (y = 0; y < h; y += 40) { |
1106 | dst = s->outpicref->data[0] + (s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[0]; |
1107 | dst[s->start_x - 3] = 200; |
1108 | dst[s->start_x + s->w + 2] = 200; |
1109 | } |
1110 | dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x; |
1111 | for (x = 0; x < s->w; x+=40) |
1112 | dst[x] = 200; |
1113 | dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x; |
1114 | for (x = 0; x < s->w; x+=80) |
1115 | dst[x] = 200; |
1116 | dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x; |
1117 | for (x = 0; x < s->w; x+=40) { |
1118 | dst[x] = 200; |
1119 | } |
1120 | dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x; |
1121 | for (x = 0; x < s->w; x+=80) { |
1122 | dst[x] = 200; |
1123 | } |
1124 | for (y = 0; y < h; y += 40) { |
1125 | float hertz = y * (inlink->sample_rate / 2) / (float)(1 << (int)ceil(log2(h))); |
1126 | char *units; |
1127 | |
1128 | if (hertz == 0) |
1129 | units = av_asprintf("DC"); |
1130 | else |
1131 | units = av_asprintf("%.2f", hertz); |
1132 | if (!units) |
1133 | return AVERROR(ENOMEM); |
1134 | |
1135 | drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, h * (ch + 1) + s->start_y - y - 4, units, 0); |
1136 | av_free(units); |
1137 | } |
1138 | } |
1139 | |
1140 | for (x = 0; x < s->w; x+=80) { |
1141 | float seconds = x * spp / inlink->sample_rate; |
1142 | char *units; |
1143 | |
1144 | if (x == 0) |
1145 | units = av_asprintf("0"); |
1146 | else if (log10(seconds) > 6) |
1147 | units = av_asprintf("%.2fh", seconds / (60 * 60)); |
1148 | else if (log10(seconds) > 3) |
1149 | units = av_asprintf("%.2fm", seconds / 60); |
1150 | else |
1151 | units = av_asprintf("%.2fs", seconds); |
1152 | if (!units) |
1153 | return AVERROR(ENOMEM); |
1154 | |
1155 | drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->h + s->start_y + 6, units, 0); |
1156 | drawtext(s->outpicref, s->start_x + x - 4 * strlen(units), s->start_y - 12, units, 0); |
1157 | av_free(units); |
1158 | } |
1159 | |
1160 | drawtext(s->outpicref, outlink->w / 2 - 4 * 4, outlink->h - s->start_y / 2, "TIME", 0); |
1161 | drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 14 * 4, "FREQUENCY (Hz)", 1); |
1162 | } else { |
1163 | int w = s->mode == SEPARATE ? s->w / s->nb_display_channels : s->w; |
1164 | for (y = 0; y < s->h; y += 20) { |
1165 | dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0]; |
1166 | dst[s->start_x - 2] = 200; |
1167 | dst[s->start_x + s->w + 1] = 200; |
1168 | } |
1169 | for (y = 0; y < s->h; y += 40) { |
1170 | dst = s->outpicref->data[0] + (s->start_y + y) * s->outpicref->linesize[0]; |
1171 | dst[s->start_x - 3] = 200; |
1172 | dst[s->start_x + s->w + 2] = 200; |
1173 | } |
1174 | for (ch = 0; ch < (s->mode == SEPARATE ? s->nb_display_channels : 1); ch++) { |
1175 | dst = s->outpicref->data[0] + (s->start_y - 2) * s->outpicref->linesize[0] + s->start_x + w * ch; |
1176 | for (x = 0; x < w; x+=40) |
1177 | dst[x] = 200; |
1178 | dst = s->outpicref->data[0] + (s->start_y - 3) * s->outpicref->linesize[0] + s->start_x + w * ch; |
1179 | for (x = 0; x < w; x+=80) |
1180 | dst[x] = 200; |
1181 | dst = s->outpicref->data[0] + (s->h + s->start_y + 1) * s->outpicref->linesize[0] + s->start_x + w * ch; |
1182 | for (x = 0; x < w; x+=40) { |
1183 | dst[x] = 200; |
1184 | } |
1185 | dst = s->outpicref->data[0] + (s->h + s->start_y + 2) * s->outpicref->linesize[0] + s->start_x + w * ch; |
1186 | for (x = 0; x < w; x+=80) { |
1187 | dst[x] = 200; |
1188 | } |
1189 | for (x = 0; x < w; x += 80) { |
1190 | float hertz = x * (inlink->sample_rate / 2) / (float)(1 << (int)ceil(log2(w))); |
1191 | char *units; |
1192 | |
1193 | if (hertz == 0) |
1194 | units = av_asprintf("DC"); |
1195 | else |
1196 | units = av_asprintf("%.2f", hertz); |
1197 | if (!units) |
1198 | return AVERROR(ENOMEM); |
1199 | |
1200 | drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->start_y - 12, units, 0); |
1201 | drawtext(s->outpicref, s->start_x - 4 * strlen(units) + x + w * ch, s->h + s->start_y + 6, units, 0); |
1202 | av_free(units); |
1203 | } |
1204 | } |
1205 | for (y = 0; y < s->h; y+=40) { |
1206 | float seconds = y * spp / inlink->sample_rate; |
1207 | char *units; |
1208 | |
1209 | if (x == 0) |
1210 | units = av_asprintf("0"); |
1211 | else if (log10(seconds) > 6) |
1212 | units = av_asprintf("%.2fh", seconds / (60 * 60)); |
1213 | else if (log10(seconds) > 3) |
1214 | units = av_asprintf("%.2fm", seconds / 60); |
1215 | else |
1216 | units = av_asprintf("%.2fs", seconds); |
1217 | if (!units) |
1218 | return AVERROR(ENOMEM); |
1219 | |
1220 | drawtext(s->outpicref, s->start_x - 8 * strlen(units) - 4, s->start_y + y - 4, units, 0); |
1221 | av_free(units); |
1222 | } |
1223 | drawtext(s->outpicref, s->start_x / 7, outlink->h / 2 - 4 * 4, "TIME", 1); |
1224 | drawtext(s->outpicref, outlink->w / 2 - 14 * 4, outlink->h - s->start_y / 2, "FREQUENCY (Hz)", 0); |
1225 | } |
1226 | |
1227 | for (ch = 0; ch < (multi ? s->nb_display_channels : 1); ch++) { |
1228 | int h = multi ? s->h / s->nb_display_channels : s->h; |
1229 | |
1230 | for (y = 0; y < h; y++) { |
1231 | float out[3] = { 0., 127.5, 127.5}; |
1232 | int chn; |
1233 | |
1234 | for (chn = 0; chn < (s->mode == SEPARATE ? 1 : s->nb_display_channels); chn++) { |
1235 | float yf, uf, vf; |
1236 | int channel = (multi) ? s->nb_display_channels - ch - 1 : chn; |
1237 | float lout[3]; |
1238 | |
1239 | color_range(s, channel, &yf, &uf, &vf); |
1240 | pick_color(s, yf, uf, vf, y / (float)h, lout); |
1241 | out[0] += lout[0]; |
1242 | out[1] += lout[1]; |
1243 | out[2] += lout[2]; |
1244 | } |
1245 | memset(s->outpicref->data[0]+(s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[0] + s->w + s->start_x + 20, av_clip_uint8(out[0]), 10); |
1246 | memset(s->outpicref->data[1]+(s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[1] + s->w + s->start_x + 20, av_clip_uint8(out[1]), 10); |
1247 | memset(s->outpicref->data[2]+(s->start_y + h * (ch + 1) - y - 1) * s->outpicref->linesize[2] + s->w + s->start_x + 20, av_clip_uint8(out[2]), 10); |
1248 | } |
1249 | |
1250 | for (y = 0; ch == 0 && y < h; y += h / 10) { |
1251 | float value = 120.0 * log10(1. - y / (float)h); |
1252 | char *text; |
1253 | |
1254 | if (value < -120) |
1255 | break; |
1256 | text = av_asprintf("%.0f dB", value); |
1257 | if (!text) |
1258 | continue; |
1259 | drawtext(s->outpicref, s->w + s->start_x + 35, s->start_y + y - 5, text, 0); |
1260 | av_free(text); |
1261 | } |
1262 | } |
1263 | } |
1264 | |
1265 | ret = ff_filter_frame(outlink, s->outpicref); |
1266 | s->outpicref = NULL; |
1267 | } |
1268 | |
1269 | return ret; |
1270 | } |
1271 | |
1272 | static int showspectrumpic_filter_frame(AVFilterLink *inlink, AVFrame *insamples) |
1273 | { |
1274 | AVFilterContext *ctx = inlink->dst; |
1275 | ShowSpectrumContext *s = ctx->priv; |
1276 | int ret; |
1277 | |
1278 | ret = av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples); |
1279 | av_frame_free(&insamples); |
1280 | return ret; |
1281 | } |
1282 | |
1283 | static const AVFilterPad showspectrumpic_inputs[] = { |
1284 | { |
1285 | .name = "default", |
1286 | .type = AVMEDIA_TYPE_AUDIO, |
1287 | .filter_frame = showspectrumpic_filter_frame, |
1288 | }, |
1289 | { NULL } |
1290 | }; |
1291 | |
1292 | static const AVFilterPad showspectrumpic_outputs[] = { |
1293 | { |
1294 | .name = "default", |
1295 | .type = AVMEDIA_TYPE_VIDEO, |
1296 | .config_props = config_output, |
1297 | .request_frame = showspectrumpic_request_frame, |
1298 | }, |
1299 | { NULL } |
1300 | }; |
1301 | |
1302 | AVFilter ff_avf_showspectrumpic = { |
1303 | .name = "showspectrumpic", |
1304 | .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output single picture."), |
1305 | .uninit = uninit, |
1306 | .query_formats = query_formats, |
1307 | .priv_size = sizeof(ShowSpectrumContext), |
1308 | .inputs = showspectrumpic_inputs, |
1309 | .outputs = showspectrumpic_outputs, |
1310 | .priv_class = &showspectrumpic_class, |
1311 | .flags = AVFILTER_FLAG_SLICE_THREADS, |
1312 | }; |
1313 | |
1314 | #endif // CONFIG_SHOWSPECTRUMPIC_FILTER |
1315 |