... | ... |
@@ -64,7 +64,7 @@ typedef struct { |
64 | 64 |
float rotation; ///< color rotation |
65 | 65 |
int data; |
66 | 66 |
int xpos; ///< x position (current column) |
67 |
- FFTContext *fft; ///< Fast Fourier Transform context |
|
67 |
+ FFTContext **fft; ///< Fast Fourier Transform context |
|
68 | 68 |
int fft_bits; ///< number of bits (FFT window size = 1<<fft_bits) |
69 | 69 |
FFTComplex **fft_data; ///< bins holder for each (displayed) channels |
70 | 70 |
float *window_func_lut; ///< Window function LUT |
... | ... |
@@ -225,11 +225,15 @@ static av_cold void uninit(AVFilterContext *ctx) |
225 | 225 |
int i; |
226 | 226 |
|
227 | 227 |
av_freep(&s->combine_buffer); |
228 |
- av_fft_end(s->fft); |
|
228 |
+ if (s->fft) { |
|
229 |
+ for (i = 0; i < s->nb_display_channels; i++) |
|
230 |
+ av_fft_end(s->fft[i]); |
|
231 |
+ } |
|
229 | 232 |
if (s->fft_data) { |
230 | 233 |
for (i = 0; i < s->nb_display_channels; i++) |
231 | 234 |
av_freep(&s->fft_data[i]); |
232 | 235 |
} |
236 |
+ av_freep(&s->fft); |
|
233 | 237 |
av_freep(&s->fft_data); |
234 | 238 |
av_freep(&s->window_func_lut); |
235 | 239 |
if (s->magnitudes) { |
... | ... |
@@ -312,26 +316,36 @@ static int config_output(AVFilterLink *outlink) |
312 | 312 |
} |
313 | 313 |
s->win_size = 1 << fft_bits; |
314 | 314 |
|
315 |
+ if (!s->fft) { |
|
316 |
+ s->fft = av_calloc(inlink->channels, sizeof(*s->fft)); |
|
317 |
+ if (!s->fft) |
|
318 |
+ return AVERROR(ENOMEM); |
|
319 |
+ } |
|
320 |
+ |
|
315 | 321 |
/* (re-)configuration if the video output changed (or first init) */ |
316 | 322 |
if (fft_bits != s->fft_bits) { |
317 | 323 |
AVFrame *outpicref; |
318 | 324 |
|
319 |
- av_fft_end(s->fft); |
|
320 |
- s->fft = av_fft_init(fft_bits, 0); |
|
321 |
- if (!s->fft) { |
|
322 |
- av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. " |
|
323 |
- "The window size might be too high.\n"); |
|
324 |
- return AVERROR(EINVAL); |
|
325 |
- } |
|
326 | 325 |
s->fft_bits = fft_bits; |
327 | 326 |
|
328 | 327 |
/* FFT buffers: x2 for each (display) channel buffer. |
329 | 328 |
* Note: we use free and malloc instead of a realloc-like function to |
330 | 329 |
* make sure the buffer is aligned in memory for the FFT functions. */ |
331 |
- for (i = 0; i < s->nb_display_channels; i++) |
|
330 |
+ for (i = 0; i < s->nb_display_channels; i++) { |
|
331 |
+ av_fft_end(s->fft[i]); |
|
332 | 332 |
av_freep(&s->fft_data[i]); |
333 |
+ } |
|
333 | 334 |
av_freep(&s->fft_data); |
335 |
+ |
|
334 | 336 |
s->nb_display_channels = inlink->channels; |
337 |
+ for (i = 0; i < s->nb_display_channels; i++) { |
|
338 |
+ s->fft[i] = av_fft_init(fft_bits, 0); |
|
339 |
+ if (!s->fft[i]) { |
|
340 |
+ av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. " |
|
341 |
+ "The window size might be too high.\n"); |
|
342 |
+ return AVERROR(EINVAL); |
|
343 |
+ } |
|
344 |
+ } |
|
335 | 345 |
|
336 | 346 |
s->magnitudes = av_calloc(s->nb_display_channels, sizeof(*s->magnitudes)); |
337 | 347 |
if (!s->magnitudes) |
... | ... |
@@ -342,7 +356,7 @@ static int config_output(AVFilterLink *outlink) |
342 | 342 |
return AVERROR(ENOMEM); |
343 | 343 |
} |
344 | 344 |
|
345 |
- s->phases = av_calloc(s->nb_display_channels, sizeof(*s->magnitudes)); |
|
345 |
+ s->phases = av_calloc(s->nb_display_channels, sizeof(*s->phases)); |
|
346 | 346 |
if (!s->phases) |
347 | 347 |
return AVERROR(ENOMEM); |
348 | 348 |
for (i = 0; i < s->nb_display_channels; i++) { |
... | ... |
@@ -425,25 +439,27 @@ static int config_output(AVFilterLink *outlink) |
425 | 425 |
return 0; |
426 | 426 |
} |
427 | 427 |
|
428 |
-static void run_fft(ShowSpectrumContext *s, AVFrame *fin) |
|
428 |
+static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
|
429 | 429 |
{ |
430 |
- int ch, n; |
|
430 |
+ ShowSpectrumContext *s = ctx->priv; |
|
431 |
+ const float *window_func_lut = s->window_func_lut; |
|
432 |
+ AVFrame *fin = arg; |
|
433 |
+ const int ch = jobnr; |
|
434 |
+ int n; |
|
431 | 435 |
|
432 | 436 |
/* fill FFT input with the number of samples available */ |
433 |
- for (ch = 0; ch < s->nb_display_channels; ch++) { |
|
434 |
- const float *p = (float *)fin->extended_data[ch]; |
|
437 |
+ const float *p = (float *)fin->extended_data[ch]; |
|
435 | 438 |
|
436 |
- for (n = 0; n < s->win_size; n++) { |
|
437 |
- s->fft_data[ch][n].re = p[n] * s->window_func_lut[n]; |
|
438 |
- s->fft_data[ch][n].im = 0; |
|
439 |
- } |
|
439 |
+ for (n = 0; n < s->win_size; n++) { |
|
440 |
+ s->fft_data[ch][n].re = p[n] * window_func_lut[n]; |
|
441 |
+ s->fft_data[ch][n].im = 0; |
|
440 | 442 |
} |
441 | 443 |
|
442 | 444 |
/* run FFT on each samples set */ |
443 |
- for (ch = 0; ch < s->nb_display_channels; ch++) { |
|
444 |
- av_fft_permute(s->fft, s->fft_data[ch]); |
|
445 |
- av_fft_calc(s->fft, s->fft_data[ch]); |
|
446 |
- } |
|
445 |
+ av_fft_permute(s->fft[ch], s->fft_data[ch]); |
|
446 |
+ av_fft_calc(s->fft[ch], s->fft_data[ch]); |
|
447 |
+ |
|
448 |
+ return 0; |
|
447 | 449 |
} |
448 | 450 |
|
449 | 451 |
#define RE(y, ch) s->fft_data[ch][y].re |
... | ... |
@@ -451,30 +467,33 @@ static void run_fft(ShowSpectrumContext *s, AVFrame *fin) |
451 | 451 |
#define MAGNITUDE(y, ch) hypot(RE(y, ch), IM(y, ch)) |
452 | 452 |
#define PHASE(y, ch) atan2(IM(y, ch), RE(y, ch)) |
453 | 453 |
|
454 |
-static void calc_magnitudes(ShowSpectrumContext *s) |
|
454 |
+static int calc_channel_magnitudes(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
|
455 | 455 |
{ |
456 |
+ ShowSpectrumContext *s = ctx->priv; |
|
456 | 457 |
const double w = s->win_scale * (s->scale == LOG ? s->win_scale : 1); |
457 |
- int ch, y, h = s->orientation == VERTICAL ? s->h : s->w; |
|
458 |
+ int y, h = s->orientation == VERTICAL ? s->h : s->w; |
|
458 | 459 |
const float f = s->gain * w; |
460 |
+ const int ch = jobnr; |
|
461 |
+ float *magnitudes = s->magnitudes[ch]; |
|
459 | 462 |
|
460 |
- for (ch = 0; ch < s->nb_display_channels; ch++) { |
|
461 |
- float *magnitudes = s->magnitudes[ch]; |
|
463 |
+ for (y = 0; y < h; y++) |
|
464 |
+ magnitudes[y] = MAGNITUDE(y, ch) * f; |
|
462 | 465 |
|
463 |
- for (y = 0; y < h; y++) |
|
464 |
- magnitudes[y] = MAGNITUDE(y, ch) * f; |
|
465 |
- } |
|
466 |
+ return 0; |
|
466 | 467 |
} |
467 | 468 |
|
468 |
-static void calc_phases(ShowSpectrumContext *s) |
|
469 |
+static int calc_channel_phases(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
|
469 | 470 |
{ |
470 |
- int ch, y, h = s->orientation == VERTICAL ? s->h : s->w; |
|
471 |
+ ShowSpectrumContext *s = ctx->priv; |
|
472 |
+ const int h = s->orientation == VERTICAL ? s->h : s->w; |
|
473 |
+ const int ch = jobnr; |
|
474 |
+ float *phases = s->phases[ch]; |
|
475 |
+ int y; |
|
471 | 476 |
|
472 |
- for (ch = 0; ch < s->nb_display_channels; ch++) { |
|
473 |
- float *phases = s->phases[ch]; |
|
477 |
+ for (y = 0; y < h; y++) |
|
478 |
+ phases[y] = (PHASE(y, ch) / M_PI + 1) / 2; |
|
474 | 479 |
|
475 |
- for (y = 0; y < h; y++) |
|
476 |
- phases[y] = (PHASE(y, ch) / M_PI + 1) / 2; |
|
477 |
- } |
|
480 |
+ return 0; |
|
478 | 481 |
} |
479 | 482 |
|
480 | 483 |
static void acalc_magnitudes(ShowSpectrumContext *s) |
... | ... |
@@ -614,75 +633,81 @@ static void clear_combine_buffer(ShowSpectrumContext *s, int size) |
614 | 614 |
} |
615 | 615 |
} |
616 | 616 |
|
617 |
+static int plot_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
|
618 |
+{ |
|
619 |
+ ShowSpectrumContext *s = ctx->priv; |
|
620 |
+ const int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width; |
|
621 |
+ const int ch = jobnr; |
|
622 |
+ float *magnitudes = s->magnitudes[ch]; |
|
623 |
+ float *phases = s->phases[ch]; |
|
624 |
+ float yf, uf, vf; |
|
625 |
+ int y; |
|
626 |
+ |
|
627 |
+ /* decide color range */ |
|
628 |
+ color_range(s, ch, &yf, &uf, &vf); |
|
629 |
+ |
|
630 |
+ /* draw the channel */ |
|
631 |
+ for (y = 0; y < h; y++) { |
|
632 |
+ int row = (s->mode == COMBINED) ? y : ch * h + y; |
|
633 |
+ float *out = &s->combine_buffer[3 * row]; |
|
634 |
+ float a; |
|
635 |
+ |
|
636 |
+ switch (s->data) { |
|
637 |
+ case D_MAGNITUDE: |
|
638 |
+ /* get magnitude */ |
|
639 |
+ a = magnitudes[y]; |
|
640 |
+ break; |
|
641 |
+ case D_PHASE: |
|
642 |
+ /* get phase */ |
|
643 |
+ a = phases[y]; |
|
644 |
+ break; |
|
645 |
+ default: |
|
646 |
+ av_assert0(0); |
|
647 |
+ } |
|
648 |
+ |
|
649 |
+ /* apply scale */ |
|
650 |
+ switch (s->scale) { |
|
651 |
+ case LINEAR: |
|
652 |
+ a = av_clipf(a, 0, 1); |
|
653 |
+ break; |
|
654 |
+ case SQRT: |
|
655 |
+ a = av_clipf(sqrt(a), 0, 1); |
|
656 |
+ break; |
|
657 |
+ case CBRT: |
|
658 |
+ a = av_clipf(cbrt(a), 0, 1); |
|
659 |
+ break; |
|
660 |
+ case FOURTHRT: |
|
661 |
+ a = av_clipf(sqrt(sqrt(a)), 0, 1); |
|
662 |
+ break; |
|
663 |
+ case FIFTHRT: |
|
664 |
+ a = av_clipf(pow(a, 0.20), 0, 1); |
|
665 |
+ break; |
|
666 |
+ case LOG: |
|
667 |
+ a = 1 + log10(av_clipd(a, 1e-6, 1)) / 6; // zero = -120dBFS |
|
668 |
+ break; |
|
669 |
+ default: |
|
670 |
+ av_assert0(0); |
|
671 |
+ } |
|
672 |
+ |
|
673 |
+ pick_color(s, yf, uf, vf, a, out); |
|
674 |
+ } |
|
675 |
+ |
|
676 |
+ return 0; |
|
677 |
+} |
|
678 |
+ |
|
617 | 679 |
static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples) |
618 | 680 |
{ |
619 |
- int ret; |
|
620 | 681 |
AVFilterContext *ctx = inlink->dst; |
621 | 682 |
AVFilterLink *outlink = ctx->outputs[0]; |
622 | 683 |
ShowSpectrumContext *s = ctx->priv; |
623 | 684 |
AVFrame *outpicref = s->outpicref; |
624 |
- int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width; |
|
625 |
- |
|
626 |
- int ch, plane, x, y; |
|
685 |
+ int ret, plane, x, y; |
|
627 | 686 |
|
628 | 687 |
/* fill a new spectrum column */ |
629 | 688 |
/* initialize buffer for combining to black */ |
630 | 689 |
clear_combine_buffer(s, s->orientation == VERTICAL ? s->h : s->w); |
631 | 690 |
|
632 |
- for (ch = 0; ch < s->nb_display_channels; ch++) { |
|
633 |
- float *magnitudes = s->magnitudes[ch]; |
|
634 |
- float *phases = s->phases[ch]; |
|
635 |
- float yf, uf, vf; |
|
636 |
- |
|
637 |
- /* decide color range */ |
|
638 |
- color_range(s, ch, &yf, &uf, &vf); |
|
639 |
- |
|
640 |
- /* draw the channel */ |
|
641 |
- for (y = 0; y < h; y++) { |
|
642 |
- int row = (s->mode == COMBINED) ? y : ch * h + y; |
|
643 |
- float *out = &s->combine_buffer[3 * row]; |
|
644 |
- float a; |
|
645 |
- |
|
646 |
- switch (s->data) { |
|
647 |
- case D_MAGNITUDE: |
|
648 |
- /* get magnitude */ |
|
649 |
- a = magnitudes[y]; |
|
650 |
- break; |
|
651 |
- case D_PHASE: |
|
652 |
- /* get phase */ |
|
653 |
- a = phases[y]; |
|
654 |
- break; |
|
655 |
- default: |
|
656 |
- av_assert0(0); |
|
657 |
- } |
|
658 |
- |
|
659 |
- /* apply scale */ |
|
660 |
- switch (s->scale) { |
|
661 |
- case LINEAR: |
|
662 |
- a = av_clipf(a, 0, 1); |
|
663 |
- break; |
|
664 |
- case SQRT: |
|
665 |
- a = av_clipf(sqrt(a), 0, 1); |
|
666 |
- break; |
|
667 |
- case CBRT: |
|
668 |
- a = av_clipf(cbrt(a), 0, 1); |
|
669 |
- break; |
|
670 |
- case FOURTHRT: |
|
671 |
- a = av_clipf(sqrt(sqrt(a)), 0, 1); |
|
672 |
- break; |
|
673 |
- case FIFTHRT: |
|
674 |
- a = av_clipf(pow(a, 0.20), 0, 1); |
|
675 |
- break; |
|
676 |
- case LOG: |
|
677 |
- a = 1 + log10(av_clipd(a, 1e-6, 1)) / 6; // zero = -120dBFS |
|
678 |
- break; |
|
679 |
- default: |
|
680 |
- av_assert0(0); |
|
681 |
- } |
|
682 |
- |
|
683 |
- pick_color(s, yf, uf, vf, a, out); |
|
684 |
- } |
|
685 |
- } |
|
691 |
+ ctx->internal->execute(ctx, plot_channel, NULL, NULL, s->nb_display_channels); |
|
686 | 692 |
|
687 | 693 |
av_frame_make_writable(s->outpicref); |
688 | 694 |
/* copy to output */ |
... | ... |
@@ -821,11 +846,13 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) |
821 | 821 |
|
822 | 822 |
av_assert0(fin->nb_samples == s->win_size); |
823 | 823 |
|
824 |
- run_fft(s, fin); |
|
824 |
+ ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels); |
|
825 |
+ |
|
825 | 826 |
if (s->data == D_MAGNITUDE) |
826 |
- calc_magnitudes(s); |
|
827 |
+ ctx->internal->execute(ctx, calc_channel_magnitudes, NULL, NULL, s->nb_display_channels); |
|
828 |
+ |
|
827 | 829 |
if (s->data == D_PHASE) |
828 |
- calc_phases(s); |
|
830 |
+ ctx->internal->execute(ctx, calc_channel_phases, NULL, NULL, s->nb_display_channels); |
|
829 | 831 |
|
830 | 832 |
ret = plot_spectrum_column(inlink, fin); |
831 | 833 |
av_frame_free(&fin); |
... | ... |
@@ -868,6 +895,7 @@ AVFilter ff_avf_showspectrum = { |
868 | 868 |
.inputs = showspectrum_inputs, |
869 | 869 |
.outputs = showspectrum_outputs, |
870 | 870 |
.priv_class = &showspectrum_class, |
871 |
+ .flags = AVFILTER_FLAG_SLICE_THREADS, |
|
871 | 872 |
}; |
872 | 873 |
#endif // CONFIG_SHOWSPECTRUM_FILTER |
873 | 874 |
|
... | ... |
@@ -961,8 +989,9 @@ static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o) |
961 | 961 |
|
962 | 962 |
static int showspectrumpic_request_frame(AVFilterLink *outlink) |
963 | 963 |
{ |
964 |
- ShowSpectrumContext *s = outlink->src->priv; |
|
965 |
- AVFilterLink *inlink = outlink->src->inputs[0]; |
|
964 |
+ AVFilterContext *ctx = outlink->src; |
|
965 |
+ ShowSpectrumContext *s = ctx->priv; |
|
966 |
+ AVFilterLink *inlink = ctx->inputs[0]; |
|
966 | 967 |
int ret; |
967 | 968 |
|
968 | 969 |
ret = ff_request_frame(inlink); |
... | ... |
@@ -996,7 +1025,7 @@ static int showspectrumpic_request_frame(AVFilterLink *outlink) |
996 | 996 |
} |
997 | 997 |
} |
998 | 998 |
|
999 |
- run_fft(s, fin); |
|
999 |
+ ctx->internal->execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels); |
|
1000 | 1000 |
acalc_magnitudes(s); |
1001 | 1001 |
|
1002 | 1002 |
consumed += spf; |
... | ... |
@@ -1242,6 +1271,7 @@ AVFilter ff_avf_showspectrumpic = { |
1242 | 1242 |
.inputs = showspectrumpic_inputs, |
1243 | 1243 |
.outputs = showspectrumpic_outputs, |
1244 | 1244 |
.priv_class = &showspectrumpic_class, |
1245 |
+ .flags = AVFILTER_FLAG_SLICE_THREADS, |
|
1245 | 1246 |
}; |
1246 | 1247 |
|
1247 | 1248 |
#endif // CONFIG_SHOWSPECTRUMPIC_FILTER |