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