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 {
62 float lfe_gain, gain_lfe;
75 FFTComplex *temp_fft[2];
76 FFTComplex *temp_afft[2];
78 FFTContext *fft[2], *ifft[2];
79 FFTComplex *data_hrtf[2];
81 AVFloatDSPContext *fdsp;
82 struct headphone_inputs {
91 static int parse_channel_name(char **arg, int *rchannel, char *buf)
93 int len, i, channel_id = 0;
94 int64_t layout, layout0;
96 if (sscanf(*arg, "%7[A-Z]%n", buf, &len)) {
97 layout0 = layout = av_get_channel_layout(buf);
98 for (i = 32; i > 0; i >>= 1) {
99 if (layout >= 1LL << i) {
104 if (channel_id >= 64 || layout0 != 1LL << channel_id)
105 return AVERROR(EINVAL);
106 *rchannel = channel_id;
110 return AVERROR(EINVAL);
113 static void parse_map(AVFilterContext *ctx)
115 HeadphoneContext *s = ctx->priv;
116 char *arg, *tokenizer, *p, *args = av_strdup(s->map);
117 uint64_t used_channels = 0;
127 for (i = 0; i < 64; i++) {
131 while ((arg = av_strtok(p, "|", &tokenizer))) {
136 if (parse_channel_name(&arg, &out_ch_id, buf)) {
137 av_log(ctx, AV_LOG_WARNING, "Failed to parse \'%s\' as channel name.\n", arg);
140 if (used_channels & (1ULL << out_ch_id)) {
141 av_log(ctx, AV_LOG_WARNING, "Ignoring duplicate channel '%s'.\n", buf);
144 used_channels |= 1ULL << out_ch_id;
145 if (out_ch_id == av_log2(AV_CH_LOW_FREQUENCY))
146 s->lfe_channel = s->nb_irs;
147 s->mapping[s->nb_irs] = out_ch_id;
151 if (s->hrir_fmt == HRIR_MULTI)
154 s->nb_inputs = s->nb_irs + 1;
159 typedef struct ThreadData {
167 FFTComplex **temp_fft;
168 FFTComplex **temp_afft;
171 static int headphone_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
173 HeadphoneContext *s = ctx->priv;
174 ThreadData *td = arg;
175 AVFrame *in = td->in, *out = td->out;
177 int *write = &td->write[jobnr];
178 const int *const delay = td->delay[jobnr];
179 const float *const ir = td->ir[jobnr];
180 int *n_clippings = &td->n_clippings[jobnr];
181 float *ringbuffer = td->ringbuffer[jobnr];
182 float *temp_src = td->temp_src[jobnr];
183 const int ir_len = s->ir_len;
184 const int air_len = s->air_len;
185 const float *src = (const float *)in->data[0];
186 float *dst = (float *)out->data[0];
187 const int in_channels = in->channels;
188 const int buffer_length = s->buffer_length;
189 const uint32_t modulo = (uint32_t)buffer_length - 1;
196 for (l = 0; l < in_channels; l++) {
197 buffer[l] = ringbuffer + l * buffer_length;
200 for (i = 0; i < in->nb_samples; i++) {
201 const float *temp_ir = ir;
204 for (l = 0; l < in_channels; l++) {
205 *(buffer[l] + wr) = src[l];
208 for (l = 0; l < in_channels; l++) {
209 const float *const bptr = buffer[l];
211 if (l == s->lfe_channel) {
212 *dst += *(buffer[s->lfe_channel] + wr) * s->gain_lfe;
217 read = (wr - *(delay + l) - (ir_len - 1) + buffer_length) & modulo;
219 if (read + ir_len < buffer_length) {
220 memcpy(temp_src, bptr + read, ir_len * sizeof(*temp_src));
222 int len = FFMIN(air_len - (read % ir_len), buffer_length - read);
224 memcpy(temp_src, bptr + read, len * sizeof(*temp_src));
225 memcpy(temp_src + len, bptr, (air_len - len) * sizeof(*temp_src));
228 dst[0] += s->fdsp->scalarproduct_float(temp_ir, temp_src, FFALIGN(ir_len, 32));
232 if (fabsf(dst[0]) > 1)
237 wr = (wr + 1) & modulo;
245 static int headphone_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
247 HeadphoneContext *s = ctx->priv;
248 ThreadData *td = arg;
249 AVFrame *in = td->in, *out = td->out;
251 int *write = &td->write[jobnr];
252 FFTComplex *hrtf = s->data_hrtf[jobnr];
253 int *n_clippings = &td->n_clippings[jobnr];
254 float *ringbuffer = td->ringbuffer[jobnr];
255 const int ir_len = s->ir_len;
256 const float *src = (const float *)in->data[0];
257 float *dst = (float *)out->data[0];
258 const int in_channels = in->channels;
259 const int buffer_length = s->buffer_length;
260 const uint32_t modulo = (uint32_t)buffer_length - 1;
261 FFTComplex *fft_in = s->temp_fft[jobnr];
262 FFTComplex *fft_acc = s->temp_afft[jobnr];
263 FFTContext *ifft = s->ifft[jobnr];
264 FFTContext *fft = s->fft[jobnr];
265 const int n_fft = s->n_fft;
266 const float fft_scale = 1.0f / s->n_fft;
267 FFTComplex *hrtf_offset;
274 n_read = FFMIN(ir_len, in->nb_samples);
275 for (j = 0; j < n_read; j++) {
276 dst[2 * j] = ringbuffer[wr];
277 ringbuffer[wr] = 0.0;
278 wr = (wr + 1) & modulo;
281 for (j = n_read; j < in->nb_samples; j++) {
285 memset(fft_acc, 0, sizeof(FFTComplex) * n_fft);
287 for (i = 0; i < in_channels; i++) {
288 if (i == s->lfe_channel) {
289 for (j = 0; j < in->nb_samples; j++) {
290 dst[2 * j] += src[i + j * in_channels] * s->gain_lfe;
296 hrtf_offset = hrtf + offset;
298 memset(fft_in, 0, sizeof(FFTComplex) * n_fft);
300 for (j = 0; j < in->nb_samples; j++) {
301 fft_in[j].re = src[j * in_channels + i];
304 av_fft_permute(fft, fft_in);
305 av_fft_calc(fft, fft_in);
306 for (j = 0; j < n_fft; j++) {
307 const FFTComplex *hcomplex = hrtf_offset + j;
308 const float re = fft_in[j].re;
309 const float im = fft_in[j].im;
311 fft_acc[j].re += re * hcomplex->re - im * hcomplex->im;
312 fft_acc[j].im += re * hcomplex->im + im * hcomplex->re;
316 av_fft_permute(ifft, fft_acc);
317 av_fft_calc(ifft, fft_acc);
319 for (j = 0; j < in->nb_samples; j++) {
320 dst[2 * j] += fft_acc[j].re * fft_scale;
323 for (j = 0; j < ir_len - 1; j++) {
324 int write_pos = (wr + j) & modulo;
326 *(ringbuffer + write_pos) += fft_acc[in->nb_samples + j].re * fft_scale;
329 for (i = 0; i < out->nb_samples; i++) {
330 if (fabsf(dst[0]) > 1) {
342 static int check_ir(AVFilterLink *inlink, int input_number)
344 AVFilterContext *ctx = inlink->dst;
345 HeadphoneContext *s = ctx->priv;
346 int ir_len, max_ir_len;
348 ir_len = ff_inlink_queued_samples(inlink);
350 if (ir_len > max_ir_len) {
351 av_log(ctx, AV_LOG_ERROR, "Too big length of IRs: %d > %d.\n", ir_len, max_ir_len);
352 return AVERROR(EINVAL);
354 s->in[input_number].ir_len = ir_len;
355 s->ir_len = FFMAX(ir_len, s->ir_len);
360 static int headphone_frame(HeadphoneContext *s, AVFrame *in, AVFilterLink *outlink)
362 AVFilterContext *ctx = outlink->src;
363 int n_clippings[2] = { 0 };
367 out = ff_get_audio_buffer(outlink, in->nb_samples);
370 return AVERROR(ENOMEM);
374 td.in = in; td.out = out; td.write = s->write;
375 td.delay = s->delay; td.ir = s->data_ir; td.n_clippings = n_clippings;
376 td.ringbuffer = s->ringbuffer; td.temp_src = s->temp_src;
377 td.temp_fft = s->temp_fft;
378 td.temp_afft = s->temp_afft;
380 if (s->type == TIME_DOMAIN) {
381 ctx->internal->execute(ctx, headphone_convolute, &td, NULL, 2);
383 ctx->internal->execute(ctx, headphone_fast_convolute, &td, NULL, 2);
387 if (n_clippings[0] + n_clippings[1] > 0) {
388 av_log(ctx, AV_LOG_WARNING, "%d of %d samples clipped. Please reduce gain.\n",
389 n_clippings[0] + n_clippings[1], out->nb_samples * 2);
393 return ff_filter_frame(outlink, out);
396 static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink)
398 struct HeadphoneContext *s = ctx->priv;
399 const int ir_len = s->ir_len;
400 int nb_irs = s->nb_irs;
401 int nb_input_channels = ctx->inputs[0]->channels;
402 float gain_lin = expf((s->gain - 3 * nb_input_channels) / 20 * M_LN10);
403 FFTComplex *data_hrtf_l = NULL;
404 FFTComplex *data_hrtf_r = NULL;
405 FFTComplex *fft_in_l = NULL;
406 FFTComplex *fft_in_r = NULL;
407 float *data_ir_l = NULL;
408 float *data_ir_r = NULL;
409 int offset = 0, ret = 0;
413 s->air_len = 1 << (32 - ff_clz(ir_len));
414 if (s->type == TIME_DOMAIN) {
415 s->air_len = FFALIGN(s->air_len, 32);
417 s->buffer_length = 1 << (32 - ff_clz(s->air_len));
418 s->n_fft = n_fft = 1 << (32 - ff_clz(ir_len + s->size));
420 if (s->type == FREQUENCY_DOMAIN) {
421 fft_in_l = av_calloc(n_fft, sizeof(*fft_in_l));
422 fft_in_r = av_calloc(n_fft, sizeof(*fft_in_r));
423 if (!fft_in_l || !fft_in_r) {
424 ret = AVERROR(ENOMEM);
428 av_fft_end(s->fft[0]);
429 av_fft_end(s->fft[1]);
430 s->fft[0] = av_fft_init(av_log2(s->n_fft), 0);
431 s->fft[1] = av_fft_init(av_log2(s->n_fft), 0);
432 av_fft_end(s->ifft[0]);
433 av_fft_end(s->ifft[1]);
434 s->ifft[0] = av_fft_init(av_log2(s->n_fft), 1);
435 s->ifft[1] = av_fft_init(av_log2(s->n_fft), 1);
437 if (!s->fft[0] || !s->fft[1] || !s->ifft[0] || !s->ifft[1]) {
438 av_log(ctx, AV_LOG_ERROR, "Unable to create FFT contexts of size %d.\n", s->n_fft);
439 ret = AVERROR(ENOMEM);
444 s->data_ir[0] = av_calloc(s->air_len, sizeof(float) * s->nb_irs);
445 s->data_ir[1] = av_calloc(s->air_len, sizeof(float) * s->nb_irs);
446 s->delay[0] = av_calloc(s->nb_irs, sizeof(float));
447 s->delay[1] = av_calloc(s->nb_irs, sizeof(float));
449 if (s->type == TIME_DOMAIN) {
450 s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
451 s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
453 s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float));
454 s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float));
455 s->temp_fft[0] = av_calloc(s->n_fft, sizeof(FFTComplex));
456 s->temp_fft[1] = av_calloc(s->n_fft, sizeof(FFTComplex));
457 s->temp_afft[0] = av_calloc(s->n_fft, sizeof(FFTComplex));
458 s->temp_afft[1] = av_calloc(s->n_fft, sizeof(FFTComplex));
459 if (!s->temp_fft[0] || !s->temp_fft[1] ||
460 !s->temp_afft[0] || !s->temp_afft[1]) {
461 ret = AVERROR(ENOMEM);
466 if (!s->data_ir[0] || !s->data_ir[1] ||
467 !s->ringbuffer[0] || !s->ringbuffer[1]) {
468 ret = AVERROR(ENOMEM);
472 if (s->type == TIME_DOMAIN) {
473 s->temp_src[0] = av_calloc(s->air_len, sizeof(float));
474 s->temp_src[1] = av_calloc(s->air_len, sizeof(float));
476 data_ir_l = av_calloc(nb_irs * s->air_len, sizeof(*data_ir_l));
477 data_ir_r = av_calloc(nb_irs * s->air_len, sizeof(*data_ir_r));
478 if (!data_ir_r || !data_ir_l || !s->temp_src[0] || !s->temp_src[1]) {
479 ret = AVERROR(ENOMEM);
483 data_hrtf_l = av_calloc(n_fft, sizeof(*data_hrtf_l) * nb_irs);
484 data_hrtf_r = av_calloc(n_fft, sizeof(*data_hrtf_r) * nb_irs);
485 if (!data_hrtf_r || !data_hrtf_l) {
486 ret = AVERROR(ENOMEM);
491 for (i = 0; i < s->nb_inputs - 1; i++) {
492 int len = s->in[i + 1].ir_len;
493 int delay_l = s->in[i + 1].delay_l;
494 int delay_r = s->in[i + 1].delay_r;
497 ret = ff_inlink_consume_samples(ctx->inputs[i + 1], len, len, &s->in[i + 1].frame);
500 ptr = (float *)s->in[i + 1].frame->extended_data[0];
502 if (s->hrir_fmt == HRIR_STEREO) {
505 for (j = 0; j < inlink->channels; j++) {
506 if (s->mapping[i] < 0) {
510 if ((av_channel_layout_extract_channel(inlink->channel_layout, j)) == (1LL << s->mapping[i])) {
518 if (s->type == TIME_DOMAIN) {
519 offset = idx * s->air_len;
520 for (j = 0; j < len; j++) {
521 data_ir_l[offset + j] = ptr[len * 2 - j * 2 - 2] * gain_lin;
522 data_ir_r[offset + j] = ptr[len * 2 - j * 2 - 1] * gain_lin;
525 memset(fft_in_l, 0, n_fft * sizeof(*fft_in_l));
526 memset(fft_in_r, 0, n_fft * sizeof(*fft_in_r));
528 offset = idx * n_fft;
529 for (j = 0; j < len; j++) {
530 fft_in_l[delay_l + j].re = ptr[j * 2 ] * gain_lin;
531 fft_in_r[delay_r + j].re = ptr[j * 2 + 1] * gain_lin;
534 av_fft_permute(s->fft[0], fft_in_l);
535 av_fft_calc(s->fft[0], fft_in_l);
536 memcpy(data_hrtf_l + offset, fft_in_l, n_fft * sizeof(*fft_in_l));
537 av_fft_permute(s->fft[0], fft_in_r);
538 av_fft_calc(s->fft[0], fft_in_r);
539 memcpy(data_hrtf_r + offset, fft_in_r, n_fft * sizeof(*fft_in_r));
542 int I, N = ctx->inputs[1]->channels;
544 for (k = 0; k < N / 2; k++) {
547 for (j = 0; j < inlink->channels; j++) {
548 if (s->mapping[k] < 0) {
552 if ((av_channel_layout_extract_channel(inlink->channel_layout, j)) == (1LL << s->mapping[k])) {
561 if (s->type == TIME_DOMAIN) {
562 offset = idx * s->air_len;
563 for (j = 0; j < len; j++) {
564 data_ir_l[offset + j] = ptr[len * N - j * N - N + I ] * gain_lin;
565 data_ir_r[offset + j] = ptr[len * N - j * N - N + I + 1] * gain_lin;
568 memset(fft_in_l, 0, n_fft * sizeof(*fft_in_l));
569 memset(fft_in_r, 0, n_fft * sizeof(*fft_in_r));
571 offset = idx * n_fft;
572 for (j = 0; j < len; j++) {
573 fft_in_l[delay_l + j].re = ptr[j * N + I ] * gain_lin;
574 fft_in_r[delay_r + j].re = ptr[j * N + I + 1] * gain_lin;
577 av_fft_permute(s->fft[0], fft_in_l);
578 av_fft_calc(s->fft[0], fft_in_l);
579 memcpy(data_hrtf_l + offset, fft_in_l, n_fft * sizeof(*fft_in_l));
580 av_fft_permute(s->fft[0], fft_in_r);
581 av_fft_calc(s->fft[0], fft_in_r);
582 memcpy(data_hrtf_r + offset, fft_in_r, n_fft * sizeof(*fft_in_r));
587 av_frame_free(&s->in[i + 1].frame);
590 if (s->type == TIME_DOMAIN) {
591 memcpy(s->data_ir[0], data_ir_l, sizeof(float) * nb_irs * s->air_len);
592 memcpy(s->data_ir[1], data_ir_r, sizeof(float) * nb_irs * s->air_len);
594 s->data_hrtf[0] = av_calloc(n_fft * s->nb_irs, sizeof(FFTComplex));
595 s->data_hrtf[1] = av_calloc(n_fft * s->nb_irs, sizeof(FFTComplex));
596 if (!s->data_hrtf[0] || !s->data_hrtf[1]) {
597 ret = AVERROR(ENOMEM);
601 memcpy(s->data_hrtf[0], data_hrtf_l,
602 sizeof(FFTComplex) * nb_irs * n_fft);
603 memcpy(s->data_hrtf[1], data_hrtf_r,
604 sizeof(FFTComplex) * nb_irs * n_fft);
611 for (i = 0; i < s->nb_inputs - 1; i++)
612 av_frame_free(&s->in[i + 1].frame);
614 av_freep(&data_ir_l);
615 av_freep(&data_ir_r);
617 av_freep(&data_hrtf_l);
618 av_freep(&data_hrtf_r);
626 static int activate(AVFilterContext *ctx)
628 HeadphoneContext *s = ctx->priv;
629 AVFilterLink *inlink = ctx->inputs[0];
630 AVFilterLink *outlink = ctx->outputs[0];
634 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
637 for (i = 1; i < s->nb_inputs; i++) {
641 if ((ret = check_ir(ctx->inputs[i], i)) < 0)
644 if (ff_outlink_get_status(ctx->inputs[i]) == AVERROR_EOF) {
645 if (!ff_inlink_queued_samples(ctx->inputs[i])) {
646 av_log(ctx, AV_LOG_ERROR, "No samples provided for "
647 "HRIR stream %d.\n", i - 1);
648 return AVERROR_INVALIDDATA;
652 if (ff_outlink_frame_wanted(ctx->outputs[0]))
653 ff_inlink_request_frame(ctx->inputs[i]);
662 if (!s->have_hrirs && s->eof_hrirs) {
663 ret = convert_coeffs(ctx, inlink);
668 if ((ret = ff_inlink_consume_samples(ctx->inputs[0], s->size, s->size, &in)) > 0) {
669 ret = headphone_frame(s, in, outlink);
677 FF_FILTER_FORWARD_STATUS(ctx->inputs[0], ctx->outputs[0]);
678 if (ff_outlink_frame_wanted(ctx->outputs[0]))
679 ff_inlink_request_frame(ctx->inputs[0]);
684 static int query_formats(AVFilterContext *ctx)
686 struct HeadphoneContext *s = ctx->priv;
687 AVFilterFormats *formats = NULL;
688 AVFilterChannelLayouts *layouts = NULL;
689 AVFilterChannelLayouts *stereo_layout = NULL;
690 AVFilterChannelLayouts *hrir_layouts = NULL;
693 ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLT);
696 ret = ff_set_common_formats(ctx, formats);
700 layouts = ff_all_channel_layouts();
702 return AVERROR(ENOMEM);
704 ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->outcfg.channel_layouts);
708 ret = ff_add_channel_layout(&stereo_layout, AV_CH_LAYOUT_STEREO);
711 ret = ff_channel_layouts_ref(stereo_layout, &ctx->outputs[0]->incfg.channel_layouts);
715 if (s->hrir_fmt == HRIR_MULTI) {
716 hrir_layouts = ff_all_channel_counts();
718 return AVERROR(ENOMEM);
719 ret = ff_channel_layouts_ref(hrir_layouts, &ctx->inputs[1]->outcfg.channel_layouts);
723 for (i = 1; i < s->nb_inputs; i++) {
724 ret = ff_channel_layouts_ref(stereo_layout, &ctx->inputs[i]->outcfg.channel_layouts);
730 formats = ff_all_samplerates();
732 return AVERROR(ENOMEM);
733 return ff_set_common_samplerates(ctx, formats);
736 static int config_input(AVFilterLink *inlink)
738 AVFilterContext *ctx = inlink->dst;
739 HeadphoneContext *s = ctx->priv;
741 if (s->nb_irs < inlink->channels) {
742 av_log(ctx, AV_LOG_ERROR, "Number of HRIRs must be >= %d.\n", inlink->channels);
743 return AVERROR(EINVAL);
749 static av_cold int init(AVFilterContext *ctx)
751 HeadphoneContext *s = ctx->priv;
756 .type = AVMEDIA_TYPE_AUDIO,
757 .config_props = config_input,
759 if ((ret = ff_insert_inpad(ctx, 0, &pad)) < 0)
763 av_log(ctx, AV_LOG_ERROR, "Valid mapping must be set.\n");
764 return AVERROR(EINVAL);
769 s->in = av_calloc(s->nb_inputs, sizeof(*s->in));
771 return AVERROR(ENOMEM);
773 for (i = 1; i < s->nb_inputs; i++) {
774 char *name = av_asprintf("hrir%d", i - 1);
777 .type = AVMEDIA_TYPE_AUDIO,
780 return AVERROR(ENOMEM);
781 if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
787 s->fdsp = avpriv_float_dsp_alloc(0);
789 return AVERROR(ENOMEM);
794 static int config_output(AVFilterLink *outlink)
796 AVFilterContext *ctx = outlink->src;
797 HeadphoneContext *s = ctx->priv;
798 AVFilterLink *inlink = ctx->inputs[0];
800 if (s->hrir_fmt == HRIR_MULTI) {
801 AVFilterLink *hrir_link = ctx->inputs[1];
803 if (hrir_link->channels < inlink->channels * 2) {
804 av_log(ctx, AV_LOG_ERROR, "Number of channels in HRIR stream must be >= %d.\n", inlink->channels * 2);
805 return AVERROR(EINVAL);
809 s->gain_lfe = expf((s->gain - 3 * inlink->channels + s->lfe_gain) / 20 * M_LN10);
814 static av_cold void uninit(AVFilterContext *ctx)
816 HeadphoneContext *s = ctx->priv;
818 av_fft_end(s->ifft[0]);
819 av_fft_end(s->ifft[1]);
820 av_fft_end(s->fft[0]);
821 av_fft_end(s->fft[1]);
822 av_freep(&s->delay[0]);
823 av_freep(&s->delay[1]);
824 av_freep(&s->data_ir[0]);
825 av_freep(&s->data_ir[1]);
826 av_freep(&s->ringbuffer[0]);
827 av_freep(&s->ringbuffer[1]);
828 av_freep(&s->temp_src[0]);
829 av_freep(&s->temp_src[1]);
830 av_freep(&s->temp_fft[0]);
831 av_freep(&s->temp_fft[1]);
832 av_freep(&s->temp_afft[0]);
833 av_freep(&s->temp_afft[1]);
834 av_freep(&s->data_hrtf[0]);
835 av_freep(&s->data_hrtf[1]);
839 for (unsigned i = 1; i < ctx->nb_inputs; i++)
840 av_freep(&ctx->input_pads[i].name);
843 #define OFFSET(x) offsetof(HeadphoneContext, x)
844 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
846 static const AVOption headphone_options[] = {
847 { "map", "set channels convolution mappings", OFFSET(map), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
848 { "gain", "set gain in dB", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, -20, 40, .flags = FLAGS },
849 { "lfe", "set lfe gain in dB", OFFSET(lfe_gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, -20, 40, .flags = FLAGS },
850 { "type", "set processing", OFFSET(type), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, .flags = FLAGS, "type" },
851 { "time", "time domain", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, .flags = FLAGS, "type" },
852 { "freq", "frequency domain", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, .flags = FLAGS, "type" },
853 { "size", "set frame size", OFFSET(size), AV_OPT_TYPE_INT, {.i64=1024},1024,96000, .flags = FLAGS },
854 { "hrir", "set hrir format", OFFSET(hrir_fmt), AV_OPT_TYPE_INT, {.i64=HRIR_STEREO}, 0, 1, .flags = FLAGS, "hrir" },
855 { "stereo", "hrir files have exactly 2 channels", 0, AV_OPT_TYPE_CONST, {.i64=HRIR_STEREO}, 0, 0, .flags = FLAGS, "hrir" },
856 { "multich", "single multichannel hrir file", 0, AV_OPT_TYPE_CONST, {.i64=HRIR_MULTI}, 0, 0, .flags = FLAGS, "hrir" },
860 AVFILTER_DEFINE_CLASS(headphone);
862 static const AVFilterPad outputs[] = {
865 .type = AVMEDIA_TYPE_AUDIO,
866 .config_props = config_output,
871 AVFilter ff_af_headphone = {
873 .description = NULL_IF_CONFIG_SMALL("Apply headphone binaural spatialization with HRTFs in additional streams."),
874 .priv_size = sizeof(HeadphoneContext),
875 .priv_class = &headphone_class,
878 .query_formats = query_formats,
879 .activate = activate,
882 .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_DYNAMIC_INPUTS,