FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_afftfilt.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 0 234 0.0%
Functions: 0 10 0.0%
Branches: 0 122 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2016 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation; either version 2.1 of the License,
9 * 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 "libavutil/avstring.h"
22 #include "libavutil/mem.h"
23 #include "libavfilter/internal.h"
24 #include "libavutil/common.h"
25 #include "libavutil/cpu.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/eval.h"
28 #include "libavutil/tx.h"
29 #include "audio.h"
30 #include "filters.h"
31 #include "window_func.h"
32
33 typedef struct AFFTFiltContext {
34 const AVClass *class;
35 char *real_str;
36 char *img_str;
37 int fft_size;
38
39 AVTXContext **fft, **ifft;
40 av_tx_fn tx_fn, itx_fn;
41 AVComplexFloat **fft_in;
42 AVComplexFloat **fft_out;
43 AVComplexFloat **fft_temp;
44 int nb_exprs;
45 int channels;
46 int window_size;
47 AVExpr **real;
48 AVExpr **imag;
49 int hop_size;
50 float overlap;
51 AVFrame *window;
52 AVFrame *buffer;
53 int win_func;
54 float *window_func_lut;
55 } AFFTFiltContext;
56
57 static const char *const var_names[] = { "sr", "b", "nb", "ch", "chs", "pts", "re", "im", NULL };
58 enum { VAR_SAMPLE_RATE, VAR_BIN, VAR_NBBINS, VAR_CHANNEL, VAR_CHANNELS, VAR_PTS, VAR_REAL, VAR_IMAG, VAR_VARS_NB };
59
60 #define OFFSET(x) offsetof(AFFTFiltContext, x)
61 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
62
63 static const AVOption afftfilt_options[] = {
64 { "real", "set channels real expressions", OFFSET(real_str), AV_OPT_TYPE_STRING, {.str = "re" }, 0, 0, A },
65 { "imag", "set channels imaginary expressions", OFFSET(img_str), AV_OPT_TYPE_STRING, {.str = "im" }, 0, 0, A },
66 { "win_size", "set window size", OFFSET(fft_size), AV_OPT_TYPE_INT, {.i64=4096}, 16, 131072, A },
67 WIN_FUNC_OPTION("win_func", OFFSET(win_func), A, WFUNC_HANNING),
68 { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, A },
69 { NULL },
70 };
71
72 AVFILTER_DEFINE_CLASS(afftfilt);
73
74 static inline double getreal(void *priv, double x, double ch)
75 {
76 AFFTFiltContext *s = priv;
77 int ich, ix;
78
79 ich = av_clip(ch, 0, s->nb_exprs - 1);
80 ix = av_clip(x, 0, s->window_size / 2);
81
82 return s->fft_out[ich][ix].re;
83 }
84
85 static inline double getimag(void *priv, double x, double ch)
86 {
87 AFFTFiltContext *s = priv;
88 int ich, ix;
89
90 ich = av_clip(ch, 0, s->nb_exprs - 1);
91 ix = av_clip(x, 0, s->window_size / 2);
92
93 return s->fft_out[ich][ix].im;
94 }
95
96 static double realf(void *priv, double x, double ch) { return getreal(priv, x, ch); }
97 static double imagf(void *priv, double x, double ch) { return getimag(priv, x, ch); }
98
99 static const char *const func2_names[] = { "real", "imag", NULL };
100 static double (*const func2[])(void *, double, double) = { realf, imagf, NULL };
101
102 static int config_input(AVFilterLink *inlink)
103 {
104 AVFilterContext *ctx = inlink->dst;
105 AFFTFiltContext *s = ctx->priv;
106 char *saveptr = NULL;
107 int ret = 0, ch;
108 float overlap, scale = 1.f;
109 char *args;
110 const char *last_expr = "1";
111 int buf_size;
112
113 s->channels = inlink->ch_layout.nb_channels;
114 s->fft = av_calloc(s->channels, sizeof(*s->fft));
115 s->ifft = av_calloc(s->channels, sizeof(*s->ifft));
116 if (!s->fft || !s->ifft)
117 return AVERROR(ENOMEM);
118
119 for (int ch = 0; ch < s->channels; ch++) {
120 ret = av_tx_init(&s->fft[ch], &s->tx_fn, AV_TX_FLOAT_FFT, 0, s->fft_size, &scale, 0);
121 if (ret < 0)
122 return ret;
123 }
124
125 for (int ch = 0; ch < s->channels; ch++) {
126 ret = av_tx_init(&s->ifft[ch], &s->itx_fn, AV_TX_FLOAT_FFT, 1, s->fft_size, &scale, 0);
127 if (ret < 0)
128 return ret;
129 }
130
131 s->window_size = s->fft_size;
132 buf_size = FFALIGN(s->window_size, av_cpu_max_align());
133
134 s->fft_in = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->fft_in));
135 if (!s->fft_in)
136 return AVERROR(ENOMEM);
137
138 s->fft_out = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->fft_out));
139 if (!s->fft_out)
140 return AVERROR(ENOMEM);
141
142 s->fft_temp = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->fft_temp));
143 if (!s->fft_temp)
144 return AVERROR(ENOMEM);
145
146 for (ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
147 s->fft_in[ch] = av_calloc(buf_size, sizeof(**s->fft_in));
148 if (!s->fft_in[ch])
149 return AVERROR(ENOMEM);
150
151 s->fft_out[ch] = av_calloc(buf_size, sizeof(**s->fft_out));
152 if (!s->fft_out[ch])
153 return AVERROR(ENOMEM);
154
155 s->fft_temp[ch] = av_calloc(buf_size, sizeof(**s->fft_temp));
156 if (!s->fft_temp[ch])
157 return AVERROR(ENOMEM);
158 }
159
160 s->real = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->real));
161 if (!s->real)
162 return AVERROR(ENOMEM);
163
164 s->imag = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->imag));
165 if (!s->imag)
166 return AVERROR(ENOMEM);
167
168 args = av_strdup(s->real_str);
169 if (!args)
170 return AVERROR(ENOMEM);
171
172 for (ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
173 char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr);
174
175 ret = av_expr_parse(&s->real[ch], arg ? arg : last_expr, var_names,
176 NULL, NULL, func2_names, func2, 0, ctx);
177 if (ret < 0)
178 goto fail;
179 if (arg)
180 last_expr = arg;
181 s->nb_exprs++;
182 }
183
184 av_freep(&args);
185
186 args = av_strdup(s->img_str ? s->img_str : s->real_str);
187 if (!args)
188 return AVERROR(ENOMEM);
189
190 saveptr = NULL;
191 last_expr = "1";
192 for (ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
193 char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr);
194
195 ret = av_expr_parse(&s->imag[ch], arg ? arg : last_expr, var_names,
196 NULL, NULL, func2_names, func2, 0, ctx);
197 if (ret < 0)
198 goto fail;
199 if (arg)
200 last_expr = arg;
201 }
202
203 av_freep(&args);
204
205 s->window_func_lut = av_realloc_f(s->window_func_lut, s->window_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->window_size, s->win_func, &overlap);
210 for (int i = 0; i < s->window_size; i++)
211 s->window_func_lut[i] = sqrtf(s->window_func_lut[i] / s->window_size);
212 if (s->overlap == 1)
213 s->overlap = overlap;
214
215 s->hop_size = s->window_size * (1 - s->overlap);
216 if (s->hop_size <= 0)
217 return AVERROR(EINVAL);
218
219 s->window = ff_get_audio_buffer(inlink, s->window_size * 2);
220 if (!s->window)
221 return AVERROR(ENOMEM);
222
223 s->buffer = ff_get_audio_buffer(inlink, s->window_size * 2);
224 if (!s->buffer)
225 return AVERROR(ENOMEM);
226
227 fail:
228 av_freep(&args);
229
230 return ret;
231 }
232
233 static int tx_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
234 {
235 AFFTFiltContext *s = ctx->priv;
236 const int channels = s->channels;
237 const int start = (channels * jobnr) / nb_jobs;
238 const int end = (channels * (jobnr+1)) / nb_jobs;
239
240 for (int ch = start; ch < end; ch++) {
241 AVComplexFloat *fft_in = s->fft_in[ch];
242 AVComplexFloat *fft_out = s->fft_out[ch];
243
244 s->tx_fn(s->fft[ch], fft_out, fft_in, sizeof(*fft_in));
245 }
246
247 return 0;
248 }
249
250 static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
251 {
252 AFFTFiltContext *s = ctx->priv;
253 const int window_size = s->window_size;
254 const float *window_lut = s->window_func_lut;
255 const float f = sqrtf(1.f - s->overlap);
256 const int channels = s->channels;
257 const int start = (channels * jobnr) / nb_jobs;
258 const int end = (channels * (jobnr+1)) / nb_jobs;
259 double values[VAR_VARS_NB];
260
261 memcpy(values, arg, sizeof(values));
262
263 for (int ch = start; ch < end; ch++) {
264 AVComplexFloat *fft_out = s->fft_out[ch];
265 AVComplexFloat *fft_temp = s->fft_temp[ch];
266 float *buf = (float *)s->buffer->extended_data[ch];
267
268 values[VAR_CHANNEL] = ch;
269
270 if (ctx->is_disabled) {
271 for (int n = 0; n < window_size; n++) {
272 fft_temp[n].re = fft_out[n].re;
273 fft_temp[n].im = fft_out[n].im;
274 }
275 } else {
276 for (int n = 0; n <= window_size / 2; n++) {
277 float fr, fi;
278
279 values[VAR_BIN] = n;
280 values[VAR_REAL] = fft_out[n].re;
281 values[VAR_IMAG] = fft_out[n].im;
282
283 fr = av_expr_eval(s->real[ch], values, s);
284 fi = av_expr_eval(s->imag[ch], values, s);
285
286 fft_temp[n].re = fr;
287 fft_temp[n].im = fi;
288 }
289
290 for (int n = window_size / 2 + 1, x = window_size / 2 - 1; n < window_size; n++, x--) {
291 fft_temp[n].re = fft_temp[x].re;
292 fft_temp[n].im = -fft_temp[x].im;
293 }
294 }
295
296 s->itx_fn(s->ifft[ch], fft_out, fft_temp, sizeof(*fft_temp));
297
298 memmove(buf, buf + s->hop_size, window_size * sizeof(float));
299 for (int i = 0; i < window_size; i++)
300 buf[i] += fft_out[i].re * window_lut[i] * f;
301 }
302
303 return 0;
304 }
305
306 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
307 {
308 AVFilterContext *ctx = inlink->dst;
309 AVFilterLink *outlink = ctx->outputs[0];
310 AFFTFiltContext *s = ctx->priv;
311 const int window_size = s->window_size;
312 const float *window_lut = s->window_func_lut;
313 double values[VAR_VARS_NB];
314 int ch, n, ret;
315 AVFrame *out;
316
317 for (ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
318 const int offset = s->window_size - s->hop_size;
319 float *src = (float *)s->window->extended_data[ch];
320 AVComplexFloat *fft_in = s->fft_in[ch];
321
322 memmove(src, &src[s->hop_size], offset * sizeof(float));
323 memcpy(&src[offset], in->extended_data[ch], in->nb_samples * sizeof(float));
324 memset(&src[offset + in->nb_samples], 0, (s->hop_size - in->nb_samples) * sizeof(float));
325
326 for (n = 0; n < window_size; n++) {
327 fft_in[n].re = src[n] * window_lut[n];
328 fft_in[n].im = 0;
329 }
330 }
331
332 values[VAR_PTS] = in->pts;
333 values[VAR_SAMPLE_RATE] = inlink->sample_rate;
334 values[VAR_NBBINS] = window_size / 2;
335 values[VAR_CHANNELS] = inlink->ch_layout.nb_channels;
336
337 ff_filter_execute(ctx, tx_channel, NULL, NULL,
338 FFMIN(s->channels, ff_filter_get_nb_threads(ctx)));
339
340 ff_filter_execute(ctx, filter_channel, values, NULL,
341 FFMIN(s->channels, ff_filter_get_nb_threads(ctx)));
342
343 out = ff_get_audio_buffer(outlink, s->hop_size);
344 if (!out) {
345 ret = AVERROR(ENOMEM);
346 goto fail;
347 }
348
349 av_frame_copy_props(out, in);
350 out->nb_samples = in->nb_samples;
351
352 for (ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
353 float *dst = (float *)out->extended_data[ch];
354 float *buf = (float *)s->buffer->extended_data[ch];
355
356 memcpy(dst, buf, s->hop_size * sizeof(float));
357 }
358
359 ret = ff_filter_frame(outlink, out);
360 if (ret < 0)
361 goto fail;
362
363 fail:
364 av_frame_free(&in);
365 return ret < 0 ? ret : 0;
366 }
367
368 static int activate(AVFilterContext *ctx)
369 {
370 AVFilterLink *inlink = ctx->inputs[0];
371 AVFilterLink *outlink = ctx->outputs[0];
372 AFFTFiltContext *s = ctx->priv;
373 AVFrame *in = NULL;
374 int ret = 0, status;
375 int64_t pts;
376
377 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
378
379 ret = ff_inlink_consume_samples(inlink, s->hop_size, s->hop_size, &in);
380 if (ret < 0)
381 return ret;
382
383 if (ret > 0)
384 ret = filter_frame(inlink, in);
385 if (ret < 0)
386 return ret;
387
388 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
389 ff_outlink_set_status(outlink, status, pts);
390 return 0;
391 }
392
393 FF_FILTER_FORWARD_WANTED(outlink, inlink);
394
395 return FFERROR_NOT_READY;
396 }
397
398 static av_cold void uninit(AVFilterContext *ctx)
399 {
400 AFFTFiltContext *s = ctx->priv;
401 int i;
402
403
404 for (i = 0; i < s->channels; i++) {
405 if (s->ifft)
406 av_tx_uninit(&s->ifft[i]);
407 if (s->fft)
408 av_tx_uninit(&s->fft[i]);
409 if (s->fft_in)
410 av_freep(&s->fft_in[i]);
411 if (s->fft_out)
412 av_freep(&s->fft_out[i]);
413 if (s->fft_temp)
414 av_freep(&s->fft_temp[i]);
415 }
416
417 av_freep(&s->fft);
418 av_freep(&s->ifft);
419 av_freep(&s->fft_in);
420 av_freep(&s->fft_out);
421 av_freep(&s->fft_temp);
422
423 for (i = 0; i < s->nb_exprs; i++) {
424 av_expr_free(s->real[i]);
425 av_expr_free(s->imag[i]);
426 }
427
428 av_freep(&s->real);
429 av_freep(&s->imag);
430 av_frame_free(&s->buffer);
431 av_frame_free(&s->window);
432 av_freep(&s->window_func_lut);
433 }
434
435 static const AVFilterPad inputs[] = {
436 {
437 .name = "default",
438 .type = AVMEDIA_TYPE_AUDIO,
439 .config_props = config_input,
440 },
441 };
442
443 const AVFilter ff_af_afftfilt = {
444 .name = "afftfilt",
445 .description = NULL_IF_CONFIG_SMALL("Apply arbitrary expressions to samples in frequency domain."),
446 .priv_size = sizeof(AFFTFiltContext),
447 .priv_class = &afftfilt_class,
448 FILTER_INPUTS(inputs),
449 FILTER_OUTPUTS(ff_audio_default_filterpad),
450 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_FLTP),
451 .activate = activate,
452 .uninit = uninit,
453 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
454 AVFILTER_FLAG_SLICE_THREADS,
455 };
456