2 * Copyright (C) 2017 Paul B Mahol
3 * Copyright (C) 2013-2015 Andreas Fuchs, Wolfgang Hrauda
4 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/avstring.h"
24 #include "libavutil/channel_layout.h"
25 #include "libavutil/float_dsp.h"
26 #include "libavutil/intmath.h"
27 #include "libavutil/opt.h"
28 #include "libavcodec/avfft.h"
36 #define FREQUENCY_DOMAIN 1
41 typedef struct HeadphoneContext {
60 float lfe_gain, gain_lfe;
73 FFTComplex *temp_fft[2];
74 FFTComplex *temp_afft[2];
76 FFTContext *fft[2], *ifft[2];
77 FFTComplex *data_hrtf[2];
79 AVFloatDSPContext *fdsp;
80 struct headphone_inputs {
90 static int parse_channel_name(char **arg, uint64_t *rchannel, char *buf)
92 int len, i, channel_id = 0;
93 uint64_t layout, layout0;
95 if (sscanf(*arg, "%7[A-Z]%n", buf, &len)) {
96 layout0 = layout = av_get_channel_layout(buf);
97 for (i = 32; i > 0; i >>= 1) {
98 if (layout >= 1LL << i) {
103 if (channel_id >= 64 || layout0 != 1ULL << channel_id)
104 return AVERROR(EINVAL);
109 return AVERROR(EINVAL);
112 static void parse_map(AVFilterContext *ctx)
114 HeadphoneContext *s = ctx->priv;
115 char *arg, *tokenizer, *p, *args = av_strdup(s->map);
116 uint64_t used_channels = 0;
125 while ((arg = av_strtok(p, "|", &tokenizer))) {
126 uint64_t out_channel;
130 if (parse_channel_name(&arg, &out_channel, buf)) {
131 av_log(ctx, AV_LOG_WARNING, "Failed to parse \'%s\' as channel name.\n", arg);
134 if (used_channels & out_channel) {
135 av_log(ctx, AV_LOG_WARNING, "Ignoring duplicate channel '%s'.\n", buf);
138 used_channels |= out_channel;
139 if (out_channel == AV_CH_LOW_FREQUENCY)
140 s->lfe_channel = s->nb_irs;
141 s->mapping[s->nb_irs] = out_channel;
145 if (s->hrir_fmt == HRIR_MULTI)
148 s->nb_inputs = s->nb_irs + 1;
153 typedef struct ThreadData {
161 FFTComplex **temp_fft;
162 FFTComplex **temp_afft;
165 static int headphone_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
167 HeadphoneContext *s = ctx->priv;
168 ThreadData *td = arg;
169 AVFrame *in = td->in, *out = td->out;
171 int *write = &td->write[jobnr];
172 const int *const delay = td->delay[jobnr];
173 const float *const ir = td->ir[jobnr];
174 int *n_clippings = &td->n_clippings[jobnr];
175 float *ringbuffer = td->ringbuffer[jobnr];
176 float *temp_src = td->temp_src[jobnr];
177 const int ir_len = s->ir_len;
178 const int air_len = s->air_len;
179 const float *src = (const float *)in->data[0];
180 float *dst = (float *)out->data[0];
181 const int in_channels = in->channels;
182 const int buffer_length = s->buffer_length;
183 const uint32_t modulo = (uint32_t)buffer_length - 1;
190 for (l = 0; l < in_channels; l++) {
191 buffer[l] = ringbuffer + l * buffer_length;
194 for (i = 0; i < in->nb_samples; i++) {
195 const float *temp_ir = ir;
198 for (l = 0; l < in_channels; l++) {
199 *(buffer[l] + wr) = src[l];
202 for (l = 0; l < in_channels; l++) {
203 const float *const bptr = buffer[l];
205 if (l == s->lfe_channel) {
206 *dst += *(buffer[s->lfe_channel] + wr) * s->gain_lfe;
211 read = (wr - *(delay + l) - (ir_len - 1) + buffer_length) & modulo;
213 if (read + ir_len < buffer_length) {
214 memcpy(temp_src, bptr + read, ir_len * sizeof(*temp_src));
216 int len = FFMIN(air_len - (read % ir_len), buffer_length - read);
218 memcpy(temp_src, bptr + read, len * sizeof(*temp_src));
219 memcpy(temp_src + len, bptr, (air_len - len) * sizeof(*temp_src));
222 dst[0] += s->fdsp->scalarproduct_float(temp_ir, temp_src, FFALIGN(ir_len, 32));
226 if (fabsf(dst[0]) > 1)
231 wr = (wr + 1) & modulo;
239 static int headphone_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
241 HeadphoneContext *s = ctx->priv;
242 ThreadData *td = arg;
243 AVFrame *in = td->in, *out = td->out;
245 int *write = &td->write[jobnr];
246 FFTComplex *hrtf = s->data_hrtf[jobnr];
247 int *n_clippings = &td->n_clippings[jobnr];
248 float *ringbuffer = td->ringbuffer[jobnr];
249 const int ir_len = s->ir_len;
250 const float *src = (const float *)in->data[0];
251 float *dst = (float *)out->data[0];
252 const int in_channels = in->channels;
253 const int buffer_length = s->buffer_length;
254 const uint32_t modulo = (uint32_t)buffer_length - 1;
255 FFTComplex *fft_in = s->temp_fft[jobnr];
256 FFTComplex *fft_acc = s->temp_afft[jobnr];
257 FFTContext *ifft = s->ifft[jobnr];
258 FFTContext *fft = s->fft[jobnr];
259 const int n_fft = s->n_fft;
260 const float fft_scale = 1.0f / s->n_fft;
261 FFTComplex *hrtf_offset;
268 n_read = FFMIN(ir_len, in->nb_samples);
269 for (j = 0; j < n_read; j++) {
270 dst[2 * j] = ringbuffer[wr];
271 ringbuffer[wr] = 0.0;
272 wr = (wr + 1) & modulo;
275 for (j = n_read; j < in->nb_samples; j++) {
279 memset(fft_acc, 0, sizeof(FFTComplex) * n_fft);
281 for (i = 0; i < in_channels; i++) {
282 if (i == s->lfe_channel) {
283 for (j = 0; j < in->nb_samples; j++) {
284 dst[2 * j] += src[i + j * in_channels] * s->gain_lfe;
290 hrtf_offset = hrtf + offset;
292 memset(fft_in, 0, sizeof(FFTComplex) * n_fft);
294 for (j = 0; j < in->nb_samples; j++) {
295 fft_in[j].re = src[j * in_channels + i];
298 av_fft_permute(fft, fft_in);
299 av_fft_calc(fft, fft_in);
300 for (j = 0; j < n_fft; j++) {
301 const FFTComplex *hcomplex = hrtf_offset + j;
302 const float re = fft_in[j].re;
303 const float im = fft_in[j].im;
305 fft_acc[j].re += re * hcomplex->re - im * hcomplex->im;
306 fft_acc[j].im += re * hcomplex->im + im * hcomplex->re;
310 av_fft_permute(ifft, fft_acc);
311 av_fft_calc(ifft, fft_acc);
313 for (j = 0; j < in->nb_samples; j++) {
314 dst[2 * j] += fft_acc[j].re * fft_scale;
317 for (j = 0; j < ir_len - 1; j++) {
318 int write_pos = (wr + j) & modulo;
320 *(ringbuffer + write_pos) += fft_acc[in->nb_samples + j].re * fft_scale;
323 for (i = 0; i < out->nb_samples; i++) {
324 if (fabsf(dst[0]) > 1) {
336 static int check_ir(AVFilterLink *inlink, int input_number)
338 AVFilterContext *ctx = inlink->dst;
339 HeadphoneContext *s = ctx->priv;
340 int ir_len, max_ir_len;
342 ir_len = ff_inlink_queued_samples(inlink);
344 if (ir_len > max_ir_len) {
345 av_log(ctx, AV_LOG_ERROR, "Too big length of IRs: %d > %d.\n", ir_len, max_ir_len);
346 return AVERROR(EINVAL);
348 s->in[input_number].ir_len = ir_len;
349 s->ir_len = FFMAX(ir_len, s->ir_len);
354 static int headphone_frame(HeadphoneContext *s, AVFrame *in, AVFilterLink *outlink)
356 AVFilterContext *ctx = outlink->src;
357 int n_clippings[2] = { 0 };
361 out = ff_get_audio_buffer(outlink, in->nb_samples);
364 return AVERROR(ENOMEM);
368 td.in = in; td.out = out; td.write = s->write;
369 td.delay = s->delay; td.ir = s->data_ir; td.n_clippings = n_clippings;
370 td.ringbuffer = s->ringbuffer; td.temp_src = s->temp_src;
371 td.temp_fft = s->temp_fft;
372 td.temp_afft = s->temp_afft;
374 if (s->type == TIME_DOMAIN) {
375 ctx->internal->execute(ctx, headphone_convolute, &td, NULL, 2);
377 ctx->internal->execute(ctx, headphone_fast_convolute, &td, NULL, 2);
381 if (n_clippings[0] + n_clippings[1] > 0) {
382 av_log(ctx, AV_LOG_WARNING, "%d of %d samples clipped. Please reduce gain.\n",
383 n_clippings[0] + n_clippings[1], out->nb_samples * 2);
387 return ff_filter_frame(outlink, out);
390 static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink)
392 struct HeadphoneContext *s = ctx->priv;
393 const int ir_len = s->ir_len;
394 int nb_irs = s->nb_irs;
395 int nb_input_channels = ctx->inputs[0]->channels;
396 float gain_lin = expf((s->gain - 3 * nb_input_channels) / 20 * M_LN10);
397 FFTComplex *data_hrtf_l = NULL;
398 FFTComplex *data_hrtf_r = NULL;
399 FFTComplex *fft_in_l = NULL;
400 FFTComplex *fft_in_r = NULL;
401 float *data_ir_l = NULL;
402 float *data_ir_r = NULL;
403 int offset = 0, ret = 0;
407 s->air_len = 1 << (32 - ff_clz(ir_len));
408 if (s->type == TIME_DOMAIN) {
409 s->air_len = FFALIGN(s->air_len, 32);
411 s->buffer_length = 1 << (32 - ff_clz(s->air_len));
412 s->n_fft = n_fft = 1 << (32 - ff_clz(ir_len + s->size));
414 if (s->type == FREQUENCY_DOMAIN) {
415 fft_in_l = av_calloc(n_fft, sizeof(*fft_in_l));
416 fft_in_r = av_calloc(n_fft, sizeof(*fft_in_r));
417 if (!fft_in_l || !fft_in_r) {
418 ret = AVERROR(ENOMEM);
422 s->fft[0] = av_fft_init(av_log2(s->n_fft), 0);
423 s->fft[1] = av_fft_init(av_log2(s->n_fft), 0);
424 s->ifft[0] = av_fft_init(av_log2(s->n_fft), 1);
425 s->ifft[1] = av_fft_init(av_log2(s->n_fft), 1);
427 if (!s->fft[0] || !s->fft[1] || !s->ifft[0] || !s->ifft[1]) {
428 av_log(ctx, AV_LOG_ERROR, "Unable to create FFT contexts of size %d.\n", s->n_fft);
429 ret = AVERROR(ENOMEM);
434 s->data_ir[0] = av_calloc(s->air_len, sizeof(float) * s->nb_irs);
435 s->data_ir[1] = av_calloc(s->air_len, sizeof(float) * s->nb_irs);
436 s->delay[0] = av_calloc(s->nb_irs, sizeof(float));
437 s->delay[1] = av_calloc(s->nb_irs, sizeof(float));
439 if (s->type == TIME_DOMAIN) {
440 s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
441 s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
443 s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float));
444 s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float));
445 s->temp_fft[0] = av_calloc(s->n_fft, sizeof(FFTComplex));
446 s->temp_fft[1] = av_calloc(s->n_fft, sizeof(FFTComplex));
447 s->temp_afft[0] = av_calloc(s->n_fft, sizeof(FFTComplex));
448 s->temp_afft[1] = av_calloc(s->n_fft, sizeof(FFTComplex));
449 if (!s->temp_fft[0] || !s->temp_fft[1] ||
450 !s->temp_afft[0] || !s->temp_afft[1]) {
451 ret = AVERROR(ENOMEM);
456 if (!s->data_ir[0] || !s->data_ir[1] ||
457 !s->ringbuffer[0] || !s->ringbuffer[1]) {
458 ret = AVERROR(ENOMEM);
462 if (s->type == TIME_DOMAIN) {
463 s->temp_src[0] = av_calloc(s->air_len, sizeof(float));
464 s->temp_src[1] = av_calloc(s->air_len, sizeof(float));
466 data_ir_l = av_calloc(nb_irs * s->air_len, sizeof(*data_ir_l));
467 data_ir_r = av_calloc(nb_irs * s->air_len, sizeof(*data_ir_r));
468 if (!data_ir_r || !data_ir_l || !s->temp_src[0] || !s->temp_src[1]) {
469 ret = AVERROR(ENOMEM);
473 data_hrtf_l = av_calloc(n_fft, sizeof(*data_hrtf_l) * nb_irs);
474 data_hrtf_r = av_calloc(n_fft, sizeof(*data_hrtf_r) * nb_irs);
475 if (!data_hrtf_r || !data_hrtf_l) {
476 ret = AVERROR(ENOMEM);
481 for (i = 0; i < s->nb_inputs - 1; i++) {
482 int len = s->in[i + 1].ir_len;
483 int delay_l = s->in[i + 1].delay_l;
484 int delay_r = s->in[i + 1].delay_r;
487 ret = ff_inlink_consume_samples(ctx->inputs[i + 1], len, len, &s->in[i + 1].frame);
490 ptr = (float *)s->in[i + 1].frame->extended_data[0];
492 if (s->hrir_fmt == HRIR_STEREO) {
495 for (j = 0; j < inlink->channels; j++) {
496 if ((av_channel_layout_extract_channel(inlink->channel_layout, j)) == s->mapping[i]) {
504 if (s->type == TIME_DOMAIN) {
505 offset = idx * s->air_len;
506 for (j = 0; j < len; j++) {
507 data_ir_l[offset + j] = ptr[len * 2 - j * 2 - 2] * gain_lin;
508 data_ir_r[offset + j] = ptr[len * 2 - j * 2 - 1] * gain_lin;
511 memset(fft_in_l, 0, n_fft * sizeof(*fft_in_l));
512 memset(fft_in_r, 0, n_fft * sizeof(*fft_in_r));
514 offset = idx * n_fft;
515 for (j = 0; j < len; j++) {
516 fft_in_l[delay_l + j].re = ptr[j * 2 ] * gain_lin;
517 fft_in_r[delay_r + j].re = ptr[j * 2 + 1] * gain_lin;
520 av_fft_permute(s->fft[0], fft_in_l);
521 av_fft_calc(s->fft[0], fft_in_l);
522 memcpy(data_hrtf_l + offset, fft_in_l, n_fft * sizeof(*fft_in_l));
523 av_fft_permute(s->fft[0], fft_in_r);
524 av_fft_calc(s->fft[0], fft_in_r);
525 memcpy(data_hrtf_r + offset, fft_in_r, n_fft * sizeof(*fft_in_r));
528 int I, N = ctx->inputs[1]->channels;
530 for (k = 0; k < N / 2; k++) {
533 for (j = 0; j < inlink->channels; j++) {
534 if ((av_channel_layout_extract_channel(inlink->channel_layout, j)) == s->mapping[k]) {
543 if (s->type == TIME_DOMAIN) {
544 offset = idx * s->air_len;
545 for (j = 0; j < len; j++) {
546 data_ir_l[offset + j] = ptr[len * N - j * N - N + I ] * gain_lin;
547 data_ir_r[offset + j] = ptr[len * N - j * N - N + I + 1] * gain_lin;
550 memset(fft_in_l, 0, n_fft * sizeof(*fft_in_l));
551 memset(fft_in_r, 0, n_fft * sizeof(*fft_in_r));
553 offset = idx * n_fft;
554 for (j = 0; j < len; j++) {
555 fft_in_l[delay_l + j].re = ptr[j * N + I ] * gain_lin;
556 fft_in_r[delay_r + j].re = ptr[j * N + I + 1] * gain_lin;
559 av_fft_permute(s->fft[0], fft_in_l);
560 av_fft_calc(s->fft[0], fft_in_l);
561 memcpy(data_hrtf_l + offset, fft_in_l, n_fft * sizeof(*fft_in_l));
562 av_fft_permute(s->fft[0], fft_in_r);
563 av_fft_calc(s->fft[0], fft_in_r);
564 memcpy(data_hrtf_r + offset, fft_in_r, n_fft * sizeof(*fft_in_r));
569 av_frame_free(&s->in[i + 1].frame);
572 if (s->type == TIME_DOMAIN) {
573 memcpy(s->data_ir[0], data_ir_l, sizeof(float) * nb_irs * s->air_len);
574 memcpy(s->data_ir[1], data_ir_r, sizeof(float) * nb_irs * s->air_len);
576 s->data_hrtf[0] = av_calloc(n_fft * s->nb_irs, sizeof(FFTComplex));
577 s->data_hrtf[1] = av_calloc(n_fft * s->nb_irs, sizeof(FFTComplex));
578 if (!s->data_hrtf[0] || !s->data_hrtf[1]) {
579 ret = AVERROR(ENOMEM);
583 memcpy(s->data_hrtf[0], data_hrtf_l,
584 sizeof(FFTComplex) * nb_irs * n_fft);
585 memcpy(s->data_hrtf[1], data_hrtf_r,
586 sizeof(FFTComplex) * nb_irs * n_fft);
593 for (i = 0; i < s->nb_inputs - 1; i++)
594 av_frame_free(&s->in[i + 1].frame);
596 av_freep(&data_ir_l);
597 av_freep(&data_ir_r);
599 av_freep(&data_hrtf_l);
600 av_freep(&data_hrtf_r);
608 static int activate(AVFilterContext *ctx)
610 HeadphoneContext *s = ctx->priv;
611 AVFilterLink *inlink = ctx->inputs[0];
612 AVFilterLink *outlink = ctx->outputs[0];
616 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
619 for (i = 1; i < s->nb_inputs; i++) {
623 if ((ret = check_ir(ctx->inputs[i], i)) < 0)
626 if (ff_outlink_get_status(ctx->inputs[i]) == AVERROR_EOF) {
627 if (!ff_inlink_queued_samples(ctx->inputs[i])) {
628 av_log(ctx, AV_LOG_ERROR, "No samples provided for "
629 "HRIR stream %d.\n", i - 1);
630 return AVERROR_INVALIDDATA;
634 if (ff_outlink_frame_wanted(ctx->outputs[0]))
635 ff_inlink_request_frame(ctx->inputs[i]);
643 ret = convert_coeffs(ctx, inlink);
646 } else if (!s->have_hrirs)
649 if ((ret = ff_inlink_consume_samples(ctx->inputs[0], s->size, s->size, &in)) > 0) {
650 ret = headphone_frame(s, in, outlink);
658 FF_FILTER_FORWARD_STATUS(ctx->inputs[0], ctx->outputs[0]);
659 if (ff_outlink_frame_wanted(ctx->outputs[0]))
660 ff_inlink_request_frame(ctx->inputs[0]);
665 static int query_formats(AVFilterContext *ctx)
667 struct HeadphoneContext *s = ctx->priv;
668 AVFilterFormats *formats = NULL;
669 AVFilterChannelLayouts *layouts = NULL;
670 AVFilterChannelLayouts *stereo_layout = NULL;
671 AVFilterChannelLayouts *hrir_layouts = NULL;
674 ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLT);
677 ret = ff_set_common_formats(ctx, formats);
681 layouts = ff_all_channel_layouts();
683 return AVERROR(ENOMEM);
685 ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->outcfg.channel_layouts);
689 ret = ff_add_channel_layout(&stereo_layout, AV_CH_LAYOUT_STEREO);
692 ret = ff_channel_layouts_ref(stereo_layout, &ctx->outputs[0]->incfg.channel_layouts);
696 if (s->hrir_fmt == HRIR_MULTI) {
697 hrir_layouts = ff_all_channel_counts();
699 return AVERROR(ENOMEM);
700 ret = ff_channel_layouts_ref(hrir_layouts, &ctx->inputs[1]->outcfg.channel_layouts);
704 for (i = 1; i < s->nb_inputs; i++) {
705 ret = ff_channel_layouts_ref(stereo_layout, &ctx->inputs[i]->outcfg.channel_layouts);
711 formats = ff_all_samplerates();
713 return AVERROR(ENOMEM);
714 return ff_set_common_samplerates(ctx, formats);
717 static int config_input(AVFilterLink *inlink)
719 AVFilterContext *ctx = inlink->dst;
720 HeadphoneContext *s = ctx->priv;
722 if (s->nb_irs < inlink->channels) {
723 av_log(ctx, AV_LOG_ERROR, "Number of HRIRs must be >= %d.\n", inlink->channels);
724 return AVERROR(EINVAL);
730 static av_cold int init(AVFilterContext *ctx)
732 HeadphoneContext *s = ctx->priv;
737 .type = AVMEDIA_TYPE_AUDIO,
738 .config_props = config_input,
740 if ((ret = ff_insert_inpad(ctx, 0, &pad)) < 0)
744 av_log(ctx, AV_LOG_ERROR, "Valid mapping must be set.\n");
745 return AVERROR(EINVAL);
750 s->in = av_calloc(s->nb_inputs, sizeof(*s->in));
752 return AVERROR(ENOMEM);
754 for (i = 1; i < s->nb_inputs; i++) {
755 char *name = av_asprintf("hrir%d", i - 1);
758 .type = AVMEDIA_TYPE_AUDIO,
761 return AVERROR(ENOMEM);
762 if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
768 s->fdsp = avpriv_float_dsp_alloc(0);
770 return AVERROR(ENOMEM);
775 static int config_output(AVFilterLink *outlink)
777 AVFilterContext *ctx = outlink->src;
778 HeadphoneContext *s = ctx->priv;
779 AVFilterLink *inlink = ctx->inputs[0];
781 if (s->hrir_fmt == HRIR_MULTI) {
782 AVFilterLink *hrir_link = ctx->inputs[1];
784 if (hrir_link->channels < inlink->channels * 2) {
785 av_log(ctx, AV_LOG_ERROR, "Number of channels in HRIR stream must be >= %d.\n", inlink->channels * 2);
786 return AVERROR(EINVAL);
790 s->gain_lfe = expf((s->gain - 3 * inlink->channels + s->lfe_gain) / 20 * M_LN10);
795 static av_cold void uninit(AVFilterContext *ctx)
797 HeadphoneContext *s = ctx->priv;
799 av_fft_end(s->ifft[0]);
800 av_fft_end(s->ifft[1]);
801 av_fft_end(s->fft[0]);
802 av_fft_end(s->fft[1]);
803 av_freep(&s->delay[0]);
804 av_freep(&s->delay[1]);
805 av_freep(&s->data_ir[0]);
806 av_freep(&s->data_ir[1]);
807 av_freep(&s->ringbuffer[0]);
808 av_freep(&s->ringbuffer[1]);
809 av_freep(&s->temp_src[0]);
810 av_freep(&s->temp_src[1]);
811 av_freep(&s->temp_fft[0]);
812 av_freep(&s->temp_fft[1]);
813 av_freep(&s->temp_afft[0]);
814 av_freep(&s->temp_afft[1]);
815 av_freep(&s->data_hrtf[0]);
816 av_freep(&s->data_hrtf[1]);
820 for (unsigned i = 1; i < ctx->nb_inputs; i++)
821 av_freep(&ctx->input_pads[i].name);
824 #define OFFSET(x) offsetof(HeadphoneContext, x)
825 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
827 static const AVOption headphone_options[] = {
828 { "map", "set channels convolution mappings", OFFSET(map), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
829 { "gain", "set gain in dB", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, -20, 40, .flags = FLAGS },
830 { "lfe", "set lfe gain in dB", OFFSET(lfe_gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, -20, 40, .flags = FLAGS },
831 { "type", "set processing", OFFSET(type), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, .flags = FLAGS, "type" },
832 { "time", "time domain", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, .flags = FLAGS, "type" },
833 { "freq", "frequency domain", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, .flags = FLAGS, "type" },
834 { "size", "set frame size", OFFSET(size), AV_OPT_TYPE_INT, {.i64=1024},1024,96000, .flags = FLAGS },
835 { "hrir", "set hrir format", OFFSET(hrir_fmt), AV_OPT_TYPE_INT, {.i64=HRIR_STEREO}, 0, 1, .flags = FLAGS, "hrir" },
836 { "stereo", "hrir files have exactly 2 channels", 0, AV_OPT_TYPE_CONST, {.i64=HRIR_STEREO}, 0, 0, .flags = FLAGS, "hrir" },
837 { "multich", "single multichannel hrir file", 0, AV_OPT_TYPE_CONST, {.i64=HRIR_MULTI}, 0, 0, .flags = FLAGS, "hrir" },
841 AVFILTER_DEFINE_CLASS(headphone);
843 static const AVFilterPad outputs[] = {
846 .type = AVMEDIA_TYPE_AUDIO,
847 .config_props = config_output,
852 AVFilter ff_af_headphone = {
854 .description = NULL_IF_CONFIG_SMALL("Apply headphone binaural spatialization with HRTFs in additional streams."),
855 .priv_size = sizeof(HeadphoneContext),
856 .priv_class = &headphone_class,
859 .query_formats = query_formats,
860 .activate = activate,
863 .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_DYNAMIC_INPUTS,