]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_afftfilt.c
Merge commit '03ef89faf23c4851848208c9fe004cd9ef690cec'
[ffmpeg] / libavfilter / af_afftfilt.c
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/audio_fifo.h"
22 #include "libavutil/avstring.h"
23 #include "libavfilter/internal.h"
24 #include "libavutil/common.h"
25 #include "libavutil/opt.h"
26 #include "libavcodec/avfft.h"
27 #include "libavutil/eval.h"
28 #include "audio.h"
29 #include "window_func.h"
30
31 typedef struct AFFTFiltContext {
32     const AVClass *class;
33     char *real_str;
34     char *img_str;
35     int fft_bits;
36
37     FFTContext *fft, *ifft;
38     FFTComplex **fft_data;
39     int nb_exprs;
40     int window_size;
41     AVExpr **real;
42     AVExpr **imag;
43     AVAudioFifo *fifo;
44     int64_t pts;
45     int hop_size;
46     float overlap;
47     AVFrame *buffer;
48     int start, end;
49     int win_func;
50     float win_scale;
51     float *window_func_lut;
52 } AFFTFiltContext;
53
54 static const char *const var_names[] = {            "sr",     "b",       "nb",        "ch",        "chs",   "pts",        NULL };
55 enum                                   { VAR_SAMPLE_RATE, VAR_BIN, VAR_NBBINS, VAR_CHANNEL, VAR_CHANNELS, VAR_PTS, VAR_VARS_NB };
56
57 #define OFFSET(x) offsetof(AFFTFiltContext, x)
58 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
59
60 static const AVOption afftfilt_options[] = {
61     { "real", "set channels real expressions",       OFFSET(real_str), AV_OPT_TYPE_STRING, {.str = "1" }, 0, 0, A },
62     { "imag",  "set channels imaginary expressions", OFFSET(img_str),  AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, A },
63     { "win_size", "set window size", OFFSET(fft_bits), AV_OPT_TYPE_INT, {.i64=12}, 4, 16, A, "fft" },
64         { "w16",    0, 0, AV_OPT_TYPE_CONST, {.i64=4},  0, 0, A, "fft" },
65         { "w32",    0, 0, AV_OPT_TYPE_CONST, {.i64=5},  0, 0, A, "fft" },
66         { "w64",    0, 0, AV_OPT_TYPE_CONST, {.i64=6},  0, 0, A, "fft" },
67         { "w128",   0, 0, AV_OPT_TYPE_CONST, {.i64=7},  0, 0, A, "fft" },
68         { "w256",   0, 0, AV_OPT_TYPE_CONST, {.i64=8},  0, 0, A, "fft" },
69         { "w512",   0, 0, AV_OPT_TYPE_CONST, {.i64=9},  0, 0, A, "fft" },
70         { "w1024",  0, 0, AV_OPT_TYPE_CONST, {.i64=10}, 0, 0, A, "fft" },
71         { "w2048",  0, 0, AV_OPT_TYPE_CONST, {.i64=11}, 0, 0, A, "fft" },
72         { "w4096",  0, 0, AV_OPT_TYPE_CONST, {.i64=12}, 0, 0, A, "fft" },
73         { "w8192",  0, 0, AV_OPT_TYPE_CONST, {.i64=13}, 0, 0, A, "fft" },
74         { "w16384", 0, 0, AV_OPT_TYPE_CONST, {.i64=14}, 0, 0, A, "fft" },
75         { "w32768", 0, 0, AV_OPT_TYPE_CONST, {.i64=15}, 0, 0, A, "fft" },
76         { "w65536", 0, 0, AV_OPT_TYPE_CONST, {.i64=16}, 0, 0, A, "fft" },
77     { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, A, "win_func" },
78         { "rect",     "Rectangular",      0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT},     0, 0, A, "win_func" },
79         { "bartlett", "Bartlett",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, A, "win_func" },
80         { "hann",     "Hann",             0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING},  0, 0, A, "win_func" },
81         { "hanning",  "Hanning",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING},  0, 0, A, "win_func" },
82         { "hamming",  "Hamming",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING},  0, 0, A, "win_func" },
83         { "sine",     "Sine",             0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE},     0, 0, A, "win_func" },
84     { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0,  1, A },
85     { NULL },
86 };
87
88 AVFILTER_DEFINE_CLASS(afftfilt);
89
90 static int config_input(AVFilterLink *inlink)
91 {
92     AVFilterContext *ctx = inlink->dst;
93     AFFTFiltContext *s = ctx->priv;
94     char *saveptr = NULL;
95     int ret = 0, ch, i;
96     float overlap;
97     char *args, *last_expr = NULL;
98
99     s->fft  = av_fft_init(s->fft_bits, 0);
100     s->ifft = av_fft_init(s->fft_bits, 1);
101     if (!s->fft || !s->ifft)
102         return AVERROR(ENOMEM);
103
104     s->window_size = 1 << s->fft_bits;
105
106     s->fft_data = av_calloc(inlink->channels, sizeof(*s->fft_data));
107     if (!s->fft_data)
108         return AVERROR(ENOMEM);
109
110     for (ch = 0; ch < inlink->channels; ch++) {
111         s->fft_data[ch] = av_calloc(s->window_size, sizeof(**s->fft_data));
112         if (!s->fft_data[ch])
113             return AVERROR(ENOMEM);
114     }
115
116     s->real = av_calloc(inlink->channels, sizeof(*s->real));
117     if (!s->real)
118         return AVERROR(ENOMEM);
119
120     s->imag = av_calloc(inlink->channels, sizeof(*s->imag));
121     if (!s->imag)
122         return AVERROR(ENOMEM);
123
124     args = av_strdup(s->real_str);
125     if (!args)
126         return AVERROR(ENOMEM);
127
128     for (ch = 0; ch < inlink->channels; ch++) {
129         char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr);
130
131         ret = av_expr_parse(&s->real[ch], arg ? arg : last_expr, var_names,
132                             NULL, NULL, NULL, NULL, 0, ctx);
133         if (ret < 0)
134             break;
135         if (arg)
136             last_expr = arg;
137         s->nb_exprs++;
138     }
139
140     av_free(args);
141
142     args = av_strdup(s->img_str ? s->img_str : s->real_str);
143     if (!args)
144         return AVERROR(ENOMEM);
145
146     for (ch = 0; ch < inlink->channels; ch++) {
147         char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr);
148
149         ret = av_expr_parse(&s->imag[ch], arg ? arg : last_expr, var_names,
150                             NULL, NULL, NULL, NULL, 0, ctx);
151         if (ret < 0)
152             break;
153         if (arg)
154             last_expr = arg;
155     }
156
157     av_free(args);
158
159     s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->window_size);
160     if (!s->fifo)
161         return AVERROR(ENOMEM);
162
163     s->window_func_lut = av_realloc_f(s->window_func_lut, s->window_size,
164                                       sizeof(*s->window_func_lut));
165     if (!s->window_func_lut)
166         return AVERROR(ENOMEM);
167     ff_generate_window_func(s->window_func_lut, s->window_size, s->win_func, &overlap);
168     if (s->overlap == 1)
169         s->overlap = overlap;
170
171     for (s->win_scale = 0, i = 0; i < s->window_size; i++) {
172         s->win_scale += s->window_func_lut[i] * s->window_func_lut[i];
173     }
174
175     s->hop_size = s->window_size * (1 - s->overlap);
176     if (s->hop_size <= 0)
177         return AVERROR(EINVAL);
178
179     s->buffer = ff_get_audio_buffer(inlink, s->window_size * 2);
180     if (!s->buffer)
181         return AVERROR(ENOMEM);
182
183     return ret;
184 }
185
186 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
187 {
188     AVFilterContext *ctx = inlink->dst;
189     AVFilterLink *outlink = ctx->outputs[0];
190     AFFTFiltContext *s = ctx->priv;
191     const int window_size = s->window_size;
192     const float f = 1. / s->win_scale;
193     double values[VAR_VARS_NB];
194     AVFrame *out, *in = NULL;
195     int ch, n, ret, i, j, k;
196     int start = s->start, end = s->end;
197
198     av_audio_fifo_write(s->fifo, (void **)frame->extended_data, frame->nb_samples);
199     av_frame_free(&frame);
200
201     while (av_audio_fifo_size(s->fifo) >= window_size) {
202         if (!in) {
203             in = ff_get_audio_buffer(outlink, window_size);
204             if (!in)
205                 return AVERROR(ENOMEM);
206         }
207
208         ret = av_audio_fifo_peek(s->fifo, (void **)in->extended_data, window_size);
209         if (ret < 0)
210             break;
211
212         for (ch = 0; ch < inlink->channels; ch++) {
213             const float *src = (float *)in->extended_data[ch];
214             FFTComplex *fft_data = s->fft_data[ch];
215
216             for (n = 0; n < in->nb_samples; n++) {
217                 fft_data[n].re = src[n] * s->window_func_lut[n];
218                 fft_data[n].im = 0;
219             }
220
221             for (; n < window_size; n++) {
222                 fft_data[n].re = 0;
223                 fft_data[n].im = 0;
224             }
225         }
226
227         values[VAR_PTS]         = s->pts;
228         values[VAR_SAMPLE_RATE] = inlink->sample_rate;
229         values[VAR_NBBINS]      = window_size / 2;
230         values[VAR_CHANNELS]    = inlink->channels;
231
232         for (ch = 0; ch < inlink->channels; ch++) {
233             FFTComplex *fft_data = s->fft_data[ch];
234             float *buf = (float *)s->buffer->extended_data[ch];
235             int x;
236
237             values[VAR_CHANNEL] = ch;
238
239             av_fft_permute(s->fft, fft_data);
240             av_fft_calc(s->fft, fft_data);
241
242             for (n = 0; n < window_size / 2; n++) {
243                 float fr, fi;
244
245                 values[VAR_BIN] = n;
246
247                 fr = av_expr_eval(s->real[ch], values, s);
248                 fi = av_expr_eval(s->imag[ch], values, s);
249
250                 fft_data[n].re *= fr;
251                 fft_data[n].im *= fi;
252             }
253
254             for (n = window_size / 2 + 1, x = window_size / 2 - 1; n < window_size; n++, x--) {
255                 fft_data[n].re =  fft_data[x].re;
256                 fft_data[n].im = -fft_data[x].im;
257             }
258
259             av_fft_permute(s->ifft, fft_data);
260             av_fft_calc(s->ifft, fft_data);
261
262             start = s->start;
263             end = s->end;
264             k = end;
265             for (i = 0, j = start; j < k && i < window_size; i++, j++) {
266                 buf[j] += s->fft_data[ch][i].re * f;
267             }
268
269             for (; i < window_size; i++, j++) {
270                 buf[j] = s->fft_data[ch][i].re * f;
271             }
272
273             start += s->hop_size;
274             end = j;
275         }
276
277         s->start = start;
278         s->end = end;
279
280         if (start >= window_size) {
281             float *dst, *buf;
282
283             start -= window_size;
284             end   -= window_size;
285
286             s->start = start;
287             s->end = end;
288
289             out = ff_get_audio_buffer(outlink, window_size);
290             if (!out) {
291                 ret = AVERROR(ENOMEM);
292                 break;
293             }
294
295             out->pts = s->pts;
296             s->pts += window_size;
297
298             for (ch = 0; ch < inlink->channels; ch++) {
299                 dst = (float *)out->extended_data[ch];
300                 buf = (float *)s->buffer->extended_data[ch];
301
302                 for (n = 0; n < window_size; n++) {
303                     dst[n] = buf[n] * (1 - s->overlap);
304                 }
305                 memmove(buf, buf + window_size, window_size * 4);
306             }
307
308             ret = ff_filter_frame(outlink, out);
309             if (ret < 0)
310                 break;
311         }
312
313         av_audio_fifo_drain(s->fifo, s->hop_size);
314     }
315
316     av_frame_free(&in);
317     return ret;
318 }
319
320 static int query_formats(AVFilterContext *ctx)
321 {
322     AVFilterFormats *formats;
323     AVFilterChannelLayouts *layouts;
324     static const enum AVSampleFormat sample_fmts[] = {
325         AV_SAMPLE_FMT_FLTP,
326         AV_SAMPLE_FMT_NONE
327     };
328     int ret;
329
330     layouts = ff_all_channel_counts();
331     if (!layouts)
332         return AVERROR(ENOMEM);
333     ret = ff_set_common_channel_layouts(ctx, layouts);
334     if (ret < 0)
335         return ret;
336
337     formats = ff_make_format_list(sample_fmts);
338     if (!formats)
339         return AVERROR(ENOMEM);
340     ret = ff_set_common_formats(ctx, formats);
341     if (ret < 0)
342         return ret;
343
344     formats = ff_all_samplerates();
345     if (!formats)
346         return AVERROR(ENOMEM);
347     return ff_set_common_samplerates(ctx, formats);
348 }
349
350 static av_cold void uninit(AVFilterContext *ctx)
351 {
352     AFFTFiltContext *s = ctx->priv;
353     int i;
354
355     av_fft_end(s->fft);
356     av_fft_end(s->ifft);
357
358     for (i = 0; i < s->nb_exprs; i++) {
359         if (s->fft_data)
360             av_freep(&s->fft_data[i]);
361     }
362     av_freep(&s->fft_data);
363
364     for (i = 0; i < s->nb_exprs; i++) {
365         av_expr_free(s->real[i]);
366         av_expr_free(s->imag[i]);
367     }
368
369     av_freep(&s->real);
370     av_freep(&s->imag);
371     av_frame_free(&s->buffer);
372 }
373
374 static const AVFilterPad inputs[] = {
375     {
376         .name         = "default",
377         .type         = AVMEDIA_TYPE_AUDIO,
378         .config_props = config_input,
379         .filter_frame = filter_frame,
380     },
381     { NULL }
382 };
383
384 static const AVFilterPad outputs[] = {
385     {
386         .name = "default",
387         .type = AVMEDIA_TYPE_AUDIO,
388     },
389     { NULL }
390 };
391
392 AVFilter ff_af_afftfilt = {
393     .name            = "afftfilt",
394     .description     = NULL_IF_CONFIG_SMALL("Apply arbitrary expressions to samples in frequency domain."),
395     .priv_size       = sizeof(AFFTFiltContext),
396     .priv_class      = &afftfilt_class,
397     .inputs          = inputs,
398     .outputs         = outputs,
399     .query_formats   = query_formats,
400     .uninit          = uninit,
401 };