Browse code

avfilter: add Dynamic Audio Normalizer filter

LoRd_MuldeR authored on 2015/07/08 01:19:59
Showing 4 changed files
... ...
@@ -1544,6 +1544,164 @@ Optional. It should have a value much less than 1 (e.g. 0.05 or 0.02) and is
1544 1544
 used to prevent clipping.
1545 1545
 @end table
1546 1546
 
1547
+@section dynaudnorm
1548
+Dynamic Audio Normalizer.
1549
+
1550
+This filter applies a certain amount of gain to the input audio in order
1551
+to bring its peak magnitude to a target level (e.g. 0 dBFS). However, in
1552
+contrast to more "simple" normalization algorithms, the Dynamic Audio
1553
+Normalizer *dynamically* re-adjusts the gain factor to the input audio.
1554
+This allows for applying extra gain to the "quiet" sections of the audio
1555
+while avoiding distortions or clipping the "loud" sections. In other words:
1556
+The Dynamic Audio Normalizer will "even out" the volume of quiet and loud
1557
+sections, in the sense that the volume of each section is brought to the
1558
+same target level. Note, however, that the Dynamic Audio Normalizer achieves
1559
+this goal *without* applying "dynamic range compressing". It will retain 100%
1560
+of the dynamic range *within* each section of the audio file.
1561
+
1562
+@table @option
1563
+@item f
1564
+Set the frame length in milliseconds. In range from 10 to 8000 milliseconds.
1565
+Default is 500 milliseconds.
1566
+The Dynamic Audio Normalizer processes the input audio in small chunks,
1567
+referred to as frames. This is required, because a peak magnitude has no
1568
+meaning for just a single sample value. Instead, we need to determine the
1569
+peak magnitude for a contiguous sequence of sample values. While a "standard"
1570
+normalizer would simply use the peak magnitude of the complete file, the
1571
+Dynamic Audio Normalizer determines the peak magnitude individually for each
1572
+frame. The length of a frame is specified in milliseconds. By default, the
1573
+Dynamic Audio Normalizer uses a frame length of 500 milliseconds, which has
1574
+been found to give good results with most files.
1575
+Note that the exact frame length, in number of samples, will be determined
1576
+automatically, based on the sampling rate of the individual input audio file.
1577
+
1578
+@item g
1579
+Set the Gaussian filter window size. In range from 3 to 301, must be odd
1580
+number. Default is 31.
1581
+Probably the most important parameter of the Dynamic Audio Normalizer is the
1582
+@code{window size} of the Gaussian smoothing filter. The filter's window size
1583
+is specified in frames, centered around the current frame. For the sake of
1584
+simplicity, this must be an odd number. Consequently, the default value of 31
1585
+takes into account the current frame, as well as the 15 preceding frames and
1586
+the 15 subsequent frames. Using a larger window results in a stronger
1587
+smoothing effect and thus in less gain variation, i.e. slower gain
1588
+adaptation. Conversely, using a smaller window results in a weaker smoothing
1589
+effect and thus in more gain variation, i.e. faster gain adaptation.
1590
+In other words, the more you increase this value, the more the Dynamic Audio
1591
+Normalizer will behave like a "traditional" normalization filter. On the
1592
+contrary, the more you decrease this value, the more the Dynamic Audio
1593
+Normalizer will behave like a dynamic range compressor.
1594
+
1595
+@item p
1596
+Set the target peak value. This specifies the highest permissible magnitude
1597
+level for the normalized audio input. This filter will try to approach the
1598
+target peak magnitude as closely as possible, but at the same time it also
1599
+makes sure that the normalized signal will never exceed the peak magnitude.
1600
+A frame's maximum local gain factor is imposed directly by the target peak
1601
+magnitude. The default value is 0.95 and thus leaves a headroom of 5%*.
1602
+It is not recommended to go above this value.
1603
+
1604
+@item m
1605
+Set the maximum gain factor. In range from 1.0 to 100.0. Default is 10.0.
1606
+The Dynamic Audio Normalizer determines the maximum possible (local) gain
1607
+factor for each input frame, i.e. the maximum gain factor that does not
1608
+result in clipping or distortion. The maximum gain factor is determined by
1609
+the frame's highest magnitude sample. However, the Dynamic Audio Normalizer
1610
+additionally bounds the frame's maximum gain factor by a predetermined
1611
+(global) maximum gain factor. This is done in order to avoid excessive gain
1612
+factors in "silent" or almost silent frames. By default, the maximum gain
1613
+factor is 10.0, For most inputs the default value should be sufficient and
1614
+it usually is not recommended to increase this value. Though, for input
1615
+with an extremely low overall volume level, it may be necessary to allow even
1616
+higher gain factors. Note, however, that the Dynamic Audio Normalizer does
1617
+not simply apply a "hard" threshold (i.e. cut off values above the threshold).
1618
+Instead, a "sigmoid" threshold function will be applied. This way, the
1619
+gain factors will smoothly approach the threshold value, but never exceed that
1620
+value.
1621
+
1622
+@item r
1623
+Set the target RMS. In range from 0.0 to 1.0. Default is 0.0 - disabled.
1624
+By default, the Dynamic Audio Normalizer performs "peak" normalization.
1625
+This means that the maximum local gain factor for each frame is defined
1626
+(only) by the frame's highest magnitude sample. This way, the samples can
1627
+be amplified as much as possible without exceeding the maximum signal
1628
+level, i.e. without clipping. Optionally, however, the Dynamic Audio
1629
+Normalizer can also take into account the frame's root mean square,
1630
+abbreviated RMS. In electrical engineering, the RMS is commonly used to
1631
+determine the power of a time-varying signal. It is therefore considered
1632
+that the RMS is a better approximation of the "perceived loudness" than
1633
+just looking at the signal's peak magnitude. Consequently, by adjusting all
1634
+frames to a constant RMS value, a uniform "perceived loudness" can be
1635
+established. If a target RMS value has been specified, a frame's local gain
1636
+factor is defined as the factor that would result in exactly that RMS value.
1637
+Note, however, that the maximum local gain factor is still restricted by the
1638
+frame's highest magnitude sample, in order to prevent clipping.
1639
+
1640
+@item n
1641
+Enable channels coupling. By default is enabled.
1642
+By default, the Dynamic Audio Normalizer will amplify all channels by the same
1643
+amount. This means the same gain factor will be applied to all channels, i.e.
1644
+the maximum possible gain factor is determined by the "loudest" channel.
1645
+However, in some recordings, it may happen that the volume of the different
1646
+channels is uneven, e.g. one channel may be "quieter" than the other one(s).
1647
+In this case, this option can be used to disable the channel coupling. This way,
1648
+the gain factor will be determined independently for each channel, depending
1649
+only on the individual channel's highest magnitude sample. This allows for
1650
+harmonizing the volume of the different channels.
1651
+
1652
+@item c
1653
+Enable DC bias correction. By default is disabled.
1654
+An audio signal (in the time domain) is a sequence of sample values.
1655
+In the Dynamic Audio Normalizer these sample values are represented in the
1656
+-1.0 to 1.0 range, regardless of the original input format. Normally, the
1657
+audio signal, or "waveform", should be centered around the zero point.
1658
+That means if we calculate the mean value of all samples in a file, or in a
1659
+single frame, then the result should be 0.0 or at least very close to that
1660
+value. If, however, there is a significant deviation of the mean value from
1661
+0.0, in either positive or negative direction, this is referred to as a
1662
+DC bias or DC offset. Since a DC bias is clearly undesirable, the Dynamic
1663
+Audio Normalizer provides optional DC bias correction.
1664
+With DC bias correction enabled, the Dynamic Audio Normalizer will determine
1665
+the mean value, or "DC correction" offset, of each input frame and subtract
1666
+that value from all of the frame's sample values which ensures those samples
1667
+are centered around 0.0 again. Also, in order to avoid "gaps" at the frame
1668
+boundaries, the DC correction offset values will be interpolated smoothly
1669
+between neighbouring frames.
1670
+
1671
+@item b
1672
+Enable alternative boundary mode. By default is disabled.
1673
+The Dynamic Audio Normalizer takes into account a certain neighbourhood
1674
+around each frame. This includes the preceding frames as well as the
1675
+subsequent frames. However, for the "boundary" frames, located at the very
1676
+beginning and at the very end of the audio file, not all neighbouring
1677
+frames are available. In particular, for the first few frames in the audio
1678
+file, the preceding frames are not known. And, similarly, for the last few
1679
+frames in the audio file, the subsequent frames are not known. Thus, the
1680
+question arises which gain factors should be assumed for the missing frames
1681
+in the "boundary" region. The Dynamic Audio Normalizer implements two modes
1682
+to deal with this situation. The default boundary mode assumes a gain factor
1683
+of exactly 1.0 for the missing frames, resulting in a smooth "fade in" and
1684
+"fade out" at the beginning and at the end of the input, respectively.
1685
+
1686
+@item s
1687
+Set the compress factor. In range from 0.0 to 30.0. Default is 0.0.
1688
+By default, the Dynamic Audio Normalizer does not apply "traditional"
1689
+compression. This means that signal peaks will not be pruned and thus the
1690
+full dynamic range will be retained within each local neighbourhood. However,
1691
+in some cases it may be desirable to combine the Dynamic Audio Normalizer's
1692
+normalization algorithm with a more "traditional" compression.
1693
+For this purpose, the Dynamic Audio Normalizer provides an optional compression
1694
+(thresholding) function. If (and only if) the compression feature is enabled,
1695
+all input frames will be processed by a soft knee thresholding function prior
1696
+to the actual normalization process. Put simply, the thresholding function is
1697
+going to prune all samples whose magnitude exceeds a certain threshold value.
1698
+However, the Dynamic Audio Normalizer does not simply apply a fixed threshold
1699
+value. Instead, the threshold value will be adjusted for each individual
1700
+frame.
1701
+In general, smaller parameters result in stronger compression, and vice versa.
1702
+Values below 3.0 are not recommended, because audible distortion may appear.
1703
+@end table
1704
+
1547 1705
 @section earwax
