Browse code

avfilter: add audio surround upmixer

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2017/05/20 03:12:04
Showing 6 changed files
... ...
@@ -16,6 +16,7 @@ version <next>:
16 16
 - spec compliant VP9 muxing support in MP4
17 17
 - remove the libnut muxer/demuxer wrappers
18 18
 - remove the libschroedinger encoder/decoder wrappers
19
+- surround audio filter
19 20
 
20 21
 version 3.3:
21 22
 - CrystalHD decoder moved to new decode API
... ...
@@ -3792,6 +3792,36 @@ channels. Default is 0.3.
3792 3792
 Set level of input signal of original channel. Default is 0.8.
3793 3793
 @end table
3794 3794
 
3795
+@section surround
3796
+Apply audio surround upmix filter.
3797
+
3798
+This filter allows to produce multichannel output from stereo audio stream.
3799
+
3800
+The filter accepts the following options:
3801
+
3802
+@table @option
3803
+@item chl_out
3804
+Set output channel layout. By default, this is @var{5.1}.
3805
+
3806
+See @ref{channel layout syntax,,the Channel Layout section in the ffmpeg-utils(1) manual,ffmpeg-utils}
3807
+for the required syntax.
3808
+
3809
+@item level_in
3810
+Set input volume level. By default, this is @var{1}.
3811
+
3812
+@item level_out
3813
+Set output volume level. By default, this is @var{1}.
3814
+
3815
+@item lfe
3816
+Enable LFE channel output if output channel layout has it. By default, this is enabled.
3817
+
3818
+@item lfe_low
3819
+Set LFE low cut off frequency. By default, this is @var{128} Hz.
3820
+
3821
+@item lfe_high
3822
+Set LFE high cut off frequency. By default, this is @var{256} Hz.
3823
+@end table
3824
+
3795 3825
 @section treble
3796 3826
 
3797 3827
 Boost or cut treble (upper) frequencies of the audio using a two-pole
... ...
@@ -108,6 +108,7 @@ OBJS-$(CONFIG_SILENCEREMOVE_FILTER)          += af_silenceremove.o
108 108
 OBJS-$(CONFIG_SOFALIZER_FILTER)              += af_sofalizer.o
109 109
 OBJS-$(CONFIG_STEREOTOOLS_FILTER)            += af_stereotools.o
110 110
 OBJS-$(CONFIG_STEREOWIDEN_FILTER)            += af_stereowiden.o
111
+OBJS-$(CONFIG_SURROUND_FILTER)               += af_surround.o
111 112
 OBJS-$(CONFIG_TREBLE_FILTER)                 += af_biquads.o
112 113
 OBJS-$(CONFIG_TREMOLO_FILTER)                += af_tremolo.o
113 114
 OBJS-$(CONFIG_VIBRATO_FILTER)                += af_vibrato.o generate_wave_table.o
