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;
54 uint64_t out_channel_layout;
55 uint64_t in_channel_layout;
61 AVFrame *overlap_buffer;
66 RDFTContext **rdft, **irdft;
67 float *window_func_lut;
71 void (*filter)(AVFilterContext *ctx);
72 void (*upmix_stereo)(AVFilterContext *ctx,
79 void (*upmix_2_1)(AVFilterContext *ctx,
88 void (*upmix_3_0)(AVFilterContext *ctx,
96 void (*upmix_5_0)(AVFilterContext *ctx,
97 float c_re, float c_im,
98 float mag_totall, float mag_totalr,
99 float fl_phase, float fr_phase,
100 float bl_phase, float br_phase,
101 float sl_phase, float sr_phase,
105 void (*upmix_5_1)(AVFilterContext *ctx,
106 float c_re, float c_im,
107 float lfe_re, float lfe_im,
108 float mag_totall, float mag_totalr,
109 float fl_phase, float fr_phase,
110 float bl_phase, float br_phase,
111 float sl_phase, float sr_phase,
115 } AudioSurroundContext;
117 static int query_formats(AVFilterContext *ctx)
119 AudioSurroundContext *s = ctx->priv;
120 AVFilterFormats *formats = NULL;
121 AVFilterChannelLayouts *layouts = NULL;
124 ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLTP);
127 ret = ff_set_common_formats(ctx, formats);
132 ret = ff_add_channel_layout(&layouts, s->out_channel_layout);
136 ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
141 ret = ff_add_channel_layout(&layouts, s->in_channel_layout);
145 ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts);
149 formats = ff_all_samplerates();
151 return AVERROR(ENOMEM);
152 return ff_set_common_samplerates(ctx, formats);
155 static int config_input(AVFilterLink *inlink)
157 AVFilterContext *ctx = inlink->dst;
158 AudioSurroundContext *s = ctx->priv;
161 s->rdft = av_calloc(inlink->channels, sizeof(*s->rdft));
163 return AVERROR(ENOMEM);
165 for (ch = 0; ch < inlink->channels; ch++) {
166 s->rdft[ch] = av_rdft_init(ff_log2(s->buf_size), DFT_R2C);
168 return AVERROR(ENOMEM);
170 s->nb_in_channels = inlink->channels;
171 s->input_levels = av_malloc_array(s->nb_in_channels, sizeof(*s->input_levels));
172 if (!s->input_levels)
173 return AVERROR(ENOMEM);
174 for (ch = 0; ch < s->nb_in_channels; ch++)
175 s->input_levels[ch] = s->level_in;
176 ch = av_get_channel_layout_channel_index(inlink->channel_layout, AV_CH_FRONT_CENTER);
178 s->input_levels[ch] *= s->fc_in;
179 ch = av_get_channel_layout_channel_index(inlink->channel_layout, AV_CH_LOW_FREQUENCY);
181 s->input_levels[ch] *= s->lfe_in;
183 s->input = ff_get_audio_buffer(inlink, s->buf_size * 2);
185 return AVERROR(ENOMEM);
187 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->buf_size);
189 return AVERROR(ENOMEM);
191 s->lowcut = 1.f * s->lowcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
192 s->highcut = 1.f * s->highcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
197 static int config_output(AVFilterLink *outlink)
199 AVFilterContext *ctx = outlink->src;
200 AudioSurroundContext *s = ctx->priv;
203 s->irdft = av_calloc(outlink->channels, sizeof(*s->irdft));
205 return AVERROR(ENOMEM);
207 for (ch = 0; ch < outlink->channels; ch++) {
208 s->irdft[ch] = av_rdft_init(ff_log2(s->buf_size), IDFT_C2R);
210 return AVERROR(ENOMEM);
212 s->nb_out_channels = outlink->channels;
213 s->output_levels = av_malloc_array(s->nb_out_channels, sizeof(*s->output_levels));
214 if (!s->output_levels)
215 return AVERROR(ENOMEM);
216 for (ch = 0; ch < s->nb_out_channels; ch++)
217 s->output_levels[ch] = s->level_out;
218 ch = av_get_channel_layout_channel_index(outlink->channel_layout, AV_CH_FRONT_CENTER);
220 s->output_levels[ch] *= s->fc_out;
221 ch = av_get_channel_layout_channel_index(outlink->channel_layout, AV_CH_LOW_FREQUENCY);
223 s->output_levels[ch] *= s->lfe_out;
225 s->output = ff_get_audio_buffer(outlink, s->buf_size * 2);
226 s->overlap_buffer = ff_get_audio_buffer(outlink, s->buf_size * 2);
227 if (!s->overlap_buffer || !s->output)
228 return AVERROR(ENOMEM);
233 static void stereo_position(float a, float p, float *x, float *y)
235 *x = av_clipf(a+FFMAX(0, sinf(p-M_PI_2))*FFDIFFSIGN(a,0), -1, 1);
236 *y = av_clipf(cosf(a*M_PI_2+M_PI)*cosf(M_PI_2-p/M_PI)*M_LN10+1, -1, 1);
239 static inline void get_lfe(int output_lfe, int n, float lowcut, float highcut,
240 float *lfe_mag, float *mag_total)
242 if (output_lfe && n < highcut) {
243 *lfe_mag = n < lowcut ? 1.f : .5f*(1.f+cosf(M_PI*(lowcut-n)/(lowcut-highcut)));
244 *lfe_mag *= *mag_total;
245 *mag_total -= *lfe_mag;
251 static void upmix_1_0(AVFilterContext *ctx,
259 AudioSurroundContext *s = ctx->priv;
262 dst = (float *)s->output->extended_data[0];
264 mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
266 dst[2 * n ] = mag * cosf(c_phase);
267 dst[2 * n + 1] = mag * sinf(c_phase);
270 static void upmix_stereo(AVFilterContext *ctx,
278 AudioSurroundContext *s = ctx->priv;
279 float l_mag, r_mag, *dstl, *dstr;
281 dstl = (float *)s->output->extended_data[0];
282 dstr = (float *)s->output->extended_data[1];
284 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
285 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
287 dstl[2 * n ] = l_mag * cosf(l_phase);
288 dstl[2 * n + 1] = l_mag * sinf(l_phase);
290 dstr[2 * n ] = r_mag * cosf(r_phase);
291 dstr[2 * n + 1] = r_mag * sinf(r_phase);
294 static void upmix_2_1(AVFilterContext *ctx,
302 AudioSurroundContext *s = ctx->priv;
303 float lfe_mag, l_mag, r_mag, *dstl, *dstr, *dstlfe;
305 dstl = (float *)s->output->extended_data[0];
306 dstr = (float *)s->output->extended_data[1];
307 dstlfe = (float *)s->output->extended_data[2];
309 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
311 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
312 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
314 dstl[2 * n ] = l_mag * cosf(l_phase);
315 dstl[2 * n + 1] = l_mag * sinf(l_phase);
317 dstr[2 * n ] = r_mag * cosf(r_phase);
318 dstr[2 * n + 1] = r_mag * sinf(r_phase);
320 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
321 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
324 static void upmix_3_0(AVFilterContext *ctx,
332 AudioSurroundContext *s = ctx->priv;
333 float l_mag, r_mag, c_mag, *dstc, *dstl, *dstr;
335 dstl = (float *)s->output->extended_data[0];
336 dstr = (float *)s->output->extended_data[1];
337 dstc = (float *)s->output->extended_data[2];
339 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
340 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
341 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
343 dstl[2 * n ] = l_mag * cosf(l_phase);
344 dstl[2 * n + 1] = l_mag * sinf(l_phase);
346 dstr[2 * n ] = r_mag * cosf(r_phase);
347 dstr[2 * n + 1] = r_mag * sinf(r_phase);
349 dstc[2 * n ] = c_mag * cosf(c_phase);
350 dstc[2 * n + 1] = c_mag * sinf(c_phase);
353 static void upmix_3_1(AVFilterContext *ctx,
361 AudioSurroundContext *s = ctx->priv;
362 float lfe_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstlfe;
364 dstl = (float *)s->output->extended_data[0];
365 dstr = (float *)s->output->extended_data[1];
366 dstc = (float *)s->output->extended_data[2];
367 dstlfe = (float *)s->output->extended_data[3];
369 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
371 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
372 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
373 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
375 dstl[2 * n ] = l_mag * cosf(l_phase);
376 dstl[2 * n + 1] = l_mag * sinf(l_phase);
378 dstr[2 * n ] = r_mag * cosf(r_phase);
379 dstr[2 * n + 1] = r_mag * sinf(r_phase);
381 dstc[2 * n ] = c_mag * cosf(c_phase);
382 dstc[2 * n + 1] = c_mag * sinf(c_phase);
384 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
385 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
388 static void upmix_3_1_surround(AVFilterContext *ctx,
397 AudioSurroundContext *s = ctx->priv;
398 float lfe_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
400 dstl = (float *)s->output->extended_data[0];
401 dstr = (float *)s->output->extended_data[1];
402 dstc = (float *)s->output->extended_data[2];
403 dstlfe = (float *)s->output->extended_data[3];
405 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &c_mag);
407 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
408 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
410 dstl[2 * n ] = l_mag * cosf(l_phase);
411 dstl[2 * n + 1] = l_mag * sinf(l_phase);
413 dstr[2 * n ] = r_mag * cosf(r_phase);
414 dstr[2 * n + 1] = r_mag * sinf(r_phase);
416 dstc[2 * n ] = c_mag * cosf(c_phase);
417 dstc[2 * n + 1] = c_mag * sinf(c_phase);
419 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
420 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
423 static void upmix_4_0(AVFilterContext *ctx,
431 float b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb;
432 AudioSurroundContext *s = ctx->priv;
434 dstl = (float *)s->output->extended_data[0];
435 dstr = (float *)s->output->extended_data[1];
436 dstc = (float *)s->output->extended_data[2];
437 dstb = (float *)s->output->extended_data[3];
439 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
440 b_mag = sqrtf(1.f - fabsf(x)) * ((1.f - y) * .5f) * mag_total;
441 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
442 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
444 dstl[2 * n ] = l_mag * cosf(l_phase);
445 dstl[2 * n + 1] = l_mag * sinf(l_phase);
447 dstr[2 * n ] = r_mag * cosf(r_phase);
448 dstr[2 * n + 1] = r_mag * sinf(r_phase);
450 dstc[2 * n ] = c_mag * cosf(c_phase);
451 dstc[2 * n + 1] = c_mag * sinf(c_phase);
453 dstb[2 * n ] = b_mag * cosf(c_phase);
454 dstb[2 * n + 1] = b_mag * sinf(c_phase);
457 static void upmix_4_1(AVFilterContext *ctx,
465 float lfe_mag, b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb, *dstlfe;
466 AudioSurroundContext *s = ctx->priv;
468 dstl = (float *)s->output->extended_data[0];
469 dstr = (float *)s->output->extended_data[1];
470 dstc = (float *)s->output->extended_data[2];
471 dstlfe = (float *)s->output->extended_data[3];
472 dstb = (float *)s->output->extended_data[4];
474 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
476 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
477 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
479 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
480 b_mag = sqrtf(1.f - fabsf(x)) * ((1.f - y) * .5f) * mag_total;
481 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
482 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
484 dstl[2 * n ] = l_mag * cosf(l_phase);
485 dstl[2 * n + 1] = l_mag * sinf(l_phase);
487 dstr[2 * n ] = r_mag * cosf(r_phase);
488 dstr[2 * n + 1] = r_mag * sinf(r_phase);
490 dstc[2 * n ] = c_mag * cosf(c_phase);
491 dstc[2 * n + 1] = c_mag * sinf(c_phase);
493 dstb[2 * n ] = b_mag * cosf(c_phase);
494 dstb[2 * n + 1] = b_mag * sinf(c_phase);
497 static void upmix_5_0_back(AVFilterContext *ctx,
505 float l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs;
506 AudioSurroundContext *s = ctx->priv;
508 dstl = (float *)s->output->extended_data[0];
509 dstr = (float *)s->output->extended_data[1];
510 dstc = (float *)s->output->extended_data[2];
511 dstls = (float *)s->output->extended_data[3];
512 dstrs = (float *)s->output->extended_data[4];
514 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
515 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
516 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
517 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
518 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
520 dstl[2 * n ] = l_mag * cosf(l_phase);
521 dstl[2 * n + 1] = l_mag * sinf(l_phase);
523 dstr[2 * n ] = r_mag * cosf(r_phase);
524 dstr[2 * n + 1] = r_mag * sinf(r_phase);
526 dstc[2 * n ] = c_mag * cosf(c_phase);
527 dstc[2 * n + 1] = c_mag * sinf(c_phase);
529 dstls[2 * n ] = ls_mag * cosf(l_phase);
530 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
532 dstrs[2 * n ] = rs_mag * cosf(r_phase);
533 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
536 static void upmix_5_1_back(AVFilterContext *ctx,
544 float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlfe;
545 AudioSurroundContext *s = ctx->priv;
547 dstl = (float *)s->output->extended_data[0];
548 dstr = (float *)s->output->extended_data[1];
549 dstc = (float *)s->output->extended_data[2];
550 dstlfe = (float *)s->output->extended_data[3];
551 dstls = (float *)s->output->extended_data[4];
552 dstrs = (float *)s->output->extended_data[5];
554 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
556 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
557 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
558 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
559 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
560 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
562 dstl[2 * n ] = l_mag * cosf(l_phase);
563 dstl[2 * n + 1] = l_mag * sinf(l_phase);
565 dstr[2 * n ] = r_mag * cosf(r_phase);
566 dstr[2 * n + 1] = r_mag * sinf(r_phase);
568 dstc[2 * n ] = c_mag * cosf(c_phase);
569 dstc[2 * n + 1] = c_mag * sinf(c_phase);
571 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
572 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
574 dstls[2 * n ] = ls_mag * cosf(l_phase);
575 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
577 dstrs[2 * n ] = rs_mag * cosf(r_phase);
578 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
581 static void upmix_5_1_back_surround(AVFilterContext *ctx,
590 AudioSurroundContext *s = ctx->priv;
591 float lfe_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
592 float ls_mag, rs_mag, *dstls, *dstrs;
594 dstl = (float *)s->output->extended_data[0];
595 dstr = (float *)s->output->extended_data[1];
596 dstc = (float *)s->output->extended_data[2];
597 dstlfe = (float *)s->output->extended_data[3];
598 dstls = (float *)s->output->extended_data[4];
599 dstrs = (float *)s->output->extended_data[5];
601 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &c_mag);
603 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
604 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
605 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
606 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
608 dstl[2 * n ] = l_mag * cosf(l_phase);
609 dstl[2 * n + 1] = l_mag * sinf(l_phase);
611 dstr[2 * n ] = r_mag * cosf(r_phase);
612 dstr[2 * n + 1] = r_mag * sinf(r_phase);
614 dstc[2 * n ] = c_mag * cosf(c_phase);
615 dstc[2 * n + 1] = c_mag * sinf(c_phase);
617 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
618 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
620 dstls[2 * n ] = ls_mag * cosf(l_phase);
621 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
623 dstrs[2 * n ] = rs_mag * cosf(r_phase);
624 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
627 static void upmix_5_1_back_2_1(AVFilterContext *ctx,
637 AudioSurroundContext *s = ctx->priv;
638 float c_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
639 float ls_mag, rs_mag, *dstls, *dstrs;
641 dstl = (float *)s->output->extended_data[0];
642 dstr = (float *)s->output->extended_data[1];
643 dstc = (float *)s->output->extended_data[2];
644 dstlfe = (float *)s->output->extended_data[3];
645 dstls = (float *)s->output->extended_data[4];
646 dstrs = (float *)s->output->extended_data[5];
648 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
649 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
650 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
651 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
652 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
654 dstl[2 * n ] = l_mag * cosf(l_phase);
655 dstl[2 * n + 1] = l_mag * sinf(l_phase);
657 dstr[2 * n ] = r_mag * cosf(r_phase);
658 dstr[2 * n + 1] = r_mag * sinf(r_phase);
660 dstc[2 * n ] = c_mag * cosf(c_phase);
661 dstc[2 * n + 1] = c_mag * sinf(c_phase);
663 dstlfe[2 * n ] = lfe_re;
664 dstlfe[2 * n + 1] = lfe_im;
666 dstls[2 * n ] = ls_mag * cosf(l_phase);
667 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
669 dstrs[2 * n ] = rs_mag * cosf(r_phase);
670 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
673 static void upmix_7_0(AVFilterContext *ctx,
681 float l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
682 float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb;
683 AudioSurroundContext *s = ctx->priv;
685 dstl = (float *)s->output->extended_data[0];
686 dstr = (float *)s->output->extended_data[1];
687 dstc = (float *)s->output->extended_data[2];
688 dstlb = (float *)s->output->extended_data[3];
689 dstrb = (float *)s->output->extended_data[4];
690 dstls = (float *)s->output->extended_data[5];
691 dstrs = (float *)s->output->extended_data[6];
693 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
694 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
695 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
696 lb_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
697 rb_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
698 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - fabsf(y)) * mag_total;
699 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - fabsf(y)) * mag_total;
701 dstl[2 * n ] = l_mag * cosf(l_phase);
702 dstl[2 * n + 1] = l_mag * sinf(l_phase);
704 dstr[2 * n ] = r_mag * cosf(r_phase);
705 dstr[2 * n + 1] = r_mag * sinf(r_phase);
707 dstc[2 * n ] = c_mag * cosf(c_phase);
708 dstc[2 * n + 1] = c_mag * sinf(c_phase);
710 dstlb[2 * n ] = lb_mag * cosf(l_phase);
711 dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
713 dstrb[2 * n ] = rb_mag * cosf(r_phase);
714 dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
716 dstls[2 * n ] = ls_mag * cosf(l_phase);
717 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
719 dstrs[2 * n ] = rs_mag * cosf(r_phase);
720 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
723 static void upmix_7_1(AVFilterContext *ctx,
731 float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
732 float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
733 AudioSurroundContext *s = ctx->priv;
735 dstl = (float *)s->output->extended_data[0];
736 dstr = (float *)s->output->extended_data[1];
737 dstc = (float *)s->output->extended_data[2];
738 dstlfe = (float *)s->output->extended_data[3];
739 dstlb = (float *)s->output->extended_data[4];
740 dstrb = (float *)s->output->extended_data[5];
741 dstls = (float *)s->output->extended_data[6];
742 dstrs = (float *)s->output->extended_data[7];
744 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
746 c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
747 l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
748 r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
749 lb_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
750 rb_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
751 ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - fabsf(y)) * mag_total;
752 rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - fabsf(y)) * mag_total;
754 dstl[2 * n ] = l_mag * cosf(l_phase);
755 dstl[2 * n + 1] = l_mag * sinf(l_phase);
757 dstr[2 * n ] = r_mag * cosf(r_phase);
758 dstr[2 * n + 1] = r_mag * sinf(r_phase);
760 dstc[2 * n ] = c_mag * cosf(c_phase);
761 dstc[2 * n + 1] = c_mag * sinf(c_phase);
763 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
764 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
766 dstlb[2 * n ] = lb_mag * cosf(l_phase);
767 dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
769 dstrb[2 * n ] = rb_mag * cosf(r_phase);
770 dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
772 dstls[2 * n ] = ls_mag * cosf(l_phase);
773 dstls[2 * n + 1] = ls_mag * sinf(l_phase);
775 dstrs[2 * n ] = rs_mag * cosf(r_phase);
776 dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
779 static void upmix_7_1_5_0_side(AVFilterContext *ctx,
780 float c_re, float c_im,
781 float mag_totall, float mag_totalr,
782 float fl_phase, float fr_phase,
783 float bl_phase, float br_phase,
784 float sl_phase, float sr_phase,
789 float fl_mag, fr_mag, ls_mag, rs_mag, lb_mag, rb_mag;
790 float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
791 float lfe_mag, c_phase, mag_total = (mag_totall + mag_totalr) * 0.5;
792 AudioSurroundContext *s = ctx->priv;
794 dstl = (float *)s->output->extended_data[0];
795 dstr = (float *)s->output->extended_data[1];
796 dstc = (float *)s->output->extended_data[2];
797 dstlfe = (float *)s->output->extended_data[3];
798 dstlb = (float *)s->output->extended_data[4];
799 dstrb = (float *)s->output->extended_data[5];
800 dstls = (float *)s->output->extended_data[6];
801 dstrs = (float *)s->output->extended_data[7];
803 c_phase = atan2f(c_im, c_re);
805 get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
807 fl_mag = sqrtf(.5f * (xl + 1.f)) * ((yl + 1.f) * .5f) * mag_totall;
808 fr_mag = sqrtf(.5f * (xr + 1.f)) * ((yr + 1.f) * .5f) * mag_totalr;
809 lb_mag = sqrtf(.5f * (-xl + 1.f)) * ((yl + 1.f) * .5f) * mag_totall;
810 rb_mag = sqrtf(.5f * (-xr + 1.f)) * ((yr + 1.f) * .5f) * mag_totalr;
811 ls_mag = sqrtf(1.f - fabsf(xl)) * ((yl + 1.f) * .5f) * mag_totall;
812 rs_mag = sqrtf(1.f - fabsf(xr)) * ((yr + 1.f) * .5f) * mag_totalr;
814 dstl[2 * n ] = fl_mag * cosf(fl_phase);
815 dstl[2 * n + 1] = fl_mag * sinf(fl_phase);
817 dstr[2 * n ] = fr_mag * cosf(fr_phase);
818 dstr[2 * n + 1] = fr_mag * sinf(fr_phase);
821 dstc[2 * n + 1] = c_im;
823 dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
824 dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
826 dstlb[2 * n ] = lb_mag * cosf(bl_phase);
827 dstlb[2 * n + 1] = lb_mag * sinf(bl_phase);
829 dstrb[2 * n ] = rb_mag * cosf(br_phase);
830 dstrb[2 * n + 1] = rb_mag * sinf(br_phase);
832 dstls[2 * n ] = ls_mag * cosf(sl_phase);
833 dstls[2 * n + 1] = ls_mag * sinf(sl_phase);
835 dstrs[2 * n ] = rs_mag * cosf(sr_phase);
836 dstrs[2 * n + 1] = rs_mag * sinf(sr_phase);
839 static void upmix_7_1_5_1(AVFilterContext *ctx,
840 float c_re, float c_im,
841 float lfe_re, float lfe_im,
842 float mag_totall, float mag_totalr,
843 float fl_phase, float fr_phase,
844 float bl_phase, float br_phase,
845 float sl_phase, float sr_phase,
850 float fl_mag, fr_mag, ls_mag, rs_mag, lb_mag, rb_mag;
851 float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
852 AudioSurroundContext *s = ctx->priv;
854 dstl = (float *)s->output->extended_data[0];
855 dstr = (float *)s->output->extended_data[1];
856 dstc = (float *)s->output->extended_data[2];
857 dstlfe = (float *)s->output->extended_data[3];
858 dstlb = (float *)s->output->extended_data[4];
859 dstrb = (float *)s->output->extended_data[5];
860 dstls = (float *)s->output->extended_data[6];
861 dstrs = (float *)s->output->extended_data[7];
863 fl_mag = sqrtf(.5f * (xl + 1.f)) * ((yl + 1.f) * .5f) * mag_totall;
864 fr_mag = sqrtf(.5f * (xr + 1.f)) * ((yr + 1.f) * .5f) * mag_totalr;
865 lb_mag = sqrtf(.5f * (-xl + 1.f)) * ((yl + 1.f) * .5f) * mag_totall;
866 rb_mag = sqrtf(.5f * (-xr + 1.f)) * ((yr + 1.f) * .5f) * mag_totalr;
867 ls_mag = sqrtf(1.f - fabsf(xl)) * ((yl + 1.f) * .5f) * mag_totall;
868 rs_mag = sqrtf(1.f - fabsf(xr)) * ((yr + 1.f) * .5f) * mag_totalr;
870 dstl[2 * n ] = fl_mag * cosf(fl_phase);
871 dstl[2 * n + 1] = fl_mag * sinf(fl_phase);
873 dstr[2 * n ] = fr_mag * cosf(fr_phase);
874 dstr[2 * n + 1] = fr_mag * sinf(fr_phase);
877 dstc[2 * n + 1] = c_im;
879 dstlfe[2 * n ] = lfe_re;
880 dstlfe[2 * n + 1] = lfe_im;
882 dstlb[2 * n ] = lb_mag * cosf(bl_phase);
883 dstlb[2 * n + 1] = lb_mag * sinf(bl_phase);
885 dstrb[2 * n ] = rb_mag * cosf(br_phase);
886 dstrb[2 * n + 1] = rb_mag * sinf(br_phase);
888 dstls[2 * n ] = ls_mag * cosf(sl_phase);
889 dstls[2 * n + 1] = ls_mag * sinf(sl_phase);
891 dstrs[2 * n ] = rs_mag * cosf(sr_phase);
892 dstrs[2 * n + 1] = rs_mag * sinf(sr_phase);
895 static void filter_stereo(AVFilterContext *ctx)
897 AudioSurroundContext *s = ctx->priv;
901 srcl = (float *)s->input->extended_data[0];
902 srcr = (float *)s->input->extended_data[1];
904 for (n = 0; n < s->buf_size; n++) {
905 float l_re = srcl[2 * n], r_re = srcr[2 * n];
906 float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
907 float c_phase = atan2f(l_im + r_im, l_re + r_re);
908 float l_mag = hypotf(l_re, l_im);
909 float r_mag = hypotf(r_re, r_im);
910 float l_phase = atan2f(l_im, l_re);
911 float r_phase = atan2f(r_im, r_re);
912 float phase_dif = fabsf(l_phase - r_phase);
913 float mag_sum = l_mag + r_mag;
914 float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum;
915 float mag_total = hypotf(l_mag, r_mag);
918 if (phase_dif > M_PI)
919 phase_dif = 2 * M_PI - phase_dif;
921 stereo_position(mag_dif, phase_dif, &x, &y);
923 s->upmix_stereo(ctx, l_phase, r_phase, c_phase, mag_total, x, y, n);
927 static void filter_surround(AVFilterContext *ctx)
929 AudioSurroundContext *s = ctx->priv;
930 float *srcl, *srcr, *srcc;
933 srcl = (float *)s->input->extended_data[0];
934 srcr = (float *)s->input->extended_data[1];
935 srcc = (float *)s->input->extended_data[2];
937 for (n = 0; n < s->buf_size; n++) {
938 float l_re = srcl[2 * n], r_re = srcr[2 * n];
939 float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
940 float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
941 float c_mag = hypotf(c_re, c_im);
942 float c_phase = atan2f(c_im, c_re);
943 float l_mag = hypotf(l_re, l_im);
944 float r_mag = hypotf(r_re, r_im);
945 float l_phase = atan2f(l_im, l_re);
946 float r_phase = atan2f(r_im, r_re);
947 float phase_dif = fabsf(l_phase - r_phase);
948 float mag_sum = l_mag + r_mag;
949 float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum;
950 float mag_total = hypotf(l_mag, r_mag);
953 if (phase_dif > M_PI)
954 phase_dif = 2 * M_PI - phase_dif;
956 stereo_position(mag_dif, phase_dif, &x, &y);
958 s->upmix_3_0(ctx, l_phase, r_phase, c_phase, c_mag, mag_total, x, y, n);
962 static void filter_2_1(AVFilterContext *ctx)
964 AudioSurroundContext *s = ctx->priv;
965 float *srcl, *srcr, *srclfe;
968 srcl = (float *)s->input->extended_data[0];
969 srcr = (float *)s->input->extended_data[1];
970 srclfe = (float *)s->input->extended_data[2];
972 for (n = 0; n < s->buf_size; n++) {
973 float l_re = srcl[2 * n], r_re = srcr[2 * n];
974 float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
975 float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
976 float c_phase = atan2f(l_im + r_im, l_re + r_re);
977 float l_mag = hypotf(l_re, l_im);
978 float r_mag = hypotf(r_re, r_im);
979 float l_phase = atan2f(l_im, l_re);
980 float r_phase = atan2f(r_im, r_re);
981 float phase_dif = fabsf(l_phase - r_phase);
982 float mag_sum = l_mag + r_mag;
983 float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum;
984 float mag_total = hypotf(l_mag, r_mag);
987 if (phase_dif > M_PI)
988 phase_dif = 2 * M_PI - phase_dif;
990 stereo_position(mag_dif, phase_dif, &x, &y);
992 s->upmix_2_1(ctx, l_phase, r_phase, c_phase, mag_total, lfe_re, lfe_im, x, y, n);
996 static void filter_5_0_side(AVFilterContext *ctx)
998 AudioSurroundContext *s = ctx->priv;
999 float *srcl, *srcr, *srcc, *srcsl, *srcsr;
1002 srcl = (float *)s->input->extended_data[0];
1003 srcr = (float *)s->input->extended_data[1];
1004 srcc = (float *)s->input->extended_data[2];
1005 srcsl = (float *)s->input->extended_data[3];
1006 srcsr = (float *)s->input->extended_data[4];
1008 for (n = 0; n < s->buf_size; n++) {
1009 float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
1010 float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
1011 float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
1012 float sl_re = srcsl[2 * n], sl_im = srcsl[2 * n + 1];
1013 float sr_re = srcsr[2 * n], sr_im = srcsr[2 * n + 1];
1014 float fl_mag = hypotf(fl_re, fl_im);
1015 float fr_mag = hypotf(fr_re, fr_im);
1016 float fl_phase = atan2f(fl_im, fl_re);
1017 float fr_phase = atan2f(fr_im, fr_re);
1018 float sl_mag = hypotf(sl_re, sl_im);
1019 float sr_mag = hypotf(sr_re, sr_im);
1020 float sl_phase = atan2f(sl_im, sl_re);
1021 float sr_phase = atan2f(sr_im, sr_re);
1022 float phase_difl = fabsf(fl_phase - sl_phase);
1023 float phase_difr = fabsf(fr_phase - sr_phase);
1024 float magl_sum = fl_mag + sl_mag;
1025 float magr_sum = fr_mag + sr_mag;
1026 float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - sl_mag) / magl_sum;
1027 float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - sr_mag) / magr_sum;
1028 float mag_totall = hypotf(fl_mag, sl_mag);
1029 float mag_totalr = hypotf(fr_mag, sr_mag);
1030 float bl_phase = atan2f(fl_im + sl_im, fl_re + sl_re);
1031 float br_phase = atan2f(fr_im + sr_im, fr_re + sr_re);
1035 if (phase_difl > M_PI)
1036 phase_difl = 2 * M_PI - phase_difl;
1038 if (phase_difr > M_PI)
1039 phase_difr = 2 * M_PI - phase_difr;
1041 stereo_position(mag_difl, phase_difl, &xl, &yl);
1042 stereo_position(mag_difr, phase_difr, &xr, &yr);
1044 s->upmix_5_0(ctx, c_re, c_im,
1045 mag_totall, mag_totalr,
1053 static void filter_5_1_side(AVFilterContext *ctx)
1055 AudioSurroundContext *s = ctx->priv;
1056 float *srcl, *srcr, *srcc, *srclfe, *srcsl, *srcsr;
1059 srcl = (float *)s->input->extended_data[0];
1060 srcr = (float *)s->input->extended_data[1];
1061 srcc = (float *)s->input->extended_data[2];
1062 srclfe = (float *)s->input->extended_data[3];
1063 srcsl = (float *)s->input->extended_data[4];
1064 srcsr = (float *)s->input->extended_data[5];
1066 for (n = 0; n < s->buf_size; n++) {
1067 float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
1068 float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
1069 float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
1070 float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
1071 float sl_re = srcsl[2 * n], sl_im = srcsl[2 * n + 1];
1072 float sr_re = srcsr[2 * n], sr_im = srcsr[2 * n + 1];
1073 float fl_mag = hypotf(fl_re, fl_im);
1074 float fr_mag = hypotf(fr_re, fr_im);
1075 float fl_phase = atan2f(fl_im, fl_re);
1076 float fr_phase = atan2f(fr_im, fr_re);
1077 float sl_mag = hypotf(sl_re, sl_im);
1078 float sr_mag = hypotf(sr_re, sr_im);
1079 float sl_phase = atan2f(sl_im, sl_re);
1080 float sr_phase = atan2f(sr_im, sr_re);
1081 float phase_difl = fabsf(fl_phase - sl_phase);
1082 float phase_difr = fabsf(fr_phase - sr_phase);
1083 float magl_sum = fl_mag + sl_mag;
1084 float magr_sum = fr_mag + sr_mag;
1085 float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - sl_mag) / magl_sum;
1086 float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - sr_mag) / magr_sum;
1087 float mag_totall = hypotf(fl_mag, sl_mag);
1088 float mag_totalr = hypotf(fr_mag, sr_mag);
1089 float bl_phase = atan2f(fl_im + sl_im, fl_re + sl_re);
1090 float br_phase = atan2f(fr_im + sr_im, fr_re + sr_re);
1094 if (phase_difl > M_PI)
1095 phase_difl = 2 * M_PI - phase_difl;
1097 if (phase_difr > M_PI)
1098 phase_difr = 2 * M_PI - phase_difr;
1100 stereo_position(mag_difl, phase_difl, &xl, &yl);
1101 stereo_position(mag_difr, phase_difr, &xr, &yr);
1103 s->upmix_5_1(ctx, c_re, c_im, lfe_re, lfe_im,
1104 mag_totall, mag_totalr,
1112 static void filter_5_1_back(AVFilterContext *ctx)
1114 AudioSurroundContext *s = ctx->priv;
1115 float *srcl, *srcr, *srcc, *srclfe, *srcbl, *srcbr;
1118 srcl = (float *)s->input->extended_data[0];
1119 srcr = (float *)s->input->extended_data[1];
1120 srcc = (float *)s->input->extended_data[2];
1121 srclfe = (float *)s->input->extended_data[3];
1122 srcbl = (float *)s->input->extended_data[4];
1123 srcbr = (float *)s->input->extended_data[5];
1125 for (n = 0; n < s->buf_size; n++) {
1126 float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
1127 float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
1128 float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
1129 float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
1130 float bl_re = srcbl[2 * n], bl_im = srcbl[2 * n + 1];
1131 float br_re = srcbr[2 * n], br_im = srcbr[2 * n + 1];
1132 float fl_mag = hypotf(fl_re, fl_im);
1133 float fr_mag = hypotf(fr_re, fr_im);
1134 float fl_phase = atan2f(fl_im, fl_re);
1135 float fr_phase = atan2f(fr_im, fr_re);
1136 float bl_mag = hypotf(bl_re, bl_im);
1137 float br_mag = hypotf(br_re, br_im);
1138 float bl_phase = atan2f(bl_im, bl_re);
1139 float br_phase = atan2f(br_im, br_re);
1140 float phase_difl = fabsf(fl_phase - bl_phase);
1141 float phase_difr = fabsf(fr_phase - br_phase);
1142 float magl_sum = fl_mag + bl_mag;
1143 float magr_sum = fr_mag + br_mag;
1144 float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - bl_mag) / magl_sum;
1145 float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - br_mag) / magr_sum;
1146 float mag_totall = hypotf(fl_mag, bl_mag);
1147 float mag_totalr = hypotf(fr_mag, br_mag);
1148 float sl_phase = atan2f(fl_im + bl_im, fl_re + bl_re);
1149 float sr_phase = atan2f(fr_im + br_im, fr_re + br_re);
1153 if (phase_difl > M_PI)
1154 phase_difl = 2 * M_PI - phase_difl;
1156 if (phase_difr > M_PI)
1157 phase_difr = 2 * M_PI - phase_difr;
1159 stereo_position(mag_difl, phase_difl, &xl, &yl);
1160 stereo_position(mag_difr, phase_difr, &xr, &yr);
1162 s->upmix_5_1(ctx, c_re, c_im, lfe_re, lfe_im,
1163 mag_totall, mag_totalr,
1171 static int init(AVFilterContext *ctx)
1173 AudioSurroundContext *s = ctx->priv;
1177 if (!(s->out_channel_layout = av_get_channel_layout(s->out_channel_layout_str))) {
1178 av_log(ctx, AV_LOG_ERROR, "Error parsing output channel layout '%s'.\n",
1179 s->out_channel_layout_str);
1180 return AVERROR(EINVAL);
1183 if (!(s->in_channel_layout = av_get_channel_layout(s->in_channel_layout_str))) {
1184 av_log(ctx, AV_LOG_ERROR, "Error parsing input channel layout '%s'.\n",
1185 s->in_channel_layout_str);
1186 return AVERROR(EINVAL);
1189 if (s->lowcutf >= s->highcutf) {
1190 av_log(ctx, AV_LOG_ERROR, "Low cut-off '%d' should be less than high cut-off '%d'.\n",
1191 s->lowcutf, s->highcutf);
1192 return AVERROR(EINVAL);
1195 switch (s->in_channel_layout) {
1196 case AV_CH_LAYOUT_STEREO:
1197 s->filter = filter_stereo;
1198 switch (s->out_channel_layout) {
1199 case AV_CH_LAYOUT_MONO:
1200 s->upmix_stereo = upmix_1_0;
1202 case AV_CH_LAYOUT_STEREO:
1203 s->upmix_stereo = upmix_stereo;
1205 case AV_CH_LAYOUT_2POINT1:
1206 s->upmix_stereo = upmix_2_1;
1208 case AV_CH_LAYOUT_SURROUND:
1209 s->upmix_stereo = upmix_3_0;
1211 case AV_CH_LAYOUT_3POINT1:
1212 s->upmix_stereo = upmix_3_1;
1214 case AV_CH_LAYOUT_4POINT0:
1215 s->upmix_stereo = upmix_4_0;
1217 case AV_CH_LAYOUT_4POINT1:
1218 s->upmix_stereo = upmix_4_1;
1220 case AV_CH_LAYOUT_5POINT0_BACK:
1221 s->upmix_stereo = upmix_5_0_back;
1223 case AV_CH_LAYOUT_5POINT1_BACK:
1224 s->upmix_stereo = upmix_5_1_back;
1226 case AV_CH_LAYOUT_7POINT0:
1227 s->upmix_stereo = upmix_7_0;
1229 case AV_CH_LAYOUT_7POINT1:
1230 s->upmix_stereo = upmix_7_1;
1236 case AV_CH_LAYOUT_2POINT1:
1237 s->filter = filter_2_1;
1238 switch (s->out_channel_layout) {
1239 case AV_CH_LAYOUT_5POINT1_BACK:
1240 s->upmix_2_1 = upmix_5_1_back_2_1;
1246 case AV_CH_LAYOUT_SURROUND:
1247 s->filter = filter_surround;
1248 switch (s->out_channel_layout) {
1249 case AV_CH_LAYOUT_3POINT1:
1250 s->upmix_3_0 = upmix_3_1_surround;
1252 case AV_CH_LAYOUT_5POINT1_BACK:
1253 s->upmix_3_0 = upmix_5_1_back_surround;
1259 case AV_CH_LAYOUT_5POINT0:
1260 s->filter = filter_5_0_side;
1261 switch (s->out_channel_layout) {
1262 case AV_CH_LAYOUT_7POINT1:
1263 s->upmix_5_0 = upmix_7_1_5_0_side;
1269 case AV_CH_LAYOUT_5POINT1:
1270 s->filter = filter_5_1_side;
1271 switch (s->out_channel_layout) {
1272 case AV_CH_LAYOUT_7POINT1:
1273 s->upmix_5_1 = upmix_7_1_5_1;
1279 case AV_CH_LAYOUT_5POINT1_BACK:
1280 s->filter = filter_5_1_back;
1281 switch (s->out_channel_layout) {
1282 case AV_CH_LAYOUT_7POINT1:
1283 s->upmix_5_1 = upmix_7_1_5_1;
1291 av_log(ctx, AV_LOG_ERROR, "Unsupported upmix: '%s' -> '%s'.\n",
1292 s->in_channel_layout_str, s->out_channel_layout_str);
1293 return AVERROR(EINVAL);
1297 s->pts = AV_NOPTS_VALUE;
1299 s->window_func_lut = av_calloc(s->buf_size, sizeof(*s->window_func_lut));
1300 if (!s->window_func_lut)
1301 return AVERROR(ENOMEM);
1303 generate_window_func(s->window_func_lut, s->buf_size, s->win_func, &overlap);
1304 if (s->overlap == 1)
1305 s->overlap = overlap;
1307 for (i = 0; i < s->buf_size; i++)
1308 s->window_func_lut[i] = sqrtf(s->window_func_lut[i] / s->buf_size);
1309 s->hop_size = s->buf_size * (1. - s->overlap);
1310 if (s->hop_size <= 0)
1311 return AVERROR(EINVAL);
1316 static int fft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
1318 AudioSurroundContext *s = ctx->priv;
1319 const float level_in = s->input_levels[ch];
1323 memset(s->input->extended_data[ch] + s->buf_size * sizeof(float), 0, s->buf_size * sizeof(float));
1325 dst = (float *)s->input->extended_data[ch];
1326 for (n = 0; n < s->buf_size; n++) {
1327 dst[n] *= s->window_func_lut[n] * level_in;
1330 av_rdft_calc(s->rdft[ch], (float *)s->input->extended_data[ch]);
1335 static int ifft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
1337 AudioSurroundContext *s = ctx->priv;
1338 const float level_out = s->output_levels[ch];
1343 av_rdft_calc(s->irdft[ch], (float *)s->output->extended_data[ch]);
1345 dst = (float *)s->output->extended_data[ch];
1346 ptr = (float *)s->overlap_buffer->extended_data[ch];
1348 memmove(s->overlap_buffer->extended_data[ch],
1349 s->overlap_buffer->extended_data[ch] + s->hop_size * sizeof(float),
1350 s->buf_size * sizeof(float));
1351 memset(s->overlap_buffer->extended_data[ch] + s->buf_size * sizeof(float),
1352 0, s->hop_size * sizeof(float));
1354 for (n = 0; n < s->buf_size; n++) {
1355 ptr[n] += dst[n] * s->window_func_lut[n] * level_out;
1358 ptr = (float *)s->overlap_buffer->extended_data[ch];
1359 dst = (float *)out->extended_data[ch];
1360 memcpy(dst, ptr, s->hop_size * sizeof(float));
1365 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
1367 AVFilterContext *ctx = inlink->dst;
1368 AVFilterLink *outlink = ctx->outputs[0];
1369 AudioSurroundContext *s = ctx->priv;
1372 ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
1374 if (ret >= 0 && s->pts == AV_NOPTS_VALUE)
1381 while (av_audio_fifo_size(s->fifo) >= s->buf_size) {
1384 ret = av_audio_fifo_peek(s->fifo, (void **)s->input->extended_data, s->buf_size);
1388 ctx->internal->execute(ctx, fft_channel, NULL, NULL, inlink->channels);
1392 out = ff_get_audio_buffer(outlink, s->hop_size);
1394 return AVERROR(ENOMEM);
1396 ctx->internal->execute(ctx, ifft_channel, out, NULL, outlink->channels);
1399 if (s->pts != AV_NOPTS_VALUE)
1400 s->pts += av_rescale_q(out->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
1401 av_audio_fifo_drain(s->fifo, s->hop_size);
1402 ret = ff_filter_frame(outlink, out);
1410 static int request_frame(AVFilterLink *outlink)
1412 AVFilterContext *ctx = outlink->src;
1413 AudioSurroundContext *s = ctx->priv;
1416 ret = ff_request_frame(ctx->inputs[0]);
1418 if (ret == AVERROR_EOF && av_audio_fifo_size(s->fifo) > 0 && av_audio_fifo_size(s->fifo) < s->buf_size) {
1421 in = ff_get_audio_buffer(outlink, s->buf_size - av_audio_fifo_size(s->fifo));
1423 return AVERROR(ENOMEM);
1424 ret = filter_frame(ctx->inputs[0], in);
1425 av_audio_fifo_drain(s->fifo, s->buf_size);
1431 static av_cold void uninit(AVFilterContext *ctx)
1433 AudioSurroundContext *s = ctx->priv;
1436 av_frame_free(&s->input);
1437 av_frame_free(&s->output);
1438 av_frame_free(&s->overlap_buffer);
1440 for (ch = 0; ch < s->nb_in_channels; ch++) {
1441 av_rdft_end(s->rdft[ch]);
1443 for (ch = 0; ch < s->nb_out_channels; ch++) {
1444 av_rdft_end(s->irdft[ch]);
1446 av_freep(&s->input_levels);
1447 av_freep(&s->output_levels);
1449 av_freep(&s->irdft);
1450 av_audio_fifo_free(s->fifo);
1451 av_freep(&s->window_func_lut);
1454 #define OFFSET(x) offsetof(AudioSurroundContext, x)
1455 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1457 static const AVOption surround_options[] = {
1458 { "chl_out", "set output channel layout", OFFSET(out_channel_layout_str), AV_OPT_TYPE_STRING, {.str="5.1"}, 0, 0, FLAGS },
1459 { "chl_in", "set input channel layout", OFFSET(in_channel_layout_str), AV_OPT_TYPE_STRING, {.str="stereo"},0, 0, FLAGS },
1460 { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1461 { "level_out", "set output level", OFFSET(level_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1462 { "lfe", "output LFE", OFFSET(output_lfe), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
1463 { "lfe_low", "LFE low cut off", OFFSET(lowcutf), AV_OPT_TYPE_INT, {.i64=128}, 0, 256, FLAGS },
1464 { "lfe_high", "LFE high cut off", OFFSET(highcutf), AV_OPT_TYPE_INT, {.i64=256}, 0, 512, FLAGS },
1465 { "fc_in", "set front center channel input level", OFFSET(fc_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1466 { "fc_out", "set front center channel output level", OFFSET(fc_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1467 { "lfe_in", "set lfe channel input level", OFFSET(lfe_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1468 { "lfe_out", "set lfe channel output level", OFFSET(lfe_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
1469 { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
1470 { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
1471 { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
1472 { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
1473 { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
1474 { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
1475 { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
1476 { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
1477 { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
1478 { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
1479 { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
1480 { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
1481 { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
1482 { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
1483 { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
1484 { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
1485 { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
1486 { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
1487 { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
1488 { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
1489 { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
1490 { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
1491 { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
1495 AVFILTER_DEFINE_CLASS(surround);
1497 static const AVFilterPad inputs[] = {
1500 .type = AVMEDIA_TYPE_AUDIO,
1501 .filter_frame = filter_frame,
1502 .config_props = config_input,
1507 static const AVFilterPad outputs[] = {
1510 .type = AVMEDIA_TYPE_AUDIO,
1511 .request_frame = request_frame,
1512 .config_props = config_output,
1517 AVFilter ff_af_surround = {
1519 .description = NULL_IF_CONFIG_SMALL("Apply audio surround upmix filter."),
1520 .query_formats = query_formats,
1521 .priv_size = sizeof(AudioSurroundContext),
1522 .priv_class = &surround_class,
1527 .flags = AVFILTER_FLAG_SLICE_THREADS,