2 * Copyright (c) 2017 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/audio_fifo.h"
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/opt.h"
24 #include "libavcodec/avfft.h"
28 #include "window_func.h"
30 typedef struct AudioSurroundContext {
33 char *out_channel_layout_str;
34 char *in_channel_layout_str;
76 uint64_t out_channel_layout;
77 uint64_t in_channel_layout;
83 AVFrame *overlap_buffer;
88 RDFTContext **rdft, **irdft;
89 float *window_func_lut;
93 void (*filter)(AVFilterContext *ctx);
94 void (*upmix_stereo)(AVFilterContext *ctx,
101 void (*upmix_2_1)(AVFilterContext *ctx,
110 void (*upmix_3_0)(AVFilterContext *ctx,
118 void (*upmix_5_0)(AVFilterContext *ctx,
119 float c_re, float c_im,
120 float mag_totall, float mag_totalr,
121 float fl_phase, float fr_phase,
122 float bl_phase, float br_phase,
123 float sl_phase, float sr_phase,
127 void (*upmix_5_1)(AVFilterContext *ctx,
128 float c_re, float c_im,
129 float lfe_re, float lfe_im,
130 float mag_totall, float mag_totalr,
131 float fl_phase, float fr_phase,
132 float bl_phase, float br_phase,
133 float sl_phase, float sr_phase,
137 } AudioSurroundContext;
139 static int query_formats(AVFilterContext *ctx)
141 AudioSurroundContext *s = ctx->priv;
142 AVFilterFormats *formats = NULL;
143 AVFilterChannelLayouts *layouts = NULL;
146 ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLTP);
149 ret = ff_set_common_formats(ctx, formats);
154 ret = ff_add_channel_layout(&layouts, s->out_channel_layout);
158 ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
163 ret = ff_add_channel_layout(&layouts, s->in_channel_layout);
167 ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts);
171 formats = ff_all_samplerates();
173 return AVERROR(ENOMEM);
174 return ff_set_common_samplerates(ctx, formats);
177 static int config_input(AVFilterLink *inlink)
179 AVFilterContext *ctx = inlink->dst;
180 AudioSurroundContext *s = ctx->priv;
183 s->rdft = av_calloc(inlink->channels, sizeof(*s->rdft));
185 return AVERROR(ENOMEM);
187 for (ch = 0; ch < inlink->channels; ch++) {
188 s->rdft[ch] = av_rdft_init(ff_log2(s->buf_size), DFT_R2C);
190 return AVERROR(ENOMEM);
192 s->nb_in_channels = inlink->channels;
193 s->input_levels = av_malloc_array(s->nb_in_channels, sizeof(*s->input_levels));
194 if (!s->input_levels)
195 return AVERROR(ENOMEM);
196 for (ch = 0; ch < s->nb_in_channels; ch++)
197 s->input_levels[ch] = s->level_in;
198 ch = av_get_channel_layout_channel_index(inlink->channel_layout, AV_CH_FRONT_CENTER);
200 s->input_levels[ch] *= s->fc_in;
201 ch = av_get_channel_layout_channel_index(inlink->channel_layout, AV_CH_LOW_FREQUENCY);
203 s->input_levels[ch] *= s->lfe_in;
205 s->input = ff_get_audio_buffer(inlink, s->buf_size * 2);
207 return AVERROR(ENOMEM);
209 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->buf_size);
211 return AVERROR(ENOMEM);
213 s->lowcut = 1.f * s->lowcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
214 s->highcut = 1.f * s->highcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
219 static int config_output(AVFilterLink *outlink)
221 AVFilterContext *ctx = outlink->src;
222 AudioSurroundContext *s = ctx->priv;
225 s->irdft = av_calloc(outlink->channels, sizeof(*s->irdft));
227 return AVERROR(ENOMEM);
229 for (ch = 0; ch < outlink->channels; ch++) {
230 s->irdft[ch] = av_rdft_init(ff_log2(s->buf_size), IDFT_C2R);
232 return AVERROR(ENOMEM);
234 s->nb_out_channels = outlink->channels;
235 s->output_levels = av_malloc_array(s->nb_out_channels, sizeof(*s->output_levels));
236 if (!s->output_levels)
237 return AVERROR(ENOMEM);
238 for (ch = 0; ch < s->nb_out_channels; ch++)
239 s->output_levels[ch] = s->level_out;
240 ch = av_get_channel_layout_channel_index(outlink->channel_layout, AV_CH_FRONT_CENTER);
242 s->output_levels[ch] *= s->fc_out;
243 ch = av_get_channel_layout_channel_index(outlink->channel_layout, AV_CH_LOW_FREQUENCY);
245 s->output_levels[ch] *= s->lfe_out;
247 s->output = ff_get_audio_buffer(outlink, s->buf_size * 2);
248 s->overlap_buffer = ff_get_audio_buffer(outlink, s->buf_size * 2);
249 if (!s->overlap_buffer || !s->output)
250 return AVERROR(ENOMEM);
255 static void stereo_position(float a, float p, float *x, float *y)
257 *x = av_clipf(a+FFMAX(0, sinf(p-M_PI_2))*FFDIFFSIGN(a,0), -1, 1);
258 *y = av_clipf(cosf(a*M_PI_2+M_PI)*cosf(M_PI_2-p/M_PI)*M_LN10+1, -1, 1);
261 static inline void get_lfe(int output_lfe, int n, float lowcut, float highcut,
262 float *lfe_mag, float *mag_total, int lfe_mode)
264 if (output_lfe && n < highcut) {
265 *lfe_mag = n < lowcut ? 1.f : .5f*(1.f+cosf(M_PI*(lowcut-n)/(lowcut-highcut)));
266 *lfe_mag *= *mag_total;
268 *mag_total -= *lfe_mag;
274 static void upmix_1_0(AVFilterContext *ctx,
282 AudioSurroundContext *s = ctx->priv;
285 dst = (float *)s->output->extended_data[0];
287 mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
289 dst[2 * n ] = mag * cosf(c_phase);
290 dst[2 * n + 1] = mag * sinf(c_phase);
293 static void upmix_stereo(AVFilterContext *ctx,
301 AudioSurroundContext *s = ctx->priv;
302 float l_mag, r_mag, *dstl, *dstr;
304 dstl = (float *)s->output->extended_data[0];
305 dstr = (float *)s->output->extended_data[1];
307 l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
308 r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
310 dstl[2 * n ] = l_mag * cosf(l_phase);
311 dstl[2 * n + 1] = l_mag * sinf(l_phase);
313 dstr[2 * n ] = r_mag * cosf(r_phase);
314 dstr[2 * n + 1] = r_mag * sinf(r_phase);
317 static void upmix_2_1(AVFilterContext *ctx,
325 AudioSurroundContext *s = ctx->priv;
326 float lfe_mag, l_mag, r_mag, *dstl, *dstr, *dstlfe;
328 dstl = (float *)s->output->extended_data[0];
329 dstr = (float *)s->output->extended_data[1];
330 dstlfe = (float *)s->output->extended_data[2];
332 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
334 l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
335 r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
337 dstl[2 * n ] = l_mag * cosf(l_phase);
338 dstl[2 * n + 1] = l_mag * sinf(l_phase);
340 dstr[2 * n ] = r_mag * cosf(r_phase);
341 dstr[2 * n + 1] = r_mag * sinf(r_phase);
343 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
344 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
347 static void upmix_3_0(AVFilterContext *ctx,
355 AudioSurroundContext *s = ctx->priv;
356 float l_mag, r_mag, c_mag, *dstc, *dstl, *dstr;
358 dstl = (float *)s->output->extended_data[0];
359 dstr = (float *)s->output->extended_data[1];
360 dstc = (float *)s->output->extended_data[2];
362 c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
363 l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
364 r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
366 dstl[2 * n ] = l_mag * cosf(l_phase);
367 dstl[2 * n + 1] = l_mag * sinf(l_phase);
369 dstr[2 * n ] = r_mag * cosf(r_phase);
370 dstr[2 * n + 1] = r_mag * sinf(r_phase);
372 dstc[2 * n ] = c_mag * cosf(c_phase);
373 dstc[2 * n + 1] = c_mag * sinf(c_phase);
376 static void upmix_3_1(AVFilterContext *ctx,
384 AudioSurroundContext *s = ctx->priv;
385 float lfe_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstlfe;
387 dstl = (float *)s->output->extended_data[0];
388 dstr = (float *)s->output->extended_data[1];
389 dstc = (float *)s->output->extended_data[2];
390 dstlfe = (float *)s->output->extended_data[3];
392 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
394 c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
395 l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
396 r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
398 dstl[2 * n ] = l_mag * cosf(l_phase);
399 dstl[2 * n + 1] = l_mag * sinf(l_phase);
401 dstr[2 * n ] = r_mag * cosf(r_phase);
402 dstr[2 * n + 1] = r_mag * sinf(r_phase);
404 dstc[2 * n ] = c_mag * cosf(c_phase);
405 dstc[2 * n + 1] = c_mag * sinf(c_phase);
407 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
408 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
411 static void upmix_3_1_surround(AVFilterContext *ctx,
420 AudioSurroundContext *s = ctx->priv;
421 float lfe_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
423 dstl = (float *)s->output->extended_data[0];
424 dstr = (float *)s->output->extended_data[1];
425 dstc = (float *)s->output->extended_data[2];
426 dstlfe = (float *)s->output->extended_data[3];
428 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &c_mag, s->lfe_mode);
430 l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
431 r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
433 dstl[2 * n ] = l_mag * cosf(l_phase);
434 dstl[2 * n + 1] = l_mag * sinf(l_phase);
436 dstr[2 * n ] = r_mag * cosf(r_phase);
437 dstr[2 * n + 1] = r_mag * sinf(r_phase);
439 dstc[2 * n ] = c_mag * cosf(c_phase);
440 dstc[2 * n + 1] = c_mag * sinf(c_phase);
442 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
443 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
446 static void upmix_4_0(AVFilterContext *ctx,
454 AudioSurroundContext *s = ctx->priv;
455 float b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb;
457 dstl = (float *)s->output->extended_data[0];
458 dstr = (float *)s->output->extended_data[1];
459 dstc = (float *)s->output->extended_data[2];
460 dstb = (float *)s->output->extended_data[3];
462 c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
463 b_mag = powf(1.f - fabsf(x), s->bc_x) * powf((1.f - y) * .5f, s->bc_y) * mag_total;
464 l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
465 r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
467 dstl[2 * n ] = l_mag * cosf(l_phase);
468 dstl[2 * n + 1] = l_mag * sinf(l_phase);
470 dstr[2 * n ] = r_mag * cosf(r_phase);
471 dstr[2 * n + 1] = r_mag * sinf(r_phase);
473 dstc[2 * n ] = c_mag * cosf(c_phase);
474 dstc[2 * n + 1] = c_mag * sinf(c_phase);
476 dstb[2 * n ] = b_mag * cosf(c_phase);
477 dstb[2 * n + 1] = b_mag * sinf(c_phase);
480 static void upmix_4_1(AVFilterContext *ctx,
488 AudioSurroundContext *s = ctx->priv;
489 float lfe_mag, b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb, *dstlfe;
491 dstl = (float *)s->output->extended_data[0];
492 dstr = (float *)s->output->extended_data[1];
493 dstc = (float *)s->output->extended_data[2];
494 dstlfe = (float *)s->output->extended_data[3];
495 dstb = (float *)s->output->extended_data[4];
497 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
499 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
500 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
502 c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
503 b_mag = powf(1.f - fabsf(x), s->bc_x) * powf((1.f - y) * .5f, s->bc_y) * mag_total;
504 l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
505 r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
507 dstl[2 * n ] = l_mag * cosf(l_phase);
508 dstl[2 * n + 1] = l_mag * sinf(l_phase);
510 dstr[2 * n ] = r_mag * cosf(r_phase);
511 dstr[2 * n + 1] = r_mag * sinf(r_phase);
513 dstc[2 * n ] = c_mag * cosf(c_phase);
514 dstc[2 * n + 1] = c_mag * sinf(c_phase);
516 dstb[2 * n ] = b_mag * cosf(c_phase);
517 dstb[2 * n + 1] = b_mag * sinf(c_phase);
520 static void upmix_5_0_back(AVFilterContext *ctx,
528 AudioSurroundContext *s = ctx->priv;
529 float l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs;
531 dstl = (float *)s->output->extended_data[0];
532 dstr = (float *)s->output->extended_data[1];
533 dstc = (float *)s->output->extended_data[2];
534 dstls = (float *)s->output->extended_data[3];
535 dstrs = (float *)s->output->extended_data[4];
537 c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
538 l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
539 r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
540 ls_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
541 rs_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
543 dstl[2 * n ] = l_mag * cosf(l_phase);
544 dstl[2 * n + 1] = l_mag * sinf(l_phase);
546 dstr[2 * n ] = r_mag * cosf(r_phase);
547 dstr[2 * n + 1] = r_mag * sinf(r_phase);
549 dstc[2 * n ] = c_mag * cosf(c_phase);
550 dstc[2 * n + 1] = c_mag * sinf(c_phase);
552 dstls[2 * n ] = ls_mag * cosf(l_phase);
553 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
555 dstrs[2 * n ] = rs_mag * cosf(r_phase);
556 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
559 static void upmix_5_1_back(AVFilterContext *ctx,
567 AudioSurroundContext *s = ctx->priv;
568 float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlfe;
570 dstl = (float *)s->output->extended_data[0];
571 dstr = (float *)s->output->extended_data[1];
572 dstc = (float *)s->output->extended_data[2];
573 dstlfe = (float *)s->output->extended_data[3];
574 dstls = (float *)s->output->extended_data[4];
575 dstrs = (float *)s->output->extended_data[5];
577 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
579 c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
580 l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
581 r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
582 ls_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
583 rs_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
585 dstl[2 * n ] = l_mag * cosf(l_phase);
586 dstl[2 * n + 1] = l_mag * sinf(l_phase);
588 dstr[2 * n ] = r_mag * cosf(r_phase);
589 dstr[2 * n + 1] = r_mag * sinf(r_phase);
591 dstc[2 * n ] = c_mag * cosf(c_phase);
592 dstc[2 * n + 1] = c_mag * sinf(c_phase);
594 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
595 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
597 dstls[2 * n ] = ls_mag * cosf(l_phase);
598 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
600 dstrs[2 * n ] = rs_mag * cosf(r_phase);
601 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
604 static void upmix_5_1_back_surround(AVFilterContext *ctx,
613 AudioSurroundContext *s = ctx->priv;
614 float lfe_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
615 float ls_mag, rs_mag, *dstls, *dstrs;
617 dstl = (float *)s->output->extended_data[0];
618 dstr = (float *)s->output->extended_data[1];
619 dstc = (float *)s->output->extended_data[2];
620 dstlfe = (float *)s->output->extended_data[3];
621 dstls = (float *)s->output->extended_data[4];
622 dstrs = (float *)s->output->extended_data[5];
624 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &c_mag, s->lfe_mode);
626 l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
627 r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
628 ls_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
629 rs_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
631 dstl[2 * n ] = l_mag * cosf(l_phase);
632 dstl[2 * n + 1] = l_mag * sinf(l_phase);
634 dstr[2 * n ] = r_mag * cosf(r_phase);
635 dstr[2 * n + 1] = r_mag * sinf(r_phase);
637 dstc[2 * n ] = c_mag * cosf(c_phase);
638 dstc[2 * n + 1] = c_mag * sinf(c_phase);
640 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
641 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
643 dstls[2 * n ] = ls_mag * cosf(l_phase);
644 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
646 dstrs[2 * n ] = rs_mag * cosf(r_phase);
647 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
650 static void upmix_5_1_back_2_1(AVFilterContext *ctx,
660 AudioSurroundContext *s = ctx->priv;
661 float c_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
662 float ls_mag, rs_mag, *dstls, *dstrs;
664 dstl = (float *)s->output->extended_data[0];
665 dstr = (float *)s->output->extended_data[1];
666 dstc = (float *)s->output->extended_data[2];
667 dstlfe = (float *)s->output->extended_data[3];
668 dstls = (float *)s->output->extended_data[4];
669 dstrs = (float *)s->output->extended_data[5];
671 c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
672 l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
673 r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
674 ls_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
675 rs_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
677 dstl[2 * n ] = l_mag * cosf(l_phase);
678 dstl[2 * n + 1] = l_mag * sinf(l_phase);
680 dstr[2 * n ] = r_mag * cosf(r_phase);
681 dstr[2 * n + 1] = r_mag * sinf(r_phase);
683 dstc[2 * n ] = c_mag * cosf(c_phase);
684 dstc[2 * n + 1] = c_mag * sinf(c_phase);
686 dstlfe[2 * n ] = lfe_re;
687 dstlfe[2 * n + 1] = lfe_im;
689 dstls[2 * n ] = ls_mag * cosf(l_phase);
690 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
692 dstrs[2 * n ] = rs_mag * cosf(r_phase);
693 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
696 static void upmix_7_0(AVFilterContext *ctx,
704 float l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
705 float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb;
706 AudioSurroundContext *s = ctx->priv;
708 dstl = (float *)s->output->extended_data[0];
709 dstr = (float *)s->output->extended_data[1];
710 dstc = (float *)s->output->extended_data[2];
711 dstlb = (float *)s->output->extended_data[3];
712 dstrb = (float *)s->output->extended_data[4];
713 dstls = (float *)s->output->extended_data[5];
714 dstrs = (float *)s->output->extended_data[6];
716 c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
717 l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
718 r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
719 lb_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
720 rb_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
721 ls_mag = powf(.5f * ( x + 1.f), s->sl_x) * powf(1.f - fabsf(y), s->sl_y) * mag_total;
722 rs_mag = powf(.5f * (-x + 1.f), s->sr_x) * powf(1.f - fabsf(y), s->sr_y) * mag_total;
724 dstl[2 * n ] = l_mag * cosf(l_phase);
725 dstl[2 * n + 1] = l_mag * sinf(l_phase);
727 dstr[2 * n ] = r_mag * cosf(r_phase);
728 dstr[2 * n + 1] = r_mag * sinf(r_phase);
730 dstc[2 * n ] = c_mag * cosf(c_phase);
731 dstc[2 * n + 1] = c_mag * sinf(c_phase);
733 dstlb[2 * n ] = lb_mag * cosf(l_phase);
734 dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
736 dstrb[2 * n ] = rb_mag * cosf(r_phase);
737 dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
739 dstls[2 * n ] = ls_mag * cosf(l_phase);
740 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
742 dstrs[2 * n ] = rs_mag * cosf(r_phase);
743 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
746 static void upmix_7_1(AVFilterContext *ctx,
754 float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
755 float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
756 AudioSurroundContext *s = ctx->priv;
758 dstl = (float *)s->output->extended_data[0];
759 dstr = (float *)s->output->extended_data[1];
760 dstc = (float *)s->output->extended_data[2];
761 dstlfe = (float *)s->output->extended_data[3];
762 dstlb = (float *)s->output->extended_data[4];
763 dstrb = (float *)s->output->extended_data[5];
764 dstls = (float *)s->output->extended_data[6];
765 dstrs = (float *)s->output->extended_data[7];
767 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
769 c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
770 l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
771 r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
772 lb_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
773 rb_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
774 ls_mag = powf(.5f * ( x + 1.f), s->sl_x) * powf(1.f - fabsf(y), s->sl_y) * mag_total;
775 rs_mag = powf(.5f * (-x + 1.f), s->sr_x) * powf(1.f - fabsf(y), s->sr_y) * mag_total;
777 dstl[2 * n ] = l_mag * cosf(l_phase);
778 dstl[2 * n + 1] = l_mag * sinf(l_phase);
780 dstr[2 * n ] = r_mag * cosf(r_phase);
781 dstr[2 * n + 1] = r_mag * sinf(r_phase);
783 dstc[2 * n ] = c_mag * cosf(c_phase);
784 dstc[2 * n + 1] = c_mag * sinf(c_phase);
786 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
787 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
789 dstlb[2 * n ] = lb_mag * cosf(l_phase);
790 dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
792 dstrb[2 * n ] = rb_mag * cosf(r_phase);
793 dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
795 dstls[2 * n ] = ls_mag * cosf(l_phase);
796 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
798 dstrs[2 * n ] = rs_mag * cosf(r_phase);
799 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
802 static void upmix_7_1_5_0_side(AVFilterContext *ctx,
803 float c_re, float c_im,
804 float mag_totall, float mag_totalr,
805 float fl_phase, float fr_phase,
806 float bl_phase, float br_phase,
807 float sl_phase, float sr_phase,
812 float fl_mag, fr_mag, ls_mag, rs_mag, lb_mag, rb_mag;
813 float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
814 float lfe_mag, c_phase, mag_total = (mag_totall + mag_totalr) * 0.5;
815 AudioSurroundContext *s = ctx->priv;
817 dstl = (float *)s->output->extended_data[0];
818 dstr = (float *)s->output->extended_data[1];
819 dstc = (float *)s->output->extended_data[2];
820 dstlfe = (float *)s->output->extended_data[3];
821 dstlb = (float *)s->output->extended_data[4];
822 dstrb = (float *)s->output->extended_data[5];
823 dstls = (float *)s->output->extended_data[6];
824 dstrs = (float *)s->output->extended_data[7];
826 c_phase = atan2f(c_im, c_re);
828 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
830 fl_mag = powf(.5f * (xl + 1.f), s->fl_x) * powf((yl + 1.f) * .5f, s->fl_y) * mag_totall;
831 fr_mag = powf(.5f * (xr + 1.f), s->fr_x) * powf((yr + 1.f) * .5f, s->fr_y) * mag_totalr;
832 lb_mag = powf(.5f * (-xl + 1.f), s->bl_x) * powf((yl + 1.f) * .5f, s->bl_y) * mag_totall;
833 rb_mag = powf(.5f * (-xr + 1.f), s->br_x) * powf((yr + 1.f) * .5f, s->br_y) * mag_totalr;
834 ls_mag = powf(1.f - fabsf(xl), s->sl_x) * powf((yl + 1.f) * .5f, s->sl_y) * mag_totall;
835 rs_mag = powf(1.f - fabsf(xr), s->sr_x) * powf((yr + 1.f) * .5f, s->sr_y) * mag_totalr;
837 dstl[2 * n ] = fl_mag * cosf(fl_phase);
838 dstl[2 * n + 1] = fl_mag * sinf(fl_phase);
840 dstr[2 * n ] = fr_mag * cosf(fr_phase);
841 dstr[2 * n + 1] = fr_mag * sinf(fr_phase);
844 dstc[2 * n + 1] = c_im;
846 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
847 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
849 dstlb[2 * n ] = lb_mag * cosf(bl_phase);
850 dstlb[2 * n + 1] = lb_mag * sinf(bl_phase);
852 dstrb[2 * n ] = rb_mag * cosf(br_phase);
853 dstrb[2 * n + 1] = rb_mag * sinf(br_phase);
855 dstls[2 * n ] = ls_mag * cosf(sl_phase);
856 dstls[2 * n + 1] = ls_mag * sinf(sl_phase);
858 dstrs[2 * n ] = rs_mag * cosf(sr_phase);
859 dstrs[2 * n + 1] = rs_mag * sinf(sr_phase);
862 static void upmix_7_1_5_1(AVFilterContext *ctx,
863 float c_re, float c_im,
864 float lfe_re, float lfe_im,
865 float mag_totall, float mag_totalr,
866 float fl_phase, float fr_phase,
867 float bl_phase, float br_phase,
868 float sl_phase, float sr_phase,
873 float fl_mag, fr_mag, ls_mag, rs_mag, lb_mag, rb_mag;
874 float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
875 AudioSurroundContext *s = ctx->priv;
877 dstl = (float *)s->output->extended_data[0];
878 dstr = (float *)s->output->extended_data[1];
879 dstc = (float *)s->output->extended_data[2];
880 dstlfe = (float *)s->output->extended_data[3];
881 dstlb = (float *)s->output->extended_data[4];
882 dstrb = (float *)s->output->extended_data[5];
883 dstls = (float *)s->output->extended_data[6];
884 dstrs = (float *)s->output->extended_data[7];
886 fl_mag = powf(.5f * (xl + 1.f), s->fl_x) * powf((yl + 1.f) * .5f, s->fl_y) * mag_totall;
887 fr_mag = powf(.5f * (xr + 1.f), s->fr_x) * powf((yr + 1.f) * .5f, s->fr_y) * mag_totalr;
888 lb_mag = powf(.5f * (-xl + 1.f), s->bl_x) * powf((yl + 1.f) * .5f, s->bl_y) * mag_totall;
889 rb_mag = powf(.5f * (-xr + 1.f), s->br_x) * powf((yr + 1.f) * .5f, s->br_y) * mag_totalr;
890 ls_mag = powf(1.f - fabsf(xl), s->sl_x) * powf((yl + 1.f) * .5f, s->sl_y) * mag_totall;
891 rs_mag = powf(1.f - fabsf(xr), s->sl_x) * powf((yr + 1.f) * .5f, s->sr_y) * mag_totalr;
893 dstl[2 * n ] = fl_mag * cosf(fl_phase);
894 dstl[2 * n + 1] = fl_mag * sinf(fl_phase);
896 dstr[2 * n ] = fr_mag * cosf(fr_phase);
897 dstr[2 * n + 1] = fr_mag * sinf(fr_phase);
900 dstc[2 * n + 1] = c_im;
902 dstlfe[2 * n ] = lfe_re;
903 dstlfe[2 * n + 1] = lfe_im;
905 dstlb[2 * n ] = lb_mag * cosf(bl_phase);
906 dstlb[2 * n + 1] = lb_mag * sinf(bl_phase);
908 dstrb[2 * n ] = rb_mag * cosf(br_phase);
909 dstrb[2 * n + 1] = rb_mag * sinf(br_phase);
911 dstls[2 * n ] = ls_mag * cosf(sl_phase);
912 dstls[2 * n + 1] = ls_mag * sinf(sl_phase);
914 dstrs[2 * n ] = rs_mag * cosf(sr_phase);
915 dstrs[2 * n + 1] = rs_mag * sinf(sr_phase);
918 static void filter_stereo(AVFilterContext *ctx)
920 AudioSurroundContext *s = ctx->priv;
924 srcl = (float *)s->input->extended_data[0];
925 srcr = (float *)s->input->extended_data[1];
927 for (n = 0; n < s->buf_size; n++) {
928 float l_re = srcl[2 * n], r_re = srcr[2 * n];
929 float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
930 float c_phase = atan2f(l_im + r_im, l_re + r_re);
931 float l_mag = hypotf(l_re, l_im);
932 float r_mag = hypotf(r_re, r_im);
933 float l_phase = atan2f(l_im, l_re);
934 float r_phase = atan2f(r_im, r_re);
935 float phase_dif = fabsf(l_phase - r_phase);
936 float mag_sum = l_mag + r_mag;
937 float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum;
938 float mag_total = hypotf(l_mag, r_mag);
941 if (phase_dif > M_PI)
942 phase_dif = 2 * M_PI - phase_dif;
944 stereo_position(mag_dif, phase_dif, &x, &y);
946 s->upmix_stereo(ctx, l_phase, r_phase, c_phase, mag_total, x, y, n);
950 static void filter_surround(AVFilterContext *ctx)
952 AudioSurroundContext *s = ctx->priv;
953 float *srcl, *srcr, *srcc;
956 srcl = (float *)s->input->extended_data[0];
957 srcr = (float *)s->input->extended_data[1];
958 srcc = (float *)s->input->extended_data[2];
960 for (n = 0; n < s->buf_size; n++) {
961 float l_re = srcl[2 * n], r_re = srcr[2 * n];
962 float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
963 float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
964 float c_mag = hypotf(c_re, c_im);
965 float c_phase = atan2f(c_im, c_re);
966 float l_mag = hypotf(l_re, l_im);
967 float r_mag = hypotf(r_re, r_im);
968 float l_phase = atan2f(l_im, l_re);
969 float r_phase = atan2f(r_im, r_re);
970 float phase_dif = fabsf(l_phase - r_phase);
971 float mag_sum = l_mag + r_mag;
972 float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum;
973 float mag_total = hypotf(l_mag, r_mag);
976 if (phase_dif > M_PI)
977 phase_dif = 2 * M_PI - phase_dif;
979 stereo_position(mag_dif, phase_dif, &x, &y);
981 s->upmix_3_0(ctx, l_phase, r_phase, c_phase, c_mag, mag_total, x, y, n);
985 static void filter_2_1(AVFilterContext *ctx)
987 AudioSurroundContext *s = ctx->priv;
988 float *srcl, *srcr, *srclfe;
991 srcl = (float *)s->input->extended_data[0];
992 srcr = (float *)s->input->extended_data[1];
993 srclfe = (float *)s->input->extended_data[2];
995 for (n = 0; n < s->buf_size; n++) {
996 float l_re = srcl[2 * n], r_re = srcr[2 * n];
997 float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
998 float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
999 float c_phase = atan2f(l_im + r_im, l_re + r_re);
1000 float l_mag = hypotf(l_re, l_im);
1001 float r_mag = hypotf(r_re, r_im);
1002 float l_phase = atan2f(l_im, l_re);
1003 float r_phase = atan2f(r_im, r_re);
1004 float phase_dif = fabsf(l_phase - r_phase);
1005 float mag_sum = l_mag + r_mag;
1006 float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum;
1007 float mag_total = hypotf(l_mag, r_mag);
1010 if (phase_dif > M_PI)
1011 phase_dif = 2 * M_PI - phase_dif;
1013 stereo_position(mag_dif, phase_dif, &x, &y);
1015 s->upmix_2_1(ctx, l_phase, r_phase, c_phase, mag_total, lfe_re, lfe_im, x, y, n);
1019 static void filter_5_0_side(AVFilterContext *ctx)
1021 AudioSurroundContext *s = ctx->priv;
1022 float *srcl, *srcr, *srcc, *srcsl, *srcsr;
1025 srcl = (float *)s->input->extended_data[0];
1026 srcr = (float *)s->input->extended_data[1];
1027 srcc = (float *)s->input->extended_data[2];
1028 srcsl = (float *)s->input->extended_data[3];
1029 srcsr = (float *)s->input->extended_data[4];
1031 for (n = 0; n < s->buf_size; n++) {
1032 float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
1033 float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
1034 float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
1035 float sl_re = srcsl[2 * n], sl_im = srcsl[2 * n + 1];
1036 float sr_re = srcsr[2 * n], sr_im = srcsr[2 * n + 1];
1037 float fl_mag = hypotf(fl_re, fl_im);
1038 float fr_mag = hypotf(fr_re, fr_im);
1039 float fl_phase = atan2f(fl_im, fl_re);
1040 float fr_phase = atan2f(fr_im, fr_re);
1041 float sl_mag = hypotf(sl_re, sl_im);
1042 float sr_mag = hypotf(sr_re, sr_im);
1043 float sl_phase = atan2f(sl_im, sl_re);
1044 float sr_phase = atan2f(sr_im, sr_re);
1045 float phase_difl = fabsf(fl_phase - sl_phase);
1046 float phase_difr = fabsf(fr_phase - sr_phase);
1047 float magl_sum = fl_mag + sl_mag;
1048 float magr_sum = fr_mag + sr_mag;
1049 float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - sl_mag) / magl_sum;
1050 float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - sr_mag) / magr_sum;
1051 float mag_totall = hypotf(fl_mag, sl_mag);
1052 float mag_totalr = hypotf(fr_mag, sr_mag);
1053 float bl_phase = atan2f(fl_im + sl_im, fl_re + sl_re);
1054 float br_phase = atan2f(fr_im + sr_im, fr_re + sr_re);
1058 if (phase_difl > M_PI)
1059 phase_difl = 2 * M_PI - phase_difl;
1061 if (phase_difr > M_PI)
1062 phase_difr = 2 * M_PI - phase_difr;
1064 stereo_position(mag_difl, phase_difl, &xl, &yl);
1065 stereo_position(mag_difr, phase_difr, &xr, &yr);
1067 s->upmix_5_0(ctx, c_re, c_im,
1068 mag_totall, mag_totalr,
1076 static void filter_5_1_side(AVFilterContext *ctx)
1078 AudioSurroundContext *s = ctx->priv;
1079 float *srcl, *srcr, *srcc, *srclfe, *srcsl, *srcsr;
1082 srcl = (float *)s->input->extended_data[0];
1083 srcr = (float *)s->input->extended_data[1];
1084 srcc = (float *)s->input->extended_data[2];
1085 srclfe = (float *)s->input->extended_data[3];
1086 srcsl = (float *)s->input->extended_data[4];
1087 srcsr = (float *)s->input->extended_data[5];
1089 for (n = 0; n < s->buf_size; n++) {
1090 float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
1091 float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
1092 float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
1093 float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
1094 float sl_re = srcsl[2 * n], sl_im = srcsl[2 * n + 1];
1095 float sr_re = srcsr[2 * n], sr_im = srcsr[2 * n + 1];
1096 float fl_mag = hypotf(fl_re, fl_im);
1097 float fr_mag = hypotf(fr_re, fr_im);
1098 float fl_phase = atan2f(fl_im, fl_re);
1099 float fr_phase = atan2f(fr_im, fr_re);
1100 float sl_mag = hypotf(sl_re, sl_im);
1101 float sr_mag = hypotf(sr_re, sr_im);
1102 float sl_phase = atan2f(sl_im, sl_re);
1103 float sr_phase = atan2f(sr_im, sr_re);
1104 float phase_difl = fabsf(fl_phase - sl_phase);
1105 float phase_difr = fabsf(fr_phase - sr_phase);
1106 float magl_sum = fl_mag + sl_mag;
1107 float magr_sum = fr_mag + sr_mag;
1108 float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - sl_mag) / magl_sum;
1109 float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - sr_mag) / magr_sum;
1110 float mag_totall = hypotf(fl_mag, sl_mag);
1111 float mag_totalr = hypotf(fr_mag, sr_mag);
1112 float bl_phase = atan2f(fl_im + sl_im, fl_re + sl_re);
1113 float br_phase = atan2f(fr_im + sr_im, fr_re + sr_re);
1117 if (phase_difl > M_PI)
1118 phase_difl = 2 * M_PI - phase_difl;
1120 if (phase_difr > M_PI)
1121 phase_difr = 2 * M_PI - phase_difr;
1123 stereo_position(mag_difl, phase_difl, &xl, &yl);
1124 stereo_position(mag_difr, phase_difr, &xr, &yr);
1126 s->upmix_5_1(ctx, c_re, c_im, lfe_re, lfe_im,
1127 mag_totall, mag_totalr,
1135 static void filter_5_1_back(AVFilterContext *ctx)
1137 AudioSurroundContext *s = ctx->priv;
1138 float *srcl, *srcr, *srcc, *srclfe, *srcbl, *srcbr;
1141 srcl = (float *)s->input->extended_data[0];
1142 srcr = (float *)s->input->extended_data[1];
1143 srcc = (float *)s->input->extended_data[2];
1144 srclfe = (float *)s->input->extended_data[3];
1145 srcbl = (float *)s->input->extended_data[4];
1146 srcbr = (float *)s->input->extended_data[5];
1148 for (n = 0; n < s->buf_size; n++) {
1149 float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
1150 float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
1151 float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
1152 float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
1153 float bl_re = srcbl[2 * n], bl_im = srcbl[2 * n + 1];
1154 float br_re = srcbr[2 * n], br_im = srcbr[2 * n + 1];
1155 float fl_mag = hypotf(fl_re, fl_im);
1156 float fr_mag = hypotf(fr_re, fr_im);
1157 float fl_phase = atan2f(fl_im, fl_re);
1158 float fr_phase = atan2f(fr_im, fr_re);
1159 float bl_mag = hypotf(bl_re, bl_im);
1160 float br_mag = hypotf(br_re, br_im);
1161 float bl_phase = atan2f(bl_im, bl_re);
1162 float br_phase = atan2f(br_im, br_re);
1163 float phase_difl = fabsf(fl_phase - bl_phase);
1164 float phase_difr = fabsf(fr_phase - br_phase);
1165 float magl_sum = fl_mag + bl_mag;
1166 float magr_sum = fr_mag + br_mag;
1167 float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - bl_mag) / magl_sum;
1168 float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - br_mag) / magr_sum;
1169 float mag_totall = hypotf(fl_mag, bl_mag);
1170 float mag_totalr = hypotf(fr_mag, br_mag);
1171 float sl_phase = atan2f(fl_im + bl_im, fl_re + bl_re);
1172 float sr_phase = atan2f(fr_im + br_im, fr_re + br_re);
1176 if (phase_difl > M_PI)
1177 phase_difl = 2 * M_PI - phase_difl;
1179 if (phase_difr > M_PI)
1180 phase_difr = 2 * M_PI - phase_difr;
1182 stereo_position(mag_difl, phase_difl, &xl, &yl);
1183 stereo_position(mag_difr, phase_difr, &xr, &yr);
1185 s->upmix_5_1(ctx, c_re, c_im, lfe_re, lfe_im,
1186 mag_totall, mag_totalr,
1194 static int init(AVFilterContext *ctx)
1196 AudioSurroundContext *s = ctx->priv;
1200 if (!(s->out_channel_layout = av_get_channel_layout(s->out_channel_layout_str))) {
1201 av_log(ctx, AV_LOG_ERROR, "Error parsing output channel layout '%s'.\n",
1202 s->out_channel_layout_str);
1203 return AVERROR(EINVAL);
1206 if (!(s->in_channel_layout = av_get_channel_layout(s->in_channel_layout_str))) {
1207 av_log(ctx, AV_LOG_ERROR, "Error parsing input channel layout '%s'.\n",
1208 s->in_channel_layout_str);
1209 return AVERROR(EINVAL);
1212 if (s->lowcutf >= s->highcutf) {
1213 av_log(ctx, AV_LOG_ERROR, "Low cut-off '%d' should be less than high cut-off '%d'.\n",
1214 s->lowcutf, s->highcutf);
1215 return AVERROR(EINVAL);
1218 switch (s->in_channel_layout) {
1219 case AV_CH_LAYOUT_STEREO:
1220 s->filter = filter_stereo;
1221 switch (s->out_channel_layout) {
1222 case AV_CH_LAYOUT_MONO:
1223 s->upmix_stereo = upmix_1_0;
1225 case AV_CH_LAYOUT_STEREO:
1226 s->upmix_stereo = upmix_stereo;
1228 case AV_CH_LAYOUT_2POINT1:
1229 s->upmix_stereo = upmix_2_1;
1231 case AV_CH_LAYOUT_SURROUND:
1232 s->upmix_stereo = upmix_3_0;
1234 case AV_CH_LAYOUT_3POINT1:
1235 s->upmix_stereo = upmix_3_1;
1237 case AV_CH_LAYOUT_4POINT0:
1238 s->upmix_stereo = upmix_4_0;
1240 case AV_CH_LAYOUT_4POINT1:
1241 s->upmix_stereo = upmix_4_1;
1243 case AV_CH_LAYOUT_5POINT0_BACK:
1244 s->upmix_stereo = upmix_5_0_back;
1246 case AV_CH_LAYOUT_5POINT1_BACK:
1247 s->upmix_stereo = upmix_5_1_back;
1249 case AV_CH_LAYOUT_7POINT0:
1250 s->upmix_stereo = upmix_7_0;
1252 case AV_CH_LAYOUT_7POINT1:
1253 s->upmix_stereo = upmix_7_1;
1259 case AV_CH_LAYOUT_2POINT1:
1260 s->filter = filter_2_1;
1261 switch (s->out_channel_layout) {
1262 case AV_CH_LAYOUT_5POINT1_BACK:
1263 s->upmix_2_1 = upmix_5_1_back_2_1;
1269 case AV_CH_LAYOUT_SURROUND:
1270 s->filter = filter_surround;
1271 switch (s->out_channel_layout) {
1272 case AV_CH_LAYOUT_3POINT1:
1273 s->upmix_3_0 = upmix_3_1_surround;
1275 case AV_CH_LAYOUT_5POINT1_BACK:
1276 s->upmix_3_0 = upmix_5_1_back_surround;
1282 case AV_CH_LAYOUT_5POINT0:
1283 s->filter = filter_5_0_side;
1284 switch (s->out_channel_layout) {
1285 case AV_CH_LAYOUT_7POINT1:
1286 s->upmix_5_0 = upmix_7_1_5_0_side;
1292 case AV_CH_LAYOUT_5POINT1:
1293 s->filter = filter_5_1_side;
1294 switch (s->out_channel_layout) {
1295 case AV_CH_LAYOUT_7POINT1:
1296 s->upmix_5_1 = upmix_7_1_5_1;
1302 case AV_CH_LAYOUT_5POINT1_BACK:
1303 s->filter = filter_5_1_back;
1304 switch (s->out_channel_layout) {
1305 case AV_CH_LAYOUT_7POINT1:
1306 s->upmix_5_1 = upmix_7_1_5_1;
1314 av_log(ctx, AV_LOG_ERROR, "Unsupported upmix: '%s' -> '%s'.\n",
1315 s->in_channel_layout_str, s->out_channel_layout_str);
1316 return AVERROR(EINVAL);
1320 s->pts = AV_NOPTS_VALUE;
1322 s->window_func_lut = av_calloc(s->buf_size, sizeof(*s->window_func_lut));
1323 if (!s->window_func_lut)
1324 return AVERROR(ENOMEM);
1326 generate_window_func(s->window_func_lut, s->buf_size, s->win_func, &overlap);
1327 if (s->overlap == 1)
1328 s->overlap = overlap;
1330 for (i = 0; i < s->buf_size; i++)
1331 s->window_func_lut[i] = sqrtf(s->window_func_lut[i] / s->buf_size);
1332 s->hop_size = s->buf_size * (1. - s->overlap);
1333 if (s->hop_size <= 0)
1334 return AVERROR(EINVAL);
1336 if (s->all_x >= 0.f)
1337 s->fc_x = s->fl_x = s->fr_x = s->bc_x = s->sl_x = s->sr_x = s->bl_x = s->br_x = s->all_x;
1338 if (s->all_y >= 0.f)
1339 s->fc_y = s->fl_y = s->fr_y = s->bc_y = s->sl_y = s->sr_y = s->bl_y = s->br_y = s->all_y;
1344 static int fft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
1346 AudioSurroundContext *s = ctx->priv;
1347 const float level_in = s->input_levels[ch];
1351 memset(s->input->extended_data[ch] + s->buf_size * sizeof(float), 0, s->buf_size * sizeof(float));
1353 dst = (float *)s->input->extended_data[ch];
1354 for (n = 0; n < s->buf_size; n++) {
1355 dst[n] *= s->window_func_lut[n] * level_in;
1358 av_rdft_calc(s->rdft[ch], (float *)s->input->extended_data[ch]);
1363 static int ifft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
1365 AudioSurroundContext *s = ctx->priv;
1366 const float level_out = s->output_levels[ch];
1371 av_rdft_calc(s->irdft[ch], (float *)s->output->extended_data[ch]);
1373 dst = (float *)s->output->extended_data[ch];
1374 ptr = (float *)s->overlap_buffer->extended_data[ch];
1376 memmove(s->overlap_buffer->extended_data[ch],
1377 s->overlap_buffer->extended_data[ch] + s->hop_size * sizeof(float),
1378 s->buf_size * sizeof(float));
1379 memset(s->overlap_buffer->extended_data[ch] + s->buf_size * sizeof(float),
1380 0, s->hop_size * sizeof(float));
1382 for (n = 0; n < s->buf_size; n++) {
1383 ptr[n] += dst[n] * s->window_func_lut[n] * level_out;
1386 ptr = (float *)s->overlap_buffer->extended_data[ch];
1387 dst = (float *)out->extended_data[ch];
1388 memcpy(dst, ptr, s->hop_size * sizeof(float));
1393 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
1395 AVFilterContext *ctx = inlink->dst;
1396 AVFilterLink *outlink = ctx->outputs[0];
1397 AudioSurroundContext *s = ctx->priv;
1400 ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
1402 if (ret >= 0 && s->pts == AV_NOPTS_VALUE)
1409 while (av_audio_fifo_size(s->fifo) >= s->buf_size) {
1412 ret = av_audio_fifo_peek(s->fifo, (void **)s->input->extended_data, s->buf_size);
1416 ctx->internal->execute(ctx, fft_channel, NULL, NULL, inlink->channels);
1420 out = ff_get_audio_buffer(outlink, s->hop_size);
1422 return AVERROR(ENOMEM);
1424 ctx->internal->execute(ctx, ifft_channel, out, NULL, outlink->channels);
1427 if (s->pts != AV_NOPTS_VALUE)
1428 s->pts += av_rescale_q(out->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
1429 av_audio_fifo_drain(s->fifo, s->hop_size);
1430 ret = ff_filter_frame(outlink, out);
1438 static int request_frame(AVFilterLink *outlink)
1440 AVFilterContext *ctx = outlink->src;
1441 AudioSurroundContext *s = ctx->priv;
1444 ret = ff_request_frame(ctx->inputs[0]);
1446 if (ret == AVERROR_EOF && av_audio_fifo_size(s->fifo) > 0 && av_audio_fifo_size(s->fifo) < s->buf_size) {
1449 in = ff_get_audio_buffer(outlink, s->buf_size - av_audio_fifo_size(s->fifo));
1451 return AVERROR(ENOMEM);
1452 ret = filter_frame(ctx->inputs[0], in);
1453 av_audio_fifo_drain(s->fifo, s->buf_size);
1459 static av_cold void uninit(AVFilterContext *ctx)
1461 AudioSurroundContext *s = ctx->priv;
1464 av_frame_free(&s->input);
1465 av_frame_free(&s->output);
1466 av_frame_free(&s->overlap_buffer);
1468 for (ch = 0; ch < s->nb_in_channels; ch++) {
1469 av_rdft_end(s->rdft[ch]);
1471 for (ch = 0; ch < s->nb_out_channels; ch++) {
1472 av_rdft_end(s->irdft[ch]);
1474 av_freep(&s->input_levels);
1475 av_freep(&s->output_levels);
1477 av_freep(&s->irdft);
1478 av_audio_fifo_free(s->fifo);
1479 av_freep(&s->window_func_lut);
1482 #define OFFSET(x) offsetof(AudioSurroundContext, x)
1483 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1485 static const AVOption surround_options[] = {
1486 { "chl_out", "set output channel layout", OFFSET(out_channel_layout_str), AV_OPT_TYPE_STRING, {.str="5.1"}, 0, 0, FLAGS },
1487 { "chl_in", "set input channel layout", OFFSET(in_channel_layout_str), AV_OPT_TYPE_STRING, {.str="stereo"},0, 0, FLAGS },
1488 { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1489 { "level_out", "set output level", OFFSET(level_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1490 { "lfe", "output LFE", OFFSET(output_lfe), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
1491 { "lfe_low", "LFE low cut off", OFFSET(lowcutf), AV_OPT_TYPE_INT, {.i64=128}, 0, 256, FLAGS },
1492 { "lfe_high", "LFE high cut off", OFFSET(highcutf), AV_OPT_TYPE_INT, {.i64=256}, 0, 512, FLAGS },
1493 { "lfe_mode", "set LFE channel mode", OFFSET(lfe_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "lfe_mode" },
1494 { "add", "just add LFE channel", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 1, FLAGS, "lfe_mode" },
1495 { "sub", "substract LFE channel with others", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 1, FLAGS, "lfe_mode" },
1496 { "fc_in", "set front center channel input level", OFFSET(fc_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1497 { "fc_out", "set front center channel output level", OFFSET(fc_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1498 { "lfe_in", "set lfe channel input level", OFFSET(lfe_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1499 { "lfe_out", "set lfe channel output level", OFFSET(lfe_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1500 { "allx", "set all channel's x spread", OFFSET(all_x), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 15, FLAGS },
1501 { "ally", "set all channel's y spread", OFFSET(all_y), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 15, FLAGS },
1502 { "fcx", "set front center channel x spread", OFFSET(fc_x), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
1503 { "flx", "set front left channel x spread", OFFSET(fl_x), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
1504 { "frx", "set front right channel x spread", OFFSET(fr_x), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
1505 { "blx", "set back left channel x spread", OFFSET(bl_x), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
1506 { "brx", "set back right channel x spread", OFFSET(br_x), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
1507 { "slx", "set side left channel x spread", OFFSET(sl_x), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
1508 { "srx", "set side right channel x spread", OFFSET(sr_x), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
1509 { "bcx", "set back center channel x spread", OFFSET(bc_x), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
1510 { "fcy", "set front center channel y spread", OFFSET(fc_y), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
1511 { "fly", "set front left channel y spread", OFFSET(fl_y), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
1512 { "fry", "set front right channel y spread", OFFSET(fr_y), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
1513 { "bly", "set back left channel y spread", OFFSET(bl_y), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
1514 { "bry", "set back right channel y spread", OFFSET(br_y), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
1515 { "sly", "set side left channel y spread", OFFSET(sl_y), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
1516 { "sry", "set side right channel y spread", OFFSET(sr_y), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
1517 { "bcy", "set back center channel y spread", OFFSET(bc_y), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
1518 { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
1519 { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
1520 { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
1521 { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
1522 { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
1523 { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
1524 { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
1525 { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
1526 { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
1527 { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
1528 { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
1529 { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
1530 { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
1531 { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
1532 { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
1533 { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
1534 { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
1535 { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
1536 { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
1537 { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
1538 { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
1539 { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
1540 { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
1544 AVFILTER_DEFINE_CLASS(surround);
1546 static const AVFilterPad inputs[] = {
1549 .type = AVMEDIA_TYPE_AUDIO,
1550 .filter_frame = filter_frame,
1551 .config_props = config_input,
1556 static const AVFilterPad outputs[] = {
1559 .type = AVMEDIA_TYPE_AUDIO,
1560 .request_frame = request_frame,
1561 .config_props = config_output,
1566 AVFilter ff_af_surround = {
1568 .description = NULL_IF_CONFIG_SMALL("Apply audio surround upmix filter."),
1569 .query_formats = query_formats,
1570 .priv_size = sizeof(AudioSurroundContext),
1571 .priv_class = &surround_class,
1576 .flags = AVFILTER_FLAG_SLICE_THREADS,