114 115
new file mode 100644
... ...
@@ -0,0 +1,835 @@
0
+/*
1
+ * Copyright (c) 2017 Paul B Mahol
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "libavutil/audio_fifo.h"
21
+#include "libavutil/channel_layout.h"
22
+#include "libavutil/opt.h"
23
+#include "libavcodec/avfft.h"
24
+#include "avfilter.h"
25
+#include "audio.h"
26
+#include "formats.h"
27
+
28
+typedef struct AudioSurroundContext {
29
+    const AVClass *class;
30
+
31
+    char *out_channel_layout_str;
32
+    float level_in;
33
+    float level_out;
34
+    int output_lfe;
35
+    int lowcutf;
36
+    int highcutf;
37
+
38
+    float lowcut;
39
+    float highcut;
40
+
41
+    uint64_t out_channel_layout;
42
+    int nb_in_channels;
43
+    int nb_out_channels;
44
+
45
+    AVFrame *input;
46
+    AVFrame *output;
47
+    AVFrame *overlap_buffer;
48
+
49
+    int buf_size;
50
+    int hop_size;
51
+    AVAudioFifo *fifo;
52
+    RDFTContext **rdft, **irdft;
53
+    float *window_func_lut;
54
+
55
+    int64_t pts;
56
+
57
+    void (*upmix)(AVFilterContext *ctx,
58
+                  float l_phase,
59
+                  float r_phase,
60
+                  float c_phase,
61
+                  float mag_total,
62
+                  float x, float y,
63
+                  int n);
64
+} AudioSurroundContext;
65
+
66
+static int query_formats(AVFilterContext *ctx)
67
+{
68
+    AudioSurroundContext *s = ctx->priv;
69
+    AVFilterFormats *formats = NULL;
70
+    AVFilterChannelLayouts *layouts = NULL;
71
+    int ret;
72
+
73
+    ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLTP);
74
+    if (ret)
75
+        return ret;
76
+    ret = ff_set_common_formats(ctx, formats);
77
+    if (ret)
78
+        return ret;
79
+
80
+    layouts = NULL;
81
+    ret = ff_add_channel_layout(&layouts, s->out_channel_layout);
82
+    if (ret)
83
+        return ret;
84
+
85
+    ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
86
+    if (ret)
87
+        return ret;
88
+
89
+    layouts = NULL;
90
+    ret = ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO);
91
+    if (ret)
92
+        return ret;
93
+
94
+    ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts);
95
+    if (ret)
96
+        return ret;
97
+
98
+    formats = ff_all_samplerates();
99
+    if (!formats)
100
+        return AVERROR(ENOMEM);
101
+    return ff_set_common_samplerates(ctx, formats);
102
+}
103
+
104
+static int config_input(AVFilterLink *inlink)
105
+{
106
+    AVFilterContext *ctx = inlink->dst;
107
+    AudioSurroundContext *s = ctx->priv;
108
+    int ch;
109
+
110
+    s->rdft = av_calloc(inlink->channels, sizeof(*s->rdft));
111
+    if (!s->rdft)
112
+        return AVERROR(ENOMEM);
113
+
114
+    for (ch = 0; ch < inlink->channels; ch++) {
115
+        s->rdft[ch]  = av_rdft_init(ff_log2(s->buf_size), DFT_R2C);
116
+        if (!s->rdft[ch])
117
+            return AVERROR(ENOMEM);
118
+    }
119
+    s->nb_in_channels = inlink->channels;
120
+
121
+    s->input = ff_get_audio_buffer(inlink, s->buf_size * 2);
122
+    if (!s->input)
123
+        return AVERROR(ENOMEM);
124
+
125
+    s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->buf_size);
126
+    if (!s->fifo)
127
+        return AVERROR(ENOMEM);
128
+
129
+    s->lowcut = 1.f * s->lowcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
130
+    s->highcut = 1.f * s->highcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
131
+
132
+    return 0;
133
+}
134
+
135
+static int config_output(AVFilterLink *outlink)
136
+{
137
+    AVFilterContext *ctx = outlink->src;
138
+    AudioSurroundContext *s = ctx->priv;
139
+    int ch;
140
+
141
+    s->irdft = av_calloc(outlink->channels, sizeof(*s->irdft));
142
+    if (!s->irdft)
143
+        return AVERROR(ENOMEM);
144
+
145
+    for (ch = 0; ch < outlink->channels; ch++) {
146
+        s->irdft[ch] = av_rdft_init(ff_log2(s->buf_size), IDFT_C2R);
147
+        if (!s->irdft[ch])
148
+            return AVERROR(ENOMEM);
149
+    }
150
+    s->nb_out_channels = outlink->channels;
151
+
152
+    s->output = ff_get_audio_buffer(outlink, s->buf_size * 2);
153
+    s->overlap_buffer = ff_get_audio_buffer(outlink, s->buf_size * 2);
154
+    if (!s->overlap_buffer || !s->output)
155
+        return AVERROR(ENOMEM);
156
+
157
+    return 0;
158
+}
159
+
160
+static void stereo_position(float a, float p, float *x, float *y)
161
+{
162
+      *x = av_clipf(a+FFMAX(0, sinf(p-M_PI_2))*FFDIFFSIGN(a,0), -1, 1);
163
+      *y = av_clipf(cosf(a*M_PI_2+M_PI)*cosf(M_PI_2-p/M_PI)*M_LN10+1, -1, 1);
164
+}
165
+
166
+static inline void get_lfe(int output_lfe, int n, float lowcut, float highcut,
167
+                           float *lfe_mag, float *mag_total)
168
+{
169
+    if (output_lfe && n < highcut) {
170
+        *lfe_mag    = n < lowcut ? 1.f : .5f*(1.f+cosf(M_PI*(lowcut-n)/(lowcut-highcut)));
171
+        *lfe_mag   *= *mag_total;
172
+        *mag_total -= *lfe_mag;
173
+    } else {
174
+        *lfe_mag = 0.f;
175
+    }
176
+}
177
+
178
+static void upmix_1_0(AVFilterContext *ctx,
179
+                      float l_phase,
180
+                      float r_phase,
181
+                      float c_phase,
182
+                      float mag_total,
183
+                      float x, float y,
184
+                      int n)
185
+{
186
+    AudioSurroundContext *s = ctx->priv;
187
+    float mag, *dst;
188
+
189
+    dst = (float *)s->output->extended_data[0];
190
+
191
+    mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
192
+
193
+    dst[2 * n    ] = mag * cosf(c_phase);
194
+    dst[2 * n + 1] = mag * sinf(c_phase);
195
+}
196
+
197
+static void upmix_stereo(AVFilterContext *ctx,
198
+                         float l_phase,
199
+                         float r_phase,
200
+                         float c_phase,
201
+                         float mag_total,
202
+                         float x, float y,
203
+                         int n)
204
+{
205
+    AudioSurroundContext *s = ctx->priv;
206
+    float l_mag, r_mag, *dstl, *dstr;
207
+
208
+    dstl = (float *)s->output->extended_data[0];
209
+    dstr = (float *)s->output->extended_data[1];
210
+
211
+    l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
212
+    r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
213
+
214
+    dstl[2 * n    ] = l_mag * cosf(l_phase);
215
+    dstl[2 * n + 1] = l_mag * sinf(l_phase);
216
+
217
+    dstr[2 * n    ] = r_mag * cosf(r_phase);
218
+    dstr[2 * n + 1] = r_mag * sinf(r_phase);
219
+}
220
+
221
+static void upmix_2_1(AVFilterContext *ctx,
222
+                      float l_phase,
223
+                      float r_phase,
224
+                      float c_phase,
225
+                      float mag_total,
226
+                      float x, float y,
227
+                      int n)
228
+{
229
+    AudioSurroundContext *s = ctx->priv;
230
+    float lfe_mag, l_mag, r_mag, *dstl, *dstr, *dstlfe;
231
+
232
+    dstl = (float *)s->output->extended_data[0];
233
+    dstr = (float *)s->output->extended_data[1];
234
+    dstlfe = (float *)s->output->extended_data[2];
235
+
236
+    get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
237
+
238
+    l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
239
+    r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
240
+
241
+    dstl[2 * n    ] = l_mag * cosf(l_phase);
242
+    dstl[2 * n + 1] = l_mag * sinf(l_phase);
243
+
244
+    dstr[2 * n    ] = r_mag * cosf(r_phase);
245
+    dstr[2 * n + 1] = r_mag * sinf(r_phase);
246
+
247
+    dstlfe[2 * n    ] = lfe_mag * cosf(c_phase);
248
+    dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
249
+}
250
+
251
+static void upmix_3_0(AVFilterContext *ctx,
252
+                      float l_phase,
253
+                      float r_phase,
254
+                      float c_phase,
255
+                      float mag_total,
256
+                      float x, float y,
257
+                      int n)
258
+{
259
+    AudioSurroundContext *s = ctx->priv;
260
+    float l_mag, r_mag, c_mag, *dstc, *dstl, *dstr;
261
+
262
+    dstl = (float *)s->output->extended_data[0];
263
+    dstr = (float *)s->output->extended_data[1];
264
+    dstc = (float *)s->output->extended_data[2];
265
+
266
+    c_mag = sqrtf(1.f - fabsf(x))   * ((y + 1.f) * .5f) * mag_total;
267
+    l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
268
+    r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
269
+
270
+    dstl[2 * n    ] = l_mag * cosf(l_phase);
271
+    dstl[2 * n + 1] = l_mag * sinf(l_phase);
272
+
273
+    dstr[2 * n    ] = r_mag * cosf(r_phase);
274
+    dstr[2 * n + 1] = r_mag * sinf(r_phase);
275
+
276
+    dstc[2 * n    ] = c_mag * cosf(c_phase);
277
+    dstc[2 * n + 1] = c_mag * sinf(c_phase);
278
+}
279
+
280
+static void upmix_3_1(AVFilterContext *ctx,
281
+                      float l_phase,
282
+                      float r_phase,
283
+                      float c_phase,
284
+                      float mag_total,
285
+                      float x, float y,
286
+                      int n)
287
+{
288
+    AudioSurroundContext *s = ctx->priv;
289
+    float lfe_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstlfe;
290
+
291
+    dstl = (float *)s->output->extended_data[0];
292
+    dstr = (float *)s->output->extended_data[1];
293
+    dstc = (float *)s->output->extended_data[2];
294
+    dstlfe = (float *)s->output->extended_data[3];
295
+
296
+    get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
297
+
298
+    c_mag = sqrtf(1.f - fabsf(x))   * ((y + 1.f) * .5f) * mag_total;
299
+    l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
300
+    r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
301
+
302
+    dstl[2 * n    ] = l_mag * cosf(l_phase);
303
+    dstl[2 * n + 1] = l_mag * sinf(l_phase);
304
+
305
+    dstr[2 * n    ] = r_mag * cosf(r_phase);
306
+    dstr[2 * n + 1] = r_mag * sinf(r_phase);
307
+
308
+    dstc[2 * n    ] = c_mag * cosf(c_phase);
309
+    dstc[2 * n + 1] = c_mag * sinf(c_phase);
310
+
311
+    dstlfe[2 * n    ] = lfe_mag * cosf(c_phase);
312
+    dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
313
+}
314
+
315
+static void upmix_4_0(AVFilterContext *ctx,
316
+                      float l_phase,
317
+                      float r_phase,
318
+                      float c_phase,
319
+                      float mag_total,
320
+                      float x, float y,
321
+                      int n)
322
+{
323
+    float b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb;
324
+    AudioSurroundContext *s = ctx->priv;
325
+
326
+    dstl = (float *)s->output->extended_data[0];
327
+    dstr = (float *)s->output->extended_data[1];
328
+    dstc = (float *)s->output->extended_data[2];
329
+    dstb = (float *)s->output->extended_data[3];
330
+
331
+    c_mag = sqrtf(1.f - fabsf(x))   * ((y + 1.f) * .5f) * mag_total;
332
+    b_mag = sqrtf(1.f - fabsf(x))   * ((1.f - y) * .5f) * mag_total;
333
+    l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
334
+    r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
335
+
336
+    dstl[2 * n    ] = l_mag * cosf(l_phase);
337
+    dstl[2 * n + 1] = l_mag * sinf(l_phase);
338
+
339
+    dstr[2 * n    ] = r_mag * cosf(r_phase);
340
+    dstr[2 * n + 1] = r_mag * sinf(r_phase);
341
+
342
+    dstc[2 * n    ] = c_mag * cosf(c_phase);
343
+    dstc[2 * n + 1] = c_mag * sinf(c_phase);
344
+
345
+    dstb[2 * n    ] = b_mag * cosf(c_phase);
346
+    dstb[2 * n + 1] = b_mag * sinf(c_phase);
347
+}
348
+
349
+static void upmix_4_1(AVFilterContext *ctx,
350
+                      float l_phase,
351
+                      float r_phase,
352
+                      float c_phase,
353
+                      float mag_total,
354
+                      float x, float y,
355
+                      int n)
356
+{
357
+    float lfe_mag, b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb, *dstlfe;
358
+    AudioSurroundContext *s = ctx->priv;
359
+
360
+    dstl = (float *)s->output->extended_data[0];
361
+    dstr = (float *)s->output->extended_data[1];
362
+    dstc = (float *)s->output->extended_data[2];
363
+    dstlfe = (float *)s->output->extended_data[3];
364
+    dstb = (float *)s->output->extended_data[4];
365
+
366
+    get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
367
+
368
+    dstlfe[2 * n    ] = lfe_mag * cosf(c_phase);
369
+    dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
370
+
371
+    c_mag = sqrtf(1.f - fabsf(x))   * ((y + 1.f) * .5f) * mag_total;
372
+    b_mag = sqrtf(1.f - fabsf(x))   * ((1.f - y) * .5f) * mag_total;
373
+    l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
374
+    r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
375
+
376
+    dstl[2 * n    ] = l_mag * cosf(l_phase);
377
+    dstl[2 * n + 1] = l_mag * sinf(l_phase);
378
+
379
+    dstr[2 * n    ] = r_mag * cosf(r_phase);
380
+    dstr[2 * n + 1] = r_mag * sinf(r_phase);
381
+
382
+    dstc[2 * n    ] = c_mag * cosf(c_phase);
383
+    dstc[2 * n + 1] = c_mag * sinf(c_phase);
384
+
385
+    dstb[2 * n    ] = b_mag * cosf(c_phase);
386
+    dstb[2 * n + 1] = b_mag * sinf(c_phase);
387
+}
388
+
389
+static void upmix_5_0_back(AVFilterContext *ctx,
390
+                           float l_phase,
391
+                           float r_phase,
392
+                           float c_phase,
393
+                           float mag_total,
394
+                           float x, float y,
395
+                           int n)
396
+{
397
+    float l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs;
398
+    AudioSurroundContext *s = ctx->priv;
399
+
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
+    dstls = (float *)s->output->extended_data[3];
404
+    dstrs = (float *)s->output->extended_data[4];
405
+
406
+    c_mag  = sqrtf(1.f - fabsf(x))   * ((y + 1.f) * .5f) * mag_total;
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;
409
+    ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
410
+    rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
411
+
412
+    dstl[2 * n    ] = l_mag * cosf(l_phase);
413
+    dstl[2 * n + 1] = l_mag * sinf(l_phase);
414
+
415
+    dstr[2 * n    ] = r_mag * cosf(r_phase);
416
+    dstr[2 * n + 1] = r_mag * sinf(r_phase);
417
+
418
+    dstc[2 * n    ] = c_mag * cosf(c_phase);
419
+    dstc[2 * n + 1] = c_mag * sinf(c_phase);
420
+
421
+    dstls[2 * n    ] = ls_mag * cosf(l_phase);
422
+    dstls[2 * n + 1] = ls_mag * sinf(l_phase);
423
+
424
+    dstrs[2 * n    ] = rs_mag * cosf(r_phase);
425
+    dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
426
+}
427
+
428
+static void upmix_5_1_back(AVFilterContext *ctx,
429
+                           float l_phase,
430
+                           float r_phase,
431
+                           float c_phase,
432
+                           float mag_total,
433
+                           float x, float y,
434
+                           int n)
435
+{
436
+    float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlfe;
437
+    AudioSurroundContext *s = ctx->priv;
438
+
439
+    dstl  = (float *)s->output->extended_data[0];
440
+    dstr  = (float *)s->output->extended_data[1];
441
+    dstc  = (float *)s->output->extended_data[2];
442
+    dstlfe = (float *)s->output->extended_data[3];
443
+    dstls = (float *)s->output->extended_data[4];
444
+    dstrs = (float *)s->output->extended_data[5];
445
+
446
+    get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
447
+
448
+    c_mag  = sqrtf(1.f - fabsf(x))   * ((y + 1.f) * .5f) * mag_total;
449
+    l_mag  = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
450
+    r_mag  = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
451
+    ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
452
+    rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
453
+
454
+    dstl[2 * n    ] = l_mag * cosf(l_phase);
455
+    dstl[2 * n + 1] = l_mag * sinf(l_phase);
456
+
457
+    dstr[2 * n    ] = r_mag * cosf(r_phase);
458
+    dstr[2 * n + 1] = r_mag * sinf(r_phase);
459
+
460
+    dstc[2 * n    ] = c_mag * cosf(c_phase);
461
+    dstc[2 * n + 1] = c_mag * sinf(c_phase);
462
+
463
+    dstlfe[2 * n    ] = lfe_mag * cosf(c_phase);
464
+    dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
465
+
466
+    dstls[2 * n    ] = ls_mag * cosf(l_phase);
467
+    dstls[2 * n + 1] = ls_mag * sinf(l_phase);
468
+
469
+    dstrs[2 * n    ] = rs_mag * cosf(r_phase);
470
+    dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
471
+}
472
+
473
+static void upmix_7_0(AVFilterContext *ctx,
474
+                      float l_phase,
475
+                      float r_phase,
476
+                      float c_phase,
477
+                      float mag_total,
478
+                      float x, float y,
479
+                      int n)
480
+{
481
+    float l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
482
+    float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb;
483
+    AudioSurroundContext *s = ctx->priv;
484
+
485
+    dstl  = (float *)s->output->extended_data[0];
486
+    dstr  = (float *)s->output->extended_data[1];
487
+    dstc  = (float *)s->output->extended_data[2];
488
+    dstlb = (float *)s->output->extended_data[3];
489
+    dstrb = (float *)s->output->extended_data[4];
490
+    dstls = (float *)s->output->extended_data[5];
491
+    dstrs = (float *)s->output->extended_data[6];
492
+
493
+    c_mag  = sqrtf(1.f - fabsf(x))   * ((y + 1.f) * .5f) * mag_total;
494
+    l_mag  = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
495
+    r_mag  = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
496
+    lb_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
497
+    rb_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
498
+    ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - fabsf(y)) * mag_total;
499
+    rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - fabsf(y)) * mag_total;
500
+
501
+    dstl[2 * n    ] = l_mag * cosf(l_phase);
502
+    dstl[2 * n + 1] = l_mag * sinf(l_phase);
503
+
504
+    dstr[2 * n    ] = r_mag * cosf(r_phase);
505
+    dstr[2 * n + 1] = r_mag * sinf(r_phase);
506
+
507
+    dstc[2 * n    ] = c_mag * cosf(c_phase);
508
+    dstc[2 * n + 1] = c_mag * sinf(c_phase);
509
+
510
+    dstlb[2 * n    ] = lb_mag * cosf(l_phase);
511
+    dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
512
+
513
+    dstrb[2 * n    ] = rb_mag * cosf(r_phase);
514
+    dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
515
+
516
+    dstls[2 * n    ] = ls_mag * cosf(l_phase);
517
+    dstls[2 * n + 1] = ls_mag * sinf(l_phase);
518
+
519
+    dstrs[2 * n    ] = rs_mag * cosf(r_phase);
520
+    dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
521
+}
522
+
523
+static void upmix_7_1(AVFilterContext *ctx,
524
+                      float l_phase,
525
+                      float r_phase,
526
+                      float c_phase,
527
+                      float mag_total,
528
+                      float x, float y,
529
+                      int n)
530
+{
531
+    float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
532
+    float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
533
+    AudioSurroundContext *s = ctx->priv;
534
+
535
+    dstl  = (float *)s->output->extended_data[0];
536
+    dstr  = (float *)s->output->extended_data[1];
537
+    dstc  = (float *)s->output->extended_data[2];
538
+    dstlfe = (float *)s->output->extended_data[3];
539
+    dstlb = (float *)s->output->extended_data[4];
540
+    dstrb = (float *)s->output->extended_data[5];
541
+    dstls = (float *)s->output->extended_data[6];
542
+    dstrs = (float *)s->output->extended_data[7];
543
+
544
+    get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
545
+
546
+    c_mag  = sqrtf(1.f - fabsf(x))   * ((y + 1.f) * .5f) * mag_total;
547
+    l_mag  = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
548
+    r_mag  = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
549
+    lb_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
550
+    rb_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
551
+    ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - fabsf(y)) * mag_total;
552
+    rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - fabsf(y)) * mag_total;
553
+
554
+    dstl[2 * n    ] = l_mag * cosf(l_phase);
555
+    dstl[2 * n + 1] = l_mag * sinf(l_phase);
556
+
557
+    dstr[2 * n    ] = r_mag * cosf(r_phase);
558
+    dstr[2 * n + 1] = r_mag * sinf(r_phase);
559
+
560
+    dstc[2 * n    ] = c_mag * cosf(c_phase);
561
+    dstc[2 * n + 1] = c_mag * sinf(c_phase);
562
+
563
+    dstlfe[2 * n    ] = lfe_mag * cosf(c_phase);
564
+    dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
565
+
566
+    dstlb[2 * n    ] = lb_mag * cosf(l_phase);
567
+    dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
568
+
569
+    dstrb[2 * n    ] = rb_mag * cosf(r_phase);
570
+    dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
571
+
572
+    dstls[2 * n    ] = ls_mag * cosf(l_phase);
573
+    dstls[2 * n + 1] = ls_mag * sinf(l_phase);
574
+
575
+    dstrs[2 * n    ] = rs_mag * cosf(r_phase);
576
+    dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
577
+}
578
+
579
+static int init(AVFilterContext *ctx)
580
+{
581
+    AudioSurroundContext *s = ctx->priv;
582
+    float overlap;
583
+    int i;
584
+
585
+    if (!(s->out_channel_layout = av_get_channel_layout(s->out_channel_layout_str))) {
586
+        av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
587
+               s->out_channel_layout_str);
588
+        return AVERROR(EINVAL);
589
+    }
590
+
591
+    if (s->lowcutf >= s->highcutf) {
592
+        av_log(ctx, AV_LOG_ERROR, "Low cut-off '%d' should be less than high cut-off '%d'.\n",
593
+               s->lowcutf, s->highcutf);
594
+        return AVERROR(EINVAL);
595
+    }
596
+
597
+    switch (s->out_channel_layout) {
598
+    case AV_CH_LAYOUT_MONO:
599
+        s->upmix = upmix_1_0;
600
+        break;
601
+    case AV_CH_LAYOUT_STEREO:
602
+        s->upmix = upmix_stereo;
603
+        break;
604
+    case AV_CH_LAYOUT_2POINT1:
605
+        s->upmix = upmix_2_1;
606
+        break;
607
+    case AV_CH_LAYOUT_SURROUND:
608
+        s->upmix = upmix_3_0;
609
+        break;
610
+    case AV_CH_LAYOUT_3POINT1:
611
+        s->upmix = upmix_3_1;
612
+        break;
613
+    case AV_CH_LAYOUT_4POINT0:
614
+        s->upmix = upmix_4_0;
615
+        break;
616
+    case AV_CH_LAYOUT_4POINT1:
617
+        s->upmix = upmix_4_1;
618
+        break;
619
+    case AV_CH_LAYOUT_5POINT0_BACK:
620
+        s->upmix = upmix_5_0_back;
621
+        break;
622
+    case AV_CH_LAYOUT_5POINT1_BACK:
623
+        s->upmix = upmix_5_1_back;
624
+        break;
625
+    case AV_CH_LAYOUT_7POINT0:
626
+        s->upmix = upmix_7_0;
627
+        break;
628
+    case AV_CH_LAYOUT_7POINT1:
629
+        s->upmix = upmix_7_1;
630
+        break;
631
+    default:
632
+        av_log(ctx, AV_LOG_ERROR, "Unsupported output channel layout '%s'.\n",
633
+               s->out_channel_layout_str);
634
+        return AVERROR(EINVAL);
635
+    }
636
+
637
+    s->buf_size = 4096;
638
+    s->pts = AV_NOPTS_VALUE;
639
+
640
+    s->window_func_lut = av_calloc(s->buf_size, sizeof(*s->window_func_lut));
641
+    if (!s->window_func_lut)
642
+        return AVERROR(ENOMEM);
643
+
644
+    for (i = 0; i < s->buf_size; i++)
645
+        s->window_func_lut[i] = sqrtf(0.5 * (1 - cosf(2 * M_PI * i / s->buf_size)) / s->buf_size);
646
+    overlap = .5;
647
+    s->hop_size = s->buf_size * (1. - overlap);
648
+
649
+    return 0;
650
+}
651
+
652
+static int fft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
653
+{
654
+    AudioSurroundContext *s = ctx->priv;
655
+    const float level_in = s->level_in;
656
+    float *dst;
657
+    int n;
658
+
659
+    memset(s->input->extended_data[ch] + s->buf_size * sizeof(float), 0, s->buf_size * sizeof(float));
660
+
661
+    dst = (float *)s->input->extended_data[ch];
662
+    for (n = 0; n < s->buf_size; n++) {
663
+        dst[n] *= s->window_func_lut[n] * level_in;
664
+    }
665
+
666
+    av_rdft_calc(s->rdft[ch], (float *)s->input->extended_data[ch]);
667
+
668
+    return 0;
669
+}
670
+
671
+static int ifft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
672
+{
673
+    AudioSurroundContext *s = ctx->priv;
674
+    const float level_out = s->level_out;
675
+    AVFrame *out = arg;
676
+    float *dst, *ptr;
677
+    int n;
678
+
679
+    av_rdft_calc(s->irdft[ch], (float *)s->output->extended_data[ch]);
680
+
681
+    dst = (float *)s->output->extended_data[ch];
682
+    ptr = (float *)s->overlap_buffer->extended_data[ch];
683
+
684
+    memmove(s->overlap_buffer->extended_data[ch],
685
+            s->overlap_buffer->extended_data[ch] + s->hop_size * sizeof(float),
686
+            s->buf_size * sizeof(float));
687
+    memset(s->overlap_buffer->extended_data[ch] + s->buf_size * sizeof(float),
688
+           0, s->hop_size * sizeof(float));
689
+
690
+    for (n = 0; n < s->buf_size; n++) {
691
+        ptr[n] += dst[n] * s->window_func_lut[n] * level_out;
692
+    }
693
+
694
+    ptr = (float *)s->overlap_buffer->extended_data[ch];
695
+    dst = (float *)out->extended_data[ch];
696
+    memcpy(dst, ptr, s->hop_size * sizeof(float));
697
+
698
+    return 0;
699
+}
700
+
701
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
702
+{
703
+    AVFilterContext *ctx = inlink->dst;
704
+    AVFilterLink *outlink = ctx->outputs[0];
705
+    AudioSurroundContext *s = ctx->priv;
706
+
707
+    av_audio_fifo_write(s->fifo, (void **)in->extended_data,
708
+                        in->nb_samples);
709
+
710
+    if (s->pts == AV_NOPTS_VALUE)
711
+        s->pts = in->pts;
712
+
713
+    av_frame_free(&in);
714
+
715
+    while (av_audio_fifo_size(s->fifo) >= s->buf_size) {
716
+        float *srcl, *srcr;
717
+        AVFrame *out;
718
+        int n, ret;
719
+
720
+        ret = av_audio_fifo_peek(s->fifo, (void **)s->input->extended_data, s->buf_size);
721
+        if (ret < 0)
722
+            return ret;
723
+
724
+        ctx->internal->execute(ctx, fft_channel, NULL, NULL, inlink->channels);
725
+
726
+        srcl = (float *)s->input->extended_data[0];
727
+        srcr = (float *)s->input->extended_data[1];
728
+
729
+        for (n = 0; n < s->buf_size; n++) {
730
+            float l_re = srcl[2 * n], r_re = srcr[2 * n];
731
+            float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
732
+            float c_phase = atan2f(l_im + r_im, l_re + r_re);
733
+            float l_mag = hypotf(l_re, l_im);
734
+            float r_mag = hypotf(r_re, r_im);
735
+            float l_phase = atan2f(l_im, l_re);
736
+            float r_phase = atan2f(r_im, r_re);
737
+            float phase_dif = fabsf(l_phase - r_phase);
738
+            float mag_dif = (l_mag - r_mag) / (l_mag + r_mag);
739
+            float mag_total = hypotf(l_mag, r_mag);
740
+            float x, y;
741
+
742
+            if (phase_dif > M_PI)
743
+                phase_dif = 2 * M_PI - phase_dif;
744
+
745
+            stereo_position(mag_dif, phase_dif, &x, &y);
746
+
747
+            s->upmix(ctx, l_phase, r_phase, c_phase, mag_total, x, y, n);
748
+        }
749
+
750
+        out = ff_get_audio_buffer(outlink, s->hop_size);
751
+        if (!out)
752
+            return AVERROR(ENOMEM);
753
+
754
+        ctx->internal->execute(ctx, ifft_channel, out, NULL, outlink->channels);
755
+
756
+        out->pts = s->pts;
757
+        if (s->pts != AV_NOPTS_VALUE)
758
+            s->pts += av_rescale_q(out->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
759
+        av_audio_fifo_drain(s->fifo, s->hop_size);
760
+        ret = ff_filter_frame(outlink, out);
761
+        if (ret < 0)
762
+            return ret;
763
+    }
764
+
765
+    return 0;
766
+}
767
+
768
+static av_cold void uninit(AVFilterContext *ctx)
769
+{
770
+    AudioSurroundContext *s = ctx->priv;
771
+    int ch;
772
+
773
+    av_frame_free(&s->input);
774
+    av_frame_free(&s->output);
775
+    av_frame_free(&s->overlap_buffer);
776
+
777
+    for (ch = 0; ch < s->nb_in_channels; ch++) {
778
+        av_rdft_end(s->rdft[ch]);
779
+    }
780
+    for (ch = 0; ch < s->nb_out_channels; ch++) {
781
+        av_rdft_end(s->irdft[ch]);
782
+    }
783
+    av_freep(&s->rdft);
784
+    av_freep(&s->irdft);
785
+    av_audio_fifo_free(s->fifo);
786
+    av_freep(&s->window_func_lut);
787
+}
788
+
789
+#define OFFSET(x) offsetof(AudioSurroundContext, x)
790
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
791
+
792
+static const AVOption surround_options[] = {
793
+    { "chl_out",   "set output channel layout", OFFSET(out_channel_layout_str), AV_OPT_TYPE_STRING, {.str="5.1"}, 0,   0, FLAGS },
794
+    { "level_in",  "set input level",           OFFSET(level_in),               AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  10, FLAGS },
795
+    { "level_out", "set output level",          OFFSET(level_out),              AV_OPT_TYPE_FLOAT,  {.dbl=1},     0,  10, FLAGS },
796
+    { "lfe",       "output LFE",                OFFSET(output_lfe),             AV_OPT_TYPE_BOOL,   {.i64=1},     0,   1, FLAGS },
797
+    { "lfe_low",   "LFE low cut off",           OFFSET(lowcutf),                AV_OPT_TYPE_INT,    {.i64=128},   0, 256, FLAGS },
798
+    { "lfe_high",  "LFE high cut off",          OFFSET(highcutf),               AV_OPT_TYPE_INT,    {.i64=256},   0, 512, FLAGS },
799
+    { NULL }
800
+};
801
+
802
+AVFILTER_DEFINE_CLASS(surround);
803
+
804
+static const AVFilterPad inputs[] = {
805
+    {
806
+        .name         = "default",
807
+        .type         = AVMEDIA_TYPE_AUDIO,
808
+        .filter_frame = filter_frame,
809
+        .config_props = config_input,
810
+    },
811
+    { NULL }
812
+};
813
+
814
+static const AVFilterPad outputs[] = {
815
+    {
816
+        .name         = "default",
817
+        .type         = AVMEDIA_TYPE_AUDIO,
818
+        .config_props = config_output,
819
+    },
820
+    { NULL }
821
+};
822
+
823
+AVFilter ff_af_surround = {
824
+    .name           = "surround",
825
+    .description    = NULL_IF_CONFIG_SMALL("Apply audio surround upmix filter."),
826
+    .query_formats  = query_formats,
827
+    .priv_size      = sizeof(AudioSurroundContext),
828
+    .priv_class     = &surround_class,
829
+    .init           = init,
830
+    .uninit         = uninit,
831
+    .inputs         = inputs,
832
+    .outputs        = outputs,
833
+    .flags          = AVFILTER_FLAG_SLICE_THREADS,
834
+};
... ...
@@ -121,6 +121,7 @@ static void register_all(void)
121 121
     REGISTER_FILTER(SOFALIZER,      sofalizer,      af);
122 122
     REGISTER_FILTER(STEREOTOOLS,    stereotools,    af);
123 123
     REGISTER_FILTER(STEREOWIDEN,    stereowiden,    af);
124
+    REGISTER_FILTER(SURROUND,       surround,       af);
124 125
     REGISTER_FILTER(TREBLE,         treble,         af);
125 126
     REGISTER_FILTER(TREMOLO,        tremolo,        af);
126 127
     REGISTER_FILTER(VIBRATO,        vibrato,        af);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  90
33
+#define LIBAVFILTER_VERSION_MINOR  91
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \