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