You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1108 lines
40KB

  1. /*****************************************************************************
  2. * sofalizer.c : SOFAlizer filter for virtual binaural acoustics
  3. *****************************************************************************
  4. * Copyright (C) 2013-2015 Andreas Fuchs, Wolfgang Hrauda,
  5. * Acoustics Research Institute (ARI), Vienna, Austria
  6. *
  7. * Authors: Andreas Fuchs <andi.fuchs.mail@gmail.com>
  8. * Wolfgang Hrauda <wolfgang.hrauda@gmx.at>
  9. *
  10. * SOFAlizer project coordinator at ARI, main developer of SOFA:
  11. * Piotr Majdak <piotr@majdak.at>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU Lesser General Public License as published by
  15. * the Free Software Foundation; either version 2.1 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public License
  24. * along with this program; if not, write to the Free Software Foundation,
  25. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  26. *****************************************************************************/
  27. #include <math.h>
  28. #include <mysofa.h>
  29. #include "libavcodec/avfft.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/channel_layout.h"
  32. #include "libavutil/float_dsp.h"
  33. #include "libavutil/intmath.h"
  34. #include "libavutil/opt.h"
  35. #include "avfilter.h"
  36. #include "filters.h"
  37. #include "internal.h"
  38. #include "audio.h"
  39. #define TIME_DOMAIN 0
  40. #define FREQUENCY_DOMAIN 1
  41. typedef struct MySofa { /* contains data of one SOFA file */
  42. struct MYSOFA_HRTF *hrtf;
  43. struct MYSOFA_LOOKUP *lookup;
  44. struct MYSOFA_NEIGHBORHOOD *neighborhood;
  45. int ir_samples; /* length of one impulse response (IR) */
  46. int n_samples; /* ir_samples to next power of 2 */
  47. float *lir, *rir; /* IRs (time-domain) */
  48. float *fir;
  49. int max_delay;
  50. } MySofa;
  51. typedef struct VirtualSpeaker {
  52. uint8_t set;
  53. float azim;
  54. float elev;
  55. } VirtualSpeaker;
  56. typedef struct SOFAlizerContext {
  57. const AVClass *class;
  58. char *filename; /* name of SOFA file */
  59. MySofa sofa; /* contains data of the SOFA file */
  60. int sample_rate; /* sample rate from SOFA file */
  61. float *speaker_azim; /* azimuth of the virtual loudspeakers */
  62. float *speaker_elev; /* elevation of the virtual loudspeakers */
  63. char *speakers_pos; /* custom positions of the virtual loudspeakers */
  64. float lfe_gain; /* initial gain for the LFE channel */
  65. float gain_lfe; /* gain applied to LFE channel */
  66. int lfe_channel; /* LFE channel position in channel layout */
  67. int n_conv; /* number of channels to convolute */
  68. /* buffer variables (for convolution) */
  69. float *ringbuffer[2]; /* buffers input samples, length of one buffer: */
  70. /* no. input ch. (incl. LFE) x buffer_length */
  71. int write[2]; /* current write position to ringbuffer */
  72. int buffer_length; /* is: longest IR plus max. delay in all SOFA files */
  73. /* then choose next power of 2 */
  74. int n_fft; /* number of samples in one FFT block */
  75. int nb_samples;
  76. /* netCDF variables */
  77. int *delay[2]; /* broadband delay for each channel/IR to be convolved */
  78. float *data_ir[2]; /* IRs for all channels to be convolved */
  79. /* (this excludes the LFE) */
  80. float *temp_src[2];
  81. FFTComplex *temp_fft[2]; /* Array to hold FFT values */
  82. FFTComplex *temp_afft[2]; /* Array to accumulate FFT values prior to IFFT */
  83. /* control variables */
  84. float gain; /* filter gain (in dB) */
  85. float rotation; /* rotation of virtual loudspeakers (in degrees) */
  86. float elevation; /* elevation of virtual loudspeakers (in deg.) */
  87. float radius; /* distance virtual loudspeakers to listener (in metres) */
  88. int type; /* processing type */
  89. int framesize; /* size of buffer */
  90. int normalize; /* should all IRs be normalized upon import ? */
  91. int interpolate; /* should wanted IRs be interpolated from neighbors ? */
  92. int minphase; /* should all IRs be minphased upon import ? */
  93. float anglestep; /* neighbor search angle step, in agles */
  94. float radstep; /* neighbor search radius step, in meters */
  95. VirtualSpeaker vspkrpos[64];
  96. FFTContext *fft[2], *ifft[2];
  97. FFTComplex *data_hrtf[2];
  98. AVFloatDSPContext *fdsp;
  99. } SOFAlizerContext;
  100. static int close_sofa(struct MySofa *sofa)
  101. {
  102. if (sofa->neighborhood)
  103. mysofa_neighborhood_free(sofa->neighborhood);
  104. sofa->neighborhood = NULL;
  105. if (sofa->lookup)
  106. mysofa_lookup_free(sofa->lookup);
  107. sofa->lookup = NULL;
  108. if (sofa->hrtf)
  109. mysofa_free(sofa->hrtf);
  110. sofa->hrtf = NULL;
  111. av_freep(&sofa->fir);
  112. return 0;
  113. }
  114. static int preload_sofa(AVFilterContext *ctx, char *filename, int *samplingrate)
  115. {
  116. struct SOFAlizerContext *s = ctx->priv;
  117. struct MYSOFA_HRTF *mysofa;
  118. char *license;
  119. int ret;
  120. mysofa = mysofa_load(filename, &ret);
  121. s->sofa.hrtf = mysofa;
  122. if (ret || !mysofa) {
  123. av_log(ctx, AV_LOG_ERROR, "Can't find SOFA-file '%s'\n", filename);
  124. return AVERROR(EINVAL);
  125. }
  126. ret = mysofa_check(mysofa);
  127. if (ret != MYSOFA_OK) {
  128. av_log(ctx, AV_LOG_ERROR, "Selected SOFA file is invalid. Please select valid SOFA file.\n");
  129. return ret;
  130. }
  131. if (s->normalize)
  132. mysofa_loudness(s->sofa.hrtf);
  133. if (s->minphase)
  134. mysofa_minphase(s->sofa.hrtf, 0.01f);
  135. mysofa_tocartesian(s->sofa.hrtf);
  136. s->sofa.lookup = mysofa_lookup_init(s->sofa.hrtf);
  137. if (s->sofa.lookup == NULL)
  138. return AVERROR(EINVAL);
  139. if (s->interpolate)
  140. s->sofa.neighborhood = mysofa_neighborhood_init_withstepdefine(s->sofa.hrtf,
  141. s->sofa.lookup,
  142. s->anglestep,
  143. s->radstep);
  144. s->sofa.fir = av_calloc(s->sofa.hrtf->N * s->sofa.hrtf->R, sizeof(*s->sofa.fir));
  145. if (!s->sofa.fir)
  146. return AVERROR(ENOMEM);
  147. if (mysofa->DataSamplingRate.elements != 1)
  148. return AVERROR(EINVAL);
  149. av_log(ctx, AV_LOG_DEBUG, "Original IR length: %d.\n", mysofa->N);
  150. *samplingrate = mysofa->DataSamplingRate.values[0];
  151. license = mysofa_getAttribute(mysofa->attributes, (char *)"License");
  152. if (license)
  153. av_log(ctx, AV_LOG_INFO, "SOFA license: %s\n", license);
  154. return 0;
  155. }
  156. static int parse_channel_name(AVFilterContext *ctx, char **arg, int *rchannel)
  157. {
  158. int len, i, channel_id = 0;
  159. int64_t layout, layout0;
  160. char buf[8] = {0};
  161. /* try to parse a channel name, e.g. "FL" */
  162. if (av_sscanf(*arg, "%7[A-Z]%n", buf, &len)) {
  163. layout0 = layout = av_get_channel_layout(buf);
  164. /* channel_id <- first set bit in layout */
  165. for (i = 32; i > 0; i >>= 1) {
  166. if (layout >= 1LL << i) {
  167. channel_id += i;
  168. layout >>= i;
  169. }
  170. }
  171. /* reject layouts that are not a single channel */
  172. if (channel_id >= 64 || layout0 != 1LL << channel_id) {
  173. av_log(ctx, AV_LOG_WARNING, "Failed to parse \'%s\' as channel name.\n", buf);
  174. return AVERROR(EINVAL);
  175. }
  176. *rchannel = channel_id;
  177. *arg += len;
  178. return 0;
  179. } else if (av_sscanf(*arg, "%d%n", &channel_id, &len) == 1) {
  180. if (channel_id < 0 || channel_id >= 64) {
  181. av_log(ctx, AV_LOG_WARNING, "Failed to parse \'%d\' as channel number.\n", channel_id);
  182. return AVERROR(EINVAL);
  183. }
  184. *rchannel = channel_id;
  185. *arg += len;
  186. return 0;
  187. }
  188. return AVERROR(EINVAL);
  189. }
  190. static void parse_speaker_pos(AVFilterContext *ctx, int64_t in_channel_layout)
  191. {
  192. SOFAlizerContext *s = ctx->priv;
  193. char *arg, *tokenizer, *p, *args = av_strdup(s->speakers_pos);
  194. if (!args)
  195. return;
  196. p = args;
  197. while ((arg = av_strtok(p, "|", &tokenizer))) {
  198. float azim, elev;
  199. int out_ch_id;
  200. p = NULL;
  201. if (parse_channel_name(ctx, &arg, &out_ch_id)) {
  202. continue;
  203. }
  204. if (av_sscanf(arg, "%f %f", &azim, &elev) == 2) {
  205. s->vspkrpos[out_ch_id].set = 1;
  206. s->vspkrpos[out_ch_id].azim = azim;
  207. s->vspkrpos[out_ch_id].elev = elev;
  208. } else if (av_sscanf(arg, "%f", &azim) == 1) {
  209. s->vspkrpos[out_ch_id].set = 1;
  210. s->vspkrpos[out_ch_id].azim = azim;
  211. s->vspkrpos[out_ch_id].elev = 0;
  212. }
  213. }
  214. av_free(args);
  215. }
  216. static int get_speaker_pos(AVFilterContext *ctx,
  217. float *speaker_azim, float *speaker_elev)
  218. {
  219. struct SOFAlizerContext *s = ctx->priv;
  220. uint64_t channels_layout = ctx->inputs[0]->channel_layout;
  221. float azim[64] = { 0 };
  222. float elev[64] = { 0 };
  223. int m, ch, n_conv = ctx->inputs[0]->channels; /* get no. input channels */
  224. if (n_conv < 0 || n_conv > 64)
  225. return AVERROR(EINVAL);
  226. s->lfe_channel = -1;
  227. if (s->speakers_pos)
  228. parse_speaker_pos(ctx, channels_layout);
  229. /* set speaker positions according to input channel configuration: */
  230. for (m = 0, ch = 0; ch < n_conv && m < 64; m++) {
  231. uint64_t mask = channels_layout & (1ULL << m);
  232. switch (mask) {
  233. case AV_CH_FRONT_LEFT: azim[ch] = 30; break;
  234. case AV_CH_FRONT_RIGHT: azim[ch] = 330; break;
  235. case AV_CH_FRONT_CENTER: azim[ch] = 0; break;
  236. case AV_CH_LOW_FREQUENCY:
  237. case AV_CH_LOW_FREQUENCY_2: s->lfe_channel = ch; break;
  238. case AV_CH_BACK_LEFT: azim[ch] = 150; break;
  239. case AV_CH_BACK_RIGHT: azim[ch] = 210; break;
  240. case AV_CH_BACK_CENTER: azim[ch] = 180; break;
  241. case AV_CH_SIDE_LEFT: azim[ch] = 90; break;
  242. case AV_CH_SIDE_RIGHT: azim[ch] = 270; break;
  243. case AV_CH_FRONT_LEFT_OF_CENTER: azim[ch] = 15; break;
  244. case AV_CH_FRONT_RIGHT_OF_CENTER: azim[ch] = 345; break;
  245. case AV_CH_TOP_CENTER: azim[ch] = 0;
  246. elev[ch] = 90; break;
  247. case AV_CH_TOP_FRONT_LEFT: azim[ch] = 30;
  248. elev[ch] = 45; break;
  249. case AV_CH_TOP_FRONT_CENTER: azim[ch] = 0;
  250. elev[ch] = 45; break;
  251. case AV_CH_TOP_FRONT_RIGHT: azim[ch] = 330;
  252. elev[ch] = 45; break;
  253. case AV_CH_TOP_BACK_LEFT: azim[ch] = 150;
  254. elev[ch] = 45; break;
  255. case AV_CH_TOP_BACK_RIGHT: azim[ch] = 210;
  256. elev[ch] = 45; break;
  257. case AV_CH_TOP_BACK_CENTER: azim[ch] = 180;
  258. elev[ch] = 45; break;
  259. case AV_CH_WIDE_LEFT: azim[ch] = 90; break;
  260. case AV_CH_WIDE_RIGHT: azim[ch] = 270; break;
  261. case AV_CH_SURROUND_DIRECT_LEFT: azim[ch] = 90; break;
  262. case AV_CH_SURROUND_DIRECT_RIGHT: azim[ch] = 270; break;
  263. case AV_CH_STEREO_LEFT: azim[ch] = 90; break;
  264. case AV_CH_STEREO_RIGHT: azim[ch] = 270; break;
  265. case 0: break;
  266. default:
  267. return AVERROR(EINVAL);
  268. }
  269. if (s->vspkrpos[m].set) {
  270. azim[ch] = s->vspkrpos[m].azim;
  271. elev[ch] = s->vspkrpos[m].elev;
  272. }
  273. if (mask)
  274. ch++;
  275. }
  276. memcpy(speaker_azim, azim, n_conv * sizeof(float));
  277. memcpy(speaker_elev, elev, n_conv * sizeof(float));
  278. return 0;
  279. }
  280. typedef struct ThreadData {
  281. AVFrame *in, *out;
  282. int *write;
  283. int **delay;
  284. float **ir;
  285. int *n_clippings;
  286. float **ringbuffer;
  287. float **temp_src;
  288. FFTComplex **temp_fft;
  289. FFTComplex **temp_afft;
  290. } ThreadData;
  291. static int sofalizer_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  292. {
  293. SOFAlizerContext *s = ctx->priv;
  294. ThreadData *td = arg;
  295. AVFrame *in = td->in, *out = td->out;
  296. int offset = jobnr;
  297. int *write = &td->write[jobnr];
  298. const int *const delay = td->delay[jobnr];
  299. const float *const ir = td->ir[jobnr];
  300. int *n_clippings = &td->n_clippings[jobnr];
  301. float *ringbuffer = td->ringbuffer[jobnr];
  302. float *temp_src = td->temp_src[jobnr];
  303. const int ir_samples = s->sofa.ir_samples; /* length of one IR */
  304. const int n_samples = s->sofa.n_samples;
  305. const int planar = in->format == AV_SAMPLE_FMT_FLTP;
  306. const int mult = 1 + !planar;
  307. const float *src = (const float *)in->extended_data[0]; /* get pointer to audio input buffer */
  308. float *dst = (float *)out->extended_data[jobnr * planar]; /* get pointer to audio output buffer */
  309. const int in_channels = s->n_conv; /* number of input channels */
  310. /* ring buffer length is: longest IR plus max. delay -> next power of 2 */
  311. const int buffer_length = s->buffer_length;
  312. /* -1 for AND instead of MODULO (applied to powers of 2): */
  313. const uint32_t modulo = (uint32_t)buffer_length - 1;
  314. float *buffer[64]; /* holds ringbuffer for each input channel */
  315. int wr = *write;
  316. int read;
  317. int i, l;
  318. if (!planar)
  319. dst += offset;
  320. for (l = 0; l < in_channels; l++) {
  321. /* get starting address of ringbuffer for each input channel */
  322. buffer[l] = ringbuffer + l * buffer_length;
  323. }
  324. for (i = 0; i < in->nb_samples; i++) {
  325. const float *temp_ir = ir; /* using same set of IRs for each sample */
  326. dst[0] = 0;
  327. if (planar) {
  328. for (l = 0; l < in_channels; l++) {
  329. const float *srcp = (const float *)in->extended_data[l];
  330. /* write current input sample to ringbuffer (for each channel) */
  331. buffer[l][wr] = srcp[i];
  332. }
  333. } else {
  334. for (l = 0; l < in_channels; l++) {
  335. /* write current input sample to ringbuffer (for each channel) */
  336. buffer[l][wr] = src[l];
  337. }
  338. }
  339. /* loop goes through all channels to be convolved */
  340. for (l = 0; l < in_channels; l++) {
  341. const float *const bptr = buffer[l];
  342. if (l == s->lfe_channel) {
  343. /* LFE is an input channel but requires no convolution */
  344. /* apply gain to LFE signal and add to output buffer */
  345. dst[0] += *(buffer[s->lfe_channel] + wr) * s->gain_lfe;
  346. temp_ir += n_samples;
  347. continue;
  348. }
  349. /* current read position in ringbuffer: input sample write position
  350. * - delay for l-th ch. + diff. betw. IR length and buffer length
  351. * (mod buffer length) */
  352. read = (wr - delay[l] - (ir_samples - 1) + buffer_length) & modulo;
  353. if (read + ir_samples < buffer_length) {
  354. memmove(temp_src, bptr + read, ir_samples * sizeof(*temp_src));
  355. } else {
  356. int len = FFMIN(n_samples - (read % ir_samples), buffer_length - read);
  357. memmove(temp_src, bptr + read, len * sizeof(*temp_src));
  358. memmove(temp_src + len, bptr, (n_samples - len) * sizeof(*temp_src));
  359. }
  360. /* multiply signal and IR, and add up the results */
  361. dst[0] += s->fdsp->scalarproduct_float(temp_ir, temp_src, FFALIGN(ir_samples, 32));
  362. temp_ir += n_samples;
  363. }
  364. /* clippings counter */
  365. if (fabsf(dst[0]) > 1)
  366. n_clippings[0]++;
  367. /* move output buffer pointer by +2 to get to next sample of processed channel: */
  368. dst += mult;
  369. src += in_channels;
  370. wr = (wr + 1) & modulo; /* update ringbuffer write position */
  371. }
  372. *write = wr; /* remember write position in ringbuffer for next call */
  373. return 0;
  374. }
  375. static int sofalizer_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  376. {
  377. SOFAlizerContext *s = ctx->priv;
  378. ThreadData *td = arg;
  379. AVFrame *in = td->in, *out = td->out;
  380. int offset = jobnr;
  381. int *write = &td->write[jobnr];
  382. FFTComplex *hrtf = s->data_hrtf[jobnr]; /* get pointers to current HRTF data */
  383. int *n_clippings = &td->n_clippings[jobnr];
  384. float *ringbuffer = td->ringbuffer[jobnr];
  385. const int ir_samples = s->sofa.ir_samples; /* length of one IR */
  386. const int planar = in->format == AV_SAMPLE_FMT_FLTP;
  387. const int mult = 1 + !planar;
  388. float *dst = (float *)out->extended_data[jobnr * planar]; /* get pointer to audio output buffer */
  389. const int in_channels = s->n_conv; /* number of input channels */
  390. /* ring buffer length is: longest IR plus max. delay -> next power of 2 */
  391. const int buffer_length = s->buffer_length;
  392. /* -1 for AND instead of MODULO (applied to powers of 2): */
  393. const uint32_t modulo = (uint32_t)buffer_length - 1;
  394. FFTComplex *fft_in = s->temp_fft[jobnr]; /* temporary array for FFT input/output data */
  395. FFTComplex *fft_acc = s->temp_afft[jobnr];
  396. FFTContext *ifft = s->ifft[jobnr];
  397. FFTContext *fft = s->fft[jobnr];
  398. const int n_conv = s->n_conv;
  399. const int n_fft = s->n_fft;
  400. const float fft_scale = 1.0f / s->n_fft;
  401. FFTComplex *hrtf_offset;
  402. int wr = *write;
  403. int n_read;
  404. int i, j;
  405. if (!planar)
  406. dst += offset;
  407. /* find minimum between number of samples and output buffer length:
  408. * (important, if one IR is longer than the output buffer) */
  409. n_read = FFMIN(ir_samples, in->nb_samples);
  410. for (j = 0; j < n_read; j++) {
  411. /* initialize output buf with saved signal from overflow buf */
  412. dst[mult * j] = ringbuffer[wr];
  413. ringbuffer[wr] = 0.0f; /* re-set read samples to zero */
  414. /* update ringbuffer read/write position */
  415. wr = (wr + 1) & modulo;
  416. }
  417. /* initialize rest of output buffer with 0 */
  418. for (j = n_read; j < in->nb_samples; j++) {
  419. dst[mult * j] = 0;
  420. }
  421. /* fill FFT accumulation with 0 */
  422. memset(fft_acc, 0, sizeof(FFTComplex) * n_fft);
  423. for (i = 0; i < n_conv; i++) {
  424. const float *src = (const float *)in->extended_data[i * planar]; /* get pointer to audio input buffer */
  425. if (i == s->lfe_channel) { /* LFE */
  426. if (in->format == AV_SAMPLE_FMT_FLT) {
  427. for (j = 0; j < in->nb_samples; j++) {
  428. /* apply gain to LFE signal and add to output buffer */
  429. dst[2 * j] += src[i + j * in_channels] * s->gain_lfe;
  430. }
  431. } else {
  432. for (j = 0; j < in->nb_samples; j++) {
  433. /* apply gain to LFE signal and add to output buffer */
  434. dst[j] += src[j] * s->gain_lfe;
  435. }
  436. }
  437. continue;
  438. }
  439. /* outer loop: go through all input channels to be convolved */
  440. offset = i * n_fft; /* no. samples already processed */
  441. hrtf_offset = hrtf + offset;
  442. /* fill FFT input with 0 (we want to zero-pad) */
  443. memset(fft_in, 0, sizeof(FFTComplex) * n_fft);
  444. if (in->format == AV_SAMPLE_FMT_FLT) {
  445. for (j = 0; j < in->nb_samples; j++) {
  446. /* prepare input for FFT */
  447. /* write all samples of current input channel to FFT input array */
  448. fft_in[j].re = src[j * in_channels + i];
  449. }
  450. } else {
  451. for (j = 0; j < in->nb_samples; j++) {
  452. /* prepare input for FFT */
  453. /* write all samples of current input channel to FFT input array */
  454. fft_in[j].re = src[j];
  455. }
  456. }
  457. /* transform input signal of current channel to frequency domain */
  458. av_fft_permute(fft, fft_in);
  459. av_fft_calc(fft, fft_in);
  460. for (j = 0; j < n_fft; j++) {
  461. const FFTComplex *hcomplex = hrtf_offset + j;
  462. const float re = fft_in[j].re;
  463. const float im = fft_in[j].im;
  464. /* complex multiplication of input signal and HRTFs */
  465. /* output channel (real): */
  466. fft_acc[j].re += re * hcomplex->re - im * hcomplex->im;
  467. /* output channel (imag): */
  468. fft_acc[j].im += re * hcomplex->im + im * hcomplex->re;
  469. }
  470. }
  471. /* transform output signal of current channel back to time domain */
  472. av_fft_permute(ifft, fft_acc);
  473. av_fft_calc(ifft, fft_acc);
  474. for (j = 0; j < in->nb_samples; j++) {
  475. /* write output signal of current channel to output buffer */
  476. dst[mult * j] += fft_acc[j].re * fft_scale;
  477. }
  478. for (j = 0; j < ir_samples - 1; j++) { /* overflow length is IR length - 1 */
  479. /* write the rest of output signal to overflow buffer */
  480. int write_pos = (wr + j) & modulo;
  481. *(ringbuffer + write_pos) += fft_acc[in->nb_samples + j].re * fft_scale;
  482. }
  483. /* go through all samples of current output buffer: count clippings */
  484. for (i = 0; i < out->nb_samples; i++) {
  485. /* clippings counter */
  486. if (fabsf(dst[i * mult]) > 1) { /* if current output sample > 1 */
  487. n_clippings[0]++;
  488. }
  489. }
  490. /* remember read/write position in ringbuffer for next call */
  491. *write = wr;
  492. return 0;
  493. }
  494. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  495. {
  496. AVFilterContext *ctx = inlink->dst;
  497. SOFAlizerContext *s = ctx->priv;
  498. AVFilterLink *outlink = ctx->outputs[0];
  499. int n_clippings[2] = { 0 };
  500. ThreadData td;
  501. AVFrame *out;
  502. out = ff_get_audio_buffer(outlink, in->nb_samples);
  503. if (!out) {
  504. av_frame_free(&in);
  505. return AVERROR(ENOMEM);
  506. }
  507. av_frame_copy_props(out, in);
  508. td.in = in; td.out = out; td.write = s->write;
  509. td.delay = s->delay; td.ir = s->data_ir; td.n_clippings = n_clippings;
  510. td.ringbuffer = s->ringbuffer; td.temp_src = s->temp_src;
  511. td.temp_fft = s->temp_fft;
  512. td.temp_afft = s->temp_afft;
  513. if (s->type == TIME_DOMAIN) {
  514. ctx->internal->execute(ctx, sofalizer_convolute, &td, NULL, 2);
  515. } else if (s->type == FREQUENCY_DOMAIN) {
  516. ctx->internal->execute(ctx, sofalizer_fast_convolute, &td, NULL, 2);
  517. }
  518. emms_c();
  519. /* display error message if clipping occurred */
  520. if (n_clippings[0] + n_clippings[1] > 0) {
  521. av_log(ctx, AV_LOG_WARNING, "%d of %d samples clipped. Please reduce gain.\n",
  522. n_clippings[0] + n_clippings[1], out->nb_samples * 2);
  523. }
  524. av_frame_free(&in);
  525. return ff_filter_frame(outlink, out);
  526. }
  527. static int activate(AVFilterContext *ctx)
  528. {
  529. AVFilterLink *inlink = ctx->inputs[0];
  530. AVFilterLink *outlink = ctx->outputs[0];
  531. SOFAlizerContext *s = ctx->priv;
  532. AVFrame *in;
  533. int ret;
  534. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  535. if (s->nb_samples)
  536. ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
  537. else
  538. ret = ff_inlink_consume_frame(inlink, &in);
  539. if (ret < 0)
  540. return ret;
  541. if (ret > 0)
  542. return filter_frame(inlink, in);
  543. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  544. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  545. return FFERROR_NOT_READY;
  546. }
  547. static int query_formats(AVFilterContext *ctx)
  548. {
  549. struct SOFAlizerContext *s = ctx->priv;
  550. AVFilterFormats *formats = NULL;
  551. AVFilterChannelLayouts *layouts = NULL;
  552. int ret, sample_rates[] = { 48000, -1 };
  553. static const enum AVSampleFormat sample_fmts[] = {
  554. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
  555. AV_SAMPLE_FMT_NONE
  556. };
  557. formats = ff_make_format_list(sample_fmts);
  558. if (!formats)
  559. return AVERROR(ENOMEM);
  560. ret = ff_set_common_formats(ctx, formats);
  561. if (ret)
  562. return ret;
  563. layouts = ff_all_channel_layouts();
  564. if (!layouts)
  565. return AVERROR(ENOMEM);
  566. ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->outcfg.channel_layouts);
  567. if (ret)
  568. return ret;
  569. layouts = NULL;
  570. ret = ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO);
  571. if (ret)
  572. return ret;
  573. ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->incfg.channel_layouts);
  574. if (ret)
  575. return ret;
  576. sample_rates[0] = s->sample_rate;
  577. formats = ff_make_format_list(sample_rates);
  578. if (!formats)
  579. return AVERROR(ENOMEM);
  580. return ff_set_common_samplerates(ctx, formats);
  581. }
  582. static int getfilter_float(AVFilterContext *ctx, float x, float y, float z,
  583. float *left, float *right,
  584. float *delay_left, float *delay_right)
  585. {
  586. struct SOFAlizerContext *s = ctx->priv;
  587. float c[3], delays[2];
  588. float *fl, *fr;
  589. int nearest;
  590. int *neighbors;
  591. float *res;
  592. c[0] = x, c[1] = y, c[2] = z;
  593. nearest = mysofa_lookup(s->sofa.lookup, c);
  594. if (nearest < 0)
  595. return AVERROR(EINVAL);
  596. if (s->interpolate) {
  597. neighbors = mysofa_neighborhood(s->sofa.neighborhood, nearest);
  598. res = mysofa_interpolate(s->sofa.hrtf, c,
  599. nearest, neighbors,
  600. s->sofa.fir, delays);
  601. } else {
  602. if (s->sofa.hrtf->DataDelay.elements > s->sofa.hrtf->R) {
  603. delays[0] = s->sofa.hrtf->DataDelay.values[nearest * s->sofa.hrtf->R];
  604. delays[1] = s->sofa.hrtf->DataDelay.values[nearest * s->sofa.hrtf->R + 1];
  605. } else {
  606. delays[0] = s->sofa.hrtf->DataDelay.values[0];
  607. delays[1] = s->sofa.hrtf->DataDelay.values[1];
  608. }
  609. res = s->sofa.hrtf->DataIR.values + nearest * s->sofa.hrtf->N * s->sofa.hrtf->R;
  610. }
  611. *delay_left = delays[0];
  612. *delay_right = delays[1];
  613. fl = res;
  614. fr = res + s->sofa.hrtf->N;
  615. memcpy(left, fl, sizeof(float) * s->sofa.hrtf->N);
  616. memcpy(right, fr, sizeof(float) * s->sofa.hrtf->N);
  617. return 0;
  618. }
  619. static int load_data(AVFilterContext *ctx, int azim, int elev, float radius, int sample_rate)
  620. {
  621. struct SOFAlizerContext *s = ctx->priv;
  622. int n_samples;
  623. int ir_samples;
  624. int n_conv = s->n_conv; /* no. channels to convolve */
  625. int n_fft;
  626. float delay_l; /* broadband delay for each IR */
  627. float delay_r;
  628. int nb_input_channels = ctx->inputs[0]->channels; /* no. input channels */
  629. float gain_lin = expf((s->gain - 3 * nb_input_channels) / 20 * M_LN10); /* gain - 3dB/channel */
  630. FFTComplex *data_hrtf_l = NULL;
  631. FFTComplex *data_hrtf_r = NULL;
  632. FFTComplex *fft_in_l = NULL;
  633. FFTComplex *fft_in_r = NULL;
  634. float *data_ir_l = NULL;
  635. float *data_ir_r = NULL;
  636. int offset = 0; /* used for faster pointer arithmetics in for-loop */
  637. int i, j, azim_orig = azim, elev_orig = elev;
  638. int ret = 0;
  639. int n_current;
  640. int n_max = 0;
  641. av_log(ctx, AV_LOG_DEBUG, "IR length: %d.\n", s->sofa.hrtf->N);
  642. s->sofa.ir_samples = s->sofa.hrtf->N;
  643. s->sofa.n_samples = 1 << (32 - ff_clz(s->sofa.ir_samples));
  644. n_samples = s->sofa.n_samples;
  645. ir_samples = s->sofa.ir_samples;
  646. if (s->type == TIME_DOMAIN) {
  647. s->data_ir[0] = av_calloc(n_samples, sizeof(float) * s->n_conv);
  648. s->data_ir[1] = av_calloc(n_samples, sizeof(float) * s->n_conv);
  649. if (!s->data_ir[0] || !s->data_ir[1]) {
  650. ret = AVERROR(ENOMEM);
  651. goto fail;
  652. }
  653. }
  654. s->delay[0] = av_calloc(s->n_conv, sizeof(int));
  655. s->delay[1] = av_calloc(s->n_conv, sizeof(int));
  656. if (!s->delay[0] || !s->delay[1]) {
  657. ret = AVERROR(ENOMEM);
  658. goto fail;
  659. }
  660. /* get temporary IR for L and R channel */
  661. data_ir_l = av_calloc(n_conv * n_samples, sizeof(*data_ir_l));
  662. data_ir_r = av_calloc(n_conv * n_samples, sizeof(*data_ir_r));
  663. if (!data_ir_r || !data_ir_l) {
  664. ret = AVERROR(ENOMEM);
  665. goto fail;
  666. }
  667. if (s->type == TIME_DOMAIN) {
  668. s->temp_src[0] = av_calloc(n_samples, sizeof(float));
  669. s->temp_src[1] = av_calloc(n_samples, sizeof(float));
  670. if (!s->temp_src[0] || !s->temp_src[1]) {
  671. ret = AVERROR(ENOMEM);
  672. goto fail;
  673. }
  674. }
  675. s->speaker_azim = av_calloc(s->n_conv, sizeof(*s->speaker_azim));
  676. s->speaker_elev = av_calloc(s->n_conv, sizeof(*s->speaker_elev));
  677. if (!s->speaker_azim || !s->speaker_elev) {
  678. ret = AVERROR(ENOMEM);
  679. goto fail;
  680. }
  681. /* get speaker positions */
  682. if ((ret = get_speaker_pos(ctx, s->speaker_azim, s->speaker_elev)) < 0) {
  683. av_log(ctx, AV_LOG_ERROR, "Couldn't get speaker positions. Input channel configuration not supported.\n");
  684. goto fail;
  685. }
  686. for (i = 0; i < s->n_conv; i++) {
  687. float coordinates[3];
  688. /* load and store IRs and corresponding delays */
  689. azim = (int)(s->speaker_azim[i] + azim_orig) % 360;
  690. elev = (int)(s->speaker_elev[i] + elev_orig) % 90;
  691. coordinates[0] = azim;
  692. coordinates[1] = elev;
  693. coordinates[2] = radius;
  694. mysofa_s2c(coordinates);
  695. /* get id of IR closest to desired position */
  696. ret = getfilter_float(ctx, coordinates[0], coordinates[1], coordinates[2],
  697. data_ir_l + n_samples * i,
  698. data_ir_r + n_samples * i,
  699. &delay_l, &delay_r);
  700. if (ret < 0)
  701. goto fail;
  702. s->delay[0][i] = delay_l * sample_rate;
  703. s->delay[1][i] = delay_r * sample_rate;
  704. s->sofa.max_delay = FFMAX3(s->sofa.max_delay, s->delay[0][i], s->delay[1][i]);
  705. }
  706. /* get size of ringbuffer (longest IR plus max. delay) */
  707. /* then choose next power of 2 for performance optimization */
  708. n_current = n_samples + s->sofa.max_delay;
  709. /* length of longest IR plus max. delay */
  710. n_max = FFMAX(n_max, n_current);
  711. /* buffer length is longest IR plus max. delay -> next power of 2
  712. (32 - count leading zeros gives required exponent) */
  713. s->buffer_length = 1 << (32 - ff_clz(n_max));
  714. s->n_fft = n_fft = 1 << (32 - ff_clz(n_max + s->framesize));
  715. if (s->type == FREQUENCY_DOMAIN) {
  716. av_fft_end(s->fft[0]);
  717. av_fft_end(s->fft[1]);
  718. s->fft[0] = av_fft_init(av_log2(s->n_fft), 0);
  719. s->fft[1] = av_fft_init(av_log2(s->n_fft), 0);
  720. av_fft_end(s->ifft[0]);
  721. av_fft_end(s->ifft[1]);
  722. s->ifft[0] = av_fft_init(av_log2(s->n_fft), 1);
  723. s->ifft[1] = av_fft_init(av_log2(s->n_fft), 1);
  724. if (!s->fft[0] || !s->fft[1] || !s->ifft[0] || !s->ifft[1]) {
  725. av_log(ctx, AV_LOG_ERROR, "Unable to create FFT contexts of size %d.\n", s->n_fft);
  726. ret = AVERROR(ENOMEM);
  727. goto fail;
  728. }
  729. }
  730. if (s->type == TIME_DOMAIN) {
  731. s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
  732. s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
  733. } else if (s->type == FREQUENCY_DOMAIN) {
  734. /* get temporary HRTF memory for L and R channel */
  735. data_hrtf_l = av_malloc_array(n_fft, sizeof(*data_hrtf_l) * n_conv);
  736. data_hrtf_r = av_malloc_array(n_fft, sizeof(*data_hrtf_r) * n_conv);
  737. if (!data_hrtf_r || !data_hrtf_l) {
  738. ret = AVERROR(ENOMEM);
  739. goto fail;
  740. }
  741. s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float));
  742. s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float));
  743. s->temp_fft[0] = av_malloc_array(s->n_fft, sizeof(FFTComplex));
  744. s->temp_fft[1] = av_malloc_array(s->n_fft, sizeof(FFTComplex));
  745. s->temp_afft[0] = av_malloc_array(s->n_fft, sizeof(FFTComplex));
  746. s->temp_afft[1] = av_malloc_array(s->n_fft, sizeof(FFTComplex));
  747. if (!s->temp_fft[0] || !s->temp_fft[1] ||
  748. !s->temp_afft[0] || !s->temp_afft[1]) {
  749. ret = AVERROR(ENOMEM);
  750. goto fail;
  751. }
  752. }
  753. if (!s->ringbuffer[0] || !s->ringbuffer[1]) {
  754. ret = AVERROR(ENOMEM);
  755. goto fail;
  756. }
  757. if (s->type == FREQUENCY_DOMAIN) {
  758. fft_in_l = av_calloc(n_fft, sizeof(*fft_in_l));
  759. fft_in_r = av_calloc(n_fft, sizeof(*fft_in_r));
  760. if (!fft_in_l || !fft_in_r) {
  761. ret = AVERROR(ENOMEM);
  762. goto fail;
  763. }
  764. }
  765. for (i = 0; i < s->n_conv; i++) {
  766. float *lir, *rir;
  767. offset = i * n_samples; /* no. samples already written */
  768. lir = data_ir_l + offset;
  769. rir = data_ir_r + offset;
  770. if (s->type == TIME_DOMAIN) {
  771. for (j = 0; j < ir_samples; j++) {
  772. /* load reversed IRs of the specified source position
  773. * sample-by-sample for left and right ear; and apply gain */
  774. s->data_ir[0][offset + j] = lir[ir_samples - 1 - j] * gain_lin;
  775. s->data_ir[1][offset + j] = rir[ir_samples - 1 - j] * gain_lin;
  776. }
  777. } else if (s->type == FREQUENCY_DOMAIN) {
  778. memset(fft_in_l, 0, n_fft * sizeof(*fft_in_l));
  779. memset(fft_in_r, 0, n_fft * sizeof(*fft_in_r));
  780. offset = i * n_fft; /* no. samples already written */
  781. for (j = 0; j < ir_samples; j++) {
  782. /* load non-reversed IRs of the specified source position
  783. * sample-by-sample and apply gain,
  784. * L channel is loaded to real part, R channel to imag part,
  785. * IRs are shifted by L and R delay */
  786. fft_in_l[s->delay[0][i] + j].re = lir[j] * gain_lin;
  787. fft_in_r[s->delay[1][i] + j].re = rir[j] * gain_lin;
  788. }
  789. /* actually transform to frequency domain (IRs -> HRTFs) */
  790. av_fft_permute(s->fft[0], fft_in_l);
  791. av_fft_calc(s->fft[0], fft_in_l);
  792. memcpy(data_hrtf_l + offset, fft_in_l, n_fft * sizeof(*fft_in_l));
  793. av_fft_permute(s->fft[0], fft_in_r);
  794. av_fft_calc(s->fft[0], fft_in_r);
  795. memcpy(data_hrtf_r + offset, fft_in_r, n_fft * sizeof(*fft_in_r));
  796. }
  797. }
  798. if (s->type == FREQUENCY_DOMAIN) {
  799. s->data_hrtf[0] = av_malloc_array(n_fft * s->n_conv, sizeof(FFTComplex));
  800. s->data_hrtf[1] = av_malloc_array(n_fft * s->n_conv, sizeof(FFTComplex));
  801. if (!s->data_hrtf[0] || !s->data_hrtf[1]) {
  802. ret = AVERROR(ENOMEM);
  803. goto fail;
  804. }
  805. memcpy(s->data_hrtf[0], data_hrtf_l, /* copy HRTF data to */
  806. sizeof(FFTComplex) * n_conv * n_fft); /* filter struct */
  807. memcpy(s->data_hrtf[1], data_hrtf_r,
  808. sizeof(FFTComplex) * n_conv * n_fft);
  809. }
  810. fail:
  811. av_freep(&data_hrtf_l); /* free temporary HRTF memory */
  812. av_freep(&data_hrtf_r);
  813. av_freep(&data_ir_l); /* free temprary IR memory */
  814. av_freep(&data_ir_r);
  815. av_freep(&fft_in_l); /* free temporary FFT memory */
  816. av_freep(&fft_in_r);
  817. return ret;
  818. }
  819. static av_cold int init(AVFilterContext *ctx)
  820. {
  821. SOFAlizerContext *s = ctx->priv;
  822. int ret;
  823. if (!s->filename) {
  824. av_log(ctx, AV_LOG_ERROR, "Valid SOFA filename must be set.\n");
  825. return AVERROR(EINVAL);
  826. }
  827. /* preload SOFA file, */
  828. ret = preload_sofa(ctx, s->filename, &s->sample_rate);
  829. if (ret) {
  830. /* file loading error */
  831. av_log(ctx, AV_LOG_ERROR, "Error while loading SOFA file: '%s'\n", s->filename);
  832. } else { /* no file loading error, resampling not required */
  833. av_log(ctx, AV_LOG_DEBUG, "File '%s' loaded.\n", s->filename);
  834. }
  835. if (ret) {
  836. av_log(ctx, AV_LOG_ERROR, "No valid SOFA file could be loaded. Please specify valid SOFA file.\n");
  837. return ret;
  838. }
  839. s->fdsp = avpriv_float_dsp_alloc(0);
  840. if (!s->fdsp)
  841. return AVERROR(ENOMEM);
  842. return 0;
  843. }
  844. static int config_input(AVFilterLink *inlink)
  845. {
  846. AVFilterContext *ctx = inlink->dst;
  847. SOFAlizerContext *s = ctx->priv;
  848. int ret;
  849. if (s->type == FREQUENCY_DOMAIN)
  850. s->nb_samples = s->framesize;
  851. /* gain -3 dB per channel */
  852. s->gain_lfe = expf((s->gain - 3 * inlink->channels + s->lfe_gain) / 20 * M_LN10);
  853. s->n_conv = inlink->channels;
  854. /* load IRs to data_ir[0] and data_ir[1] for required directions */
  855. if ((ret = load_data(ctx, s->rotation, s->elevation, s->radius, inlink->sample_rate)) < 0)
  856. return ret;
  857. av_log(ctx, AV_LOG_DEBUG, "Samplerate: %d Channels to convolute: %d, Length of ringbuffer: %d x %d\n",
  858. inlink->sample_rate, s->n_conv, inlink->channels, s->buffer_length);
  859. return 0;
  860. }
  861. static av_cold void uninit(AVFilterContext *ctx)
  862. {
  863. SOFAlizerContext *s = ctx->priv;
  864. close_sofa(&s->sofa);
  865. av_fft_end(s->ifft[0]);
  866. av_fft_end(s->ifft[1]);
  867. av_fft_end(s->fft[0]);
  868. av_fft_end(s->fft[1]);
  869. s->ifft[0] = NULL;
  870. s->ifft[1] = NULL;
  871. s->fft[0] = NULL;
  872. s->fft[1] = NULL;
  873. av_freep(&s->delay[0]);
  874. av_freep(&s->delay[1]);
  875. av_freep(&s->data_ir[0]);
  876. av_freep(&s->data_ir[1]);
  877. av_freep(&s->ringbuffer[0]);
  878. av_freep(&s->ringbuffer[1]);
  879. av_freep(&s->speaker_azim);
  880. av_freep(&s->speaker_elev);
  881. av_freep(&s->temp_src[0]);
  882. av_freep(&s->temp_src[1]);
  883. av_freep(&s->temp_afft[0]);
  884. av_freep(&s->temp_afft[1]);
  885. av_freep(&s->temp_fft[0]);
  886. av_freep(&s->temp_fft[1]);
  887. av_freep(&s->data_hrtf[0]);
  888. av_freep(&s->data_hrtf[1]);
  889. av_freep(&s->fdsp);
  890. }
  891. #define OFFSET(x) offsetof(SOFAlizerContext, x)
  892. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  893. static const AVOption sofalizer_options[] = {
  894. { "sofa", "sofa filename", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  895. { "gain", "set gain in dB", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, -20, 40, .flags = FLAGS },
  896. { "rotation", "set rotation" , OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl=0}, -360, 360, .flags = FLAGS },
  897. { "elevation", "set elevation", OFFSET(elevation), AV_OPT_TYPE_FLOAT, {.dbl=0}, -90, 90, .flags = FLAGS },
  898. { "radius", "set radius", OFFSET(radius), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 5, .flags = FLAGS },
  899. { "type", "set processing", OFFSET(type), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, .flags = FLAGS, "type" },
  900. { "time", "time domain", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, .flags = FLAGS, "type" },
  901. { "freq", "frequency domain", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, .flags = FLAGS, "type" },
  902. { "speakers", "set speaker custom positions", OFFSET(speakers_pos), AV_OPT_TYPE_STRING, {.str=0}, 0, 0, .flags = FLAGS },
  903. { "lfegain", "set lfe gain", OFFSET(lfe_gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, -20,40, .flags = FLAGS },
  904. { "framesize", "set frame size", OFFSET(framesize), AV_OPT_TYPE_INT, {.i64=1024},1024,96000, .flags = FLAGS },
  905. { "normalize", "normalize IRs", OFFSET(normalize), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, .flags = FLAGS },
  906. { "interpolate","interpolate IRs from neighbors", OFFSET(interpolate),AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
  907. { "minphase", "minphase IRs", OFFSET(minphase), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
  908. { "anglestep", "set neighbor search angle step", OFFSET(anglestep), AV_OPT_TYPE_FLOAT, {.dbl=.5}, 0.01, 10, .flags = FLAGS },
  909. { "radstep", "set neighbor search radius step", OFFSET(radstep), AV_OPT_TYPE_FLOAT, {.dbl=.01}, 0.01, 1, .flags = FLAGS },
  910. { NULL }
  911. };
  912. AVFILTER_DEFINE_CLASS(sofalizer);
  913. static const AVFilterPad inputs[] = {
  914. {
  915. .name = "default",
  916. .type = AVMEDIA_TYPE_AUDIO,
  917. .config_props = config_input,
  918. },
  919. { NULL }
  920. };
  921. static const AVFilterPad outputs[] = {
  922. {
  923. .name = "default",
  924. .type = AVMEDIA_TYPE_AUDIO,
  925. },
  926. { NULL }
  927. };
  928. AVFilter ff_af_sofalizer = {
  929. .name = "sofalizer",
  930. .description = NULL_IF_CONFIG_SMALL("SOFAlizer (Spatially Oriented Format for Acoustics)."),
  931. .priv_size = sizeof(SOFAlizerContext),
  932. .priv_class = &sofalizer_class,
  933. .init = init,
  934. .activate = activate,
  935. .uninit = uninit,
  936. .query_formats = query_formats,
  937. .inputs = inputs,
  938. .outputs = outputs,
  939. .flags = AVFILTER_FLAG_SLICE_THREADS,
  940. };