af_headphone.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. * Copyright (C) 2017 Paul B Mahol
  3. * Copyright (C) 2013-2015 Andreas Fuchs, Wolfgang Hrauda
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <math.h>
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/channel_layout.h"
  23. #include "libavutil/float_dsp.h"
  24. #include "libavutil/intmath.h"
  25. #include "libavutil/opt.h"
  26. #include "libavcodec/avfft.h"
  27. #include "avfilter.h"
  28. #include "filters.h"
  29. #include "internal.h"
  30. #include "audio.h"
  31. #define TIME_DOMAIN 0
  32. #define FREQUENCY_DOMAIN 1
  33. #define HRIR_STEREO 0
  34. #define HRIR_MULTI 1
  35. typedef struct HeadphoneContext {
  36. const AVClass *class;
  37. char *map;
  38. int type;
  39. int lfe_channel;
  40. int have_hrirs;
  41. int eof_hrirs;
  42. int ir_len;
  43. int air_len;
  44. int mapping[64];
  45. int nb_inputs;
  46. int nb_irs;
  47. float gain;
  48. float lfe_gain, gain_lfe;
  49. float *ringbuffer[2];
  50. int write[2];
  51. int buffer_length;
  52. int n_fft;
  53. int size;
  54. int hrir_fmt;
  55. int *delay[2];
  56. float *data_ir[2];
  57. float *temp_src[2];
  58. FFTComplex *temp_fft[2];
  59. FFTComplex *temp_afft[2];
  60. FFTContext *fft[2], *ifft[2];
  61. FFTComplex *data_hrtf[2];
  62. AVFloatDSPContext *fdsp;
  63. struct headphone_inputs {
  64. AVFrame *frame;
  65. int ir_len;
  66. int delay_l;
  67. int delay_r;
  68. int eof;
  69. } *in;
  70. } HeadphoneContext;
  71. static int parse_channel_name(HeadphoneContext *s, int x, char **arg, int *rchannel, char *buf)
  72. {
  73. int len, i, channel_id = 0;
  74. int64_t layout, layout0;
  75. if (sscanf(*arg, "%7[A-Z]%n", buf, &len)) {
  76. layout0 = layout = av_get_channel_layout(buf);
  77. if (layout == AV_CH_LOW_FREQUENCY)
  78. s->lfe_channel = x;
  79. for (i = 32; i > 0; i >>= 1) {
  80. if (layout >= 1LL << i) {
  81. channel_id += i;
  82. layout >>= i;
  83. }
  84. }
  85. if (channel_id >= 64 || layout0 != 1LL << channel_id)
  86. return AVERROR(EINVAL);
  87. *rchannel = channel_id;
  88. *arg += len;
  89. return 0;
  90. }
  91. return AVERROR(EINVAL);
  92. }
  93. static void parse_map(AVFilterContext *ctx)
  94. {
  95. HeadphoneContext *s = ctx->priv;
  96. char *arg, *tokenizer, *p, *args = av_strdup(s->map);
  97. int i;
  98. if (!args)
  99. return;
  100. p = args;
  101. s->lfe_channel = -1;
  102. s->nb_inputs = 1;
  103. for (i = 0; i < 64; i++) {
  104. s->mapping[i] = -1;
  105. }
  106. while ((arg = av_strtok(p, "|", &tokenizer))) {
  107. int out_ch_id;
  108. char buf[8];
  109. p = NULL;
  110. if (parse_channel_name(s, s->nb_irs, &arg, &out_ch_id, buf)) {
  111. av_log(ctx, AV_LOG_WARNING, "Failed to parse \'%s\' as channel name.\n", buf);
  112. continue;
  113. }
  114. s->mapping[s->nb_irs] = out_ch_id;
  115. s->nb_irs++;
  116. }
  117. if (s->hrir_fmt == HRIR_MULTI)
  118. s->nb_inputs = 2;
  119. else
  120. s->nb_inputs = s->nb_irs + 1;
  121. av_free(args);
  122. }
  123. typedef struct ThreadData {
  124. AVFrame *in, *out;
  125. int *write;
  126. int **delay;
  127. float **ir;
  128. int *n_clippings;
  129. float **ringbuffer;
  130. float **temp_src;
  131. FFTComplex **temp_fft;
  132. FFTComplex **temp_afft;
  133. } ThreadData;
  134. static int headphone_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  135. {
  136. HeadphoneContext *s = ctx->priv;
  137. ThreadData *td = arg;
  138. AVFrame *in = td->in, *out = td->out;
  139. int offset = jobnr;
  140. int *write = &td->write[jobnr];
  141. const int *const delay = td->delay[jobnr];
  142. const float *const ir = td->ir[jobnr];
  143. int *n_clippings = &td->n_clippings[jobnr];
  144. float *ringbuffer = td->ringbuffer[jobnr];
  145. float *temp_src = td->temp_src[jobnr];
  146. const int ir_len = s->ir_len;
  147. const int air_len = s->air_len;
  148. const float *src = (const float *)in->data[0];
  149. float *dst = (float *)out->data[0];
  150. const int in_channels = in->channels;
  151. const int buffer_length = s->buffer_length;
  152. const uint32_t modulo = (uint32_t)buffer_length - 1;
  153. float *buffer[16];
  154. int wr = *write;
  155. int read;
  156. int i, l;
  157. dst += offset;
  158. for (l = 0; l < in_channels; l++) {
  159. buffer[l] = ringbuffer + l * buffer_length;
  160. }
  161. for (i = 0; i < in->nb_samples; i++) {
  162. const float *temp_ir = ir;
  163. *dst = 0;
  164. for (l = 0; l < in_channels; l++) {
  165. *(buffer[l] + wr) = src[l];
  166. }
  167. for (l = 0; l < in_channels; l++) {
  168. const float *const bptr = buffer[l];
  169. if (l == s->lfe_channel) {
  170. *dst += *(buffer[s->lfe_channel] + wr) * s->gain_lfe;
  171. temp_ir += air_len;
  172. continue;
  173. }
  174. read = (wr - *(delay + l) - (ir_len - 1) + buffer_length) & modulo;
  175. if (read + ir_len < buffer_length) {
  176. memcpy(temp_src, bptr + read, ir_len * sizeof(*temp_src));
  177. } else {
  178. int len = FFMIN(air_len - (read % ir_len), buffer_length - read);
  179. memcpy(temp_src, bptr + read, len * sizeof(*temp_src));
  180. memcpy(temp_src + len, bptr, (air_len - len) * sizeof(*temp_src));
  181. }
  182. dst[0] += s->fdsp->scalarproduct_float(temp_ir, temp_src, FFALIGN(ir_len, 32));
  183. temp_ir += air_len;
  184. }
  185. if (fabsf(dst[0]) > 1)
  186. n_clippings[0]++;
  187. dst += 2;
  188. src += in_channels;
  189. wr = (wr + 1) & modulo;
  190. }
  191. *write = wr;
  192. return 0;
  193. }
  194. static int headphone_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  195. {
  196. HeadphoneContext *s = ctx->priv;
  197. ThreadData *td = arg;
  198. AVFrame *in = td->in, *out = td->out;
  199. int offset = jobnr;
  200. int *write = &td->write[jobnr];
  201. FFTComplex *hrtf = s->data_hrtf[jobnr];
  202. int *n_clippings = &td->n_clippings[jobnr];
  203. float *ringbuffer = td->ringbuffer[jobnr];
  204. const int ir_len = s->ir_len;
  205. const float *src = (const float *)in->data[0];
  206. float *dst = (float *)out->data[0];
  207. const int in_channels = in->channels;
  208. const int buffer_length = s->buffer_length;
  209. const uint32_t modulo = (uint32_t)buffer_length - 1;
  210. FFTComplex *fft_in = s->temp_fft[jobnr];
  211. FFTComplex *fft_acc = s->temp_afft[jobnr];
  212. FFTContext *ifft = s->ifft[jobnr];
  213. FFTContext *fft = s->fft[jobnr];
  214. const int n_fft = s->n_fft;
  215. const float fft_scale = 1.0f / s->n_fft;
  216. FFTComplex *hrtf_offset;
  217. int wr = *write;
  218. int n_read;
  219. int i, j;
  220. dst += offset;
  221. n_read = FFMIN(ir_len, in->nb_samples);
  222. for (j = 0; j < n_read; j++) {
  223. dst[2 * j] = ringbuffer[wr];
  224. ringbuffer[wr] = 0.0;
  225. wr = (wr + 1) & modulo;
  226. }
  227. for (j = n_read; j < in->nb_samples; j++) {
  228. dst[2 * j] = 0;
  229. }
  230. memset(fft_acc, 0, sizeof(FFTComplex) * n_fft);
  231. for (i = 0; i < in_channels; i++) {
  232. if (i == s->lfe_channel) {
  233. for (j = 0; j < in->nb_samples; j++) {
  234. dst[2 * j] += src[i + j * in_channels] * s->gain_lfe;
  235. }
  236. continue;
  237. }
  238. offset = i * n_fft;
  239. hrtf_offset = hrtf + offset;
  240. memset(fft_in, 0, sizeof(FFTComplex) * n_fft);
  241. for (j = 0; j < in->nb_samples; j++) {
  242. fft_in[j].re = src[j * in_channels + i];
  243. }
  244. av_fft_permute(fft, fft_in);
  245. av_fft_calc(fft, fft_in);
  246. for (j = 0; j < n_fft; j++) {
  247. const FFTComplex *hcomplex = hrtf_offset + j;
  248. const float re = fft_in[j].re;
  249. const float im = fft_in[j].im;
  250. fft_acc[j].re += re * hcomplex->re - im * hcomplex->im;
  251. fft_acc[j].im += re * hcomplex->im + im * hcomplex->re;
  252. }
  253. }
  254. av_fft_permute(ifft, fft_acc);
  255. av_fft_calc(ifft, fft_acc);
  256. for (j = 0; j < in->nb_samples; j++) {
  257. dst[2 * j] += fft_acc[j].re * fft_scale;
  258. }
  259. for (j = 0; j < ir_len - 1; j++) {
  260. int write_pos = (wr + j) & modulo;
  261. *(ringbuffer + write_pos) += fft_acc[in->nb_samples + j].re * fft_scale;
  262. }
  263. for (i = 0; i < out->nb_samples; i++) {
  264. if (fabsf(dst[0]) > 1) {
  265. n_clippings[0]++;
  266. }
  267. dst += 2;
  268. }
  269. *write = wr;
  270. return 0;
  271. }
  272. static int check_ir(AVFilterLink *inlink, int input_number)
  273. {
  274. AVFilterContext *ctx = inlink->dst;
  275. HeadphoneContext *s = ctx->priv;
  276. int ir_len, max_ir_len;
  277. ir_len = ff_inlink_queued_samples(inlink);
  278. max_ir_len = 65536;
  279. if (ir_len > max_ir_len) {
  280. av_log(ctx, AV_LOG_ERROR, "Too big length of IRs: %d > %d.\n", ir_len, max_ir_len);
  281. return AVERROR(EINVAL);
  282. }
  283. s->in[input_number].ir_len = ir_len;
  284. s->ir_len = FFMAX(ir_len, s->ir_len);
  285. return 0;
  286. }
  287. static int headphone_frame(HeadphoneContext *s, AVFrame *in, AVFilterLink *outlink)
  288. {
  289. AVFilterContext *ctx = outlink->src;
  290. int n_clippings[2] = { 0 };
  291. ThreadData td;
  292. AVFrame *out;
  293. out = ff_get_audio_buffer(outlink, in->nb_samples);
  294. if (!out) {
  295. av_frame_free(&in);
  296. return AVERROR(ENOMEM);
  297. }
  298. out->pts = in->pts;
  299. td.in = in; td.out = out; td.write = s->write;
  300. td.delay = s->delay; td.ir = s->data_ir; td.n_clippings = n_clippings;
  301. td.ringbuffer = s->ringbuffer; td.temp_src = s->temp_src;
  302. td.temp_fft = s->temp_fft;
  303. td.temp_afft = s->temp_afft;
  304. if (s->type == TIME_DOMAIN) {
  305. ctx->internal->execute(ctx, headphone_convolute, &td, NULL, 2);
  306. } else {
  307. ctx->internal->execute(ctx, headphone_fast_convolute, &td, NULL, 2);
  308. }
  309. emms_c();
  310. if (n_clippings[0] + n_clippings[1] > 0) {
  311. av_log(ctx, AV_LOG_WARNING, "%d of %d samples clipped. Please reduce gain.\n",
  312. n_clippings[0] + n_clippings[1], out->nb_samples * 2);
  313. }
  314. av_frame_free(&in);
  315. return ff_filter_frame(outlink, out);
  316. }
  317. static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink)
  318. {
  319. struct HeadphoneContext *s = ctx->priv;
  320. const int ir_len = s->ir_len;
  321. int nb_irs = s->nb_irs;
  322. int nb_input_channels = ctx->inputs[0]->channels;
  323. float gain_lin = expf((s->gain - 3 * nb_input_channels) / 20 * M_LN10);
  324. FFTComplex *data_hrtf_l = NULL;
  325. FFTComplex *data_hrtf_r = NULL;
  326. FFTComplex *fft_in_l = NULL;
  327. FFTComplex *fft_in_r = NULL;
  328. float *data_ir_l = NULL;
  329. float *data_ir_r = NULL;
  330. int offset = 0, ret = 0;
  331. int n_fft;
  332. int i, j, k;
  333. s->air_len = 1 << (32 - ff_clz(ir_len));
  334. s->buffer_length = 1 << (32 - ff_clz(s->air_len));
  335. s->n_fft = n_fft = 1 << (32 - ff_clz(ir_len + s->size));
  336. if (s->type == FREQUENCY_DOMAIN) {
  337. fft_in_l = av_calloc(n_fft, sizeof(*fft_in_l));
  338. fft_in_r = av_calloc(n_fft, sizeof(*fft_in_r));
  339. if (!fft_in_l || !fft_in_r) {
  340. ret = AVERROR(ENOMEM);
  341. goto fail;
  342. }
  343. av_fft_end(s->fft[0]);
  344. av_fft_end(s->fft[1]);
  345. s->fft[0] = av_fft_init(av_log2(s->n_fft), 0);
  346. s->fft[1] = av_fft_init(av_log2(s->n_fft), 0);
  347. av_fft_end(s->ifft[0]);
  348. av_fft_end(s->ifft[1]);
  349. s->ifft[0] = av_fft_init(av_log2(s->n_fft), 1);
  350. s->ifft[1] = av_fft_init(av_log2(s->n_fft), 1);
  351. if (!s->fft[0] || !s->fft[1] || !s->ifft[0] || !s->ifft[1]) {
  352. av_log(ctx, AV_LOG_ERROR, "Unable to create FFT contexts of size %d.\n", s->n_fft);
  353. ret = AVERROR(ENOMEM);
  354. goto fail;
  355. }
  356. }
  357. s->data_ir[0] = av_calloc(s->air_len, sizeof(float) * s->nb_irs);
  358. s->data_ir[1] = av_calloc(s->air_len, sizeof(float) * s->nb_irs);
  359. s->delay[0] = av_calloc(s->nb_irs, sizeof(float));
  360. s->delay[1] = av_calloc(s->nb_irs, sizeof(float));
  361. if (s->type == TIME_DOMAIN) {
  362. s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
  363. s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
  364. } else {
  365. s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float));
  366. s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float));
  367. s->temp_fft[0] = av_calloc(s->n_fft, sizeof(FFTComplex));
  368. s->temp_fft[1] = av_calloc(s->n_fft, sizeof(FFTComplex));
  369. s->temp_afft[0] = av_calloc(s->n_fft, sizeof(FFTComplex));
  370. s->temp_afft[1] = av_calloc(s->n_fft, sizeof(FFTComplex));
  371. if (!s->temp_fft[0] || !s->temp_fft[1] ||
  372. !s->temp_afft[0] || !s->temp_afft[1]) {
  373. ret = AVERROR(ENOMEM);
  374. goto fail;
  375. }
  376. }
  377. if (!s->data_ir[0] || !s->data_ir[1] ||
  378. !s->ringbuffer[0] || !s->ringbuffer[1]) {
  379. ret = AVERROR(ENOMEM);
  380. goto fail;
  381. }
  382. if (s->type == TIME_DOMAIN) {
  383. s->temp_src[0] = av_calloc(s->air_len, sizeof(float));
  384. s->temp_src[1] = av_calloc(s->air_len, sizeof(float));
  385. data_ir_l = av_calloc(nb_irs * s->air_len, sizeof(*data_ir_l));
  386. data_ir_r = av_calloc(nb_irs * s->air_len, sizeof(*data_ir_r));
  387. if (!data_ir_r || !data_ir_l || !s->temp_src[0] || !s->temp_src[1]) {
  388. ret = AVERROR(ENOMEM);
  389. goto fail;
  390. }
  391. } else {
  392. data_hrtf_l = av_calloc(n_fft, sizeof(*data_hrtf_l) * nb_irs);
  393. data_hrtf_r = av_calloc(n_fft, sizeof(*data_hrtf_r) * nb_irs);
  394. if (!data_hrtf_r || !data_hrtf_l) {
  395. ret = AVERROR(ENOMEM);
  396. goto fail;
  397. }
  398. }
  399. for (i = 0; i < s->nb_inputs - 1; i++) {
  400. int len = s->in[i + 1].ir_len;
  401. int delay_l = s->in[i + 1].delay_l;
  402. int delay_r = s->in[i + 1].delay_r;
  403. float *ptr;
  404. ret = ff_inlink_consume_samples(ctx->inputs[i + 1], len, len, &s->in[i + 1].frame);
  405. if (ret < 0)
  406. goto fail;
  407. ptr = (float *)s->in[i + 1].frame->extended_data[0];
  408. if (s->hrir_fmt == HRIR_STEREO) {
  409. int idx = -1;
  410. for (j = 0; j < inlink->channels; j++) {
  411. if (s->mapping[i] < 0) {
  412. continue;
  413. }
  414. if ((av_channel_layout_extract_channel(inlink->channel_layout, j)) == (1LL << s->mapping[i])) {
  415. idx = i;
  416. break;
  417. }
  418. }
  419. if (idx == -1)
  420. continue;
  421. if (s->type == TIME_DOMAIN) {
  422. offset = idx * s->air_len;
  423. for (j = 0; j < len; j++) {
  424. data_ir_l[offset + j] = ptr[len * 2 - j * 2 - 2] * gain_lin;
  425. data_ir_r[offset + j] = ptr[len * 2 - j * 2 - 1] * gain_lin;
  426. }
  427. } else {
  428. memset(fft_in_l, 0, n_fft * sizeof(*fft_in_l));
  429. memset(fft_in_r, 0, n_fft * sizeof(*fft_in_r));
  430. offset = idx * n_fft;
  431. for (j = 0; j < len; j++) {
  432. fft_in_l[delay_l + j].re = ptr[j * 2 ] * gain_lin;
  433. fft_in_r[delay_r + j].re = ptr[j * 2 + 1] * gain_lin;
  434. }
  435. av_fft_permute(s->fft[0], fft_in_l);
  436. av_fft_calc(s->fft[0], fft_in_l);
  437. memcpy(data_hrtf_l + offset, fft_in_l, n_fft * sizeof(*fft_in_l));
  438. av_fft_permute(s->fft[0], fft_in_r);
  439. av_fft_calc(s->fft[0], fft_in_r);
  440. memcpy(data_hrtf_r + offset, fft_in_r, n_fft * sizeof(*fft_in_r));
  441. }
  442. } else {
  443. int I, N = ctx->inputs[1]->channels;
  444. for (k = 0; k < N / 2; k++) {
  445. int idx = -1;
  446. for (j = 0; j < inlink->channels; j++) {
  447. if (s->mapping[k] < 0) {
  448. continue;
  449. }
  450. if ((av_channel_layout_extract_channel(inlink->channel_layout, j)) == (1LL << s->mapping[k])) {
  451. idx = k;
  452. break;
  453. }
  454. }
  455. if (idx == -1)
  456. continue;
  457. I = idx * 2;
  458. if (s->type == TIME_DOMAIN) {
  459. offset = idx * s->air_len;
  460. for (j = 0; j < len; j++) {
  461. data_ir_l[offset + j] = ptr[len * N - j * N - N + I ] * gain_lin;
  462. data_ir_r[offset + j] = ptr[len * N - j * N - N + I + 1] * gain_lin;
  463. }
  464. } else {
  465. memset(fft_in_l, 0, n_fft * sizeof(*fft_in_l));
  466. memset(fft_in_r, 0, n_fft * sizeof(*fft_in_r));
  467. offset = idx * n_fft;
  468. for (j = 0; j < len; j++) {
  469. fft_in_l[delay_l + j].re = ptr[j * N + I ] * gain_lin;
  470. fft_in_r[delay_r + j].re = ptr[j * N + I + 1] * gain_lin;
  471. }
  472. av_fft_permute(s->fft[0], fft_in_l);
  473. av_fft_calc(s->fft[0], fft_in_l);
  474. memcpy(data_hrtf_l + offset, fft_in_l, n_fft * sizeof(*fft_in_l));
  475. av_fft_permute(s->fft[0], fft_in_r);
  476. av_fft_calc(s->fft[0], fft_in_r);
  477. memcpy(data_hrtf_r + offset, fft_in_r, n_fft * sizeof(*fft_in_r));
  478. }
  479. }
  480. }
  481. av_frame_free(&s->in[i + 1].frame);
  482. }
  483. if (s->type == TIME_DOMAIN) {
  484. memcpy(s->data_ir[0], data_ir_l, sizeof(float) * nb_irs * s->air_len);
  485. memcpy(s->data_ir[1], data_ir_r, sizeof(float) * nb_irs * s->air_len);
  486. } else {
  487. s->data_hrtf[0] = av_calloc(n_fft * s->nb_irs, sizeof(FFTComplex));
  488. s->data_hrtf[1] = av_calloc(n_fft * s->nb_irs, sizeof(FFTComplex));
  489. if (!s->data_hrtf[0] || !s->data_hrtf[1]) {
  490. ret = AVERROR(ENOMEM);
  491. goto fail;
  492. }
  493. memcpy(s->data_hrtf[0], data_hrtf_l,
  494. sizeof(FFTComplex) * nb_irs * n_fft);
  495. memcpy(s->data_hrtf[1], data_hrtf_r,
  496. sizeof(FFTComplex) * nb_irs * n_fft);
  497. }
  498. s->have_hrirs = 1;
  499. fail:
  500. for (i = 0; i < s->nb_inputs - 1; i++)
  501. av_frame_free(&s->in[i + 1].frame);
  502. av_freep(&data_ir_l);
  503. av_freep(&data_ir_r);
  504. av_freep(&data_hrtf_l);
  505. av_freep(&data_hrtf_r);
  506. av_freep(&fft_in_l);
  507. av_freep(&fft_in_r);
  508. return ret;
  509. }
  510. static int activate(AVFilterContext *ctx)
  511. {
  512. HeadphoneContext *s = ctx->priv;
  513. AVFilterLink *inlink = ctx->inputs[0];
  514. AVFilterLink *outlink = ctx->outputs[0];
  515. AVFrame *in = NULL;
  516. int i, ret;
  517. FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
  518. if (!s->eof_hrirs) {
  519. for (i = 1; i < s->nb_inputs; i++) {
  520. if (s->in[i].eof)
  521. continue;
  522. if ((ret = check_ir(ctx->inputs[i], i)) < 0)
  523. return ret;
  524. if (!s->in[i].eof) {
  525. if (ff_outlink_get_status(ctx->inputs[i]) == AVERROR_EOF)
  526. s->in[i].eof = 1;
  527. }
  528. }
  529. for (i = 1; i < s->nb_inputs; i++) {
  530. if (!s->in[i].eof)
  531. break;
  532. }
  533. if (i != s->nb_inputs) {
  534. if (ff_outlink_frame_wanted(ctx->outputs[0])) {
  535. for (i = 1; i < s->nb_inputs; i++) {
  536. if (!s->in[i].eof)
  537. ff_inlink_request_frame(ctx->inputs[i]);
  538. }
  539. }
  540. return 0;
  541. } else {
  542. s->eof_hrirs = 1;
  543. }
  544. }
  545. if (!s->have_hrirs && s->eof_hrirs) {
  546. ret = convert_coeffs(ctx, inlink);
  547. if (ret < 0)
  548. return ret;
  549. }
  550. if ((ret = ff_inlink_consume_samples(ctx->inputs[0], s->size, s->size, &in)) > 0) {
  551. ret = headphone_frame(s, in, outlink);
  552. if (ret < 0)
  553. return ret;
  554. }
  555. if (ret < 0)
  556. return ret;
  557. FF_FILTER_FORWARD_STATUS(ctx->inputs[0], ctx->outputs[0]);
  558. if (ff_outlink_frame_wanted(ctx->outputs[0]))
  559. ff_inlink_request_frame(ctx->inputs[0]);
  560. return 0;
  561. }
  562. static int query_formats(AVFilterContext *ctx)
  563. {
  564. struct HeadphoneContext *s = ctx->priv;
  565. AVFilterFormats *formats = NULL;
  566. AVFilterChannelLayouts *layouts = NULL;
  567. AVFilterChannelLayouts *stereo_layout = NULL;
  568. AVFilterChannelLayouts *hrir_layouts = NULL;
  569. int ret, i;
  570. ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLT);
  571. if (ret)
  572. return ret;
  573. ret = ff_set_common_formats(ctx, formats);
  574. if (ret)
  575. return ret;
  576. layouts = ff_all_channel_layouts();
  577. if (!layouts)
  578. return AVERROR(ENOMEM);
  579. ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts);
  580. if (ret)
  581. return ret;
  582. ret = ff_add_channel_layout(&stereo_layout, AV_CH_LAYOUT_STEREO);
  583. if (ret)
  584. return ret;
  585. if (s->hrir_fmt == HRIR_MULTI) {
  586. hrir_layouts = ff_all_channel_counts();
  587. if (!hrir_layouts)
  588. ret = AVERROR(ENOMEM);
  589. ret = ff_channel_layouts_ref(hrir_layouts, &ctx->inputs[1]->out_channel_layouts);
  590. if (ret)
  591. return ret;
  592. } else {
  593. for (i = 1; i < s->nb_inputs; i++) {
  594. ret = ff_channel_layouts_ref(stereo_layout, &ctx->inputs[i]->out_channel_layouts);
  595. if (ret)
  596. return ret;
  597. }
  598. }
  599. ret = ff_channel_layouts_ref(stereo_layout, &ctx->outputs[0]->in_channel_layouts);
  600. if (ret)
  601. return ret;
  602. formats = ff_all_samplerates();
  603. if (!formats)
  604. return AVERROR(ENOMEM);
  605. return ff_set_common_samplerates(ctx, formats);
  606. }
  607. static int config_input(AVFilterLink *inlink)
  608. {
  609. AVFilterContext *ctx = inlink->dst;
  610. HeadphoneContext *s = ctx->priv;
  611. if (s->nb_irs < inlink->channels) {
  612. av_log(ctx, AV_LOG_ERROR, "Number of HRIRs must be >= %d.\n", inlink->channels);
  613. return AVERROR(EINVAL);
  614. }
  615. return 0;
  616. }
  617. static av_cold int init(AVFilterContext *ctx)
  618. {
  619. HeadphoneContext *s = ctx->priv;
  620. int i, ret;
  621. AVFilterPad pad = {
  622. .name = "in0",
  623. .type = AVMEDIA_TYPE_AUDIO,
  624. .config_props = config_input,
  625. };
  626. if ((ret = ff_insert_inpad(ctx, 0, &pad)) < 0)
  627. return ret;
  628. if (!s->map) {
  629. av_log(ctx, AV_LOG_ERROR, "Valid mapping must be set.\n");
  630. return AVERROR(EINVAL);
  631. }
  632. parse_map(ctx);
  633. s->in = av_calloc(s->nb_inputs, sizeof(*s->in));
  634. if (!s->in)
  635. return AVERROR(ENOMEM);
  636. for (i = 1; i < s->nb_inputs; i++) {
  637. char *name = av_asprintf("hrir%d", i - 1);
  638. AVFilterPad pad = {
  639. .name = name,
  640. .type = AVMEDIA_TYPE_AUDIO,
  641. };
  642. if (!name)
  643. return AVERROR(ENOMEM);
  644. if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
  645. av_freep(&pad.name);
  646. return ret;
  647. }
  648. }
  649. s->fdsp = avpriv_float_dsp_alloc(0);
  650. if (!s->fdsp)
  651. return AVERROR(ENOMEM);
  652. return 0;
  653. }
  654. static int config_output(AVFilterLink *outlink)
  655. {
  656. AVFilterContext *ctx = outlink->src;
  657. HeadphoneContext *s = ctx->priv;
  658. AVFilterLink *inlink = ctx->inputs[0];
  659. if (s->hrir_fmt == HRIR_MULTI) {
  660. AVFilterLink *hrir_link = ctx->inputs[1];
  661. if (hrir_link->channels < inlink->channels * 2) {
  662. av_log(ctx, AV_LOG_ERROR, "Number of channels in HRIR stream must be >= %d.\n", inlink->channels * 2);
  663. return AVERROR(EINVAL);
  664. }
  665. }
  666. s->gain_lfe = expf((s->gain - 3 * inlink->channels + s->lfe_gain) / 20 * M_LN10);
  667. return 0;
  668. }
  669. static av_cold void uninit(AVFilterContext *ctx)
  670. {
  671. HeadphoneContext *s = ctx->priv;
  672. int i;
  673. av_fft_end(s->ifft[0]);
  674. av_fft_end(s->ifft[1]);
  675. av_fft_end(s->fft[0]);
  676. av_fft_end(s->fft[1]);
  677. av_freep(&s->delay[0]);
  678. av_freep(&s->delay[1]);
  679. av_freep(&s->data_ir[0]);
  680. av_freep(&s->data_ir[1]);
  681. av_freep(&s->ringbuffer[0]);
  682. av_freep(&s->ringbuffer[1]);
  683. av_freep(&s->temp_src[0]);
  684. av_freep(&s->temp_src[1]);
  685. av_freep(&s->temp_fft[0]);
  686. av_freep(&s->temp_fft[1]);
  687. av_freep(&s->temp_afft[0]);
  688. av_freep(&s->temp_afft[1]);
  689. av_freep(&s->data_hrtf[0]);
  690. av_freep(&s->data_hrtf[1]);
  691. av_freep(&s->fdsp);
  692. for (i = 0; i < s->nb_inputs; i++) {
  693. if (ctx->input_pads && i)
  694. av_freep(&ctx->input_pads[i].name);
  695. }
  696. av_freep(&s->in);
  697. }
  698. #define OFFSET(x) offsetof(HeadphoneContext, x)
  699. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  700. static const AVOption headphone_options[] = {
  701. { "map", "set channels convolution mappings", OFFSET(map), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  702. { "gain", "set gain in dB", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, -20, 40, .flags = FLAGS },
  703. { "lfe", "set lfe gain in dB", OFFSET(lfe_gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, -20, 40, .flags = FLAGS },
  704. { "type", "set processing", OFFSET(type), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, .flags = FLAGS, "type" },
  705. { "time", "time domain", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, .flags = FLAGS, "type" },
  706. { "freq", "frequency domain", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, .flags = FLAGS, "type" },
  707. { "size", "set frame size", OFFSET(size), AV_OPT_TYPE_INT, {.i64=1024},1024,96000, .flags = FLAGS },
  708. { "hrir", "set hrir format", OFFSET(hrir_fmt), AV_OPT_TYPE_INT, {.i64=HRIR_STEREO}, 0, 1, .flags = FLAGS, "hrir" },
  709. { "stereo", "hrir files have exactly 2 channels", 0, AV_OPT_TYPE_CONST, {.i64=HRIR_STEREO}, 0, 0, .flags = FLAGS, "hrir" },
  710. { "multich", "single multichannel hrir file", 0, AV_OPT_TYPE_CONST, {.i64=HRIR_MULTI}, 0, 0, .flags = FLAGS, "hrir" },
  711. { NULL }
  712. };
  713. AVFILTER_DEFINE_CLASS(headphone);
  714. static const AVFilterPad outputs[] = {
  715. {
  716. .name = "default",
  717. .type = AVMEDIA_TYPE_AUDIO,
  718. .config_props = config_output,
  719. },
  720. { NULL }
  721. };
  722. AVFilter ff_af_headphone = {
  723. .name = "headphone",
  724. .description = NULL_IF_CONFIG_SMALL("Apply headphone binaural spatialization with HRTFs in additional streams."),
  725. .priv_size = sizeof(HeadphoneContext),
  726. .priv_class = &headphone_class,
  727. .init = init,
  728. .uninit = uninit,
  729. .query_formats = query_formats,
  730. .activate = activate,
  731. .inputs = NULL,
  732. .outputs = outputs,
  733. .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_DYNAMIC_INPUTS,
  734. };