FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
af_headphone.c
Go to the documentation of this file.
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"
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 
45 
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;
60 
61  float *ringbuffer[2];
62  int write[2];
63 
65  int n_fft;
66  int size;
67 
68  int *delay[2];
69  float *data_ir[2];
70  float *temp_src[2];
72 
73  FFTContext *fft[2], *ifft[2];
75 
80  int ir_len;
81  int delay_l;
82  int delay_r;
83  int eof;
84  } *in;
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 
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 {
147  int *write;
148  int **delay;
149  float **ir;
151  float **ringbuffer;
152  float **temp_src;
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 = 65536;
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 
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 
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 
599 {
600  struct HeadphoneContext *s = ctx->priv;
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;
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 
661 {
662  HeadphoneContext *s = ctx->priv;
663  int i, ret;
664 
665  AVFilterPad pad = {
666  .name = "in0",
667  .type = AVMEDIA_TYPE_AUDIO,
668  .config_props = config_input,
669  .filter_frame = filter_frame,
670  };
671  if ((ret = ff_insert_inpad(ctx, 0, &pad)) < 0)
672  return ret;
673 
674  if (!s->map) {
675  av_log(ctx, AV_LOG_ERROR, "Valid mapping must be set.\n");
676  return AVERROR(EINVAL);
677  }
678 
679  parse_map(ctx);
680 
681  s->in = av_calloc(s->nb_inputs, sizeof(*s->in));
682  if (!s->in)
683  return AVERROR(ENOMEM);
684 
685  for (i = 1; i < s->nb_inputs; i++) {
686  char *name = av_asprintf("hrir%d", i - 1);
687  AVFilterPad pad = {
688  .name = name,
689  .type = AVMEDIA_TYPE_AUDIO,
690  .filter_frame = read_ir,
691  };
692  if (!name)
693  return AVERROR(ENOMEM);
694  if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
695  av_freep(&pad.name);
696  return ret;
697  }
698  }
699 
701  if (!s->fdsp)
702  return AVERROR(ENOMEM);
703  s->pts = AV_NOPTS_VALUE;
704 
705  return 0;
706 }
707 
708 static int config_output(AVFilterLink *outlink)
709 {
710  AVFilterContext *ctx = outlink->src;
711  HeadphoneContext *s = ctx->priv;
712  AVFilterLink *inlink = ctx->inputs[0];
713  int i;
714 
715  if (s->type == TIME_DOMAIN)
716  s->size = 1024;
717  else
718  s->size = inlink->sample_rate;
719 
720  for (i = 0; i < s->nb_inputs; i++) {
721  s->in[i].fifo = av_audio_fifo_alloc(ctx->inputs[i]->format, ctx->inputs[i]->channels, 1024);
722  if (!s->in[i].fifo)
723  return AVERROR(ENOMEM);
724  }
725  s->gain_lfe = expf((s->gain - 3 * inlink->channels - 6 + s->lfe_gain) / 20 * M_LN10);
726 
727  return 0;
728 }
729 
730 static int request_frame(AVFilterLink *outlink)
731 {
732  AVFilterContext *ctx = outlink->src;
733  HeadphoneContext *s = ctx->priv;
734  int i, ret;
735 
736  for (i = 1; !s->eof_hrirs && i < s->nb_inputs; i++) {
737  if (!s->in[i].eof) {
738  ret = ff_request_frame(ctx->inputs[i]);
739  if (ret == AVERROR_EOF) {
740  s->in[i].eof = 1;
741  ret = 0;
742  }
743  return ret;
744  } else {
745  if (i == s->nb_inputs - 1)
746  s->eof_hrirs = 1;
747  }
748  }
749  return ff_request_frame(ctx->inputs[0]);
750 }
751 
753 {
754  HeadphoneContext *s = ctx->priv;
755  int i;
756 
757  av_fft_end(s->ifft[0]);
758  av_fft_end(s->ifft[1]);
759  av_fft_end(s->fft[0]);
760  av_fft_end(s->fft[1]);
761  av_freep(&s->delay[0]);
762  av_freep(&s->delay[1]);
763  av_freep(&s->data_ir[0]);
764  av_freep(&s->data_ir[1]);
765  av_freep(&s->ringbuffer[0]);
766  av_freep(&s->ringbuffer[1]);
767  av_freep(&s->temp_src[0]);
768  av_freep(&s->temp_src[1]);
769  av_freep(&s->temp_fft[0]);
770  av_freep(&s->temp_fft[1]);
771  av_freep(&s->data_hrtf[0]);
772  av_freep(&s->data_hrtf[1]);
773  av_freep(&s->fdsp);
774 
775  for (i = 0; i < s->nb_inputs; i++) {
776  av_frame_free(&s->in[i].frame);
777  av_audio_fifo_free(s->in[i].fifo);
778  if (ctx->input_pads && i)
779  av_freep(&ctx->input_pads[i].name);
780  }
781  av_freep(&s->in);
782 }
783 
784 #define OFFSET(x) offsetof(HeadphoneContext, x)
785 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
786 
787 static const AVOption headphone_options[] = {
788  { "map", "set channels convolution mappings", OFFSET(map), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
789  { "gain", "set gain in dB", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, -20, 40, .flags = FLAGS },
790  { "lfe", "set lfe gain in dB", OFFSET(lfe_gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, -20, 40, .flags = FLAGS },
791  { "type", "set processing", OFFSET(type), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, .flags = FLAGS, "type" },
792  { "time", "time domain", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, .flags = FLAGS, "type" },
793  { "freq", "frequency domain", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, .flags = FLAGS, "type" },
794  { NULL }
795 };
796 
797 AVFILTER_DEFINE_CLASS(headphone);
798 
799 static const AVFilterPad outputs[] = {
800  {
801  .name = "default",
802  .type = AVMEDIA_TYPE_AUDIO,
803  .config_props = config_output,
804  .request_frame = request_frame,
805  },
806  { NULL }
807 };
808 
810  .name = "headphone",
811  .description = NULL_IF_CONFIG_SMALL("Apply headphone binaural spatialization with HRTFs in additional streams."),
812  .priv_size = sizeof(HeadphoneContext),
813  .priv_class = &headphone_class,
814  .init = init,
815  .uninit = uninit,
817  .inputs = NULL,
818  .outputs = outputs,
820 };
#define NULL
Definition: coverity.c:32
static int config_output(AVFilterLink *outlink)
Definition: af_headphone.c:708
const char * s
Definition: avisynth_c.h:768
AVFrame * out
Definition: af_headphone.c:146
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:59
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:181
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
AVOption.
Definition: opt.h:246
av_cold void av_fft_end(FFTContext *s)
Definition: avfft.c:48
static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink)
Definition: af_headphone.c:386
float re
Definition: fft.c:82
float ** temp_src
Definition: af_headphone.c:152
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
Main libavfilter public API header.
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition: avfilter.h:105
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1780
float(* scalarproduct_float)(const float *v1, const float *v2, int len)
Calculate the scalar product of two vectors of floats.
Definition: float_dsp.h:175
FFTContext * fft[2]
Definition: af_headphone.c:73
FFTSample re
Definition: avfft.h:38
void av_fft_permute(FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling ff_fft_calc().
Definition: avfft.c:38
#define AV_CH_LAYOUT_STEREO
AVFloatDSPContext * fdsp
Definition: af_headphone.c:76
#define src
Definition: vp8dsp.c:254
#define TIME_DOMAIN
Definition: af_headphone.c:35
static int read_ir(AVFilterLink *inlink, AVFrame *frame)
Definition: af_headphone.c:323
#define log2(x)
Definition: libm.h:404
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:230
static int headphone_frame(HeadphoneContext *s, AVFilterLink *outlink)
Definition: af_headphone.c:349
const char * name
Pad name.
Definition: internal.h:60
uint64_t av_get_channel_layout(const char *name)
Return a channel layout id that matches name, or 0 if no match is found.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:435
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1151
float ** ringbuffer
Definition: af_headphone.c:151
static int parse_channel_name(HeadphoneContext *s, int x, char **arg, int *rchannel, char *buf)
Definition: af_headphone.c:87
AVFrame * in
Definition: af_headphone.c:146
static int request_frame(AVFilterLink *outlink)
Definition: af_headphone.c:730
#define av_cold
Definition: attributes.h:82
AVOptions.
int ** delay
Definition: af_headphone.c:148
#define OFFSET(x)
Definition: af_headphone.c:784
#define emms_c()
Definition: internal.h:54
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:294
#define AV_CH_LOW_FREQUENCY
static AVFrame * frame
static int flags
Definition: log.c:57
#define AVERROR_EOF
End of file.
Definition: error.h:55
FFTContext * ifft[2]
Definition: af_headphone.c:73
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
static int config_input(AVFilterLink *inlink)
Definition: af_headphone.c:641
A filter pad used for either input or output.
Definition: internal.h:54
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
#define expf(x)
Definition: libm.h:283
AVFilterPad * input_pads
array of input pads
Definition: avfilter.h:345
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:568
#define td
Definition: regdef.h:70
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:127
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:343
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:86
#define FLAGS
Definition: af_headphone.c:785
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:116
static void parse_map(AVFilterContext *ctx)
Definition: af_headphone.c:111
const char * arg
Definition: jacosubdec.c:66
static const AVFilterPad outputs[]
Definition: af_headphone.c:799
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:337
FFTContext * av_fft_init(int nbits, int inverse)
Set up a complex FFT.
Definition: avfft.c:28
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define FFMAX(a, b)
Definition: common.h:94
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_headphone.c:752
#define fail()
Definition: checkasm.h:109
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_headphone.c:568
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
AVFILTER_DEFINE_CLASS(headphone)
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
static int headphone_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_headphone.c:156
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:228
FFTComplex * data_hrtf[2]
Definition: af_headphone.c:74
Definition: fft.h:88
int channels
number of audio channels, only used for audio.
Definition: frame.h:506
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:96
#define ff_clz
Definition: intmath.h:142
AVS_Value args
Definition: avisynth_c.h:699
float * data_ir[2]
Definition: af_headphone.c:69
AVFormatContext * ctx
Definition: movenc.c:48
AVFilter ff_af_headphone
Definition: af_headphone.c:809
int * n_clippings
Definition: af_headphone.c:150
static int query_formats(AVFilterContext *ctx)
Definition: af_headphone.c:598
FFTComplex * temp_fft[2]
Definition: af_headphone.c:71
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (w...
Definition: formats.c:401
static const AVOption headphone_options[]
Definition: af_headphone.c:787
A list of supported channel layouts.
Definition: formats.h:85
FFTComplex ** temp_fft
Definition: af_headphone.c:153
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:379
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:237
FFT functions.
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
void * buf
Definition: avisynth_c.h:690
GLint GLenum type
Definition: opengl_enc.c:105
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
Rational number (pair of numerator and denominator).
Definition: rational.h:58
float im
Definition: fft.c:82
const char * name
Filter name.
Definition: avfilter.h:148
const VDPAUPixFmtMap * map
float * temp_src[2]
Definition: af_headphone.c:70
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
static av_cold int init(AVFilterContext *ctx)
Definition: af_headphone.c:660
float ** ir
Definition: af_headphone.c:149
float * ringbuffer[2]
Definition: af_headphone.c:61
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:112
struct HeadphoneContext::headphone_inputs * in
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:184
#define M_LN10
Definition: mathematics.h:43
static void fft(const int32_t in[2 *256], cplx32 out[256])
Definition: dcaenc.c:406
FFTSample im
Definition: avfft.h:38
if(ret< 0)
Definition: vf_mcdeint.c:279
uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
Get the channel with the given index in channel_layout.
#define av_free(p)
Audio FIFO Buffer.
int len
static int headphone_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_headphone.c:229
A list of supported formats for one end of a filter link.
Definition: formats.h:64
uint64_t layout
An instance of a filter.
Definition: avfilter.h:338
FILE * out
Definition: movenc.c:54
#define av_freep(p)
#define av_malloc_array(a, b)
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:405
formats
Definition: signature.h:48
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:248
void av_fft_calc(FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in av_fft_init().
Definition: avfft.c:43
#define FREQUENCY_DOMAIN
Definition: af_headphone.c:36
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:267
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
GLuint buffer
Definition: opengl_enc.c:102
const char * name
Definition: opengl_enc.c:103
static int ff_insert_inpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new input pad for the filter.
Definition: internal.h:277