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(const AVFilterContext *ctx, |
120 |
|
|
AVFilterFormatsConfig **cfg_in, |
121 |
|
|
AVFilterFormatsConfig **cfg_out) |
122 |
|
|
{ |
123 |
|
✗ |
AVFilterFormats *formats = NULL; |
124 |
|
|
static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE }; |
125 |
|
|
static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE }; |
126 |
|
|
int ret; |
127 |
|
|
|
128 |
|
|
/* set input audio formats */ |
129 |
|
✗ |
formats = ff_make_format_list(sample_fmts); |
130 |
|
✗ |
if ((ret = ff_formats_ref(formats, &cfg_in[0]->formats)) < 0) |
131 |
|
✗ |
return ret; |
132 |
|
|
|
133 |
|
|
/* set output video format */ |
134 |
|
✗ |
formats = ff_make_format_list(pix_fmts); |
135 |
|
✗ |
if ((ret = ff_formats_ref(formats, &cfg_out[0]->formats)) < 0) |
136 |
|
✗ |
return ret; |
137 |
|
|
|
138 |
|
✗ |
return 0; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
✗ |
static int config_output(AVFilterLink *outlink) |
142 |
|
|
{ |
143 |
|
✗ |
FilterLink *l = ff_filter_link(outlink); |
144 |
|
✗ |
AVFilterContext *ctx = outlink->src; |
145 |
|
✗ |
AVFilterLink *inlink = ctx->inputs[0]; |
146 |
|
✗ |
ShowFreqsContext *s = ctx->priv; |
147 |
|
✗ |
float overlap, scale = 1.f; |
148 |
|
|
int i, ret; |
149 |
|
|
|
150 |
|
✗ |
s->old_pts = AV_NOPTS_VALUE; |
151 |
|
✗ |
s->nb_freq = s->fft_size / 2; |
152 |
|
✗ |
s->win_size = s->fft_size; |
153 |
|
✗ |
av_tx_uninit(&s->fft); |
154 |
|
✗ |
ret = av_tx_init(&s->fft, &s->tx_fn, AV_TX_FLOAT_FFT, 0, s->fft_size, &scale, 0); |
155 |
|
✗ |
if (ret < 0) { |
156 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. " |
157 |
|
|
"The window size might be too high.\n"); |
158 |
|
✗ |
return ret; |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
/* FFT buffers: x2 for each (display) channel buffer. |
162 |
|
|
* Note: we use free and malloc instead of a realloc-like function to |
163 |
|
|
* make sure the buffer is aligned in memory for the FFT functions. */ |
164 |
|
✗ |
for (i = 0; i < s->nb_channels; i++) { |
165 |
|
✗ |
av_freep(&s->fft_input[i]); |
166 |
|
✗ |
av_freep(&s->fft_data[i]); |
167 |
|
✗ |
av_freep(&s->avg_data[i]); |
168 |
|
|
} |
169 |
|
✗ |
av_freep(&s->bypass); |
170 |
|
✗ |
av_freep(&s->fft_input); |
171 |
|
✗ |
av_freep(&s->fft_data); |
172 |
|
✗ |
av_freep(&s->avg_data); |
173 |
|
✗ |
s->nb_channels = inlink->ch_layout.nb_channels; |
174 |
|
|
|
175 |
|
✗ |
s->bypass = av_calloc(s->nb_channels, sizeof(*s->bypass)); |
176 |
|
✗ |
if (!s->bypass) |
177 |
|
✗ |
return AVERROR(ENOMEM); |
178 |
|
✗ |
s->fft_input = av_calloc(s->nb_channels, sizeof(*s->fft_input)); |
179 |
|
✗ |
if (!s->fft_input) |
180 |
|
✗ |
return AVERROR(ENOMEM); |
181 |
|
✗ |
s->fft_data = av_calloc(s->nb_channels, sizeof(*s->fft_data)); |
182 |
|
✗ |
if (!s->fft_data) |
183 |
|
✗ |
return AVERROR(ENOMEM); |
184 |
|
✗ |
s->avg_data = av_calloc(s->nb_channels, sizeof(*s->avg_data)); |
185 |
|
✗ |
if (!s->avg_data) |
186 |
|
✗ |
return AVERROR(ENOMEM); |
187 |
|
✗ |
for (i = 0; i < s->nb_channels; i++) { |
188 |
|
✗ |
s->fft_input[i] = av_calloc(FFALIGN(s->win_size, 512), sizeof(**s->fft_input)); |
189 |
|
✗ |
s->fft_data[i] = av_calloc(FFALIGN(s->win_size, 512), sizeof(**s->fft_data)); |
190 |
|
✗ |
s->avg_data[i] = av_calloc(s->nb_freq, sizeof(**s->avg_data)); |
191 |
|
✗ |
if (!s->fft_data[i] || !s->avg_data[i] || !s->fft_input[i]) |
192 |
|
✗ |
return AVERROR(ENOMEM); |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
/* pre-calc windowing function */ |
196 |
|
✗ |
s->window_func_lut = av_realloc_f(s->window_func_lut, s->win_size, |
197 |
|
|
sizeof(*s->window_func_lut)); |
198 |
|
✗ |
if (!s->window_func_lut) |
199 |
|
✗ |
return AVERROR(ENOMEM); |
200 |
|
✗ |
generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap); |
201 |
|
✗ |
if (s->overlap == 1.) |
202 |
|
✗ |
s->overlap = overlap; |
203 |
|
✗ |
s->hop_size = (1. - s->overlap) * s->win_size; |
204 |
|
✗ |
if (s->hop_size < 1) { |
205 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap); |
206 |
|
✗ |
return AVERROR(EINVAL); |
207 |
|
|
} |
208 |
|
|
|
209 |
|
✗ |
for (s->scale = 0, i = 0; i < s->win_size; i++) { |
210 |
|
✗ |
s->scale += s->window_func_lut[i] * s->window_func_lut[i]; |
211 |
|
|
} |
212 |
|
|
|
213 |
|
✗ |
s->window = ff_get_audio_buffer(inlink, s->win_size * 2); |
214 |
|
✗ |
if (!s->window) |
215 |
|
✗ |
return AVERROR(ENOMEM); |
216 |
|
|
|
217 |
|
✗ |
l->frame_rate = s->frame_rate; |
218 |
|
✗ |
outlink->time_base = av_inv_q(l->frame_rate); |
219 |
|
✗ |
outlink->sample_aspect_ratio = (AVRational){1,1}; |
220 |
|
✗ |
outlink->w = s->w; |
221 |
|
✗ |
outlink->h = s->h; |
222 |
|
|
|
223 |
|
✗ |
ret = av_channel_layout_copy(&s->ch_layout, &inlink->ch_layout); |
224 |
|
✗ |
if (ret < 0) |
225 |
|
✗ |
return ret; |
226 |
|
✗ |
s->nb_draw_channels = s->nb_channels; |
227 |
|
|
|
228 |
|
✗ |
if (strcmp(s->ch_layout_str, "all")) { |
229 |
|
✗ |
int nb_draw_channels = 0; |
230 |
|
✗ |
av_channel_layout_from_string(&s->ch_layout, |
231 |
|
✗ |
s->ch_layout_str); |
232 |
|
|
|
233 |
|
✗ |
for (int ch = 0; ch < s->nb_channels; ch++) { |
234 |
|
✗ |
const enum AVChannel channel = av_channel_layout_channel_from_index(&inlink->ch_layout, ch); |
235 |
|
|
|
236 |
|
✗ |
s->bypass[ch] = av_channel_layout_index_from_channel(&s->ch_layout, channel) < 0; |
237 |
|
✗ |
nb_draw_channels += s->bypass[ch] == 0; |
238 |
|
|
} |
239 |
|
|
|
240 |
|
✗ |
s->nb_draw_channels = nb_draw_channels; |
241 |
|
|
} |
242 |
|
|
|
243 |
|
✗ |
return 0; |
244 |
|
|
} |
245 |
|
|
|
246 |
|
✗ |
static inline void draw_dot(AVFrame *out, int x, int y, uint8_t fg[4]) |
247 |
|
|
{ |
248 |
|
|
|
249 |
|
✗ |
uint32_t color = AV_RL32(out->data[0] + y * out->linesize[0] + x * 4); |
250 |
|
|
|
251 |
|
✗ |
if ((color & 0xffffff) != 0) |
252 |
|
✗ |
AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg) | color); |
253 |
|
|
else |
254 |
|
✗ |
AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg)); |
255 |
|
✗ |
} |
256 |
|
|
|
257 |
|
✗ |
static int get_sx(ShowFreqsContext *s, int f) |
258 |
|
|
{ |
259 |
|
✗ |
switch (s->fscale) { |
260 |
|
✗ |
case FS_LINEAR: |
261 |
|
✗ |
return (s->w/(float)s->nb_freq)*f; |
262 |
|
✗ |
case FS_LOG: |
263 |
|
✗ |
return s->w-pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.)); |
264 |
|
✗ |
case FS_RLOG: |
265 |
|
✗ |
return pow(s->w, f/(s->nb_freq-1.)); |
266 |
|
|
} |
267 |
|
|
|
268 |
|
✗ |
return 0; |
269 |
|
|
} |
270 |
|
|
|
271 |
|
✗ |
static float get_bsize(ShowFreqsContext *s, int f) |
272 |
|
|
{ |
273 |
|
✗ |
switch (s->fscale) { |
274 |
|
✗ |
case FS_LINEAR: |
275 |
|
✗ |
return s->w/(float)s->nb_freq; |
276 |
|
✗ |
case FS_LOG: |
277 |
|
✗ |
return pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.))- |
278 |
|
✗ |
pow(s->w, (s->nb_freq-f-2)/(s->nb_freq-1.)); |
279 |
|
✗ |
case FS_RLOG: |
280 |
|
✗ |
return pow(s->w, (f+1)/(s->nb_freq-1.))- |
281 |
|
✗ |
pow(s->w, f /(s->nb_freq-1.)); |
282 |
|
|
} |
283 |
|
|
|
284 |
|
✗ |
return 1.; |
285 |
|
|
} |
286 |
|
|
|
287 |
|
✗ |
static inline void plot_freq(ShowFreqsContext *s, int ch, |
288 |
|
|
double a, int f, uint8_t fg[4], int *prev_y, |
289 |
|
|
AVFrame *out, AVFilterLink *outlink) |
290 |
|
|
{ |
291 |
|
✗ |
FilterLink *outl = ff_filter_link(outlink); |
292 |
|
✗ |
const int w = s->w; |
293 |
|
✗ |
const float min = s->minamp; |
294 |
|
✗ |
const float avg = s->avg_data[ch][f]; |
295 |
|
✗ |
const float bsize = get_bsize(s, f); |
296 |
|
✗ |
const int sx = get_sx(s, f); |
297 |
|
✗ |
int end = outlink->h; |
298 |
|
|
int x, y, i; |
299 |
|
|
|
300 |
|
✗ |
switch(s->ascale) { |
301 |
|
✗ |
case AS_SQRT: |
302 |
|
✗ |
a = 1.0 - sqrt(a); |
303 |
|
✗ |
break; |
304 |
|
✗ |
case AS_CBRT: |
305 |
|
✗ |
a = 1.0 - cbrt(a); |
306 |
|
✗ |
break; |
307 |
|
✗ |
case AS_LOG: |
308 |
|
✗ |
a = log(av_clipd(a, min, 1)) / log(min); |
309 |
|
✗ |
break; |
310 |
|
✗ |
case AS_LINEAR: |
311 |
|
✗ |
a = 1.0 - a; |
312 |
|
✗ |
break; |
313 |
|
|
} |
314 |
|
|
|
315 |
|
✗ |
switch (s->cmode) { |
316 |
|
✗ |
case COMBINED: |
317 |
|
✗ |
y = a * outlink->h - 1; |
318 |
|
✗ |
break; |
319 |
|
✗ |
case SEPARATE: |
320 |
|
✗ |
end = (outlink->h / s->nb_draw_channels) * (ch + 1); |
321 |
|
✗ |
y = (outlink->h / s->nb_draw_channels) * ch + a * (outlink->h / s->nb_draw_channels) - 1; |
322 |
|
✗ |
break; |
323 |
|
✗ |
default: |
324 |
|
✗ |
av_assert0(0); |
325 |
|
|
} |
326 |
|
✗ |
if (y < 0) |
327 |
|
✗ |
return; |
328 |
|
|
|
329 |
|
✗ |
switch (s->avg) { |
330 |
|
✗ |
case 0: |
331 |
|
✗ |
y = s->avg_data[ch][f] = !outl->frame_count_in ? y : FFMIN(0, y); |
332 |
|
✗ |
break; |
333 |
|
✗ |
case 1: |
334 |
|
✗ |
break; |
335 |
|
✗ |
default: |
336 |
|
✗ |
s->avg_data[ch][f] = avg + y * (y - avg) / (FFMIN(outl->frame_count_in + 1, s->avg) * (float)y); |
337 |
|
✗ |
y = av_clip(s->avg_data[ch][f], 0, outlink->h - 1); |
338 |
|
✗ |
break; |
339 |
|
|
} |
340 |
|
|
|
341 |
|
✗ |
switch(s->mode) { |
342 |
|
✗ |
case LINE: |
343 |
|
✗ |
if (*prev_y == -1) { |
344 |
|
✗ |
*prev_y = y; |
345 |
|
|
} |
346 |
|
✗ |
if (y <= *prev_y) { |
347 |
|
✗ |
for (x = sx + 1; x < sx + bsize && x < w; x++) |
348 |
|
✗ |
draw_dot(out, x, y, fg); |
349 |
|
✗ |
for (i = y; i <= *prev_y; i++) |
350 |
|
✗ |
draw_dot(out, sx, i, fg); |
351 |
|
|
} else { |
352 |
|
✗ |
for (i = *prev_y; i <= y; i++) |
353 |
|
✗ |
draw_dot(out, sx, i, fg); |
354 |
|
✗ |
for (x = sx + 1; x < sx + bsize && x < w; x++) |
355 |
|
✗ |
draw_dot(out, x, i - 1, fg); |
356 |
|
|
} |
357 |
|
✗ |
*prev_y = y; |
358 |
|
✗ |
break; |
359 |
|
✗ |
case BAR: |
360 |
|
✗ |
for (x = sx; x < sx + bsize && x < w; x++) |
361 |
|
✗ |
for (i = y; i < end; i++) |
362 |
|
✗ |
draw_dot(out, x, i, fg); |
363 |
|
✗ |
break; |
364 |
|
✗ |
case DOT: |
365 |
|
✗ |
for (x = sx; x < sx + bsize && x < w; x++) |
366 |
|
✗ |
draw_dot(out, x, y, fg); |
367 |
|
✗ |
break; |
368 |
|
|
} |
369 |
|
|
} |
370 |
|
|
|
371 |
|
✗ |
static int plot_freqs(AVFilterLink *inlink, int64_t pts) |
372 |
|
|
{ |
373 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
374 |
|
✗ |
AVFilterLink *outlink = ctx->outputs[0]; |
375 |
|
✗ |
ShowFreqsContext *s = ctx->priv; |
376 |
|
✗ |
AVFrame *in = s->window; |
377 |
|
✗ |
const int win_size = s->win_size; |
378 |
|
✗ |
char *colors, *color, *saveptr = NULL; |
379 |
|
|
AVFrame *out; |
380 |
|
|
int ch, n; |
381 |
|
|
|
382 |
|
|
/* fill FFT input with the number of samples available */ |
383 |
|
✗ |
for (ch = 0; ch < s->nb_channels; ch++) { |
384 |
|
✗ |
const float *p = (float *)in->extended_data[ch]; |
385 |
|
|
|
386 |
|
✗ |
if (s->bypass[ch]) |
387 |
|
✗ |
continue; |
388 |
|
|
|
389 |
|
✗ |
for (n = 0; n < win_size; n++) { |
390 |
|
✗ |
s->fft_input[ch][n].re = p[n] * s->window_func_lut[n]; |
391 |
|
✗ |
s->fft_input[ch][n].im = 0; |
392 |
|
|
} |
393 |
|
|
} |
394 |
|
|
|
395 |
|
|
/* run FFT on each samples set */ |
396 |
|
✗ |
for (ch = 0; ch < s->nb_channels; ch++) { |
397 |
|
✗ |
if (s->bypass[ch]) |
398 |
|
✗ |
continue; |
399 |
|
|
|
400 |
|
✗ |
s->tx_fn(s->fft, s->fft_data[ch], s->fft_input[ch], sizeof(AVComplexFloat)); |
401 |
|
|
} |
402 |
|
|
|
403 |
|
✗ |
s->pts = av_rescale_q(pts, inlink->time_base, outlink->time_base); |
404 |
|
✗ |
if (s->old_pts >= s->pts) |
405 |
|
✗ |
return 0; |
406 |
|
✗ |
s->old_pts = s->pts; |
407 |
|
|
|
408 |
|
|
#define RE(x, ch) s->fft_data[ch][x].re |
409 |
|
|
#define IM(x, ch) s->fft_data[ch][x].im |
410 |
|
|
#define M(a, b) (sqrt((a) * (a) + (b) * (b))) |
411 |
|
|
#define P(a, b) (atan2((b), (a))) |
412 |
|
|
|
413 |
|
✗ |
colors = av_strdup(s->colors); |
414 |
|
✗ |
if (!colors) |
415 |
|
✗ |
return AVERROR(ENOMEM); |
416 |
|
|
|
417 |
|
✗ |
out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
418 |
|
✗ |
if (!out) { |
419 |
|
✗ |
av_free(colors); |
420 |
|
✗ |
return AVERROR(ENOMEM); |
421 |
|
|
} |
422 |
|
|
|
423 |
|
✗ |
for (n = 0; n < outlink->h; n++) |
424 |
|
✗ |
memset(out->data[0] + out->linesize[0] * n, 0, outlink->w * 4); |
425 |
|
|
|
426 |
|
✗ |
for (ch = 0; ch < s->nb_channels; ch++) { |
427 |
|
✗ |
uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff }; |
428 |
|
✗ |
int prev_y = -1, f; |
429 |
|
|
double a; |
430 |
|
|
|
431 |
|
✗ |
color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr); |
432 |
|
✗ |
if (color) |
433 |
|
✗ |
av_parse_color(fg, color, -1, ctx); |
434 |
|
|
|
435 |
|
✗ |
if (s->bypass[ch]) |
436 |
|
✗ |
continue; |
437 |
|
|
|
438 |
|
✗ |
switch (s->data_mode) { |
439 |
|
✗ |
case MAGNITUDE: |
440 |
|
✗ |
for (f = 0; f < s->nb_freq; f++) { |
441 |
|
✗ |
a = av_clipd(M(RE(f, ch), IM(f, ch)) / s->scale, 0, 1); |
442 |
|
|
|
443 |
|
✗ |
plot_freq(s, ch, a, f, fg, &prev_y, out, outlink); |
444 |
|
|
} |
445 |
|
✗ |
break; |
446 |
|
✗ |
case PHASE: |
447 |
|
✗ |
for (f = 0; f < s->nb_freq; f++) { |
448 |
|
✗ |
a = av_clipd((M_PI + P(RE(f, ch), IM(f, ch))) / (2. * M_PI), 0, 1); |
449 |
|
|
|
450 |
|
✗ |
plot_freq(s, ch, a, f, fg, &prev_y, out, outlink); |
451 |
|
|
} |
452 |
|
✗ |
break; |
453 |
|
✗ |
case DELAY: |
454 |
|
✗ |
for (f = 0; f < s->nb_freq; f++) { |
455 |
|
✗ |
a = av_clipd((M_PI - P(IM(f, ch) * RE(f-1, ch) - IM(f-1, ch) * RE(f, ch), |
456 |
|
|
RE(f, ch) * RE(f-1, ch) + IM(f, ch) * IM(f-1, ch))) / (2. * M_PI), 0, 1); |
457 |
|
|
|
458 |
|
✗ |
plot_freq(s, ch, a, f, fg, &prev_y, out, outlink); |
459 |
|
|
} |
460 |
|
✗ |
break; |
461 |
|
|
} |
462 |
|
|
} |
463 |
|
|
|
464 |
|
✗ |
av_free(colors); |
465 |
|
✗ |
out->pts = s->pts; |
466 |
|
✗ |
out->duration = 1; |
467 |
|
✗ |
out->sample_aspect_ratio = (AVRational){1,1}; |
468 |
|
✗ |
return ff_filter_frame(outlink, out); |
469 |
|
|
} |
470 |
|
|
|
471 |
|
✗ |
static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
472 |
|
|
{ |
473 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
474 |
|
✗ |
ShowFreqsContext *s = ctx->priv; |
475 |
|
✗ |
const int offset = s->win_size - s->hop_size; |
476 |
|
✗ |
int64_t pts = in->pts; |
477 |
|
|
|
478 |
|
✗ |
for (int ch = 0; ch < in->ch_layout.nb_channels; ch++) { |
479 |
|
✗ |
float *dst = (float *)s->window->extended_data[ch]; |
480 |
|
|
|
481 |
|
✗ |
memmove(dst, &dst[s->hop_size], offset * sizeof(float)); |
482 |
|
✗ |
memcpy(&dst[offset], in->extended_data[ch], in->nb_samples * sizeof(float)); |
483 |
|
✗ |
memset(&dst[offset + in->nb_samples], 0, (s->hop_size - in->nb_samples) * sizeof(float)); |
484 |
|
|
} |
485 |
|
|
|
486 |
|
✗ |
av_frame_free(&in); |
487 |
|
|
|
488 |
|
✗ |
return plot_freqs(inlink, pts); |
489 |
|
|
} |
490 |
|
|
|
491 |
|
✗ |
static int activate(AVFilterContext *ctx) |
492 |
|
|
{ |
493 |
|
✗ |
AVFilterLink *inlink = ctx->inputs[0]; |
494 |
|
✗ |
AVFilterLink *outlink = ctx->outputs[0]; |
495 |
|
✗ |
ShowFreqsContext *s = ctx->priv; |
496 |
|
|
AVFrame *in; |
497 |
|
|
int ret; |
498 |
|
|
|
499 |
|
✗ |
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); |
500 |
|
|
|
501 |
|
✗ |
ret = ff_inlink_consume_samples(inlink, s->hop_size, s->hop_size, &in); |
502 |
|
✗ |
if (ret < 0) |
503 |
|
✗ |
return ret; |
504 |
|
|
|
505 |
|
✗ |
if (ret > 0) |
506 |
|
✗ |
ret = filter_frame(inlink, in); |
507 |
|
✗ |
if (ret < 0) |
508 |
|
✗ |
return ret; |
509 |
|
|
|
510 |
|
✗ |
if (ff_inlink_queued_samples(inlink) >= s->hop_size) { |
511 |
|
✗ |
ff_filter_set_ready(ctx, 10); |
512 |
|
✗ |
return 0; |
513 |
|
|
} |
514 |
|
|
|
515 |
|
✗ |
FF_FILTER_FORWARD_STATUS(inlink, outlink); |
516 |
|
✗ |
FF_FILTER_FORWARD_WANTED(outlink, inlink); |
517 |
|
|
|
518 |
|
✗ |
return FFERROR_NOT_READY; |
519 |
|
|
} |
520 |
|
|
|
521 |
|
✗ |
static av_cold void uninit(AVFilterContext *ctx) |
522 |
|
|
{ |
523 |
|
✗ |
ShowFreqsContext *s = ctx->priv; |
524 |
|
|
int i; |
525 |
|
|
|
526 |
|
✗ |
av_channel_layout_uninit(&s->ch_layout); |
527 |
|
✗ |
av_tx_uninit(&s->fft); |
528 |
|
✗ |
for (i = 0; i < s->nb_channels; i++) { |
529 |
|
✗ |
if (s->fft_input) |
530 |
|
✗ |
av_freep(&s->fft_input[i]); |
531 |
|
✗ |
if (s->fft_data) |
532 |
|
✗ |
av_freep(&s->fft_data[i]); |
533 |
|
✗ |
if (s->avg_data) |
534 |
|
✗ |
av_freep(&s->avg_data[i]); |
535 |
|
|
} |
536 |
|
✗ |
av_freep(&s->bypass); |
537 |
|
✗ |
av_freep(&s->fft_input); |
538 |
|
✗ |
av_freep(&s->fft_data); |
539 |
|
✗ |
av_freep(&s->avg_data); |
540 |
|
✗ |
av_freep(&s->window_func_lut); |
541 |
|
✗ |
av_frame_free(&s->window); |
542 |
|
✗ |
} |
543 |
|
|
|
544 |
|
|
static const AVFilterPad showfreqs_outputs[] = { |
545 |
|
|
{ |
546 |
|
|
.name = "default", |
547 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
548 |
|
|
.config_props = config_output, |
549 |
|
|
}, |
550 |
|
|
}; |
551 |
|
|
|
552 |
|
|
const AVFilter ff_avf_showfreqs = { |
553 |
|
|
.name = "showfreqs", |
554 |
|
|
.description = NULL_IF_CONFIG_SMALL("Convert input audio to a frequencies video output."), |
555 |
|
|
.uninit = uninit, |
556 |
|
|
.priv_size = sizeof(ShowFreqsContext), |
557 |
|
|
.activate = activate, |
558 |
|
|
FILTER_INPUTS(ff_audio_default_filterpad), |
559 |
|
|
FILTER_OUTPUTS(showfreqs_outputs), |
560 |
|
|
FILTER_QUERY_FUNC2(query_formats), |
561 |
|
|
.priv_class = &showfreqs_class, |
562 |
|
|
}; |
563 |
|
|
|