]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_surround.c
Merge commit '083ea8768121ee800893e124b08483011b798919'
[ffmpeg] / libavfilter / af_surround.c
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"
22 #include "libavutil/channel_layout.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
32     char *out_channel_layout_str;
33     char *in_channel_layout_str;
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;
44     int output_lfe;
45     int lowcutf;
46     int highcutf;
47
48     float lowcut;
49     float highcut;
50
51     uint64_t out_channel_layout;
52     uint64_t in_channel_layout;
53     int nb_in_channels;
54     int nb_out_channels;
55
56     AVFrame *input;
57     AVFrame *output;
58     AVFrame *overlap_buffer;
59
60     int buf_size;
61     int hop_size;
62     AVAudioFifo *fifo;
63     RDFTContext **rdft, **irdft;
64     float *window_func_lut;
65
66     int64_t pts;
67
68     void (*filter)(AVFilterContext *ctx);
69     void (*upmix_stereo)(AVFilterContext *ctx,
70                          float l_phase,
71                          float r_phase,
72                          float c_phase,
73                          float mag_total,
74                          float x, float y,
75                          int n);
76     void (*upmix_2_1)(AVFilterContext *ctx,
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);
85     void (*upmix_3_0)(AVFilterContext *ctx,
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);
93     void (*upmix_5_1)(AVFilterContext *ctx,
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);
103 } AudioSurroundContext;
104
105 static int query_formats(AVFilterContext *ctx)
106 {
107     AudioSurroundContext *s = ctx->priv;
108     AVFilterFormats *formats = NULL;
109     AVFilterChannelLayouts *layouts = NULL;
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++) {
154         s->rdft[ch]  = av_rdft_init(ff_log2(s->buf_size), DFT_R2C);
155         if (!s->rdft[ch])
156             return AVERROR(ENOMEM);
157     }
158     s->nb_in_channels = inlink->channels;
159     s->input_levels = av_malloc_array(s->nb_in_channels, sizeof(*s->input_levels));
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;
164     ch = av_get_channel_layout_channel_index(inlink->channel_layout, AV_CH_FRONT_CENTER);
165     if (ch >= 0)
166         s->input_levels[ch] *= s->fc_in;
167     ch = av_get_channel_layout_channel_index(inlink->channel_layout, AV_CH_LOW_FREQUENCY);
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++) {
196         s->irdft[ch] = av_rdft_init(ff_log2(s->buf_size), IDFT_C2R);
197         if (!s->irdft[ch])
198             return AVERROR(ENOMEM);
199     }
200     s->nb_out_channels = outlink->channels;
201     s->output_levels = av_malloc_array(s->nb_out_channels, sizeof(*s->output_levels));
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;
206     ch = av_get_channel_layout_channel_index(outlink->channel_layout, AV_CH_FRONT_CENTER);
207     if (ch >= 0)
208         s->output_levels[ch] *= s->fc_out;
209     ch = av_get_channel_layout_channel_index(outlink->channel_layout, AV_CH_LOW_FREQUENCY);
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
239 static void upmix_1_0(AVFilterContext *ctx,
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
258 static void upmix_stereo(AVFilterContext *ctx,
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
282 static void upmix_2_1(AVFilterContext *ctx,
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
312 static void upmix_3_0(AVFilterContext *ctx,
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
341 static void upmix_3_1(AVFilterContext *ctx,
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
376 static void upmix_3_1_surround(AVFilterContext *ctx,
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
411 static void upmix_4_0(AVFilterContext *ctx,
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
445 static void upmix_4_1(AVFilterContext *ctx,
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
485 static void upmix_5_0_back(AVFilterContext *ctx,
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
524 static void upmix_5_1_back(AVFilterContext *ctx,
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
569 static void upmix_5_1_back_surround(AVFilterContext *ctx,
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
615 static void upmix_5_1_back_2_1(AVFilterContext *ctx,
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
661 static void upmix_7_0(AVFilterContext *ctx,
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
711 static void upmix_7_1(AVFilterContext *ctx,
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
767 static void upmix_7_1_5_1(AVFilterContext *ctx,
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
823 static void filter_stereo(AVFilterContext *ctx)
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
854 static void filter_surround(AVFilterContext *ctx)
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
888 static void filter_2_1(AVFilterContext *ctx)
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
921 static void filter_5_1_back(AVFilterContext *ctx)
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
978 static int init(AVFilterContext *ctx)
979 {
980     AudioSurroundContext *s = ctx->priv;
981     float overlap;
982     int i;
983
984     if (!(s->out_channel_layout = av_get_channel_layout(s->out_channel_layout_str))) {
985         av_log(ctx, AV_LOG_ERROR, "Error parsing output channel layout '%s'.\n",
986                s->out_channel_layout_str);
987         return AVERROR(EINVAL);
988     }
989
990     if (!(s->in_channel_layout = av_get_channel_layout(s->in_channel_layout_str))) {
991         av_log(ctx, AV_LOG_ERROR, "Error parsing input channel layout '%s'.\n",
992                s->in_channel_layout_str);
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:
1010             s->upmix_stereo = upmix_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;
1027         case AV_CH_LAYOUT_5POINT0_BACK:
1028             s->upmix_stereo = upmix_5_0_back;
1029             break;
1030         case AV_CH_LAYOUT_5POINT1_BACK:
1031             s->upmix_stereo = upmix_5_1_back;
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) {
1046         case AV_CH_LAYOUT_5POINT1_BACK:
1047             s->upmix_2_1 = upmix_5_1_back_2_1;
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:
1057             s->upmix_3_0 = upmix_3_1_surround;
1058             break;
1059         case AV_CH_LAYOUT_5POINT1_BACK:
1060             s->upmix_3_0 = upmix_5_1_back_surround;
1061             break;
1062         default:
1063             goto fail;
1064         }
1065         break;
1066     case AV_CH_LAYOUT_5POINT1_BACK:
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",
1079                s->in_channel_layout_str, s->out_channel_layout_str);
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);
1182         av_audio_fifo_drain(s->fifo, s->hop_size);
1183         ret = ff_filter_frame(outlink, out);
1184         if (ret < 0)
1185             return ret;
1186     }
1187
1188     return 0;
1189 }
1190
1191 static av_cold void uninit(AVFilterContext *ctx)
1192 {
1193     AudioSurroundContext *s = ctx->priv;
1194     int ch;
1195
1196     av_frame_free(&s->input);
1197     av_frame_free(&s->output);
1198     av_frame_free(&s->overlap_buffer);
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);
1210     av_audio_fifo_free(s->fifo);
1211     av_freep(&s->window_func_lut);
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
1253 AVFilter ff_af_surround = {
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,
1263     .flags          = AVFILTER_FLAG_SLICE_THREADS,
1264 };