avfilter/af_sofalizer: switch to TX FFT from avutil

pull/360/head
Paul B Mahol 3 years ago
parent 6846d48fa6
commit f6aeb94d72
  1. 3
      configure
  2. 149
      libavfilter/af_sofalizer.c

3
configure vendored

@ -3672,8 +3672,7 @@ sinc_filter_deps="avcodec"
sinc_filter_select="rdft"
smartblur_filter_deps="gpl swscale"
sobel_opencl_filter_deps="opencl"
sofalizer_filter_deps="libmysofa avcodec"
sofalizer_filter_select="fft"
sofalizer_filter_deps="libmysofa"
spectrumsynth_filter_deps="avcodec"
spectrumsynth_filter_select="fft"
spp_filter_deps="gpl avcodec"

@ -28,7 +28,7 @@
#include <math.h>
#include <mysofa.h>
#include "libavcodec/avfft.h"
#include "libavutil/tx.h"
#include "libavutil/avstring.h"
#include "libavutil/channel_layout.h"
#include "libavutil/float_dsp.h"
@ -90,8 +90,9 @@ typedef struct SOFAlizerContext {
float *data_ir[2]; /* IRs for all channels to be convolved */
/* (this excludes the LFE) */
float *temp_src[2];
FFTComplex *temp_fft[2]; /* Array to hold FFT values */
FFTComplex *temp_afft[2]; /* Array to accumulate FFT values prior to IFFT */
AVComplexFloat *in_fft[2]; /* Array to hold input FFT values */
AVComplexFloat *out_fft[2]; /* Array to hold output FFT values */
AVComplexFloat *temp_afft[2]; /* Array to accumulate FFT values prior to IFFT */
/* control variables */
float gain; /* filter gain (in dB) */
@ -108,8 +109,9 @@ typedef struct SOFAlizerContext {
VirtualSpeaker vspkrpos[64];
FFTContext *fft[2], *ifft[2];
FFTComplex *data_hrtf[2];
AVTXContext *fft[2], *ifft[2];
av_tx_fn tx_fn[2], itx_fn[2];
AVComplexFloat *data_hrtf[2];
AVFloatDSPContext *fdsp;
} SOFAlizerContext;
@ -333,8 +335,9 @@ typedef struct ThreadData {
int *n_clippings;
float **ringbuffer;
float **temp_src;
FFTComplex **temp_fft;
FFTComplex **temp_afft;
AVComplexFloat **in_fft;
AVComplexFloat **out_fft;
AVComplexFloat **temp_afft;
} ThreadData;
static int sofalizer_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
@ -444,7 +447,7 @@ static int sofalizer_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr,
AVFrame *in = td->in, *out = td->out;
int offset = jobnr;
int *write = &td->write[jobnr];
FFTComplex *hrtf = s->data_hrtf[jobnr]; /* get pointers to current HRTF data */
AVComplexFloat *hrtf = s->data_hrtf[jobnr]; /* get pointers to current HRTF data */
int *n_clippings = &td->n_clippings[jobnr];
float *ringbuffer = td->ringbuffer[jobnr];
const int ir_samples = s->sofa.ir_samples; /* length of one IR */
@ -456,14 +459,17 @@ static int sofalizer_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr,
const int buffer_length = s->buffer_length;
/* -1 for AND instead of MODULO (applied to powers of 2): */
const uint32_t modulo = (uint32_t)buffer_length - 1;
FFTComplex *fft_in = s->temp_fft[jobnr]; /* temporary array for FFT input/output data */
FFTComplex *fft_acc = s->temp_afft[jobnr];
FFTContext *ifft = s->ifft[jobnr];
FFTContext *fft = s->fft[jobnr];
AVComplexFloat *fft_in = s->in_fft[jobnr]; /* temporary array for FFT input data */
AVComplexFloat *fft_out = s->out_fft[jobnr]; /* temporary array for FFT output data */
AVComplexFloat *fft_acc = s->temp_afft[jobnr];
AVTXContext *ifft = s->ifft[jobnr];
av_tx_fn itx_fn = s->itx_fn[jobnr];
AVTXContext *fft = s->fft[jobnr];
av_tx_fn tx_fn = s->tx_fn[jobnr];
const int n_conv = s->n_conv;
const int n_fft = s->n_fft;
const float fft_scale = 1.0f / s->n_fft;
FFTComplex *hrtf_offset;
AVComplexFloat *hrtf_offset;
int wr = *write;
int n_read;
int i, j;
@ -488,7 +494,7 @@ static int sofalizer_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr,
}
/* fill FFT accumulation with 0 */
memset(fft_acc, 0, sizeof(FFTComplex) * n_fft);
memset(fft_acc, 0, sizeof(AVComplexFloat) * n_fft);
for (i = 0; i < n_conv; i++) {
const float *src = (const float *)in->extended_data[i * planar]; /* get pointer to audio input buffer */
@ -513,7 +519,7 @@ static int sofalizer_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr,
hrtf_offset = hrtf + offset;
/* fill FFT input with 0 (we want to zero-pad) */
memset(fft_in, 0, sizeof(FFTComplex) * n_fft);
memset(fft_in, 0, sizeof(AVComplexFloat) * n_fft);
if (in->format == AV_SAMPLE_FMT_FLT) {
for (j = 0; j < in->nb_samples; j++) {
@ -530,12 +536,12 @@ static int sofalizer_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr,
}
/* transform input signal of current channel to frequency domain */
av_fft_permute(fft, fft_in);
av_fft_calc(fft, fft_in);
tx_fn(fft, fft_out, fft_in, sizeof(float));
for (j = 0; j < n_fft; j++) {
const FFTComplex *hcomplex = hrtf_offset + j;
const float re = fft_in[j].re;
const float im = fft_in[j].im;
const AVComplexFloat *hcomplex = hrtf_offset + j;
const float re = fft_out[j].re;
const float im = fft_out[j].im;
/* complex multiplication of input signal and HRTFs */
/* output channel (real): */
@ -546,19 +552,18 @@ static int sofalizer_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr,
}
/* transform output signal of current channel back to time domain */
av_fft_permute(ifft, fft_acc);
av_fft_calc(ifft, fft_acc);
itx_fn(ifft, fft_out, fft_acc, sizeof(float));
for (j = 0; j < in->nb_samples; j++) {
/* write output signal of current channel to output buffer */
dst[mult * j] += fft_acc[j].re * fft_scale;
dst[mult * j] += fft_out[j].re * fft_scale;
}
for (j = 0; j < ir_samples - 1; j++) { /* overflow length is IR length - 1 */
/* write the rest of output signal to overflow buffer */
int write_pos = (wr + j) & modulo;
*(ringbuffer + write_pos) += fft_acc[in->nb_samples + j].re * fft_scale;
*(ringbuffer + write_pos) += fft_out[in->nb_samples + j].re * fft_scale;
}
/* go through all samples of current output buffer: count clippings */
@ -594,7 +599,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
td.in = in; td.out = out; td.write = s->write;
td.delay = s->delay; td.ir = s->data_ir; td.n_clippings = n_clippings;
td.ringbuffer = s->ringbuffer; td.temp_src = s->temp_src;
td.temp_fft = s->temp_fft;
td.in_fft = s->in_fft;
td.out_fft = s->out_fft;
td.temp_afft = s->temp_afft;
if (s->type == TIME_DOMAIN) {
@ -736,10 +742,12 @@ static int load_data(AVFilterContext *ctx, int azim, int elev, float radius, int
float delay_r;
int nb_input_channels = ctx->inputs[0]->channels; /* no. input channels */
float gain_lin = expf((s->gain - 3 * nb_input_channels) / 20 * M_LN10); /* gain - 3dB/channel */
FFTComplex *data_hrtf_l = NULL;
FFTComplex *data_hrtf_r = NULL;
FFTComplex *fft_in_l = NULL;
FFTComplex *fft_in_r = NULL;
AVComplexFloat *data_hrtf_l = NULL;
AVComplexFloat *data_hrtf_r = NULL;
AVComplexFloat *fft_out_l = NULL;
AVComplexFloat *fft_out_r = NULL;
AVComplexFloat *fft_in_l = NULL;
AVComplexFloat *fft_in_r = NULL;
float *data_ir_l = NULL;
float *data_ir_r = NULL;
int offset = 0; /* used for faster pointer arithmetics in for-loop */
@ -842,20 +850,24 @@ static int load_data(AVFilterContext *ctx, int azim, int elev, float radius, int
s->n_fft = n_fft = 1 << (32 - ff_clz(n_max + s->framesize));
if (s->type == FREQUENCY_DOMAIN) {
av_fft_end(s->fft[0]);
av_fft_end(s->fft[1]);
s->fft[0] = av_fft_init(av_log2(s->n_fft), 0);
s->fft[1] = av_fft_init(av_log2(s->n_fft), 0);
av_fft_end(s->ifft[0]);
av_fft_end(s->ifft[1]);
s->ifft[0] = av_fft_init(av_log2(s->n_fft), 1);
s->ifft[1] = av_fft_init(av_log2(s->n_fft), 1);
if (!s->fft[0] || !s->fft[1] || !s->ifft[0] || !s->ifft[1]) {
av_log(ctx, AV_LOG_ERROR, "Unable to create FFT contexts of size %d.\n", s->n_fft);
ret = AVERROR(ENOMEM);
float scale;
av_tx_uninit(&s->fft[0]);
av_tx_uninit(&s->fft[1]);
ret = av_tx_init(&s->fft[0], &s->tx_fn[0], AV_TX_FLOAT_FFT, 0, s->n_fft, &scale, 0);
if (ret < 0)
goto fail;
ret = av_tx_init(&s->fft[1], &s->tx_fn[1], AV_TX_FLOAT_FFT, 0, s->n_fft, &scale, 0);
if (ret < 0)
goto fail;
av_tx_uninit(&s->ifft[0]);
av_tx_uninit(&s->ifft[1]);
ret = av_tx_init(&s->ifft[0], &s->itx_fn[0], AV_TX_FLOAT_FFT, 1, s->n_fft, &scale, 0);
if (ret < 0)
goto fail;
ret = av_tx_init(&s->ifft[1], &s->itx_fn[1], AV_TX_FLOAT_FFT, 1, s->n_fft, &scale, 0);
if (ret < 0)
goto fail;
}
}
if (s->type == TIME_DOMAIN) {
@ -872,11 +884,14 @@ static int load_data(AVFilterContext *ctx, int azim, int elev, float radius, int
s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float));
s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float));
s->temp_fft[0] = av_malloc_array(s->n_fft, sizeof(FFTComplex));
s->temp_fft[1] = av_malloc_array(s->n_fft, sizeof(FFTComplex));
s->temp_afft[0] = av_malloc_array(s->n_fft, sizeof(FFTComplex));
s->temp_afft[1] = av_malloc_array(s->n_fft, sizeof(FFTComplex));
if (!s->temp_fft[0] || !s->temp_fft[1] ||
s->in_fft[0] = av_malloc_array(s->n_fft, sizeof(AVComplexFloat));
s->in_fft[1] = av_malloc_array(s->n_fft, sizeof(AVComplexFloat));
s->out_fft[0] = av_malloc_array(s->n_fft, sizeof(AVComplexFloat));
s->out_fft[1] = av_malloc_array(s->n_fft, sizeof(AVComplexFloat));
s->temp_afft[0] = av_malloc_array(s->n_fft, sizeof(AVComplexFloat));
s->temp_afft[1] = av_malloc_array(s->n_fft, sizeof(AVComplexFloat));
if (!s->in_fft[0] || !s->in_fft[1] ||
!s->out_fft[0] || !s->out_fft[1] ||
!s->temp_afft[0] || !s->temp_afft[1]) {
ret = AVERROR(ENOMEM);
goto fail;
@ -889,9 +904,12 @@ static int load_data(AVFilterContext *ctx, int azim, int elev, float radius, int
}
if (s->type == FREQUENCY_DOMAIN) {
fft_out_l = av_calloc(n_fft, sizeof(*fft_out_l));
fft_out_r = av_calloc(n_fft, sizeof(*fft_out_r));
fft_in_l = av_calloc(n_fft, sizeof(*fft_in_l));
fft_in_r = av_calloc(n_fft, sizeof(*fft_in_r));
if (!fft_in_l || !fft_in_r) {
if (!fft_in_l || !fft_in_r ||
!fft_out_l || !fft_out_r) {
ret = AVERROR(ENOMEM);
goto fail;
}
@ -927,27 +945,25 @@ static int load_data(AVFilterContext *ctx, int azim, int elev, float radius, int
}
/* actually transform to frequency domain (IRs -> HRTFs) */
av_fft_permute(s->fft[0], fft_in_l);
av_fft_calc(s->fft[0], fft_in_l);
memcpy(data_hrtf_l + offset, fft_in_l, n_fft * sizeof(*fft_in_l));
av_fft_permute(s->fft[0], fft_in_r);
av_fft_calc(s->fft[0], fft_in_r);
memcpy(data_hrtf_r + offset, fft_in_r, n_fft * sizeof(*fft_in_r));
s->tx_fn[0](s->fft[0], fft_out_l, fft_in_l, sizeof(float));
memcpy(data_hrtf_l + offset, fft_out_l, n_fft * sizeof(*fft_out_l));
s->tx_fn[1](s->fft[1], fft_out_r, fft_in_r, sizeof(float));
memcpy(data_hrtf_r + offset, fft_out_r, n_fft * sizeof(*fft_out_r));
}
}
if (s->type == FREQUENCY_DOMAIN) {
s->data_hrtf[0] = av_malloc_array(n_fft * s->n_conv, sizeof(FFTComplex));
s->data_hrtf[1] = av_malloc_array(n_fft * s->n_conv, sizeof(FFTComplex));
s->data_hrtf[0] = av_malloc_array(n_fft * s->n_conv, sizeof(AVComplexFloat));
s->data_hrtf[1] = av_malloc_array(n_fft * s->n_conv, sizeof(AVComplexFloat));
if (!s->data_hrtf[0] || !s->data_hrtf[1]) {
ret = AVERROR(ENOMEM);
goto fail;
}
memcpy(s->data_hrtf[0], data_hrtf_l, /* copy HRTF data to */
sizeof(FFTComplex) * n_conv * n_fft); /* filter struct */
sizeof(AVComplexFloat) * n_conv * n_fft); /* filter struct */
memcpy(s->data_hrtf[1], data_hrtf_r,
sizeof(FFTComplex) * n_conv * n_fft);
sizeof(AVComplexFloat) * n_conv * n_fft);
}
fail:
@ -957,6 +973,9 @@ fail:
av_freep(&data_ir_l); /* free temprary IR memory */
av_freep(&data_ir_r);
av_freep(&fft_out_l); /* free temporary FFT memory */
av_freep(&fft_out_r);
av_freep(&fft_in_l); /* free temporary FFT memory */
av_freep(&fft_in_r);
@ -1023,10 +1042,10 @@ static av_cold void uninit(AVFilterContext *ctx)
SOFAlizerContext *s = ctx->priv;
close_sofa(&s->sofa);
av_fft_end(s->ifft[0]);
av_fft_end(s->ifft[1]);
av_fft_end(s->fft[0]);
av_fft_end(s->fft[1]);
av_tx_uninit(&s->ifft[0]);
av_tx_uninit(&s->ifft[1]);
av_tx_uninit(&s->fft[0]);
av_tx_uninit(&s->fft[1]);
s->ifft[0] = NULL;
s->ifft[1] = NULL;
s->fft[0] = NULL;
@ -1043,8 +1062,10 @@ static av_cold void uninit(AVFilterContext *ctx)
av_freep(&s->temp_src[1]);
av_freep(&s->temp_afft[0]);
av_freep(&s->temp_afft[1]);
av_freep(&s->temp_fft[0]);
av_freep(&s->temp_fft[1]);
av_freep(&s->in_fft[0]);
av_freep(&s->in_fft[1]);
av_freep(&s->out_fft[0]);
av_freep(&s->out_fft[1]);
av_freep(&s->data_hrtf[0]);
av_freep(&s->data_hrtf[1]);
av_freep(&s->fdsp);

Loading…
Cancel
Save