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