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