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