FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
af_surround.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Paul B Mahol
3  *
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 "libavutil/audio_fifo.h"
23 #include "libavutil/opt.h"
24 #include "libavcodec/avfft.h"
25 #include "avfilter.h"
26 #include "audio.h"
27 #include "formats.h"
28 
29 typedef struct AudioSurroundContext {
30  const AVClass *class;
31 
34 
35  float level_in;
36  float level_out;
37  float fc_in;
38  float fc_out;
39  float lfe_in;
40  float lfe_out;
41 
42  float *input_levels;
43  float *output_levels;
45  int lowcutf;
46  int highcutf;
47 
48  float lowcut;
49  float highcut;
50 
55 
59 
60  int buf_size;
61  int hop_size;
65 
66  int64_t pts;
67 
70  float l_phase,
71  float r_phase,
72  float c_phase,
73  float mag_total,
74  float x, float y,
75  int n);
77  float l_phase,
78  float r_phase,
79  float c_phase,
80  float mag_total,
81  float lfe_im,
82  float lfe_re,
83  float x, float y,
84  int n);
86  float l_phase,
87  float r_phase,
88  float c_mag,
89  float c_phase,
90  float mag_total,
91  float x, float y,
92  int n);
94  float c_re, float c_im,
95  float lfe_re, float lfe_im,
96  float mag_totall, float mag_totalr,
97  float fl_phase, float fr_phase,
98  float bl_phase, float br_phase,
99  float sl_phase, float sr_phase,
100  float xl, float yl,
101  float xr, float yr,
102  int n);
104 
106 {
107  AudioSurroundContext *s = ctx->priv;
110  int ret;
111 
112  ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLTP);
113  if (ret)
114  return ret;
115  ret = ff_set_common_formats(ctx, formats);
116  if (ret)
117  return ret;
118 
119  layouts = NULL;
120  ret = ff_add_channel_layout(&layouts, s->out_channel_layout);
121  if (ret)
122  return ret;
123 
124  ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
125  if (ret)
126  return ret;
127 
128  layouts = NULL;
129  ret = ff_add_channel_layout(&layouts, s->in_channel_layout);
130  if (ret)
131  return ret;
132 
133  ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts);
134  if (ret)
135  return ret;
136 
137  formats = ff_all_samplerates();
138  if (!formats)
139  return AVERROR(ENOMEM);
140  return ff_set_common_samplerates(ctx, formats);
141 }
142 
143 static int config_input(AVFilterLink *inlink)
144 {
145  AVFilterContext *ctx = inlink->dst;
146  AudioSurroundContext *s = ctx->priv;
147  int ch;
148 
149  s->rdft = av_calloc(inlink->channels, sizeof(*s->rdft));
150  if (!s->rdft)
151  return AVERROR(ENOMEM);
152 
153  for (ch = 0; ch < inlink->channels; ch++) {
155  if (!s->rdft[ch])
156  return AVERROR(ENOMEM);
157  }
158  s->nb_in_channels = inlink->channels;
160  if (!s->input_levels)
161  return AVERROR(ENOMEM);
162  for (ch = 0; ch < s->nb_in_channels; ch++)
163  s->input_levels[ch] = s->level_in;
165  if (ch >= 0)
166  s->input_levels[ch] *= s->fc_in;
168  if (ch >= 0)
169  s->input_levels[ch] *= s->lfe_in;
170 
171  s->input = ff_get_audio_buffer(inlink, s->buf_size * 2);
172  if (!s->input)
173  return AVERROR(ENOMEM);
174 
175  s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->buf_size);
176  if (!s->fifo)
177  return AVERROR(ENOMEM);
178 
179  s->lowcut = 1.f * s->lowcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
180  s->highcut = 1.f * s->highcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
181 
182  return 0;
183 }
184 
185 static int config_output(AVFilterLink *outlink)
186 {
187  AVFilterContext *ctx = outlink->src;
188  AudioSurroundContext *s = ctx->priv;
189  int ch;
190 
191  s->irdft = av_calloc(outlink->channels, sizeof(*s->irdft));
192  if (!s->irdft)
193  return AVERROR(ENOMEM);
194 
195  for (ch = 0; ch < outlink->channels; ch++) {
197  if (!s->irdft[ch])
198  return AVERROR(ENOMEM);
199  }
200  s->nb_out_channels = outlink->channels;
202  if (!s->output_levels)
203  return AVERROR(ENOMEM);
204  for (ch = 0; ch < s->nb_out_channels; ch++)
205  s->output_levels[ch] = s->level_out;
207  if (ch >= 0)
208  s->output_levels[ch] *= s->fc_out;
210  if (ch >= 0)
211  s->output_levels[ch] *= s->lfe_out;
212 
213  s->output = ff_get_audio_buffer(outlink, s->buf_size * 2);
214  s->overlap_buffer = ff_get_audio_buffer(outlink, s->buf_size * 2);
215  if (!s->overlap_buffer || !s->output)
216  return AVERROR(ENOMEM);
217 
218  return 0;
219 }
220 
221 static void stereo_position(float a, float p, float *x, float *y)
222 {
223  *x = av_clipf(a+FFMAX(0, sinf(p-M_PI_2))*FFDIFFSIGN(a,0), -1, 1);
224  *y = av_clipf(cosf(a*M_PI_2+M_PI)*cosf(M_PI_2-p/M_PI)*M_LN10+1, -1, 1);
225 }
226 
227 static inline void get_lfe(int output_lfe, int n, float lowcut, float highcut,
228  float *lfe_mag, float *mag_total)
229 {
230  if (output_lfe && n < highcut) {
231  *lfe_mag = n < lowcut ? 1.f : .5f*(1.f+cosf(M_PI*(lowcut-n)/(lowcut-highcut)));
232  *lfe_mag *= *mag_total;
233  *mag_total -= *lfe_mag;
234  } else {
235  *lfe_mag = 0.f;
236  }
237 }
238 
240  float l_phase,
241  float r_phase,
242  float c_phase,
243  float mag_total,
244  float x, float y,
245  int n)
246 {
247  AudioSurroundContext *s = ctx->priv;
248  float mag, *dst;
249 
250  dst = (float *)s->output->extended_data[0];
251 
252  mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
253 
254  dst[2 * n ] = mag * cosf(c_phase);
255  dst[2 * n + 1] = mag * sinf(c_phase);
256 }
257 
259  float l_phase,
260  float r_phase,
261  float c_phase,
262  float mag_total,
263  float x, float y,
264  int n)
265 {
266  AudioSurroundContext *s = ctx->priv;
267  float l_mag, r_mag, *dstl, *dstr;
268 
269  dstl = (float *)s->output->extended_data[0];
270  dstr = (float *)s->output->extended_data[1];
271 
272  l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
273  r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
274 
275  dstl[2 * n ] = l_mag * cosf(l_phase);
276  dstl[2 * n + 1] = l_mag * sinf(l_phase);
277 
278  dstr[2 * n ] = r_mag * cosf(r_phase);
279  dstr[2 * n + 1] = r_mag * sinf(r_phase);
280 }
281 
283  float l_phase,
284  float r_phase,
285  float c_phase,
286  float mag_total,
287  float x, float y,
288  int n)
289 {
290  AudioSurroundContext *s = ctx->priv;
291  float lfe_mag, l_mag, r_mag, *dstl, *dstr, *dstlfe;
292 
293  dstl = (float *)s->output->extended_data[0];
294  dstr = (float *)s->output->extended_data[1];
295  dstlfe = (float *)s->output->extended_data[2];
296 
297  get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
298 
299  l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
300  r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
301 
302  dstl[2 * n ] = l_mag * cosf(l_phase);
303  dstl[2 * n + 1] = l_mag * sinf(l_phase);
304 
305  dstr[2 * n ] = r_mag * cosf(r_phase);
306  dstr[2 * n + 1] = r_mag * sinf(r_phase);
307 
308  dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
309  dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
310 }
311 
313  float l_phase,
314  float r_phase,
315  float c_phase,
316  float mag_total,
317  float x, float y,
318  int n)
319 {
320  AudioSurroundContext *s = ctx->priv;
321  float l_mag, r_mag, c_mag, *dstc, *dstl, *dstr;
322 
323  dstl = (float *)s->output->extended_data[0];
324  dstr = (float *)s->output->extended_data[1];
325  dstc = (float *)s->output->extended_data[2];
326 
327  c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
328  l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
329  r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
330 
331  dstl[2 * n ] = l_mag * cosf(l_phase);
332  dstl[2 * n + 1] = l_mag * sinf(l_phase);
333 
334  dstr[2 * n ] = r_mag * cosf(r_phase);
335  dstr[2 * n + 1] = r_mag * sinf(r_phase);
336 
337  dstc[2 * n ] = c_mag * cosf(c_phase);
338  dstc[2 * n + 1] = c_mag * sinf(c_phase);
339 }
340 
342  float l_phase,
343  float r_phase,
344  float c_phase,
345  float mag_total,
346  float x, float y,
347  int n)
348 {
349  AudioSurroundContext *s = ctx->priv;
350  float lfe_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstlfe;
351 
352  dstl = (float *)s->output->extended_data[0];
353  dstr = (float *)s->output->extended_data[1];
354  dstc = (float *)s->output->extended_data[2];
355  dstlfe = (float *)s->output->extended_data[3];
356 
357  get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
358 
359  c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
360  l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
361  r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
362 
363  dstl[2 * n ] = l_mag * cosf(l_phase);
364  dstl[2 * n + 1] = l_mag * sinf(l_phase);
365 
366  dstr[2 * n ] = r_mag * cosf(r_phase);
367  dstr[2 * n + 1] = r_mag * sinf(r_phase);
368 
369  dstc[2 * n ] = c_mag * cosf(c_phase);
370  dstc[2 * n + 1] = c_mag * sinf(c_phase);
371 
372  dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
373  dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
374 }
375 
377  float l_phase,
378  float r_phase,
379  float c_phase,
380  float c_mag,
381  float mag_total,
382  float x, float y,
383  int n)
384 {
385  AudioSurroundContext *s = ctx->priv;
386  float lfe_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
387 
388  dstl = (float *)s->output->extended_data[0];
389  dstr = (float *)s->output->extended_data[1];
390  dstc = (float *)s->output->extended_data[2];
391  dstlfe = (float *)s->output->extended_data[3];
392 
393  get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &c_mag);
394 
395  l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
396  r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
397 
398  dstl[2 * n ] = l_mag * cosf(l_phase);
399  dstl[2 * n + 1] = l_mag * sinf(l_phase);
400 
401  dstr[2 * n ] = r_mag * cosf(r_phase);
402  dstr[2 * n + 1] = r_mag * sinf(r_phase);
403 
404  dstc[2 * n ] = c_mag * cosf(c_phase);
405  dstc[2 * n + 1] = c_mag * sinf(c_phase);
406 
407  dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
408  dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
409 }
410 
412  float l_phase,
413  float r_phase,
414  float c_phase,
415  float mag_total,
416  float x, float y,
417  int n)
418 {
419  float b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb;
420  AudioSurroundContext *s = ctx->priv;
421 
422  dstl = (float *)s->output->extended_data[0];
423  dstr = (float *)s->output->extended_data[1];
424  dstc = (float *)s->output->extended_data[2];
425  dstb = (float *)s->output->extended_data[3];
426 
427  c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
428  b_mag = sqrtf(1.f - fabsf(x)) * ((1.f - y) * .5f) * mag_total;
429  l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
430  r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
431 
432  dstl[2 * n ] = l_mag * cosf(l_phase);
433  dstl[2 * n + 1] = l_mag * sinf(l_phase);
434 
435  dstr[2 * n ] = r_mag * cosf(r_phase);
436  dstr[2 * n + 1] = r_mag * sinf(r_phase);
437 
438  dstc[2 * n ] = c_mag * cosf(c_phase);
439  dstc[2 * n + 1] = c_mag * sinf(c_phase);
440 
441  dstb[2 * n ] = b_mag * cosf(c_phase);
442  dstb[2 * n + 1] = b_mag * sinf(c_phase);
443 }
444 
446  float l_phase,
447  float r_phase,
448  float c_phase,
449  float mag_total,
450  float x, float y,
451  int n)
452 {
453  float lfe_mag, b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb, *dstlfe;
454  AudioSurroundContext *s = ctx->priv;
455 
456  dstl = (float *)s->output->extended_data[0];
457  dstr = (float *)s->output->extended_data[1];
458  dstc = (float *)s->output->extended_data[2];
459  dstlfe = (float *)s->output->extended_data[3];
460  dstb = (float *)s->output->extended_data[4];
461 
462  get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
463 
464  dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
465  dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
466 
467  c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
468  b_mag = sqrtf(1.f - fabsf(x)) * ((1.f - y) * .5f) * mag_total;
469  l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
470  r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
471 
472  dstl[2 * n ] = l_mag * cosf(l_phase);
473  dstl[2 * n + 1] = l_mag * sinf(l_phase);
474 
475  dstr[2 * n ] = r_mag * cosf(r_phase);
476  dstr[2 * n + 1] = r_mag * sinf(r_phase);
477 
478  dstc[2 * n ] = c_mag * cosf(c_phase);
479  dstc[2 * n + 1] = c_mag * sinf(c_phase);
480 
481  dstb[2 * n ] = b_mag * cosf(c_phase);
482  dstb[2 * n + 1] = b_mag * sinf(c_phase);
483 }
484 
486  float l_phase,
487  float r_phase,
488  float c_phase,
489  float mag_total,
490  float x, float y,
491  int n)
492 {
493  float l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs;
494  AudioSurroundContext *s = ctx->priv;
495 
496  dstl = (float *)s->output->extended_data[0];
497  dstr = (float *)s->output->extended_data[1];
498  dstc = (float *)s->output->extended_data[2];
499  dstls = (float *)s->output->extended_data[3];
500  dstrs = (float *)s->output->extended_data[4];
501 
502  c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
503  l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
504  r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
505  ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
506  rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
507 
508  dstl[2 * n ] = l_mag * cosf(l_phase);
509  dstl[2 * n + 1] = l_mag * sinf(l_phase);
510 
511  dstr[2 * n ] = r_mag * cosf(r_phase);
512  dstr[2 * n + 1] = r_mag * sinf(r_phase);
513 
514  dstc[2 * n ] = c_mag * cosf(c_phase);
515  dstc[2 * n + 1] = c_mag * sinf(c_phase);
516 
517  dstls[2 * n ] = ls_mag * cosf(l_phase);
518  dstls[2 * n + 1] = ls_mag * sinf(l_phase);
519 
520  dstrs[2 * n ] = rs_mag * cosf(r_phase);
521  dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
522 }
523 
525  float l_phase,
526  float r_phase,
527  float c_phase,
528  float mag_total,
529  float x, float y,
530  int n)
531 {
532  float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlfe;
533  AudioSurroundContext *s = ctx->priv;
534 
535  dstl = (float *)s->output->extended_data[0];
536  dstr = (float *)s->output->extended_data[1];
537  dstc = (float *)s->output->extended_data[2];
538  dstlfe = (float *)s->output->extended_data[3];
539  dstls = (float *)s->output->extended_data[4];
540  dstrs = (float *)s->output->extended_data[5];
541 
542  get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
543 
544  c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
545  l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
546  r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
547  ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
548  rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
549 
550  dstl[2 * n ] = l_mag * cosf(l_phase);
551  dstl[2 * n + 1] = l_mag * sinf(l_phase);
552 
553  dstr[2 * n ] = r_mag * cosf(r_phase);
554  dstr[2 * n + 1] = r_mag * sinf(r_phase);
555 
556  dstc[2 * n ] = c_mag * cosf(c_phase);
557  dstc[2 * n + 1] = c_mag * sinf(c_phase);
558 
559  dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
560  dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
561 
562  dstls[2 * n ] = ls_mag * cosf(l_phase);
563  dstls[2 * n + 1] = ls_mag * sinf(l_phase);
564 
565  dstrs[2 * n ] = rs_mag * cosf(r_phase);
566  dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
567 }
568 
570  float l_phase,
571  float r_phase,
572  float c_phase,
573  float c_mag,
574  float mag_total,
575  float x, float y,
576  int n)
577 {
578  AudioSurroundContext *s = ctx->priv;
579  float lfe_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
580  float ls_mag, rs_mag, *dstls, *dstrs;
581 
582  dstl = (float *)s->output->extended_data[0];
583  dstr = (float *)s->output->extended_data[1];
584  dstc = (float *)s->output->extended_data[2];
585  dstlfe = (float *)s->output->extended_data[3];
586  dstls = (float *)s->output->extended_data[4];
587  dstrs = (float *)s->output->extended_data[5];
588 
589  get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &c_mag);
590 
591  l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
592  r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
593  ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
594  rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
595 
596  dstl[2 * n ] = l_mag * cosf(l_phase);
597  dstl[2 * n + 1] = l_mag * sinf(l_phase);
598 
599  dstr[2 * n ] = r_mag * cosf(r_phase);
600  dstr[2 * n + 1] = r_mag * sinf(r_phase);
601 
602  dstc[2 * n ] = c_mag * cosf(c_phase);
603  dstc[2 * n + 1] = c_mag * sinf(c_phase);
604 
605  dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
606  dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
607 
608  dstls[2 * n ] = ls_mag * cosf(l_phase);
609  dstls[2 * n + 1] = ls_mag * sinf(l_phase);
610 
611  dstrs[2 * n ] = rs_mag * cosf(r_phase);
612  dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
613 }
614 
616  float l_phase,
617  float r_phase,
618  float c_phase,
619  float mag_total,
620  float lfe_re,
621  float lfe_im,
622  float x, float y,
623  int n)
624 {
625  AudioSurroundContext *s = ctx->priv;
626  float c_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
627  float ls_mag, rs_mag, *dstls, *dstrs;
628 
629  dstl = (float *)s->output->extended_data[0];
630  dstr = (float *)s->output->extended_data[1];
631  dstc = (float *)s->output->extended_data[2];
632  dstlfe = (float *)s->output->extended_data[3];
633  dstls = (float *)s->output->extended_data[4];
634  dstrs = (float *)s->output->extended_data[5];
635 
636  c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
637  l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
638  r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
639  ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
640  rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
641 
642  dstl[2 * n ] = l_mag * cosf(l_phase);
643  dstl[2 * n + 1] = l_mag * sinf(l_phase);
644 
645  dstr[2 * n ] = r_mag * cosf(r_phase);
646  dstr[2 * n + 1] = r_mag * sinf(r_phase);
647 
648  dstc[2 * n ] = c_mag * cosf(c_phase);
649  dstc[2 * n + 1] = c_mag * sinf(c_phase);
650 
651  dstlfe[2 * n ] = lfe_re;
652  dstlfe[2 * n + 1] = lfe_im;
653 
654  dstls[2 * n ] = ls_mag * cosf(l_phase);
655  dstls[2 * n + 1] = ls_mag * sinf(l_phase);
656 
657  dstrs[2 * n ] = rs_mag * cosf(r_phase);
658  dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
659 }
660 
662  float l_phase,
663  float r_phase,
664  float c_phase,
665  float mag_total,
666  float x, float y,
667  int n)
668 {
669  float l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
670  float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb;
671  AudioSurroundContext *s = ctx->priv;
672 
673  dstl = (float *)s->output->extended_data[0];
674  dstr = (float *)s->output->extended_data[1];
675  dstc = (float *)s->output->extended_data[2];
676  dstlb = (float *)s->output->extended_data[3];
677  dstrb = (float *)s->output->extended_data[4];
678  dstls = (float *)s->output->extended_data[5];
679  dstrs = (float *)s->output->extended_data[6];
680 
681  c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
682  l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
683  r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
684  lb_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
685  rb_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
686  ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - fabsf(y)) * mag_total;
687  rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - fabsf(y)) * mag_total;
688 
689  dstl[2 * n ] = l_mag * cosf(l_phase);
690  dstl[2 * n + 1] = l_mag * sinf(l_phase);
691 
692  dstr[2 * n ] = r_mag * cosf(r_phase);
693  dstr[2 * n + 1] = r_mag * sinf(r_phase);
694 
695  dstc[2 * n ] = c_mag * cosf(c_phase);
696  dstc[2 * n + 1] = c_mag * sinf(c_phase);
697 
698  dstlb[2 * n ] = lb_mag * cosf(l_phase);
699  dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
700 
701  dstrb[2 * n ] = rb_mag * cosf(r_phase);
702  dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
703 
704  dstls[2 * n ] = ls_mag * cosf(l_phase);
705  dstls[2 * n + 1] = ls_mag * sinf(l_phase);
706 
707  dstrs[2 * n ] = rs_mag * cosf(r_phase);
708  dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
709 }
710 
712  float l_phase,
713  float r_phase,
714  float c_phase,
715  float mag_total,
716  float x, float y,
717  int n)
718 {
719  float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
720  float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
721  AudioSurroundContext *s = ctx->priv;
722 
723  dstl = (float *)s->output->extended_data[0];
724  dstr = (float *)s->output->extended_data[1];
725  dstc = (float *)s->output->extended_data[2];
726  dstlfe = (float *)s->output->extended_data[3];
727  dstlb = (float *)s->output->extended_data[4];
728  dstrb = (float *)s->output->extended_data[5];
729  dstls = (float *)s->output->extended_data[6];
730  dstrs = (float *)s->output->extended_data[7];
731 
732  get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
733 
734  c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
735  l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
736  r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
737  lb_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
738  rb_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
739  ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - fabsf(y)) * mag_total;
740  rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - fabsf(y)) * mag_total;
741 
742  dstl[2 * n ] = l_mag * cosf(l_phase);
743  dstl[2 * n + 1] = l_mag * sinf(l_phase);
744 
745  dstr[2 * n ] = r_mag * cosf(r_phase);
746  dstr[2 * n + 1] = r_mag * sinf(r_phase);
747 
748  dstc[2 * n ] = c_mag * cosf(c_phase);
749  dstc[2 * n + 1] = c_mag * sinf(c_phase);
750 
751  dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
752  dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
753 
754  dstlb[2 * n ] = lb_mag * cosf(l_phase);
755  dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
756 
757  dstrb[2 * n ] = rb_mag * cosf(r_phase);
758  dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
759 
760  dstls[2 * n ] = ls_mag * cosf(l_phase);
761  dstls[2 * n + 1] = ls_mag * sinf(l_phase);
762 
763  dstrs[2 * n ] = rs_mag * cosf(r_phase);
764  dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
765 }
766 
768  float c_re, float c_im,
769  float lfe_re, float lfe_im,
770  float mag_totall, float mag_totalr,
771  float fl_phase, float fr_phase,
772  float bl_phase, float br_phase,
773  float sl_phase, float sr_phase,
774  float xl, float yl,
775  float xr, float yr,
776  int n)
777 {
778  float fl_mag, fr_mag, ls_mag, rs_mag, lb_mag, rb_mag;
779  float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
780  AudioSurroundContext *s = ctx->priv;
781 
782  dstl = (float *)s->output->extended_data[0];
783  dstr = (float *)s->output->extended_data[1];
784  dstc = (float *)s->output->extended_data[2];
785  dstlfe = (float *)s->output->extended_data[3];
786  dstlb = (float *)s->output->extended_data[4];
787  dstrb = (float *)s->output->extended_data[5];
788  dstls = (float *)s->output->extended_data[6];
789  dstrs = (float *)s->output->extended_data[7];
790 
791  fl_mag = sqrtf(.5f * (xl + 1.f)) * ((yl + 1.f) * .5f) * mag_totall;
792  fr_mag = sqrtf(.5f * (xr + 1.f)) * ((yr + 1.f) * .5f) * mag_totalr;
793  lb_mag = sqrtf(.5f * (-xl + 1.f)) * ((yl + 1.f) * .5f) * mag_totall;
794  rb_mag = sqrtf(.5f * (-xr + 1.f)) * ((yr + 1.f) * .5f) * mag_totalr;
795  ls_mag = sqrtf(1.f - fabsf(xl)) * ((yl + 1.f) * .5f) * mag_totall;
796  rs_mag = sqrtf(1.f - fabsf(xr)) * ((yr + 1.f) * .5f) * mag_totalr;
797 
798  dstl[2 * n ] = fl_mag * cosf(fl_phase);
799  dstl[2 * n + 1] = fl_mag * sinf(fl_phase);
800 
801  dstr[2 * n ] = fr_mag * cosf(fr_phase);
802  dstr[2 * n + 1] = fr_mag * sinf(fr_phase);
803 
804  dstc[2 * n ] = c_re;
805  dstc[2 * n + 1] = c_im;
806 
807  dstlfe[2 * n ] = lfe_re;
808  dstlfe[2 * n + 1] = lfe_im;
809 
810  dstlb[2 * n ] = lb_mag * cosf(bl_phase);
811  dstlb[2 * n + 1] = lb_mag * sinf(bl_phase);
812 
813  dstrb[2 * n ] = rb_mag * cosf(br_phase);
814  dstrb[2 * n + 1] = rb_mag * sinf(br_phase);
815 
816  dstls[2 * n ] = ls_mag * cosf(sl_phase);
817  dstls[2 * n + 1] = ls_mag * sinf(sl_phase);
818 
819  dstrs[2 * n ] = rs_mag * cosf(sr_phase);
820  dstrs[2 * n + 1] = rs_mag * sinf(sr_phase);
821 }
822 
824 {
825  AudioSurroundContext *s = ctx->priv;
826  float *srcl, *srcr;
827  int n;
828 
829  srcl = (float *)s->input->extended_data[0];
830  srcr = (float *)s->input->extended_data[1];
831 
832  for (n = 0; n < s->buf_size; n++) {
833  float l_re = srcl[2 * n], r_re = srcr[2 * n];
834  float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
835  float c_phase = atan2f(l_im + r_im, l_re + r_re);
836  float l_mag = hypotf(l_re, l_im);
837  float r_mag = hypotf(r_re, r_im);
838  float l_phase = atan2f(l_im, l_re);
839  float r_phase = atan2f(r_im, r_re);
840  float phase_dif = fabsf(l_phase - r_phase);
841  float mag_dif = (l_mag - r_mag) / (l_mag + r_mag);
842  float mag_total = hypotf(l_mag, r_mag);
843  float x, y;
844 
845  if (phase_dif > M_PI)
846  phase_dif = 2 * M_PI - phase_dif;
847 
848  stereo_position(mag_dif, phase_dif, &x, &y);
849 
850  s->upmix_stereo(ctx, l_phase, r_phase, c_phase, mag_total, x, y, n);
851  }
852 }
853 
855 {
856  AudioSurroundContext *s = ctx->priv;
857  float *srcl, *srcr, *srcc;
858  int n;
859 
860  srcl = (float *)s->input->extended_data[0];
861  srcr = (float *)s->input->extended_data[1];
862  srcc = (float *)s->input->extended_data[2];
863 
864  for (n = 0; n < s->buf_size; n++) {
865  float l_re = srcl[2 * n], r_re = srcr[2 * n];
866  float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
867  float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
868  float c_mag = hypotf(c_re, c_im);
869  float c_phase = atan2f(c_im, c_re);
870  float l_mag = hypotf(l_re, l_im);
871  float r_mag = hypotf(r_re, r_im);
872  float l_phase = atan2f(l_im, l_re);
873  float r_phase = atan2f(r_im, r_re);
874  float phase_dif = fabsf(l_phase - r_phase);
875  float mag_dif = (l_mag - r_mag) / (l_mag + r_mag);
876  float mag_total = hypotf(l_mag, r_mag);
877  float x, y;
878 
879  if (phase_dif > M_PI)
880  phase_dif = 2 * M_PI - phase_dif;
881 
882  stereo_position(mag_dif, phase_dif, &x, &y);
883 
884  s->upmix_3_0(ctx, l_phase, r_phase, c_phase, c_mag, mag_total, x, y, n);
885  }
886 }
887 
889 {
890  AudioSurroundContext *s = ctx->priv;
891  float *srcl, *srcr, *srclfe;
892  int n;
893 
894  srcl = (float *)s->input->extended_data[0];
895  srcr = (float *)s->input->extended_data[1];
896  srclfe = (float *)s->input->extended_data[2];
897 
898  for (n = 0; n < s->buf_size; n++) {
899  float l_re = srcl[2 * n], r_re = srcr[2 * n];
900  float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
901  float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
902  float c_phase = atan2f(l_im + r_im, l_re + r_re);
903  float l_mag = hypotf(l_re, l_im);
904  float r_mag = hypotf(r_re, r_im);
905  float l_phase = atan2f(l_im, l_re);
906  float r_phase = atan2f(r_im, r_re);
907  float phase_dif = fabsf(l_phase - r_phase);
908  float mag_dif = (l_mag - r_mag) / (l_mag + r_mag);
909  float mag_total = hypotf(l_mag, r_mag);
910  float x, y;
911 
912  if (phase_dif > M_PI)
913  phase_dif = 2 * M_PI - phase_dif;
914 
915  stereo_position(mag_dif, phase_dif, &x, &y);
916 
917  s->upmix_2_1(ctx, l_phase, r_phase, c_phase, mag_total, lfe_re, lfe_im, x, y, n);
918  }
919 }
920 
922 {
923  AudioSurroundContext *s = ctx->priv;
924  float *srcl, *srcr, *srcc, *srclfe, *srcbl, *srcbr;
925  int n;
926 
927  srcl = (float *)s->input->extended_data[0];
928  srcr = (float *)s->input->extended_data[1];
929  srcc = (float *)s->input->extended_data[2];
930  srclfe = (float *)s->input->extended_data[3];
931  srcbl = (float *)s->input->extended_data[4];
932  srcbr = (float *)s->input->extended_data[5];
933 
934  for (n = 0; n < s->buf_size; n++) {
935  float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
936  float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
937  float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
938  float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
939  float bl_re = srcbl[2 * n], bl_im = srcbl[2 * n + 1];
940  float br_re = srcbr[2 * n], br_im = srcbr[2 * n + 1];
941  float fl_mag = hypotf(fl_re, fl_im);
942  float fr_mag = hypotf(fr_re, fr_im);
943  float fl_phase = atan2f(fl_im, fl_re);
944  float fr_phase = atan2f(fr_im, fr_re);
945  float bl_mag = hypotf(bl_re, bl_im);
946  float br_mag = hypotf(br_re, br_im);
947  float bl_phase = atan2f(bl_im, bl_re);
948  float br_phase = atan2f(br_im, br_re);
949  float phase_difl = fabsf(fl_phase - bl_phase);
950  float phase_difr = fabsf(fr_phase - br_phase);
951  float mag_difl = (fl_mag - bl_mag) / (fl_mag + bl_mag);
952  float mag_difr = (fr_mag - br_mag) / (fr_mag + br_mag);
953  float mag_totall = hypotf(fl_mag, bl_mag);
954  float mag_totalr = hypotf(fr_mag, br_mag);
955  float sl_phase = atan2f(fl_im + bl_im, fl_re + bl_re);
956  float sr_phase = atan2f(fr_im + br_im, fr_re + br_re);
957  float xl, yl;
958  float xr, yr;
959 
960  if (phase_difl > M_PI)
961  phase_difl = 2 * M_PI - phase_difl;
962 
963  if (phase_difr > M_PI)
964  phase_difr = 2 * M_PI - phase_difr;
965 
966  stereo_position(mag_difl, phase_difl, &xl, &yl);
967  stereo_position(mag_difr, phase_difr, &xr, &yr);
968 
969  s->upmix_5_1(ctx, c_re, c_im, lfe_re, lfe_im,
970  mag_totall, mag_totalr,
971  fl_phase, fr_phase,
972  bl_phase, br_phase,
973  sl_phase, sr_phase,
974  xl, yl, xr, yr, n);
975  }
976 }
977 
979 {
980  AudioSurroundContext *s = ctx->priv;
981  float overlap;
982  int i;
983 
985  av_log(ctx, AV_LOG_ERROR, "Error parsing output channel layout '%s'.\n",
987  return AVERROR(EINVAL);
988  }
989 
991  av_log(ctx, AV_LOG_ERROR, "Error parsing input channel layout '%s'.\n",
993  return AVERROR(EINVAL);
994  }
995 
996  if (s->lowcutf >= s->highcutf) {
997  av_log(ctx, AV_LOG_ERROR, "Low cut-off '%d' should be less than high cut-off '%d'.\n",
998  s->lowcutf, s->highcutf);
999  return AVERROR(EINVAL);
1000  }
1001 
1002  switch (s->in_channel_layout) {
1003  case AV_CH_LAYOUT_STEREO:
1004  s->filter = filter_stereo;
1005  switch (s->out_channel_layout) {
1006  case AV_CH_LAYOUT_MONO:
1007  s->upmix_stereo = upmix_1_0;
1008  break;
1009  case AV_CH_LAYOUT_STEREO:
1011  break;
1012  case AV_CH_LAYOUT_2POINT1:
1013  s->upmix_stereo = upmix_2_1;
1014  break;
1015  case AV_CH_LAYOUT_SURROUND:
1016  s->upmix_stereo = upmix_3_0;
1017  break;
1018  case AV_CH_LAYOUT_3POINT1:
1019  s->upmix_stereo = upmix_3_1;
1020  break;
1021  case AV_CH_LAYOUT_4POINT0:
1022  s->upmix_stereo = upmix_4_0;
1023  break;
1024  case AV_CH_LAYOUT_4POINT1:
1025  s->upmix_stereo = upmix_4_1;
1026  break;
1029  break;
1032  break;
1033  case AV_CH_LAYOUT_7POINT0:
1034  s->upmix_stereo = upmix_7_0;
1035  break;
1036  case AV_CH_LAYOUT_7POINT1:
1037  s->upmix_stereo = upmix_7_1;
1038  break;
1039  default:
1040  goto fail;
1041  }
1042  break;
1043  case AV_CH_LAYOUT_2POINT1:
1044  s->filter = filter_2_1;
1045  switch (s->out_channel_layout) {
1048  break;
1049  default:
1050  goto fail;
1051  }
1052  break;
1053  case AV_CH_LAYOUT_SURROUND:
1054  s->filter = filter_surround;
1055  switch (s->out_channel_layout) {
1056  case AV_CH_LAYOUT_3POINT1:
1058  break;
1061  break;
1062  default:
1063  goto fail;
1064  }
1065  break;
1067  s->filter = filter_5_1_back;
1068  switch (s->out_channel_layout) {
1069  case AV_CH_LAYOUT_7POINT1:
1070  s->upmix_5_1 = upmix_7_1_5_1;
1071  break;
1072  default:
1073  goto fail;
1074  }
1075  break;
1076  default:
1077 fail:
1078  av_log(ctx, AV_LOG_ERROR, "Unsupported upmix: '%s' -> '%s'.\n",
1080  return AVERROR(EINVAL);
1081  }
1082 
1083  s->buf_size = 4096;
1084  s->pts = AV_NOPTS_VALUE;
1085 
1086  s->window_func_lut = av_calloc(s->buf_size, sizeof(*s->window_func_lut));
1087  if (!s->window_func_lut)
1088  return AVERROR(ENOMEM);
1089 
1090  for (i = 0; i < s->buf_size; i++)
1091  s->window_func_lut[i] = sqrtf(0.5 * (1 - cosf(2 * M_PI * i / s->buf_size)) / s->buf_size);
1092  overlap = .5;
1093  s->hop_size = s->buf_size * (1. - overlap);
1094 
1095  return 0;
1096 }
1097 
1098 static int fft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
1099 {
1100  AudioSurroundContext *s = ctx->priv;
1101  const float level_in = s->input_levels[ch];
1102  float *dst;
1103  int n;
1104 
1105  memset(s->input->extended_data[ch] + s->buf_size * sizeof(float), 0, s->buf_size * sizeof(float));
1106 
1107  dst = (float *)s->input->extended_data[ch];
1108  for (n = 0; n < s->buf_size; n++) {
1109  dst[n] *= s->window_func_lut[n] * level_in;
1110  }
1111 
1112  av_rdft_calc(s->rdft[ch], (float *)s->input->extended_data[ch]);
1113 
1114  return 0;
1115 }
1116 
1117 static int ifft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
1118 {
1119  AudioSurroundContext *s = ctx->priv;
1120  const float level_out = s->output_levels[ch];
1121  AVFrame *out = arg;
1122  float *dst, *ptr;
1123  int n;
1124 
1125  av_rdft_calc(s->irdft[ch], (float *)s->output->extended_data[ch]);
1126 
1127  dst = (float *)s->output->extended_data[ch];
1128  ptr = (float *)s->overlap_buffer->extended_data[ch];
1129 
1130  memmove(s->overlap_buffer->extended_data[ch],
1131  s->overlap_buffer->extended_data[ch] + s->hop_size * sizeof(float),
1132  s->buf_size * sizeof(float));
1133  memset(s->overlap_buffer->extended_data[ch] + s->buf_size * sizeof(float),
1134  0, s->hop_size * sizeof(float));
1135 
1136  for (n = 0; n < s->buf_size; n++) {
1137  ptr[n] += dst[n] * s->window_func_lut[n] * level_out;
1138  }
1139 
1140  ptr = (float *)s->overlap_buffer->extended_data[ch];
1141  dst = (float *)out->extended_data[ch];
1142  memcpy(dst, ptr, s->hop_size * sizeof(float));
1143 
1144  return 0;
1145 }
1146 
1147 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
1148 {
1149  AVFilterContext *ctx = inlink->dst;
1150  AVFilterLink *outlink = ctx->outputs[0];
1151  AudioSurroundContext *s = ctx->priv;
1152 
1153  av_audio_fifo_write(s->fifo, (void **)in->extended_data,
1154  in->nb_samples);
1155 
1156  if (s->pts == AV_NOPTS_VALUE)
1157  s->pts = in->pts;
1158 
1159  av_frame_free(&in);
1160 
1161  while (av_audio_fifo_size(s->fifo) >= s->buf_size) {
1162  AVFrame *out;
1163  int ret;
1164 
1165  ret = av_audio_fifo_peek(s->fifo, (void **)s->input->extended_data, s->buf_size);
1166  if (ret < 0)
1167  return ret;
1168 
1169  ctx->internal->execute(ctx, fft_channel, NULL, NULL, inlink->channels);
1170 
1171  s->filter(ctx);
1172 
1173  out = ff_get_audio_buffer(outlink, s->hop_size);
1174  if (!out)
1175  return AVERROR(ENOMEM);
1176 
1177  ctx->internal->execute(ctx, ifft_channel, out, NULL, outlink->channels);
1178 
1179  out->pts = s->pts;
1180  if (s->pts != AV_NOPTS_VALUE)
1181  s->pts += av_rescale_q(out->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
1183  ret = ff_filter_frame(outlink, out);
1184  if (ret < 0)
1185  return ret;
1186  }
1187 
1188  return 0;
1189 }
1190 
1192 {
1193  AudioSurroundContext *s = ctx->priv;
1194  int ch;
1195 
1196  av_frame_free(&s->input);
1197  av_frame_free(&s->output);
1199 
1200  for (ch = 0; ch < s->nb_in_channels; ch++) {
1201  av_rdft_end(s->rdft[ch]);
1202  }
1203  for (ch = 0; ch < s->nb_out_channels; ch++) {
1204  av_rdft_end(s->irdft[ch]);
1205  }
1206  av_freep(&s->input_levels);
1207  av_freep(&s->output_levels);
1208  av_freep(&s->rdft);
1209  av_freep(&s->irdft);
1212 }
1213 
1214 #define OFFSET(x) offsetof(AudioSurroundContext, x)
1215 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1216 
1217 static const AVOption surround_options[] = {
1218  { "chl_out", "set output channel layout", OFFSET(out_channel_layout_str), AV_OPT_TYPE_STRING, {.str="5.1"}, 0, 0, FLAGS },
1219  { "chl_in", "set input channel layout", OFFSET(in_channel_layout_str), AV_OPT_TYPE_STRING, {.str="stereo"},0, 0, FLAGS },
1220  { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1221  { "level_out", "set output level", OFFSET(level_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1222  { "lfe", "output LFE", OFFSET(output_lfe), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
1223  { "lfe_low", "LFE low cut off", OFFSET(lowcutf), AV_OPT_TYPE_INT, {.i64=128}, 0, 256, FLAGS },
1224  { "lfe_high", "LFE high cut off", OFFSET(highcutf), AV_OPT_TYPE_INT, {.i64=256}, 0, 512, FLAGS },
1225  { "fc_in", "set front center channel input level", OFFSET(fc_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1226  { "fc_out", "set front center channel output level", OFFSET(fc_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1227  { "lfe_in", "set lfe channel input level", OFFSET(lfe_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1228  { "lfe_out", "set lfe channel output level", OFFSET(lfe_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1229  { NULL }
1230 };
1231 
1232 AVFILTER_DEFINE_CLASS(surround);
1233 
1234 static const AVFilterPad inputs[] = {
1235  {
1236  .name = "default",
1237  .type = AVMEDIA_TYPE_AUDIO,
1238  .filter_frame = filter_frame,
1239  .config_props = config_input,
1240  },
1241  { NULL }
1242 };
1243 
1244 static const AVFilterPad outputs[] = {
1245  {
1246  .name = "default",
1247  .type = AVMEDIA_TYPE_AUDIO,
1248  .config_props = config_output,
1249  },
1250  { NULL }
1251 };
1252 
1254  .name = "surround",
1255  .description = NULL_IF_CONFIG_SMALL("Apply audio surround upmix filter."),
1256  .query_formats = query_formats,
1257  .priv_size = sizeof(AudioSurroundContext),
1258  .priv_class = &surround_class,
1259  .init = init,
1260  .uninit = uninit,
1261  .inputs = inputs,
1262  .outputs = outputs,
1264 };
float, planar
Definition: samplefmt.h:69
#define NULL
Definition: coverity.c:32
#define AV_CH_LAYOUT_7POINT1
#define AV_CH_LAYOUT_4POINT1
const char * s
Definition: avisynth_c.h:768
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:59
static int config_input(AVFilterLink *inlink)
Definition: af_surround.c:143
static void stereo_position(float a, float p, float *x, float *y)
Definition: af_surround.c:221
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
AVOption.
Definition: opt.h:246
static int init(AVFilterContext *ctx)
Definition: af_surround.c:978
void(* filter)(AVFilterContext *ctx)
Definition: af_surround.c:68
static void upmix_4_0(AVFilterContext *ctx, float l_phase, float r_phase, float c_phase, float mag_total, float x, float y, int n)
Definition: af_surround.c:411
#define AV_CH_LAYOUT_SURROUND
Main libavfilter public API header.
void(* upmix_5_1)(AVFilterContext *ctx, float c_re, float c_im, float lfe_re, float lfe_im, float mag_totall, float mag_totalr, float fl_phase, float fr_phase, float bl_phase, float br_phase, float sl_phase, float sr_phase, float xl, float yl, float xr, float yr, int n)
Definition: af_surround.c:93
static int query_formats(AVFilterContext *ctx)
Definition: af_surround.c:105
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
static void upmix_2_1(AVFilterContext *ctx, float l_phase, float r_phase, float c_phase, float mag_total, float x, float y, int n)
Definition: af_surround.c:282
#define OFFSET(x)
Definition: af_surround.c:1214
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_surround.c:1191
#define AV_CH_LAYOUT_4POINT0
#define AV_CH_LAYOUT_7POINT0
#define AV_CH_LAYOUT_STEREO
void(* upmix_stereo)(AVFilterContext *ctx, float l_phase, float r_phase, float c_phase, float mag_total, float x, float y, int n)
Definition: af_surround.c:69
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:230
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
static void upmix_3_1_surround(AVFilterContext *ctx, float l_phase, float r_phase, float c_phase, float c_mag, float mag_total, float x, float y, int n)
Definition: af_surround.c:376
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
#define FLAGS
Definition: af_surround.c:1215
#define av_cold
Definition: attributes.h:82
AVOptions.
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
static void upmix_7_1_5_1(AVFilterContext *ctx, float c_re, float c_im, float lfe_re, float lfe_im, float mag_totall, float mag_totalr, float fl_phase, float fr_phase, float bl_phase, float br_phase, float sl_phase, float sr_phase, float xl, float yl, float xr, float yr, int n)
Definition: af_surround.c:767
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:294
#define cosf(x)
Definition: libm.h:78
#define AV_CH_LOW_FREQUENCY
static void filter_stereo(AVFilterContext *ctx)
Definition: af_surround.c:823
static int flags
Definition: log.c:57
#define atan2f(y, x)
Definition: libm.h:45
#define av_log(a,...)
static int ifft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
Definition: af_surround.c:1117
A filter pad used for either input or output.
Definition: internal.h:54
static void upmix_7_1(AVFilterContext *ctx, float l_phase, float r_phase, float c_phase, float mag_total, float x, float y, int n)
Definition: af_surround.c:711
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 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
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
static void filter_5_1_back(AVFilterContext *ctx)
Definition: af_surround.c:921
#define AVERROR(e)
Definition: error.h:43
static const AVOption surround_options[]
Definition: af_surround.c:1217
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
static void upmix_5_1_back(AVFilterContext *ctx, float l_phase, float r_phase, float c_phase, float mag_total, float x, float y, int n)
Definition: af_surround.c:524
#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
const char * arg
Definition: jacosubdec.c:66
Definition: avfft.h:73
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:337
static void upmix_5_1_back_2_1(AVFilterContext *ctx, float l_phase, float r_phase, float c_phase, float mag_total, float lfe_re, float lfe_im, float x, float y, int n)
Definition: af_surround.c:615
#define FFMAX(a, b)
Definition: common.h:94
static void get_lfe(int output_lfe, int n, float lowcut, float highcut, float *lfe_mag, float *mag_total)
Definition: af_surround.c:227
#define fail()
Definition: checkasm.h:109
void av_rdft_calc(RDFTContext *s, FFTSample *data)
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
#define FFDIFFSIGN(x, y)
Comparator.
Definition: common.h:92
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:228
audio channel layout utility functions
static void upmix_4_1(AVFilterContext *ctx, float l_phase, float r_phase, float c_phase, float mag_total, float x, float y, int n)
Definition: af_surround.c:445
static int config_output(AVFilterLink *outlink)
Definition: af_surround.c:185
#define AV_CH_LAYOUT_3POINT1
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
#define M_PI_2
Definition: mathematics.h:55
static int fft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
Definition: af_surround.c:1098
static const AVFilterPad outputs[]
Definition: af_surround.c:1244
AVFormatContext * ctx
Definition: movenc.c:48
char * out_channel_layout_str
Definition: af_surround.c:32
Definition: avfft.h:72
void av_rdft_end(RDFTContext *s)
int n
Definition: avisynth_c.h:684
RDFTContext * av_rdft_init(int nbits, enum RDFTransformType trans)
Set up a real FFT.
#define AV_CH_FRONT_CENTER
#define AV_CH_LAYOUT_5POINT1_BACK
static void upmix_stereo(AVFilterContext *ctx, float l_phase, float r_phase, float c_phase, float mag_total, float x, float y, int n)
Definition: af_surround.c:258
static void upmix_1_0(AVFilterContext *ctx, float l_phase, float r_phase, float c_phase, float mag_total, float x, float y, int n)
Definition: af_surround.c:239
A list of supported channel layouts.
Definition: formats.h:85
AVFilter ff_af_surround
Definition: af_surround.c:1253
RDFTContext ** rdft
Definition: af_surround.c:63
#define sinf(x)
Definition: libm.h:419
#define ff_log2
Definition: intmath.h:50
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
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
#define AV_CH_LAYOUT_5POINT0_BACK
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
int av_get_channel_layout_channel_index(uint64_t channel_layout, uint64_t channel)
Get the index of a channel in channel_layout.
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
static void upmix_7_0(AVFilterContext *ctx, float l_phase, float r_phase, float c_phase, float mag_total, float x, float y, int n)
Definition: af_surround.c:661
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:378
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:112
void(* upmix_3_0)(AVFilterContext *ctx, float l_phase, float r_phase, float c_mag, float c_phase, float mag_total, float x, float y, int n)
Definition: af_surround.c:85
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:201
#define M_LN10
Definition: mathematics.h:43
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_surround.c:1147
static void upmix_5_1_back_surround(AVFilterContext *ctx, float l_phase, float r_phase, float c_phase, float c_mag, float mag_total, float x, float y, int n)
Definition: af_surround.c:569
static void filter_surround(AVFilterContext *ctx)
Definition: af_surround.c:854
#define AV_CH_LAYOUT_2POINT1
RDFTContext ** irdft
Definition: af_surround.c:63
avfilter_execute_func * execute
Definition: internal.h:155
void(* upmix_2_1)(AVFilterContext *ctx, float l_phase, float r_phase, float c_phase, float mag_total, float lfe_im, float lfe_re, float x, float y, int n)
Definition: af_surround.c:76
Audio FIFO Buffer.
static void upmix_3_1(AVFilterContext *ctx, float l_phase, float r_phase, float c_phase, float mag_total, float x, float y, int n)
Definition: af_surround.c:341
uint64_t in_channel_layout
Definition: af_surround.c:52
char * in_channel_layout_str
Definition: af_surround.c:33
A list of supported formats for one end of a filter link.
Definition: formats.h:64
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:138
An instance of a filter.
Definition: avfilter.h:338
uint64_t out_channel_layout
Definition: af_surround.c:51
FILE * out
Definition: movenc.c:54
#define av_freep(p)
AVFILTER_DEFINE_CLASS(surround)
#define M_PI
Definition: mathematics.h:52
#define av_malloc_array(a, b)
formats
Definition: signature.h:48
AVFrame * overlap_buffer
Definition: af_surround.c:58
AVAudioFifo * fifo
Definition: af_surround.c:62
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:248
#define AV_CH_LAYOUT_MONO
static void filter_2_1(AVFilterContext *ctx)
Definition: af_surround.c:888
static const AVFilterPad inputs[]
Definition: af_surround.c:1234
static void upmix_5_0_back(AVFilterContext *ctx, float l_phase, float r_phase, float c_phase, float mag_total, float x, float y, int n)
Definition: af_surround.c:485
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:267
for(j=16;j >0;--j)
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
static void upmix_3_0(AVFilterContext *ctx, float l_phase, float r_phase, float c_phase, float mag_total, float x, float y, int n)
Definition: af_surround.c:312