]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_surround.c
avfilter/af_surround: switch to activate
[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 "filters.h"
28 #include "internal.h"
29 #include "formats.h"
30 #include "window_func.h"
31
32 typedef struct AudioSurroundContext {
33     const AVClass *class;
34
35     char *out_channel_layout_str;
36     char *in_channel_layout_str;
37
38     float level_in;
39     float level_out;
40     float fc_in;
41     float fc_out;
42     float lfe_in;
43     float lfe_out;
44     int   lfe_mode;
45     int   win_size;
46     int   win_func;
47     float overlap;
48
49     float all_x;
50     float all_y;
51
52     float fc_x;
53     float fl_x;
54     float fr_x;
55     float bl_x;
56     float br_x;
57     float sl_x;
58     float sr_x;
59     float bc_x;
60
61     float fc_y;
62     float fl_y;
63     float fr_y;
64     float bl_y;
65     float br_y;
66     float sl_y;
67     float sr_y;
68     float bc_y;
69
70     float *input_levels;
71     float *output_levels;
72     int output_lfe;
73     int lowcutf;
74     int highcutf;
75
76     float lowcut;
77     float highcut;
78
79     uint64_t out_channel_layout;
80     uint64_t in_channel_layout;
81     int nb_in_channels;
82     int nb_out_channels;
83
84     AVFrame *input;
85     AVFrame *output;
86     AVFrame *overlap_buffer;
87
88     int buf_size;
89     int hop_size;
90     AVAudioFifo *fifo;
91     RDFTContext **rdft, **irdft;
92     float *window_func_lut;
93
94     int64_t pts;
95     int eof;
96
97     void (*filter)(AVFilterContext *ctx);
98     void (*upmix_stereo)(AVFilterContext *ctx,
99                          float l_phase,
100                          float r_phase,
101                          float c_phase,
102                          float mag_total,
103                          float x, float y,
104                          int n);
105     void (*upmix_2_1)(AVFilterContext *ctx,
106                       float l_phase,
107                       float r_phase,
108                       float c_phase,
109                       float mag_total,
110                       float lfe_im,
111                       float lfe_re,
112                       float x, float y,
113                       int n);
114     void (*upmix_3_0)(AVFilterContext *ctx,
115                       float l_phase,
116                       float r_phase,
117                       float c_mag,
118                       float c_phase,
119                       float mag_total,
120                       float x, float y,
121                       int n);
122     void (*upmix_5_0)(AVFilterContext *ctx,
123                       float c_re, float c_im,
124                       float mag_totall, float mag_totalr,
125                       float fl_phase, float fr_phase,
126                       float bl_phase, float br_phase,
127                       float sl_phase, float sr_phase,
128                       float xl, float yl,
129                       float xr, float yr,
130                       int n);
131     void (*upmix_5_1)(AVFilterContext *ctx,
132                       float c_re, float c_im,
133                       float lfe_re, float lfe_im,
134                       float mag_totall, float mag_totalr,
135                       float fl_phase, float fr_phase,
136                       float bl_phase, float br_phase,
137                       float sl_phase, float sr_phase,
138                       float xl, float yl,
139                       float xr, float yr,
140                       int n);
141 } AudioSurroundContext;
142
143 static int query_formats(AVFilterContext *ctx)
144 {
145     AudioSurroundContext *s = ctx->priv;
146     AVFilterFormats *formats = NULL;
147     AVFilterChannelLayouts *layouts = NULL;
148     int ret;
149
150     ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLTP);
151     if (ret)
152         return ret;
153     ret = ff_set_common_formats(ctx, formats);
154     if (ret)
155         return ret;
156
157     layouts = NULL;
158     ret = ff_add_channel_layout(&layouts, s->out_channel_layout);
159     if (ret)
160         return ret;
161
162     ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
163     if (ret)
164         return ret;
165
166     layouts = NULL;
167     ret = ff_add_channel_layout(&layouts, s->in_channel_layout);
168     if (ret)
169         return ret;
170
171     ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts);
172     if (ret)
173         return ret;
174
175     formats = ff_all_samplerates();
176     if (!formats)
177         return AVERROR(ENOMEM);
178     return ff_set_common_samplerates(ctx, formats);
179 }
180
181 static int config_input(AVFilterLink *inlink)
182 {
183     AVFilterContext *ctx = inlink->dst;
184     AudioSurroundContext *s = ctx->priv;
185     int ch;
186
187     s->rdft = av_calloc(inlink->channels, sizeof(*s->rdft));
188     if (!s->rdft)
189         return AVERROR(ENOMEM);
190
191     for (ch = 0; ch < inlink->channels; ch++) {
192         s->rdft[ch]  = av_rdft_init(ff_log2(s->buf_size), DFT_R2C);
193         if (!s->rdft[ch])
194             return AVERROR(ENOMEM);
195     }
196     s->nb_in_channels = inlink->channels;
197     s->input_levels = av_malloc_array(s->nb_in_channels, sizeof(*s->input_levels));
198     if (!s->input_levels)
199         return AVERROR(ENOMEM);
200     for (ch = 0;  ch < s->nb_in_channels; ch++)
201         s->input_levels[ch] = s->level_in;
202     ch = av_get_channel_layout_channel_index(inlink->channel_layout, AV_CH_FRONT_CENTER);
203     if (ch >= 0)
204         s->input_levels[ch] *= s->fc_in;
205     ch = av_get_channel_layout_channel_index(inlink->channel_layout, AV_CH_LOW_FREQUENCY);
206     if (ch >= 0)
207         s->input_levels[ch] *= s->lfe_in;
208
209     s->input = ff_get_audio_buffer(inlink, s->buf_size * 2);
210     if (!s->input)
211         return AVERROR(ENOMEM);
212
213     s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->buf_size);
214     if (!s->fifo)
215         return AVERROR(ENOMEM);
216
217     s->lowcut = 1.f * s->lowcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
218     s->highcut = 1.f * s->highcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
219
220     return 0;
221 }
222
223 static int config_output(AVFilterLink *outlink)
224 {
225     AVFilterContext *ctx = outlink->src;
226     AudioSurroundContext *s = ctx->priv;
227     int ch;
228
229     s->irdft = av_calloc(outlink->channels, sizeof(*s->irdft));
230     if (!s->irdft)
231         return AVERROR(ENOMEM);
232
233     for (ch = 0; ch < outlink->channels; ch++) {
234         s->irdft[ch] = av_rdft_init(ff_log2(s->buf_size), IDFT_C2R);
235         if (!s->irdft[ch])
236             return AVERROR(ENOMEM);
237     }
238     s->nb_out_channels = outlink->channels;
239     s->output_levels = av_malloc_array(s->nb_out_channels, sizeof(*s->output_levels));
240     if (!s->output_levels)
241         return AVERROR(ENOMEM);
242     for (ch = 0;  ch < s->nb_out_channels; ch++)
243         s->output_levels[ch] = s->level_out;
244     ch = av_get_channel_layout_channel_index(outlink->channel_layout, AV_CH_FRONT_CENTER);
245     if (ch >= 0)
246         s->output_levels[ch] *= s->fc_out;
247     ch = av_get_channel_layout_channel_index(outlink->channel_layout, AV_CH_LOW_FREQUENCY);
248     if (ch >= 0)
249         s->output_levels[ch] *= s->lfe_out;
250
251     s->output = ff_get_audio_buffer(outlink, s->buf_size * 2);
252     s->overlap_buffer = ff_get_audio_buffer(outlink, s->buf_size * 2);
253     if (!s->overlap_buffer || !s->output)
254         return AVERROR(ENOMEM);
255
256     return 0;
257 }
258
259 static void stereo_position(float a, float p, float *x, float *y)
260 {
261     *x = av_clipf(a+FFMAX(0, sinf(p-M_PI_2))*FFDIFFSIGN(a,0), -1, 1);
262     *y = av_clipf(cosf(a*M_PI_2+M_PI)*cosf(M_PI_2-p/M_PI)*M_LN10+1, -1, 1);
263 }
264
265 static inline void get_lfe(int output_lfe, int n, float lowcut, float highcut,
266                            float *lfe_mag, float *mag_total, int lfe_mode)
267 {
268     if (output_lfe && n < highcut) {
269         *lfe_mag    = n < lowcut ? 1.f : .5f*(1.f+cosf(M_PI*(lowcut-n)/(lowcut-highcut)));
270         *lfe_mag   *= *mag_total;
271         if (lfe_mode)
272             *mag_total -= *lfe_mag;
273     } else {
274         *lfe_mag = 0.f;
275     }
276 }
277
278 static void upmix_1_0(AVFilterContext *ctx,
279                       float l_phase,
280                       float r_phase,
281                       float c_phase,
282                       float mag_total,
283                       float x, float y,
284                       int n)
285 {
286     AudioSurroundContext *s = ctx->priv;
287     float mag, *dst;
288
289     dst = (float *)s->output->extended_data[0];
290
291     mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
292
293     dst[2 * n    ] = mag * cosf(c_phase);
294     dst[2 * n + 1] = mag * sinf(c_phase);
295 }
296
297 static void upmix_stereo(AVFilterContext *ctx,
298                          float l_phase,
299                          float r_phase,
300                          float c_phase,
301                          float mag_total,
302                          float x, float y,
303                          int n)
304 {
305     AudioSurroundContext *s = ctx->priv;
306     float l_mag, r_mag, *dstl, *dstr;
307
308     dstl = (float *)s->output->extended_data[0];
309     dstr = (float *)s->output->extended_data[1];
310
311     l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
312     r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
313
314     dstl[2 * n    ] = l_mag * cosf(l_phase);
315     dstl[2 * n + 1] = l_mag * sinf(l_phase);
316
317     dstr[2 * n    ] = r_mag * cosf(r_phase);
318     dstr[2 * n + 1] = r_mag * sinf(r_phase);
319 }
320
321 static void upmix_2_1(AVFilterContext *ctx,
322                       float l_phase,
323                       float r_phase,
324                       float c_phase,
325                       float mag_total,
326                       float x, float y,
327                       int n)
328 {
329     AudioSurroundContext *s = ctx->priv;
330     float lfe_mag, l_mag, r_mag, *dstl, *dstr, *dstlfe;
331
332     dstl = (float *)s->output->extended_data[0];
333     dstr = (float *)s->output->extended_data[1];
334     dstlfe = (float *)s->output->extended_data[2];
335
336     get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
337
338     l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
339     r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
340
341     dstl[2 * n    ] = l_mag * cosf(l_phase);
342     dstl[2 * n + 1] = l_mag * sinf(l_phase);
343
344     dstr[2 * n    ] = r_mag * cosf(r_phase);
345     dstr[2 * n + 1] = r_mag * sinf(r_phase);
346
347     dstlfe[2 * n    ] = lfe_mag * cosf(c_phase);
348     dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
349 }
350
351 static void upmix_3_0(AVFilterContext *ctx,
352                       float l_phase,
353                       float r_phase,
354                       float c_phase,
355                       float mag_total,
356                       float x, float y,
357                       int n)
358 {
359     AudioSurroundContext *s = ctx->priv;
360     float l_mag, r_mag, c_mag, *dstc, *dstl, *dstr;
361
362     dstl = (float *)s->output->extended_data[0];
363     dstr = (float *)s->output->extended_data[1];
364     dstc = (float *)s->output->extended_data[2];
365
366     c_mag = powf(1.f - fabsf(x),   s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
367     l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
368     r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
369
370     dstl[2 * n    ] = l_mag * cosf(l_phase);
371     dstl[2 * n + 1] = l_mag * sinf(l_phase);
372
373     dstr[2 * n    ] = r_mag * cosf(r_phase);
374     dstr[2 * n + 1] = r_mag * sinf(r_phase);
375
376     dstc[2 * n    ] = c_mag * cosf(c_phase);
377     dstc[2 * n + 1] = c_mag * sinf(c_phase);
378 }
379
380 static void upmix_3_1(AVFilterContext *ctx,
381                       float l_phase,
382                       float r_phase,
383                       float c_phase,
384                       float mag_total,
385                       float x, float y,
386                       int n)
387 {
388     AudioSurroundContext *s = ctx->priv;
389     float lfe_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstlfe;
390
391     dstl = (float *)s->output->extended_data[0];
392     dstr = (float *)s->output->extended_data[1];
393     dstc = (float *)s->output->extended_data[2];
394     dstlfe = (float *)s->output->extended_data[3];
395
396     get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
397
398     c_mag = powf(1.f - fabsf(x),   s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
399     l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
400     r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
401
402     dstl[2 * n    ] = l_mag * cosf(l_phase);
403     dstl[2 * n + 1] = l_mag * sinf(l_phase);
404
405     dstr[2 * n    ] = r_mag * cosf(r_phase);
406     dstr[2 * n + 1] = r_mag * sinf(r_phase);
407
408     dstc[2 * n    ] = c_mag * cosf(c_phase);
409     dstc[2 * n + 1] = c_mag * sinf(c_phase);
410
411     dstlfe[2 * n    ] = lfe_mag * cosf(c_phase);
412     dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
413 }
414
415 static void upmix_3_1_surround(AVFilterContext *ctx,
416                                float l_phase,
417                                float r_phase,
418                                float c_phase,
419                                float c_mag,
420                                float mag_total,
421                                float x, float y,
422                                int n)
423 {
424     AudioSurroundContext *s = ctx->priv;
425     float lfe_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
426
427     dstl = (float *)s->output->extended_data[0];
428     dstr = (float *)s->output->extended_data[1];
429     dstc = (float *)s->output->extended_data[2];
430     dstlfe = (float *)s->output->extended_data[3];
431
432     get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &c_mag, s->lfe_mode);
433
434     l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
435     r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
436
437     dstl[2 * n    ] = l_mag * cosf(l_phase);
438     dstl[2 * n + 1] = l_mag * sinf(l_phase);
439
440     dstr[2 * n    ] = r_mag * cosf(r_phase);
441     dstr[2 * n + 1] = r_mag * sinf(r_phase);
442
443     dstc[2 * n    ] = c_mag * cosf(c_phase);
444     dstc[2 * n + 1] = c_mag * sinf(c_phase);
445
446     dstlfe[2 * n    ] = lfe_mag * cosf(c_phase);
447     dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
448 }
449
450 static void upmix_4_0(AVFilterContext *ctx,
451                       float l_phase,
452                       float r_phase,
453                       float c_phase,
454                       float mag_total,
455                       float x, float y,
456                       int n)
457 {
458     AudioSurroundContext *s = ctx->priv;
459     float b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb;
460
461     dstl = (float *)s->output->extended_data[0];
462     dstr = (float *)s->output->extended_data[1];
463     dstc = (float *)s->output->extended_data[2];
464     dstb = (float *)s->output->extended_data[3];
465
466     c_mag = powf(1.f - fabsf(x),   s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
467     b_mag = powf(1.f - fabsf(x),   s->bc_x) * powf((1.f - y) * .5f, s->bc_y) * mag_total;
468     l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
469     r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
470
471     dstl[2 * n    ] = l_mag * cosf(l_phase);
472     dstl[2 * n + 1] = l_mag * sinf(l_phase);
473
474     dstr[2 * n    ] = r_mag * cosf(r_phase);
475     dstr[2 * n + 1] = r_mag * sinf(r_phase);
476
477     dstc[2 * n    ] = c_mag * cosf(c_phase);
478     dstc[2 * n + 1] = c_mag * sinf(c_phase);
479
480     dstb[2 * n    ] = b_mag * cosf(c_phase);
481     dstb[2 * n + 1] = b_mag * sinf(c_phase);
482 }
483
484 static void upmix_4_1(AVFilterContext *ctx,
485                       float l_phase,
486                       float r_phase,
487                       float c_phase,
488                       float mag_total,
489                       float x, float y,
490                       int n)
491 {
492     AudioSurroundContext *s = ctx->priv;
493     float lfe_mag, b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb, *dstlfe;
494
495     dstl = (float *)s->output->extended_data[0];
496     dstr = (float *)s->output->extended_data[1];
497     dstc = (float *)s->output->extended_data[2];
498     dstlfe = (float *)s->output->extended_data[3];
499     dstb = (float *)s->output->extended_data[4];
500
501     get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
502
503     dstlfe[2 * n    ] = lfe_mag * cosf(c_phase);
504     dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
505
506     c_mag = powf(1.f - fabsf(x),   s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
507     b_mag = powf(1.f - fabsf(x),   s->bc_x) * powf((1.f - y) * .5f, s->bc_y) * mag_total;
508     l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
509     r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
510
511     dstl[2 * n    ] = l_mag * cosf(l_phase);
512     dstl[2 * n + 1] = l_mag * sinf(l_phase);
513
514     dstr[2 * n    ] = r_mag * cosf(r_phase);
515     dstr[2 * n + 1] = r_mag * sinf(r_phase);
516
517     dstc[2 * n    ] = c_mag * cosf(c_phase);
518     dstc[2 * n + 1] = c_mag * sinf(c_phase);
519
520     dstb[2 * n    ] = b_mag * cosf(c_phase);
521     dstb[2 * n + 1] = b_mag * sinf(c_phase);
522 }
523
524 static void upmix_5_0_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     AudioSurroundContext *s = ctx->priv;
533     float l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs;
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     dstls = (float *)s->output->extended_data[3];
539     dstrs = (float *)s->output->extended_data[4];
540
541     c_mag  = powf(1.f - fabsf(x),   s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
542     l_mag  = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
543     r_mag  = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
544     ls_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
545     rs_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
546
547     dstl[2 * n    ] = l_mag * cosf(l_phase);
548     dstl[2 * n + 1] = l_mag * sinf(l_phase);
549
550     dstr[2 * n    ] = r_mag * cosf(r_phase);
551     dstr[2 * n + 1] = r_mag * sinf(r_phase);
552
553     dstc[2 * n    ] = c_mag * cosf(c_phase);
554     dstc[2 * n + 1] = c_mag * sinf(c_phase);
555
556     dstls[2 * n    ] = ls_mag * cosf(l_phase);
557     dstls[2 * n + 1] = ls_mag * sinf(l_phase);
558
559     dstrs[2 * n    ] = rs_mag * cosf(r_phase);
560     dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
561 }
562
563 static void upmix_5_1_back(AVFilterContext *ctx,
564                            float l_phase,
565                            float r_phase,
566                            float c_phase,
567                            float mag_total,
568                            float x, float y,
569                            int n)
570 {
571     AudioSurroundContext *s = ctx->priv;
572     float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlfe;
573
574     dstl  = (float *)s->output->extended_data[0];
575     dstr  = (float *)s->output->extended_data[1];
576     dstc  = (float *)s->output->extended_data[2];
577     dstlfe = (float *)s->output->extended_data[3];
578     dstls = (float *)s->output->extended_data[4];
579     dstrs = (float *)s->output->extended_data[5];
580
581     get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
582
583     c_mag  = powf(1.f - fabsf(x),   s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
584     l_mag  = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
585     r_mag  = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
586     ls_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
587     rs_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
588
589     dstl[2 * n    ] = l_mag * cosf(l_phase);
590     dstl[2 * n + 1] = l_mag * sinf(l_phase);
591
592     dstr[2 * n    ] = r_mag * cosf(r_phase);
593     dstr[2 * n + 1] = r_mag * sinf(r_phase);
594
595     dstc[2 * n    ] = c_mag * cosf(c_phase);
596     dstc[2 * n + 1] = c_mag * sinf(c_phase);
597
598     dstlfe[2 * n    ] = lfe_mag * cosf(c_phase);
599     dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
600
601     dstls[2 * n    ] = ls_mag * cosf(l_phase);
602     dstls[2 * n + 1] = ls_mag * sinf(l_phase);
603
604     dstrs[2 * n    ] = rs_mag * cosf(r_phase);
605     dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
606 }
607
608 static void upmix_6_0(AVFilterContext *ctx,
609                       float l_phase,
610                       float r_phase,
611                       float c_phase,
612                       float mag_total,
613                       float x, float y,
614                       int n)
615 {
616     AudioSurroundContext *s = ctx->priv;
617     float l_mag, r_mag, ls_mag, rs_mag, c_mag, b_mag, *dstc, *dstb, *dstl, *dstr, *dstls, *dstrs;
618
619     dstl  = (float *)s->output->extended_data[0];
620     dstr  = (float *)s->output->extended_data[1];
621     dstc  = (float *)s->output->extended_data[2];
622     dstb  = (float *)s->output->extended_data[3];
623     dstls = (float *)s->output->extended_data[4];
624     dstrs = (float *)s->output->extended_data[5];
625
626     c_mag  = powf(1.f - fabsf(x),   s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
627     b_mag  = powf(1.f - fabsf(x),   s->bc_x) * powf((1.f - y) * .5f, s->bc_y) * mag_total;
628     l_mag  = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
629     r_mag  = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
630     ls_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
631     rs_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
632
633     dstl[2 * n    ] = l_mag * cosf(l_phase);
634     dstl[2 * n + 1] = l_mag * sinf(l_phase);
635
636     dstr[2 * n    ] = r_mag * cosf(r_phase);
637     dstr[2 * n + 1] = r_mag * sinf(r_phase);
638
639     dstc[2 * n    ] = c_mag * cosf(c_phase);
640     dstc[2 * n + 1] = c_mag * sinf(c_phase);
641
642     dstls[2 * n    ] = ls_mag * cosf(l_phase);
643     dstls[2 * n + 1] = ls_mag * sinf(l_phase);
644
645     dstrs[2 * n    ] = rs_mag * cosf(r_phase);
646     dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
647
648     dstb[2 * n    ] = b_mag * cosf(c_phase);
649     dstb[2 * n + 1] = b_mag * sinf(c_phase);
650 }
651
652 static void upmix_6_1(AVFilterContext *ctx,
653                       float l_phase,
654                       float r_phase,
655                       float c_phase,
656                       float mag_total,
657                       float x, float y,
658                       int n)
659 {
660     AudioSurroundContext *s = ctx->priv;
661     float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, b_mag, *dstc, *dstb, *dstl, *dstr, *dstls, *dstrs, *dstlfe;
662
663     dstl  = (float *)s->output->extended_data[0];
664     dstr  = (float *)s->output->extended_data[1];
665     dstc  = (float *)s->output->extended_data[2];
666     dstlfe = (float *)s->output->extended_data[3];
667     dstb  = (float *)s->output->extended_data[4];
668     dstls = (float *)s->output->extended_data[5];
669     dstrs = (float *)s->output->extended_data[6];
670
671     get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
672
673     c_mag  = powf(1.f - fabsf(x),   s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
674     b_mag  = powf(1.f - fabsf(x),   s->bc_x) * powf((1.f - y) * .5f, s->bc_y) * mag_total;
675     l_mag  = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
676     r_mag  = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
677     ls_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
678     rs_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
679
680     dstl[2 * n    ] = l_mag * cosf(l_phase);
681     dstl[2 * n + 1] = l_mag * sinf(l_phase);
682
683     dstr[2 * n    ] = r_mag * cosf(r_phase);
684     dstr[2 * n + 1] = r_mag * sinf(r_phase);
685
686     dstc[2 * n    ] = c_mag * cosf(c_phase);
687     dstc[2 * n + 1] = c_mag * sinf(c_phase);
688
689     dstlfe[2 * n    ] = lfe_mag * cosf(c_phase);
690     dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
691
692     dstls[2 * n    ] = ls_mag * cosf(l_phase);
693     dstls[2 * n + 1] = ls_mag * sinf(l_phase);
694
695     dstrs[2 * n    ] = rs_mag * cosf(r_phase);
696     dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
697
698     dstb[2 * n    ] = b_mag * cosf(c_phase);
699     dstb[2 * n + 1] = b_mag * sinf(c_phase);
700 }
701
702 static void upmix_5_1_back_surround(AVFilterContext *ctx,
703                                     float l_phase,
704                                     float r_phase,
705                                     float c_phase,
706                                     float c_mag,
707                                     float mag_total,
708                                     float x, float y,
709                                     int n)
710 {
711     AudioSurroundContext *s = ctx->priv;
712     float lfe_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
713     float ls_mag, rs_mag, *dstls, *dstrs;
714
715     dstl = (float *)s->output->extended_data[0];
716     dstr = (float *)s->output->extended_data[1];
717     dstc = (float *)s->output->extended_data[2];
718     dstlfe = (float *)s->output->extended_data[3];
719     dstls = (float *)s->output->extended_data[4];
720     dstrs = (float *)s->output->extended_data[5];
721
722     get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &c_mag, s->lfe_mode);
723
724     l_mag = powf(.5f * ( x + 1.f),  s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
725     r_mag = powf(.5f * (-x + 1.f),  s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
726     ls_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
727     rs_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
728
729     dstl[2 * n    ] = l_mag * cosf(l_phase);
730     dstl[2 * n + 1] = l_mag * sinf(l_phase);
731
732     dstr[2 * n    ] = r_mag * cosf(r_phase);
733     dstr[2 * n + 1] = r_mag * sinf(r_phase);
734
735     dstc[2 * n    ] = c_mag * cosf(c_phase);
736     dstc[2 * n + 1] = c_mag * sinf(c_phase);
737
738     dstlfe[2 * n    ] = lfe_mag * cosf(c_phase);
739     dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
740
741     dstls[2 * n    ] = ls_mag * cosf(l_phase);
742     dstls[2 * n + 1] = ls_mag * sinf(l_phase);
743
744     dstrs[2 * n    ] = rs_mag * cosf(r_phase);
745     dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
746 }
747
748 static void upmix_5_1_back_2_1(AVFilterContext *ctx,
749                                float l_phase,
750                                float r_phase,
751                                float c_phase,
752                                float mag_total,
753                                float lfe_re,
754                                float lfe_im,
755                                float x, float y,
756                                int n)
757 {
758     AudioSurroundContext *s = ctx->priv;
759     float c_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
760     float ls_mag, rs_mag, *dstls, *dstrs;
761
762     dstl = (float *)s->output->extended_data[0];
763     dstr = (float *)s->output->extended_data[1];
764     dstc = (float *)s->output->extended_data[2];
765     dstlfe = (float *)s->output->extended_data[3];
766     dstls = (float *)s->output->extended_data[4];
767     dstrs = (float *)s->output->extended_data[5];
768
769     c_mag  = powf(1.f - fabsf(x),   s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
770     l_mag  = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
771     r_mag  = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
772     ls_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
773     rs_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
774
775     dstl[2 * n    ] = l_mag * cosf(l_phase);
776     dstl[2 * n + 1] = l_mag * sinf(l_phase);
777
778     dstr[2 * n    ] = r_mag * cosf(r_phase);
779     dstr[2 * n + 1] = r_mag * sinf(r_phase);
780
781     dstc[2 * n    ] = c_mag * cosf(c_phase);
782     dstc[2 * n + 1] = c_mag * sinf(c_phase);
783
784     dstlfe[2 * n    ] = lfe_re;
785     dstlfe[2 * n + 1] = lfe_im;
786
787     dstls[2 * n    ] = ls_mag * cosf(l_phase);
788     dstls[2 * n + 1] = ls_mag * sinf(l_phase);
789
790     dstrs[2 * n    ] = rs_mag * cosf(r_phase);
791     dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
792 }
793
794 static void upmix_7_0(AVFilterContext *ctx,
795                       float l_phase,
796                       float r_phase,
797                       float c_phase,
798                       float mag_total,
799                       float x, float y,
800                       int n)
801 {
802     float l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
803     float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb;
804     AudioSurroundContext *s = ctx->priv;
805
806     dstl  = (float *)s->output->extended_data[0];
807     dstr  = (float *)s->output->extended_data[1];
808     dstc  = (float *)s->output->extended_data[2];
809     dstlb = (float *)s->output->extended_data[3];
810     dstrb = (float *)s->output->extended_data[4];
811     dstls = (float *)s->output->extended_data[5];
812     dstrs = (float *)s->output->extended_data[6];
813
814     c_mag  = powf(1.f - fabsf(x),   s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
815     l_mag  = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
816     r_mag  = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
817     lb_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
818     rb_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
819     ls_mag = powf(.5f * ( x + 1.f), s->sl_x) * powf(1.f - fabsf(y), s->sl_y) * mag_total;
820     rs_mag = powf(.5f * (-x + 1.f), s->sr_x) * powf(1.f - fabsf(y), s->sr_y) * mag_total;
821
822     dstl[2 * n    ] = l_mag * cosf(l_phase);
823     dstl[2 * n + 1] = l_mag * sinf(l_phase);
824
825     dstr[2 * n    ] = r_mag * cosf(r_phase);
826     dstr[2 * n + 1] = r_mag * sinf(r_phase);
827
828     dstc[2 * n    ] = c_mag * cosf(c_phase);
829     dstc[2 * n + 1] = c_mag * sinf(c_phase);
830
831     dstlb[2 * n    ] = lb_mag * cosf(l_phase);
832     dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
833
834     dstrb[2 * n    ] = rb_mag * cosf(r_phase);
835     dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
836
837     dstls[2 * n    ] = ls_mag * cosf(l_phase);
838     dstls[2 * n + 1] = ls_mag * sinf(l_phase);
839
840     dstrs[2 * n    ] = rs_mag * cosf(r_phase);
841     dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
842 }
843
844 static void upmix_7_1(AVFilterContext *ctx,
845                       float l_phase,
846                       float r_phase,
847                       float c_phase,
848                       float mag_total,
849                       float x, float y,
850                       int n)
851 {
852     float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
853     float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
854     AudioSurroundContext *s = ctx->priv;
855
856     dstl  = (float *)s->output->extended_data[0];
857     dstr  = (float *)s->output->extended_data[1];
858     dstc  = (float *)s->output->extended_data[2];
859     dstlfe = (float *)s->output->extended_data[3];
860     dstlb = (float *)s->output->extended_data[4];
861     dstrb = (float *)s->output->extended_data[5];
862     dstls = (float *)s->output->extended_data[6];
863     dstrs = (float *)s->output->extended_data[7];
864
865     get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
866
867     c_mag  = powf(1.f - fabsf(x), s->fc_x)   * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
868     l_mag  = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
869     r_mag  = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
870     lb_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
871     rb_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
872     ls_mag = powf(.5f * ( x + 1.f), s->sl_x) * powf(1.f - fabsf(y), s->sl_y) * mag_total;
873     rs_mag = powf(.5f * (-x + 1.f), s->sr_x) * powf(1.f - fabsf(y), s->sr_y) * mag_total;
874
875     dstl[2 * n    ] = l_mag * cosf(l_phase);
876     dstl[2 * n + 1] = l_mag * sinf(l_phase);
877
878     dstr[2 * n    ] = r_mag * cosf(r_phase);
879     dstr[2 * n + 1] = r_mag * sinf(r_phase);
880
881     dstc[2 * n    ] = c_mag * cosf(c_phase);
882     dstc[2 * n + 1] = c_mag * sinf(c_phase);
883
884     dstlfe[2 * n    ] = lfe_mag * cosf(c_phase);
885     dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
886
887     dstlb[2 * n    ] = lb_mag * cosf(l_phase);
888     dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
889
890     dstrb[2 * n    ] = rb_mag * cosf(r_phase);
891     dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
892
893     dstls[2 * n    ] = ls_mag * cosf(l_phase);
894     dstls[2 * n + 1] = ls_mag * sinf(l_phase);
895
896     dstrs[2 * n    ] = rs_mag * cosf(r_phase);
897     dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
898 }
899
900 static void upmix_7_1_5_0_side(AVFilterContext *ctx,
901                                float c_re, float c_im,
902                                float mag_totall, float mag_totalr,
903                                float fl_phase, float fr_phase,
904                                float bl_phase, float br_phase,
905                                float sl_phase, float sr_phase,
906                                float xl, float yl,
907                                float xr, float yr,
908                                int n)
909 {
910     float fl_mag, fr_mag, ls_mag, rs_mag, lb_mag, rb_mag;
911     float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
912     float lfe_mag, c_phase, mag_total = (mag_totall + mag_totalr) * 0.5;
913     AudioSurroundContext *s = ctx->priv;
914
915     dstl  = (float *)s->output->extended_data[0];
916     dstr  = (float *)s->output->extended_data[1];
917     dstc  = (float *)s->output->extended_data[2];
918     dstlfe = (float *)s->output->extended_data[3];
919     dstlb = (float *)s->output->extended_data[4];
920     dstrb = (float *)s->output->extended_data[5];
921     dstls = (float *)s->output->extended_data[6];
922     dstrs = (float *)s->output->extended_data[7];
923
924     c_phase = atan2f(c_im, c_re);
925
926     get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
927
928     fl_mag = powf(.5f * (xl + 1.f), s->fl_x) * powf((yl + 1.f) * .5f, s->fl_y) * mag_totall;
929     fr_mag = powf(.5f * (xr + 1.f), s->fr_x) * powf((yr + 1.f) * .5f, s->fr_y) * mag_totalr;
930     lb_mag = powf(.5f * (-xl + 1.f), s->bl_x) * powf((yl + 1.f) * .5f, s->bl_y) * mag_totall;
931     rb_mag = powf(.5f * (-xr + 1.f), s->br_x) * powf((yr + 1.f) * .5f, s->br_y) * mag_totalr;
932     ls_mag = powf(1.f - fabsf(xl), s->sl_x) * powf((yl + 1.f) * .5f, s->sl_y) * mag_totall;
933     rs_mag = powf(1.f - fabsf(xr), s->sr_x) * powf((yr + 1.f) * .5f, s->sr_y) * mag_totalr;
934
935     dstl[2 * n    ] = fl_mag * cosf(fl_phase);
936     dstl[2 * n + 1] = fl_mag * sinf(fl_phase);
937
938     dstr[2 * n    ] = fr_mag * cosf(fr_phase);
939     dstr[2 * n + 1] = fr_mag * sinf(fr_phase);
940
941     dstc[2 * n    ] = c_re;
942     dstc[2 * n + 1] = c_im;
943
944     dstlfe[2 * n    ] = lfe_mag * cosf(c_phase);
945     dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
946
947     dstlb[2 * n    ] = lb_mag * cosf(bl_phase);
948     dstlb[2 * n + 1] = lb_mag * sinf(bl_phase);
949
950     dstrb[2 * n    ] = rb_mag * cosf(br_phase);
951     dstrb[2 * n + 1] = rb_mag * sinf(br_phase);
952
953     dstls[2 * n    ] = ls_mag * cosf(sl_phase);
954     dstls[2 * n + 1] = ls_mag * sinf(sl_phase);
955
956     dstrs[2 * n    ] = rs_mag * cosf(sr_phase);
957     dstrs[2 * n + 1] = rs_mag * sinf(sr_phase);
958 }
959
960 static void upmix_7_1_5_1(AVFilterContext *ctx,
961                           float c_re, float c_im,
962                           float lfe_re, float lfe_im,
963                           float mag_totall, float mag_totalr,
964                           float fl_phase, float fr_phase,
965                           float bl_phase, float br_phase,
966                           float sl_phase, float sr_phase,
967                           float xl, float yl,
968                           float xr, float yr,
969                           int n)
970 {
971     float fl_mag, fr_mag, ls_mag, rs_mag, lb_mag, rb_mag;
972     float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
973     AudioSurroundContext *s = ctx->priv;
974
975     dstl  = (float *)s->output->extended_data[0];
976     dstr  = (float *)s->output->extended_data[1];
977     dstc  = (float *)s->output->extended_data[2];
978     dstlfe = (float *)s->output->extended_data[3];
979     dstlb = (float *)s->output->extended_data[4];
980     dstrb = (float *)s->output->extended_data[5];
981     dstls = (float *)s->output->extended_data[6];
982     dstrs = (float *)s->output->extended_data[7];
983
984     fl_mag = powf(.5f * (xl + 1.f), s->fl_x) * powf((yl + 1.f) * .5f, s->fl_y) * mag_totall;
985     fr_mag = powf(.5f * (xr + 1.f), s->fr_x) * powf((yr + 1.f) * .5f, s->fr_y) * mag_totalr;
986     lb_mag = powf(.5f * (-xl + 1.f), s->bl_x) * powf((yl + 1.f) * .5f, s->bl_y) * mag_totall;
987     rb_mag = powf(.5f * (-xr + 1.f), s->br_x) * powf((yr + 1.f) * .5f, s->br_y) * mag_totalr;
988     ls_mag = powf(1.f - fabsf(xl), s->sl_x) * powf((yl + 1.f) * .5f, s->sl_y) * mag_totall;
989     rs_mag = powf(1.f - fabsf(xr), s->sl_x) * powf((yr + 1.f) * .5f, s->sr_y) * mag_totalr;
990
991     dstl[2 * n    ] = fl_mag * cosf(fl_phase);
992     dstl[2 * n + 1] = fl_mag * sinf(fl_phase);
993
994     dstr[2 * n    ] = fr_mag * cosf(fr_phase);
995     dstr[2 * n + 1] = fr_mag * sinf(fr_phase);
996
997     dstc[2 * n    ] = c_re;
998     dstc[2 * n + 1] = c_im;
999
1000     dstlfe[2 * n    ] = lfe_re;
1001     dstlfe[2 * n + 1] = lfe_im;
1002
1003     dstlb[2 * n    ] = lb_mag * cosf(bl_phase);
1004     dstlb[2 * n + 1] = lb_mag * sinf(bl_phase);
1005
1006     dstrb[2 * n    ] = rb_mag * cosf(br_phase);
1007     dstrb[2 * n + 1] = rb_mag * sinf(br_phase);
1008
1009     dstls[2 * n    ] = ls_mag * cosf(sl_phase);
1010     dstls[2 * n + 1] = ls_mag * sinf(sl_phase);
1011
1012     dstrs[2 * n    ] = rs_mag * cosf(sr_phase);
1013     dstrs[2 * n + 1] = rs_mag * sinf(sr_phase);
1014 }
1015
1016 static void filter_stereo(AVFilterContext *ctx)
1017 {
1018     AudioSurroundContext *s = ctx->priv;
1019     float *srcl, *srcr;
1020     int n;
1021
1022     srcl = (float *)s->input->extended_data[0];
1023     srcr = (float *)s->input->extended_data[1];
1024
1025     for (n = 0; n < s->buf_size; n++) {
1026         float l_re = srcl[2 * n], r_re = srcr[2 * n];
1027         float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
1028         float c_phase = atan2f(l_im + r_im, l_re + r_re);
1029         float l_mag = hypotf(l_re, l_im);
1030         float r_mag = hypotf(r_re, r_im);
1031         float l_phase = atan2f(l_im, l_re);
1032         float r_phase = atan2f(r_im, r_re);
1033         float phase_dif = fabsf(l_phase - r_phase);
1034         float mag_sum = l_mag + r_mag;
1035         float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum;
1036         float mag_total = hypotf(l_mag, r_mag);
1037         float x, y;
1038
1039         if (phase_dif > M_PI)
1040             phase_dif = 2 * M_PI - phase_dif;
1041
1042         stereo_position(mag_dif, phase_dif, &x, &y);
1043
1044         s->upmix_stereo(ctx, l_phase, r_phase, c_phase, mag_total, x, y, n);
1045     }
1046 }
1047
1048 static void filter_surround(AVFilterContext *ctx)
1049 {
1050     AudioSurroundContext *s = ctx->priv;
1051     float *srcl, *srcr, *srcc;
1052     int n;
1053
1054     srcl = (float *)s->input->extended_data[0];
1055     srcr = (float *)s->input->extended_data[1];
1056     srcc = (float *)s->input->extended_data[2];
1057
1058     for (n = 0; n < s->buf_size; n++) {
1059         float l_re = srcl[2 * n], r_re = srcr[2 * n];
1060         float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
1061         float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
1062         float c_mag = hypotf(c_re, c_im);
1063         float c_phase = atan2f(c_im, c_re);
1064         float l_mag = hypotf(l_re, l_im);
1065         float r_mag = hypotf(r_re, r_im);
1066         float l_phase = atan2f(l_im, l_re);
1067         float r_phase = atan2f(r_im, r_re);
1068         float phase_dif = fabsf(l_phase - r_phase);
1069         float mag_sum = l_mag + r_mag;
1070         float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum;
1071         float mag_total = hypotf(l_mag, r_mag);
1072         float x, y;
1073
1074         if (phase_dif > M_PI)
1075             phase_dif = 2 * M_PI - phase_dif;
1076
1077         stereo_position(mag_dif, phase_dif, &x, &y);
1078
1079         s->upmix_3_0(ctx, l_phase, r_phase, c_phase, c_mag, mag_total, x, y, n);
1080     }
1081 }
1082
1083 static void filter_2_1(AVFilterContext *ctx)
1084 {
1085     AudioSurroundContext *s = ctx->priv;
1086     float *srcl, *srcr, *srclfe;
1087     int n;
1088
1089     srcl = (float *)s->input->extended_data[0];
1090     srcr = (float *)s->input->extended_data[1];
1091     srclfe = (float *)s->input->extended_data[2];
1092
1093     for (n = 0; n < s->buf_size; n++) {
1094         float l_re = srcl[2 * n], r_re = srcr[2 * n];
1095         float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
1096         float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
1097         float c_phase = atan2f(l_im + r_im, l_re + r_re);
1098         float l_mag = hypotf(l_re, l_im);
1099         float r_mag = hypotf(r_re, r_im);
1100         float l_phase = atan2f(l_im, l_re);
1101         float r_phase = atan2f(r_im, r_re);
1102         float phase_dif = fabsf(l_phase - r_phase);
1103         float mag_sum = l_mag + r_mag;
1104         float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum;
1105         float mag_total = hypotf(l_mag, r_mag);
1106         float x, y;
1107
1108         if (phase_dif > M_PI)
1109             phase_dif = 2 * M_PI - phase_dif;
1110
1111         stereo_position(mag_dif, phase_dif, &x, &y);
1112
1113         s->upmix_2_1(ctx, l_phase, r_phase, c_phase, mag_total, lfe_re, lfe_im, x, y, n);
1114     }
1115 }
1116
1117 static void filter_5_0_side(AVFilterContext *ctx)
1118 {
1119     AudioSurroundContext *s = ctx->priv;
1120     float *srcl, *srcr, *srcc, *srcsl, *srcsr;
1121     int n;
1122
1123     srcl = (float *)s->input->extended_data[0];
1124     srcr = (float *)s->input->extended_data[1];
1125     srcc = (float *)s->input->extended_data[2];
1126     srcsl = (float *)s->input->extended_data[3];
1127     srcsr = (float *)s->input->extended_data[4];
1128
1129     for (n = 0; n < s->buf_size; n++) {
1130         float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
1131         float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
1132         float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
1133         float sl_re = srcsl[2 * n], sl_im = srcsl[2 * n + 1];
1134         float sr_re = srcsr[2 * n], sr_im = srcsr[2 * n + 1];
1135         float fl_mag = hypotf(fl_re, fl_im);
1136         float fr_mag = hypotf(fr_re, fr_im);
1137         float fl_phase = atan2f(fl_im, fl_re);
1138         float fr_phase = atan2f(fr_im, fr_re);
1139         float sl_mag = hypotf(sl_re, sl_im);
1140         float sr_mag = hypotf(sr_re, sr_im);
1141         float sl_phase = atan2f(sl_im, sl_re);
1142         float sr_phase = atan2f(sr_im, sr_re);
1143         float phase_difl = fabsf(fl_phase - sl_phase);
1144         float phase_difr = fabsf(fr_phase - sr_phase);
1145         float magl_sum = fl_mag + sl_mag;
1146         float magr_sum = fr_mag + sr_mag;
1147         float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - sl_mag) / magl_sum;
1148         float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - sr_mag) / magr_sum;
1149         float mag_totall = hypotf(fl_mag, sl_mag);
1150         float mag_totalr = hypotf(fr_mag, sr_mag);
1151         float bl_phase = atan2f(fl_im + sl_im, fl_re + sl_re);
1152         float br_phase = atan2f(fr_im + sr_im, fr_re + sr_re);
1153         float xl, yl;
1154         float xr, yr;
1155
1156         if (phase_difl > M_PI)
1157             phase_difl = 2 * M_PI - phase_difl;
1158
1159         if (phase_difr > M_PI)
1160             phase_difr = 2 * M_PI - phase_difr;
1161
1162         stereo_position(mag_difl, phase_difl, &xl, &yl);
1163         stereo_position(mag_difr, phase_difr, &xr, &yr);
1164
1165         s->upmix_5_0(ctx, c_re, c_im,
1166                      mag_totall, mag_totalr,
1167                      fl_phase, fr_phase,
1168                      bl_phase, br_phase,
1169                      sl_phase, sr_phase,
1170                      xl, yl, xr, yr, n);
1171     }
1172 }
1173
1174 static void filter_5_1_side(AVFilterContext *ctx)
1175 {
1176     AudioSurroundContext *s = ctx->priv;
1177     float *srcl, *srcr, *srcc, *srclfe, *srcsl, *srcsr;
1178     int n;
1179
1180     srcl = (float *)s->input->extended_data[0];
1181     srcr = (float *)s->input->extended_data[1];
1182     srcc = (float *)s->input->extended_data[2];
1183     srclfe = (float *)s->input->extended_data[3];
1184     srcsl = (float *)s->input->extended_data[4];
1185     srcsr = (float *)s->input->extended_data[5];
1186
1187     for (n = 0; n < s->buf_size; n++) {
1188         float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
1189         float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
1190         float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
1191         float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
1192         float sl_re = srcsl[2 * n], sl_im = srcsl[2 * n + 1];
1193         float sr_re = srcsr[2 * n], sr_im = srcsr[2 * n + 1];
1194         float fl_mag = hypotf(fl_re, fl_im);
1195         float fr_mag = hypotf(fr_re, fr_im);
1196         float fl_phase = atan2f(fl_im, fl_re);
1197         float fr_phase = atan2f(fr_im, fr_re);
1198         float sl_mag = hypotf(sl_re, sl_im);
1199         float sr_mag = hypotf(sr_re, sr_im);
1200         float sl_phase = atan2f(sl_im, sl_re);
1201         float sr_phase = atan2f(sr_im, sr_re);
1202         float phase_difl = fabsf(fl_phase - sl_phase);
1203         float phase_difr = fabsf(fr_phase - sr_phase);
1204         float magl_sum = fl_mag + sl_mag;
1205         float magr_sum = fr_mag + sr_mag;
1206         float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - sl_mag) / magl_sum;
1207         float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - sr_mag) / magr_sum;
1208         float mag_totall = hypotf(fl_mag, sl_mag);
1209         float mag_totalr = hypotf(fr_mag, sr_mag);
1210         float bl_phase = atan2f(fl_im + sl_im, fl_re + sl_re);
1211         float br_phase = atan2f(fr_im + sr_im, fr_re + sr_re);
1212         float xl, yl;
1213         float xr, yr;
1214
1215         if (phase_difl > M_PI)
1216             phase_difl = 2 * M_PI - phase_difl;
1217
1218         if (phase_difr > M_PI)
1219             phase_difr = 2 * M_PI - phase_difr;
1220
1221         stereo_position(mag_difl, phase_difl, &xl, &yl);
1222         stereo_position(mag_difr, phase_difr, &xr, &yr);
1223
1224         s->upmix_5_1(ctx, c_re, c_im, lfe_re, lfe_im,
1225                      mag_totall, mag_totalr,
1226                      fl_phase, fr_phase,
1227                      bl_phase, br_phase,
1228                      sl_phase, sr_phase,
1229                      xl, yl, xr, yr, n);
1230     }
1231 }
1232
1233 static void filter_5_1_back(AVFilterContext *ctx)
1234 {
1235     AudioSurroundContext *s = ctx->priv;
1236     float *srcl, *srcr, *srcc, *srclfe, *srcbl, *srcbr;
1237     int n;
1238
1239     srcl = (float *)s->input->extended_data[0];
1240     srcr = (float *)s->input->extended_data[1];
1241     srcc = (float *)s->input->extended_data[2];
1242     srclfe = (float *)s->input->extended_data[3];
1243     srcbl = (float *)s->input->extended_data[4];
1244     srcbr = (float *)s->input->extended_data[5];
1245
1246     for (n = 0; n < s->buf_size; n++) {
1247         float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
1248         float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
1249         float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
1250         float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
1251         float bl_re = srcbl[2 * n], bl_im = srcbl[2 * n + 1];
1252         float br_re = srcbr[2 * n], br_im = srcbr[2 * n + 1];
1253         float fl_mag = hypotf(fl_re, fl_im);
1254         float fr_mag = hypotf(fr_re, fr_im);
1255         float fl_phase = atan2f(fl_im, fl_re);
1256         float fr_phase = atan2f(fr_im, fr_re);
1257         float bl_mag = hypotf(bl_re, bl_im);
1258         float br_mag = hypotf(br_re, br_im);
1259         float bl_phase = atan2f(bl_im, bl_re);
1260         float br_phase = atan2f(br_im, br_re);
1261         float phase_difl = fabsf(fl_phase - bl_phase);
1262         float phase_difr = fabsf(fr_phase - br_phase);
1263         float magl_sum = fl_mag + bl_mag;
1264         float magr_sum = fr_mag + br_mag;
1265         float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - bl_mag) / magl_sum;
1266         float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - br_mag) / magr_sum;
1267         float mag_totall = hypotf(fl_mag, bl_mag);
1268         float mag_totalr = hypotf(fr_mag, br_mag);
1269         float sl_phase = atan2f(fl_im + bl_im, fl_re + bl_re);
1270         float sr_phase = atan2f(fr_im + br_im, fr_re + br_re);
1271         float xl, yl;
1272         float xr, yr;
1273
1274         if (phase_difl > M_PI)
1275             phase_difl = 2 * M_PI - phase_difl;
1276
1277         if (phase_difr > M_PI)
1278             phase_difr = 2 * M_PI - phase_difr;
1279
1280         stereo_position(mag_difl, phase_difl, &xl, &yl);
1281         stereo_position(mag_difr, phase_difr, &xr, &yr);
1282
1283         s->upmix_5_1(ctx, c_re, c_im, lfe_re, lfe_im,
1284                      mag_totall, mag_totalr,
1285                      fl_phase, fr_phase,
1286                      bl_phase, br_phase,
1287                      sl_phase, sr_phase,
1288                      xl, yl, xr, yr, n);
1289     }
1290 }
1291
1292 static int init(AVFilterContext *ctx)
1293 {
1294     AudioSurroundContext *s = ctx->priv;
1295     float overlap;
1296     int i;
1297
1298     if (!(s->out_channel_layout = av_get_channel_layout(s->out_channel_layout_str))) {
1299         av_log(ctx, AV_LOG_ERROR, "Error parsing output channel layout '%s'.\n",
1300                s->out_channel_layout_str);
1301         return AVERROR(EINVAL);
1302     }
1303
1304     if (!(s->in_channel_layout = av_get_channel_layout(s->in_channel_layout_str))) {
1305         av_log(ctx, AV_LOG_ERROR, "Error parsing input channel layout '%s'.\n",
1306                s->in_channel_layout_str);
1307         return AVERROR(EINVAL);
1308     }
1309
1310     if (s->lowcutf >= s->highcutf) {
1311         av_log(ctx, AV_LOG_ERROR, "Low cut-off '%d' should be less than high cut-off '%d'.\n",
1312                s->lowcutf, s->highcutf);
1313         return AVERROR(EINVAL);
1314     }
1315
1316     switch (s->in_channel_layout) {
1317     case AV_CH_LAYOUT_STEREO:
1318         s->filter = filter_stereo;
1319         switch (s->out_channel_layout) {
1320         case AV_CH_LAYOUT_MONO:
1321             s->upmix_stereo = upmix_1_0;
1322             break;
1323         case AV_CH_LAYOUT_STEREO:
1324             s->upmix_stereo = upmix_stereo;
1325             break;
1326         case AV_CH_LAYOUT_2POINT1:
1327             s->upmix_stereo = upmix_2_1;
1328             break;
1329         case AV_CH_LAYOUT_SURROUND:
1330             s->upmix_stereo = upmix_3_0;
1331             break;
1332         case AV_CH_LAYOUT_3POINT1:
1333             s->upmix_stereo = upmix_3_1;
1334             break;
1335         case AV_CH_LAYOUT_4POINT0:
1336             s->upmix_stereo = upmix_4_0;
1337             break;
1338         case AV_CH_LAYOUT_4POINT1:
1339             s->upmix_stereo = upmix_4_1;
1340             break;
1341         case AV_CH_LAYOUT_5POINT0_BACK:
1342             s->upmix_stereo = upmix_5_0_back;
1343             break;
1344         case AV_CH_LAYOUT_5POINT1_BACK:
1345             s->upmix_stereo = upmix_5_1_back;
1346             break;
1347         case AV_CH_LAYOUT_6POINT0:
1348             s->upmix_stereo = upmix_6_0;
1349             break;
1350         case AV_CH_LAYOUT_6POINT1:
1351             s->upmix_stereo = upmix_6_1;
1352             break;
1353         case AV_CH_LAYOUT_7POINT0:
1354             s->upmix_stereo = upmix_7_0;
1355             break;
1356         case AV_CH_LAYOUT_7POINT1:
1357             s->upmix_stereo = upmix_7_1;
1358             break;
1359         default:
1360             goto fail;
1361         }
1362         break;
1363     case AV_CH_LAYOUT_2POINT1:
1364         s->filter = filter_2_1;
1365         switch (s->out_channel_layout) {
1366         case AV_CH_LAYOUT_5POINT1_BACK:
1367             s->upmix_2_1 = upmix_5_1_back_2_1;
1368             break;
1369         default:
1370             goto fail;
1371         }
1372         break;
1373     case AV_CH_LAYOUT_SURROUND:
1374         s->filter = filter_surround;
1375         switch (s->out_channel_layout) {
1376         case AV_CH_LAYOUT_3POINT1:
1377             s->upmix_3_0 = upmix_3_1_surround;
1378             break;
1379         case AV_CH_LAYOUT_5POINT1_BACK:
1380             s->upmix_3_0 = upmix_5_1_back_surround;
1381             break;
1382         default:
1383             goto fail;
1384         }
1385         break;
1386     case AV_CH_LAYOUT_5POINT0:
1387         s->filter = filter_5_0_side;
1388         switch (s->out_channel_layout) {
1389         case AV_CH_LAYOUT_7POINT1:
1390             s->upmix_5_0 = upmix_7_1_5_0_side;
1391             break;
1392         default:
1393             goto fail;
1394         }
1395         break;
1396     case AV_CH_LAYOUT_5POINT1:
1397         s->filter = filter_5_1_side;
1398         switch (s->out_channel_layout) {
1399         case AV_CH_LAYOUT_7POINT1:
1400             s->upmix_5_1 = upmix_7_1_5_1;
1401             break;
1402         default:
1403             goto fail;
1404         }
1405         break;
1406     case AV_CH_LAYOUT_5POINT1_BACK:
1407         s->filter = filter_5_1_back;
1408         switch (s->out_channel_layout) {
1409         case AV_CH_LAYOUT_7POINT1:
1410             s->upmix_5_1 = upmix_7_1_5_1;
1411             break;
1412         default:
1413             goto fail;
1414         }
1415         break;
1416     default:
1417 fail:
1418         av_log(ctx, AV_LOG_ERROR, "Unsupported upmix: '%s' -> '%s'.\n",
1419                s->in_channel_layout_str, s->out_channel_layout_str);
1420         return AVERROR(EINVAL);
1421     }
1422
1423     s->buf_size = 1 << av_log2(s->win_size);
1424     s->pts = AV_NOPTS_VALUE;
1425
1426     s->window_func_lut = av_calloc(s->buf_size, sizeof(*s->window_func_lut));
1427     if (!s->window_func_lut)
1428         return AVERROR(ENOMEM);
1429
1430     generate_window_func(s->window_func_lut, s->buf_size, s->win_func, &overlap);
1431     if (s->overlap == 1)
1432         s->overlap = overlap;
1433
1434     for (i = 0; i < s->buf_size; i++)
1435         s->window_func_lut[i] = sqrtf(s->window_func_lut[i] / s->buf_size);
1436     s->hop_size = s->buf_size * (1. - s->overlap);
1437     if (s->hop_size <= 0)
1438         return AVERROR(EINVAL);
1439
1440     if (s->all_x >= 0.f)
1441         s->fc_x = s->fl_x = s->fr_x = s->bc_x = s->sl_x = s->sr_x = s->bl_x = s->br_x = s->all_x;
1442     if (s->all_y >= 0.f)
1443         s->fc_y = s->fl_y = s->fr_y = s->bc_y = s->sl_y = s->sr_y = s->bl_y = s->br_y = s->all_y;
1444
1445     return 0;
1446 }
1447
1448 static int fft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
1449 {
1450     AudioSurroundContext *s = ctx->priv;
1451     const float level_in = s->input_levels[ch];
1452     float *dst;
1453     int n;
1454
1455     memset(s->input->extended_data[ch] + s->buf_size * sizeof(float), 0, s->buf_size * sizeof(float));
1456
1457     dst = (float *)s->input->extended_data[ch];
1458     for (n = 0; n < s->buf_size; n++) {
1459         dst[n] *= s->window_func_lut[n] * level_in;
1460     }
1461
1462     av_rdft_calc(s->rdft[ch], (float *)s->input->extended_data[ch]);
1463
1464     return 0;
1465 }
1466
1467 static int ifft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
1468 {
1469     AudioSurroundContext *s = ctx->priv;
1470     const float level_out = s->output_levels[ch];
1471     AVFrame *out = arg;
1472     float *dst, *ptr;
1473     int n;
1474
1475     av_rdft_calc(s->irdft[ch], (float *)s->output->extended_data[ch]);
1476
1477     dst = (float *)s->output->extended_data[ch];
1478     ptr = (float *)s->overlap_buffer->extended_data[ch];
1479
1480     memmove(s->overlap_buffer->extended_data[ch],
1481             s->overlap_buffer->extended_data[ch] + s->hop_size * sizeof(float),
1482             s->buf_size * sizeof(float));
1483     memset(s->overlap_buffer->extended_data[ch] + s->buf_size * sizeof(float),
1484            0, s->hop_size * sizeof(float));
1485
1486     for (n = 0; n < s->buf_size; n++) {
1487         ptr[n] += dst[n] * s->window_func_lut[n] * level_out;
1488     }
1489
1490     ptr = (float *)s->overlap_buffer->extended_data[ch];
1491     dst = (float *)out->extended_data[ch];
1492     memcpy(dst, ptr, s->hop_size * sizeof(float));
1493
1494     return 0;
1495 }
1496
1497 static int filter_frame(AVFilterLink *inlink)
1498 {
1499     AVFilterContext *ctx = inlink->dst;
1500     AVFilterLink *outlink = ctx->outputs[0];
1501     AudioSurroundContext *s = ctx->priv;
1502     AVFrame *out;
1503     int ret;
1504
1505     ret = av_audio_fifo_peek(s->fifo, (void **)s->input->extended_data, s->buf_size);
1506     if (ret < 0)
1507         return ret;
1508
1509     ctx->internal->execute(ctx, fft_channel, NULL, NULL, inlink->channels);
1510
1511     s->filter(ctx);
1512
1513     out = ff_get_audio_buffer(outlink, s->hop_size);
1514     if (!out)
1515         return AVERROR(ENOMEM);
1516
1517     ctx->internal->execute(ctx, ifft_channel, out, NULL, outlink->channels);
1518
1519     out->pts = s->pts;
1520     if (s->pts != AV_NOPTS_VALUE)
1521         s->pts += av_rescale_q(out->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
1522     av_audio_fifo_drain(s->fifo, FFMIN(av_audio_fifo_size(s->fifo), s->hop_size));
1523
1524     return ff_filter_frame(outlink, out);
1525 }
1526
1527 static int activate(AVFilterContext *ctx)
1528 {
1529     AVFilterLink *inlink = ctx->inputs[0];
1530     AVFilterLink *outlink = ctx->outputs[0];
1531     AudioSurroundContext *s = ctx->priv;
1532     AVFrame *in = NULL;
1533     int ret = 0, status;
1534     int64_t pts;
1535
1536     FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
1537
1538     if (!s->eof && av_audio_fifo_size(s->fifo) < s->buf_size) {
1539         ret = ff_inlink_consume_frame(inlink, &in);
1540         if (ret < 0)
1541             return ret;
1542
1543         if (ret > 0) {
1544             ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
1545                                       in->nb_samples);
1546             if (ret >= 0 && s->pts == AV_NOPTS_VALUE)
1547                 s->pts = in->pts;
1548
1549             av_frame_free(&in);
1550             if (ret < 0)
1551                 return ret;
1552         }
1553     }
1554
1555     if ((av_audio_fifo_size(s->fifo) >= s->buf_size) ||
1556         (av_audio_fifo_size(s->fifo) > 0 && s->eof)) {
1557         ret = filter_frame(inlink);
1558         if (av_audio_fifo_size(s->fifo) >= s->buf_size)
1559             ff_filter_set_ready(ctx, 100);
1560         return ret;
1561     }
1562
1563     if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
1564         if (status == AVERROR_EOF) {
1565             s->eof = 1;
1566             if (av_audio_fifo_size(s->fifo) >= 0) {
1567                 ff_filter_set_ready(ctx, 100);
1568                 return 0;
1569             }
1570         }
1571     }
1572
1573     if (s->eof && av_audio_fifo_size(s->fifo) <= 0) {
1574         ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
1575         return 0;
1576     }
1577
1578     if (!s->eof)
1579         FF_FILTER_FORWARD_WANTED(outlink, inlink);
1580
1581     return FFERROR_NOT_READY;
1582 }
1583
1584 static av_cold void uninit(AVFilterContext *ctx)
1585 {
1586     AudioSurroundContext *s = ctx->priv;
1587     int ch;
1588
1589     av_frame_free(&s->input);
1590     av_frame_free(&s->output);
1591     av_frame_free(&s->overlap_buffer);
1592
1593     for (ch = 0; ch < s->nb_in_channels; ch++) {
1594         av_rdft_end(s->rdft[ch]);
1595     }
1596     for (ch = 0; ch < s->nb_out_channels; ch++) {
1597         av_rdft_end(s->irdft[ch]);
1598     }
1599     av_freep(&s->input_levels);
1600     av_freep(&s->output_levels);
1601     av_freep(&s->rdft);
1602     av_freep(&s->irdft);
1603     av_audio_fifo_free(s->fifo);
1604     av_freep(&s->window_func_lut);
1605 }
1606
1607 #define OFFSET(x) offsetof(AudioSurroundContext, x)
1608 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1609
1610 static const AVOption surround_options[] = {
1611     { "chl_out",   "set output channel layout", OFFSET(out_channel_layout_str), AV_OPT_TYPE_STRING, {.str="5.1"}, 0,   0, FLAGS },
1612     { "chl_in",    "set input channel layout",  OFFSET(in_channel_layout_str),  AV_OPT_TYPE_STRING, {.str="stereo"},0, 0, FLAGS },
1613     { "level_in",  "set input level",           OFFSET(level_in),               AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  10, FLAGS },
1614     { "level_out", "set output level",          OFFSET(level_out),              AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  10, FLAGS },
1615     { "lfe",       "output LFE",                OFFSET(output_lfe),             AV_OPT_TYPE_BOOL,   {.i64=1},     0,   1, FLAGS },
1616     { "lfe_low",   "LFE low cut off",           OFFSET(lowcutf),                AV_OPT_TYPE_INT,    {.i64=128},   0, 256, FLAGS },
1617     { "lfe_high",  "LFE high cut off",          OFFSET(highcutf),               AV_OPT_TYPE_INT,    {.i64=256},   0, 512, FLAGS },
1618     { "lfe_mode",  "set LFE channel mode",      OFFSET(lfe_mode),               AV_OPT_TYPE_INT,    {.i64=0},     0,   1, FLAGS, "lfe_mode" },
1619     {  "add",      "just add LFE channel",                  0,                  AV_OPT_TYPE_CONST,  {.i64=0},     0,   1, FLAGS, "lfe_mode" },
1620     {  "sub",      "substract LFE channel with others",     0,                  AV_OPT_TYPE_CONST,  {.i64=1},     0,   1, FLAGS, "lfe_mode" },
1621     { "fc_in",     "set front center channel input level",  OFFSET(fc_in),      AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  10, FLAGS },
1622     { "fc_out",    "set front center channel output level", OFFSET(fc_out),     AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  10, FLAGS },
1623     { "lfe_in",    "set lfe channel input level",  OFFSET(lfe_in),              AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  10, FLAGS },
1624     { "lfe_out",   "set lfe channel output level", OFFSET(lfe_out),             AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  10, FLAGS },
1625     { "allx",      "set all channel's x spread",         OFFSET(all_x),         AV_OPT_TYPE_FLOAT,  {.dbl=-1},   -1,  15, FLAGS },
1626     { "ally",      "set all channel's y spread",         OFFSET(all_y),         AV_OPT_TYPE_FLOAT,  {.dbl=-1},   -1,  15, FLAGS },
1627     { "fcx",       "set front center channel x spread",  OFFSET(fc_x),          AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  15, FLAGS },
1628     { "flx",       "set front left channel x spread",    OFFSET(fl_x),          AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  15, FLAGS },
1629     { "frx",       "set front right channel x spread",   OFFSET(fr_x),          AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  15, FLAGS },
1630     { "blx",       "set back left channel x spread",     OFFSET(bl_x),          AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  15, FLAGS },
1631     { "brx",       "set back right channel x spread",    OFFSET(br_x),          AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  15, FLAGS },
1632     { "slx",       "set side left channel x spread",     OFFSET(sl_x),          AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  15, FLAGS },
1633     { "srx",       "set side right channel x spread",    OFFSET(sr_x),          AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  15, FLAGS },
1634     { "bcx",       "set back center channel x spread",   OFFSET(bc_x),          AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  15, FLAGS },
1635     { "fcy",       "set front center channel y spread",  OFFSET(fc_y),          AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  15, FLAGS },
1636     { "fly",       "set front left channel y spread",    OFFSET(fl_y),          AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  15, FLAGS },
1637     { "fry",       "set front right channel y spread",   OFFSET(fr_y),          AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  15, FLAGS },
1638     { "bly",       "set back left channel y spread",     OFFSET(bl_y),          AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  15, FLAGS },
1639     { "bry",       "set back right channel y spread",    OFFSET(br_y),          AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  15, FLAGS },
1640     { "sly",       "set side left channel y spread",     OFFSET(sl_y),          AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  15, FLAGS },
1641     { "sry",       "set side right channel y spread",    OFFSET(sr_y),          AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  15, FLAGS },
1642     { "bcy",       "set back center channel y spread",   OFFSET(bc_y),          AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  15, FLAGS },
1643     { "win_size", "set window size", OFFSET(win_size), AV_OPT_TYPE_INT, {.i64 = 4096}, 1024, 65536, FLAGS },
1644     { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
1645         { "rect",     "Rectangular",      0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT},     0, 0, FLAGS, "win_func" },
1646         { "bartlett", "Bartlett",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
1647         { "hann",     "Hann",             0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING},  0, 0, FLAGS, "win_func" },
1648         { "hanning",  "Hanning",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING},  0, 0, FLAGS, "win_func" },
1649         { "hamming",  "Hamming",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING},  0, 0, FLAGS, "win_func" },
1650         { "blackman", "Blackman",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
1651         { "welch",    "Welch",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH},    0, 0, FLAGS, "win_func" },
1652         { "flattop",  "Flat-top",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP},  0, 0, FLAGS, "win_func" },
1653         { "bharris",  "Blackman-Harris",  0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS},  0, 0, FLAGS, "win_func" },
1654         { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
1655         { "bhann",    "Bartlett-Hann",    0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN},    0, 0, FLAGS, "win_func" },
1656         { "sine",     "Sine",             0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE},     0, 0, FLAGS, "win_func" },
1657         { "nuttall",  "Nuttall",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL},  0, 0, FLAGS, "win_func" },
1658         { "lanczos",  "Lanczos",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS},  0, 0, FLAGS, "win_func" },
1659         { "gauss",    "Gauss",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS},    0, 0, FLAGS, "win_func" },
1660         { "tukey",    "Tukey",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY},    0, 0, FLAGS, "win_func" },
1661         { "dolph",    "Dolph-Chebyshev",  0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH},    0, 0, FLAGS, "win_func" },
1662         { "cauchy",   "Cauchy",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY},   0, 0, FLAGS, "win_func" },
1663         { "parzen",   "Parzen",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN},   0, 0, FLAGS, "win_func" },
1664         { "poisson",  "Poisson",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON},  0, 0, FLAGS, "win_func" },
1665         { "bohman",   "Bohman",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN},   0, 0, FLAGS, "win_func" },
1666     { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
1667     { NULL }
1668 };
1669
1670 AVFILTER_DEFINE_CLASS(surround);
1671
1672 static const AVFilterPad inputs[] = {
1673     {
1674         .name         = "default",
1675         .type         = AVMEDIA_TYPE_AUDIO,
1676         .config_props = config_input,
1677     },
1678     { NULL }
1679 };
1680
1681 static const AVFilterPad outputs[] = {
1682     {
1683         .name          = "default",
1684         .type          = AVMEDIA_TYPE_AUDIO,
1685         .config_props  = config_output,
1686     },
1687     { NULL }
1688 };
1689
1690 AVFilter ff_af_surround = {
1691     .name           = "surround",
1692     .description    = NULL_IF_CONFIG_SMALL("Apply audio surround upmix filter."),
1693     .query_formats  = query_formats,
1694     .priv_size      = sizeof(AudioSurroundContext),
1695     .priv_class     = &surround_class,
1696     .init           = init,
1697     .uninit         = uninit,
1698     .activate       = activate,
1699     .inputs         = inputs,
1700     .outputs        = outputs,
1701     .flags          = AVFILTER_FLAG_SLICE_THREADS,
1702 };