FFmpeg
Loading...
Searching...
No Matches
af_sofalizer.c
Go to the documentation of this file.
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
28#include <math.h>
29#include <mysofa.h>
30
31#include "libavutil/mem.h"
32#include "libavutil/tx.h"
33#include "libavutil/avstring.h"
35#include "libavutil/float_dsp.h"
36#include "libavutil/intmath.h"
37#include "libavutil/opt.h"
38#include "avfilter.h"
39#include "filters.h"
40#include "formats.h"
41#include "audio.h"
42
43#define TIME_DOMAIN 0
44#define FREQUENCY_DOMAIN 1
45
46typedef struct MySofa { /* contains data of one SOFA file */
47 struct MYSOFA_HRTF *hrtf;
48 struct MYSOFA_LOOKUP *lookup;
49 struct MYSOFA_NEIGHBORHOOD *neighborhood;
50 int ir_samples; /* length of one impulse response (IR) */
51 int n_samples; /* ir_samples to next power of 2 */
52 float *lir, *rir; /* IRs (time-domain) */
53 float *fir;
55} MySofa;
56
57typedef struct VirtualSpeaker {
58 uint8_t set;
59 float azim;
60 float elev;
62
63typedef struct SOFAlizerContext {
64 const AVClass *class;
65
66 char *filename; /* name of SOFA file */
67 MySofa sofa; /* contains data of the SOFA file */
68
69 int sample_rate; /* sample rate from SOFA file */
70 float *speaker_azim; /* azimuth of the virtual loudspeakers */
71 float *speaker_elev; /* elevation of the virtual loudspeakers */
72 char *speakers_pos; /* custom positions of the virtual loudspeakers */
73 float lfe_gain; /* initial gain for the LFE channel */
74 float gain_lfe; /* gain applied to LFE channel */
75 int lfe_channel; /* LFE channel position in channel layout */
76
77 int n_conv; /* number of channels to convolute */
78
79 /* buffer variables (for convolution) */
80 float *ringbuffer[2]; /* buffers input samples, length of one buffer: */
81 /* no. input ch. (incl. LFE) x buffer_length */
82 int write[2]; /* current write position to ringbuffer */
83 int buffer_length; /* is: longest IR plus max. delay in all SOFA files */
84 /* then choose next power of 2 */
85 int n_fft; /* number of samples in one FFT block */
87
88 /* netCDF variables */
89 int *delay[2]; /* broadband delay for each channel/IR to be convolved */
90
91 float *data_ir[2]; /* IRs for all channels to be convolved */
92 /* (this excludes the LFE) */
93 float *temp_src[2];
94 AVComplexFloat *in_fft[2]; /* Array to hold input FFT values */
95 AVComplexFloat *out_fft[2]; /* Array to hold output FFT values */
96 AVComplexFloat *temp_afft[2]; /* Array to accumulate FFT values prior to IFFT */
97
98 /* control variables */
99 float gain; /* filter gain (in dB) */
100 float rotation; /* rotation of virtual loudspeakers (in degrees) */
101 float elevation; /* elevation of virtual loudspeakers (in deg.) */
102 float radius; /* distance virtual loudspeakers to listener (in metres) */
103 int type; /* processing type */
104 int framesize; /* size of buffer */
105 int normalize; /* should all IRs be normalized upon import ? */
106 int interpolate; /* should wanted IRs be interpolated from neighbors ? */
107 int minphase; /* should all IRs be minphased upon import ? */
108 float anglestep; /* neighbor search angle step, in agles */
109 float radstep; /* neighbor search radius step, in meters */
110
112
116
119
120static int close_sofa(struct MySofa *sofa)
121{
122 if (sofa->neighborhood)
123 mysofa_neighborhood_free(sofa->neighborhood);
124 sofa->neighborhood = NULL;
125 if (sofa->lookup)
126 mysofa_lookup_free(sofa->lookup);
127 sofa->lookup = NULL;
128 if (sofa->hrtf)
129 mysofa_free(sofa->hrtf);
130 sofa->hrtf = NULL;
131 av_freep(&sofa->fir);
132
133 return 0;
134}
135
136static int preload_sofa(AVFilterContext *ctx, char *filename, int *samplingrate)
137{
138 struct SOFAlizerContext *s = ctx->priv;
139 struct MYSOFA_HRTF *mysofa;
140 char *license;
141 int ret;
142
143 mysofa = mysofa_load(filename, &ret);
144 s->sofa.hrtf = mysofa;
145 if (ret || !mysofa) {
146 av_log(ctx, AV_LOG_ERROR, "Can't find SOFA-file '%s'\n", filename);
147 return AVERROR(EINVAL);
148 }
149
150 ret = mysofa_check(mysofa);
151 if (ret != MYSOFA_OK) {
152 av_log(ctx, AV_LOG_ERROR, "Selected SOFA file is invalid. Please select valid SOFA file.\n");
153 return ret;
154 }
155
156 if (!mysofa->N || !mysofa->M || mysofa->DataIR.elements / mysofa->M / mysofa->R < mysofa->N ||
157 (mysofa->DataDelay.elements != mysofa->R && mysofa->DataDelay.elements / mysofa->R < mysofa->M)) {
158 av_log(ctx, AV_LOG_ERROR, "Selected SOFA file has inconsistent dimensions.\n");
159 return AVERROR_INVALIDDATA;
160 }
161
162 if (s->normalize)
163 mysofa_loudness(s->sofa.hrtf);
164
165 if (s->minphase)
166 mysofa_minphase(s->sofa.hrtf, 0.01f);
167
168 mysofa_tocartesian(s->sofa.hrtf);
169
170 s->sofa.lookup = mysofa_lookup_init(s->sofa.hrtf);
171 if (s->sofa.lookup == NULL)
172 return AVERROR(EINVAL);
173
174 if (s->interpolate)
175 s->sofa.neighborhood = mysofa_neighborhood_init_withstepdefine(s->sofa.hrtf,
176 s->sofa.lookup,
177 s->anglestep,
178 s->radstep);
179
180 s->sofa.fir = av_calloc(s->sofa.hrtf->N * s->sofa.hrtf->R, sizeof(*s->sofa.fir));
181 if (!s->sofa.fir)
182 return AVERROR(ENOMEM);
183
184 if (mysofa->DataSamplingRate.elements != 1)
185 return AVERROR(EINVAL);
186 av_log(ctx, AV_LOG_DEBUG, "Original IR length: %d.\n", mysofa->N);
187 *samplingrate = mysofa->DataSamplingRate.values[0];
188 license = mysofa_getAttribute(mysofa->attributes, (char *)"License");
189 if (license)
190 av_log(ctx, AV_LOG_INFO, "SOFA license: %s\n", license);
191
192 return 0;
193}
194
195static int parse_channel_name(AVFilterContext *ctx, char **arg, int *rchannel)
196{
197 int len;
198 enum AVChannel channel_id = 0;
199 char buf[8] = {0};
200
201 /* try to parse a channel name, e.g. "FL" */
202 if (av_sscanf(*arg, "%7[A-Z]%n", buf, &len)) {
203 channel_id = av_channel_from_string(buf);
204 if (channel_id < 0 || channel_id >= 64) {
205 av_log(ctx, AV_LOG_WARNING, "Failed to parse \'%s\' as channel name.\n", buf);
206 return AVERROR(EINVAL);
207 }
208
209 *rchannel = channel_id;
210 *arg += len;
211 return 0;
212 } else if (av_sscanf(*arg, "%d%n", &channel_id, &len) == 1) {
213 if (channel_id < 0 || channel_id >= 64) {
214 av_log(ctx, AV_LOG_WARNING, "Failed to parse \'%d\' as channel number.\n", channel_id);
215 return AVERROR(EINVAL);
216 }
217 *rchannel = channel_id;
218 *arg += len;
219 return 0;
220 }
221 return AVERROR(EINVAL);
222}
223
225{
226 SOFAlizerContext *s = ctx->priv;
227 char *arg, *tokenizer, *p, *args = av_strdup(s->speakers_pos);
228
229 if (!args)
230 return;
231 p = args;
232
233 while ((arg = av_strtok(p, "|", &tokenizer))) {
234 float azim, elev;
235 int out_ch_id;
236
237 p = NULL;
238 if (parse_channel_name(ctx, &arg, &out_ch_id)) {
239 continue;
240 }
241 if (av_sscanf(arg, "%f %f", &azim, &elev) == 2) {
242 s->vspkrpos[out_ch_id].set = 1;
243 s->vspkrpos[out_ch_id].azim = azim;
244 s->vspkrpos[out_ch_id].elev = elev;
245 } else if (av_sscanf(arg, "%f", &azim) == 1) {
246 s->vspkrpos[out_ch_id].set = 1;
247 s->vspkrpos[out_ch_id].azim = azim;
248 s->vspkrpos[out_ch_id].elev = 0;
249 }
250 }
251
252 av_free(args);
253}
254
256 float *speaker_azim, float *speaker_elev)
257{
258 struct SOFAlizerContext *s = ctx->priv;
259 AVChannelLayout *channel_layout = &ctx->inputs[0]->ch_layout;
260 float azim[64] = { 0 };
261 float elev[64] = { 0 };
262 int ch, n_conv = ctx->inputs[0]->ch_layout.nb_channels; /* get no. input channels */
263
265 return AVERROR(EINVAL);
266
267 s->lfe_channel = -1;
268
269 if (s->speakers_pos)
271
272 /* set speaker positions according to input channel configuration: */
273 for (ch = 0; ch < n_conv; ch++) {
274 int chan = av_channel_layout_channel_from_index(channel_layout, ch);
275
276 switch (chan) {
277 case AV_CHAN_FRONT_LEFT: azim[ch] = 30; break;
278 case AV_CHAN_FRONT_RIGHT: azim[ch] = 330; break;
279 case AV_CHAN_FRONT_CENTER: azim[ch] = 0; break;
281 case AV_CHAN_LOW_FREQUENCY_2: s->lfe_channel = ch; break;
282 case AV_CHAN_BACK_LEFT: azim[ch] = 150; break;
283 case AV_CHAN_BACK_RIGHT: azim[ch] = 210; break;
284 case AV_CHAN_BACK_CENTER: azim[ch] = 180; break;
285 case AV_CHAN_SIDE_LEFT: azim[ch] = 90; break;
286 case AV_CHAN_SIDE_RIGHT: azim[ch] = 270; break;
287 case AV_CHAN_FRONT_LEFT_OF_CENTER: azim[ch] = 15; break;
288 case AV_CHAN_FRONT_RIGHT_OF_CENTER: azim[ch] = 345; break;
289 case AV_CHAN_TOP_CENTER: azim[ch] = 0;
290 elev[ch] = 90; break;
291 case AV_CHAN_TOP_FRONT_LEFT: azim[ch] = 30;
292 elev[ch] = 45; break;
293 case AV_CHAN_TOP_FRONT_CENTER: azim[ch] = 0;
294 elev[ch] = 45; break;
295 case AV_CHAN_TOP_FRONT_RIGHT: azim[ch] = 330;
296 elev[ch] = 45; break;
297 case AV_CHAN_TOP_BACK_LEFT: azim[ch] = 150;
298 elev[ch] = 45; break;
299 case AV_CHAN_TOP_BACK_RIGHT: azim[ch] = 210;
300 elev[ch] = 45; break;
301 case AV_CHAN_TOP_BACK_CENTER: azim[ch] = 180;
302 elev[ch] = 45; break;
303 case AV_CHAN_WIDE_LEFT: azim[ch] = 90; break;
304 case AV_CHAN_WIDE_RIGHT: azim[ch] = 270; break;
305 case AV_CHAN_SURROUND_DIRECT_LEFT: azim[ch] = 90; break;
306 case AV_CHAN_SURROUND_DIRECT_RIGHT: azim[ch] = 270; break;
307 case AV_CHAN_STEREO_LEFT: azim[ch] = 90; break;
308 case AV_CHAN_STEREO_RIGHT: azim[ch] = 270; break;
309 default:
310 return AVERROR(EINVAL);
311 }
312
313 if (s->vspkrpos[ch].set) {
314 azim[ch] = s->vspkrpos[ch].azim;
315 elev[ch] = s->vspkrpos[ch].elev;
316 }
317 }
318
319 memcpy(speaker_azim, azim, n_conv * sizeof(float));
320 memcpy(speaker_elev, elev, n_conv * sizeof(float));
321
322 return 0;
323
324}
325
326typedef struct ThreadData {
327 AVFrame *in, *out;
328 int *write;
329 int **delay;
330 float **ir;
331 int *n_clippings;
332 float **ringbuffer;
333 float **temp_src;
337} ThreadData;
338
339static int sofalizer_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
340{
341 SOFAlizerContext *s = ctx->priv;
342 ThreadData *td = arg;
343 AVFrame *in = td->in, *out = td->out;
344 int offset = jobnr;
345 int *write = &td->write[jobnr];
346 const int *const delay = td->delay[jobnr];
347 const float *const ir = td->ir[jobnr];
348 int *n_clippings = &td->n_clippings[jobnr];
349 float *ringbuffer = td->ringbuffer[jobnr];
350 float *temp_src = td->temp_src[jobnr];
351 const int ir_samples = s->sofa.ir_samples; /* length of one IR */
352 const int n_samples = s->sofa.n_samples;
353 const int planar = in->format == AV_SAMPLE_FMT_FLTP;
354 const int mult = 1 + !planar;
355 const float *src = (const float *)in->extended_data[0]; /* get pointer to audio input buffer */
356 float *dst = (float *)out->extended_data[jobnr * planar]; /* get pointer to audio output buffer */
357 const int in_channels = s->n_conv; /* number of input channels */
358 /* ring buffer length is: longest IR plus max. delay -> next power of 2 */
359 const int buffer_length = s->buffer_length;
360 /* -1 for AND instead of MODULO (applied to powers of 2): */
361 const uint32_t modulo = (uint32_t)buffer_length - 1;
362 float *buffer[64]; /* holds ringbuffer for each input channel */
363 int wr = *write;
364 int read;
365 int i, l;
366
367 if (!planar)
368 dst += offset;
369
370 for (l = 0; l < in_channels; l++) {
371 /* get starting address of ringbuffer for each input channel */
372 buffer[l] = ringbuffer + l * buffer_length;
373 }
374
375 for (i = 0; i < in->nb_samples; i++) {
376 const float *temp_ir = ir; /* using same set of IRs for each sample */
377
378 dst[0] = 0;
379 if (planar) {
380 for (l = 0; l < in_channels; l++) {
381 const float *srcp = (const float *)in->extended_data[l];
382
383 /* write current input sample to ringbuffer (for each channel) */
384 buffer[l][wr] = srcp[i];
385 }
386 } else {
387 for (l = 0; l < in_channels; l++) {
388 /* write current input sample to ringbuffer (for each channel) */
389 buffer[l][wr] = src[l];
390 }
391 }
392
393 /* loop goes through all channels to be convolved */
394 for (l = 0; l < in_channels; l++) {
395 const float *const bptr = buffer[l];
396
397 if (l == s->lfe_channel) {
398 /* LFE is an input channel but requires no convolution */
399 /* apply gain to LFE signal and add to output buffer */
400 dst[0] += *(buffer[s->lfe_channel] + wr) * s->gain_lfe;
401 temp_ir += n_samples;
402 continue;
403 }
404
405 /* current read position in ringbuffer: input sample write position
406 * - delay for l-th ch. + diff. betw. IR length and buffer length
407 * (mod buffer length) */
408 read = (wr - delay[l] - (ir_samples - 1) + buffer_length) & modulo;
409
410 if (read + ir_samples < buffer_length) {
411 memmove(temp_src, bptr + read, ir_samples * sizeof(*temp_src));
412 } else {
413 int len = FFMIN(n_samples - (read % ir_samples), buffer_length - read);
414
415 memmove(temp_src, bptr + read, len * sizeof(*temp_src));
416 memmove(temp_src + len, bptr, (n_samples - len) * sizeof(*temp_src));
417 }
418
419 /* multiply signal and IR, and add up the results */
420 dst[0] += s->fdsp->scalarproduct_float(temp_ir, temp_src, FFALIGN(ir_samples, 32));
421 temp_ir += n_samples;
422 }
423
424 /* clippings counter */
425 if (fabsf(dst[0]) > 1)
426 n_clippings[0]++;
427
428 /* move output buffer pointer by +2 to get to next sample of processed channel: */
429 dst += mult;
430 src += in_channels;
431 wr = (wr + 1) & modulo; /* update ringbuffer write position */
432 }
433
434 *write = wr; /* remember write position in ringbuffer for next call */
435
436 return 0;
437}
438
439static int sofalizer_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
440{
441 SOFAlizerContext *s = ctx->priv;
442 ThreadData *td = arg;
443 AVFrame *in = td->in, *out = td->out;
444 int offset = jobnr;
445 int *write = &td->write[jobnr];
446 AVComplexFloat *hrtf = s->data_hrtf[jobnr]; /* get pointers to current HRTF data */
447 int *n_clippings = &td->n_clippings[jobnr];
448 float *ringbuffer = td->ringbuffer[jobnr];
449 const int ir_samples = s->sofa.ir_samples; /* length of one IR */
450 const int planar = in->format == AV_SAMPLE_FMT_FLTP;
451 const int mult = 1 + !planar;
452 float *dst = (float *)out->extended_data[jobnr * planar]; /* get pointer to audio output buffer */
453 const int in_channels = s->n_conv; /* number of input channels */
454 /* ring buffer length is: longest IR plus max. delay -> next power of 2 */
455 const int buffer_length = s->buffer_length;
456 /* -1 for AND instead of MODULO (applied to powers of 2): */
457 const uint32_t modulo = (uint32_t)buffer_length - 1;
458 AVComplexFloat *fft_in = s->in_fft[jobnr]; /* temporary array for FFT input data */
459 AVComplexFloat *fft_out = s->out_fft[jobnr]; /* temporary array for FFT output data */
460 AVComplexFloat *fft_acc = s->temp_afft[jobnr];
461 AVTXContext *ifft = s->ifft[jobnr];
462 av_tx_fn itx_fn = s->itx_fn[jobnr];
463 AVTXContext *fft = s->fft[jobnr];
464 av_tx_fn tx_fn = s->tx_fn[jobnr];
465 const int n_conv = s->n_conv;
466 const int n_fft = s->n_fft;
467 const float fft_scale = 1.0f / s->n_fft;
468 AVComplexFloat *hrtf_offset;
469 int wr = *write;
470 int n_read;
471 int i, j;
472
473 if (!planar)
474 dst += offset;
475
476 /* find minimum between number of samples and output buffer length:
477 * (important, if one IR is longer than the output buffer) */
478 n_read = FFMIN(ir_samples, in->nb_samples);
479 for (j = 0; j < n_read; j++) {
480 /* initialize output buf with saved signal from overflow buf */
481 dst[mult * j] = ringbuffer[wr];
482 ringbuffer[wr] = 0.0f; /* re-set read samples to zero */
483 /* update ringbuffer read/write position */
484 wr = (wr + 1) & modulo;
485 }
486
487 /* initialize rest of output buffer with 0 */
488 for (j = n_read; j < in->nb_samples; j++) {
489 dst[mult * j] = 0;
490 }
491
492 /* fill FFT accumulation with 0 */
493 memset(fft_acc, 0, sizeof(AVComplexFloat) * n_fft);
494
495 for (i = 0; i < n_conv; i++) {
496 const float *src = (const float *)in->extended_data[i * planar]; /* get pointer to audio input buffer */
497
498 if (i == s->lfe_channel) { /* LFE */
499 if (in->format == AV_SAMPLE_FMT_FLT) {
500 for (j = 0; j < in->nb_samples; j++) {
501 /* apply gain to LFE signal and add to output buffer */
502 dst[2 * j] += src[i + j * in_channels] * s->gain_lfe;
503 }
504 } else {
505 for (j = 0; j < in->nb_samples; j++) {
506 /* apply gain to LFE signal and add to output buffer */
507 dst[j] += src[j] * s->gain_lfe;
508 }
509 }
510 continue;
511 }
512
513 /* outer loop: go through all input channels to be convolved */
514 offset = i * n_fft; /* no. samples already processed */
515 hrtf_offset = hrtf + offset;
516
517 /* fill FFT input with 0 (we want to zero-pad) */
518 memset(fft_in, 0, sizeof(AVComplexFloat) * n_fft);
519
520 if (in->format == AV_SAMPLE_FMT_FLT) {
521 for (j = 0; j < in->nb_samples; j++) {
522 /* prepare input for FFT */
523 /* write all samples of current input channel to FFT input array */
524 fft_in[j].re = src[j * in_channels + i];
525 }
526 } else {
527 for (j = 0; j < in->nb_samples; j++) {
528 /* prepare input for FFT */
529 /* write all samples of current input channel to FFT input array */
530 fft_in[j].re = src[j];
531 }
532 }
533
534 /* transform input signal of current channel to frequency domain */
535 tx_fn(fft, fft_out, fft_in, sizeof(*fft_in));
536
537 for (j = 0; j < n_fft; j++) {
538 const AVComplexFloat *hcomplex = hrtf_offset + j;
539 const float re = fft_out[j].re;
540 const float im = fft_out[j].im;
541
542 /* complex multiplication of input signal and HRTFs */
543 /* output channel (real): */
544 fft_acc[j].re += re * hcomplex->re - im * hcomplex->im;
545 /* output channel (imag): */
546 fft_acc[j].im += re * hcomplex->im + im * hcomplex->re;
547 }
548 }
549
550 /* transform output signal of current channel back to time domain */
551 itx_fn(ifft, fft_out, fft_acc, sizeof(*fft_acc));
552
553 for (j = 0; j < in->nb_samples; j++) {
554 /* write output signal of current channel to output buffer */
555 dst[mult * j] += fft_out[j].re * fft_scale;
556 }
557
558 for (j = 0; j < ir_samples - 1; j++) { /* overflow length is IR length - 1 */
559 /* write the rest of output signal to overflow buffer */
560 int write_pos = (wr + j) & modulo;
561
562 *(ringbuffer + write_pos) += fft_out[in->nb_samples + j].re * fft_scale;
563 }
564
565 /* go through all samples of current output buffer: count clippings */
566 for (i = 0; i < out->nb_samples; i++) {
567 /* clippings counter */
568 if (fabsf(dst[i * mult]) > 1) { /* if current output sample > 1 */
569 n_clippings[0]++;
570 }
571 }
572
573 /* remember read/write position in ringbuffer for next call */
574 *write = wr;
575
576 return 0;
577}
578
579static int filter_frame(AVFilterLink *inlink, AVFrame *in)
580{
581 AVFilterContext *ctx = inlink->dst;
582 SOFAlizerContext *s = ctx->priv;
583 AVFilterLink *outlink = ctx->outputs[0];
584 int n_clippings[2] = { 0 };
585 ThreadData td;
586 AVFrame *out;
587
588 out = ff_get_audio_buffer(outlink, in->nb_samples);
589 if (!out) {
590 av_frame_free(&in);
591 return AVERROR(ENOMEM);
592 }
594
595 td.in = in; td.out = out; td.write = s->write;
596 td.delay = s->delay; td.ir = s->data_ir; td.n_clippings = n_clippings;
597 td.ringbuffer = s->ringbuffer; td.temp_src = s->temp_src;
598 td.in_fft = s->in_fft;
599 td.out_fft = s->out_fft;
600 td.temp_afft = s->temp_afft;
601
602 if (s->type == TIME_DOMAIN) {
604 } else if (s->type == FREQUENCY_DOMAIN) {
606 }
607
608 /* display error message if clipping occurred */
609 if (n_clippings[0] + n_clippings[1] > 0) {
610 av_log(ctx, AV_LOG_WARNING, "%d of %d samples clipped. Please reduce gain.\n",
611 n_clippings[0] + n_clippings[1], out->nb_samples * 2);
612 }
613
614 av_frame_free(&in);
615 return ff_filter_frame(outlink, out);
616}
617
619{
620 AVFilterLink *inlink = ctx->inputs[0];
621 AVFilterLink *outlink = ctx->outputs[0];
622 SOFAlizerContext *s = ctx->priv;
623 AVFrame *in;
624 int ret;
625
626 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
627
628 if (s->nb_samples)
629 ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
630 else
631 ret = ff_inlink_consume_frame(inlink, &in);
632 if (ret < 0)
633 return ret;
634 if (ret > 0)
635 return filter_frame(inlink, in);
636
637 FF_FILTER_FORWARD_STATUS(inlink, outlink);
638 FF_FILTER_FORWARD_WANTED(outlink, inlink);
639
640 return FFERROR_NOT_READY;
641}
642
644 AVFilterFormatsConfig **cfg_in,
645 AVFilterFormatsConfig **cfg_out)
646{
647 const SOFAlizerContext *s = ctx->priv;
649 int ret, sample_rates[] = { 48000, -1 };
650 static const enum AVSampleFormat sample_fmts[] = {
653 };
654
656 if (ret)
657 return ret;
658
660 if (!layouts)
661 return AVERROR(ENOMEM);
662
664 if (ret)
665 return ret;
666
667 layouts = NULL;
669 if (ret)
670 return ret;
671
673 if (ret)
674 return ret;
675
676 sample_rates[0] = s->sample_rate;
677 return ff_set_common_samplerates_from_list2(ctx, cfg_in, cfg_out, sample_rates);
678}
679
680static int getfilter_float(AVFilterContext *ctx, float x, float y, float z,
681 float *left, float *right,
682 float *delay_left, float *delay_right)
683{
684 struct SOFAlizerContext *s = ctx->priv;
685 float c[3], delays[2];
686 float *fl, *fr;
687 int nearest;
688 int *neighbors;
689 float *res;
690
691 c[0] = x, c[1] = y, c[2] = z;
692 nearest = mysofa_lookup(s->sofa.lookup, c);
693 if (nearest < 0)
694 return AVERROR(EINVAL);
695
696 if (s->interpolate) {
697 neighbors = mysofa_neighborhood(s->sofa.neighborhood, nearest);
698 res = mysofa_interpolate(s->sofa.hrtf, c,
699 nearest, neighbors,
700 s->sofa.fir, delays);
701 } else {
702 if (s->sofa.hrtf->DataDelay.elements > s->sofa.hrtf->R) {
703 delays[0] = s->sofa.hrtf->DataDelay.values[nearest * s->sofa.hrtf->R];
704 delays[1] = s->sofa.hrtf->DataDelay.values[nearest * s->sofa.hrtf->R + 1];
705 } else {
706 delays[0] = s->sofa.hrtf->DataDelay.values[0];
707 delays[1] = s->sofa.hrtf->DataDelay.values[1];
708 }
709 res = s->sofa.hrtf->DataIR.values + nearest * s->sofa.hrtf->N * s->sofa.hrtf->R;
710 }
711
712 *delay_left = delays[0];
713 *delay_right = delays[1];
714
715 fl = res;
716 fr = res + s->sofa.hrtf->N;
717
718 memcpy(left, fl, sizeof(float) * s->sofa.hrtf->N);
719 memcpy(right, fr, sizeof(float) * s->sofa.hrtf->N);
720
721 return 0;
722}
723
724static int load_data(AVFilterContext *ctx, int azim, int elev, float radius, int sample_rate)
725{
726 struct SOFAlizerContext *s = ctx->priv;
727 int n_samples;
728 int ir_samples;
729 int n_conv = s->n_conv; /* no. channels to convolve */
730 int n_fft;
731 float delay_l; /* broadband delay for each IR */
732 float delay_r;
733 int nb_input_channels = ctx->inputs[0]->ch_layout.nb_channels; /* no. input channels */
734 float gain_lin = expf((s->gain - 3 * nb_input_channels) / 20 * M_LN10); /* gain - 3dB/channel */
735 AVComplexFloat *data_hrtf_l = NULL;
736 AVComplexFloat *data_hrtf_r = NULL;
737 AVComplexFloat *fft_out_l = NULL;
738 AVComplexFloat *fft_out_r = NULL;
739 AVComplexFloat *fft_in_l = NULL;
740 AVComplexFloat *fft_in_r = NULL;
741 float *data_ir_l = NULL;
742 float *data_ir_r = NULL;
743 int offset = 0; /* used for faster pointer arithmetic in for-loop */
744 int i, j, azim_orig = azim, elev_orig = elev;
745 int ret = 0;
746 int n_current;
747 int n_max = 0;
748
749 av_log(ctx, AV_LOG_DEBUG, "IR length: %d.\n", s->sofa.hrtf->N);
750 s->sofa.ir_samples = s->sofa.hrtf->N;
751 s->sofa.n_samples = FFMAX(32, 1 << (32 - ff_clz(s->sofa.ir_samples)));
752
753 n_samples = s->sofa.n_samples;
754 ir_samples = s->sofa.ir_samples;
755
756 if (s->type == TIME_DOMAIN) {
757 s->data_ir[0] = av_calloc(n_samples, sizeof(float) * s->n_conv);
758 s->data_ir[1] = av_calloc(n_samples, sizeof(float) * s->n_conv);
759
760 if (!s->data_ir[0] || !s->data_ir[1]) {
761 ret = AVERROR(ENOMEM);
762 goto fail;
763 }
764 }
765
766 s->delay[0] = av_calloc(s->n_conv, sizeof(int));
767 s->delay[1] = av_calloc(s->n_conv, sizeof(int));
768
769 if (!s->delay[0] || !s->delay[1]) {
770 ret = AVERROR(ENOMEM);
771 goto fail;
772 }
773
774 /* get temporary IR for L and R channel */
775 data_ir_l = av_calloc(n_conv * n_samples, sizeof(*data_ir_l));
776 data_ir_r = av_calloc(n_conv * n_samples, sizeof(*data_ir_r));
777 if (!data_ir_r || !data_ir_l) {
778 ret = AVERROR(ENOMEM);
779 goto fail;
780 }
781
782 if (s->type == TIME_DOMAIN) {
783 s->temp_src[0] = av_calloc(n_samples, sizeof(float));
784 s->temp_src[1] = av_calloc(n_samples, sizeof(float));
785 if (!s->temp_src[0] || !s->temp_src[1]) {
786 ret = AVERROR(ENOMEM);
787 goto fail;
788 }
789 }
790
791 s->speaker_azim = av_calloc(s->n_conv, sizeof(*s->speaker_azim));
792 s->speaker_elev = av_calloc(s->n_conv, sizeof(*s->speaker_elev));
793 if (!s->speaker_azim || !s->speaker_elev) {
794 ret = AVERROR(ENOMEM);
795 goto fail;
796 }
797
798 /* get speaker positions */
799 if ((ret = get_speaker_pos(ctx, s->speaker_azim, s->speaker_elev)) < 0) {
800 av_log(ctx, AV_LOG_ERROR, "Couldn't get speaker positions. Input channel configuration not supported.\n");
801 goto fail;
802 }
803
804 for (i = 0; i < s->n_conv; i++) {
805 float coordinates[3];
806
807 /* load and store IRs and corresponding delays */
808 azim = (int)(s->speaker_azim[i] + azim_orig) % 360;
809 elev = (int)(s->speaker_elev[i] + elev_orig) % 90;
810
811 coordinates[0] = azim;
812 coordinates[1] = elev;
813 coordinates[2] = radius;
814
815 mysofa_s2c(coordinates);
816
817 /* get id of IR closest to desired position */
818 ret = getfilter_float(ctx, coordinates[0], coordinates[1], coordinates[2],
819 data_ir_l + n_samples * i,
820 data_ir_r + n_samples * i,
821 &delay_l, &delay_r);
822 if (ret < 0)
823 goto fail;
824
825 s->delay[0][i] = delay_l * sample_rate;
826 s->delay[1][i] = delay_r * sample_rate;
827
828 s->sofa.max_delay = FFMAX3(s->sofa.max_delay, s->delay[0][i], s->delay[1][i]);
829 }
830
831 /* get size of ringbuffer (longest IR plus max. delay) */
832 /* then choose next power of 2 for performance optimization */
833 n_current = n_samples + s->sofa.max_delay;
834 /* length of longest IR plus max. delay */
835 n_max = FFMAX(n_max, n_current);
836
837 /* buffer length is longest IR plus max. delay -> next power of 2
838 (32 - count leading zeros gives required exponent) */
839 s->buffer_length = 1 << (32 - ff_clz(n_max));
840 s->n_fft = n_fft = 1 << (32 - ff_clz(n_max + s->framesize));
841
842 if (s->type == FREQUENCY_DOMAIN) {
843 float scale = 1.f;
844
845 av_tx_uninit(&s->fft[0]);
846 av_tx_uninit(&s->fft[1]);
847 ret = av_tx_init(&s->fft[0], &s->tx_fn[0], AV_TX_FLOAT_FFT, 0, s->n_fft, &scale, 0);
848 if (ret < 0)
849 goto fail;
850 ret = av_tx_init(&s->fft[1], &s->tx_fn[1], AV_TX_FLOAT_FFT, 0, s->n_fft, &scale, 0);
851 if (ret < 0)
852 goto fail;
853 av_tx_uninit(&s->ifft[0]);
854 av_tx_uninit(&s->ifft[1]);
855 ret = av_tx_init(&s->ifft[0], &s->itx_fn[0], AV_TX_FLOAT_FFT, 1, s->n_fft, &scale, 0);
856 if (ret < 0)
857 goto fail;
858 ret = av_tx_init(&s->ifft[1], &s->itx_fn[1], AV_TX_FLOAT_FFT, 1, s->n_fft, &scale, 0);
859 if (ret < 0)
860 goto fail;
861 }
862
863 if (s->type == TIME_DOMAIN) {
864 s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
865 s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
866 } else if (s->type == FREQUENCY_DOMAIN) {
867 /* get temporary HRTF memory for L and R channel */
868 data_hrtf_l = av_malloc_array(n_fft, sizeof(*data_hrtf_l) * n_conv);
869 data_hrtf_r = av_malloc_array(n_fft, sizeof(*data_hrtf_r) * n_conv);
870 if (!data_hrtf_r || !data_hrtf_l) {
871 ret = AVERROR(ENOMEM);
872 goto fail;
873 }
874
875 s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float));
876 s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float));
877 s->in_fft[0] = av_malloc_array(s->n_fft, sizeof(AVComplexFloat));
878 s->in_fft[1] = av_malloc_array(s->n_fft, sizeof(AVComplexFloat));
879 s->out_fft[0] = av_malloc_array(s->n_fft, sizeof(AVComplexFloat));
880 s->out_fft[1] = av_malloc_array(s->n_fft, sizeof(AVComplexFloat));
881 s->temp_afft[0] = av_malloc_array(s->n_fft, sizeof(AVComplexFloat));
882 s->temp_afft[1] = av_malloc_array(s->n_fft, sizeof(AVComplexFloat));
883 if (!s->in_fft[0] || !s->in_fft[1] ||
884 !s->out_fft[0] || !s->out_fft[1] ||
885 !s->temp_afft[0] || !s->temp_afft[1]) {
886 ret = AVERROR(ENOMEM);
887 goto fail;
888 }
889 }
890
891 if (!s->ringbuffer[0] || !s->ringbuffer[1]) {
892 ret = AVERROR(ENOMEM);
893 goto fail;
894 }
895
896 if (s->type == FREQUENCY_DOMAIN) {
897 fft_out_l = av_calloc(n_fft, sizeof(*fft_out_l));
898 fft_out_r = av_calloc(n_fft, sizeof(*fft_out_r));
899 fft_in_l = av_calloc(n_fft, sizeof(*fft_in_l));
900 fft_in_r = av_calloc(n_fft, sizeof(*fft_in_r));
901 if (!fft_in_l || !fft_in_r ||
902 !fft_out_l || !fft_out_r) {
903 ret = AVERROR(ENOMEM);
904 goto fail;
905 }
906 }
907
908 for (i = 0; i < s->n_conv; i++) {
909 float *lir, *rir;
910
911 offset = i * n_samples; /* no. samples already written */
912
913 lir = data_ir_l + offset;
914 rir = data_ir_r + offset;
915
916 if (s->type == TIME_DOMAIN) {
917 for (j = 0; j < ir_samples; j++) {
918 /* load reversed IRs of the specified source position
919 * sample-by-sample for left and right ear; and apply gain */
920 s->data_ir[0][offset + j] = lir[ir_samples - 1 - j] * gain_lin;
921 s->data_ir[1][offset + j] = rir[ir_samples - 1 - j] * gain_lin;
922 }
923 } else if (s->type == FREQUENCY_DOMAIN) {
924 memset(fft_in_l, 0, n_fft * sizeof(*fft_in_l));
925 memset(fft_in_r, 0, n_fft * sizeof(*fft_in_r));
926
927 offset = i * n_fft; /* no. samples already written */
928 for (j = 0; j < ir_samples; j++) {
929 /* load non-reversed IRs of the specified source position
930 * sample-by-sample and apply gain,
931 * L channel is loaded to real part, R channel to imag part,
932 * IRs are shifted by L and R delay */
933 fft_in_l[s->delay[0][i] + j].re = lir[j] * gain_lin;
934 fft_in_r[s->delay[1][i] + j].re = rir[j] * gain_lin;
935 }
936
937 /* actually transform to frequency domain (IRs -> HRTFs) */
938 s->tx_fn[0](s->fft[0], fft_out_l, fft_in_l, sizeof(*fft_in_l));
939 memcpy(data_hrtf_l + offset, fft_out_l, n_fft * sizeof(*fft_out_l));
940 s->tx_fn[1](s->fft[1], fft_out_r, fft_in_r, sizeof(*fft_in_r));
941 memcpy(data_hrtf_r + offset, fft_out_r, n_fft * sizeof(*fft_out_r));
942 }
943 }
944
945 if (s->type == FREQUENCY_DOMAIN) {
946 s->data_hrtf[0] = av_malloc_array(n_fft * s->n_conv, sizeof(AVComplexFloat));
947 s->data_hrtf[1] = av_malloc_array(n_fft * s->n_conv, sizeof(AVComplexFloat));
948 if (!s->data_hrtf[0] || !s->data_hrtf[1]) {
949 ret = AVERROR(ENOMEM);
950 goto fail;
951 }
952
953 memcpy(s->data_hrtf[0], data_hrtf_l, /* copy HRTF data to */
954 sizeof(AVComplexFloat) * n_conv * n_fft); /* filter struct */
955 memcpy(s->data_hrtf[1], data_hrtf_r,
956 sizeof(AVComplexFloat) * n_conv * n_fft);
957 }
958
959fail:
960 av_freep(&data_hrtf_l); /* free temporary HRTF memory */
961 av_freep(&data_hrtf_r);
962
963 av_freep(&data_ir_l); /* free temporary IR memory */
964 av_freep(&data_ir_r);
965
966 av_freep(&fft_out_l); /* free temporary FFT memory */
967 av_freep(&fft_out_r);
968
969 av_freep(&fft_in_l); /* free temporary FFT memory */
970 av_freep(&fft_in_r);
971
972 return ret;
973}
974
976{
977 SOFAlizerContext *s = ctx->priv;
978 int ret;
979
980 if (!s->filename) {
981 av_log(ctx, AV_LOG_ERROR, "Valid SOFA filename must be set.\n");
982 return AVERROR(EINVAL);
983 }
984
985 /* preload SOFA file, */
986 ret = preload_sofa(ctx, s->filename, &s->sample_rate);
987 if (ret) {
988 /* file loading error */
989 av_log(ctx, AV_LOG_ERROR, "Error while loading SOFA file: '%s'\n", s->filename);
990 } else { /* no file loading error, resampling not required */
991 av_log(ctx, AV_LOG_DEBUG, "File '%s' loaded.\n", s->filename);
992 }
993
994 if (ret) {
995 av_log(ctx, AV_LOG_ERROR, "No valid SOFA file could be loaded. Please specify valid SOFA file.\n");
996 return ret;
997 }
998
999 s->fdsp = avpriv_float_dsp_alloc(0);
1000 if (!s->fdsp)
1001 return AVERROR(ENOMEM);
1002
1003 return 0;
1004}
1005
1006static int config_input(AVFilterLink *inlink)
1007{
1008 AVFilterContext *ctx = inlink->dst;
1009 SOFAlizerContext *s = ctx->priv;
1010 int ret;
1011
1012 if (s->type == FREQUENCY_DOMAIN)
1013 s->nb_samples = s->framesize;
1014
1015 /* gain -3 dB per channel */
1016 s->gain_lfe = expf((s->gain - 3 * inlink->ch_layout.nb_channels + s->lfe_gain) / 20 * M_LN10);
1017
1018 s->n_conv = inlink->ch_layout.nb_channels;
1019
1020 /* load IRs to data_ir[0] and data_ir[1] for required directions */
1021 if ((ret = load_data(ctx, s->rotation, s->elevation, s->radius, inlink->sample_rate)) < 0)
1022 return ret;
1023
1024 av_log(ctx, AV_LOG_DEBUG, "Samplerate: %d Channels to convolute: %d, Length of ringbuffer: %d x %d\n",
1025 inlink->sample_rate, s->n_conv, inlink->ch_layout.nb_channels, s->buffer_length);
1026
1027 return 0;
1028}
1029
1031{
1032 SOFAlizerContext *s = ctx->priv;
1033
1034 close_sofa(&s->sofa);
1035 av_tx_uninit(&s->ifft[0]);
1036 av_tx_uninit(&s->ifft[1]);
1037 av_tx_uninit(&s->fft[0]);
1038 av_tx_uninit(&s->fft[1]);
1039 s->ifft[0] = NULL;
1040 s->ifft[1] = NULL;
1041 s->fft[0] = NULL;
1042 s->fft[1] = NULL;
1043 av_freep(&s->delay[0]);
1044 av_freep(&s->delay[1]);
1045 av_freep(&s->data_ir[0]);
1046 av_freep(&s->data_ir[1]);
1047 av_freep(&s->ringbuffer[0]);
1048 av_freep(&s->ringbuffer[1]);
1049 av_freep(&s->speaker_azim);
1050 av_freep(&s->speaker_elev);
1051 av_freep(&s->temp_src[0]);
1052 av_freep(&s->temp_src[1]);
1053 av_freep(&s->temp_afft[0]);
1054 av_freep(&s->temp_afft[1]);
1055 av_freep(&s->in_fft[0]);
1056 av_freep(&s->in_fft[1]);
1057 av_freep(&s->out_fft[0]);
1058 av_freep(&s->out_fft[1]);
1059 av_freep(&s->data_hrtf[0]);
1060 av_freep(&s->data_hrtf[1]);
1061 av_freep(&s->fdsp);
1062}
1063
1064#define OFFSET(x) offsetof(SOFAlizerContext, x)
1065#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1066
1067static const AVOption sofalizer_options[] = {
1068 { "sofa", "sofa filename", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
1069 { "gain", "set gain in dB", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, -20, 40, .flags = FLAGS },
1070 { "rotation", "set rotation" , OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl=0}, -360, 360, .flags = FLAGS },
1071 { "elevation", "set elevation", OFFSET(elevation), AV_OPT_TYPE_FLOAT, {.dbl=0}, -90, 90, .flags = FLAGS },
1072 { "radius", "set radius", OFFSET(radius), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 5, .flags = FLAGS },
1073 { "type", "set processing", OFFSET(type), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, .flags = FLAGS, .unit = "type" },
1074 { "time", "time domain", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, .flags = FLAGS, .unit = "type" },
1075 { "freq", "frequency domain", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, .flags = FLAGS, .unit = "type" },
1076 { "speakers", "set speaker custom positions", OFFSET(speakers_pos), AV_OPT_TYPE_STRING, {.str=0}, 0, 0, .flags = FLAGS },
1077 { "lfegain", "set lfe gain", OFFSET(lfe_gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, -20,40, .flags = FLAGS },
1078 { "framesize", "set frame size", OFFSET(framesize), AV_OPT_TYPE_INT, {.i64=1024},1024,96000, .flags = FLAGS },
1079 { "normalize", "normalize IRs", OFFSET(normalize), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, .flags = FLAGS },
1080 { "interpolate","interpolate IRs from neighbors", OFFSET(interpolate),AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
1081 { "minphase", "minphase IRs", OFFSET(minphase), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
1082 { "anglestep", "set neighbor search angle step", OFFSET(anglestep), AV_OPT_TYPE_FLOAT, {.dbl=.5}, 0.01, 10, .flags = FLAGS },
1083 { "radstep", "set neighbor search radius step", OFFSET(radstep), AV_OPT_TYPE_FLOAT, {.dbl=.01}, 0.01, 1, .flags = FLAGS },
1084 { NULL }
1085};
1086
1088
1089static const AVFilterPad inputs[] = {
1090 {
1091 .name = "default",
1092 .type = AVMEDIA_TYPE_AUDIO,
1093 .config_props = config_input,
1094 },
1095};
1096
1098 .p.name = "sofalizer",
1099 .p.description = NULL_IF_CONFIG_SMALL("SOFAlizer (Spatially Oriented Format for Acoustics)."),
1100 .p.priv_class = &sofalizer_class,
1101 .p.flags = AVFILTER_FLAG_SLICE_THREADS,
1102 .priv_size = sizeof(SOFAlizerContext),
1103 .init = init,
1104 .activate = activate,
1105 .uninit = uninit,
1109};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
static enum AVSampleFormat sample_fmts[]
Definition adpcmenc.c:933
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static const AVFilterPad inputs[]
Definition af_aap.c:299
static int config_input(AVFilterLink *inlink)
#define TIME_DOMAIN
#define FREQUENCY_DOMAIN
static int preload_sofa(AVFilterContext *ctx, char *filename, int *samplingrate)
const FFFilter ff_af_sofalizer
static int close_sofa(struct MySofa *sofa)
static int load_data(AVFilterContext *ctx, int azim, int elev, float radius, int sample_rate)
static int config_input(AVFilterLink *inlink)
static int sofalizer_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static const AVOption sofalizer_options[]
static int get_speaker_pos(AVFilterContext *ctx, float *speaker_azim, float *speaker_elev)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static int getfilter_float(AVFilterContext *ctx, float x, float y, float z, float *left, float *right, float *delay_left, float *delay_right)
#define OFFSET(x)
static int sofalizer_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static void parse_speaker_pos(AVFilterContext *ctx)
static int parse_channel_name(AVFilterContext *ctx, char **arg, int *rchannel)
static FILE * out
static AVFormatContext * ctx
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1076
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1718
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
Definition avfilter.c:1544
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition avfilter.c:1524
Main libavfilter public API header.
int av_sscanf(const char *string, const char *format,...)
Definition avsscanf.c:961
static uint32_t BS_FUNC read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FLAGS
Definition cmdutils.c:596
#define NULL
Definition coverity.c:32
static __device__ float fabsf(float a)
static const uint16_t channel_layouts[7]
Definition dca_lbr.c:112
static const int sample_rates[]
Definition dcaenc.h:34
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ret
Definition ffplay.c:391
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (w...
Definition formats.c:688
int ff_add_channel_layout(AVFilterChannelLayouts **l, const AVChannelLayout *channel_layout)
Definition formats.c:597
int ff_set_common_samplerates_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const int *samplerates)
Definition formats.c:1088
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition formats.c:771
int ff_set_sample_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const enum AVSampleFormat *fmts)
Definition formats.c:1217
#define fail
Definition test.h:479
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:172
#define AV_CHANNEL_LAYOUT_STEREO
enum AVChannel av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, unsigned int idx)
Get the channel with the given index in a channel layout.
AVChannel
enum AVChannel av_channel_from_string(const char *str)
This is the inverse function of av_channel_name().
@ AV_CHAN_LOW_FREQUENCY
@ AV_CHAN_BACK_RIGHT
@ AV_CHAN_FRONT_RIGHT_OF_CENTER
@ AV_CHAN_FRONT_LEFT
@ AV_CHAN_TOP_BACK_LEFT
@ AV_CHAN_TOP_FRONT_RIGHT
@ AV_CHAN_FRONT_RIGHT
@ AV_CHAN_BACK_CENTER
@ AV_CHAN_FRONT_CENTER
@ AV_CHAN_SIDE_RIGHT
@ AV_CHAN_WIDE_LEFT
@ AV_CHAN_FRONT_LEFT_OF_CENTER
@ AV_CHAN_LOW_FREQUENCY_2
@ AV_CHAN_TOP_BACK_CENTER
@ AV_CHAN_BACK_LEFT
@ AV_CHAN_SIDE_LEFT
@ AV_CHAN_SURROUND_DIRECT_RIGHT
@ AV_CHAN_TOP_FRONT_LEFT
@ AV_CHAN_TOP_BACK_RIGHT
@ AV_CHAN_STEREO_RIGHT
See above.
@ AV_CHAN_WIDE_RIGHT
@ AV_CHAN_SURROUND_DIRECT_LEFT
@ AV_CHAN_TOP_CENTER
@ AV_CHAN_STEREO_LEFT
Stereo downmix.
@ AV_CHAN_TOP_FRONT_CENTER
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:602
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
#define ff_clz
Definition intmath.h:141
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition avstring.c:179
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
uint32_t type
Definition jpegmpfenc.c:80
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
unsigned offset
Definition libaomenc.c:763
static int16_t mult(Float11 *f1, Float11 *f2)
Definition g726.c:60
const char * arg
Definition jacosubdec.c:66
#define FILTER_INPUTS(array)
Definition filters.h:279
#define FILTER_OUTPUTS(array)
Definition filters.h:280
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition filters.h:720
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition filters.h:692
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition filters.h:665
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:493
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:256
#define av_cold
Definition attributes.h:117
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition float_dsp.c:135
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
#define FFMAX3(a, b, c)
Definition macros.h:48
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
#define M_LN10
Definition mathematics.h:49
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
enum MovChannelLayoutTag * layouts
Definition mov_chan.c:335
#define av_strdup(s)
Definition ops_static.c:55
AVOptions.
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
float im
Definition tx.h:28
float re
Definition tx.h:28
A list of supported channel layouts.
Definition formats.h:85
An instance of a filter.
Definition avfilter.h:279
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:559
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:566
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:540
AVOption.
Definition opt.h:428
int n_samples
float * lir
struct MYSOFA_NEIGHBORHOOD * neighborhood
int max_delay
struct MYSOFA_HRTF * hrtf
int ir_samples
struct MYSOFA_LOOKUP * lookup
float * rir
float * fir
float * ringbuffer[2]
AVComplexFloat * in_fft[2]
AVComplexFloat * temp_afft[2]
AVTXContext * fft[2]
float * temp_src[2]
AVComplexFloat * out_fft[2]
VirtualSpeaker vspkrpos[64]
float * data_ir[2]
av_tx_fn itx_fn[2]
AVTXContext * ifft[2]
av_tx_fn tx_fn[2]
AVComplexFloat * data_hrtf[2]
AVFloatDSPContext * fdsp
Used for passing data between threads.
Definition v210dec.c:35
int * n_clippings
AVFrame * out
AVComplexFloat ** in_fft
AVComplexFloat ** out_fft
float ** ir
AVComplexFloat ** temp_afft
float ** temp_src
float ** ringbuffer
#define av_free(p)
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static char buffer[20]
Definition seek.c:32
static void interpolate(float *out, float v1, float v2, int size)
Definition twinvq.c:85
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition tx.c:295
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition tx.c:903
@ AV_TX_FLOAT_FFT
Standard complex to complex FFT with sample data type of AVComplexFloat, AVComplexDouble or AVComplex...
Definition tx.h:47
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition tx.h:151
int len
static double c[64]