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
26#include "libavcodec/avfft.h"26#include "libavcodec/avfft.h"
27#include "libavutil/eval.h"27#include "libavutil/eval.h"
28#include "audio.h"28#include "audio.h"
29#include "filters.h"
29#include "window_func.h"30#include "window_func.h"
30 31
31typedef struct AFFTFiltContext {32typedef struct AFFTFiltContext {
@@ -46,7 +47,7 @@ typedef struct AFFTFiltContext { · 14 unmodified lines
46 int hop_size;47 int hop_size;
47 float overlap;48 float overlap;
48 AVFrame *buffer;49 AVFrame *buffer;
49 int start, end;50 int eof;
50 int win_func;51 int win_func;
51 float win_scale;52 float win_scale;
52 float *window_func_lut;53 float *window_func_lut;
@@ -240,7 +241,7 @@ static int config_input(AVFilterLink *inlink) · 187 unmodified lines
240 return ret;241 return ret;
241}242}
242 243
243static int filter_frame(AVFilterLink *inlink, AVFrame *frame)244static int filter_frame(AVFilterLink *inlink)
244{245{
245 AVFilterContext *ctx = inlink->dst;246 AVFilterContext *ctx = inlink->dst;
246 AVFilterLink *outlink = ctx->outputs[0];247 AVFilterLink *outlink = ctx->outputs[0];
@@ -249,140 +250,163 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame) · 2 unmodified lines
249 const float f = 1. / s->win_scale;250 const float f = 1. / s->win_scale;
250 double values[VAR_VARS_NB];251 double values[VAR_VARS_NB];
251 AVFrame *out, *in = NULL;252 AVFrame *out, *in = NULL;
252 int ch, n, ret, i, j, k;253 int ch, n, ret, i;
253 int start = s->start, end = s->end;
254 254
255 if (s->pts == AV_NOPTS_VALUE)255 if (!in) {
256 s->pts = frame->pts;256 in = ff_get_audio_buffer(outlink, window_size);
257 if (!in)
258 return AVERROR(ENOMEM);
259 }
257 260
258 ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, frame->nb_samples);261 ret = av_audio_fifo_peek(s->fifo, (void **)in->extended_data, window_size);
259 av_frame_free(&frame);
260 if (ret < 0)262 if (ret < 0)
261 return ret;263 goto fail;
262 264
263 while (av_audio_fifo_size(s->fifo) >= window_size) {265 for (ch = 0; ch < inlink->channels; ch++) {
264 if (!in) {266 const float *src = (float *)in->extended_data[ch];
265 in = ff_get_audio_buffer(outlink, window_size);267 FFTComplex *fft_data = s->fft_data[ch];
266 if (!in)268
267 return AVERROR(ENOMEM);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;
268 }272 }
269 273
270 ret = av_audio_fifo_peek(s->fifo, (void **)in->extended_data, window_size);274 for (; n < window_size; n++) {
271 if (ret < 0)275 fft_data[n].re = 0;
272 break;276 fft_data[n].im = 0;
277 }
278 }
273 279
274 for (ch = 0; ch < inlink->channels; ch++) {280 values[VAR_PTS] = s->pts;
275 const float *src = (float *)in->extended_data[ch];281 values[VAR_SAMPLE_RATE] = inlink->sample_rate;
276 FFTComplex *fft_data = s->fft_data[ch];282 values[VAR_NBBINS] = window_size / 2;
283 values[VAR_CHANNELS] = inlink->channels;
277 284
278 for (n = 0; n < in->nb_samples; n++) {285 for (ch = 0; ch < inlink->channels; ch++) {
279 fft_data[n].re = src[n] * s->window_func_lut[n];286 FFTComplex *fft_data = s->fft_data[ch];
280 fft_data[n].im = 0;
281 }
282 287
283 for (; n < window_size; n++) {288 av_fft_permute(s->fft, fft_data);
284 fft_data[n].re = 0;289 av_fft_calc(s->fft, fft_data);
285 fft_data[n].im = 0;290 }
286 }
287 }
288 291
289 values[VAR_PTS] = s->pts;292 for (ch = 0; ch < inlink->channels; ch++) {
290 values[VAR_SAMPLE_RATE] = inlink->sample_rate;293 FFTComplex *fft_data = s->fft_data[ch];
291 values[VAR_NBBINS] = window_size / 2;294 FFTComplex *fft_temp = s->fft_temp[ch];
292 values[VAR_CHANNELS] = inlink->channels;295 float *buf = (float *)s->buffer->extended_data[ch];
296 int x;
297 values[VAR_CHANNEL] = ch;
293 298
294 for (ch = 0; ch < inlink->channels; ch++) {299 for (n = 0; n <= window_size / 2; n++) {
295 FFTComplex *fft_data = s->fft_data[ch];300 float fr, fi;
296 301
297 av_fft_permute(s->fft, fft_data);302 values[VAR_BIN] = n;
298 av_fft_calc(s->fft, fft_data);303 values[VAR_REAL] = fft_data[n].re;
299 }304 values[VAR_IMAG] = fft_data[n].im;
300 305
301 for (ch = 0; ch < inlink->channels; ch++) {306 fr = av_expr_eval(s->real[ch], values, s);
302 FFTComplex *fft_data = s->fft_data[ch];307 fi = av_expr_eval(s->imag[ch], values, s);
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;
307 308
308 for (n = 0; n <= window_size / 2; n++) {309 fft_temp[n].re = fr;
309 float fr, fi;310 fft_temp[n].im = fi;
311 }
310 312
311 values[VAR_BIN] = n;313 for (n = window_size / 2 + 1, x = window_size / 2 - 1; n < window_size; n++, x--) {
312 values[VAR_REAL] = fft_data[n].re;314 fft_temp[n].re = fft_temp[x].re;
313 values[VAR_IMAG] = fft_data[n].im;315 fft_temp[n].im = -fft_temp[x].im;
316 }
314 317
315 fr = av_expr_eval(s->real[ch], values, s);318 av_fft_permute(s->ifft, fft_temp);
316 fi = av_expr_eval(s->imag[ch], values, s);319 av_fft_calc(s->ifft, fft_temp);
317 320
318 fft_temp[n].re = fr;321 for (i = 0; i < window_size; i++) {
319 fft_temp[n].im = fi;322 buf[i] += s->fft_temp[ch][i].re * f;
320 }323 }
324 }
321 325
322 for (n = window_size / 2 + 1, x = window_size / 2 - 1; n < window_size; n++, x--) {326 out = ff_get_audio_buffer(outlink, s->hop_size);
323 fft_temp[n].re = fft_temp[x].re;327 if (!out) {
324 fft_temp[n].im = -fft_temp[x].im;328 ret = AVERROR(ENOMEM);
325 }329 goto fail;
330 }
326 331
327 av_fft_permute(s->ifft, fft_temp);332 out->pts = s->pts;
328 av_fft_calc(s->ifft, fft_temp);333 s->pts += s->hop_size;
329 334
330 start = s->start;335 for (ch = 0; ch < inlink->channels; ch++) {
331 end = s->end;336 float *dst = (float *)out->extended_data[ch];
332 k = end;337 float *buf = (float *)s->buffer->extended_data[ch];
333 for (i = 0, j = start; j < k && i < window_size; i++, j++) {
334 buf[j] += s->fft_temp[ch][i].re * f;
335 }
336 338
337 for (; i < window_size; i++, j++) {339 for (n = 0; n < s->hop_size; n++)
338 buf[j] = s->fft_temp[ch][i].re * f;340 dst[n] = buf[n] * (1.f - s->overlap);
339 }341 memmove(buf, buf + s->hop_size, window_size * 4);
342 }
340 343
341 start += s->hop_size;344 ret = ff_filter_frame(outlink, out);
342 end = j;345 if (ret < 0)
343 }346 goto fail;
344 347
345 s->start = start;348 av_audio_fifo_drain(s->fifo, s->hop_size);
346 s->end = end;
347 349
348 if (start >= window_size) {350fail:
349 float *dst, *buf;351 av_frame_free(&in);
352 return ret < 0 ? ret : 0;
353}
350 354
351 start -= window_size;355static int activate(AVFilterContext *ctx)
352 end -= window_size;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;
353 363
354 s->start = start;364 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
355 s->end = end;
356 365
357 out = ff_get_audio_buffer(outlink, window_size);366 if (!s->eof && av_audio_fifo_size(s->fifo) < s->window_size) {
358 if (!out) {367 ret = ff_inlink_consume_frame(inlink, &in);
359 ret = AVERROR(ENOMEM);368 if (ret < 0)
360 break;369 return ret;
361 }
362 370
363 out->pts = s->pts;371 if (ret > 0) {
364 s->pts += window_size;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;
365 376
366 for (ch = 0; ch < inlink->channels; ch++) {377 av_frame_free(&in);
367 dst = (float *)out->extended_data[ch];378 if (ret < 0)
368 buf = (float *)s->buffer->extended_data[ch];379 return ret;
380 }
381 }
369 382
370 for (n = 0; n < window_size; n++) {383 if ((av_audio_fifo_size(s->fifo) >= s->window_size) ||
371 dst[n] = buf[n] * (1 - s->overlap);384 (av_audio_fifo_size(s->fifo) > 0 && s->eof)) {
372 }385 ret = filter_frame(inlink);
373 memmove(buf, buf + window_size, window_size * 4);386 if (av_audio_fifo_size(s->fifo) >= s->window_size)
374 }387 ff_filter_set_ready(ctx, 100);
388 return ret;
389 }
375 390
376 ret = ff_filter_frame(outlink, out);391 if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
377 if (ret < 0)392 if (status == AVERROR_EOF) {
378 break;393 s->eof = 1;
394 if (av_audio_fifo_size(s->fifo) >= 0) {
395 ff_filter_set_ready(ctx, 100);
396 return 0;
397 }
379 }398 }
399 }
380 400
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;
382 }404 }
383 405
384 av_frame_free(&in);406 if (!s->eof)
385 return ret < 0 ? ret : 0;407 FF_FILTER_FORWARD_WANTED(outlink, inlink);
408
409 return FFERROR_NOT_READY;
386}410}
387 411
388static int query_formats(AVFilterContext *ctx)412static int query_formats(AVFilterContext *ctx)
@@ -450,7 +474,6 @@ static const AVFilterPad inputs[] = { · 61 unmodified lines
450 .name = "default",474 .name = "default",
451 .type = AVMEDIA_TYPE_AUDIO,475 .type = AVMEDIA_TYPE_AUDIO,
452 .config_props = config_input,476 .config_props = config_input,
453 .filter_frame = filter_frame,
454 },477 },
455 { NULL }478 { NULL }
456};479};
@@ -470,6 +493,7 @@ AVFilter ff_af_afftfilt = { · 13 unmodified lines
470 .priv_class = &afftfilt_class,493 .priv_class = &afftfilt_class,
471 .inputs = inputs,494 .inputs = inputs,
472 .outputs = outputs,495 .outputs = outputs,
496 .activate = activate,
473 .query_formats = query_formats,497 .query_formats = query_formats,
474 .uninit = uninit,498 .uninit = uninit,
475};499};

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