History

avfilter/af_afftfilt: switch to activate

Paul B Mahol committed 7 years ago

Commit c539dd91 parent cc869821 file · +126 −102Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/af_afftfilt.c+126−102
@@ -26,6 +26,7 @@ · 25 unmodified lines
2626 #include "libavcodec/avfft.h"
2727 #include "libavutil/eval.h"
2828 #include "audio.h"
29+#include "filters.h"
2930 #include "window_func.h"
3031
3132 typedef struct AFFTFiltContext {
@@ -46,7 +47,7 @@ typedef struct AFFTFiltContext { · 14 unmodified lines
4647 int hop_size;
4748 float overlap;
4849 AVFrame *buffer;
49− int start, end;
50+ int eof;
5051 int win_func;
5152 float win_scale;
5253 float *window_func_lut;
@@ -240,7 +241,7 @@ static int config_input(AVFilterLink *inlink) · 187 unmodified lines
240241 return ret;
241242 }
242243
243−static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
244+static int filter_frame(AVFilterLink *inlink)
244245 {
245246 AVFilterContext *ctx = inlink->dst;
246247 AVFilterLink *outlink = ctx->outputs[0];
@@ -249,140 +250,163 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame) · 2 unmodified lines
249250 const float f = 1. / s->win_scale;
250251 double values[VAR_VARS_NB];
251252 AVFrame *out, *in = NULL;
252− int ch, n, ret, i, j, k;
253− int start = s->start, end = s->end;
253+ int ch, n, ret, i;
254254
255− if (s->pts == AV_NOPTS_VALUE)
256− s->pts = frame->pts;
255+ if (!in) {
256+ in = ff_get_audio_buffer(outlink, window_size);
257+ if (!in)
258+ return AVERROR(ENOMEM);
259+ }
257260
258− ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, frame->nb_samples);
259− av_frame_free(&frame);
261+ ret = av_audio_fifo_peek(s->fifo, (void **)in->extended_data, window_size);
260262 if (ret < 0)
261− return ret;
263+ goto fail;
262264
263− while (av_audio_fifo_size(s->fifo) >= window_size) {
264− if (!in) {
265− in = ff_get_audio_buffer(outlink, window_size);
266− if (!in)
267− return AVERROR(ENOMEM);
265+ for (ch = 0; ch < inlink->channels; ch++) {
266+ const float *src = (float *)in->extended_data[ch];
267+ FFTComplex *fft_data = s->fft_data[ch];
268+
269+ for (n = 0; n < in->nb_samples; n++) {
270+ fft_data[n].re = src[n] * s->window_func_lut[n];
271+ fft_data[n].im = 0;
268272 }
269273
270− ret = av_audio_fifo_peek(s->fifo, (void **)in->extended_data, window_size);
271− if (ret < 0)
272− break;
274+ for (; n < window_size; n++) {
275+ fft_data[n].re = 0;
276+ fft_data[n].im = 0;
277+ }
278+ }
273279
274− for (ch = 0; ch < inlink->channels; ch++) {
275− const float *src = (float *)in->extended_data[ch];
276− FFTComplex *fft_data = s->fft_data[ch];
280+ values[VAR_PTS] = s->pts;
281+ values[VAR_SAMPLE_RATE] = inlink->sample_rate;
282+ values[VAR_NBBINS] = window_size / 2;
283+ values[VAR_CHANNELS] = inlink->channels;
277284
278− for (n = 0; n < in->nb_samples; n++) {
279− fft_data[n].re = src[n] * s->window_func_lut[n];
280− fft_data[n].im = 0;
281− }
285+ for (ch = 0; ch < inlink->channels; ch++) {
286+ FFTComplex *fft_data = s->fft_data[ch];
282287
283− for (; n < window_size; n++) {
284− fft_data[n].re = 0;
285− fft_data[n].im = 0;
286− }
287− }
288+ av_fft_permute(s->fft, fft_data);
289+ av_fft_calc(s->fft, fft_data);
290+ }
288291
289− values[VAR_PTS] = s->pts;
290− values[VAR_SAMPLE_RATE] = inlink->sample_rate;
291− values[VAR_NBBINS] = window_size / 2;
292− values[VAR_CHANNELS] = inlink->channels;
292+ for (ch = 0; ch < inlink->channels; ch++) {
293+ FFTComplex *fft_data = s->fft_data[ch];
294+ FFTComplex *fft_temp = s->fft_temp[ch];
295+ float *buf = (float *)s->buffer->extended_data[ch];
296+ int x;
297+ values[VAR_CHANNEL] = ch;
293298
294− for (ch = 0; ch < inlink->channels; ch++) {
295− FFTComplex *fft_data = s->fft_data[ch];
299+ for (n = 0; n <= window_size / 2; n++) {
300+ float fr, fi;
296301
297− av_fft_permute(s->fft, fft_data);
298− av_fft_calc(s->fft, fft_data);
299− }
302+ values[VAR_BIN] = n;
303+ values[VAR_REAL] = fft_data[n].re;
304+ values[VAR_IMAG] = fft_data[n].im;
300305
301− for (ch = 0; ch < inlink->channels; ch++) {
302− FFTComplex *fft_data = s->fft_data[ch];
303− FFTComplex *fft_temp = s->fft_temp[ch];
304− float *buf = (float *)s->buffer->extended_data[ch];
305− int x;
306− values[VAR_CHANNEL] = ch;
306+ fr = av_expr_eval(s->real[ch], values, s);
307+ fi = av_expr_eval(s->imag[ch], values, s);
307308
308− for (n = 0; n <= window_size / 2; n++) {
309− float fr, fi;
309+ fft_temp[n].re = fr;
310+ fft_temp[n].im = fi;
311+ }
310312
311− values[VAR_BIN] = n;
312− values[VAR_REAL] = fft_data[n].re;
313− values[VAR_IMAG] = fft_data[n].im;
313+ for (n = window_size / 2 + 1, x = window_size / 2 - 1; n < window_size; n++, x--) {
314+ fft_temp[n].re = fft_temp[x].re;
315+ fft_temp[n].im = -fft_temp[x].im;
316+ }
314317
315− fr = av_expr_eval(s->real[ch], values, s);
316− fi = av_expr_eval(s->imag[ch], values, s);
318+ av_fft_permute(s->ifft, fft_temp);
319+ av_fft_calc(s->ifft, fft_temp);
317320
318− fft_temp[n].re = fr;
319− fft_temp[n].im = fi;
320− }
321+ for (i = 0; i < window_size; i++) {
322+ buf[i] += s->fft_temp[ch][i].re * f;
323+ }
324+ }
321325
322− for (n = window_size / 2 + 1, x = window_size / 2 - 1; n < window_size; n++, x--) {
323− fft_temp[n].re = fft_temp[x].re;
324− fft_temp[n].im = -fft_temp[x].im;
325− }
326+ out = ff_get_audio_buffer(outlink, s->hop_size);
327+ if (!out) {
328+ ret = AVERROR(ENOMEM);
329+ goto fail;
330+ }
326331
327− av_fft_permute(s->ifft, fft_temp);
328− av_fft_calc(s->ifft, fft_temp);
332+ out->pts = s->pts;
333+ s->pts += s->hop_size;
329334
330− start = s->start;
331− end = s->end;
332− k = end;
333− for (i = 0, j = start; j < k && i < window_size; i++, j++) {
334− buf[j] += s->fft_temp[ch][i].re * f;
335− }
335+ for (ch = 0; ch < inlink->channels; ch++) {
336+ float *dst = (float *)out->extended_data[ch];
337+ float *buf = (float *)s->buffer->extended_data[ch];
336338
337− for (; i < window_size; i++, j++) {
338− buf[j] = s->fft_temp[ch][i].re * f;
339− }
339+ for (n = 0; n < s->hop_size; n++)
340+ dst[n] = buf[n] * (1.f - s->overlap);
341+ memmove(buf, buf + s->hop_size, window_size * 4);
342+ }
340343
341− start += s->hop_size;
342− end = j;
343− }
344+ ret = ff_filter_frame(outlink, out);
345+ if (ret < 0)
346+ goto fail;
344347
345− s->start = start;
346− s->end = end;
348+ av_audio_fifo_drain(s->fifo, s->hop_size);
347349
348− if (start >= window_size) {
349− float *dst, *buf;
350+fail:
351+ av_frame_free(&in);
352+ return ret < 0 ? ret : 0;
353+}
350354
351− start -= window_size;
352− end -= window_size;
355+static int activate(AVFilterContext *ctx)
356+{
357+ AVFilterLink *inlink = ctx->inputs[0];
358+ AVFilterLink *outlink = ctx->outputs[0];
359+ AFFTFiltContext *s = ctx->priv;
360+ AVFrame *in = NULL;
361+ int ret = 0, status;
362+ int64_t pts;
353363
354− s->start = start;
355− s->end = end;
364+ FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
356365
357− out = ff_get_audio_buffer(outlink, window_size);
358− if (!out) {
359− ret = AVERROR(ENOMEM);
360− break;
361− }
366+ if (!s->eof && av_audio_fifo_size(s->fifo) < s->window_size) {
367+ ret = ff_inlink_consume_frame(inlink, &in);
368+ if (ret < 0)
369+ return ret;
362370
363− out->pts = s->pts;
364− s->pts += window_size;
371+ if (ret > 0) {
372+ ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
373+ in->nb_samples);
374+ if (ret >= 0 && s->pts == AV_NOPTS_VALUE)
375+ s->pts = in->pts;
365376
366− for (ch = 0; ch < inlink->channels; ch++) {
367− dst = (float *)out->extended_data[ch];
368− buf = (float *)s->buffer->extended_data[ch];
377+ av_frame_free(&in);
378+ if (ret < 0)
379+ return ret;
380+ }
381+ }
369382
370− for (n = 0; n < window_size; n++) {
371− dst[n] = buf[n] * (1 - s->overlap);
372− }
373− memmove(buf, buf + window_size, window_size * 4);
374− }
383+ if ((av_audio_fifo_size(s->fifo) >= s->window_size) ||
384+ (av_audio_fifo_size(s->fifo) > 0 && s->eof)) {
385+ ret = filter_frame(inlink);
386+ if (av_audio_fifo_size(s->fifo) >= s->window_size)
387+ ff_filter_set_ready(ctx, 100);
388+ return ret;
389+ }
375390
376− ret = ff_filter_frame(outlink, out);
377− if (ret < 0)
378− break;
391+ if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
392+ if (status == AVERROR_EOF) {
393+ s->eof = 1;
394+ if (av_audio_fifo_size(s->fifo) >= 0) {
395+ ff_filter_set_ready(ctx, 100);
396+ return 0;
397+ }
379398 }
399+ }
380400
381− av_audio_fifo_drain(s->fifo, s->hop_size);
401+ if (s->eof && av_audio_fifo_size(s->fifo) <= 0) {
402+ ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
403+ return 0;
382404 }
383405
384− av_frame_free(&in);
385− return ret < 0 ? ret : 0;
406+ if (!s->eof)
407+ FF_FILTER_FORWARD_WANTED(outlink, inlink);
408+
409+ return FFERROR_NOT_READY;
386410 }
387411
388412 static int query_formats(AVFilterContext *ctx)
@@ -450,7 +474,6 @@ static const AVFilterPad inputs[] = { · 61 unmodified lines
450474 .name = "default",
451475 .type = AVMEDIA_TYPE_AUDIO,
452476 .config_props = config_input,
453− .filter_frame = filter_frame,
454477 },
455478 { NULL }
456479 };
@@ -470,6 +493,7 @@ AVFilter ff_af_afftfilt = { · 13 unmodified lines
470493 .priv_class = &afftfilt_class,
471494 .inputs = inputs,
472495 .outputs = outputs,
496+ .activate = activate,
473497 .query_formats = query_formats,
474498 .uninit = uninit,
475499 };

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t