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