af_sofalizer.c 40 KB

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