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.

1137 lines
43KB

  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 <netcdf.h>
  29. #include "libavcodec/avfft.h"
  30. #include "libavutil/float_dsp.h"
  31. #include "libavutil/intmath.h"
  32. #include "libavutil/opt.h"
  33. #include "avfilter.h"
  34. #include "internal.h"
  35. #include "audio.h"
  36. #define TIME_DOMAIN 0
  37. #define FREQUENCY_DOMAIN 1
  38. typedef struct NCSofa { /* contains data of one SOFA file */
  39. int ncid; /* netCDF ID of the opened SOFA file */
  40. int n_samples; /* length of one impulse response (IR) */
  41. int m_dim; /* number of measurement positions */
  42. int *data_delay; /* broadband delay of each IR */
  43. /* all measurement positions for each receiver (i.e. ear): */
  44. float *sp_a; /* azimuth angles */
  45. float *sp_e; /* elevation angles */
  46. float *sp_r; /* radii */
  47. /* data at each measurement position for each receiver: */
  48. float *data_ir; /* IRs (time-domain) */
  49. } NCSofa;
  50. typedef struct SOFAlizerContext {
  51. const AVClass *class;
  52. char *filename; /* name of SOFA file */
  53. NCSofa sofa; /* contains data of the SOFA file */
  54. int sample_rate; /* sample rate from SOFA file */
  55. float *speaker_azim; /* azimuth of the virtual loudspeakers */
  56. float *speaker_elev; /* elevation of the virtual loudspeakers */
  57. float gain_lfe; /* gain applied to LFE channel */
  58. int lfe_channel; /* LFE channel position in channel layout */
  59. int n_conv; /* number of channels to convolute */
  60. /* buffer variables (for convolution) */
  61. float *ringbuffer[2]; /* buffers input samples, length of one buffer: */
  62. /* no. input ch. (incl. LFE) x buffer_length */
  63. int write[2]; /* current write position to ringbuffer */
  64. int buffer_length; /* is: longest IR plus max. delay in all SOFA files */
  65. /* then choose next power of 2 */
  66. int n_fft; /* number of samples in one FFT block */
  67. /* netCDF variables */
  68. int *delay[2]; /* broadband delay for each channel/IR to be convolved */
  69. float *data_ir[2]; /* IRs for all channels to be convolved */
  70. /* (this excludes the LFE) */
  71. float *temp_src[2];
  72. FFTComplex *temp_fft[2];
  73. /* control variables */
  74. float gain; /* filter gain (in dB) */
  75. float rotation; /* rotation of virtual loudspeakers (in degrees) */
  76. float elevation; /* elevation of virtual loudspeakers (in deg.) */
  77. float radius; /* distance virtual loudspeakers to listener (in metres) */
  78. int type; /* processing type */
  79. FFTContext *fft[2], *ifft[2];
  80. FFTComplex *data_hrtf[2];
  81. AVFloatDSPContext *fdsp;
  82. } SOFAlizerContext;
  83. static int close_sofa(struct NCSofa *sofa)
  84. {
  85. av_freep(&sofa->data_delay);
  86. av_freep(&sofa->sp_a);
  87. av_freep(&sofa->sp_e);
  88. av_freep(&sofa->sp_r);
  89. av_freep(&sofa->data_ir);
  90. nc_close(sofa->ncid);
  91. sofa->ncid = 0;
  92. return 0;
  93. }
  94. static int load_sofa(AVFilterContext *ctx, char *filename, int *samplingrate)
  95. {
  96. struct SOFAlizerContext *s = ctx->priv;
  97. /* variables associated with content of SOFA file: */
  98. int ncid, n_dims, n_vars, n_gatts, n_unlim_dim_id, status;
  99. char data_delay_dim_name[NC_MAX_NAME];
  100. float *sp_a, *sp_e, *sp_r, *data_ir;
  101. char *sofa_conventions;
  102. char dim_name[NC_MAX_NAME]; /* names of netCDF dimensions */
  103. size_t *dim_length; /* lengths of netCDF dimensions */
  104. char *text;
  105. unsigned int sample_rate;
  106. int data_delay_dim_id[2];
  107. int samplingrate_id;
  108. int data_delay_id;
  109. int n_samples;
  110. int m_dim_id = -1;
  111. int n_dim_id = -1;
  112. int data_ir_id;
  113. size_t att_len;
  114. int m_dim;
  115. int *data_delay;
  116. int sp_id;
  117. int i, ret;
  118. s->sofa.ncid = 0;
  119. status = nc_open(filename, NC_NOWRITE, &ncid); /* open SOFA file read-only */
  120. if (status != NC_NOERR) {
  121. av_log(ctx, AV_LOG_ERROR, "Can't find SOFA-file '%s'\n", filename);
  122. return AVERROR(EINVAL);
  123. }
  124. /* get number of dimensions, vars, global attributes and Id of unlimited dimensions: */
  125. nc_inq(ncid, &n_dims, &n_vars, &n_gatts, &n_unlim_dim_id);
  126. /* -- get number of measurements ("M") and length of one IR ("N") -- */
  127. dim_length = av_malloc_array(n_dims, sizeof(*dim_length));
  128. if (!dim_length) {
  129. nc_close(ncid);
  130. return AVERROR(ENOMEM);
  131. }
  132. for (i = 0; i < n_dims; i++) { /* go through all dimensions of file */
  133. nc_inq_dim(ncid, i, (char *)&dim_name, &dim_length[i]); /* get dimensions */
  134. if (!strncmp("M", (const char *)&dim_name, 1)) /* get ID of dimension "M" */
  135. m_dim_id = i;
  136. if (!strncmp("N", (const char *)&dim_name, 1)) /* get ID of dimension "N" */
  137. n_dim_id = i;
  138. }
  139. if ((m_dim_id == -1) || (n_dim_id == -1)) { /* dimension "M" or "N" couldn't be found */
  140. av_log(ctx, AV_LOG_ERROR, "Can't find required dimensions in SOFA file.\n");
  141. av_freep(&dim_length);
  142. nc_close(ncid);
  143. return AVERROR(EINVAL);
  144. }
  145. n_samples = dim_length[n_dim_id]; /* get length of one IR */
  146. m_dim = dim_length[m_dim_id]; /* get number of measurements */
  147. av_freep(&dim_length);
  148. /* -- check file type -- */
  149. /* get length of attritube "Conventions" */
  150. status = nc_inq_attlen(ncid, NC_GLOBAL, "Conventions", &att_len);
  151. if (status != NC_NOERR) {
  152. av_log(ctx, AV_LOG_ERROR, "Can't get length of attribute \"Conventions\".\n");
  153. nc_close(ncid);
  154. return AVERROR_INVALIDDATA;
  155. }
  156. /* check whether file is SOFA file */
  157. text = av_malloc(att_len + 1);
  158. if (!text) {
  159. nc_close(ncid);
  160. return AVERROR(ENOMEM);
  161. }
  162. nc_get_att_text(ncid, NC_GLOBAL, "Conventions", text);
  163. *(text + att_len) = 0;
  164. if (strncmp("SOFA", text, 4)) {
  165. av_log(ctx, AV_LOG_ERROR, "Not a SOFA file!\n");
  166. av_freep(&text);
  167. nc_close(ncid);
  168. return AVERROR(EINVAL);
  169. }
  170. av_freep(&text);
  171. status = nc_inq_attlen(ncid, NC_GLOBAL, "License", &att_len);
  172. if (status == NC_NOERR) {
  173. text = av_malloc(att_len + 1);
  174. if (text) {
  175. nc_get_att_text(ncid, NC_GLOBAL, "License", text);
  176. *(text + att_len) = 0;
  177. av_log(ctx, AV_LOG_INFO, "SOFA file License: %s\n", text);
  178. av_freep(&text);
  179. }
  180. }
  181. status = nc_inq_attlen(ncid, NC_GLOBAL, "SourceDescription", &att_len);
  182. if (status == NC_NOERR) {
  183. text = av_malloc(att_len + 1);
  184. if (text) {
  185. nc_get_att_text(ncid, NC_GLOBAL, "SourceDescription", text);
  186. *(text + att_len) = 0;
  187. av_log(ctx, AV_LOG_INFO, "SOFA file SourceDescription: %s\n", text);
  188. av_freep(&text);
  189. }
  190. }
  191. status = nc_inq_attlen(ncid, NC_GLOBAL, "Comment", &att_len);
  192. if (status == NC_NOERR) {
  193. text = av_malloc(att_len + 1);
  194. if (text) {
  195. nc_get_att_text(ncid, NC_GLOBAL, "Comment", text);
  196. *(text + att_len) = 0;
  197. av_log(ctx, AV_LOG_INFO, "SOFA file Comment: %s\n", text);
  198. av_freep(&text);
  199. }
  200. }
  201. status = nc_inq_attlen(ncid, NC_GLOBAL, "SOFAConventions", &att_len);
  202. if (status != NC_NOERR) {
  203. av_log(ctx, AV_LOG_ERROR, "Can't get length of attribute \"SOFAConventions\".\n");
  204. nc_close(ncid);
  205. return AVERROR_INVALIDDATA;
  206. }
  207. sofa_conventions = av_malloc(att_len + 1);
  208. if (!sofa_conventions) {
  209. nc_close(ncid);
  210. return AVERROR(ENOMEM);
  211. }
  212. nc_get_att_text(ncid, NC_GLOBAL, "SOFAConventions", sofa_conventions);
  213. *(sofa_conventions + att_len) = 0;
  214. if (strncmp("SimpleFreeFieldHRIR", sofa_conventions, att_len)) {
  215. av_log(ctx, AV_LOG_ERROR, "Not a SimpleFreeFieldHRIR file!\n");
  216. av_freep(&sofa_conventions);
  217. nc_close(ncid);
  218. return AVERROR(EINVAL);
  219. }
  220. av_freep(&sofa_conventions);
  221. /* -- get sampling rate of HRTFs -- */
  222. /* read ID, then value */
  223. status = nc_inq_varid(ncid, "Data.SamplingRate", &samplingrate_id);
  224. status += nc_get_var_uint(ncid, samplingrate_id, &sample_rate);
  225. if (status != NC_NOERR) {
  226. av_log(ctx, AV_LOG_ERROR, "Couldn't read Data.SamplingRate.\n");
  227. nc_close(ncid);
  228. return AVERROR(EINVAL);
  229. }
  230. *samplingrate = sample_rate; /* remember sampling rate */
  231. /* -- allocate memory for one value for each measurement position: -- */
  232. sp_a = s->sofa.sp_a = av_malloc_array(m_dim, sizeof(float));
  233. sp_e = s->sofa.sp_e = av_malloc_array(m_dim, sizeof(float));
  234. sp_r = s->sofa.sp_r = av_malloc_array(m_dim, sizeof(float));
  235. /* delay and IR values required for each ear and measurement position: */
  236. data_delay = s->sofa.data_delay = av_calloc(m_dim, 2 * sizeof(int));
  237. data_ir = s->sofa.data_ir = av_malloc_array(m_dim * n_samples, sizeof(float) * 2);
  238. if (!data_delay || !sp_a || !sp_e || !sp_r || !data_ir) {
  239. /* if memory could not be allocated */
  240. close_sofa(&s->sofa);
  241. return AVERROR(ENOMEM);
  242. }
  243. /* get impulse responses (HRTFs): */
  244. /* get corresponding ID */
  245. status = nc_inq_varid(ncid, "Data.IR", &data_ir_id);
  246. status += nc_get_var_float(ncid, data_ir_id, data_ir); /* read and store IRs */
  247. if (status != NC_NOERR) {
  248. av_log(ctx, AV_LOG_ERROR, "Couldn't read Data.IR!\n");
  249. ret = AVERROR(EINVAL);
  250. goto error;
  251. }
  252. /* get source positions of the HRTFs in the SOFA file: */
  253. status = nc_inq_varid(ncid, "SourcePosition", &sp_id); /* get corresponding ID */
  254. status += nc_get_vara_float(ncid, sp_id, (size_t[2]){ 0, 0 } ,
  255. (size_t[2]){ m_dim, 1}, sp_a); /* read & store azimuth angles */
  256. status += nc_get_vara_float(ncid, sp_id, (size_t[2]){ 0, 1 } ,
  257. (size_t[2]){ m_dim, 1}, sp_e); /* read & store elevation angles */
  258. status += nc_get_vara_float(ncid, sp_id, (size_t[2]){ 0, 2 } ,
  259. (size_t[2]){ m_dim, 1}, sp_r); /* read & store radii */
  260. if (status != NC_NOERR) { /* if any source position variable coudn't be read */
  261. av_log(ctx, AV_LOG_ERROR, "Couldn't read SourcePosition.\n");
  262. ret = AVERROR(EINVAL);
  263. goto error;
  264. }
  265. /* read Data.Delay, check for errors and fit it to data_delay */
  266. status = nc_inq_varid(ncid, "Data.Delay", &data_delay_id);
  267. status += nc_inq_vardimid(ncid, data_delay_id, &data_delay_dim_id[0]);
  268. status += nc_inq_dimname(ncid, data_delay_dim_id[0], data_delay_dim_name);
  269. if (status != NC_NOERR) {
  270. av_log(ctx, AV_LOG_ERROR, "Couldn't read Data.Delay.\n");
  271. ret = AVERROR(EINVAL);
  272. goto error;
  273. }
  274. /* Data.Delay dimension check */
  275. /* dimension of Data.Delay is [I R]: */
  276. if (!strncmp(data_delay_dim_name, "I", 2)) {
  277. /* check 2 characters to assure string is 0-terminated after "I" */
  278. int delay[2]; /* delays get from SOFA file: */
  279. av_log(ctx, AV_LOG_DEBUG, "Data.Delay has dimension [I R]\n");
  280. status = nc_get_var_int(ncid, data_delay_id, &delay[0]);
  281. if (status != NC_NOERR) {
  282. av_log(ctx, AV_LOG_ERROR, "Couldn't read Data.Delay\n");
  283. ret = AVERROR(EINVAL);
  284. goto error;
  285. }
  286. int *data_delay_r = data_delay + m_dim;
  287. for (i = 0; i < m_dim; i++) { /* extend given dimension [I R] to [M R] */
  288. /* assign constant delay value for all measurements to data_delay fields */
  289. data_delay[i] = delay[0];
  290. data_delay_r[i] = delay[1];
  291. }
  292. /* dimension of Data.Delay is [M R] */
  293. } else if (!strncmp(data_delay_dim_name, "M", 2)) {
  294. av_log(ctx, AV_LOG_ERROR, "Data.Delay in dimension [M R]\n");
  295. /* get delays from SOFA file: */
  296. status = nc_get_var_int(ncid, data_delay_id, data_delay);
  297. if (status != NC_NOERR) {
  298. av_log(ctx, AV_LOG_ERROR, "Couldn't read Data.Delay\n");
  299. ret = AVERROR(EINVAL);
  300. goto error;
  301. }
  302. } else { /* dimension of Data.Delay is neither [I R] nor [M R] */
  303. av_log(ctx, AV_LOG_ERROR, "Data.Delay does not have the required dimensions [I R] or [M R].\n");
  304. ret = AVERROR(EINVAL);
  305. goto error;
  306. }
  307. /* save information in SOFA struct: */
  308. s->sofa.m_dim = m_dim; /* no. measurement positions */
  309. s->sofa.n_samples = n_samples; /* length on one IR */
  310. s->sofa.ncid = ncid; /* netCDF ID of SOFA file */
  311. nc_close(ncid); /* close SOFA file */
  312. return 0;
  313. error:
  314. close_sofa(&s->sofa);
  315. return ret;
  316. }
  317. static int get_speaker_pos(AVFilterContext *ctx,
  318. float *speaker_azim, float *speaker_elev)
  319. {
  320. struct SOFAlizerContext *s = ctx->priv;
  321. uint64_t channels_layout = ctx->inputs[0]->channel_layout;
  322. float azim[16] = { 0 };
  323. float elev[16] = { 0 };
  324. int m, ch, n_conv = ctx->inputs[0]->channels; /* get no. input channels */
  325. if (n_conv > 16)
  326. return AVERROR(EINVAL);
  327. s->lfe_channel = -1;
  328. /* set speaker positions according to input channel configuration: */
  329. for (m = 0, ch = 0; ch < n_conv && m < 64; m++) {
  330. uint64_t mask = channels_layout & (1 << m);
  331. switch (mask) {
  332. case AV_CH_FRONT_LEFT: azim[ch] = 30; break;
  333. case AV_CH_FRONT_RIGHT: azim[ch] = 330; break;
  334. case AV_CH_FRONT_CENTER: azim[ch] = 0; break;
  335. case AV_CH_LOW_FREQUENCY:
  336. case AV_CH_LOW_FREQUENCY_2: s->lfe_channel = ch; break;
  337. case AV_CH_BACK_LEFT: azim[ch] = 150; break;
  338. case AV_CH_BACK_RIGHT: azim[ch] = 210; break;
  339. case AV_CH_BACK_CENTER: azim[ch] = 180; break;
  340. case AV_CH_SIDE_LEFT: azim[ch] = 90; break;
  341. case AV_CH_SIDE_RIGHT: azim[ch] = 270; break;
  342. case AV_CH_FRONT_LEFT_OF_CENTER: azim[ch] = 15; break;
  343. case AV_CH_FRONT_RIGHT_OF_CENTER: azim[ch] = 345; break;
  344. case AV_CH_TOP_CENTER: azim[ch] = 0;
  345. elev[ch] = 90; break;
  346. case AV_CH_TOP_FRONT_LEFT: azim[ch] = 30;
  347. elev[ch] = 45; break;
  348. case AV_CH_TOP_FRONT_CENTER: azim[ch] = 0;
  349. elev[ch] = 45; break;
  350. case AV_CH_TOP_FRONT_RIGHT: azim[ch] = 330;
  351. elev[ch] = 45; break;
  352. case AV_CH_TOP_BACK_LEFT: azim[ch] = 150;
  353. elev[ch] = 45; break;
  354. case AV_CH_TOP_BACK_RIGHT: azim[ch] = 210;
  355. elev[ch] = 45; break;
  356. case AV_CH_TOP_BACK_CENTER: azim[ch] = 180;
  357. elev[ch] = 45; break;
  358. case AV_CH_WIDE_LEFT: azim[ch] = 90; break;
  359. case AV_CH_WIDE_RIGHT: azim[ch] = 270; break;
  360. case AV_CH_SURROUND_DIRECT_LEFT: azim[ch] = 90; break;
  361. case AV_CH_SURROUND_DIRECT_RIGHT: azim[ch] = 270; break;
  362. case AV_CH_STEREO_LEFT: azim[ch] = 90; break;
  363. case AV_CH_STEREO_RIGHT: azim[ch] = 270; break;
  364. case 0: break;
  365. default:
  366. return AVERROR(EINVAL);
  367. }
  368. if (mask)
  369. ch++;
  370. }
  371. memcpy(speaker_azim, azim, n_conv * sizeof(float));
  372. memcpy(speaker_elev, elev, n_conv * sizeof(float));
  373. return 0;
  374. }
  375. static int max_delay(struct NCSofa *sofa)
  376. {
  377. int i, max = 0;
  378. for (i = 0; i < sofa->m_dim * 2; i++) {
  379. /* search maximum delay in given SOFA file */
  380. max = FFMAX(max, sofa->data_delay[i]);
  381. }
  382. return max;
  383. }
  384. static int find_m(SOFAlizerContext *s, int azim, int elev, float radius)
  385. {
  386. /* get source positions and M of currently selected SOFA file */
  387. float *sp_a = s->sofa.sp_a; /* azimuth angle */
  388. float *sp_e = s->sofa.sp_e; /* elevation angle */
  389. float *sp_r = s->sofa.sp_r; /* radius */
  390. int m_dim = s->sofa.m_dim; /* no. measurements */
  391. int best_id = 0; /* index m currently closest to desired source pos. */
  392. float delta = 1000; /* offset between desired and currently best pos. */
  393. float current;
  394. int i;
  395. for (i = 0; i < m_dim; i++) {
  396. /* search through all measurements in currently selected SOFA file */
  397. /* distance of current to desired source position: */
  398. current = fabs(sp_a[i] - azim) +
  399. fabs(sp_e[i] - elev) +
  400. fabs(sp_r[i] - radius);
  401. if (current <= delta) {
  402. /* if current distance is smaller than smallest distance so far */
  403. delta = current;
  404. best_id = i; /* remember index */
  405. }
  406. }
  407. return best_id;
  408. }
  409. static int compensate_volume(AVFilterContext *ctx)
  410. {
  411. struct SOFAlizerContext *s = ctx->priv;
  412. float compensate;
  413. float energy = 0;
  414. float *ir;
  415. int m;
  416. if (s->sofa.ncid) {
  417. /* find IR at front center position in the SOFA file (IR closest to 0°,0°,1m) */
  418. struct NCSofa *sofa = &s->sofa;
  419. m = find_m(s, 0, 0, 1);
  420. /* get energy of that IR and compensate volume */
  421. ir = sofa->data_ir + 2 * m * sofa->n_samples;
  422. if (sofa->n_samples & 31) {
  423. energy = avpriv_scalarproduct_float_c(ir, ir, sofa->n_samples);
  424. } else {
  425. energy = s->fdsp->scalarproduct_float(ir, ir, sofa->n_samples);
  426. }
  427. compensate = 256 / (sofa->n_samples * sqrt(energy));
  428. av_log(ctx, AV_LOG_DEBUG, "Compensate-factor: %f\n", compensate);
  429. ir = sofa->data_ir;
  430. /* apply volume compensation to IRs */
  431. s->fdsp->vector_fmul_scalar(ir, ir, compensate, sofa->n_samples * sofa->m_dim * 2);
  432. emms_c();
  433. }
  434. return 0;
  435. }
  436. typedef struct ThreadData {
  437. AVFrame *in, *out;
  438. int *write;
  439. int **delay;
  440. float **ir;
  441. int *n_clippings;
  442. float **ringbuffer;
  443. float **temp_src;
  444. FFTComplex **temp_fft;
  445. } ThreadData;
  446. static int sofalizer_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  447. {
  448. SOFAlizerContext *s = ctx->priv;
  449. ThreadData *td = arg;
  450. AVFrame *in = td->in, *out = td->out;
  451. int offset = jobnr;
  452. int *write = &td->write[jobnr];
  453. const int *const delay = td->delay[jobnr];
  454. const float *const ir = td->ir[jobnr];
  455. int *n_clippings = &td->n_clippings[jobnr];
  456. float *ringbuffer = td->ringbuffer[jobnr];
  457. float *temp_src = td->temp_src[jobnr];
  458. const int n_samples = s->sofa.n_samples; /* length of one IR */
  459. const float *src = (const float *)in->data[0]; /* get pointer to audio input buffer */
  460. float *dst = (float *)out->data[0]; /* get pointer to audio output buffer */
  461. const int in_channels = s->n_conv; /* number of input channels */
  462. /* ring buffer length is: longest IR plus max. delay -> next power of 2 */
  463. const int buffer_length = s->buffer_length;
  464. /* -1 for AND instead of MODULO (applied to powers of 2): */
  465. const uint32_t modulo = (uint32_t)buffer_length - 1;
  466. float *buffer[16]; /* holds ringbuffer for each input channel */
  467. int wr = *write;
  468. int read;
  469. int i, l;
  470. dst += offset;
  471. for (l = 0; l < in_channels; l++) {
  472. /* get starting address of ringbuffer for each input channel */
  473. buffer[l] = ringbuffer + l * buffer_length;
  474. }
  475. for (i = 0; i < in->nb_samples; i++) {
  476. const float *temp_ir = ir; /* using same set of IRs for each sample */
  477. *dst = 0;
  478. for (l = 0; l < in_channels; l++) {
  479. /* write current input sample to ringbuffer (for each channel) */
  480. *(buffer[l] + wr) = src[l];
  481. }
  482. /* loop goes through all channels to be convolved */
  483. for (l = 0; l < in_channels; l++) {
  484. const float *const bptr = buffer[l];
  485. if (l == s->lfe_channel) {
  486. /* LFE is an input channel but requires no convolution */
  487. /* apply gain to LFE signal and add to output buffer */
  488. *dst += *(buffer[s->lfe_channel] + wr) * s->gain_lfe;
  489. temp_ir += n_samples;
  490. continue;
  491. }
  492. /* current read position in ringbuffer: input sample write position
  493. * - delay for l-th ch. + diff. betw. IR length and buffer length
  494. * (mod buffer length) */
  495. read = (wr - *(delay + l) - (n_samples - 1) + buffer_length) & modulo;
  496. if (read + n_samples < buffer_length) {
  497. memcpy(temp_src, bptr + read, n_samples * sizeof(*temp_src));
  498. } else {
  499. int len = FFMIN(n_samples - (read % n_samples), buffer_length - read);
  500. memcpy(temp_src, bptr + read, len * sizeof(*temp_src));
  501. memcpy(temp_src + len, bptr, (n_samples - len) * sizeof(*temp_src));
  502. }
  503. /* multiply signal and IR, and add up the results */
  504. dst[0] += s->fdsp->scalarproduct_float(temp_ir, temp_src, n_samples);
  505. temp_ir += n_samples;
  506. }
  507. /* clippings counter */
  508. if (fabs(*dst) > 1)
  509. *n_clippings += 1;
  510. /* move output buffer pointer by +2 to get to next sample of processed channel: */
  511. dst += 2;
  512. src += in_channels;
  513. wr = (wr + 1) & modulo; /* update ringbuffer write position */
  514. }
  515. *write = wr; /* remember write position in ringbuffer for next call */
  516. return 0;
  517. }
  518. static int sofalizer_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  519. {
  520. SOFAlizerContext *s = ctx->priv;
  521. ThreadData *td = arg;
  522. AVFrame *in = td->in, *out = td->out;
  523. int offset = jobnr;
  524. int *write = &td->write[jobnr];
  525. FFTComplex *hrtf = s->data_hrtf[jobnr]; /* get pointers to current HRTF data */
  526. int *n_clippings = &td->n_clippings[jobnr];
  527. float *ringbuffer = td->ringbuffer[jobnr];
  528. const int n_samples = s->sofa.n_samples; /* length of one IR */
  529. const float *src = (const float *)in->data[0]; /* get pointer to audio input buffer */
  530. float *dst = (float *)out->data[0]; /* get pointer to audio output buffer */
  531. const int in_channels = s->n_conv; /* number of input channels */
  532. /* ring buffer length is: longest IR plus max. delay -> next power of 2 */
  533. const int buffer_length = s->buffer_length;
  534. /* -1 for AND instead of MODULO (applied to powers of 2): */
  535. const uint32_t modulo = (uint32_t)buffer_length - 1;
  536. FFTComplex *fft_in = s->temp_fft[jobnr]; /* temporary array for FFT input/output data */
  537. FFTContext *ifft = s->ifft[jobnr];
  538. FFTContext *fft = s->fft[jobnr];
  539. const int n_conv = s->n_conv;
  540. const int n_fft = s->n_fft;
  541. int wr = *write;
  542. int n_read;
  543. int i, j;
  544. dst += offset;
  545. /* find minimum between number of samples and output buffer length:
  546. * (important, if one IR is longer than the output buffer) */
  547. n_read = FFMIN(s->sofa.n_samples, in->nb_samples);
  548. for (j = 0; j < n_read; j++) {
  549. /* initialize output buf with saved signal from overflow buf */
  550. dst[2 * j] = ringbuffer[wr];
  551. ringbuffer[wr] = 0.0; /* re-set read samples to zero */
  552. /* update ringbuffer read/write position */
  553. wr = (wr + 1) & modulo;
  554. }
  555. /* initialize rest of output buffer with 0 */
  556. for (j = n_read; j < in->nb_samples; j++) {
  557. dst[2 * j] = 0;
  558. }
  559. for (i = 0; i < n_conv; i++) {
  560. if (i == s->lfe_channel) { /* LFE */
  561. for (j = 0; j < in->nb_samples; j++) {
  562. /* apply gain to LFE signal and add to output buffer */
  563. dst[2 * j] += src[i + j * in_channels] * s->gain_lfe;
  564. }
  565. continue;
  566. }
  567. /* outer loop: go through all input channels to be convolved */
  568. offset = i * n_fft; /* no. samples already processed */
  569. /* fill FFT input with 0 (we want to zero-pad) */
  570. memset(fft_in, 0, sizeof(FFTComplex) * n_fft);
  571. for (j = 0; j < in->nb_samples; j++) {
  572. /* prepare input for FFT */
  573. /* write all samples of current input channel to FFT input array */
  574. fft_in[j].re = src[j * in_channels + i];
  575. }
  576. /* transform input signal of current channel to frequency domain */
  577. av_fft_permute(fft, fft_in);
  578. av_fft_calc(fft, fft_in);
  579. for (j = 0; j < n_fft; j++) {
  580. const float re = fft_in[j].re;
  581. const float im = fft_in[j].im;
  582. /* complex multiplication of input signal and HRTFs */
  583. /* output channel (real): */
  584. fft_in[j].re = re * (hrtf + offset + j)->re - im * (hrtf + offset + j)->im;
  585. /* output channel (imag): */
  586. fft_in[j].im = re * (hrtf + offset + j)->im + im * (hrtf + offset + j)->re;
  587. }
  588. /* transform output signal of current channel back to time domain */
  589. av_fft_permute(ifft, fft_in);
  590. av_fft_calc(ifft, fft_in);
  591. for (j = 0; j < in->nb_samples; j++) {
  592. /* write output signal of current channel to output buffer */
  593. dst[2 * j] += fft_in[j].re / (float)n_fft;
  594. }
  595. for (j = 0; j < n_samples - 1; j++) { /* overflow length is IR length - 1 */
  596. /* write the rest of output signal to overflow buffer */
  597. int write_pos = (wr + j) & modulo;
  598. *(ringbuffer + write_pos) += fft_in[in->nb_samples + j].re / (float)n_fft;
  599. }
  600. }
  601. /* go through all samples of current output buffer: count clippings */
  602. for (i = 0; i < out->nb_samples; i++) {
  603. /* clippings counter */
  604. if (fabs(*dst) > 1) { /* if current output sample > 1 */
  605. *n_clippings = *n_clippings + 1;
  606. }
  607. /* move output buffer pointer by +2 to get to next sample of processed channel: */
  608. dst += 2;
  609. }
  610. /* remember read/write position in ringbuffer for next call */
  611. *write = wr;
  612. return 0;
  613. }
  614. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  615. {
  616. AVFilterContext *ctx = inlink->dst;
  617. SOFAlizerContext *s = ctx->priv;
  618. AVFilterLink *outlink = ctx->outputs[0];
  619. int n_clippings[2] = { 0 };
  620. ThreadData td;
  621. AVFrame *out;
  622. out = ff_get_audio_buffer(outlink, in->nb_samples);
  623. if (!out) {
  624. av_frame_free(&in);
  625. return AVERROR(ENOMEM);
  626. }
  627. av_frame_copy_props(out, in);
  628. td.in = in; td.out = out; td.write = s->write;
  629. td.delay = s->delay; td.ir = s->data_ir; td.n_clippings = n_clippings;
  630. td.ringbuffer = s->ringbuffer; td.temp_src = s->temp_src;
  631. td.temp_fft = s->temp_fft;
  632. if (s->type == TIME_DOMAIN) {
  633. ctx->internal->execute(ctx, sofalizer_convolute, &td, NULL, 2);
  634. } else {
  635. ctx->internal->execute(ctx, sofalizer_fast_convolute, &td, NULL, 2);
  636. }
  637. emms_c();
  638. /* display error message if clipping occurred */
  639. if (n_clippings[0] + n_clippings[1] > 0) {
  640. av_log(ctx, AV_LOG_WARNING, "%d of %d samples clipped. Please reduce gain.\n",
  641. n_clippings[0] + n_clippings[1], out->nb_samples * 2);
  642. }
  643. av_frame_free(&in);
  644. return ff_filter_frame(outlink, out);
  645. }
  646. static int query_formats(AVFilterContext *ctx)
  647. {
  648. struct SOFAlizerContext *s = ctx->priv;
  649. AVFilterFormats *formats = NULL;
  650. AVFilterChannelLayouts *layouts = NULL;
  651. int ret, sample_rates[] = { 48000, -1 };
  652. ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLT);
  653. if (ret)
  654. return ret;
  655. ret = ff_set_common_formats(ctx, formats);
  656. if (ret)
  657. return ret;
  658. layouts = ff_all_channel_layouts();
  659. if (!layouts)
  660. return AVERROR(ENOMEM);
  661. ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts);
  662. if (ret)
  663. return ret;
  664. layouts = NULL;
  665. ret = ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO);
  666. if (ret)
  667. return ret;
  668. ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
  669. if (ret)
  670. return ret;
  671. sample_rates[0] = s->sample_rate;
  672. formats = ff_make_format_list(sample_rates);
  673. if (!formats)
  674. return AVERROR(ENOMEM);
  675. return ff_set_common_samplerates(ctx, formats);
  676. }
  677. static int load_data(AVFilterContext *ctx, int azim, int elev, float radius)
  678. {
  679. struct SOFAlizerContext *s = ctx->priv;
  680. const int n_samples = s->sofa.n_samples;
  681. int n_conv = s->n_conv; /* no. channels to convolve */
  682. int n_fft = s->n_fft;
  683. int delay_l[16]; /* broadband delay for each IR */
  684. int delay_r[16];
  685. int nb_input_channels = ctx->inputs[0]->channels; /* no. input channels */
  686. float gain_lin = expf((s->gain - 3 * nb_input_channels) / 20 * M_LN10); /* gain - 3dB/channel */
  687. FFTComplex *data_hrtf_l = NULL;
  688. FFTComplex *data_hrtf_r = NULL;
  689. FFTComplex *fft_in_l = NULL;
  690. FFTComplex *fft_in_r = NULL;
  691. float *data_ir_l = NULL;
  692. float *data_ir_r = NULL;
  693. int offset = 0; /* used for faster pointer arithmetics in for-loop */
  694. int m[16]; /* measurement index m of IR closest to required source positions */
  695. int i, j, azim_orig = azim, elev_orig = elev;
  696. if (!s->sofa.ncid) { /* if an invalid SOFA file has been selected */
  697. av_log(ctx, AV_LOG_ERROR, "Selected SOFA file is invalid. Please select valid SOFA file.\n");
  698. return AVERROR_INVALIDDATA;
  699. }
  700. if (s->type == TIME_DOMAIN) {
  701. s->temp_src[0] = av_calloc(FFALIGN(n_samples, 16), sizeof(float));
  702. s->temp_src[1] = av_calloc(FFALIGN(n_samples, 16), sizeof(float));
  703. /* get temporary IR for L and R channel */
  704. data_ir_l = av_malloc_array(n_conv * n_samples, sizeof(*data_ir_l));
  705. data_ir_r = av_malloc_array(n_conv * n_samples, sizeof(*data_ir_r));
  706. if (!data_ir_r || !data_ir_l || !s->temp_src[0] || !s->temp_src[1]) {
  707. av_free(data_ir_l);
  708. av_free(data_ir_r);
  709. return AVERROR(ENOMEM);
  710. }
  711. } else {
  712. /* get temporary HRTF memory for L and R channel */
  713. data_hrtf_l = av_malloc_array(n_fft, sizeof(*data_hrtf_l) * n_conv);
  714. data_hrtf_r = av_malloc_array(n_fft, sizeof(*data_hrtf_r) * n_conv);
  715. if (!data_hrtf_r || !data_hrtf_l) {
  716. av_free(data_hrtf_l);
  717. av_free(data_hrtf_r);
  718. return AVERROR(ENOMEM);
  719. }
  720. }
  721. for (i = 0; i < s->n_conv; i++) {
  722. /* load and store IRs and corresponding delays */
  723. azim = (int)(s->speaker_azim[i] + azim_orig) % 360;
  724. elev = (int)(s->speaker_elev[i] + elev_orig) % 90;
  725. /* get id of IR closest to desired position */
  726. m[i] = find_m(s, azim, elev, radius);
  727. /* load the delays associated with the current IRs */
  728. delay_l[i] = *(s->sofa.data_delay + 2 * m[i]);
  729. delay_r[i] = *(s->sofa.data_delay + 2 * m[i] + 1);
  730. if (s->type == TIME_DOMAIN) {
  731. offset = i * n_samples; /* no. samples already written */
  732. for (j = 0; j < n_samples; j++) {
  733. /* load reversed IRs of the specified source position
  734. * sample-by-sample for left and right ear; and apply gain */
  735. *(data_ir_l + offset + j) = /* left channel */
  736. *(s->sofa.data_ir + 2 * m[i] * n_samples + n_samples - 1 - j) * gain_lin;
  737. *(data_ir_r + offset + j) = /* right channel */
  738. *(s->sofa.data_ir + 2 * m[i] * n_samples + n_samples - 1 - j + n_samples) * gain_lin;
  739. }
  740. } else {
  741. fft_in_l = av_calloc(n_fft, sizeof(*fft_in_l));
  742. fft_in_r = av_calloc(n_fft, sizeof(*fft_in_r));
  743. if (!fft_in_l || !fft_in_r) {
  744. av_free(data_hrtf_l);
  745. av_free(data_hrtf_r);
  746. av_free(fft_in_l);
  747. av_free(fft_in_r);
  748. return AVERROR(ENOMEM);
  749. }
  750. offset = i * n_fft; /* no. samples already written */
  751. for (j = 0; j < n_samples; j++) {
  752. /* load non-reversed IRs of the specified source position
  753. * sample-by-sample and apply gain,
  754. * L channel is loaded to real part, R channel to imag part,
  755. * IRs ared shifted by L and R delay */
  756. fft_in_l[delay_l[i] + j].re = /* left channel */
  757. *(s->sofa.data_ir + 2 * m[i] * n_samples + j) * gain_lin;
  758. fft_in_r[delay_r[i] + j].re = /* right channel */
  759. *(s->sofa.data_ir + (2 * m[i] + 1) * n_samples + j) * gain_lin;
  760. }
  761. /* actually transform to frequency domain (IRs -> HRTFs) */
  762. av_fft_permute(s->fft[0], fft_in_l);
  763. av_fft_calc(s->fft[0], fft_in_l);
  764. memcpy(data_hrtf_l + offset, fft_in_l, n_fft * sizeof(*fft_in_l));
  765. av_fft_permute(s->fft[0], fft_in_r);
  766. av_fft_calc(s->fft[0], fft_in_r);
  767. memcpy(data_hrtf_r + offset, fft_in_r, n_fft * sizeof(*fft_in_r));
  768. }
  769. av_log(ctx, AV_LOG_DEBUG, "Index: %d, Azimuth: %f, Elevation: %f, Radius: %f of SOFA file.\n",
  770. m[i], *(s->sofa.sp_a + m[i]), *(s->sofa.sp_e + m[i]), *(s->sofa.sp_r + m[i]));
  771. }
  772. if (s->type == TIME_DOMAIN) {
  773. /* copy IRs and delays to allocated memory in the SOFAlizerContext struct: */
  774. memcpy(s->data_ir[0], data_ir_l, sizeof(float) * n_conv * n_samples);
  775. memcpy(s->data_ir[1], data_ir_r, sizeof(float) * n_conv * n_samples);
  776. av_freep(&data_ir_l); /* free temporary IR memory */
  777. av_freep(&data_ir_r);
  778. } else {
  779. s->data_hrtf[0] = av_malloc_array(n_fft * s->n_conv, sizeof(FFTComplex));
  780. s->data_hrtf[1] = av_malloc_array(n_fft * s->n_conv, sizeof(FFTComplex));
  781. if (!s->data_hrtf[0] || !s->data_hrtf[1]) {
  782. av_freep(&data_hrtf_l);
  783. av_freep(&data_hrtf_r);
  784. av_freep(&fft_in_l);
  785. av_freep(&fft_in_r);
  786. return AVERROR(ENOMEM); /* memory allocation failed */
  787. }
  788. memcpy(s->data_hrtf[0], data_hrtf_l, /* copy HRTF data to */
  789. sizeof(FFTComplex) * n_conv * n_fft); /* filter struct */
  790. memcpy(s->data_hrtf[1], data_hrtf_r,
  791. sizeof(FFTComplex) * n_conv * n_fft);
  792. av_freep(&data_hrtf_l); /* free temporary HRTF memory */
  793. av_freep(&data_hrtf_r);
  794. av_freep(&fft_in_l); /* free temporary FFT memory */
  795. av_freep(&fft_in_r);
  796. }
  797. memcpy(s->delay[0], &delay_l[0], sizeof(int) * s->n_conv);
  798. memcpy(s->delay[1], &delay_r[0], sizeof(int) * s->n_conv);
  799. return 0;
  800. }
  801. static av_cold int init(AVFilterContext *ctx)
  802. {
  803. SOFAlizerContext *s = ctx->priv;
  804. int ret;
  805. /* load SOFA file, */
  806. /* initialize file IDs to 0 before attempting to load SOFA files,
  807. * this assures that in case of error, only the memory of already
  808. * loaded files is free'd */
  809. s->sofa.ncid = 0;
  810. ret = load_sofa(ctx, s->filename, &s->sample_rate);
  811. if (ret) {
  812. /* file loading error */
  813. av_log(ctx, AV_LOG_ERROR, "Error while loading SOFA file: '%s'\n", s->filename);
  814. } else { /* no file loading error, resampling not required */
  815. av_log(ctx, AV_LOG_DEBUG, "File '%s' loaded.\n", s->filename);
  816. }
  817. if (ret) {
  818. av_log(ctx, AV_LOG_ERROR, "No valid SOFA file could be loaded. Please specify valid SOFA file.\n");
  819. return ret;
  820. }
  821. s->fdsp = avpriv_float_dsp_alloc(0);
  822. if (!s->fdsp)
  823. return AVERROR(ENOMEM);
  824. return 0;
  825. }
  826. static int config_input(AVFilterLink *inlink)
  827. {
  828. AVFilterContext *ctx = inlink->dst;
  829. SOFAlizerContext *s = ctx->priv;
  830. int nb_input_channels = inlink->channels; /* no. input channels */
  831. int n_max_ir = 0;
  832. int n_current;
  833. int n_max = 0;
  834. int ret;
  835. if (s->type == FREQUENCY_DOMAIN) {
  836. inlink->partial_buf_size =
  837. inlink->min_samples =
  838. inlink->max_samples = inlink->sample_rate;
  839. }
  840. /* gain -3 dB per channel, -6 dB to get LFE on a similar level */
  841. s->gain_lfe = expf((s->gain - 3 * inlink->channels - 6) / 20 * M_LN10);
  842. s->n_conv = nb_input_channels;
  843. /* get size of ringbuffer (longest IR plus max. delay) */
  844. /* then choose next power of 2 for performance optimization */
  845. n_current = s->sofa.n_samples + max_delay(&s->sofa);
  846. if (n_current > n_max) {
  847. /* length of longest IR plus max. delay (in all SOFA files) */
  848. n_max = n_current;
  849. /* length of longest IR (without delay, in all SOFA files) */
  850. n_max_ir = s->sofa.n_samples;
  851. }
  852. /* buffer length is longest IR plus max. delay -> next power of 2
  853. (32 - count leading zeros gives required exponent) */
  854. s->buffer_length = 1 << (32 - ff_clz(n_max));
  855. s->n_fft = 1 << (32 - ff_clz(n_max + inlink->sample_rate));
  856. if (s->type == FREQUENCY_DOMAIN) {
  857. av_fft_end(s->fft[0]);
  858. av_fft_end(s->fft[1]);
  859. s->fft[0] = av_fft_init(log2(s->n_fft), 0);
  860. s->fft[1] = av_fft_init(log2(s->n_fft), 0);
  861. av_fft_end(s->ifft[0]);
  862. av_fft_end(s->ifft[1]);
  863. s->ifft[0] = av_fft_init(log2(s->n_fft), 1);
  864. s->ifft[1] = av_fft_init(log2(s->n_fft), 1);
  865. if (!s->fft[0] || !s->fft[1] || !s->ifft[0] || !s->ifft[1]) {
  866. av_log(ctx, AV_LOG_ERROR, "Unable to create FFT contexts.\n");
  867. return AVERROR(ENOMEM);
  868. }
  869. }
  870. /* Allocate memory for the impulse responses, delays and the ringbuffers */
  871. /* size: (longest IR) * (number of channels to convolute) */
  872. s->data_ir[0] = av_malloc_array(n_max_ir, sizeof(float) * s->n_conv);
  873. s->data_ir[1] = av_malloc_array(n_max_ir, sizeof(float) * s->n_conv);
  874. /* length: number of channels to convolute */
  875. s->delay[0] = av_malloc_array(s->n_conv, sizeof(float));
  876. s->delay[1] = av_malloc_array(s->n_conv, sizeof(float));
  877. /* length: (buffer length) * (number of input channels),
  878. * OR: buffer length (if frequency domain processing)
  879. * calloc zero-initializes the buffer */
  880. if (s->type == TIME_DOMAIN) {
  881. s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
  882. s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
  883. } else {
  884. s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float));
  885. s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float));
  886. s->temp_fft[0] = av_malloc_array(s->n_fft, sizeof(FFTComplex));
  887. s->temp_fft[1] = av_malloc_array(s->n_fft, sizeof(FFTComplex));
  888. if (!s->temp_fft[0] || !s->temp_fft[1])
  889. return AVERROR(ENOMEM);
  890. }
  891. /* length: number of channels to convolute */
  892. s->speaker_azim = av_calloc(s->n_conv, sizeof(*s->speaker_azim));
  893. s->speaker_elev = av_calloc(s->n_conv, sizeof(*s->speaker_elev));
  894. /* memory allocation failed: */
  895. if (!s->data_ir[0] || !s->data_ir[1] || !s->delay[1] ||
  896. !s->delay[0] || !s->ringbuffer[0] || !s->ringbuffer[1] ||
  897. !s->speaker_azim || !s->speaker_elev)
  898. return AVERROR(ENOMEM);
  899. compensate_volume(ctx);
  900. /* get speaker positions */
  901. if ((ret = get_speaker_pos(ctx, s->speaker_azim, s->speaker_elev)) < 0) {
  902. av_log(ctx, AV_LOG_ERROR, "Couldn't get speaker positions. Input channel configuration not supported.\n");
  903. return ret;
  904. }
  905. /* load IRs to data_ir[0] and data_ir[1] for required directions */
  906. if ((ret = load_data(ctx, s->rotation, s->elevation, s->radius)) < 0)
  907. return ret;
  908. av_log(ctx, AV_LOG_DEBUG, "Samplerate: %d Channels to convolute: %d, Length of ringbuffer: %d x %d\n",
  909. inlink->sample_rate, s->n_conv, nb_input_channels, s->buffer_length);
  910. return 0;
  911. }
  912. static av_cold void uninit(AVFilterContext *ctx)
  913. {
  914. SOFAlizerContext *s = ctx->priv;
  915. if (s->sofa.ncid) {
  916. av_freep(&s->sofa.sp_a);
  917. av_freep(&s->sofa.sp_e);
  918. av_freep(&s->sofa.sp_r);
  919. av_freep(&s->sofa.data_delay);
  920. av_freep(&s->sofa.data_ir);
  921. }
  922. av_fft_end(s->ifft[0]);
  923. av_fft_end(s->ifft[1]);
  924. av_fft_end(s->fft[0]);
  925. av_fft_end(s->fft[1]);
  926. av_freep(&s->delay[0]);
  927. av_freep(&s->delay[1]);
  928. av_freep(&s->data_ir[0]);
  929. av_freep(&s->data_ir[1]);
  930. av_freep(&s->ringbuffer[0]);
  931. av_freep(&s->ringbuffer[1]);
  932. av_freep(&s->speaker_azim);
  933. av_freep(&s->speaker_elev);
  934. av_freep(&s->temp_src[0]);
  935. av_freep(&s->temp_src[1]);
  936. av_freep(&s->temp_fft[0]);
  937. av_freep(&s->temp_fft[1]);
  938. av_freep(&s->data_hrtf[0]);
  939. av_freep(&s->data_hrtf[1]);
  940. av_freep(&s->fdsp);
  941. }
  942. #define OFFSET(x) offsetof(SOFAlizerContext, x)
  943. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  944. static const AVOption sofalizer_options[] = {
  945. { "sofa", "sofa filename", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  946. { "gain", "set gain in dB", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, -20, 40, .flags = FLAGS },
  947. { "rotation", "set rotation" , OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl=0}, -360, 360, .flags = FLAGS },
  948. { "elevation", "set elevation", OFFSET(elevation), AV_OPT_TYPE_FLOAT, {.dbl=0}, -90, 90, .flags = FLAGS },
  949. { "radius", "set radius", OFFSET(radius), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 3, .flags = FLAGS },
  950. { "type", "set processing", OFFSET(type), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, .flags = FLAGS, "type" },
  951. { "time", "time domain", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, .flags = FLAGS, "type" },
  952. { "freq", "frequency domain", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, .flags = FLAGS, "type" },
  953. { NULL }
  954. };
  955. AVFILTER_DEFINE_CLASS(sofalizer);
  956. static const AVFilterPad inputs[] = {
  957. {
  958. .name = "default",
  959. .type = AVMEDIA_TYPE_AUDIO,
  960. .config_props = config_input,
  961. .filter_frame = filter_frame,
  962. },
  963. { NULL }
  964. };
  965. static const AVFilterPad outputs[] = {
  966. {
  967. .name = "default",
  968. .type = AVMEDIA_TYPE_AUDIO,
  969. },
  970. { NULL }
  971. };
  972. AVFilter ff_af_sofalizer = {
  973. .name = "sofalizer",
  974. .description = NULL_IF_CONFIG_SMALL("SOFAlizer (Spatially Oriented Format for Acoustics)."),
  975. .priv_size = sizeof(SOFAlizerContext),
  976. .priv_class = &sofalizer_class,
  977. .init = init,
  978. .uninit = uninit,
  979. .query_formats = query_formats,
  980. .inputs = inputs,
  981. .outputs = outputs,
  982. .flags = AVFILTER_FLAG_SLICE_THREADS,
  983. };