History

avfilter/avf_showcwt: optimize multiplication with kernel

Paul B Mahol committed 3 years ago

Commit 2f2f76d1 parent 587189c1 file · +61 −12Browse files

Unified · Split

libavfilter/avf_showcwt.c+61−12
@@ -25,6 +25,7 @@ · 24 unmodified lines
2525 #include "libavutil/avassert.h"
2626 #include "libavutil/avstring.h"
2727 #include "libavutil/channel_layout.h"
28+#include "libavutil/float_dsp.h"
2829 #include "libavutil/cpu.h"
2930 #include "libavutil/opt.h"
3031 #include "libavutil/parseutils.h"
@@ -89,6 +90,9 @@ typedef struct ShowCWTContext { · 58 unmodified lines
8990 AVFrame *outpicref;
9091 AVFrame *fft_in;
9192 AVFrame *fft_out;
93+ AVFrame *dst_x;
94+ AVFrame *src_x;
95+ AVFrame *kernel_x;
9296 AVFrame *ifft_in;
9397 AVFrame *ifft_out;
9498 AVFrame *ch_out;
@@ -114,6 +118,8 @@ typedef struct ShowCWTContext { · 19 unmodified lines
114118 float minimum_frequency;
115119 float maximum_frequency;
116120 float deviation;
121+
122+ AVFloatDSPContext *fdsp;
117123 } ShowCWTContext;
118124
119125 #define OFFSET(x) offsetof(ShowCWTContext, x)
@@ -172,6 +178,9 @@ static av_cold void uninit(AVFilterContext *ctx) · 52 unmodified lines
172178 av_frame_free(&s->outpicref);
173179 av_frame_free(&s->fft_in);
174180 av_frame_free(&s->fft_out);
181+ av_frame_free(&s->dst_x);
182+ av_frame_free(&s->src_x);
183+ av_frame_free(&s->kernel_x);
175184 av_frame_free(&s->ifft_in);
176185 av_frame_free(&s->ifft_out);
177186 av_frame_free(&s->ch_out);
@@ -187,6 +196,8 @@ static av_cold void uninit(AVFilterContext *ctx) · 9 unmodified lines
187196 av_tx_uninit(&s->ifft[n]);
188197 av_freep(&s->ifft);
189198 }
199+
200+ av_freep(&s->fdsp);
190201 }
191202
192203 static int query_formats(AVFilterContext *ctx)
@@ -483,18 +494,27 @@ static int run_channel_cwt(AVFilterContext *ctx, void *arg, int jobnr, int nb_jo · 290 unmodified lines
483494 AVComplexFloat *isrc = (AVComplexFloat *)s->ifft_in->extended_data[y];
484495 AVComplexFloat *idst = (AVComplexFloat *)s->ifft_out->extended_data[y];
485496 AVComplexFloat *chout = ((AVComplexFloat *)s->ch_out->extended_data[ch]) + y * ihop_size;
486− const float *kernel = (const float *)s->kernel->extended_data[y];
497+ AVComplexFloat *dstx = (AVComplexFloat *)s->dst_x->extended_data[jobnr];
498+ AVComplexFloat *srcx = (AVComplexFloat *)s->src_x->extended_data[jobnr];
499+ AVComplexFloat *kernelx = (AVComplexFloat *)s->kernel_x->extended_data[jobnr];
500+ const AVComplexFloat *kernel = (const AVComplexFloat *)s->kernel->extended_data[y];
487501 const unsigned *index = (const unsigned *)s->index;
488502 const int kernel_start = s->kernel_start[y];
489503 const int kernel_stop = s->kernel_stop[y];
504+ const int kernel_range = kernel_stop - kernel_start;
505+
506+ memcpy(kernelx, kernel + kernel_start, sizeof(*kernel) * kernel_range);
507+ memcpy(srcx, dst + kernel_start, sizeof(*dst) * kernel_range);
508+
509+ s->fdsp->vector_fmul((float *)dstx, (const float *)srcx,
510+ (const float *)kernelx, FFALIGN(kernel_range * 2, 16));
490511
491512 memset(isrc, 0, sizeof(*isrc) * output_sample_count);
492− for (int i = kernel_start; i < kernel_stop; i++) {
493− const unsigned n = index[i];
494− const float ff = kernel[i];
513+ for (int i = 0; i < kernel_range; i++) {
514+ const unsigned n = index[i + kernel_start];
495515
496− isrc[n].re += ff * dst[i].re;
497− isrc[n].im += ff * dst[i].im;
516+ isrc[n].re += dstx[i].re;
517+ isrc[n].im += dstx[i].im;
498518 }
499519
500520 s->itx_fn(s->ifft[jobnr], idst, isrc, sizeof(*isrc));
@@ -515,7 +535,7 @@ static void compute_kernel(AVFilterContext *ctx) · 14 unmodified lines
515535 unsigned *index = s->index;
516536
517537 for (int y = 0; y < fsize; y++) {
518− float *kernel = (float *)s->kernel->extended_data[y];
538+ AVComplexFloat *kernel = (AVComplexFloat *)s->kernel->extended_data[y];
519539 int *kernel_start = s->kernel_start;
520540 int *kernel_stop = s->kernel_stop;
521541 float frequency = s->frequency_band[y*2];
@@ -527,18 +547,19 @@ static void compute_kernel(AVFilterContext *ctx) · 5 unmodified lines
527547
528548 f = size - fabsf(f - size);
529549 ff = expf(-f*f*deviation) * scale_factor;
530− kernel[n] = ff;
550+ kernel[n].re = ff;
551+ kernel[n].im = ff;
531552 }
532553
533554 for (int n = 0; n < size; n++) {
534− if (kernel[n] != 0.f) {
555+ if (kernel[n].re != 0.f) {
535556 kernel_start[y] = n;
536557 break;
537558 }
538559 }
539560
540561 for (int n = 0; n < size; n++) {
541− if (kernel[size - n - 1] != 0.f) {
562+ if (kernel[size - n - 1].re != 0.f) {
542563 kernel_stop[y] = size - n;
543564 break;
544565 }
@@ -561,6 +582,10 @@ static int config_output(AVFilterLink *outlink) · 16 unmodified lines
561582
562583 uninit(ctx);
563584
585+ s->fdsp = avpriv_float_dsp_alloc(0);
586+ if (!s->fdsp)
587+ return AVERROR(ENOMEM);
588+
564589 switch (s->direction) {
565590 case DIRECTION_LR:
566591 case DIRECTION_RL:
@@ -622,6 +647,9 @@ static int config_output(AVFilterLink *outlink) · 55 unmodified lines
622647 s->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
623648 s->fft_in = ff_get_audio_buffer(inlink, s->fft_in_size * 2);
624649 s->fft_out = ff_get_audio_buffer(inlink, s->fft_out_size * 2);
650+ s->dst_x = av_frame_alloc();
651+ s->src_x = av_frame_alloc();
652+ s->kernel_x = av_frame_alloc();
625653 s->cache = ff_get_audio_buffer(inlink, s->fft_in_size * 2);
626654 s->ch_out = ff_get_audio_buffer(inlink, s->frequency_band_count * 2 * s->ihop_size);
627655 s->ifft_in = av_frame_alloc();
@@ -630,7 +658,7 @@ static int config_output(AVFilterLink *outlink) · 2 unmodified lines
630658 s->index = av_calloc(s->input_padding_size, sizeof(*s->index));
631659 s->kernel_start = av_calloc(s->frequency_band_count, sizeof(*s->kernel_start));
632660 s->kernel_stop = av_calloc(s->frequency_band_count, sizeof(*s->kernel_stop));
633− if (!s->outpicref || !s->fft_in || !s->fft_out ||
661+ if (!s->outpicref || !s->fft_in || !s->fft_out || !s->src_x || !s->dst_x || !s->kernel_x ||
634662 !s->ifft_in || !s->ifft_out || !s->kernel_start || !s->kernel_stop ||
635663 !s->frequency_band || !s->kernel || !s->cache || !s->index)
636664 return AVERROR(ENOMEM);
@@ -650,12 +678,33 @@ static int config_output(AVFilterLink *outlink) · 13 unmodified lines
650678 return ret;
651679
652680 s->kernel->format = inlink->format;
653− s->kernel->nb_samples = s->input_padding_size;
681+ s->kernel->nb_samples = s->input_padding_size * 2;
654682 s->kernel->ch_layout.nb_channels = s->frequency_band_count;
655683 ret = av_frame_get_buffer(s->kernel, 0);
656684 if (ret < 0)
657685 return ret;
658686
687+ s->src_x->format = inlink->format;
688+ s->src_x->nb_samples = s->fft_out_size * 2;
689+ s->src_x->ch_layout.nb_channels = s->nb_threads;
690+ ret = av_frame_get_buffer(s->src_x, 0);
691+ if (ret < 0)
692+ return ret;
693+
694+ s->dst_x->format = inlink->format;
695+ s->dst_x->nb_samples = s->fft_out_size * 2;
696+ s->dst_x->ch_layout.nb_channels = s->nb_threads;
697+ ret = av_frame_get_buffer(s->dst_x, 0);
698+ if (ret < 0)
699+ return ret;
700+
701+ s->kernel_x->format = inlink->format;
702+ s->kernel_x->nb_samples = s->fft_out_size * 2;
703+ s->kernel_x->ch_layout.nb_channels = s->nb_threads;
704+ ret = av_frame_get_buffer(s->kernel_x, 0);
705+ if (ret < 0)
706+ return ret;
707+
659708 s->outpicref->sample_aspect_ratio = (AVRational){1,1};
660709
661710 for (int y = 0; y < outlink->h; y++) {

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