History

avfilter/avf_showcwt: add some small optimizations

Paul B Mahol committed 3 years ago

Commit bcb5ef61 parent d7a5f061 file · +26 −30Browse files

Unified · Split

libavfilter/avf_showcwt.c+26−30
@@ -81,7 +81,7 @@ typedef struct ShowCWTContext { · 80 unmodified lines
8181 unsigned *index;
8282 int *kernel_start;
8383 int *kernel_stop;
84− AVFrame *overlap;
84+ AVFrame *cache[2];
8585 AVFrame *outpicref;
8686 AVFrame *fft_in;
8787 AVFrame *fft_out;
@@ -158,7 +158,8 @@ static av_cold void uninit(AVFilterContext *ctx) · 70 unmodified lines
158158 av_freep(&s->index);
159159
160160 av_frame_free(&s->kernel);
161− av_frame_free(&s->overlap);
161+ av_frame_free(&s->cache[0]);
162+ av_frame_free(&s->cache[1]);
162163 av_frame_free(&s->outpicref);
163164 av_frame_free(&s->fft_in);
164165 av_frame_free(&s->fft_out);
@@ -243,12 +244,6 @@ static void frequency_band(float *frequency_band, · 78 unmodified lines
243244 }
244245 }
245246
246−#define cmul(operator, index) { \
247− const float ff = kernel[index]; \
248− isrc[n].re operator ff*dst[index].re; \
249− isrc[n].im operator ff*dst[index].im; \
250−}
251−
252247 static float remap_log(float value, float log_factor)
253248 {
254249 float sign = (0 < value) - (value < 0);
@@ -261,36 +256,33 @@ static float remap_log(float value, float log_factor) · 6 unmodified lines
261256 static int run_channel_cwt_prepare(AVFilterContext *ctx, void *arg, int jobnr, int ch)
262257 {
263258 ShowCWTContext *s = ctx->priv;
259+ const int hop_size = s->hop_size;
264260 AVFrame *fin = arg;
265− float *overlap = (float *)s->overlap->extended_data[ch];
261+ float *cache0 = (float *)s->cache[0]->extended_data[ch];
262+ float *cache = (float *)s->cache[1]->extended_data[ch];
266263 AVComplexFloat *src = (AVComplexFloat *)s->fft_in->extended_data[ch];
267264 AVComplexFloat *dst = (AVComplexFloat *)s->fft_out->extended_data[ch];
268− const int nb_consumed_samples = s->nb_consumed_samples;
269− const int input_padding_size = s->input_padding_size;
270265
271266 if (fin) {
272− const int hop_size = fin->nb_samples;
273− const int offset = input_padding_size - hop_size;
267+ const int offset = s->hop_index;
274268 const float *input = (const float *)fin->extended_data[ch];
275269
276− memmove(overlap, &overlap[hop_size], offset * sizeof(float));
277− memcpy(&overlap[offset], input,
270+ memcpy(&cache[offset], input,
278271 fin->nb_samples * sizeof(float));
279272 }
280273
281274 if (fin == NULL) {
282− const int hop_size = s->hop_size;
283−
284− memmove(overlap, &overlap[hop_size], hop_size * sizeof(float));
285− memset(&overlap[s->hop_index], 0,
275+ memset(&cache[s->hop_index], 0,
286276 (hop_size - s->hop_index) * sizeof(float));
287− } else if (s->hop_index + fin->nb_samples < s->hop_size) {
277+ } else if (s->hop_index + fin->nb_samples < hop_size) {
288278 return 0;
289279 }
290280
291− for (int n = 0; n < nb_consumed_samples; n++) {
292− src[n].re = overlap[n];
281+ for (int n = 0; n < hop_size; n++) {
282+ src[n].re = cache0[n];
293283 src[n].im = 0.f;
284+ src[n + hop_size].re = cache[n];
285+ src[n + hop_size].im = 0.f;
294286 }
295287
296288 s->tx_fn(s->fft[jobnr], dst, src, sizeof(*src));
@@ -442,15 +434,15 @@ static int run_channel_cwt(AVFilterContext *ctx, void *arg, int jobnr, int nb_jo · 145 unmodified lines
442434 memset(isrc, 0, sizeof(*isrc) * output_sample_count);
443435 for (int i = kernel_start; i < kernel_stop; i++) {
444436 const unsigned n = index[i];
445− cmul(+=, i);
437+ const float ff = kernel[i];
438+
439+ isrc[n].re += ff * dst[i].re;
440+ isrc[n].im += ff * dst[i].im;
446441 }
447442
448443 s->itx_fn(s->ifft[jobnr], idst, isrc, sizeof(*isrc));
449444
450− for (int i = 0; i < ihop_size; i++) {
451− chout[i].re = idst[ioffset + i].re;
452− chout[i].im = idst[ioffset + i].im;
453− }
445+ memcpy(chout, idst + ioffset, sizeof(*chout) * ihop_size);
454446 }
455447
456448 return 0;
@@ -572,7 +564,8 @@ static int config_output(AVFilterLink *outlink) · 115 unmodified lines
572564 s->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
573565 s->fft_in = ff_get_audio_buffer(inlink, s->fft_in_size * 2);
574566 s->fft_out = ff_get_audio_buffer(inlink, s->fft_out_size * 2);
575− s->overlap = ff_get_audio_buffer(inlink, s->input_padding_size);
567+ s->cache[0] = ff_get_audio_buffer(inlink, s->hop_size);
568+ s->cache[1] = ff_get_audio_buffer(inlink, s->hop_size);
576569 s->ch_out = ff_get_audio_buffer(inlink, s->frequency_band_count * 2 * s->ihop_size);
577570 s->ifft_in = av_frame_alloc();
578571 s->ifft_out = av_frame_alloc();
@@ -582,7 +575,7 @@ static int config_output(AVFilterLink *outlink) · 3 unmodified lines
582575 s->kernel_stop = av_calloc(s->frequency_band_count, sizeof(*s->kernel_stop));
583576 if (!s->outpicref || !s->fft_in || !s->fft_out ||
584577 !s->ifft_in || !s->ifft_out || !s->kernel_start || !s->kernel_stop ||
585− !s->frequency_band || !s->kernel || !s->overlap || !s->index)
578+ !s->frequency_band || !s->kernel || !s->cache[0] || !s->cache[1] || !s->index)
586579 return AVERROR(ENOMEM);
587580
588581 s->ifft_in->format = inlink->format;
@@ -834,7 +827,10 @@ static int activate(AVFilterContext *ctx) · 245 unmodified lines
834827 }
835828
836829 if (s->hop_index >= s->hop_size || s->ihop_index > 0) {
837− s->hop_index = 0;
830+ if (s->hop_index) {
831+ FFSWAP(AVFrame *, s->cache[0], s->cache[1]);
832+ s->hop_index = 0;
833+ }
838834
839835 for (int ch = 0; ch < s->nb_channels && s->ihop_index == 0; ch++) {
840836 ff_filter_execute(ctx, run_channel_cwt, (void *)&ch, NULL,

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