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;
55 uint64_t out_channel_layout;
56 uint64_t in_channel_layout;
62 AVFrame *overlap_buffer;
67 RDFTContext **rdft, **irdft;
68 float *window_func_lut;
72 void (*filter)(AVFilterContext *ctx);
73 void (*upmix_stereo)(AVFilterContext *ctx,
80 void (*upmix_2_1)(AVFilterContext *ctx,
89 void (*upmix_3_0)(AVFilterContext *ctx,
97 void (*upmix_5_0)(AVFilterContext *ctx,
98 float c_re, float c_im,
99 float mag_totall, float mag_totalr,
100 float fl_phase, float fr_phase,
101 float bl_phase, float br_phase,
102 float sl_phase, float sr_phase,
106 void (*upmix_5_1)(AVFilterContext *ctx,
107 float c_re, float c_im,
108 float lfe_re, float lfe_im,
109 float mag_totall, float mag_totalr,
110 float fl_phase, float fr_phase,
111 float bl_phase, float br_phase,
112 float sl_phase, float sr_phase,
116 } AudioSurroundContext;
118 static int query_formats(AVFilterContext *ctx)
120 AudioSurroundContext *s = ctx->priv;
121 AVFilterFormats *formats = NULL;
122 AVFilterChannelLayouts *layouts = NULL;
125 ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLTP);
128 ret = ff_set_common_formats(ctx, formats);
133 ret = ff_add_channel_layout(&layouts, s->out_channel_layout);
137 ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
142 ret = ff_add_channel_layout(&layouts, s->in_channel_layout);
146 ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts);
150 formats = ff_all_samplerates();
152 return AVERROR(ENOMEM);
153 return ff_set_common_samplerates(ctx, formats);
156 static int config_input(AVFilterLink *inlink)
158 AVFilterContext *ctx = inlink->dst;
159 AudioSurroundContext *s = ctx->priv;
162 s->rdft = av_calloc(inlink->channels, sizeof(*s->rdft));
164 return AVERROR(ENOMEM);
166 for (ch = 0; ch < inlink->channels; ch++) {
167 s->rdft[ch] = av_rdft_init(ff_log2(s->buf_size), DFT_R2C);
169 return AVERROR(ENOMEM);
171 s->nb_in_channels = inlink->channels;
172 s->input_levels = av_malloc_array(s->nb_in_channels, sizeof(*s->input_levels));
173 if (!s->input_levels)
174 return AVERROR(ENOMEM);
175 for (ch = 0; ch < s->nb_in_channels; ch++)
176 s->input_levels[ch] = s->level_in;
177 ch = av_get_channel_layout_channel_index(inlink->channel_layout, AV_CH_FRONT_CENTER);
179 s->input_levels[ch] *= s->fc_in;
180 ch = av_get_channel_layout_channel_index(inlink->channel_layout, AV_CH_LOW_FREQUENCY);
182 s->input_levels[ch] *= s->lfe_in;
184 s->input = ff_get_audio_buffer(inlink, s->buf_size * 2);
186 return AVERROR(ENOMEM);
188 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->buf_size);
190 return AVERROR(ENOMEM);
192 s->lowcut = 1.f * s->lowcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
193 s->highcut = 1.f * s->highcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
198 static int config_output(AVFilterLink *outlink)
200 AVFilterContext *ctx = outlink->src;
201 AudioSurroundContext *s = ctx->priv;
204 s->irdft = av_calloc(outlink->channels, sizeof(*s->irdft));
206 return AVERROR(ENOMEM);
208 for (ch = 0; ch < outlink->channels; ch++) {
209 s->irdft[ch] = av_rdft_init(ff_log2(s->buf_size), IDFT_C2R);
211 return AVERROR(ENOMEM);
213 s->nb_out_channels = outlink->channels;
214 s->output_levels = av_malloc_array(s->nb_out_channels, sizeof(*s->output_levels));
215 if (!s->output_levels)
216 return AVERROR(ENOMEM);
217 for (ch = 0; ch < s->nb_out_channels; ch++)
218 s->output_levels[ch] = s->level_out;
219 ch = av_get_channel_layout_channel_index(outlink->channel_layout, AV_CH_FRONT_CENTER);
221 s->output_levels[ch] *= s->fc_out;
222 ch = av_get_channel_layout_channel_index(outlink->channel_layout, AV_CH_LOW_FREQUENCY);
224 s->output_levels[ch] *= s->lfe_out;
226 s->output = ff_get_audio_buffer(outlink, s->buf_size * 2);
227 s->overlap_buffer = ff_get_audio_buffer(outlink, s->buf_size * 2);
228 if (!s->overlap_buffer || !s->output)
229 return AVERROR(ENOMEM);
234 static void stereo_position(float a, float p, float *x, float *y)
236 *x = av_clipf(a+FFMAX(0, sinf(p-M_PI_2))*FFDIFFSIGN(a,0), -1, 1);
237 *y = av_clipf(cosf(a*M_PI_2+M_PI)*cosf(M_PI_2-p/M_PI)*M_LN10+1, -1, 1);
240 static inline void get_lfe(int output_lfe, int n, float lowcut, float highcut,
241 float *lfe_mag, float *mag_total, int lfe_mode)
243 if (output_lfe && n < highcut) {
244 *lfe_mag = n < lowcut ? 1.f : .5f*(1.f+cosf(M_PI*(lowcut-n)/(lowcut-highcut)));
245 *lfe_mag *= *mag_total;
247 *mag_total -= *lfe_mag;
253 static void upmix_1_0(AVFilterContext *ctx,
261 AudioSurroundContext *s = ctx->priv;
264 dst = (float *)s->output->extended_data[0];
266 mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
268 dst[2 * n ] = mag * cosf(c_phase);
269 dst[2 * n + 1] = mag * sinf(c_phase);
272 static void upmix_stereo(AVFilterContext *ctx,
280 AudioSurroundContext *s = ctx->priv;
281 float l_mag, r_mag, *dstl, *dstr;
283 dstl = (float *)s->output->extended_data[0];
284 dstr = (float *)s->output->extended_data[1];
286 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
287 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
289 dstl[2 * n ] = l_mag * cosf(l_phase);
290 dstl[2 * n + 1] = l_mag * sinf(l_phase);
292 dstr[2 * n ] = r_mag * cosf(r_phase);
293 dstr[2 * n + 1] = r_mag * sinf(r_phase);
296 static void upmix_2_1(AVFilterContext *ctx,
304 AudioSurroundContext *s = ctx->priv;
305 float lfe_mag, l_mag, r_mag, *dstl, *dstr, *dstlfe;
307 dstl = (float *)s->output->extended_data[0];
308 dstr = (float *)s->output->extended_data[1];
309 dstlfe = (float *)s->output->extended_data[2];
311 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
313 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
314 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
316 dstl[2 * n ] = l_mag * cosf(l_phase);
317 dstl[2 * n + 1] = l_mag * sinf(l_phase);
319 dstr[2 * n ] = r_mag * cosf(r_phase);
320 dstr[2 * n + 1] = r_mag * sinf(r_phase);
322 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
323 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
326 static void upmix_3_0(AVFilterContext *ctx,
334 AudioSurroundContext *s = ctx->priv;
335 float l_mag, r_mag, c_mag, *dstc, *dstl, *dstr;
337 dstl = (float *)s->output->extended_data[0];
338 dstr = (float *)s->output->extended_data[1];
339 dstc = (float *)s->output->extended_data[2];
341 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
342 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
343 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
345 dstl[2 * n ] = l_mag * cosf(l_phase);
346 dstl[2 * n + 1] = l_mag * sinf(l_phase);
348 dstr[2 * n ] = r_mag * cosf(r_phase);
349 dstr[2 * n + 1] = r_mag * sinf(r_phase);
351 dstc[2 * n ] = c_mag * cosf(c_phase);
352 dstc[2 * n + 1] = c_mag * sinf(c_phase);
355 static void upmix_3_1(AVFilterContext *ctx,
363 AudioSurroundContext *s = ctx->priv;
364 float lfe_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstlfe;
366 dstl = (float *)s->output->extended_data[0];
367 dstr = (float *)s->output->extended_data[1];
368 dstc = (float *)s->output->extended_data[2];
369 dstlfe = (float *)s->output->extended_data[3];
371 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
373 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
374 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
375 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
377 dstl[2 * n ] = l_mag * cosf(l_phase);
378 dstl[2 * n + 1] = l_mag * sinf(l_phase);
380 dstr[2 * n ] = r_mag * cosf(r_phase);
381 dstr[2 * n + 1] = r_mag * sinf(r_phase);
383 dstc[2 * n ] = c_mag * cosf(c_phase);
384 dstc[2 * n + 1] = c_mag * sinf(c_phase);
386 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
387 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
390 static void upmix_3_1_surround(AVFilterContext *ctx,
399 AudioSurroundContext *s = ctx->priv;
400 float lfe_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
402 dstl = (float *)s->output->extended_data[0];
403 dstr = (float *)s->output->extended_data[1];
404 dstc = (float *)s->output->extended_data[2];
405 dstlfe = (float *)s->output->extended_data[3];
407 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &c_mag, s->lfe_mode);
409 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
410 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
412 dstl[2 * n ] = l_mag * cosf(l_phase);
413 dstl[2 * n + 1] = l_mag * sinf(l_phase);
415 dstr[2 * n ] = r_mag * cosf(r_phase);
416 dstr[2 * n + 1] = r_mag * sinf(r_phase);
418 dstc[2 * n ] = c_mag * cosf(c_phase);
419 dstc[2 * n + 1] = c_mag * sinf(c_phase);
421 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
422 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
425 static void upmix_4_0(AVFilterContext *ctx,
433 float b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb;
434 AudioSurroundContext *s = ctx->priv;
436 dstl = (float *)s->output->extended_data[0];
437 dstr = (float *)s->output->extended_data[1];
438 dstc = (float *)s->output->extended_data[2];
439 dstb = (float *)s->output->extended_data[3];
441 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
442 b_mag = sqrtf(1.f - fabsf(x)) * ((1.f - y) * .5f) * mag_total;
443 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
444 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
446 dstl[2 * n ] = l_mag * cosf(l_phase);
447 dstl[2 * n + 1] = l_mag * sinf(l_phase);
449 dstr[2 * n ] = r_mag * cosf(r_phase);
450 dstr[2 * n + 1] = r_mag * sinf(r_phase);
452 dstc[2 * n ] = c_mag * cosf(c_phase);
453 dstc[2 * n + 1] = c_mag * sinf(c_phase);
455 dstb[2 * n ] = b_mag * cosf(c_phase);
456 dstb[2 * n + 1] = b_mag * sinf(c_phase);
459 static void upmix_4_1(AVFilterContext *ctx,
467 float lfe_mag, b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb, *dstlfe;
468 AudioSurroundContext *s = ctx->priv;
470 dstl = (float *)s->output->extended_data[0];
471 dstr = (float *)s->output->extended_data[1];
472 dstc = (float *)s->output->extended_data[2];
473 dstlfe = (float *)s->output->extended_data[3];
474 dstb = (float *)s->output->extended_data[4];
476 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
478 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
479 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
481 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
482 b_mag = sqrtf(1.f - fabsf(x)) * ((1.f - y) * .5f) * mag_total;
483 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
484 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
486 dstl[2 * n ] = l_mag * cosf(l_phase);
487 dstl[2 * n + 1] = l_mag * sinf(l_phase);
489 dstr[2 * n ] = r_mag * cosf(r_phase);
490 dstr[2 * n + 1] = r_mag * sinf(r_phase);
492 dstc[2 * n ] = c_mag * cosf(c_phase);
493 dstc[2 * n + 1] = c_mag * sinf(c_phase);
495 dstb[2 * n ] = b_mag * cosf(c_phase);
496 dstb[2 * n + 1] = b_mag * sinf(c_phase);
499 static void upmix_5_0_back(AVFilterContext *ctx,
507 float l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs;
508 AudioSurroundContext *s = ctx->priv;
510 dstl = (float *)s->output->extended_data[0];
511 dstr = (float *)s->output->extended_data[1];
512 dstc = (float *)s->output->extended_data[2];
513 dstls = (float *)s->output->extended_data[3];
514 dstrs = (float *)s->output->extended_data[4];
516 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
517 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
518 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
519 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
520 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
522 dstl[2 * n ] = l_mag * cosf(l_phase);
523 dstl[2 * n + 1] = l_mag * sinf(l_phase);
525 dstr[2 * n ] = r_mag * cosf(r_phase);
526 dstr[2 * n + 1] = r_mag * sinf(r_phase);
528 dstc[2 * n ] = c_mag * cosf(c_phase);
529 dstc[2 * n + 1] = c_mag * sinf(c_phase);
531 dstls[2 * n ] = ls_mag * cosf(l_phase);
532 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
534 dstrs[2 * n ] = rs_mag * cosf(r_phase);
535 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
538 static void upmix_5_1_back(AVFilterContext *ctx,
546 float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlfe;
547 AudioSurroundContext *s = ctx->priv;
549 dstl = (float *)s->output->extended_data[0];
550 dstr = (float *)s->output->extended_data[1];
551 dstc = (float *)s->output->extended_data[2];
552 dstlfe = (float *)s->output->extended_data[3];
553 dstls = (float *)s->output->extended_data[4];
554 dstrs = (float *)s->output->extended_data[5];
556 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
558 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
559 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
560 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
561 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
562 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
564 dstl[2 * n ] = l_mag * cosf(l_phase);
565 dstl[2 * n + 1] = l_mag * sinf(l_phase);
567 dstr[2 * n ] = r_mag * cosf(r_phase);
568 dstr[2 * n + 1] = r_mag * sinf(r_phase);
570 dstc[2 * n ] = c_mag * cosf(c_phase);
571 dstc[2 * n + 1] = c_mag * sinf(c_phase);
573 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
574 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
576 dstls[2 * n ] = ls_mag * cosf(l_phase);
577 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
579 dstrs[2 * n ] = rs_mag * cosf(r_phase);
580 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
583 static void upmix_5_1_back_surround(AVFilterContext *ctx,
592 AudioSurroundContext *s = ctx->priv;
593 float lfe_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
594 float ls_mag, rs_mag, *dstls, *dstrs;
596 dstl = (float *)s->output->extended_data[0];
597 dstr = (float *)s->output->extended_data[1];
598 dstc = (float *)s->output->extended_data[2];
599 dstlfe = (float *)s->output->extended_data[3];
600 dstls = (float *)s->output->extended_data[4];
601 dstrs = (float *)s->output->extended_data[5];
603 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &c_mag, s->lfe_mode);
605 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
606 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
607 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
608 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
610 dstl[2 * n ] = l_mag * cosf(l_phase);
611 dstl[2 * n + 1] = l_mag * sinf(l_phase);
613 dstr[2 * n ] = r_mag * cosf(r_phase);
614 dstr[2 * n + 1] = r_mag * sinf(r_phase);
616 dstc[2 * n ] = c_mag * cosf(c_phase);
617 dstc[2 * n + 1] = c_mag * sinf(c_phase);
619 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
620 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
622 dstls[2 * n ] = ls_mag * cosf(l_phase);
623 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
625 dstrs[2 * n ] = rs_mag * cosf(r_phase);
626 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
629 static void upmix_5_1_back_2_1(AVFilterContext *ctx,
639 AudioSurroundContext *s = ctx->priv;
640 float c_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
641 float ls_mag, rs_mag, *dstls, *dstrs;
643 dstl = (float *)s->output->extended_data[0];
644 dstr = (float *)s->output->extended_data[1];
645 dstc = (float *)s->output->extended_data[2];
646 dstlfe = (float *)s->output->extended_data[3];
647 dstls = (float *)s->output->extended_data[4];
648 dstrs = (float *)s->output->extended_data[5];
650 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
651 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
652 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
653 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
654 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
656 dstl[2 * n ] = l_mag * cosf(l_phase);
657 dstl[2 * n + 1] = l_mag * sinf(l_phase);
659 dstr[2 * n ] = r_mag * cosf(r_phase);
660 dstr[2 * n + 1] = r_mag * sinf(r_phase);
662 dstc[2 * n ] = c_mag * cosf(c_phase);
663 dstc[2 * n + 1] = c_mag * sinf(c_phase);
665 dstlfe[2 * n ] = lfe_re;
666 dstlfe[2 * n + 1] = lfe_im;
668 dstls[2 * n ] = ls_mag * cosf(l_phase);
669 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
671 dstrs[2 * n ] = rs_mag * cosf(r_phase);
672 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
675 static void upmix_7_0(AVFilterContext *ctx,
683 float l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
684 float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb;
685 AudioSurroundContext *s = ctx->priv;
687 dstl = (float *)s->output->extended_data[0];
688 dstr = (float *)s->output->extended_data[1];
689 dstc = (float *)s->output->extended_data[2];
690 dstlb = (float *)s->output->extended_data[3];
691 dstrb = (float *)s->output->extended_data[4];
692 dstls = (float *)s->output->extended_data[5];
693 dstrs = (float *)s->output->extended_data[6];
695 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
696 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
697 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
698 lb_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
699 rb_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
700 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - fabsf(y)) * mag_total;
701 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - fabsf(y)) * mag_total;
703 dstl[2 * n ] = l_mag * cosf(l_phase);
704 dstl[2 * n + 1] = l_mag * sinf(l_phase);
706 dstr[2 * n ] = r_mag * cosf(r_phase);
707 dstr[2 * n + 1] = r_mag * sinf(r_phase);
709 dstc[2 * n ] = c_mag * cosf(c_phase);
710 dstc[2 * n + 1] = c_mag * sinf(c_phase);
712 dstlb[2 * n ] = lb_mag * cosf(l_phase);
713 dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
715 dstrb[2 * n ] = rb_mag * cosf(r_phase);
716 dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
718 dstls[2 * n ] = ls_mag * cosf(l_phase);
719 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
721 dstrs[2 * n ] = rs_mag * cosf(r_phase);
722 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
725 static void upmix_7_1(AVFilterContext *ctx,
733 float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
734 float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
735 AudioSurroundContext *s = ctx->priv;
737 dstl = (float *)s->output->extended_data[0];
738 dstr = (float *)s->output->extended_data[1];
739 dstc = (float *)s->output->extended_data[2];
740 dstlfe = (float *)s->output->extended_data[3];
741 dstlb = (float *)s->output->extended_data[4];
742 dstrb = (float *)s->output->extended_data[5];
743 dstls = (float *)s->output->extended_data[6];
744 dstrs = (float *)s->output->extended_data[7];
746 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
748 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
749 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
750 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
751 lb_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
752 rb_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
753 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - fabsf(y)) * mag_total;
754 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - fabsf(y)) * mag_total;
756 dstl[2 * n ] = l_mag * cosf(l_phase);
757 dstl[2 * n + 1] = l_mag * sinf(l_phase);
759 dstr[2 * n ] = r_mag * cosf(r_phase);
760 dstr[2 * n + 1] = r_mag * sinf(r_phase);
762 dstc[2 * n ] = c_mag * cosf(c_phase);
763 dstc[2 * n + 1] = c_mag * sinf(c_phase);
765 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
766 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
768 dstlb[2 * n ] = lb_mag * cosf(l_phase);
769 dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
771 dstrb[2 * n ] = rb_mag * cosf(r_phase);
772 dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
774 dstls[2 * n ] = ls_mag * cosf(l_phase);
775 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
777 dstrs[2 * n ] = rs_mag * cosf(r_phase);
778 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
781 static void upmix_7_1_5_0_side(AVFilterContext *ctx,
782 float c_re, float c_im,
783 float mag_totall, float mag_totalr,
784 float fl_phase, float fr_phase,
785 float bl_phase, float br_phase,
786 float sl_phase, float sr_phase,
791 float fl_mag, fr_mag, ls_mag, rs_mag, lb_mag, rb_mag;
792 float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
793 float lfe_mag, c_phase, mag_total = (mag_totall + mag_totalr) * 0.5;
794 AudioSurroundContext *s = ctx->priv;
796 dstl = (float *)s->output->extended_data[0];
797 dstr = (float *)s->output->extended_data[1];
798 dstc = (float *)s->output->extended_data[2];
799 dstlfe = (float *)s->output->extended_data[3];
800 dstlb = (float *)s->output->extended_data[4];
801 dstrb = (float *)s->output->extended_data[5];
802 dstls = (float *)s->output->extended_data[6];
803 dstrs = (float *)s->output->extended_data[7];
805 c_phase = atan2f(c_im, c_re);
807 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
809 fl_mag = sqrtf(.5f * (xl + 1.f)) * ((yl + 1.f) * .5f) * mag_totall;
810 fr_mag = sqrtf(.5f * (xr + 1.f)) * ((yr + 1.f) * .5f) * mag_totalr;
811 lb_mag = sqrtf(.5f * (-xl + 1.f)) * ((yl + 1.f) * .5f) * mag_totall;
812 rb_mag = sqrtf(.5f * (-xr + 1.f)) * ((yr + 1.f) * .5f) * mag_totalr;
813 ls_mag = sqrtf(1.f - fabsf(xl)) * ((yl + 1.f) * .5f) * mag_totall;
814 rs_mag = sqrtf(1.f - fabsf(xr)) * ((yr + 1.f) * .5f) * mag_totalr;
816 dstl[2 * n ] = fl_mag * cosf(fl_phase);
817 dstl[2 * n + 1] = fl_mag * sinf(fl_phase);
819 dstr[2 * n ] = fr_mag * cosf(fr_phase);
820 dstr[2 * n + 1] = fr_mag * sinf(fr_phase);
823 dstc[2 * n + 1] = c_im;
825 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
826 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
828 dstlb[2 * n ] = lb_mag * cosf(bl_phase);
829 dstlb[2 * n + 1] = lb_mag * sinf(bl_phase);
831 dstrb[2 * n ] = rb_mag * cosf(br_phase);
832 dstrb[2 * n + 1] = rb_mag * sinf(br_phase);
834 dstls[2 * n ] = ls_mag * cosf(sl_phase);
835 dstls[2 * n + 1] = ls_mag * sinf(sl_phase);
837 dstrs[2 * n ] = rs_mag * cosf(sr_phase);
838 dstrs[2 * n + 1] = rs_mag * sinf(sr_phase);
841 static void upmix_7_1_5_1(AVFilterContext *ctx,
842 float c_re, float c_im,
843 float lfe_re, float lfe_im,
844 float mag_totall, float mag_totalr,
845 float fl_phase, float fr_phase,
846 float bl_phase, float br_phase,
847 float sl_phase, float sr_phase,
852 float fl_mag, fr_mag, ls_mag, rs_mag, lb_mag, rb_mag;
853 float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
854 AudioSurroundContext *s = ctx->priv;
856 dstl = (float *)s->output->extended_data[0];
857 dstr = (float *)s->output->extended_data[1];
858 dstc = (float *)s->output->extended_data[2];
859 dstlfe = (float *)s->output->extended_data[3];
860 dstlb = (float *)s->output->extended_data[4];
861 dstrb = (float *)s->output->extended_data[5];
862 dstls = (float *)s->output->extended_data[6];
863 dstrs = (float *)s->output->extended_data[7];
865 fl_mag = sqrtf(.5f * (xl + 1.f)) * ((yl + 1.f) * .5f) * mag_totall;
866 fr_mag = sqrtf(.5f * (xr + 1.f)) * ((yr + 1.f) * .5f) * mag_totalr;
867 lb_mag = sqrtf(.5f * (-xl + 1.f)) * ((yl + 1.f) * .5f) * mag_totall;
868 rb_mag = sqrtf(.5f * (-xr + 1.f)) * ((yr + 1.f) * .5f) * mag_totalr;
869 ls_mag = sqrtf(1.f - fabsf(xl)) * ((yl + 1.f) * .5f) * mag_totall;
870 rs_mag = sqrtf(1.f - fabsf(xr)) * ((yr + 1.f) * .5f) * mag_totalr;
872 dstl[2 * n ] = fl_mag * cosf(fl_phase);
873 dstl[2 * n + 1] = fl_mag * sinf(fl_phase);
875 dstr[2 * n ] = fr_mag * cosf(fr_phase);
876 dstr[2 * n + 1] = fr_mag * sinf(fr_phase);
879 dstc[2 * n + 1] = c_im;
881 dstlfe[2 * n ] = lfe_re;
882 dstlfe[2 * n + 1] = lfe_im;
884 dstlb[2 * n ] = lb_mag * cosf(bl_phase);
885 dstlb[2 * n + 1] = lb_mag * sinf(bl_phase);
887 dstrb[2 * n ] = rb_mag * cosf(br_phase);
888 dstrb[2 * n + 1] = rb_mag * sinf(br_phase);
890 dstls[2 * n ] = ls_mag * cosf(sl_phase);
891 dstls[2 * n + 1] = ls_mag * sinf(sl_phase);
893 dstrs[2 * n ] = rs_mag * cosf(sr_phase);
894 dstrs[2 * n + 1] = rs_mag * sinf(sr_phase);
897 static void filter_stereo(AVFilterContext *ctx)
899 AudioSurroundContext *s = ctx->priv;
903 srcl = (float *)s->input->extended_data[0];
904 srcr = (float *)s->input->extended_data[1];
906 for (n = 0; n < s->buf_size; n++) {
907 float l_re = srcl[2 * n], r_re = srcr[2 * n];
908 float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
909 float c_phase = atan2f(l_im + r_im, l_re + r_re);
910 float l_mag = hypotf(l_re, l_im);
911 float r_mag = hypotf(r_re, r_im);
912 float l_phase = atan2f(l_im, l_re);
913 float r_phase = atan2f(r_im, r_re);
914 float phase_dif = fabsf(l_phase - r_phase);
915 float mag_sum = l_mag + r_mag;
916 float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum;
917 float mag_total = hypotf(l_mag, r_mag);
920 if (phase_dif > M_PI)
921 phase_dif = 2 * M_PI - phase_dif;
923 stereo_position(mag_dif, phase_dif, &x, &y);
925 s->upmix_stereo(ctx, l_phase, r_phase, c_phase, mag_total, x, y, n);
929 static void filter_surround(AVFilterContext *ctx)
931 AudioSurroundContext *s = ctx->priv;
932 float *srcl, *srcr, *srcc;
935 srcl = (float *)s->input->extended_data[0];
936 srcr = (float *)s->input->extended_data[1];
937 srcc = (float *)s->input->extended_data[2];
939 for (n = 0; n < s->buf_size; n++) {
940 float l_re = srcl[2 * n], r_re = srcr[2 * n];
941 float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
942 float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
943 float c_mag = hypotf(c_re, c_im);
944 float c_phase = atan2f(c_im, c_re);
945 float l_mag = hypotf(l_re, l_im);
946 float r_mag = hypotf(r_re, r_im);
947 float l_phase = atan2f(l_im, l_re);
948 float r_phase = atan2f(r_im, r_re);
949 float phase_dif = fabsf(l_phase - r_phase);
950 float mag_sum = l_mag + r_mag;
951 float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum;
952 float mag_total = hypotf(l_mag, r_mag);
955 if (phase_dif > M_PI)
956 phase_dif = 2 * M_PI - phase_dif;
958 stereo_position(mag_dif, phase_dif, &x, &y);
960 s->upmix_3_0(ctx, l_phase, r_phase, c_phase, c_mag, mag_total, x, y, n);
964 static void filter_2_1(AVFilterContext *ctx)
966 AudioSurroundContext *s = ctx->priv;
967 float *srcl, *srcr, *srclfe;
970 srcl = (float *)s->input->extended_data[0];
971 srcr = (float *)s->input->extended_data[1];
972 srclfe = (float *)s->input->extended_data[2];
974 for (n = 0; n < s->buf_size; n++) {
975 float l_re = srcl[2 * n], r_re = srcr[2 * n];
976 float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
977 float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
978 float c_phase = atan2f(l_im + r_im, l_re + r_re);
979 float l_mag = hypotf(l_re, l_im);
980 float r_mag = hypotf(r_re, r_im);
981 float l_phase = atan2f(l_im, l_re);
982 float r_phase = atan2f(r_im, r_re);
983 float phase_dif = fabsf(l_phase - r_phase);
984 float mag_sum = l_mag + r_mag;
985 float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum;
986 float mag_total = hypotf(l_mag, r_mag);
989 if (phase_dif > M_PI)
990 phase_dif = 2 * M_PI - phase_dif;
992 stereo_position(mag_dif, phase_dif, &x, &y);
994 s->upmix_2_1(ctx, l_phase, r_phase, c_phase, mag_total, lfe_re, lfe_im, x, y, n);
998 static void filter_5_0_side(AVFilterContext *ctx)
1000 AudioSurroundContext *s = ctx->priv;
1001 float *srcl, *srcr, *srcc, *srcsl, *srcsr;
1004 srcl = (float *)s->input->extended_data[0];
1005 srcr = (float *)s->input->extended_data[1];
1006 srcc = (float *)s->input->extended_data[2];
1007 srcsl = (float *)s->input->extended_data[3];
1008 srcsr = (float *)s->input->extended_data[4];
1010 for (n = 0; n < s->buf_size; n++) {
1011 float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
1012 float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
1013 float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
1014 float sl_re = srcsl[2 * n], sl_im = srcsl[2 * n + 1];
1015 float sr_re = srcsr[2 * n], sr_im = srcsr[2 * n + 1];
1016 float fl_mag = hypotf(fl_re, fl_im);
1017 float fr_mag = hypotf(fr_re, fr_im);
1018 float fl_phase = atan2f(fl_im, fl_re);
1019 float fr_phase = atan2f(fr_im, fr_re);
1020 float sl_mag = hypotf(sl_re, sl_im);
1021 float sr_mag = hypotf(sr_re, sr_im);
1022 float sl_phase = atan2f(sl_im, sl_re);
1023 float sr_phase = atan2f(sr_im, sr_re);
1024 float phase_difl = fabsf(fl_phase - sl_phase);
1025 float phase_difr = fabsf(fr_phase - sr_phase);
1026 float magl_sum = fl_mag + sl_mag;
1027 float magr_sum = fr_mag + sr_mag;
1028 float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - sl_mag) / magl_sum;
1029 float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - sr_mag) / magr_sum;
1030 float mag_totall = hypotf(fl_mag, sl_mag);
1031 float mag_totalr = hypotf(fr_mag, sr_mag);
1032 float bl_phase = atan2f(fl_im + sl_im, fl_re + sl_re);
1033 float br_phase = atan2f(fr_im + sr_im, fr_re + sr_re);
1037 if (phase_difl > M_PI)
1038 phase_difl = 2 * M_PI - phase_difl;
1040 if (phase_difr > M_PI)
1041 phase_difr = 2 * M_PI - phase_difr;
1043 stereo_position(mag_difl, phase_difl, &xl, &yl);
1044 stereo_position(mag_difr, phase_difr, &xr, &yr);
1046 s->upmix_5_0(ctx, c_re, c_im,
1047 mag_totall, mag_totalr,
1055 static void filter_5_1_side(AVFilterContext *ctx)
1057 AudioSurroundContext *s = ctx->priv;
1058 float *srcl, *srcr, *srcc, *srclfe, *srcsl, *srcsr;
1061 srcl = (float *)s->input->extended_data[0];
1062 srcr = (float *)s->input->extended_data[1];
1063 srcc = (float *)s->input->extended_data[2];
1064 srclfe = (float *)s->input->extended_data[3];
1065 srcsl = (float *)s->input->extended_data[4];
1066 srcsr = (float *)s->input->extended_data[5];
1068 for (n = 0; n < s->buf_size; n++) {
1069 float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
1070 float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
1071 float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
1072 float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
1073 float sl_re = srcsl[2 * n], sl_im = srcsl[2 * n + 1];
1074 float sr_re = srcsr[2 * n], sr_im = srcsr[2 * n + 1];
1075 float fl_mag = hypotf(fl_re, fl_im);
1076 float fr_mag = hypotf(fr_re, fr_im);
1077 float fl_phase = atan2f(fl_im, fl_re);
1078 float fr_phase = atan2f(fr_im, fr_re);
1079 float sl_mag = hypotf(sl_re, sl_im);
1080 float sr_mag = hypotf(sr_re, sr_im);
1081 float sl_phase = atan2f(sl_im, sl_re);
1082 float sr_phase = atan2f(sr_im, sr_re);
1083 float phase_difl = fabsf(fl_phase - sl_phase);
1084 float phase_difr = fabsf(fr_phase - sr_phase);
1085 float magl_sum = fl_mag + sl_mag;
1086 float magr_sum = fr_mag + sr_mag;
1087 float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - sl_mag) / magl_sum;
1088 float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - sr_mag) / magr_sum;
1089 float mag_totall = hypotf(fl_mag, sl_mag);
1090 float mag_totalr = hypotf(fr_mag, sr_mag);
1091 float bl_phase = atan2f(fl_im + sl_im, fl_re + sl_re);
1092 float br_phase = atan2f(fr_im + sr_im, fr_re + sr_re);
1096 if (phase_difl > M_PI)
1097 phase_difl = 2 * M_PI - phase_difl;
1099 if (phase_difr > M_PI)
1100 phase_difr = 2 * M_PI - phase_difr;
1102 stereo_position(mag_difl, phase_difl, &xl, &yl);
1103 stereo_position(mag_difr, phase_difr, &xr, &yr);
1105 s->upmix_5_1(ctx, c_re, c_im, lfe_re, lfe_im,
1106 mag_totall, mag_totalr,
1114 static void filter_5_1_back(AVFilterContext *ctx)
1116 AudioSurroundContext *s = ctx->priv;
1117 float *srcl, *srcr, *srcc, *srclfe, *srcbl, *srcbr;
1120 srcl = (float *)s->input->extended_data[0];
1121 srcr = (float *)s->input->extended_data[1];
1122 srcc = (float *)s->input->extended_data[2];
1123 srclfe = (float *)s->input->extended_data[3];
1124 srcbl = (float *)s->input->extended_data[4];
1125 srcbr = (float *)s->input->extended_data[5];
1127 for (n = 0; n < s->buf_size; n++) {
1128 float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
1129 float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
1130 float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
1131 float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
1132 float bl_re = srcbl[2 * n], bl_im = srcbl[2 * n + 1];
1133 float br_re = srcbr[2 * n], br_im = srcbr[2 * n + 1];
1134 float fl_mag = hypotf(fl_re, fl_im);
1135 float fr_mag = hypotf(fr_re, fr_im);
1136 float fl_phase = atan2f(fl_im, fl_re);
1137 float fr_phase = atan2f(fr_im, fr_re);
1138 float bl_mag = hypotf(bl_re, bl_im);
1139 float br_mag = hypotf(br_re, br_im);
1140 float bl_phase = atan2f(bl_im, bl_re);
1141 float br_phase = atan2f(br_im, br_re);
1142 float phase_difl = fabsf(fl_phase - bl_phase);
1143 float phase_difr = fabsf(fr_phase - br_phase);
1144 float magl_sum = fl_mag + bl_mag;
1145 float magr_sum = fr_mag + br_mag;
1146 float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - bl_mag) / magl_sum;
1147 float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - br_mag) / magr_sum;
1148 float mag_totall = hypotf(fl_mag, bl_mag);
1149 float mag_totalr = hypotf(fr_mag, br_mag);
1150 float sl_phase = atan2f(fl_im + bl_im, fl_re + bl_re);
1151 float sr_phase = atan2f(fr_im + br_im, fr_re + br_re);
1155 if (phase_difl > M_PI)
1156 phase_difl = 2 * M_PI - phase_difl;
1158 if (phase_difr > M_PI)
1159 phase_difr = 2 * M_PI - phase_difr;
1161 stereo_position(mag_difl, phase_difl, &xl, &yl);
1162 stereo_position(mag_difr, phase_difr, &xr, &yr);
1164 s->upmix_5_1(ctx, c_re, c_im, lfe_re, lfe_im,
1165 mag_totall, mag_totalr,
1173 static int init(AVFilterContext *ctx)
1175 AudioSurroundContext *s = ctx->priv;
1179 if (!(s->out_channel_layout = av_get_channel_layout(s->out_channel_layout_str))) {
1180 av_log(ctx, AV_LOG_ERROR, "Error parsing output channel layout '%s'.\n",
1181 s->out_channel_layout_str);
1182 return AVERROR(EINVAL);
1185 if (!(s->in_channel_layout = av_get_channel_layout(s->in_channel_layout_str))) {
1186 av_log(ctx, AV_LOG_ERROR, "Error parsing input channel layout '%s'.\n",
1187 s->in_channel_layout_str);
1188 return AVERROR(EINVAL);
1191 if (s->lowcutf >= s->highcutf) {
1192 av_log(ctx, AV_LOG_ERROR, "Low cut-off '%d' should be less than high cut-off '%d'.\n",
1193 s->lowcutf, s->highcutf);
1194 return AVERROR(EINVAL);
1197 switch (s->in_channel_layout) {
1198 case AV_CH_LAYOUT_STEREO:
1199 s->filter = filter_stereo;
1200 switch (s->out_channel_layout) {
1201 case AV_CH_LAYOUT_MONO:
1202 s->upmix_stereo = upmix_1_0;
1204 case AV_CH_LAYOUT_STEREO:
1205 s->upmix_stereo = upmix_stereo;
1207 case AV_CH_LAYOUT_2POINT1:
1208 s->upmix_stereo = upmix_2_1;
1210 case AV_CH_LAYOUT_SURROUND:
1211 s->upmix_stereo = upmix_3_0;
1213 case AV_CH_LAYOUT_3POINT1:
1214 s->upmix_stereo = upmix_3_1;
1216 case AV_CH_LAYOUT_4POINT0:
1217 s->upmix_stereo = upmix_4_0;
1219 case AV_CH_LAYOUT_4POINT1:
1220 s->upmix_stereo = upmix_4_1;
1222 case AV_CH_LAYOUT_5POINT0_BACK:
1223 s->upmix_stereo = upmix_5_0_back;
1225 case AV_CH_LAYOUT_5POINT1_BACK:
1226 s->upmix_stereo = upmix_5_1_back;
1228 case AV_CH_LAYOUT_7POINT0:
1229 s->upmix_stereo = upmix_7_0;
1231 case AV_CH_LAYOUT_7POINT1:
1232 s->upmix_stereo = upmix_7_1;
1238 case AV_CH_LAYOUT_2POINT1:
1239 s->filter = filter_2_1;
1240 switch (s->out_channel_layout) {
1241 case AV_CH_LAYOUT_5POINT1_BACK:
1242 s->upmix_2_1 = upmix_5_1_back_2_1;
1248 case AV_CH_LAYOUT_SURROUND:
1249 s->filter = filter_surround;
1250 switch (s->out_channel_layout) {
1251 case AV_CH_LAYOUT_3POINT1:
1252 s->upmix_3_0 = upmix_3_1_surround;
1254 case AV_CH_LAYOUT_5POINT1_BACK:
1255 s->upmix_3_0 = upmix_5_1_back_surround;
1261 case AV_CH_LAYOUT_5POINT0:
1262 s->filter = filter_5_0_side;
1263 switch (s->out_channel_layout) {
1264 case AV_CH_LAYOUT_7POINT1:
1265 s->upmix_5_0 = upmix_7_1_5_0_side;
1271 case AV_CH_LAYOUT_5POINT1:
1272 s->filter = filter_5_1_side;
1273 switch (s->out_channel_layout) {
1274 case AV_CH_LAYOUT_7POINT1:
1275 s->upmix_5_1 = upmix_7_1_5_1;
1281 case AV_CH_LAYOUT_5POINT1_BACK:
1282 s->filter = filter_5_1_back;
1283 switch (s->out_channel_layout) {
1284 case AV_CH_LAYOUT_7POINT1:
1285 s->upmix_5_1 = upmix_7_1_5_1;
1293 av_log(ctx, AV_LOG_ERROR, "Unsupported upmix: '%s' -> '%s'.\n",
1294 s->in_channel_layout_str, s->out_channel_layout_str);
1295 return AVERROR(EINVAL);
1299 s->pts = AV_NOPTS_VALUE;
1301 s->window_func_lut = av_calloc(s->buf_size, sizeof(*s->window_func_lut));
1302 if (!s->window_func_lut)
1303 return AVERROR(ENOMEM);
1305 generate_window_func(s->window_func_lut, s->buf_size, s->win_func, &overlap);
1306 if (s->overlap == 1)
1307 s->overlap = overlap;
1309 for (i = 0; i < s->buf_size; i++)
1310 s->window_func_lut[i] = sqrtf(s->window_func_lut[i] / s->buf_size);
1311 s->hop_size = s->buf_size * (1. - s->overlap);
1312 if (s->hop_size <= 0)
1313 return AVERROR(EINVAL);
1318 static int fft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
1320 AudioSurroundContext *s = ctx->priv;
1321 const float level_in = s->input_levels[ch];
1325 memset(s->input->extended_data[ch] + s->buf_size * sizeof(float), 0, s->buf_size * sizeof(float));
1327 dst = (float *)s->input->extended_data[ch];
1328 for (n = 0; n < s->buf_size; n++) {
1329 dst[n] *= s->window_func_lut[n] * level_in;
1332 av_rdft_calc(s->rdft[ch], (float *)s->input->extended_data[ch]);
1337 static int ifft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
1339 AudioSurroundContext *s = ctx->priv;
1340 const float level_out = s->output_levels[ch];
1345 av_rdft_calc(s->irdft[ch], (float *)s->output->extended_data[ch]);
1347 dst = (float *)s->output->extended_data[ch];
1348 ptr = (float *)s->overlap_buffer->extended_data[ch];
1350 memmove(s->overlap_buffer->extended_data[ch],
1351 s->overlap_buffer->extended_data[ch] + s->hop_size * sizeof(float),
1352 s->buf_size * sizeof(float));
1353 memset(s->overlap_buffer->extended_data[ch] + s->buf_size * sizeof(float),
1354 0, s->hop_size * sizeof(float));
1356 for (n = 0; n < s->buf_size; n++) {
1357 ptr[n] += dst[n] * s->window_func_lut[n] * level_out;
1360 ptr = (float *)s->overlap_buffer->extended_data[ch];
1361 dst = (float *)out->extended_data[ch];
1362 memcpy(dst, ptr, s->hop_size * sizeof(float));
1367 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
1369 AVFilterContext *ctx = inlink->dst;
1370 AVFilterLink *outlink = ctx->outputs[0];
1371 AudioSurroundContext *s = ctx->priv;
1374 ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
1376 if (ret >= 0 && s->pts == AV_NOPTS_VALUE)
1383 while (av_audio_fifo_size(s->fifo) >= s->buf_size) {
1386 ret = av_audio_fifo_peek(s->fifo, (void **)s->input->extended_data, s->buf_size);
1390 ctx->internal->execute(ctx, fft_channel, NULL, NULL, inlink->channels);
1394 out = ff_get_audio_buffer(outlink, s->hop_size);
1396 return AVERROR(ENOMEM);
1398 ctx->internal->execute(ctx, ifft_channel, out, NULL, outlink->channels);
1401 if (s->pts != AV_NOPTS_VALUE)
1402 s->pts += av_rescale_q(out->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
1403 av_audio_fifo_drain(s->fifo, s->hop_size);
1404 ret = ff_filter_frame(outlink, out);
1412 static int request_frame(AVFilterLink *outlink)
1414 AVFilterContext *ctx = outlink->src;
1415 AudioSurroundContext *s = ctx->priv;
1418 ret = ff_request_frame(ctx->inputs[0]);
1420 if (ret == AVERROR_EOF && av_audio_fifo_size(s->fifo) > 0 && av_audio_fifo_size(s->fifo) < s->buf_size) {
1423 in = ff_get_audio_buffer(outlink, s->buf_size - av_audio_fifo_size(s->fifo));
1425 return AVERROR(ENOMEM);
1426 ret = filter_frame(ctx->inputs[0], in);
1427 av_audio_fifo_drain(s->fifo, s->buf_size);
1433 static av_cold void uninit(AVFilterContext *ctx)
1435 AudioSurroundContext *s = ctx->priv;
1438 av_frame_free(&s->input);
1439 av_frame_free(&s->output);
1440 av_frame_free(&s->overlap_buffer);
1442 for (ch = 0; ch < s->nb_in_channels; ch++) {
1443 av_rdft_end(s->rdft[ch]);
1445 for (ch = 0; ch < s->nb_out_channels; ch++) {
1446 av_rdft_end(s->irdft[ch]);
1448 av_freep(&s->input_levels);
1449 av_freep(&s->output_levels);
1451 av_freep(&s->irdft);
1452 av_audio_fifo_free(s->fifo);
1453 av_freep(&s->window_func_lut);
1456 #define OFFSET(x) offsetof(AudioSurroundContext, x)
1457 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1459 static const AVOption surround_options[] = {
1460 { "chl_out", "set output channel layout", OFFSET(out_channel_layout_str), AV_OPT_TYPE_STRING, {.str="5.1"}, 0, 0, FLAGS },
1461 { "chl_in", "set input channel layout", OFFSET(in_channel_layout_str), AV_OPT_TYPE_STRING, {.str="stereo"},0, 0, FLAGS },
1462 { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1463 { "level_out", "set output level", OFFSET(level_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1464 { "lfe", "output LFE", OFFSET(output_lfe), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
1465 { "lfe_low", "LFE low cut off", OFFSET(lowcutf), AV_OPT_TYPE_INT, {.i64=128}, 0, 256, FLAGS },
1466 { "lfe_high", "LFE high cut off", OFFSET(highcutf), AV_OPT_TYPE_INT, {.i64=256}, 0, 512, FLAGS },
1467 { "lfe_mode", "set LFE channel mode", OFFSET(lfe_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "lfe_mode" },
1468 { "add", "just add LFE channel", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 1, FLAGS, "lfe_mode" },
1469 { "sub", "substract LFE channel with others", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 1, FLAGS, "lfe_mode" },
1470 { "fc_in", "set front center channel input level", OFFSET(fc_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1471 { "fc_out", "set front center channel output level", OFFSET(fc_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1472 { "lfe_in", "set lfe channel input level", OFFSET(lfe_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1473 { "lfe_out", "set lfe channel output level", OFFSET(lfe_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1474 { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
1475 { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
1476 { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
1477 { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
1478 { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
1479 { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
1480 { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
1481 { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
1482 { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
1483 { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
1484 { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
1485 { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
1486 { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
1487 { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
1488 { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
1489 { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
1490 { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
1491 { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
1492 { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
1493 { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
1494 { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
1495 { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
1496 { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
1500 AVFILTER_DEFINE_CLASS(surround);
1502 static const AVFilterPad inputs[] = {
1505 .type = AVMEDIA_TYPE_AUDIO,
1506 .filter_frame = filter_frame,
1507 .config_props = config_input,
1512 static const AVFilterPad outputs[] = {
1515 .type = AVMEDIA_TYPE_AUDIO,
1516 .request_frame = request_frame,
1517 .config_props = config_output,
1522 AVFilter ff_af_surround = {
1524 .description = NULL_IF_CONFIG_SMALL("Apply audio surround upmix filter."),
1525 .query_formats = query_formats,
1526 .priv_size = sizeof(AudioSurroundContext),
1527 .priv_class = &surround_class,
1532 .flags = AVFILTER_FLAG_SLICE_THREADS,