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.

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