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.

1152 lines
44KB

  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_calloc(m_dim * FFALIGN(n_samples, 16), 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. int *data_delay_r;
  280. av_log(ctx, AV_LOG_DEBUG, "Data.Delay has dimension [I R]\n");
  281. status = nc_get_var_int(ncid, data_delay_id, &delay[0]);
  282. if (status != NC_NOERR) {
  283. av_log(ctx, AV_LOG_ERROR, "Couldn't read Data.Delay\n");
  284. ret = AVERROR(EINVAL);
  285. goto error;
  286. }
  287. data_delay_r = data_delay + m_dim;
  288. for (i = 0; i < m_dim; i++) { /* extend given dimension [I R] to [M R] */
  289. /* assign constant delay value for all measurements to data_delay fields */
  290. data_delay[i] = delay[0];
  291. data_delay_r[i] = delay[1];
  292. }
  293. /* dimension of Data.Delay is [M R] */
  294. } else if (!strncmp(data_delay_dim_name, "M", 2)) {
  295. av_log(ctx, AV_LOG_ERROR, "Data.Delay in dimension [M R]\n");
  296. /* get delays from SOFA file: */
  297. status = nc_get_var_int(ncid, data_delay_id, data_delay);
  298. if (status != NC_NOERR) {
  299. av_log(ctx, AV_LOG_ERROR, "Couldn't read Data.Delay\n");
  300. ret = AVERROR(EINVAL);
  301. goto error;
  302. }
  303. } else { /* dimension of Data.Delay is neither [I R] nor [M R] */
  304. av_log(ctx, AV_LOG_ERROR, "Data.Delay does not have the required dimensions [I R] or [M R].\n");
  305. ret = AVERROR(EINVAL);
  306. goto error;
  307. }
  308. /* save information in SOFA struct: */
  309. s->sofa.m_dim = m_dim; /* no. measurement positions */
  310. s->sofa.n_samples = n_samples; /* length on one IR */
  311. s->sofa.ncid = ncid; /* netCDF ID of SOFA file */
  312. nc_close(ncid); /* close SOFA file */
  313. av_log(ctx, AV_LOG_DEBUG, "m_dim: %d n_samples %d\n", m_dim, n_samples);
  314. return 0;
  315. error:
  316. close_sofa(&s->sofa);
  317. return ret;
  318. }
  319. static int get_speaker_pos(AVFilterContext *ctx,
  320. float *speaker_azim, float *speaker_elev)
  321. {
  322. struct SOFAlizerContext *s = ctx->priv;
  323. uint64_t channels_layout = ctx->inputs[0]->channel_layout;
  324. float azim[16] = { 0 };
  325. float elev[16] = { 0 };
  326. int m, ch, n_conv = ctx->inputs[0]->channels; /* get no. input channels */
  327. if (n_conv > 16)
  328. return AVERROR(EINVAL);
  329. s->lfe_channel = -1;
  330. /* set speaker positions according to input channel configuration: */
  331. for (m = 0, ch = 0; ch < n_conv && m < 64; m++) {
  332. uint64_t mask = channels_layout & (1 << m);
  333. switch (mask) {
  334. case AV_CH_FRONT_LEFT: azim[ch] = 30; break;
  335. case AV_CH_FRONT_RIGHT: azim[ch] = 330; break;
  336. case AV_CH_FRONT_CENTER: azim[ch] = 0; break;
  337. case AV_CH_LOW_FREQUENCY:
  338. case AV_CH_LOW_FREQUENCY_2: s->lfe_channel = ch; break;
  339. case AV_CH_BACK_LEFT: azim[ch] = 150; break;
  340. case AV_CH_BACK_RIGHT: azim[ch] = 210; break;
  341. case AV_CH_BACK_CENTER: azim[ch] = 180; break;
  342. case AV_CH_SIDE_LEFT: azim[ch] = 90; break;
  343. case AV_CH_SIDE_RIGHT: azim[ch] = 270; break;
  344. case AV_CH_FRONT_LEFT_OF_CENTER: azim[ch] = 15; break;
  345. case AV_CH_FRONT_RIGHT_OF_CENTER: azim[ch] = 345; break;
  346. case AV_CH_TOP_CENTER: azim[ch] = 0;
  347. elev[ch] = 90; break;
  348. case AV_CH_TOP_FRONT_LEFT: azim[ch] = 30;
  349. elev[ch] = 45; break;
  350. case AV_CH_TOP_FRONT_CENTER: azim[ch] = 0;
  351. elev[ch] = 45; break;
  352. case AV_CH_TOP_FRONT_RIGHT: azim[ch] = 330;
  353. elev[ch] = 45; break;
  354. case AV_CH_TOP_BACK_LEFT: azim[ch] = 150;
  355. elev[ch] = 45; break;
  356. case AV_CH_TOP_BACK_RIGHT: azim[ch] = 210;
  357. elev[ch] = 45; break;
  358. case AV_CH_TOP_BACK_CENTER: azim[ch] = 180;
  359. elev[ch] = 45; break;
  360. case AV_CH_WIDE_LEFT: azim[ch] = 90; break;
  361. case AV_CH_WIDE_RIGHT: azim[ch] = 270; break;
  362. case AV_CH_SURROUND_DIRECT_LEFT: azim[ch] = 90; break;
  363. case AV_CH_SURROUND_DIRECT_RIGHT: azim[ch] = 270; break;
  364. case AV_CH_STEREO_LEFT: azim[ch] = 90; break;
  365. case AV_CH_STEREO_RIGHT: azim[ch] = 270; break;
  366. case 0: break;
  367. default:
  368. return AVERROR(EINVAL);
  369. }
  370. if (mask)
  371. ch++;
  372. }
  373. memcpy(speaker_azim, azim, n_conv * sizeof(float));
  374. memcpy(speaker_elev, elev, n_conv * sizeof(float));
  375. return 0;
  376. }
  377. static int max_delay(struct NCSofa *sofa)
  378. {
  379. int i, max = 0;
  380. for (i = 0; i < sofa->m_dim * 2; i++) {
  381. /* search maximum delay in given SOFA file */
  382. max = FFMAX(max, sofa->data_delay[i]);
  383. }
  384. return max;
  385. }
  386. static int find_m(SOFAlizerContext *s, int azim, int elev, float radius)
  387. {
  388. /* get source positions and M of currently selected SOFA file */
  389. float *sp_a = s->sofa.sp_a; /* azimuth angle */
  390. float *sp_e = s->sofa.sp_e; /* elevation angle */
  391. float *sp_r = s->sofa.sp_r; /* radius */
  392. int m_dim = s->sofa.m_dim; /* no. measurements */
  393. int best_id = 0; /* index m currently closest to desired source pos. */
  394. float delta = 1000; /* offset between desired and currently best pos. */
  395. float current;
  396. int i;
  397. for (i = 0; i < m_dim; i++) {
  398. /* search through all measurements in currently selected SOFA file */
  399. /* distance of current to desired source position: */
  400. current = fabs(sp_a[i] - azim) +
  401. fabs(sp_e[i] - elev) +
  402. fabs(sp_r[i] - radius);
  403. if (current <= delta) {
  404. /* if current distance is smaller than smallest distance so far */
  405. delta = current;
  406. best_id = i; /* remember index */
  407. }
  408. }
  409. return best_id;
  410. }
  411. static int compensate_volume(AVFilterContext *ctx)
  412. {
  413. struct SOFAlizerContext *s = ctx->priv;
  414. float compensate;
  415. float energy = 0;
  416. float *ir;
  417. int m;
  418. if (s->sofa.ncid) {
  419. /* find IR at front center position in the SOFA file (IR closest to 0°,0°,1m) */
  420. struct NCSofa *sofa = &s->sofa;
  421. m = find_m(s, 0, 0, 1);
  422. /* get energy of that IR and compensate volume */
  423. ir = sofa->data_ir + 2 * m * sofa->n_samples;
  424. if (sofa->n_samples & 31) {
  425. energy = avpriv_scalarproduct_float_c(ir, ir, sofa->n_samples);
  426. } else {
  427. energy = s->fdsp->scalarproduct_float(ir, ir, sofa->n_samples);
  428. }
  429. compensate = 256 / (sofa->n_samples * sqrt(energy));
  430. av_log(ctx, AV_LOG_DEBUG, "Compensate-factor: %f\n", compensate);
  431. ir = sofa->data_ir;
  432. /* apply volume compensation to IRs */
  433. if (sofa->n_samples & 31) {
  434. int i;
  435. for (i = 0; i < sofa->n_samples * sofa->m_dim * 2; i++) {
  436. ir[i] = ir[i] * compensate;
  437. }
  438. } else {
  439. s->fdsp->vector_fmul_scalar(ir, ir, compensate, sofa->n_samples * sofa->m_dim * 2);
  440. emms_c();
  441. }
  442. }
  443. return 0;
  444. }
  445. typedef struct ThreadData {
  446. AVFrame *in, *out;
  447. int *write;
  448. int **delay;
  449. float **ir;
  450. int *n_clippings;
  451. float **ringbuffer;
  452. float **temp_src;
  453. FFTComplex **temp_fft;
  454. } ThreadData;
  455. static int sofalizer_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  456. {
  457. SOFAlizerContext *s = ctx->priv;
  458. ThreadData *td = arg;
  459. AVFrame *in = td->in, *out = td->out;
  460. int offset = jobnr;
  461. int *write = &td->write[jobnr];
  462. const int *const delay = td->delay[jobnr];
  463. const float *const ir = td->ir[jobnr];
  464. int *n_clippings = &td->n_clippings[jobnr];
  465. float *ringbuffer = td->ringbuffer[jobnr];
  466. float *temp_src = td->temp_src[jobnr];
  467. const int n_samples = s->sofa.n_samples; /* length of one IR */
  468. const float *src = (const float *)in->data[0]; /* get pointer to audio input buffer */
  469. float *dst = (float *)out->data[0]; /* get pointer to audio output buffer */
  470. const int in_channels = s->n_conv; /* number of input channels */
  471. /* ring buffer length is: longest IR plus max. delay -> next power of 2 */
  472. const int buffer_length = s->buffer_length;
  473. /* -1 for AND instead of MODULO (applied to powers of 2): */
  474. const uint32_t modulo = (uint32_t)buffer_length - 1;
  475. float *buffer[16]; /* holds ringbuffer for each input channel */
  476. int wr = *write;
  477. int read;
  478. int i, l;
  479. dst += offset;
  480. for (l = 0; l < in_channels; l++) {
  481. /* get starting address of ringbuffer for each input channel */
  482. buffer[l] = ringbuffer + l * buffer_length;
  483. }
  484. for (i = 0; i < in->nb_samples; i++) {
  485. const float *temp_ir = ir; /* using same set of IRs for each sample */
  486. *dst = 0;
  487. for (l = 0; l < in_channels; l++) {
  488. /* write current input sample to ringbuffer (for each channel) */
  489. *(buffer[l] + wr) = src[l];
  490. }
  491. /* loop goes through all channels to be convolved */
  492. for (l = 0; l < in_channels; l++) {
  493. const float *const bptr = buffer[l];
  494. if (l == s->lfe_channel) {
  495. /* LFE is an input channel but requires no convolution */
  496. /* apply gain to LFE signal and add to output buffer */
  497. *dst += *(buffer[s->lfe_channel] + wr) * s->gain_lfe;
  498. temp_ir += FFALIGN(n_samples, 16);
  499. continue;
  500. }
  501. /* current read position in ringbuffer: input sample write position
  502. * - delay for l-th ch. + diff. betw. IR length and buffer length
  503. * (mod buffer length) */
  504. read = (wr - *(delay + l) - (n_samples - 1) + buffer_length) & modulo;
  505. if (read + n_samples < buffer_length) {
  506. memcpy(temp_src, bptr + read, n_samples * sizeof(*temp_src));
  507. } else {
  508. int len = FFMIN(n_samples - (read % n_samples), buffer_length - read);
  509. memcpy(temp_src, bptr + read, len * sizeof(*temp_src));
  510. memcpy(temp_src + len, bptr, (n_samples - len) * sizeof(*temp_src));
  511. }
  512. /* multiply signal and IR, and add up the results */
  513. dst[0] += s->fdsp->scalarproduct_float(temp_ir, temp_src, n_samples);
  514. temp_ir += FFALIGN(n_samples, 16);
  515. }
  516. /* clippings counter */
  517. if (fabs(*dst) > 1)
  518. *n_clippings += 1;
  519. /* move output buffer pointer by +2 to get to next sample of processed channel: */
  520. dst += 2;
  521. src += in_channels;
  522. wr = (wr + 1) & modulo; /* update ringbuffer write position */
  523. }
  524. *write = wr; /* remember write position in ringbuffer for next call */
  525. return 0;
  526. }
  527. static int sofalizer_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  528. {
  529. SOFAlizerContext *s = ctx->priv;
  530. ThreadData *td = arg;
  531. AVFrame *in = td->in, *out = td->out;
  532. int offset = jobnr;
  533. int *write = &td->write[jobnr];
  534. FFTComplex *hrtf = s->data_hrtf[jobnr]; /* get pointers to current HRTF data */
  535. int *n_clippings = &td->n_clippings[jobnr];
  536. float *ringbuffer = td->ringbuffer[jobnr];
  537. const int n_samples = s->sofa.n_samples; /* length of one IR */
  538. const float *src = (const float *)in->data[0]; /* get pointer to audio input buffer */
  539. float *dst = (float *)out->data[0]; /* get pointer to audio output buffer */
  540. const int in_channels = s->n_conv; /* number of input channels */
  541. /* ring buffer length is: longest IR plus max. delay -> next power of 2 */
  542. const int buffer_length = s->buffer_length;
  543. /* -1 for AND instead of MODULO (applied to powers of 2): */
  544. const uint32_t modulo = (uint32_t)buffer_length - 1;
  545. FFTComplex *fft_in = s->temp_fft[jobnr]; /* temporary array for FFT input/output data */
  546. FFTContext *ifft = s->ifft[jobnr];
  547. FFTContext *fft = s->fft[jobnr];
  548. const int n_conv = s->n_conv;
  549. const int n_fft = s->n_fft;
  550. int wr = *write;
  551. int n_read;
  552. int i, j;
  553. dst += offset;
  554. /* find minimum between number of samples and output buffer length:
  555. * (important, if one IR is longer than the output buffer) */
  556. n_read = FFMIN(s->sofa.n_samples, in->nb_samples);
  557. for (j = 0; j < n_read; j++) {
  558. /* initialize output buf with saved signal from overflow buf */
  559. dst[2 * j] = ringbuffer[wr];
  560. ringbuffer[wr] = 0.0; /* re-set read samples to zero */
  561. /* update ringbuffer read/write position */
  562. wr = (wr + 1) & modulo;
  563. }
  564. /* initialize rest of output buffer with 0 */
  565. for (j = n_read; j < in->nb_samples; j++) {
  566. dst[2 * j] = 0;
  567. }
  568. for (i = 0; i < n_conv; i++) {
  569. if (i == s->lfe_channel) { /* LFE */
  570. for (j = 0; j < in->nb_samples; j++) {
  571. /* apply gain to LFE signal and add to output buffer */
  572. dst[2 * j] += src[i + j * in_channels] * s->gain_lfe;
  573. }
  574. continue;
  575. }
  576. /* outer loop: go through all input channels to be convolved */
  577. offset = i * n_fft; /* no. samples already processed */
  578. /* fill FFT input with 0 (we want to zero-pad) */
  579. memset(fft_in, 0, sizeof(FFTComplex) * n_fft);
  580. for (j = 0; j < in->nb_samples; j++) {
  581. /* prepare input for FFT */
  582. /* write all samples of current input channel to FFT input array */
  583. fft_in[j].re = src[j * in_channels + i];
  584. }
  585. /* transform input signal of current channel to frequency domain */
  586. av_fft_permute(fft, fft_in);
  587. av_fft_calc(fft, fft_in);
  588. for (j = 0; j < n_fft; j++) {
  589. const float re = fft_in[j].re;
  590. const float im = fft_in[j].im;
  591. /* complex multiplication of input signal and HRTFs */
  592. /* output channel (real): */
  593. fft_in[j].re = re * (hrtf + offset + j)->re - im * (hrtf + offset + j)->im;
  594. /* output channel (imag): */
  595. fft_in[j].im = re * (hrtf + offset + j)->im + im * (hrtf + offset + j)->re;
  596. }
  597. /* transform output signal of current channel back to time domain */
  598. av_fft_permute(ifft, fft_in);
  599. av_fft_calc(ifft, fft_in);
  600. for (j = 0; j < in->nb_samples; j++) {
  601. /* write output signal of current channel to output buffer */
  602. dst[2 * j] += fft_in[j].re / (float)n_fft;
  603. }
  604. for (j = 0; j < n_samples - 1; j++) { /* overflow length is IR length - 1 */
  605. /* write the rest of output signal to overflow buffer */
  606. int write_pos = (wr + j) & modulo;
  607. *(ringbuffer + write_pos) += fft_in[in->nb_samples + j].re / (float)n_fft;
  608. }
  609. }
  610. /* go through all samples of current output buffer: count clippings */
  611. for (i = 0; i < out->nb_samples; i++) {
  612. /* clippings counter */
  613. if (fabs(*dst) > 1) { /* if current output sample > 1 */
  614. *n_clippings = *n_clippings + 1;
  615. }
  616. /* move output buffer pointer by +2 to get to next sample of processed channel: */
  617. dst += 2;
  618. }
  619. /* remember read/write position in ringbuffer for next call */
  620. *write = wr;
  621. return 0;
  622. }
  623. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  624. {
  625. AVFilterContext *ctx = inlink->dst;
  626. SOFAlizerContext *s = ctx->priv;
  627. AVFilterLink *outlink = ctx->outputs[0];
  628. int n_clippings[2] = { 0 };
  629. ThreadData td;
  630. AVFrame *out;
  631. out = ff_get_audio_buffer(outlink, in->nb_samples);
  632. if (!out) {
  633. av_frame_free(&in);
  634. return AVERROR(ENOMEM);
  635. }
  636. av_frame_copy_props(out, in);
  637. td.in = in; td.out = out; td.write = s->write;
  638. td.delay = s->delay; td.ir = s->data_ir; td.n_clippings = n_clippings;
  639. td.ringbuffer = s->ringbuffer; td.temp_src = s->temp_src;
  640. td.temp_fft = s->temp_fft;
  641. if (s->type == TIME_DOMAIN) {
  642. ctx->internal->execute(ctx, sofalizer_convolute, &td, NULL, 2);
  643. } else {
  644. ctx->internal->execute(ctx, sofalizer_fast_convolute, &td, NULL, 2);
  645. }
  646. emms_c();
  647. /* display error message if clipping occurred */
  648. if (n_clippings[0] + n_clippings[1] > 0) {
  649. av_log(ctx, AV_LOG_WARNING, "%d of %d samples clipped. Please reduce gain.\n",
  650. n_clippings[0] + n_clippings[1], out->nb_samples * 2);
  651. }
  652. av_frame_free(&in);
  653. return ff_filter_frame(outlink, out);
  654. }
  655. static int query_formats(AVFilterContext *ctx)
  656. {
  657. struct SOFAlizerContext *s = ctx->priv;
  658. AVFilterFormats *formats = NULL;
  659. AVFilterChannelLayouts *layouts = NULL;
  660. int ret, sample_rates[] = { 48000, -1 };
  661. ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLT);
  662. if (ret)
  663. return ret;
  664. ret = ff_set_common_formats(ctx, formats);
  665. if (ret)
  666. return ret;
  667. layouts = ff_all_channel_layouts();
  668. if (!layouts)
  669. return AVERROR(ENOMEM);
  670. ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts);
  671. if (ret)
  672. return ret;
  673. layouts = NULL;
  674. ret = ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO);
  675. if (ret)
  676. return ret;
  677. ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
  678. if (ret)
  679. return ret;
  680. sample_rates[0] = s->sample_rate;
  681. formats = ff_make_format_list(sample_rates);
  682. if (!formats)
  683. return AVERROR(ENOMEM);
  684. return ff_set_common_samplerates(ctx, formats);
  685. }
  686. static int load_data(AVFilterContext *ctx, int azim, int elev, float radius)
  687. {
  688. struct SOFAlizerContext *s = ctx->priv;
  689. const int n_samples = s->sofa.n_samples;
  690. int n_conv = s->n_conv; /* no. channels to convolve */
  691. int n_fft = s->n_fft;
  692. int delay_l[16]; /* broadband delay for each IR */
  693. int delay_r[16];
  694. int nb_input_channels = ctx->inputs[0]->channels; /* no. input channels */
  695. float gain_lin = expf((s->gain - 3 * nb_input_channels) / 20 * M_LN10); /* gain - 3dB/channel */
  696. FFTComplex *data_hrtf_l = NULL;
  697. FFTComplex *data_hrtf_r = NULL;
  698. FFTComplex *fft_in_l = NULL;
  699. FFTComplex *fft_in_r = NULL;
  700. float *data_ir_l = NULL;
  701. float *data_ir_r = NULL;
  702. int offset = 0; /* used for faster pointer arithmetics in for-loop */
  703. int m[16]; /* measurement index m of IR closest to required source positions */
  704. int i, j, azim_orig = azim, elev_orig = elev;
  705. if (!s->sofa.ncid) { /* if an invalid SOFA file has been selected */
  706. av_log(ctx, AV_LOG_ERROR, "Selected SOFA file is invalid. Please select valid SOFA file.\n");
  707. return AVERROR_INVALIDDATA;
  708. }
  709. if (s->type == TIME_DOMAIN) {
  710. s->temp_src[0] = av_calloc(FFALIGN(n_samples, 16), sizeof(float));
  711. s->temp_src[1] = av_calloc(FFALIGN(n_samples, 16), sizeof(float));
  712. /* get temporary IR for L and R channel */
  713. data_ir_l = av_calloc(n_conv * FFALIGN(n_samples, 16), sizeof(*data_ir_l));
  714. data_ir_r = av_calloc(n_conv * FFALIGN(n_samples, 16), sizeof(*data_ir_r));
  715. if (!data_ir_r || !data_ir_l || !s->temp_src[0] || !s->temp_src[1]) {
  716. av_free(data_ir_l);
  717. av_free(data_ir_r);
  718. return AVERROR(ENOMEM);
  719. }
  720. } else {
  721. /* get temporary HRTF memory for L and R channel */
  722. data_hrtf_l = av_malloc_array(n_fft, sizeof(*data_hrtf_l) * n_conv);
  723. data_hrtf_r = av_malloc_array(n_fft, sizeof(*data_hrtf_r) * n_conv);
  724. if (!data_hrtf_r || !data_hrtf_l) {
  725. av_free(data_hrtf_l);
  726. av_free(data_hrtf_r);
  727. return AVERROR(ENOMEM);
  728. }
  729. }
  730. for (i = 0; i < s->n_conv; i++) {
  731. /* load and store IRs and corresponding delays */
  732. azim = (int)(s->speaker_azim[i] + azim_orig) % 360;
  733. elev = (int)(s->speaker_elev[i] + elev_orig) % 90;
  734. /* get id of IR closest to desired position */
  735. m[i] = find_m(s, azim, elev, radius);
  736. /* load the delays associated with the current IRs */
  737. delay_l[i] = *(s->sofa.data_delay + 2 * m[i]);
  738. delay_r[i] = *(s->sofa.data_delay + 2 * m[i] + 1);
  739. if (s->type == TIME_DOMAIN) {
  740. offset = i * FFALIGN(n_samples, 16); /* no. samples already written */
  741. for (j = 0; j < n_samples; j++) {
  742. /* load reversed IRs of the specified source position
  743. * sample-by-sample for left and right ear; and apply gain */
  744. *(data_ir_l + offset + j) = /* left channel */
  745. *(s->sofa.data_ir + 2 * m[i] * n_samples + n_samples - 1 - j) * gain_lin;
  746. *(data_ir_r + offset + j) = /* right channel */
  747. *(s->sofa.data_ir + 2 * m[i] * n_samples + n_samples - 1 - j + n_samples) * gain_lin;
  748. }
  749. } else {
  750. fft_in_l = av_calloc(n_fft, sizeof(*fft_in_l));
  751. fft_in_r = av_calloc(n_fft, sizeof(*fft_in_r));
  752. if (!fft_in_l || !fft_in_r) {
  753. av_free(data_hrtf_l);
  754. av_free(data_hrtf_r);
  755. av_free(fft_in_l);
  756. av_free(fft_in_r);
  757. return AVERROR(ENOMEM);
  758. }
  759. offset = i * n_fft; /* no. samples already written */
  760. for (j = 0; j < n_samples; j++) {
  761. /* load non-reversed IRs of the specified source position
  762. * sample-by-sample and apply gain,
  763. * L channel is loaded to real part, R channel to imag part,
  764. * IRs ared shifted by L and R delay */
  765. fft_in_l[delay_l[i] + j].re = /* left channel */
  766. *(s->sofa.data_ir + 2 * m[i] * n_samples + j) * gain_lin;
  767. fft_in_r[delay_r[i] + j].re = /* right channel */
  768. *(s->sofa.data_ir + (2 * m[i] + 1) * n_samples + j) * gain_lin;
  769. }
  770. /* actually transform to frequency domain (IRs -> HRTFs) */
  771. av_fft_permute(s->fft[0], fft_in_l);
  772. av_fft_calc(s->fft[0], fft_in_l);
  773. memcpy(data_hrtf_l + offset, fft_in_l, n_fft * sizeof(*fft_in_l));
  774. av_fft_permute(s->fft[0], fft_in_r);
  775. av_fft_calc(s->fft[0], fft_in_r);
  776. memcpy(data_hrtf_r + offset, fft_in_r, n_fft * sizeof(*fft_in_r));
  777. }
  778. av_log(ctx, AV_LOG_DEBUG, "Index: %d, Azimuth: %f, Elevation: %f, Radius: %f of SOFA file.\n",
  779. m[i], *(s->sofa.sp_a + m[i]), *(s->sofa.sp_e + m[i]), *(s->sofa.sp_r + m[i]));
  780. }
  781. if (s->type == TIME_DOMAIN) {
  782. /* copy IRs and delays to allocated memory in the SOFAlizerContext struct: */
  783. memcpy(s->data_ir[0], data_ir_l, sizeof(float) * n_conv * FFALIGN(n_samples, 16));
  784. memcpy(s->data_ir[1], data_ir_r, sizeof(float) * n_conv * FFALIGN(n_samples, 16));
  785. av_freep(&data_ir_l); /* free temporary IR memory */
  786. av_freep(&data_ir_r);
  787. } else {
  788. s->data_hrtf[0] = av_malloc_array(n_fft * s->n_conv, sizeof(FFTComplex));
  789. s->data_hrtf[1] = av_malloc_array(n_fft * s->n_conv, sizeof(FFTComplex));
  790. if (!s->data_hrtf[0] || !s->data_hrtf[1]) {
  791. av_freep(&data_hrtf_l);
  792. av_freep(&data_hrtf_r);
  793. av_freep(&fft_in_l);
  794. av_freep(&fft_in_r);
  795. return AVERROR(ENOMEM); /* memory allocation failed */
  796. }
  797. memcpy(s->data_hrtf[0], data_hrtf_l, /* copy HRTF data to */
  798. sizeof(FFTComplex) * n_conv * n_fft); /* filter struct */
  799. memcpy(s->data_hrtf[1], data_hrtf_r,
  800. sizeof(FFTComplex) * n_conv * n_fft);
  801. av_freep(&data_hrtf_l); /* free temporary HRTF memory */
  802. av_freep(&data_hrtf_r);
  803. av_freep(&fft_in_l); /* free temporary FFT memory */
  804. av_freep(&fft_in_r);
  805. }
  806. memcpy(s->delay[0], &delay_l[0], sizeof(int) * s->n_conv);
  807. memcpy(s->delay[1], &delay_r[0], sizeof(int) * s->n_conv);
  808. return 0;
  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. /* load SOFA file, */
  819. /* initialize file IDs to 0 before attempting to load SOFA files,
  820. * this assures that in case of error, only the memory of already
  821. * loaded files is free'd */
  822. s->sofa.ncid = 0;
  823. ret = load_sofa(ctx, s->filename, &s->sample_rate);
  824. if (ret) {
  825. /* file loading error */
  826. av_log(ctx, AV_LOG_ERROR, "Error while loading SOFA file: '%s'\n", s->filename);
  827. } else { /* no file loading error, resampling not required */
  828. av_log(ctx, AV_LOG_DEBUG, "File '%s' loaded.\n", s->filename);
  829. }
  830. if (ret) {
  831. av_log(ctx, AV_LOG_ERROR, "No valid SOFA file could be loaded. Please specify valid SOFA file.\n");
  832. return ret;
  833. }
  834. s->fdsp = avpriv_float_dsp_alloc(0);
  835. if (!s->fdsp)
  836. return AVERROR(ENOMEM);
  837. return 0;
  838. }
  839. static int config_input(AVFilterLink *inlink)
  840. {
  841. AVFilterContext *ctx = inlink->dst;
  842. SOFAlizerContext *s = ctx->priv;
  843. int nb_input_channels = inlink->channels; /* no. input channels */
  844. int n_max_ir = 0;
  845. int n_current;
  846. int n_max = 0;
  847. int ret;
  848. if (s->type == FREQUENCY_DOMAIN) {
  849. inlink->partial_buf_size =
  850. inlink->min_samples =
  851. inlink->max_samples = inlink->sample_rate;
  852. }
  853. /* gain -3 dB per channel, -6 dB to get LFE on a similar level */
  854. s->gain_lfe = expf((s->gain - 3 * inlink->channels - 6) / 20 * M_LN10);
  855. s->n_conv = nb_input_channels;
  856. /* get size of ringbuffer (longest IR plus max. delay) */
  857. /* then choose next power of 2 for performance optimization */
  858. n_current = s->sofa.n_samples + max_delay(&s->sofa);
  859. if (n_current > n_max) {
  860. /* length of longest IR plus max. delay (in all SOFA files) */
  861. n_max = n_current;
  862. /* length of longest IR (without delay, in all SOFA files) */
  863. n_max_ir = s->sofa.n_samples;
  864. }
  865. /* buffer length is longest IR plus max. delay -> next power of 2
  866. (32 - count leading zeros gives required exponent) */
  867. s->buffer_length = 1 << (32 - ff_clz(n_max));
  868. s->n_fft = 1 << (32 - ff_clz(n_max + inlink->sample_rate));
  869. if (s->type == FREQUENCY_DOMAIN) {
  870. av_fft_end(s->fft[0]);
  871. av_fft_end(s->fft[1]);
  872. s->fft[0] = av_fft_init(log2(s->n_fft), 0);
  873. s->fft[1] = av_fft_init(log2(s->n_fft), 0);
  874. av_fft_end(s->ifft[0]);
  875. av_fft_end(s->ifft[1]);
  876. s->ifft[0] = av_fft_init(log2(s->n_fft), 1);
  877. s->ifft[1] = av_fft_init(log2(s->n_fft), 1);
  878. if (!s->fft[0] || !s->fft[1] || !s->ifft[0] || !s->ifft[1]) {
  879. av_log(ctx, AV_LOG_ERROR, "Unable to create FFT contexts of size %d.\n", s->n_fft);
  880. return AVERROR(ENOMEM);
  881. }
  882. }
  883. /* Allocate memory for the impulse responses, delays and the ringbuffers */
  884. /* size: (longest IR) * (number of channels to convolute) */
  885. s->data_ir[0] = av_calloc(FFALIGN(n_max_ir, 16), sizeof(float) * s->n_conv);
  886. s->data_ir[1] = av_calloc(FFALIGN(n_max_ir, 16), sizeof(float) * s->n_conv);
  887. /* length: number of channels to convolute */
  888. s->delay[0] = av_malloc_array(s->n_conv, sizeof(float));
  889. s->delay[1] = av_malloc_array(s->n_conv, sizeof(float));
  890. /* length: (buffer length) * (number of input channels),
  891. * OR: buffer length (if frequency domain processing)
  892. * calloc zero-initializes the buffer */
  893. if (s->type == TIME_DOMAIN) {
  894. s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
  895. s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
  896. } else {
  897. s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float));
  898. s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float));
  899. s->temp_fft[0] = av_malloc_array(s->n_fft, sizeof(FFTComplex));
  900. s->temp_fft[1] = av_malloc_array(s->n_fft, sizeof(FFTComplex));
  901. if (!s->temp_fft[0] || !s->temp_fft[1])
  902. return AVERROR(ENOMEM);
  903. }
  904. /* length: number of channels to convolute */
  905. s->speaker_azim = av_calloc(s->n_conv, sizeof(*s->speaker_azim));
  906. s->speaker_elev = av_calloc(s->n_conv, sizeof(*s->speaker_elev));
  907. /* memory allocation failed: */
  908. if (!s->data_ir[0] || !s->data_ir[1] || !s->delay[1] ||
  909. !s->delay[0] || !s->ringbuffer[0] || !s->ringbuffer[1] ||
  910. !s->speaker_azim || !s->speaker_elev)
  911. return AVERROR(ENOMEM);
  912. compensate_volume(ctx);
  913. /* get speaker positions */
  914. if ((ret = get_speaker_pos(ctx, s->speaker_azim, s->speaker_elev)) < 0) {
  915. av_log(ctx, AV_LOG_ERROR, "Couldn't get speaker positions. Input channel configuration not supported.\n");
  916. return ret;
  917. }
  918. /* load IRs to data_ir[0] and data_ir[1] for required directions */
  919. if ((ret = load_data(ctx, s->rotation, s->elevation, s->radius)) < 0)
  920. return ret;
  921. av_log(ctx, AV_LOG_DEBUG, "Samplerate: %d Channels to convolute: %d, Length of ringbuffer: %d x %d\n",
  922. inlink->sample_rate, s->n_conv, nb_input_channels, s->buffer_length);
  923. return 0;
  924. }
  925. static av_cold void uninit(AVFilterContext *ctx)
  926. {
  927. SOFAlizerContext *s = ctx->priv;
  928. if (s->sofa.ncid) {
  929. av_freep(&s->sofa.sp_a);
  930. av_freep(&s->sofa.sp_e);
  931. av_freep(&s->sofa.sp_r);
  932. av_freep(&s->sofa.data_delay);
  933. av_freep(&s->sofa.data_ir);
  934. }
  935. av_fft_end(s->ifft[0]);
  936. av_fft_end(s->ifft[1]);
  937. av_fft_end(s->fft[0]);
  938. av_fft_end(s->fft[1]);
  939. av_freep(&s->delay[0]);
  940. av_freep(&s->delay[1]);
  941. av_freep(&s->data_ir[0]);
  942. av_freep(&s->data_ir[1]);
  943. av_freep(&s->ringbuffer[0]);
  944. av_freep(&s->ringbuffer[1]);
  945. av_freep(&s->speaker_azim);
  946. av_freep(&s->speaker_elev);
  947. av_freep(&s->temp_src[0]);
  948. av_freep(&s->temp_src[1]);
  949. av_freep(&s->temp_fft[0]);
  950. av_freep(&s->temp_fft[1]);
  951. av_freep(&s->data_hrtf[0]);
  952. av_freep(&s->data_hrtf[1]);
  953. av_freep(&s->fdsp);
  954. }
  955. #define OFFSET(x) offsetof(SOFAlizerContext, x)
  956. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  957. static const AVOption sofalizer_options[] = {
  958. { "sofa", "sofa filename", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  959. { "gain", "set gain in dB", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, -20, 40, .flags = FLAGS },
  960. { "rotation", "set rotation" , OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl=0}, -360, 360, .flags = FLAGS },
  961. { "elevation", "set elevation", OFFSET(elevation), AV_OPT_TYPE_FLOAT, {.dbl=0}, -90, 90, .flags = FLAGS },
  962. { "radius", "set radius", OFFSET(radius), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 3, .flags = FLAGS },
  963. { "type", "set processing", OFFSET(type), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, .flags = FLAGS, "type" },
  964. { "time", "time domain", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, .flags = FLAGS, "type" },
  965. { "freq", "frequency domain", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, .flags = FLAGS, "type" },
  966. { NULL }
  967. };
  968. AVFILTER_DEFINE_CLASS(sofalizer);
  969. static const AVFilterPad inputs[] = {
  970. {
  971. .name = "default",
  972. .type = AVMEDIA_TYPE_AUDIO,
  973. .config_props = config_input,
  974. .filter_frame = filter_frame,
  975. },
  976. { NULL }
  977. };
  978. static const AVFilterPad outputs[] = {
  979. {
  980. .name = "default",
  981. .type = AVMEDIA_TYPE_AUDIO,
  982. },
  983. { NULL }
  984. };
  985. AVFilter ff_af_sofalizer = {
  986. .name = "sofalizer",
  987. .description = NULL_IF_CONFIG_SMALL("SOFAlizer (Spatially Oriented Format for Acoustics)."),
  988. .priv_size = sizeof(SOFAlizerContext),
  989. .priv_class = &sofalizer_class,
  990. .init = init,
  991. .uninit = uninit,
  992. .query_formats = query_formats,
  993. .inputs = inputs,
  994. .outputs = outputs,
  995. .flags = AVFILTER_FLAG_SLICE_THREADS,
  996. };