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.

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