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"
29 typedef struct AudioSurroundContext {
32 char *out_channel_layout_str;
33 char *in_channel_layout_str;
51 uint64_t out_channel_layout;
52 uint64_t in_channel_layout;
58 AVFrame *overlap_buffer;
63 RDFTContext **rdft, **irdft;
64 float *window_func_lut;
68 void (*filter)(AVFilterContext *ctx);
69 void (*upmix_stereo)(AVFilterContext *ctx,
76 void (*upmix_2_1)(AVFilterContext *ctx,
85 void (*upmix_3_0)(AVFilterContext *ctx,
93 void (*upmix_5_0)(AVFilterContext *ctx,
94 float c_re, float c_im,
95 float mag_totall, float mag_totalr,
96 float fl_phase, float fr_phase,
97 float bl_phase, float br_phase,
98 float sl_phase, float sr_phase,
102 void (*upmix_5_1)(AVFilterContext *ctx,
103 float c_re, float c_im,
104 float lfe_re, float lfe_im,
105 float mag_totall, float mag_totalr,
106 float fl_phase, float fr_phase,
107 float bl_phase, float br_phase,
108 float sl_phase, float sr_phase,
112 } AudioSurroundContext;
114 static int query_formats(AVFilterContext *ctx)
116 AudioSurroundContext *s = ctx->priv;
117 AVFilterFormats *formats = NULL;
118 AVFilterChannelLayouts *layouts = NULL;
121 ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLTP);
124 ret = ff_set_common_formats(ctx, formats);
129 ret = ff_add_channel_layout(&layouts, s->out_channel_layout);
133 ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
138 ret = ff_add_channel_layout(&layouts, s->in_channel_layout);
142 ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts);
146 formats = ff_all_samplerates();
148 return AVERROR(ENOMEM);
149 return ff_set_common_samplerates(ctx, formats);
152 static int config_input(AVFilterLink *inlink)
154 AVFilterContext *ctx = inlink->dst;
155 AudioSurroundContext *s = ctx->priv;
158 s->rdft = av_calloc(inlink->channels, sizeof(*s->rdft));
160 return AVERROR(ENOMEM);
162 for (ch = 0; ch < inlink->channels; ch++) {
163 s->rdft[ch] = av_rdft_init(ff_log2(s->buf_size), DFT_R2C);
165 return AVERROR(ENOMEM);
167 s->nb_in_channels = inlink->channels;
168 s->input_levels = av_malloc_array(s->nb_in_channels, sizeof(*s->input_levels));
169 if (!s->input_levels)
170 return AVERROR(ENOMEM);
171 for (ch = 0; ch < s->nb_in_channels; ch++)
172 s->input_levels[ch] = s->level_in;
173 ch = av_get_channel_layout_channel_index(inlink->channel_layout, AV_CH_FRONT_CENTER);
175 s->input_levels[ch] *= s->fc_in;
176 ch = av_get_channel_layout_channel_index(inlink->channel_layout, AV_CH_LOW_FREQUENCY);
178 s->input_levels[ch] *= s->lfe_in;
180 s->input = ff_get_audio_buffer(inlink, s->buf_size * 2);
182 return AVERROR(ENOMEM);
184 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->buf_size);
186 return AVERROR(ENOMEM);
188 s->lowcut = 1.f * s->lowcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
189 s->highcut = 1.f * s->highcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
194 static int config_output(AVFilterLink *outlink)
196 AVFilterContext *ctx = outlink->src;
197 AudioSurroundContext *s = ctx->priv;
200 s->irdft = av_calloc(outlink->channels, sizeof(*s->irdft));
202 return AVERROR(ENOMEM);
204 for (ch = 0; ch < outlink->channels; ch++) {
205 s->irdft[ch] = av_rdft_init(ff_log2(s->buf_size), IDFT_C2R);
207 return AVERROR(ENOMEM);
209 s->nb_out_channels = outlink->channels;
210 s->output_levels = av_malloc_array(s->nb_out_channels, sizeof(*s->output_levels));
211 if (!s->output_levels)
212 return AVERROR(ENOMEM);
213 for (ch = 0; ch < s->nb_out_channels; ch++)
214 s->output_levels[ch] = s->level_out;
215 ch = av_get_channel_layout_channel_index(outlink->channel_layout, AV_CH_FRONT_CENTER);
217 s->output_levels[ch] *= s->fc_out;
218 ch = av_get_channel_layout_channel_index(outlink->channel_layout, AV_CH_LOW_FREQUENCY);
220 s->output_levels[ch] *= s->lfe_out;
222 s->output = ff_get_audio_buffer(outlink, s->buf_size * 2);
223 s->overlap_buffer = ff_get_audio_buffer(outlink, s->buf_size * 2);
224 if (!s->overlap_buffer || !s->output)
225 return AVERROR(ENOMEM);
230 static void stereo_position(float a, float p, float *x, float *y)
232 *x = av_clipf(a+FFMAX(0, sinf(p-M_PI_2))*FFDIFFSIGN(a,0), -1, 1);
233 *y = av_clipf(cosf(a*M_PI_2+M_PI)*cosf(M_PI_2-p/M_PI)*M_LN10+1, -1, 1);
236 static inline void get_lfe(int output_lfe, int n, float lowcut, float highcut,
237 float *lfe_mag, float *mag_total)
239 if (output_lfe && n < highcut) {
240 *lfe_mag = n < lowcut ? 1.f : .5f*(1.f+cosf(M_PI*(lowcut-n)/(lowcut-highcut)));
241 *lfe_mag *= *mag_total;
242 *mag_total -= *lfe_mag;
248 static void upmix_1_0(AVFilterContext *ctx,
256 AudioSurroundContext *s = ctx->priv;
259 dst = (float *)s->output->extended_data[0];
261 mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
263 dst[2 * n ] = mag * cosf(c_phase);
264 dst[2 * n + 1] = mag * sinf(c_phase);
267 static void upmix_stereo(AVFilterContext *ctx,
275 AudioSurroundContext *s = ctx->priv;
276 float l_mag, r_mag, *dstl, *dstr;
278 dstl = (float *)s->output->extended_data[0];
279 dstr = (float *)s->output->extended_data[1];
281 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
282 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
284 dstl[2 * n ] = l_mag * cosf(l_phase);
285 dstl[2 * n + 1] = l_mag * sinf(l_phase);
287 dstr[2 * n ] = r_mag * cosf(r_phase);
288 dstr[2 * n + 1] = r_mag * sinf(r_phase);
291 static void upmix_2_1(AVFilterContext *ctx,
299 AudioSurroundContext *s = ctx->priv;
300 float lfe_mag, l_mag, r_mag, *dstl, *dstr, *dstlfe;
302 dstl = (float *)s->output->extended_data[0];
303 dstr = (float *)s->output->extended_data[1];
304 dstlfe = (float *)s->output->extended_data[2];
306 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
308 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
309 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
311 dstl[2 * n ] = l_mag * cosf(l_phase);
312 dstl[2 * n + 1] = l_mag * sinf(l_phase);
314 dstr[2 * n ] = r_mag * cosf(r_phase);
315 dstr[2 * n + 1] = r_mag * sinf(r_phase);
317 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
318 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
321 static void upmix_3_0(AVFilterContext *ctx,
329 AudioSurroundContext *s = ctx->priv;
330 float l_mag, r_mag, c_mag, *dstc, *dstl, *dstr;
332 dstl = (float *)s->output->extended_data[0];
333 dstr = (float *)s->output->extended_data[1];
334 dstc = (float *)s->output->extended_data[2];
336 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
337 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
338 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
340 dstl[2 * n ] = l_mag * cosf(l_phase);
341 dstl[2 * n + 1] = l_mag * sinf(l_phase);
343 dstr[2 * n ] = r_mag * cosf(r_phase);
344 dstr[2 * n + 1] = r_mag * sinf(r_phase);
346 dstc[2 * n ] = c_mag * cosf(c_phase);
347 dstc[2 * n + 1] = c_mag * sinf(c_phase);
350 static void upmix_3_1(AVFilterContext *ctx,
358 AudioSurroundContext *s = ctx->priv;
359 float lfe_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstlfe;
361 dstl = (float *)s->output->extended_data[0];
362 dstr = (float *)s->output->extended_data[1];
363 dstc = (float *)s->output->extended_data[2];
364 dstlfe = (float *)s->output->extended_data[3];
366 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
368 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
369 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
370 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
372 dstl[2 * n ] = l_mag * cosf(l_phase);
373 dstl[2 * n + 1] = l_mag * sinf(l_phase);
375 dstr[2 * n ] = r_mag * cosf(r_phase);
376 dstr[2 * n + 1] = r_mag * sinf(r_phase);
378 dstc[2 * n ] = c_mag * cosf(c_phase);
379 dstc[2 * n + 1] = c_mag * sinf(c_phase);
381 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
382 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
385 static void upmix_3_1_surround(AVFilterContext *ctx,
394 AudioSurroundContext *s = ctx->priv;
395 float lfe_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
397 dstl = (float *)s->output->extended_data[0];
398 dstr = (float *)s->output->extended_data[1];
399 dstc = (float *)s->output->extended_data[2];
400 dstlfe = (float *)s->output->extended_data[3];
402 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &c_mag);
404 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
405 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
407 dstl[2 * n ] = l_mag * cosf(l_phase);
408 dstl[2 * n + 1] = l_mag * sinf(l_phase);
410 dstr[2 * n ] = r_mag * cosf(r_phase);
411 dstr[2 * n + 1] = r_mag * sinf(r_phase);
413 dstc[2 * n ] = c_mag * cosf(c_phase);
414 dstc[2 * n + 1] = c_mag * sinf(c_phase);
416 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
417 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
420 static void upmix_4_0(AVFilterContext *ctx,
428 float b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb;
429 AudioSurroundContext *s = ctx->priv;
431 dstl = (float *)s->output->extended_data[0];
432 dstr = (float *)s->output->extended_data[1];
433 dstc = (float *)s->output->extended_data[2];
434 dstb = (float *)s->output->extended_data[3];
436 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
437 b_mag = sqrtf(1.f - fabsf(x)) * ((1.f - y) * .5f) * mag_total;
438 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
439 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
441 dstl[2 * n ] = l_mag * cosf(l_phase);
442 dstl[2 * n + 1] = l_mag * sinf(l_phase);
444 dstr[2 * n ] = r_mag * cosf(r_phase);
445 dstr[2 * n + 1] = r_mag * sinf(r_phase);
447 dstc[2 * n ] = c_mag * cosf(c_phase);
448 dstc[2 * n + 1] = c_mag * sinf(c_phase);
450 dstb[2 * n ] = b_mag * cosf(c_phase);
451 dstb[2 * n + 1] = b_mag * sinf(c_phase);
454 static void upmix_4_1(AVFilterContext *ctx,
462 float lfe_mag, b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb, *dstlfe;
463 AudioSurroundContext *s = ctx->priv;
465 dstl = (float *)s->output->extended_data[0];
466 dstr = (float *)s->output->extended_data[1];
467 dstc = (float *)s->output->extended_data[2];
468 dstlfe = (float *)s->output->extended_data[3];
469 dstb = (float *)s->output->extended_data[4];
471 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
473 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
474 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
476 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
477 b_mag = sqrtf(1.f - fabsf(x)) * ((1.f - y) * .5f) * mag_total;
478 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
479 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
481 dstl[2 * n ] = l_mag * cosf(l_phase);
482 dstl[2 * n + 1] = l_mag * sinf(l_phase);
484 dstr[2 * n ] = r_mag * cosf(r_phase);
485 dstr[2 * n + 1] = r_mag * sinf(r_phase);
487 dstc[2 * n ] = c_mag * cosf(c_phase);
488 dstc[2 * n + 1] = c_mag * sinf(c_phase);
490 dstb[2 * n ] = b_mag * cosf(c_phase);
491 dstb[2 * n + 1] = b_mag * sinf(c_phase);
494 static void upmix_5_0_back(AVFilterContext *ctx,
502 float l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs;
503 AudioSurroundContext *s = ctx->priv;
505 dstl = (float *)s->output->extended_data[0];
506 dstr = (float *)s->output->extended_data[1];
507 dstc = (float *)s->output->extended_data[2];
508 dstls = (float *)s->output->extended_data[3];
509 dstrs = (float *)s->output->extended_data[4];
511 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
512 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
513 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
514 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
515 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
517 dstl[2 * n ] = l_mag * cosf(l_phase);
518 dstl[2 * n + 1] = l_mag * sinf(l_phase);
520 dstr[2 * n ] = r_mag * cosf(r_phase);
521 dstr[2 * n + 1] = r_mag * sinf(r_phase);
523 dstc[2 * n ] = c_mag * cosf(c_phase);
524 dstc[2 * n + 1] = c_mag * sinf(c_phase);
526 dstls[2 * n ] = ls_mag * cosf(l_phase);
527 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
529 dstrs[2 * n ] = rs_mag * cosf(r_phase);
530 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
533 static void upmix_5_1_back(AVFilterContext *ctx,
541 float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlfe;
542 AudioSurroundContext *s = ctx->priv;
544 dstl = (float *)s->output->extended_data[0];
545 dstr = (float *)s->output->extended_data[1];
546 dstc = (float *)s->output->extended_data[2];
547 dstlfe = (float *)s->output->extended_data[3];
548 dstls = (float *)s->output->extended_data[4];
549 dstrs = (float *)s->output->extended_data[5];
551 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
553 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
554 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
555 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
556 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
557 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
559 dstl[2 * n ] = l_mag * cosf(l_phase);
560 dstl[2 * n + 1] = l_mag * sinf(l_phase);
562 dstr[2 * n ] = r_mag * cosf(r_phase);
563 dstr[2 * n + 1] = r_mag * sinf(r_phase);
565 dstc[2 * n ] = c_mag * cosf(c_phase);
566 dstc[2 * n + 1] = c_mag * sinf(c_phase);
568 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
569 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
571 dstls[2 * n ] = ls_mag * cosf(l_phase);
572 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
574 dstrs[2 * n ] = rs_mag * cosf(r_phase);
575 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
578 static void upmix_5_1_back_surround(AVFilterContext *ctx,
587 AudioSurroundContext *s = ctx->priv;
588 float lfe_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
589 float ls_mag, rs_mag, *dstls, *dstrs;
591 dstl = (float *)s->output->extended_data[0];
592 dstr = (float *)s->output->extended_data[1];
593 dstc = (float *)s->output->extended_data[2];
594 dstlfe = (float *)s->output->extended_data[3];
595 dstls = (float *)s->output->extended_data[4];
596 dstrs = (float *)s->output->extended_data[5];
598 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &c_mag);
600 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
601 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
602 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
603 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
605 dstl[2 * n ] = l_mag * cosf(l_phase);
606 dstl[2 * n + 1] = l_mag * sinf(l_phase);
608 dstr[2 * n ] = r_mag * cosf(r_phase);
609 dstr[2 * n + 1] = r_mag * sinf(r_phase);
611 dstc[2 * n ] = c_mag * cosf(c_phase);
612 dstc[2 * n + 1] = c_mag * sinf(c_phase);
614 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
615 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
617 dstls[2 * n ] = ls_mag * cosf(l_phase);
618 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
620 dstrs[2 * n ] = rs_mag * cosf(r_phase);
621 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
624 static void upmix_5_1_back_2_1(AVFilterContext *ctx,
634 AudioSurroundContext *s = ctx->priv;
635 float c_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
636 float ls_mag, rs_mag, *dstls, *dstrs;
638 dstl = (float *)s->output->extended_data[0];
639 dstr = (float *)s->output->extended_data[1];
640 dstc = (float *)s->output->extended_data[2];
641 dstlfe = (float *)s->output->extended_data[3];
642 dstls = (float *)s->output->extended_data[4];
643 dstrs = (float *)s->output->extended_data[5];
645 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
646 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
647 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
648 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
649 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
651 dstl[2 * n ] = l_mag * cosf(l_phase);
652 dstl[2 * n + 1] = l_mag * sinf(l_phase);
654 dstr[2 * n ] = r_mag * cosf(r_phase);
655 dstr[2 * n + 1] = r_mag * sinf(r_phase);
657 dstc[2 * n ] = c_mag * cosf(c_phase);
658 dstc[2 * n + 1] = c_mag * sinf(c_phase);
660 dstlfe[2 * n ] = lfe_re;
661 dstlfe[2 * n + 1] = lfe_im;
663 dstls[2 * n ] = ls_mag * cosf(l_phase);
664 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
666 dstrs[2 * n ] = rs_mag * cosf(r_phase);
667 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
670 static void upmix_7_0(AVFilterContext *ctx,
678 float l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
679 float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb;
680 AudioSurroundContext *s = ctx->priv;
682 dstl = (float *)s->output->extended_data[0];
683 dstr = (float *)s->output->extended_data[1];
684 dstc = (float *)s->output->extended_data[2];
685 dstlb = (float *)s->output->extended_data[3];
686 dstrb = (float *)s->output->extended_data[4];
687 dstls = (float *)s->output->extended_data[5];
688 dstrs = (float *)s->output->extended_data[6];
690 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
691 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
692 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
693 lb_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
694 rb_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
695 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - fabsf(y)) * mag_total;
696 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - fabsf(y)) * mag_total;
698 dstl[2 * n ] = l_mag * cosf(l_phase);
699 dstl[2 * n + 1] = l_mag * sinf(l_phase);
701 dstr[2 * n ] = r_mag * cosf(r_phase);
702 dstr[2 * n + 1] = r_mag * sinf(r_phase);
704 dstc[2 * n ] = c_mag * cosf(c_phase);
705 dstc[2 * n + 1] = c_mag * sinf(c_phase);
707 dstlb[2 * n ] = lb_mag * cosf(l_phase);
708 dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
710 dstrb[2 * n ] = rb_mag * cosf(r_phase);
711 dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
713 dstls[2 * n ] = ls_mag * cosf(l_phase);
714 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
716 dstrs[2 * n ] = rs_mag * cosf(r_phase);
717 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
720 static void upmix_7_1(AVFilterContext *ctx,
728 float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
729 float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
730 AudioSurroundContext *s = ctx->priv;
732 dstl = (float *)s->output->extended_data[0];
733 dstr = (float *)s->output->extended_data[1];
734 dstc = (float *)s->output->extended_data[2];
735 dstlfe = (float *)s->output->extended_data[3];
736 dstlb = (float *)s->output->extended_data[4];
737 dstrb = (float *)s->output->extended_data[5];
738 dstls = (float *)s->output->extended_data[6];
739 dstrs = (float *)s->output->extended_data[7];
741 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
743 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
744 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
745 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
746 lb_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
747 rb_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
748 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - fabsf(y)) * mag_total;
749 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - fabsf(y)) * mag_total;
751 dstl[2 * n ] = l_mag * cosf(l_phase);
752 dstl[2 * n + 1] = l_mag * sinf(l_phase);
754 dstr[2 * n ] = r_mag * cosf(r_phase);
755 dstr[2 * n + 1] = r_mag * sinf(r_phase);
757 dstc[2 * n ] = c_mag * cosf(c_phase);
758 dstc[2 * n + 1] = c_mag * sinf(c_phase);
760 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
761 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
763 dstlb[2 * n ] = lb_mag * cosf(l_phase);
764 dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
766 dstrb[2 * n ] = rb_mag * cosf(r_phase);
767 dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
769 dstls[2 * n ] = ls_mag * cosf(l_phase);
770 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
772 dstrs[2 * n ] = rs_mag * cosf(r_phase);
773 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
776 static void upmix_7_1_5_0_side(AVFilterContext *ctx,
777 float c_re, float c_im,
778 float mag_totall, float mag_totalr,
779 float fl_phase, float fr_phase,
780 float bl_phase, float br_phase,
781 float sl_phase, float sr_phase,
786 float fl_mag, fr_mag, ls_mag, rs_mag, lb_mag, rb_mag;
787 float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
788 float lfe_mag, c_phase, mag_total = (mag_totall + mag_totalr) * 0.5;
789 AudioSurroundContext *s = ctx->priv;
791 dstl = (float *)s->output->extended_data[0];
792 dstr = (float *)s->output->extended_data[1];
793 dstc = (float *)s->output->extended_data[2];
794 dstlfe = (float *)s->output->extended_data[3];
795 dstlb = (float *)s->output->extended_data[4];
796 dstrb = (float *)s->output->extended_data[5];
797 dstls = (float *)s->output->extended_data[6];
798 dstrs = (float *)s->output->extended_data[7];
800 c_phase = atan2f(c_im, c_re);
802 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
804 fl_mag = sqrtf(.5f * (xl + 1.f)) * ((yl + 1.f) * .5f) * mag_totall;
805 fr_mag = sqrtf(.5f * (xr + 1.f)) * ((yr + 1.f) * .5f) * mag_totalr;
806 lb_mag = sqrtf(.5f * (-xl + 1.f)) * ((yl + 1.f) * .5f) * mag_totall;
807 rb_mag = sqrtf(.5f * (-xr + 1.f)) * ((yr + 1.f) * .5f) * mag_totalr;
808 ls_mag = sqrtf(1.f - fabsf(xl)) * ((yl + 1.f) * .5f) * mag_totall;
809 rs_mag = sqrtf(1.f - fabsf(xr)) * ((yr + 1.f) * .5f) * mag_totalr;
811 dstl[2 * n ] = fl_mag * cosf(fl_phase);
812 dstl[2 * n + 1] = fl_mag * sinf(fl_phase);
814 dstr[2 * n ] = fr_mag * cosf(fr_phase);
815 dstr[2 * n + 1] = fr_mag * sinf(fr_phase);
818 dstc[2 * n + 1] = c_im;
820 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
821 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
823 dstlb[2 * n ] = lb_mag * cosf(bl_phase);
824 dstlb[2 * n + 1] = lb_mag * sinf(bl_phase);
826 dstrb[2 * n ] = rb_mag * cosf(br_phase);
827 dstrb[2 * n + 1] = rb_mag * sinf(br_phase);
829 dstls[2 * n ] = ls_mag * cosf(sl_phase);
830 dstls[2 * n + 1] = ls_mag * sinf(sl_phase);
832 dstrs[2 * n ] = rs_mag * cosf(sr_phase);
833 dstrs[2 * n + 1] = rs_mag * sinf(sr_phase);
836 static void upmix_7_1_5_1(AVFilterContext *ctx,
837 float c_re, float c_im,
838 float lfe_re, float lfe_im,
839 float mag_totall, float mag_totalr,
840 float fl_phase, float fr_phase,
841 float bl_phase, float br_phase,
842 float sl_phase, float sr_phase,
847 float fl_mag, fr_mag, ls_mag, rs_mag, lb_mag, rb_mag;
848 float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
849 AudioSurroundContext *s = ctx->priv;
851 dstl = (float *)s->output->extended_data[0];
852 dstr = (float *)s->output->extended_data[1];
853 dstc = (float *)s->output->extended_data[2];
854 dstlfe = (float *)s->output->extended_data[3];
855 dstlb = (float *)s->output->extended_data[4];
856 dstrb = (float *)s->output->extended_data[5];
857 dstls = (float *)s->output->extended_data[6];
858 dstrs = (float *)s->output->extended_data[7];
860 fl_mag = sqrtf(.5f * (xl + 1.f)) * ((yl + 1.f) * .5f) * mag_totall;
861 fr_mag = sqrtf(.5f * (xr + 1.f)) * ((yr + 1.f) * .5f) * mag_totalr;
862 lb_mag = sqrtf(.5f * (-xl + 1.f)) * ((yl + 1.f) * .5f) * mag_totall;
863 rb_mag = sqrtf(.5f * (-xr + 1.f)) * ((yr + 1.f) * .5f) * mag_totalr;
864 ls_mag = sqrtf(1.f - fabsf(xl)) * ((yl + 1.f) * .5f) * mag_totall;
865 rs_mag = sqrtf(1.f - fabsf(xr)) * ((yr + 1.f) * .5f) * mag_totalr;
867 dstl[2 * n ] = fl_mag * cosf(fl_phase);
868 dstl[2 * n + 1] = fl_mag * sinf(fl_phase);
870 dstr[2 * n ] = fr_mag * cosf(fr_phase);
871 dstr[2 * n + 1] = fr_mag * sinf(fr_phase);
874 dstc[2 * n + 1] = c_im;
876 dstlfe[2 * n ] = lfe_re;
877 dstlfe[2 * n + 1] = lfe_im;
879 dstlb[2 * n ] = lb_mag * cosf(bl_phase);
880 dstlb[2 * n + 1] = lb_mag * sinf(bl_phase);
882 dstrb[2 * n ] = rb_mag * cosf(br_phase);
883 dstrb[2 * n + 1] = rb_mag * sinf(br_phase);
885 dstls[2 * n ] = ls_mag * cosf(sl_phase);
886 dstls[2 * n + 1] = ls_mag * sinf(sl_phase);
888 dstrs[2 * n ] = rs_mag * cosf(sr_phase);
889 dstrs[2 * n + 1] = rs_mag * sinf(sr_phase);
892 static void filter_stereo(AVFilterContext *ctx)
894 AudioSurroundContext *s = ctx->priv;
898 srcl = (float *)s->input->extended_data[0];
899 srcr = (float *)s->input->extended_data[1];
901 for (n = 0; n < s->buf_size; n++) {
902 float l_re = srcl[2 * n], r_re = srcr[2 * n];
903 float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
904 float c_phase = atan2f(l_im + r_im, l_re + r_re);
905 float l_mag = hypotf(l_re, l_im);
906 float r_mag = hypotf(r_re, r_im);
907 float l_phase = atan2f(l_im, l_re);
908 float r_phase = atan2f(r_im, r_re);
909 float phase_dif = fabsf(l_phase - r_phase);
910 float mag_dif = (l_mag - r_mag) / (l_mag + r_mag);
911 float mag_total = hypotf(l_mag, r_mag);
914 if (phase_dif > M_PI)
915 phase_dif = 2 * M_PI - phase_dif;
917 stereo_position(mag_dif, phase_dif, &x, &y);
919 s->upmix_stereo(ctx, l_phase, r_phase, c_phase, mag_total, x, y, n);
923 static void filter_surround(AVFilterContext *ctx)
925 AudioSurroundContext *s = ctx->priv;
926 float *srcl, *srcr, *srcc;
929 srcl = (float *)s->input->extended_data[0];
930 srcr = (float *)s->input->extended_data[1];
931 srcc = (float *)s->input->extended_data[2];
933 for (n = 0; n < s->buf_size; n++) {
934 float l_re = srcl[2 * n], r_re = srcr[2 * n];
935 float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
936 float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
937 float c_mag = hypotf(c_re, c_im);
938 float c_phase = atan2f(c_im, c_re);
939 float l_mag = hypotf(l_re, l_im);
940 float r_mag = hypotf(r_re, r_im);
941 float l_phase = atan2f(l_im, l_re);
942 float r_phase = atan2f(r_im, r_re);
943 float phase_dif = fabsf(l_phase - r_phase);
944 float mag_dif = (l_mag - r_mag) / (l_mag + r_mag);
945 float mag_total = hypotf(l_mag, r_mag);
948 if (phase_dif > M_PI)
949 phase_dif = 2 * M_PI - phase_dif;
951 stereo_position(mag_dif, phase_dif, &x, &y);
953 s->upmix_3_0(ctx, l_phase, r_phase, c_phase, c_mag, mag_total, x, y, n);
957 static void filter_2_1(AVFilterContext *ctx)
959 AudioSurroundContext *s = ctx->priv;
960 float *srcl, *srcr, *srclfe;
963 srcl = (float *)s->input->extended_data[0];
964 srcr = (float *)s->input->extended_data[1];
965 srclfe = (float *)s->input->extended_data[2];
967 for (n = 0; n < s->buf_size; n++) {
968 float l_re = srcl[2 * n], r_re = srcr[2 * n];
969 float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
970 float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
971 float c_phase = atan2f(l_im + r_im, l_re + r_re);
972 float l_mag = hypotf(l_re, l_im);
973 float r_mag = hypotf(r_re, r_im);
974 float l_phase = atan2f(l_im, l_re);
975 float r_phase = atan2f(r_im, r_re);
976 float phase_dif = fabsf(l_phase - r_phase);
977 float mag_dif = (l_mag - r_mag) / (l_mag + r_mag);
978 float mag_total = hypotf(l_mag, r_mag);
981 if (phase_dif > M_PI)
982 phase_dif = 2 * M_PI - phase_dif;
984 stereo_position(mag_dif, phase_dif, &x, &y);
986 s->upmix_2_1(ctx, l_phase, r_phase, c_phase, mag_total, lfe_re, lfe_im, x, y, n);
990 static void filter_5_0_side(AVFilterContext *ctx)
992 AudioSurroundContext *s = ctx->priv;
993 float *srcl, *srcr, *srcc, *srcsl, *srcsr;
996 srcl = (float *)s->input->extended_data[0];
997 srcr = (float *)s->input->extended_data[1];
998 srcc = (float *)s->input->extended_data[2];
999 srcsl = (float *)s->input->extended_data[3];
1000 srcsr = (float *)s->input->extended_data[4];
1002 for (n = 0; n < s->buf_size; n++) {
1003 float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
1004 float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
1005 float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
1006 float sl_re = srcsl[2 * n], sl_im = srcsl[2 * n + 1];
1007 float sr_re = srcsr[2 * n], sr_im = srcsr[2 * n + 1];
1008 float fl_mag = hypotf(fl_re, fl_im);
1009 float fr_mag = hypotf(fr_re, fr_im);
1010 float fl_phase = atan2f(fl_im, fl_re);
1011 float fr_phase = atan2f(fr_im, fr_re);
1012 float sl_mag = hypotf(sl_re, sl_im);
1013 float sr_mag = hypotf(sr_re, sr_im);
1014 float sl_phase = atan2f(sl_im, sl_re);
1015 float sr_phase = atan2f(sr_im, sr_re);
1016 float phase_difl = fabsf(fl_phase - sl_phase);
1017 float phase_difr = fabsf(fr_phase - sr_phase);
1018 float mag_difl = (fl_mag - sl_mag) / (fl_mag + sl_mag);
1019 float mag_difr = (fr_mag - sr_mag) / (fr_mag + sr_mag);
1020 float mag_totall = hypotf(fl_mag, sl_mag);
1021 float mag_totalr = hypotf(fr_mag, sr_mag);
1022 float bl_phase = atan2f(fl_im + sl_im, fl_re + sl_re);
1023 float br_phase = atan2f(fr_im + sr_im, fr_re + sr_re);
1027 if (phase_difl > M_PI)
1028 phase_difl = 2 * M_PI - phase_difl;
1030 if (phase_difr > M_PI)
1031 phase_difr = 2 * M_PI - phase_difr;
1033 stereo_position(mag_difl, phase_difl, &xl, &yl);
1034 stereo_position(mag_difr, phase_difr, &xr, &yr);
1036 s->upmix_5_0(ctx, c_re, c_im,
1037 mag_totall, mag_totalr,
1045 static void filter_5_1_side(AVFilterContext *ctx)
1047 AudioSurroundContext *s = ctx->priv;
1048 float *srcl, *srcr, *srcc, *srclfe, *srcsl, *srcsr;
1051 srcl = (float *)s->input->extended_data[0];
1052 srcr = (float *)s->input->extended_data[1];
1053 srcc = (float *)s->input->extended_data[2];
1054 srclfe = (float *)s->input->extended_data[3];
1055 srcsl = (float *)s->input->extended_data[4];
1056 srcsr = (float *)s->input->extended_data[5];
1058 for (n = 0; n < s->buf_size; n++) {
1059 float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
1060 float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
1061 float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
1062 float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
1063 float sl_re = srcsl[2 * n], sl_im = srcsl[2 * n + 1];
1064 float sr_re = srcsr[2 * n], sr_im = srcsr[2 * n + 1];
1065 float fl_mag = hypotf(fl_re, fl_im);
1066 float fr_mag = hypotf(fr_re, fr_im);
1067 float fl_phase = atan2f(fl_im, fl_re);
1068 float fr_phase = atan2f(fr_im, fr_re);
1069 float sl_mag = hypotf(sl_re, sl_im);
1070 float sr_mag = hypotf(sr_re, sr_im);
1071 float sl_phase = atan2f(sl_im, sl_re);
1072 float sr_phase = atan2f(sr_im, sr_re);
1073 float phase_difl = fabsf(fl_phase - sl_phase);
1074 float phase_difr = fabsf(fr_phase - sr_phase);
1075 float mag_difl = (fl_mag - sl_mag) / (fl_mag + sl_mag);
1076 float mag_difr = (fr_mag - sr_mag) / (fr_mag + sr_mag);
1077 float mag_totall = hypotf(fl_mag, sl_mag);
1078 float mag_totalr = hypotf(fr_mag, sr_mag);
1079 float bl_phase = atan2f(fl_im + sl_im, fl_re + sl_re);
1080 float br_phase = atan2f(fr_im + sr_im, fr_re + sr_re);
1084 if (phase_difl > M_PI)
1085 phase_difl = 2 * M_PI - phase_difl;
1087 if (phase_difr > M_PI)
1088 phase_difr = 2 * M_PI - phase_difr;
1090 stereo_position(mag_difl, phase_difl, &xl, &yl);
1091 stereo_position(mag_difr, phase_difr, &xr, &yr);
1093 s->upmix_5_1(ctx, c_re, c_im, lfe_re, lfe_im,
1094 mag_totall, mag_totalr,
1102 static void filter_5_1_back(AVFilterContext *ctx)
1104 AudioSurroundContext *s = ctx->priv;
1105 float *srcl, *srcr, *srcc, *srclfe, *srcbl, *srcbr;
1108 srcl = (float *)s->input->extended_data[0];
1109 srcr = (float *)s->input->extended_data[1];
1110 srcc = (float *)s->input->extended_data[2];
1111 srclfe = (float *)s->input->extended_data[3];
1112 srcbl = (float *)s->input->extended_data[4];
1113 srcbr = (float *)s->input->extended_data[5];
1115 for (n = 0; n < s->buf_size; n++) {
1116 float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
1117 float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
1118 float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
1119 float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
1120 float bl_re = srcbl[2 * n], bl_im = srcbl[2 * n + 1];
1121 float br_re = srcbr[2 * n], br_im = srcbr[2 * n + 1];
1122 float fl_mag = hypotf(fl_re, fl_im);
1123 float fr_mag = hypotf(fr_re, fr_im);
1124 float fl_phase = atan2f(fl_im, fl_re);
1125 float fr_phase = atan2f(fr_im, fr_re);
1126 float bl_mag = hypotf(bl_re, bl_im);
1127 float br_mag = hypotf(br_re, br_im);
1128 float bl_phase = atan2f(bl_im, bl_re);
1129 float br_phase = atan2f(br_im, br_re);
1130 float phase_difl = fabsf(fl_phase - bl_phase);
1131 float phase_difr = fabsf(fr_phase - br_phase);
1132 float mag_difl = (fl_mag - bl_mag) / (fl_mag + bl_mag);
1133 float mag_difr = (fr_mag - br_mag) / (fr_mag + br_mag);
1134 float mag_totall = hypotf(fl_mag, bl_mag);
1135 float mag_totalr = hypotf(fr_mag, br_mag);
1136 float sl_phase = atan2f(fl_im + bl_im, fl_re + bl_re);
1137 float sr_phase = atan2f(fr_im + br_im, fr_re + br_re);
1141 if (phase_difl > M_PI)
1142 phase_difl = 2 * M_PI - phase_difl;
1144 if (phase_difr > M_PI)
1145 phase_difr = 2 * M_PI - phase_difr;
1147 stereo_position(mag_difl, phase_difl, &xl, &yl);
1148 stereo_position(mag_difr, phase_difr, &xr, &yr);
1150 s->upmix_5_1(ctx, c_re, c_im, lfe_re, lfe_im,
1151 mag_totall, mag_totalr,
1159 static int init(AVFilterContext *ctx)
1161 AudioSurroundContext *s = ctx->priv;
1165 if (!(s->out_channel_layout = av_get_channel_layout(s->out_channel_layout_str))) {
1166 av_log(ctx, AV_LOG_ERROR, "Error parsing output channel layout '%s'.\n",
1167 s->out_channel_layout_str);
1168 return AVERROR(EINVAL);
1171 if (!(s->in_channel_layout = av_get_channel_layout(s->in_channel_layout_str))) {
1172 av_log(ctx, AV_LOG_ERROR, "Error parsing input channel layout '%s'.\n",
1173 s->in_channel_layout_str);
1174 return AVERROR(EINVAL);
1177 if (s->lowcutf >= s->highcutf) {
1178 av_log(ctx, AV_LOG_ERROR, "Low cut-off '%d' should be less than high cut-off '%d'.\n",
1179 s->lowcutf, s->highcutf);
1180 return AVERROR(EINVAL);
1183 switch (s->in_channel_layout) {
1184 case AV_CH_LAYOUT_STEREO:
1185 s->filter = filter_stereo;
1186 switch (s->out_channel_layout) {
1187 case AV_CH_LAYOUT_MONO:
1188 s->upmix_stereo = upmix_1_0;
1190 case AV_CH_LAYOUT_STEREO:
1191 s->upmix_stereo = upmix_stereo;
1193 case AV_CH_LAYOUT_2POINT1:
1194 s->upmix_stereo = upmix_2_1;
1196 case AV_CH_LAYOUT_SURROUND:
1197 s->upmix_stereo = upmix_3_0;
1199 case AV_CH_LAYOUT_3POINT1:
1200 s->upmix_stereo = upmix_3_1;
1202 case AV_CH_LAYOUT_4POINT0:
1203 s->upmix_stereo = upmix_4_0;
1205 case AV_CH_LAYOUT_4POINT1:
1206 s->upmix_stereo = upmix_4_1;
1208 case AV_CH_LAYOUT_5POINT0_BACK:
1209 s->upmix_stereo = upmix_5_0_back;
1211 case AV_CH_LAYOUT_5POINT1_BACK:
1212 s->upmix_stereo = upmix_5_1_back;
1214 case AV_CH_LAYOUT_7POINT0:
1215 s->upmix_stereo = upmix_7_0;
1217 case AV_CH_LAYOUT_7POINT1:
1218 s->upmix_stereo = upmix_7_1;
1224 case AV_CH_LAYOUT_2POINT1:
1225 s->filter = filter_2_1;
1226 switch (s->out_channel_layout) {
1227 case AV_CH_LAYOUT_5POINT1_BACK:
1228 s->upmix_2_1 = upmix_5_1_back_2_1;
1234 case AV_CH_LAYOUT_SURROUND:
1235 s->filter = filter_surround;
1236 switch (s->out_channel_layout) {
1237 case AV_CH_LAYOUT_3POINT1:
1238 s->upmix_3_0 = upmix_3_1_surround;
1240 case AV_CH_LAYOUT_5POINT1_BACK:
1241 s->upmix_3_0 = upmix_5_1_back_surround;
1247 case AV_CH_LAYOUT_5POINT0:
1248 s->filter = filter_5_0_side;
1249 switch (s->out_channel_layout) {
1250 case AV_CH_LAYOUT_7POINT1:
1251 s->upmix_5_0 = upmix_7_1_5_0_side;
1257 case AV_CH_LAYOUT_5POINT1:
1258 s->filter = filter_5_1_side;
1259 switch (s->out_channel_layout) {
1260 case AV_CH_LAYOUT_7POINT1:
1261 s->upmix_5_1 = upmix_7_1_5_1;
1267 case AV_CH_LAYOUT_5POINT1_BACK:
1268 s->filter = filter_5_1_back;
1269 switch (s->out_channel_layout) {
1270 case AV_CH_LAYOUT_7POINT1:
1271 s->upmix_5_1 = upmix_7_1_5_1;
1279 av_log(ctx, AV_LOG_ERROR, "Unsupported upmix: '%s' -> '%s'.\n",
1280 s->in_channel_layout_str, s->out_channel_layout_str);
1281 return AVERROR(EINVAL);
1285 s->pts = AV_NOPTS_VALUE;
1287 s->window_func_lut = av_calloc(s->buf_size, sizeof(*s->window_func_lut));
1288 if (!s->window_func_lut)
1289 return AVERROR(ENOMEM);
1291 for (i = 0; i < s->buf_size; i++)
1292 s->window_func_lut[i] = sqrtf(0.5 * (1 - cosf(2 * M_PI * i / s->buf_size)) / s->buf_size);
1294 s->hop_size = s->buf_size * (1. - overlap);
1299 static int fft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
1301 AudioSurroundContext *s = ctx->priv;
1302 const float level_in = s->input_levels[ch];
1306 memset(s->input->extended_data[ch] + s->buf_size * sizeof(float), 0, s->buf_size * sizeof(float));
1308 dst = (float *)s->input->extended_data[ch];
1309 for (n = 0; n < s->buf_size; n++) {
1310 dst[n] *= s->window_func_lut[n] * level_in;
1313 av_rdft_calc(s->rdft[ch], (float *)s->input->extended_data[ch]);
1318 static int ifft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
1320 AudioSurroundContext *s = ctx->priv;
1321 const float level_out = s->output_levels[ch];
1326 av_rdft_calc(s->irdft[ch], (float *)s->output->extended_data[ch]);
1328 dst = (float *)s->output->extended_data[ch];
1329 ptr = (float *)s->overlap_buffer->extended_data[ch];
1331 memmove(s->overlap_buffer->extended_data[ch],
1332 s->overlap_buffer->extended_data[ch] + s->hop_size * sizeof(float),
1333 s->buf_size * sizeof(float));
1334 memset(s->overlap_buffer->extended_data[ch] + s->buf_size * sizeof(float),
1335 0, s->hop_size * sizeof(float));
1337 for (n = 0; n < s->buf_size; n++) {
1338 ptr[n] += dst[n] * s->window_func_lut[n] * level_out;
1341 ptr = (float *)s->overlap_buffer->extended_data[ch];
1342 dst = (float *)out->extended_data[ch];
1343 memcpy(dst, ptr, s->hop_size * sizeof(float));
1348 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
1350 AVFilterContext *ctx = inlink->dst;
1351 AVFilterLink *outlink = ctx->outputs[0];
1352 AudioSurroundContext *s = ctx->priv;
1355 ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
1357 if (ret >= 0 && s->pts == AV_NOPTS_VALUE)
1364 while (av_audio_fifo_size(s->fifo) >= s->buf_size) {
1367 ret = av_audio_fifo_peek(s->fifo, (void **)s->input->extended_data, s->buf_size);
1371 ctx->internal->execute(ctx, fft_channel, NULL, NULL, inlink->channels);
1375 out = ff_get_audio_buffer(outlink, s->hop_size);
1377 return AVERROR(ENOMEM);
1379 ctx->internal->execute(ctx, ifft_channel, out, NULL, outlink->channels);
1382 if (s->pts != AV_NOPTS_VALUE)
1383 s->pts += av_rescale_q(out->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
1384 av_audio_fifo_drain(s->fifo, s->hop_size);
1385 ret = ff_filter_frame(outlink, out);
1393 static int request_frame(AVFilterLink *outlink)
1395 AVFilterContext *ctx = outlink->src;
1396 AudioSurroundContext *s = ctx->priv;
1399 ret = ff_request_frame(ctx->inputs[0]);
1401 if (ret == AVERROR_EOF && av_audio_fifo_size(s->fifo) > 0 && av_audio_fifo_size(s->fifo) < s->buf_size) {
1404 in = ff_get_audio_buffer(outlink, s->buf_size - av_audio_fifo_size(s->fifo));
1406 return AVERROR(ENOMEM);
1407 ret = filter_frame(ctx->inputs[0], in);
1408 av_audio_fifo_drain(s->fifo, s->buf_size);
1414 static av_cold void uninit(AVFilterContext *ctx)
1416 AudioSurroundContext *s = ctx->priv;
1419 av_frame_free(&s->input);
1420 av_frame_free(&s->output);
1421 av_frame_free(&s->overlap_buffer);
1423 for (ch = 0; ch < s->nb_in_channels; ch++) {
1424 av_rdft_end(s->rdft[ch]);
1426 for (ch = 0; ch < s->nb_out_channels; ch++) {
1427 av_rdft_end(s->irdft[ch]);
1429 av_freep(&s->input_levels);
1430 av_freep(&s->output_levels);
1432 av_freep(&s->irdft);
1433 av_audio_fifo_free(s->fifo);
1434 av_freep(&s->window_func_lut);
1437 #define OFFSET(x) offsetof(AudioSurroundContext, x)
1438 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1440 static const AVOption surround_options[] = {
1441 { "chl_out", "set output channel layout", OFFSET(out_channel_layout_str), AV_OPT_TYPE_STRING, {.str="5.1"}, 0, 0, FLAGS },
1442 { "chl_in", "set input channel layout", OFFSET(in_channel_layout_str), AV_OPT_TYPE_STRING, {.str="stereo"},0, 0, FLAGS },
1443 { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1444 { "level_out", "set output level", OFFSET(level_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1445 { "lfe", "output LFE", OFFSET(output_lfe), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
1446 { "lfe_low", "LFE low cut off", OFFSET(lowcutf), AV_OPT_TYPE_INT, {.i64=128}, 0, 256, FLAGS },
1447 { "lfe_high", "LFE high cut off", OFFSET(highcutf), AV_OPT_TYPE_INT, {.i64=256}, 0, 512, FLAGS },
1448 { "fc_in", "set front center channel input level", OFFSET(fc_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1449 { "fc_out", "set front center channel output level", OFFSET(fc_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1450 { "lfe_in", "set lfe channel input level", OFFSET(lfe_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1451 { "lfe_out", "set lfe channel output level", OFFSET(lfe_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1455 AVFILTER_DEFINE_CLASS(surround);
1457 static const AVFilterPad inputs[] = {
1460 .type = AVMEDIA_TYPE_AUDIO,
1461 .filter_frame = filter_frame,
1462 .config_props = config_input,
1467 static const AVFilterPad outputs[] = {
1470 .type = AVMEDIA_TYPE_AUDIO,
1471 .request_frame = request_frame,
1472 .config_props = config_output,
1477 AVFilter ff_af_surround = {
1479 .description = NULL_IF_CONFIG_SMALL("Apply audio surround upmix filter."),
1480 .query_formats = query_formats,
1481 .priv_size = sizeof(AudioSurroundContext),
1482 .priv_class = &surround_class,
1487 .flags = AVFILTER_FLAG_SLICE_THREADS,