1548 1706
 
1549 1707
 Make audio easier to listen to on headphones.
... ...
@@ -67,6 +67,7 @@ OBJS-$(CONFIG_CHANNELSPLIT_FILTER)           += af_channelsplit.o
67 67
 OBJS-$(CONFIG_CHORUS_FILTER)                 += af_chorus.o generate_wave_table.o
68 68
 OBJS-$(CONFIG_COMPAND_FILTER)                += af_compand.o
69 69
 OBJS-$(CONFIG_DCSHIFT_FILTER)                += af_dcshift.o
70
+OBJS-$(CONFIG_DYNAUDNORM_FILTER)             += af_dynaudnorm.o
70 71
 OBJS-$(CONFIG_EARWAX_FILTER)                 += af_earwax.o
71 72
 OBJS-$(CONFIG_EBUR128_FILTER)                += f_ebur128.o
72 73
 OBJS-$(CONFIG_EQUALIZER_FILTER)              += af_biquads.o
73 74
new file mode 100644
... ...
@@ -0,0 +1,734 @@
0
+/*
1
+ * Dynamic Audio Normalizer
2
+ * Copyright (c) 2015 LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.
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
+/**
22
+ * @file
23
+ * Dynamic Audio Normalizer
24
+ */
25
+
26
+#include <float.h>
27
+
28
+#include "libavutil/avassert.h"
29
+#include "libavutil/opt.h"
30
+
31
+#define FF_BUFQUEUE_SIZE 302
32
+#include "libavfilter/bufferqueue.h"
33
+
34
+#include "audio.h"
35
+#include "avfilter.h"
36
+#include "internal.h"
37
+
38
+typedef struct cqueue {
39
+    double *elements;
40
+    int size;
41
+    int nb_elements;
42
+    int first;
43
+} cqueue;
44
+
45
+typedef struct DynamicAudioNormalizerContext {
46
+    const AVClass *class;
47
+
48
+    struct FFBufQueue queue;
49
+
50
+    int frame_len;
51
+    int frame_len_msec;
52
+    int filter_size;
53
+    int dc_correction;
54
+    int channels_coupled;
55
+    int alt_boundary_mode;
56
+
57
+    double peak_value;
58
+    double max_amplification;
59
+    double target_rms;
60
+    double compress_factor;
61
+    double *prev_amplification_factor;
62
+    double *dc_correction_value;
63
+    double *compress_threshold;
64
+    double *fade_factors[2];
65
+    double *weights;
66
+
67
+    int channels;
68
+    int delay;
69
+
70
+    cqueue **gain_history_original;
71
+    cqueue **gain_history_minimum;
72
+    cqueue **gain_history_smoothed;
73
+} DynamicAudioNormalizerContext;
74
+
75
+#define OFFSET(x) offsetof(DynamicAudioNormalizerContext, x)
76
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
77
+
78
+static const AVOption dynaudnorm_options[] = {
79
+    { "f", "set the frame length in msec",     OFFSET(frame_len_msec),    AV_OPT_TYPE_INT,    {.i64 = 500},   10,  8000, FLAGS },
80
+    { "g", "set the filter size",              OFFSET(filter_size),       AV_OPT_TYPE_INT,    {.i64 = 31},     3,   301, FLAGS },
81
+    { "p", "set the peak value",               OFFSET(peak_value),        AV_OPT_TYPE_DOUBLE, {.dbl = 0.95}, 0.0,   1.0, FLAGS },
82
+    { "m", "set the max amplification",        OFFSET(max_amplification), AV_OPT_TYPE_DOUBLE, {.dbl = 10.0}, 1.0, 100.0, FLAGS },
83
+    { "r", "set the target RMS",               OFFSET(target_rms),        AV_OPT_TYPE_DOUBLE, {.dbl = 0.0},  0.0,   1.0, FLAGS },
84
+    { "n", "enable channel coupling",          OFFSET(channels_coupled),  AV_OPT_TYPE_INT,    {.i64 = 1},      0,     1, FLAGS },
85
+    { "c", "enable DC correction",             OFFSET(dc_correction),     AV_OPT_TYPE_INT,    {.i64 = 0},      0,     1, FLAGS },
86
+    { "b", "enable alternative boundary mode", OFFSET(alt_boundary_mode), AV_OPT_TYPE_INT,    {.i64 = 0},      0,     1, FLAGS },
87
+    { "s", "set the compress factor",          OFFSET(compress_factor),   AV_OPT_TYPE_DOUBLE, {.dbl = 0.0},  0.0,  30.0, FLAGS },
88
+    { NULL }
89
+};
90
+
91
+AVFILTER_DEFINE_CLASS(dynaudnorm);
92
+
93
+static av_cold int init(AVFilterContext *ctx)
94
+{
95
+    DynamicAudioNormalizerContext *s = ctx->priv;
96
+
97
+    if (!(s->filter_size & 1)) {
98
+        av_log(ctx, AV_LOG_ERROR, "filter size %d is invalid. Must be an odd value.\n", s->filter_size);
99
+        return AVERROR(EINVAL);
100
+    }
101
+
102
+    return 0;
103
+}
104
+
105
+static int query_formats(AVFilterContext *ctx)
106
+{
107
+    AVFilterFormats *formats;
108
+    AVFilterChannelLayouts *layouts;
109
+    static const enum AVSampleFormat sample_fmts[] = {
110
+        AV_SAMPLE_FMT_DBLP,
111
+        AV_SAMPLE_FMT_NONE
112
+    };
113
+    int ret;
114
+
115
+    layouts = ff_all_channel_layouts();
116
+    if (!layouts)
117
+        return AVERROR(ENOMEM);
118
+    ret = ff_set_common_channel_layouts(ctx, layouts);
119
+    if (ret < 0)
120
+        return ret;
121
+
122
+    formats = ff_make_format_list(sample_fmts);
123
+    if (!formats)
124
+        return AVERROR(ENOMEM);
125
+    ret = ff_set_common_formats(ctx, formats);
126
+    if (ret < 0)
127
+        return ret;
128
+
129
+    formats = ff_all_samplerates();
130
+    if (!formats)
131
+        return AVERROR(ENOMEM);
132
+    return ff_set_common_samplerates(ctx, formats);
133
+}
134
+
135
+static inline int frame_size(int sample_rate, int frame_len_msec)
136
+{
137
+    const int frame_size = round((double)sample_rate * (frame_len_msec / 1000.0));
138
+    return frame_size + (frame_size % 2);
139
+}
140
+
141
+static void precalculate_fade_factors(double *fade_factors[2], int frame_len)
142
+{
143
+    const double step_size = 1.0 / frame_len;
144
+    int pos;
145
+
146
+    for (pos = 0; pos < frame_len; pos++) {
147
+        fade_factors[0][pos] = 1.0 - (step_size * (pos + 1.0));
148
+        fade_factors[1][pos] = 1.0 - fade_factors[0][pos];
149
+    }
150
+}
151
+
152
+static cqueue *cqueue_create(int size)
153
+{
154
+    cqueue *q;
155
+
156
+    q = av_malloc(sizeof(cqueue));
157
+    if (!q)
158
+        return NULL;
159
+
160
+    q->size = size;
161
+    q->nb_elements = 0;
162
+    q->first = 0;
163
+
164
+    q->elements = av_malloc(sizeof(double) * size);
165
+    if (!q->elements) {
166
+        av_free(q);
167
+        return NULL;
168
+    }
169
+
170
+    return q;
171
+}
172
+
173
+static void cqueue_free(cqueue *q)
174
+{
175
+    av_free(q->elements);
176
+    av_free(q);
177
+}
178
+
179
+static int cqueue_size(cqueue *q)
180
+{
181
+    return q->nb_elements;
182
+}
183
+
184
+static int cqueue_empty(cqueue *q)
185
+{
186
+    return !q->nb_elements;
187
+}
188
+
189
+static int cqueue_enqueue(cqueue *q, double element)
190
+{
191
+    int i;
192
+
193
+    av_assert2(q->nb_elements |= q->size);
194
+
195
+    i = (q->first + q->nb_elements) % q->size;
196
+    q->elements[i] = element;
197
+    q->nb_elements++;
198
+
199
+    return 0;
200
+}
201
+
202
+static double cqueue_peek(cqueue *q, int index)
203
+{
204
+    av_assert2(index < q->nb_elements);
205
+    return q->elements[(q->first + index) % q->size];
206
+}
207
+
208
+static int cqueue_dequeue(cqueue *q, double *element)
209
+{
210
+    av_assert2(!cqueue_empty(q));
211
+
212
+    *element = q->elements[q->first];
213
+    q->first = (q->first + 1) % q->size;
214
+    q->nb_elements--;
215
+
216
+    return 0;
217
+}
218
+
219
+static int cqueue_pop(cqueue *q)
220
+{
221
+    av_assert2(!cqueue_empty(q));
222
+
223
+    q->first = (q->first + 1) % q->size;
224
+    q->nb_elements--;
225
+
226
+    return 0;
227
+}
228
+
229
+static const double s_pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;
230
+
231
+static void init_gaussian_filter(DynamicAudioNormalizerContext *s)
232
+{
233
+    double total_weight = 0.0;
234
+    const double sigma = (((s->filter_size / 2.0) - 1.0) / 3.0) + (1.0 / 3.0);
235
+    double adjust;
236
+    int i;
237
+
238
+    // Pre-compute constants
239
+    const int offset = s->filter_size / 2;
240
+    const double c1 = 1.0 / (sigma * sqrt(2.0 * s_pi));
241
+    const double c2 = 2.0 * pow(sigma, 2.0);
242
+
243
+    // Compute weights
244
+    for (i = 0; i < s->filter_size; i++) {
245
+        const int x = i - offset;
246
+
247
+        s->weights[i] = c1 * exp(-(pow(x, 2.0) / c2));
248
+        total_weight += s->weights[i];
249
+    }
250
+
251
+    // Adjust weights
252
+    adjust = 1.0 / total_weight;
253
+    for (i = 0; i < s->filter_size; i++) {
254
+        s->weights[i] *= adjust;
255
+    }
256
+}
257
+
258
+static int config_input(AVFilterLink *inlink)
259
+{
260
+    AVFilterContext *ctx = inlink->dst;
261
+    DynamicAudioNormalizerContext *s = ctx->priv;
262
+    int c;
263
+
264
+    s->frame_len =
265
+    inlink->min_samples =
266
+    inlink->max_samples =
267
+    inlink->partial_buf_size = frame_size(inlink->sample_rate, s->frame_len_msec);
268
+    av_log(ctx, AV_LOG_DEBUG, "frame len %d\n", s->frame_len);
269
+
270
+    s->fade_factors[0] = av_malloc(s->frame_len * sizeof(*s->fade_factors[0]));
271
+    s->fade_factors[1] = av_malloc(s->frame_len * sizeof(*s->fade_factors[1]));
272
+
273
+    s->prev_amplification_factor = av_malloc(inlink->channels * sizeof(*s->prev_amplification_factor));
274
+    s->dc_correction_value = av_calloc(inlink->channels, sizeof(*s->dc_correction_value));
275
+    s->compress_threshold = av_calloc(inlink->channels, sizeof(*s->compress_threshold));
276
+    s->gain_history_original = av_calloc(inlink->channels, sizeof(*s->gain_history_original));
277
+    s->gain_history_minimum = av_calloc(inlink->channels, sizeof(*s->gain_history_minimum));
278
+    s->gain_history_smoothed = av_calloc(inlink->channels, sizeof(*s->gain_history_smoothed));
279
+    s->weights = av_malloc(s->filter_size * sizeof(*s->weights));
280
+    if (!s->prev_amplification_factor || !s->dc_correction_value ||
281
+        !s->compress_threshold || !s->fade_factors[0] || !s->fade_factors[1] ||
282
+        !s->gain_history_original || !s->gain_history_minimum ||
283
+        !s->gain_history_smoothed || !s->weights)
284
+        return AVERROR(ENOMEM);
285
+
286
+    for (c = 0; c < inlink->channels; c++) {
287
+        s->prev_amplification_factor[c] = 1.0;
288
+
289
+        s->gain_history_original[c] = cqueue_create(s->filter_size);
290
+        s->gain_history_minimum[c]  = cqueue_create(s->filter_size);
291
+        s->gain_history_smoothed[c] = cqueue_create(s->filter_size);
292
+
293
+        if (!s->gain_history_original[c] || !s->gain_history_minimum[c] ||
294
+            !s->gain_history_smoothed[c])
295
+            return AVERROR(ENOMEM);
296
+    }
297
+
298
+    precalculate_fade_factors(s->fade_factors, s->frame_len);
299
+    init_gaussian_filter(s);
300
+
301
+    s->channels = inlink->channels;
302
+    s->delay = s->filter_size;
303
+
304
+    return 0;
305
+}
306
+
307
+static int config_output(AVFilterLink *outlink)
308
+{
309
+    outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
310
+    return 0;
311
+}
312
+
313
+static inline double fade(double prev, double next, int pos,
314
+                          double *fade_factors[2])
315
+{
316
+    return fade_factors[0][pos] * prev + fade_factors[1][pos] * next;
317
+}
318
+
319
+static inline double pow2(const double value)
320
+{
321
+    return value * value;
322
+}
323
+
324
+static inline double bound(const double threshold, const double val)
325
+{
326
+    const double CONST = 0.8862269254527580136490837416705725913987747280611935; //sqrt(PI) / 2.0
327
+    return erf(CONST * (val / threshold)) * threshold;
328
+}
329
+
330
+static double find_peak_magnitude(AVFrame *frame, int channel)
331
+{
332
+    double max = DBL_EPSILON;
333
+    int c, i;
334
+
335
+    if (channel == -1) {
336
+        for (c = 0; c < frame->channels; c++) {
337
+            double *data_ptr = (double *)frame->extended_data[c];
338
+
339
+            for (i = 0; i < frame->nb_samples; i++)
340
+                max = FFMAX(max, fabs(data_ptr[i]));
341
+        }
342
+    } else {
343
+        double *data_ptr = (double *)frame->extended_data[channel];
344
+
345
+        for (i = 0; i < frame->nb_samples; i++)
346
+            max = FFMAX(max, fabs(data_ptr[i]));
347
+    }
348
+
349
+    return max;
350
+}
351
+
352
+static double compute_frame_rms(AVFrame *frame, int channel)
353
+{
354
+    double rms_value = 0.0;
355
+    int c, i;
356
+
357
+    if (channel == -1) {
358
+        for (c = 0; c < frame->channels; c++) {
359
+            const double *data_ptr = (double *)frame->extended_data[c];
360
+
361
+            for (i = 0; i < frame->nb_samples; i++) {
362
+                rms_value += pow2(data_ptr[i]);
363
+            }
364
+        }
365
+
366
+        rms_value /= frame->nb_samples * frame->channels;
367
+    } else {
368
+        const double *data_ptr = (double *)frame->extended_data[channel];
369
+        for (i = 0; i < frame->nb_samples; i++) {
370
+            rms_value += pow2(data_ptr[i]);
371
+        }
372
+
373
+        rms_value /= frame->nb_samples;
374
+    }
375
+
376
+    return FFMAX(sqrt(rms_value), DBL_EPSILON);
377
+}
378
+
379
+static double get_max_local_gain(DynamicAudioNormalizerContext *s, AVFrame *frame,
380
+                                 int channel)
381
+{
382
+    const double maximum_gain = s->peak_value / find_peak_magnitude(frame, channel);
383
+    const double rms_gain = s->target_rms > DBL_EPSILON ? (s->target_rms / compute_frame_rms(frame, channel)) : DBL_MAX;
384
+    return bound(s->max_amplification, FFMIN(maximum_gain, rms_gain));
385
+}
386
+
387
+static double minimum_filter(cqueue *q)
388
+{
389
+    double min = DBL_MAX;
390
+    int i;
391
+
392
+    for (i = 0; i < cqueue_size(q); i++) {
393
+        min = FFMIN(min, cqueue_peek(q, i));
394
+    }
395
+
396
+    return min;
397
+}
398
+
399
+static double gaussian_filter(DynamicAudioNormalizerContext *s, cqueue *q)
400
+{
401
+    double result = 0.0;
402
+    int i;
403
+
404
+    for (i = 0; i < cqueue_size(q); i++) {
405
+        result += cqueue_peek(q, i) * s->weights[i];
406
+    }
407
+
408
+    return result;
409
+}
410
+
411
+static void update_gain_history(DynamicAudioNormalizerContext *s, int channel,
412
+                                double current_gain_factor)
413
+{
414
+    if (cqueue_empty(s->gain_history_original[channel]) ||
415
+        cqueue_empty(s->gain_history_minimum[channel])) {
416
+        const int pre_fill_size = s->filter_size / 2;
417
+
418
+        s->prev_amplification_factor[channel] = s->alt_boundary_mode ? current_gain_factor : 1.0;
419
+
420
+        while (cqueue_size(s->gain_history_original[channel]) < pre_fill_size) {
421
+            cqueue_enqueue(s->gain_history_original[channel], s->alt_boundary_mode ? current_gain_factor : 1.0);
422
+        }
423
+
424
+        while (cqueue_size(s->gain_history_minimum[channel]) < pre_fill_size) {
425
+            cqueue_enqueue(s->gain_history_minimum[channel], s->alt_boundary_mode ? current_gain_factor : 1.0);
426
+        }
427
+    }
428
+
429
+    cqueue_enqueue(s->gain_history_original[channel], current_gain_factor);
430
+
431
+    while (cqueue_size(s->gain_history_original[channel]) >= s->filter_size) {
432
+        av_assert0(cqueue_size(s->gain_history_original[channel]) == s->filter_size);
433
+        const double minimum = minimum_filter(s->gain_history_original[channel]);
434
+
435
+        cqueue_enqueue(s->gain_history_minimum[channel], minimum);
436
+
437
+        cqueue_pop(s->gain_history_original[channel]);
438
+    }
439
+
440
+    while (cqueue_size(s->gain_history_minimum[channel]) >= s->filter_size) {
441
+        av_assert0(cqueue_size(s->gain_history_minimum[channel]) == s->filter_size);
442
+        const double smoothed = gaussian_filter(s, s->gain_history_minimum[channel]);
443
+
444
+        cqueue_enqueue(s->gain_history_smoothed[channel], smoothed);
445
+
446
+        cqueue_pop(s->gain_history_minimum[channel]);
447
+    }
448
+}
449
+
450
+static inline double update_value(double new, double old, double aggressiveness)
451
+{
452
+    av_assert0((aggressiveness >= 0.0) && (aggressiveness <= 1.0));
453
+    return aggressiveness * new + (1.0 - aggressiveness) * old;
454
+}
455
+
456
+static void perform_dc_correction(DynamicAudioNormalizerContext *s, AVFrame *frame)
457
+{
458
+    const double diff = 1.0 / frame->nb_samples;
459
+    int is_first_frame = cqueue_empty(s->gain_history_original[0]);
460
+    int c, i;
461
+
462
+    for (c = 0; c < s->channels; c++) {
463
+        double *dst_ptr = (double *)frame->extended_data[c];
464
+        double current_average_value = 0.0;
465
+
466
+        for (i = 0; i < frame->nb_samples; i++)
467
+            current_average_value += dst_ptr[i] * diff;
468
+
469
+        const double prev_value = is_first_frame ? current_average_value : s->dc_correction_value[c];
470
+        s->dc_correction_value[c] = is_first_frame ? current_average_value : update_value(current_average_value, s->dc_correction_value[c], 0.1);
471
+
472
+        for (i = 0; i < frame->nb_samples; i++) {
473
+            dst_ptr[i] -= fade(prev_value, s->dc_correction_value[c], i, s->fade_factors);
474
+        }
475
+    }
476
+}
477
+
478
+static double setup_compress_thresh(double threshold)
479
+{
480
+    if ((threshold > DBL_EPSILON) && (threshold < (1.0 - DBL_EPSILON))) {
481
+        double current_threshold = threshold;
482
+        double step_size = 1.0;
483
+
484
+        while (step_size > DBL_EPSILON) {
485
+            while ((current_threshold + step_size > current_threshold) &&
486
+                   (bound(current_threshold + step_size, 1.0) <= threshold)) {
487
+                current_threshold += step_size;
488
+            }
489
+
490
+            step_size /= 2.0;
491
+        }
492
+
493
+        return current_threshold;
494
+    } else {
495
+        return threshold;
496
+    }
497
+}
498
+
499
+static double compute_frame_std_dev(DynamicAudioNormalizerContext *s,
500
+                                    AVFrame *frame, int channel)
501
+{
502
+    double variance = 0.0;
503
+    int i, c;
504
+
505
+    if (channel == -1) {
506
+        for (c = 0; c < s->channels; c++) {
507
+            const double *data_ptr = (double *)frame->extended_data[c];
508
+
509
+            for (i = 0; i < frame->nb_samples; i++) {
510
+                variance += pow2(data_ptr[i]);  // Assume that MEAN is *zero*
511
+            }
512
+        }
513
+        variance /= (s->channels * frame->nb_samples) - 1;
514
+    } else {
515
+        const double *data_ptr = (double *)frame->extended_data[channel];
516
+
517
+        for (i = 0; i < frame->nb_samples; i++) {
518
+            variance += pow2(data_ptr[i]);      // Assume that MEAN is *zero*
519
+        }
520
+        variance /= frame->nb_samples - 1;
521
+    }
522
+
523
+    return FFMAX(sqrt(variance), DBL_EPSILON);
524
+}
525
+
526
+static void perform_compression(DynamicAudioNormalizerContext *s, AVFrame *frame)
527
+{
528
+    int is_first_frame = cqueue_empty(s->gain_history_original[0]);
529
+    int c, i;
530
+
531
+    if (s->channels_coupled) {
532
+        const double standard_deviation = compute_frame_std_dev(s, frame, -1);
533
+        const double current_threshold  = FFMIN(1.0, s->compress_factor * standard_deviation);
534
+
535
+        const double prev_value = is_first_frame ? current_threshold : s->compress_threshold[0];
536
+        s->compress_threshold[0] = is_first_frame ? current_threshold : update_value(current_threshold, s->compress_threshold[0], (1.0/3.0));
537
+
538
+        const double prev_actual_thresh = setup_compress_thresh(prev_value);
539
+        const double curr_actual_thresh = setup_compress_thresh(s->compress_threshold[0]);
540
+
541
+        for (c = 0; c < s->channels; c++) {
542
+            double *const dst_ptr = (double *)frame->extended_data[c];
543
+            for (i = 0; i < frame->nb_samples; i++) {
544
+                const double localThresh = fade(prev_actual_thresh, curr_actual_thresh, i, s->fade_factors);
545
+                dst_ptr[i] = copysign(bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]);
546
+            }
547
+        }
548
+    } else {
549
+        for (c = 0; c < s->channels; c++) {
550
+            const double standard_deviation = compute_frame_std_dev(s, frame, c);
551
+            const double current_threshold  = setup_compress_thresh(FFMIN(1.0, s->compress_factor * standard_deviation));
552
+
553
+            const double prev_value = is_first_frame ? current_threshold : s->compress_threshold[c];
554
+            s->compress_threshold[c] = is_first_frame ? current_threshold : update_value(current_threshold, s->compress_threshold[c], 1.0/3.0);
555
+
556
+            const double prev_actual_thresh = setup_compress_thresh(prev_value);
557
+            const double curr_actual_thresh = setup_compress_thresh(s->compress_threshold[c]);
558
+
559
+            double *const dst_ptr = (double *)frame->extended_data[c];
560
+            for (i = 0; i < frame->nb_samples; i++) {
561
+                const double localThresh = fade(prev_actual_thresh, curr_actual_thresh, i, s->fade_factors);
562
+                dst_ptr[i] = copysign(bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]);
563
+            }
564
+        }
565
+    }
566
+}
567
+
568
+static void analyze_frame(DynamicAudioNormalizerContext *s, AVFrame *frame)
569
+{
570
+    if (s->dc_correction) {
571
+        perform_dc_correction(s, frame);
572
+    }
573
+
574
+    if (s->compress_factor > DBL_EPSILON) {
575
+        perform_compression(s, frame);
576
+    }
577
+
578
+    if (s->channels_coupled) {
579
+        const double current_gain_factor = get_max_local_gain(s, frame, -1);
580
+        int c;
581
+
582
+        for (c = 0; c < s->channels; c++)
583
+            update_gain_history(s, c, current_gain_factor);
584
+    } else {
585
+        int c;
586
+
587
+        for (c = 0; c < s->channels; c++)
588
+            update_gain_history(s, c, get_max_local_gain(s, frame, c));
589
+    }
590
+}
591
+
592
+static void amplify_frame(DynamicAudioNormalizerContext *s, AVFrame *frame)
593
+{
594
+    int c, i;
595
+
596
+    for (c = 0; c < s->channels; c++) {
597
+        double *dst_ptr = (double *)frame->extended_data[c];
598
+        double current_amplification_factor;
599
+
600
+        cqueue_dequeue(s->gain_history_smoothed[c], &current_amplification_factor);
601
+
602
+        for (i = 0; i < frame->nb_samples; i++) {
603
+            const double amplification_factor = fade(s->prev_amplification_factor[c],
604
+                                                     current_amplification_factor, i,
605
+                                                     s->fade_factors);
606
+
607
+            dst_ptr[i] *= amplification_factor;
608
+
609
+            if (fabs(dst_ptr[i]) > s->peak_value)
610
+                dst_ptr[i] = copysign(s->peak_value, dst_ptr[i]);
611
+        }
612
+
613
+        s->prev_amplification_factor[c] = current_amplification_factor;
614
+    }
615
+}
616
+
617
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
618
+{
619
+    AVFilterContext *ctx = inlink->dst;
620
+    DynamicAudioNormalizerContext *s = ctx->priv;
621
+    AVFilterLink *outlink = inlink->dst->outputs[0];
622
+    int ret = 0;
623
+
624
+    if (!cqueue_empty(s->gain_history_smoothed[0])) {
625
+        AVFrame *out = ff_bufqueue_get(&s->queue);
626
+
627
+        amplify_frame(s, out);
628
+        ret = ff_filter_frame(outlink, out);
629
+    }
630
+
631
+    analyze_frame(s, in);
632
+    ff_bufqueue_add(ctx, &s->queue, in);
633
+
634
+    return ret;
635
+}
636
+
637
+static int flush_buffer(DynamicAudioNormalizerContext *s, AVFilterLink *inlink,
638
+                        AVFilterLink *outlink)
639
+{
640
+    AVFrame *out = ff_get_audio_buffer(outlink, s->frame_len);
641
+    int c, i;
642
+
643
+    if (!out)
644
+        return AVERROR(ENOMEM);
645
+
646
+    for (c = 0; c < s->channels; c++) {
647
+        double *dst_ptr = (double *)out->extended_data[c];
648
+
649
+        for (i = 0; i < out->nb_samples; i++) {
650
+            dst_ptr[i] = s->alt_boundary_mode ? DBL_EPSILON : ((s->target_rms > DBL_EPSILON) ? FFMIN(s->peak_value, s->target_rms) : s->peak_value);
651
+            if (s->dc_correction) {
652
+                dst_ptr[i] *= ((i % 2) == 1) ? -1 : 1;
653
+                dst_ptr[i] += s->dc_correction_value[c];
654
+            }
655
+        }
656
+    }
657
+
658
+    s->delay--;
659
+    return filter_frame(inlink, out);
660
+}
661
+
662
+static int request_frame(AVFilterLink *outlink)
663
+{
664
+    AVFilterContext *ctx = outlink->src;
665
+    DynamicAudioNormalizerContext *s = ctx->priv;
666
+    int ret = 0;
667
+
668
+    ret = ff_request_frame(ctx->inputs[0]);
669
+
670
+    if (ret == AVERROR_EOF && !ctx->is_disabled && s->delay)
671
+        ret = flush_buffer(s, ctx->inputs[0], outlink);
672
+
673
+    return ret;
674
+}
675
+
676
+static av_cold void uninit(AVFilterContext *ctx)
677
+{
678
+    DynamicAudioNormalizerContext *s = ctx->priv;
679
+    int c;
680
+
681
+    av_freep(&s->prev_amplification_factor);
682
+    av_freep(&s->dc_correction_value);
683
+    av_freep(&s->compress_threshold);
684
+    av_freep(&s->fade_factors[0]);
685
+    av_freep(&s->fade_factors[1]);
686
+
687
+    for (c = 0; c < s->channels; c++) {
688
+        cqueue_free(s->gain_history_original[c]);
689
+        cqueue_free(s->gain_history_minimum[c]);
690
+        cqueue_free(s->gain_history_smoothed[c]);
691
+    }
692
+
693
+    av_freep(&s->gain_history_original);
694
+    av_freep(&s->gain_history_minimum);
695
+    av_freep(&s->gain_history_smoothed);
696
+
697
+    av_freep(&s->weights);
698
+
699
+    ff_bufqueue_discard_all(&s->queue);
700
+}
701
+
702
+static const AVFilterPad avfilter_af_dynaudnorm_inputs[] = {
703
+    {
704
+        .name           = "default",
705
+        .type           = AVMEDIA_TYPE_AUDIO,
706
+        .filter_frame   = filter_frame,
707
+        .config_props   = config_input,
708
+        .needs_writable = 1,
709
+    },
710
+    { NULL }
711
+};
712
+
713
+static const AVFilterPad avfilter_af_dynaudnorm_outputs[] = {
714
+    {
715
+        .name          = "default",
716
+        .type          = AVMEDIA_TYPE_AUDIO,
717
+        .config_props  = config_output,
718
+        .request_frame = request_frame,
719
+    },
720
+    { NULL }
721
+};
722
+
723
+AVFilter ff_af_dynaudnorm = {
724
+    .name          = "dynaudnorm",
725
+    .description   = NULL_IF_CONFIG_SMALL("Dynamic Audio Normalizer."),
726
+    .query_formats = query_formats,
727
+    .priv_size     = sizeof(DynamicAudioNormalizerContext),
728
+    .init          = init,
729
+    .uninit        = uninit,
730
+    .inputs        = avfilter_af_dynaudnorm_inputs,
731
+    .outputs       = avfilter_af_dynaudnorm_outputs,
732
+    .priv_class    = &dynaudnorm_class,
733
+};
... ...
@@ -83,6 +83,7 @@ void avfilter_register_all(void)
83 83
     REGISTER_FILTER(CHORUS,         chorus,         af);
84 84
     REGISTER_FILTER(COMPAND,        compand,        af);
85 85
     REGISTER_FILTER(DCSHIFT,        dcshift,        af);
86
+    REGISTER_FILTER(DYNAUDNORM,     dynaudnorm,     af);
86 87
     REGISTER_FILTER(EARWAX,         earwax,         af);
87 88
     REGISTER_FILTER(EBUR128,        ebur128,        af);
88 89
     REGISTER_FILTER(EQUALIZER,      equalizer,      af);