Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Copyright (c) 2015 Paul B Mahol |
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 |
|
|
#include <float.h> |
22 |
|
|
#include <math.h> |
23 |
|
|
|
24 |
|
|
#include "libavutil/mem.h" |
25 |
|
|
#include "libavutil/tx.h" |
26 |
|
|
#include "libavutil/avassert.h" |
27 |
|
|
#include "libavutil/avstring.h" |
28 |
|
|
#include "libavutil/channel_layout.h" |
29 |
|
|
#include "libavutil/intreadwrite.h" |
30 |
|
|
#include "libavutil/opt.h" |
31 |
|
|
#include "libavutil/parseutils.h" |
32 |
|
|
#include "audio.h" |
33 |
|
|
#include "filters.h" |
34 |
|
|
#include "formats.h" |
35 |
|
|
#include "video.h" |
36 |
|
|
#include "avfilter.h" |
37 |
|
|
#include "window_func.h" |
38 |
|
|
|
39 |
|
|
enum DataMode { MAGNITUDE, PHASE, DELAY, NB_DATA }; |
40 |
|
|
enum DisplayMode { LINE, BAR, DOT, NB_MODES }; |
41 |
|
|
enum ChannelMode { COMBINED, SEPARATE, NB_CMODES }; |
42 |
|
|
enum FrequencyScale { FS_LINEAR, FS_LOG, FS_RLOG, NB_FSCALES }; |
43 |
|
|
enum AmplitudeScale { AS_LINEAR, AS_SQRT, AS_CBRT, AS_LOG, NB_ASCALES }; |
44 |
|
|
|
45 |
|
|
typedef struct ShowFreqsContext { |
46 |
|
|
const AVClass *class; |
47 |
|
|
int w, h; |
48 |
|
|
int mode; |
49 |
|
|
int data_mode; |
50 |
|
|
int cmode; |
51 |
|
|
int fft_size; |
52 |
|
|
int ascale, fscale; |
53 |
|
|
int avg; |
54 |
|
|
int win_func; |
55 |
|
|
char *ch_layout_str; |
56 |
|
|
uint8_t *bypass; |
57 |
|
|
AVChannelLayout ch_layout; |
58 |
|
|
AVTXContext *fft; |
59 |
|
|
av_tx_fn tx_fn; |
60 |
|
|
AVComplexFloat **fft_input; |
61 |
|
|
AVComplexFloat **fft_data; |
62 |
|
|
AVFrame *window; |
63 |
|
|
float **avg_data; |
64 |
|
|
float *window_func_lut; |
65 |
|
|
float overlap; |
66 |
|
|
float minamp; |
67 |
|
|
int hop_size; |
68 |
|
|
int nb_channels; |
69 |
|
|
int nb_draw_channels; |
70 |
|
|
int nb_freq; |
71 |
|
|
int win_size; |
72 |
|
|
float scale; |
73 |
|
|
char *colors; |
74 |
|
|
int64_t pts; |
75 |
|
|
int64_t old_pts; |
76 |
|
|
AVRational frame_rate; |
77 |
|
|
} ShowFreqsContext; |
78 |
|
|
|
79 |
|
|
#define OFFSET(x) offsetof(ShowFreqsContext, x) |
80 |
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM |
81 |
|
|
|
82 |
|
|
static const AVOption showfreqs_options[] = { |
83 |
|
|
{ "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1024x512"}, 0, 0, FLAGS }, |
84 |
|
|
{ "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1024x512"}, 0, 0, FLAGS }, |
85 |
|
|
{ "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS }, |
86 |
|
|
{ "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS }, |
87 |
|
|
{ "mode", "set display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=BAR}, 0, NB_MODES-1, FLAGS, .unit = "mode" }, |
88 |
|
|
{ "line", "show lines", 0, AV_OPT_TYPE_CONST, {.i64=LINE}, 0, 0, FLAGS, .unit = "mode" }, |
89 |
|
|
{ "bar", "show bars", 0, AV_OPT_TYPE_CONST, {.i64=BAR}, 0, 0, FLAGS, .unit = "mode" }, |
90 |
|
|
{ "dot", "show dots", 0, AV_OPT_TYPE_CONST, {.i64=DOT}, 0, 0, FLAGS, .unit = "mode" }, |
91 |
|
|
{ "ascale", "set amplitude scale", OFFSET(ascale), AV_OPT_TYPE_INT, {.i64=AS_LOG}, 0, NB_ASCALES-1, FLAGS, .unit = "ascale" }, |
92 |
|
|
{ "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=AS_LINEAR}, 0, 0, FLAGS, .unit = "ascale" }, |
93 |
|
|
{ "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=AS_SQRT}, 0, 0, FLAGS, .unit = "ascale" }, |
94 |
|
|
{ "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=AS_CBRT}, 0, 0, FLAGS, .unit = "ascale" }, |
95 |
|
|
{ "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=AS_LOG}, 0, 0, FLAGS, .unit = "ascale" }, |
96 |
|
|
{ "fscale", "set frequency scale", OFFSET(fscale), AV_OPT_TYPE_INT, {.i64=FS_LINEAR}, 0, NB_FSCALES-1, FLAGS, .unit = "fscale" }, |
97 |
|
|
{ "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=FS_LINEAR}, 0, 0, FLAGS, .unit = "fscale" }, |
98 |
|
|
{ "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=FS_LOG}, 0, 0, FLAGS, .unit = "fscale" }, |
99 |
|
|
{ "rlog", "reverse logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=FS_RLOG}, 0, 0, FLAGS, .unit = "fscale" }, |
100 |
|
|
{ "win_size", "set window size", OFFSET(fft_size), AV_OPT_TYPE_INT, {.i64=2048}, 16, 65536, FLAGS }, |
101 |
|
|
WIN_FUNC_OPTION("win_func", OFFSET(win_func), FLAGS, WFUNC_HANNING), |
102 |
|
|
{ "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=1.}, 0., 1., FLAGS }, |
103 |
|
|
{ "averaging", "set time averaging", OFFSET(avg), AV_OPT_TYPE_INT, {.i64=1}, 0, INT32_MAX, FLAGS }, |
104 |
|
|
{ "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS }, |
105 |
|
|
{ "cmode", "set channel mode", OFFSET(cmode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_CMODES-1, FLAGS, .unit = "cmode" }, |
106 |
|
|
{ "combined", "show all channels in same window", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, .unit = "cmode" }, |
107 |
|
|
{ "separate", "show each channel in own window", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, .unit = "cmode" }, |
108 |
|
|
{ "minamp", "set minimum amplitude", OFFSET(minamp), AV_OPT_TYPE_FLOAT, {.dbl=1e-6}, FLT_MIN, 1e-6, FLAGS }, |
109 |
|
|
{ "data", "set data mode", OFFSET(data_mode), AV_OPT_TYPE_INT, {.i64=MAGNITUDE}, 0, NB_DATA-1, FLAGS, .unit = "data" }, |
110 |
|
|
{ "magnitude", "show magnitude", 0, AV_OPT_TYPE_CONST, {.i64=MAGNITUDE}, 0, 0, FLAGS, .unit = "data" }, |
111 |
|
|
{ "phase", "show phase", 0, AV_OPT_TYPE_CONST, {.i64=PHASE}, 0, 0, FLAGS, .unit = "data" }, |
112 |
|
|
{ "delay", "show group delay",0, AV_OPT_TYPE_CONST, {.i64=DELAY}, 0, 0, FLAGS, .unit = "data" }, |
113 |
|
|
{ "channels", "set channels to draw", OFFSET(ch_layout_str), AV_OPT_TYPE_STRING, {.str="all"}, 0, 0, FLAGS }, |
114 |
|
|
{ NULL } |
115 |
|
|
}; |
116 |
|
|
|
117 |
|
|
AVFILTER_DEFINE_CLASS(showfreqs); |
118 |
|
|
|
119 |
|
✗ |
static int query_formats(AVFilterContext *ctx) |
120 |
|
|
{ |
121 |
|
✗ |
AVFilterFormats *formats = NULL; |
122 |
|
✗ |
AVFilterChannelLayouts *layouts = NULL; |
123 |
|
✗ |
AVFilterLink *inlink = ctx->inputs[0]; |
124 |
|
✗ |
AVFilterLink *outlink = ctx->outputs[0]; |
125 |
|
|
static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE }; |
126 |
|
|
static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE }; |
127 |
|
|
int ret; |
128 |
|
|
|
129 |
|
|
/* set input audio formats */ |
130 |
|
✗ |
formats = ff_make_format_list(sample_fmts); |
131 |
|
✗ |
if ((ret = ff_formats_ref(formats, &inlink->outcfg.formats)) < 0) |
132 |
|
✗ |
return ret; |
133 |
|
|
|
134 |
|
✗ |
layouts = ff_all_channel_counts(); |
135 |
|
✗ |
if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0) |
136 |
|
✗ |
return ret; |
137 |
|
|
|
138 |
|
✗ |
formats = ff_all_samplerates(); |
139 |
|
✗ |
if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0) |
140 |
|
✗ |
return ret; |
141 |
|
|
|
142 |
|
|
/* set output video format */ |
143 |
|
✗ |
formats = ff_make_format_list(pix_fmts); |
144 |
|
✗ |
if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0) |
145 |
|
✗ |
return ret; |
146 |
|
|
|
147 |
|
✗ |
return 0; |
148 |
|
|
} |
149 |
|
|
|
150 |
|
✗ |
static int config_output(AVFilterLink *outlink) |
151 |
|
|
{ |
152 |
|
✗ |
FilterLink *l = ff_filter_link(outlink); |
153 |
|
✗ |
AVFilterContext *ctx = outlink->src; |
154 |
|
✗ |
AVFilterLink *inlink = ctx->inputs[0]; |
155 |
|
✗ |
ShowFreqsContext *s = ctx->priv; |
156 |
|
✗ |
float overlap, scale = 1.f; |
157 |
|
|
int i, ret; |
158 |
|
|
|
159 |
|
✗ |
s->old_pts = AV_NOPTS_VALUE; |
160 |
|
✗ |
s->nb_freq = s->fft_size / 2; |
161 |
|
✗ |
s->win_size = s->fft_size; |
162 |
|
✗ |
av_tx_uninit(&s->fft); |
163 |
|
✗ |
ret = av_tx_init(&s->fft, &s->tx_fn, AV_TX_FLOAT_FFT, 0, s->fft_size, &scale, 0); |
164 |
|
✗ |
if (ret < 0) { |
165 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. " |
166 |
|
|
"The window size might be too high.\n"); |
167 |
|
✗ |
return ret; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
/* FFT buffers: x2 for each (display) channel buffer. |
171 |
|
|
* Note: we use free and malloc instead of a realloc-like function to |
172 |
|
|
* make sure the buffer is aligned in memory for the FFT functions. */ |
173 |
|
✗ |
for (i = 0; i < s->nb_channels; i++) { |
174 |
|
✗ |
av_freep(&s->fft_input[i]); |
175 |
|
✗ |
av_freep(&s->fft_data[i]); |
176 |
|
✗ |
av_freep(&s->avg_data[i]); |
177 |
|
|
} |
178 |
|
✗ |
av_freep(&s->bypass); |
179 |
|
✗ |
av_freep(&s->fft_input); |
180 |
|
✗ |
av_freep(&s->fft_data); |
181 |
|
✗ |
av_freep(&s->avg_data); |
182 |
|
✗ |
s->nb_channels = inlink->ch_layout.nb_channels; |
183 |
|
|
|
184 |
|
✗ |
s->bypass = av_calloc(s->nb_channels, sizeof(*s->bypass)); |
185 |
|
✗ |
if (!s->bypass) |
186 |
|
✗ |
return AVERROR(ENOMEM); |
187 |
|
✗ |
s->fft_input = av_calloc(s->nb_channels, sizeof(*s->fft_input)); |
188 |
|
✗ |
if (!s->fft_input) |
189 |
|
✗ |
return AVERROR(ENOMEM); |
190 |
|
✗ |
s->fft_data = av_calloc(s->nb_channels, sizeof(*s->fft_data)); |
191 |
|
✗ |
if (!s->fft_data) |
192 |
|
✗ |
return AVERROR(ENOMEM); |
193 |
|
✗ |
s->avg_data = av_calloc(s->nb_channels, sizeof(*s->avg_data)); |
194 |
|
✗ |
if (!s->avg_data) |
195 |
|
✗ |
return AVERROR(ENOMEM); |
196 |
|
✗ |
for (i = 0; i < s->nb_channels; i++) { |
197 |
|
✗ |
s->fft_input[i] = av_calloc(FFALIGN(s->win_size, 512), sizeof(**s->fft_input)); |
198 |
|
✗ |
s->fft_data[i] = av_calloc(FFALIGN(s->win_size, 512), sizeof(**s->fft_data)); |
199 |
|
✗ |
s->avg_data[i] = av_calloc(s->nb_freq, sizeof(**s->avg_data)); |
200 |
|
✗ |
if (!s->fft_data[i] || !s->avg_data[i] || !s->fft_input[i]) |
201 |
|
✗ |
return AVERROR(ENOMEM); |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
/* pre-calc windowing function */ |
205 |
|
✗ |
s->window_func_lut = av_realloc_f(s->window_func_lut, s->win_size, |
206 |
|
|
sizeof(*s->window_func_lut)); |
207 |
|
✗ |
if (!s->window_func_lut) |
208 |
|
✗ |
return AVERROR(ENOMEM); |
209 |
|
✗ |
generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap); |
210 |
|
✗ |
if (s->overlap == 1.) |
211 |
|
✗ |
s->overlap = overlap; |
212 |
|
✗ |
s->hop_size = (1. - s->overlap) * s->win_size; |
213 |
|
✗ |
if (s->hop_size < 1) { |
214 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap); |
215 |
|
✗ |
return AVERROR(EINVAL); |
216 |
|
|
} |
217 |
|
|
|
218 |
|
✗ |
for (s->scale = 0, i = 0; i < s->win_size; i++) { |
219 |
|
✗ |
s->scale += s->window_func_lut[i] * s->window_func_lut[i]; |
220 |
|
|
} |
221 |
|
|
|
222 |
|
✗ |
s->window = ff_get_audio_buffer(inlink, s->win_size * 2); |
223 |
|
✗ |
if (!s->window) |
224 |
|
✗ |
return AVERROR(ENOMEM); |
225 |
|
|
|
226 |
|
✗ |
l->frame_rate = s->frame_rate; |
227 |
|
✗ |
outlink->time_base = av_inv_q(l->frame_rate); |
228 |
|
✗ |
outlink->sample_aspect_ratio = (AVRational){1,1}; |
229 |
|
✗ |
outlink->w = s->w; |
230 |
|
✗ |
outlink->h = s->h; |
231 |
|
|
|
232 |
|
✗ |
ret = av_channel_layout_copy(&s->ch_layout, &inlink->ch_layout); |
233 |
|
✗ |
if (ret < 0) |
234 |
|
✗ |
return ret; |
235 |
|
✗ |
s->nb_draw_channels = s->nb_channels; |
236 |
|
|
|
237 |
|
✗ |
if (strcmp(s->ch_layout_str, "all")) { |
238 |
|
✗ |
int nb_draw_channels = 0; |
239 |
|
✗ |
av_channel_layout_from_string(&s->ch_layout, |
240 |
|
✗ |
s->ch_layout_str); |
241 |
|
|
|
242 |
|
✗ |
for (int ch = 0; ch < s->nb_channels; ch++) { |
243 |
|
✗ |
const enum AVChannel channel = av_channel_layout_channel_from_index(&inlink->ch_layout, ch); |
244 |
|
|
|
245 |
|
✗ |
s->bypass[ch] = av_channel_layout_index_from_channel(&s->ch_layout, channel) < 0; |
246 |
|
✗ |
nb_draw_channels += s->bypass[ch] == 0; |
247 |
|
|
} |
248 |
|
|
|
249 |
|
✗ |
s->nb_draw_channels = nb_draw_channels; |
250 |
|
|
} |
251 |
|
|
|
252 |
|
✗ |
return 0; |
253 |
|
|
} |
254 |
|
|
|
255 |
|
✗ |
static inline void draw_dot(AVFrame *out, int x, int y, uint8_t fg[4]) |
256 |
|
|
{ |
257 |
|
|
|
258 |
|
✗ |
uint32_t color = AV_RL32(out->data[0] + y * out->linesize[0] + x * 4); |
259 |
|
|
|
260 |
|
✗ |
if ((color & 0xffffff) != 0) |
261 |
|
✗ |
AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg) | color); |
262 |
|
|
else |
263 |
|
✗ |
AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg)); |
264 |
|
✗ |
} |
265 |
|
|
|
266 |
|
✗ |
static int get_sx(ShowFreqsContext *s, int f) |
267 |
|
|
{ |
268 |
|
✗ |
switch (s->fscale) { |
269 |
|
✗ |
case FS_LINEAR: |
270 |
|
✗ |
return (s->w/(float)s->nb_freq)*f; |
271 |
|
✗ |
case FS_LOG: |
272 |
|
✗ |
return s->w-pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.)); |
273 |
|
✗ |
case FS_RLOG: |
274 |
|
✗ |
return pow(s->w, f/(s->nb_freq-1.)); |
275 |
|
|
} |
276 |
|
|
|
277 |
|
✗ |
return 0; |
278 |
|
|
} |
279 |
|
|
|
280 |
|
✗ |
static float get_bsize(ShowFreqsContext *s, int f) |
281 |
|
|
{ |
282 |
|
✗ |
switch (s->fscale) { |
283 |
|
✗ |
case FS_LINEAR: |
284 |
|
✗ |
return s->w/(float)s->nb_freq; |
285 |
|
✗ |
case FS_LOG: |
286 |
|
✗ |
return pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.))- |
287 |
|
✗ |
pow(s->w, (s->nb_freq-f-2)/(s->nb_freq-1.)); |
288 |
|
✗ |
case FS_RLOG: |
289 |
|
✗ |
return pow(s->w, (f+1)/(s->nb_freq-1.))- |
290 |
|
✗ |
pow(s->w, f /(s->nb_freq-1.)); |
291 |
|
|
} |
292 |
|
|
|
293 |
|
✗ |
return 1.; |
294 |
|
|
} |
295 |
|
|
|
296 |
|
✗ |
static inline void plot_freq(ShowFreqsContext *s, int ch, |
297 |
|
|
double a, int f, uint8_t fg[4], int *prev_y, |
298 |
|
|
AVFrame *out, AVFilterLink *outlink) |
299 |
|
|
{ |
300 |
|
✗ |
FilterLink *outl = ff_filter_link(outlink); |
301 |
|
✗ |
const int w = s->w; |
302 |
|
✗ |
const float min = s->minamp; |
303 |
|
✗ |
const float avg = s->avg_data[ch][f]; |
304 |
|
✗ |
const float bsize = get_bsize(s, f); |
305 |
|
✗ |
const int sx = get_sx(s, f); |
306 |
|
✗ |
int end = outlink->h; |
307 |
|
|
int x, y, i; |
308 |
|
|
|
309 |
|
✗ |
switch(s->ascale) { |
310 |
|
✗ |
case AS_SQRT: |
311 |
|
✗ |
a = 1.0 - sqrt(a); |
312 |
|
✗ |
break; |
313 |
|
✗ |
case AS_CBRT: |
314 |
|
✗ |
a = 1.0 - cbrt(a); |
315 |
|
✗ |
break; |
316 |
|
✗ |
case AS_LOG: |
317 |
|
✗ |
a = log(av_clipd(a, min, 1)) / log(min); |
318 |
|
✗ |
break; |
319 |
|
✗ |
case AS_LINEAR: |
320 |
|
✗ |
a = 1.0 - a; |
321 |
|
✗ |
break; |
322 |
|
|
} |
323 |
|
|
|
324 |
|
✗ |
switch (s->cmode) { |
325 |
|
✗ |
case COMBINED: |
326 |
|
✗ |
y = a * outlink->h - 1; |
327 |
|
✗ |
break; |
328 |
|
✗ |
case SEPARATE: |
329 |
|
✗ |
end = (outlink->h / s->nb_draw_channels) * (ch + 1); |
330 |
|
✗ |
y = (outlink->h / s->nb_draw_channels) * ch + a * (outlink->h / s->nb_draw_channels) - 1; |
331 |
|
✗ |
break; |
332 |
|
✗ |
default: |
333 |
|
✗ |
av_assert0(0); |
334 |
|
|
} |
335 |
|
✗ |
if (y < 0) |
336 |
|
✗ |
return; |
337 |
|
|
|
338 |
|
✗ |
switch (s->avg) { |
339 |
|
✗ |
case 0: |
340 |
|
✗ |
y = s->avg_data[ch][f] = !outl->frame_count_in ? y : FFMIN(0, y); |
341 |
|
✗ |
break; |
342 |
|
✗ |
case 1: |
343 |
|
✗ |
break; |
344 |
|
✗ |
default: |
345 |
|
✗ |
s->avg_data[ch][f] = avg + y * (y - avg) / (FFMIN(outl->frame_count_in + 1, s->avg) * (float)y); |
346 |
|
✗ |
y = av_clip(s->avg_data[ch][f], 0, outlink->h - 1); |
347 |
|
✗ |
break; |
348 |
|
|
} |
349 |
|
|
|
350 |
|
✗ |
switch(s->mode) { |
351 |
|
✗ |
case LINE: |
352 |
|
✗ |
if (*prev_y == -1) { |
353 |
|
✗ |
*prev_y = y; |
354 |
|
|
} |
355 |
|
✗ |
if (y <= *prev_y) { |
356 |
|
✗ |
for (x = sx + 1; x < sx + bsize && x < w; x++) |
357 |
|
✗ |
draw_dot(out, x, y, fg); |
358 |
|
✗ |
for (i = y; i <= *prev_y; i++) |
359 |
|
✗ |
draw_dot(out, sx, i, fg); |
360 |
|
|
} else { |
361 |
|
✗ |
for (i = *prev_y; i <= y; i++) |
362 |
|
✗ |
draw_dot(out, sx, i, fg); |
363 |
|
✗ |
for (x = sx + 1; x < sx + bsize && x < w; x++) |
364 |
|
✗ |
draw_dot(out, x, i - 1, fg); |
365 |
|
|
} |
366 |
|
✗ |
*prev_y = y; |
367 |
|
✗ |
break; |
368 |
|
✗ |
case BAR: |
369 |
|
✗ |
for (x = sx; x < sx + bsize && x < w; x++) |
370 |
|
✗ |
for (i = y; i < end; i++) |
371 |
|
✗ |
draw_dot(out, x, i, fg); |
372 |
|
✗ |
break; |
373 |
|
✗ |
case DOT: |
374 |
|
✗ |
for (x = sx; x < sx + bsize && x < w; x++) |
375 |
|
✗ |
draw_dot(out, x, y, fg); |
376 |
|
✗ |
break; |
377 |
|
|
} |
378 |
|
|
} |
379 |
|
|
|
380 |
|
✗ |
static int plot_freqs(AVFilterLink *inlink, int64_t pts) |
381 |
|
|
{ |
382 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
383 |
|
✗ |
AVFilterLink *outlink = ctx->outputs[0]; |
384 |
|
✗ |
ShowFreqsContext *s = ctx->priv; |
385 |
|
✗ |
AVFrame *in = s->window; |
386 |
|
✗ |
const int win_size = s->win_size; |
387 |
|
✗ |
char *colors, *color, *saveptr = NULL; |
388 |
|
|
AVFrame *out; |
389 |
|
|
int ch, n; |
390 |
|
|
|
391 |
|
|
/* fill FFT input with the number of samples available */ |
392 |
|
✗ |
for (ch = 0; ch < s->nb_channels; ch++) { |
393 |
|
✗ |
const float *p = (float *)in->extended_data[ch]; |
394 |
|
|
|
395 |
|
✗ |
if (s->bypass[ch]) |
396 |
|
✗ |
continue; |
397 |
|
|
|
398 |
|
✗ |
for (n = 0; n < win_size; n++) { |
399 |
|
✗ |
s->fft_input[ch][n].re = p[n] * s->window_func_lut[n]; |
400 |
|
✗ |
s->fft_input[ch][n].im = 0; |
401 |
|
|
} |
402 |
|
|
} |
403 |
|
|
|
404 |
|
|
/* run FFT on each samples set */ |
405 |
|
✗ |
for (ch = 0; ch < s->nb_channels; ch++) { |
406 |
|
✗ |
if (s->bypass[ch]) |
407 |
|
✗ |
continue; |
408 |
|
|
|
409 |
|
✗ |
s->tx_fn(s->fft, s->fft_data[ch], s->fft_input[ch], sizeof(AVComplexFloat)); |
410 |
|
|
} |
411 |
|
|
|
412 |
|
✗ |
s->pts = av_rescale_q(pts, inlink->time_base, outlink->time_base); |
413 |
|
✗ |
if (s->old_pts >= s->pts) |
414 |
|
✗ |
return 0; |
415 |
|
✗ |
s->old_pts = s->pts; |
416 |
|
|
|
417 |
|
|
#define RE(x, ch) s->fft_data[ch][x].re |
418 |
|
|
#define IM(x, ch) s->fft_data[ch][x].im |
419 |
|
|
#define M(a, b) (sqrt((a) * (a) + (b) * (b))) |
420 |
|
|
#define P(a, b) (atan2((b), (a))) |
421 |
|
|
|
422 |
|
✗ |
colors = av_strdup(s->colors); |
423 |
|
✗ |
if (!colors) |
424 |
|
✗ |
return AVERROR(ENOMEM); |
425 |
|
|
|
426 |
|
✗ |
out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
427 |
|
✗ |
if (!out) { |
428 |
|
✗ |
av_free(colors); |
429 |
|
✗ |
return AVERROR(ENOMEM); |
430 |
|
|
} |
431 |
|
|
|
432 |
|
✗ |
for (n = 0; n < outlink->h; n++) |
433 |
|
✗ |
memset(out->data[0] + out->linesize[0] * n, 0, outlink->w * 4); |
434 |
|
|
|
435 |
|
✗ |
for (ch = 0; ch < s->nb_channels; ch++) { |
436 |
|
✗ |
uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff }; |
437 |
|
✗ |
int prev_y = -1, f; |
438 |
|
|
double a; |
439 |
|
|
|
440 |
|
✗ |
color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr); |
441 |
|
✗ |
if (color) |
442 |
|
✗ |
av_parse_color(fg, color, -1, ctx); |
443 |
|
|
|
444 |
|
✗ |
if (s->bypass[ch]) |
445 |
|
✗ |
continue; |
446 |
|
|
|
447 |
|
✗ |
switch (s->data_mode) { |
448 |
|
✗ |
case MAGNITUDE: |
449 |
|
✗ |
for (f = 0; f < s->nb_freq; f++) { |
450 |
|
✗ |
a = av_clipd(M(RE(f, ch), IM(f, ch)) / s->scale, 0, 1); |
451 |
|
|
|
452 |
|
✗ |
plot_freq(s, ch, a, f, fg, &prev_y, out, outlink); |
453 |
|
|
} |
454 |
|
✗ |
break; |
455 |
|
✗ |
case PHASE: |
456 |
|
✗ |
for (f = 0; f < s->nb_freq; f++) { |
457 |
|
✗ |
a = av_clipd((M_PI + P(RE(f, ch), IM(f, ch))) / (2. * M_PI), 0, 1); |
458 |
|
|
|
459 |
|
✗ |
plot_freq(s, ch, a, f, fg, &prev_y, out, outlink); |
460 |
|
|
} |
461 |
|
✗ |
break; |
462 |
|
✗ |
case DELAY: |
463 |
|
✗ |
for (f = 0; f < s->nb_freq; f++) { |
464 |
|
✗ |
a = av_clipd((M_PI - P(IM(f, ch) * RE(f-1, ch) - IM(f-1, ch) * RE(f, ch), |
465 |
|
|
RE(f, ch) * RE(f-1, ch) + IM(f, ch) * IM(f-1, ch))) / (2. * M_PI), 0, 1); |
466 |
|
|
|
467 |
|
✗ |
plot_freq(s, ch, a, f, fg, &prev_y, out, outlink); |
468 |
|
|
} |
469 |
|
✗ |
break; |
470 |
|
|
} |
471 |
|
|
} |
472 |
|
|
|
473 |
|
✗ |
av_free(colors); |
474 |
|
✗ |
out->pts = s->pts; |
475 |
|
✗ |
out->duration = 1; |
476 |
|
✗ |
out->sample_aspect_ratio = (AVRational){1,1}; |
477 |
|
✗ |
return ff_filter_frame(outlink, out); |
478 |
|
|
} |
479 |
|
|
|
480 |
|
✗ |
static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
481 |
|
|
{ |
482 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
483 |
|
✗ |
ShowFreqsContext *s = ctx->priv; |
484 |
|
✗ |
const int offset = s->win_size - s->hop_size; |
485 |
|
✗ |
int64_t pts = in->pts; |
486 |
|
|
|
487 |
|
✗ |
for (int ch = 0; ch < in->ch_layout.nb_channels; ch++) { |
488 |
|
✗ |
float *dst = (float *)s->window->extended_data[ch]; |
489 |
|
|
|
490 |
|
✗ |
memmove(dst, &dst[s->hop_size], offset * sizeof(float)); |
491 |
|
✗ |
memcpy(&dst[offset], in->extended_data[ch], in->nb_samples * sizeof(float)); |
492 |
|
✗ |
memset(&dst[offset + in->nb_samples], 0, (s->hop_size - in->nb_samples) * sizeof(float)); |
493 |
|
|
} |
494 |
|
|
|
495 |
|
✗ |
av_frame_free(&in); |
496 |
|
|
|
497 |
|
✗ |
return plot_freqs(inlink, pts); |
498 |
|
|
} |
499 |
|
|
|
500 |
|
✗ |
static int activate(AVFilterContext *ctx) |
501 |
|
|
{ |
502 |
|
✗ |
AVFilterLink *inlink = ctx->inputs[0]; |
503 |
|
✗ |
AVFilterLink *outlink = ctx->outputs[0]; |
504 |
|
✗ |
ShowFreqsContext *s = ctx->priv; |
505 |
|
|
AVFrame *in; |
506 |
|
|
int ret; |
507 |
|
|
|
508 |
|
✗ |
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); |
509 |
|
|
|
510 |
|
✗ |
ret = ff_inlink_consume_samples(inlink, s->hop_size, s->hop_size, &in); |
511 |
|
✗ |
if (ret < 0) |
512 |
|
✗ |
return ret; |
513 |
|
|
|
514 |
|
✗ |
if (ret > 0) |
515 |
|
✗ |
ret = filter_frame(inlink, in); |
516 |
|
✗ |
if (ret < 0) |
517 |
|
✗ |
return ret; |
518 |
|
|
|
519 |
|
✗ |
if (ff_inlink_queued_samples(inlink) >= s->hop_size) { |
520 |
|
✗ |
ff_filter_set_ready(ctx, 10); |
521 |
|
✗ |
return 0; |
522 |
|
|
} |
523 |
|
|
|
524 |
|
✗ |
FF_FILTER_FORWARD_STATUS(inlink, outlink); |
525 |
|
✗ |
FF_FILTER_FORWARD_WANTED(outlink, inlink); |
526 |
|
|
|
527 |
|
✗ |
return FFERROR_NOT_READY; |
528 |
|
|
} |
529 |
|
|
|
530 |
|
✗ |
static av_cold void uninit(AVFilterContext *ctx) |
531 |
|
|
{ |
532 |
|
✗ |
ShowFreqsContext *s = ctx->priv; |
533 |
|
|
int i; |
534 |
|
|
|
535 |
|
✗ |
av_channel_layout_uninit(&s->ch_layout); |
536 |
|
✗ |
av_tx_uninit(&s->fft); |
537 |
|
✗ |
for (i = 0; i < s->nb_channels; i++) { |
538 |
|
✗ |
if (s->fft_input) |
539 |
|
✗ |
av_freep(&s->fft_input[i]); |
540 |
|
✗ |
if (s->fft_data) |
541 |
|
✗ |
av_freep(&s->fft_data[i]); |
542 |
|
✗ |
if (s->avg_data) |
543 |
|
✗ |
av_freep(&s->avg_data[i]); |
544 |
|
|
} |
545 |
|
✗ |
av_freep(&s->bypass); |
546 |
|
✗ |
av_freep(&s->fft_input); |
547 |
|
✗ |
av_freep(&s->fft_data); |
548 |
|
✗ |
av_freep(&s->avg_data); |
549 |
|
✗ |
av_freep(&s->window_func_lut); |
550 |
|
✗ |
av_frame_free(&s->window); |
551 |
|
✗ |
} |
552 |
|
|
|
553 |
|
|
static const AVFilterPad showfreqs_outputs[] = { |
554 |
|
|
{ |
555 |
|
|
.name = "default", |
556 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
557 |
|
|
.config_props = config_output, |
558 |
|
|
}, |
559 |
|
|
}; |
560 |
|
|
|
561 |
|
|
const AVFilter ff_avf_showfreqs = { |
562 |
|
|
.name = "showfreqs", |
563 |
|
|
.description = NULL_IF_CONFIG_SMALL("Convert input audio to a frequencies video output."), |
564 |
|
|
.uninit = uninit, |
565 |
|
|
.priv_size = sizeof(ShowFreqsContext), |
566 |
|
|
.activate = activate, |
567 |
|
|
FILTER_INPUTS(ff_audio_default_filterpad), |
568 |
|
|
FILTER_OUTPUTS(showfreqs_outputs), |
569 |
|
|
FILTER_QUERY_FUNC(query_formats), |
570 |
|
|
.priv_class = &showfreqs_class, |
571 |
|
|
}; |
572 |
|
|
|