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