Browse code

astats filter

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

Paul B Mahol authored on 2013/04/22 21:38:24
Showing 6 changed files
... ...
@@ -33,6 +33,7 @@ version <next>:
33 33
 - timeline editing with filters
34 34
 - vidstabdetect and vidstabtransform filters for video stabilization using
35 35
   the vid.stab library
36
+- astats filter
36 37
 
37 38
 
38 39
 version 1.2:
... ...
@@ -990,6 +990,51 @@ the data is treated as if all the planes were concatenated.
990 990
 A list of Adler-32 checksums for each data plane.
991 991
 @end table
992 992
 
993
+@section astats
994
+
995
+Display time domain statistical information about the audio channels.
996
+Statistics are calculated and displayed for each audio channel and,
997
+where applicable, an overall figure is also given.
998
+
999
+The filter accepts the following option:
1000
+@table @option
1001
+@item length
1002
+Short window length in seconds, used for peak and trough RMS measurement.
1003
+Default is @code{0.05} (50 miliseconds). Allowed range is @code{[0.1 - 10]}.
1004
+@end table
1005
+
1006
+A description of each shown parameter follows:
1007
+
1008
+@table @option
1009
+@item DC offset
1010
+Mean amplitude displacement from zero.
1011
+
1012
+@item Min level
1013
+Minimal sample level.
1014
+
1015
+@item Max level
1016
+Maximal sample level.
1017
+
1018
+@item Peak level dB
1019
+@item RMS level dB
1020
+Standard peak and RMS level measured in dBFS.
1021
+
1022
+@item RMS peak dB
1023
+@item RMS trough dB
1024
+Peak and trough values for RMS level measured over a short window.
1025
+
1026
+@item Crest factor
1027
+Standard ratio of peak to RMS level (note: not in dB).
1028
+
1029
+@item Flat factor
1030
+Flatness (i.e. consecutive samples with the same value) of the signal at its peak levels
1031
+(i.e. either @var{Min level} or @var{Max level}).
1032
+
1033
+@item Peak count
1034
+Number of occasions (not the number of samples) that the signal attained either
1035
+@var{Min level} or @var{Max level}.
1036
+@end table
1037
+
993 1038
 @section astreamsync
994 1039
 
995 1040
 Forward two audio streams and control the order the buffers are forwarded.
... ...
@@ -69,6 +69,7 @@ OBJS-$(CONFIG_ASETRATE_FILTER)               += af_asetrate.o
69 69
 OBJS-$(CONFIG_ASETTB_FILTER)                 += f_settb.o
70 70
 OBJS-$(CONFIG_ASHOWINFO_FILTER)              += af_ashowinfo.o
71 71
 OBJS-$(CONFIG_ASPLIT_FILTER)                 += split.o
72
+OBJS-$(CONFIG_ASTATS_FILTER)                 += af_astats.o
72 73
 OBJS-$(CONFIG_ASTREAMSYNC_FILTER)            += af_astreamsync.o
73 74
 OBJS-$(CONFIG_ASYNCTS_FILTER)                += af_asyncts.o
74 75
 OBJS-$(CONFIG_ATEMPO_FILTER)                 += af_atempo.o
75 76
new file mode 100644
... ...
@@ -0,0 +1,274 @@
0
+/*
1
+ * Copyright (c) 2009 Rob Sykes <robs@users.sourceforge.net>
2
+ * Copyright (c) 2013 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 <float.h>
22
+
23
+#include "libavutil/opt.h"
24
+#include "audio.h"
25
+#include "avfilter.h"
26
+#include "internal.h"
27
+
28
+typedef struct ChannelStats {
29
+    double last;
30
+    double sigma_x, sigma_x2;
31
+    double avg_sigma_x2, min_sigma_x2, max_sigma_x2;
32
+    double min, max;
33
+    double min_run, max_run;
34
+    double min_runs, max_runs;
35
+    uint64_t min_count, max_count;
36
+    uint64_t nb_samples;
37
+} ChannelStats;
38
+
39
+typedef struct {
40
+    const AVClass *class;
41
+    ChannelStats *chstats;
42
+    int nb_channels;
43
+    uint64_t tc_samples;
44
+    double time_constant;
45
+    double mult;
46
+} AudioStatsContext;
47
+
48
+#define OFFSET(x) offsetof(AudioStatsContext, x)
49
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50
+
51
+static const AVOption astats_options[] = {
52
+    { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=.05}, .01, 10, FLAGS },
53
+    {NULL},
54
+};
55
+
56
+AVFILTER_DEFINE_CLASS(astats);
57
+
58
+static int query_formats(AVFilterContext *ctx)
59
+{
60
+    AVFilterFormats *formats;
61
+    AVFilterChannelLayouts *layouts;
62
+    static const enum AVSampleFormat sample_fmts[] = {
63
+        AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
64
+        AV_SAMPLE_FMT_NONE
65
+    };
66
+
67
+    layouts = ff_all_channel_layouts();
68
+    if (!layouts)
69
+        return AVERROR(ENOMEM);
70
+    ff_set_common_channel_layouts(ctx, layouts);
71
+
72
+    formats = ff_make_format_list(sample_fmts);
73
+    if (!formats)
74
+        return AVERROR(ENOMEM);
75
+    ff_set_common_formats(ctx, formats);
76
+
77
+    formats = ff_all_samplerates();
78
+    if (!formats)
79
+        return AVERROR(ENOMEM);
80
+    ff_set_common_samplerates(ctx, formats);
81
+
82
+    return 0;
83
+}
84
+
85
+static int config_output(AVFilterLink *outlink)
86
+{
87
+    AudioStatsContext *s = outlink->src->priv;
88
+    int c;
89
+
90
+    s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
91
+    if (!s->chstats)
92
+        return AVERROR(ENOMEM);
93
+    s->nb_channels = outlink->channels;
94
+    s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
95
+    s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5;
96
+
97
+    for (c = 0; c < s->nb_channels; c++) {
98
+        ChannelStats *p = &s->chstats[c];
99
+
100
+        p->min = p->min_sigma_x2 = DBL_MAX;
101
+        p->max = p->max_sigma_x2 = DBL_MIN;
102
+    }
103
+
104
+    return 0;
105
+}
106
+
107
+static inline void stat(AudioStatsContext *s, ChannelStats *p, double d)
108
+{
109
+    if (d < p->min) {
110
+        p->min = d;
111
+        p->min_run = 1;
112
+        p->min_runs = 0;
113
+        p->min_count = 1;
114
+    } else if (d == p->min) {
115
+        p->min_count++;
116
+        p->min_run = d == p->last ? p->min_run + 1 : 1;
117
+    } else if (p->last == p->min) {
118
+        p->min_runs += p->min_run * p->min_run;
119
+    }
120
+
121
+    if (d > p->max) {
122
+        p->max = d;
123
+        p->max_run = 1;
124
+        p->max_runs = 0;
125
+        p->max_count = 1;
126
+    } else if (d == p->max) {
127
+        p->max_count++;
128
+        p->max_run = d == p->last ? p->max_run + 1 : 1;
129
+    } else if (p->last == p->max) {
130
+        p->max_runs += p->max_run * p->max_run;
131
+    }
132
+
133
+    p->sigma_x += d;
134
+    p->sigma_x2 += d * d;
135
+    p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * d * d;
136
+    p->last = d;
137
+
138
+    if (p->nb_samples >= s->tc_samples) {
139
+        p->max_sigma_x2 = FFMAX(p->max_sigma_x2, p->avg_sigma_x2);
140
+        p->min_sigma_x2 = FFMIN(p->min_sigma_x2, p->avg_sigma_x2);
141
+    }
142
+    p->nb_samples++;
143
+}
144
+
145
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
146
+{
147
+    AudioStatsContext *s = inlink->dst->priv;
148
+    const int channels = s->nb_channels;
149
+    const double *src;
150
+    int i, c;
151
+
152
+    switch (inlink->format) {
153
+    case AV_SAMPLE_FMT_DBLP:
154
+        for (c = 0; c < channels; c++) {
155
+            ChannelStats *p = &s->chstats[c];
156
+            src = (const double *)buf->extended_data[c];
157
+
158
+            for (i = 0; i < buf->nb_samples; i++, src++)
159
+                stat(s, p, *src);
160
+        }
161
+        break;
162
+    case AV_SAMPLE_FMT_DBL:
163
+        src = (const double *)buf->extended_data[0];
164
+
165
+        for (i = 0; i < buf->nb_samples; i++) {
166
+            for (c = 0; c < channels; c++, src++)
167
+                stat(s, &s->chstats[c], *src);
168
+        }
169
+        break;
170
+    }
171
+
172
+    return ff_filter_frame(inlink->dst->outputs[0], buf);
173
+}
174
+
175
+#define LINEAR_TO_DB(x) (log10(x) * 20)
176
+
177
+static void print_stats(AVFilterContext *ctx)
178
+{
179
+    AudioStatsContext *s = ctx->priv;
180
+    uint64_t min_count = 0, max_count = 0, nb_samples = 0;
181
+    double min_runs = 0, max_runs = 0,
182
+           min = DBL_MAX, max = DBL_MIN,
183
+           max_sigma_x = 0,
184
+           sigma_x = 0,
185
+           sigma_x2 = 0,
186
+           min_sigma_x2 = DBL_MAX,
187
+           max_sigma_x2 = DBL_MIN;
188
+    int c;
189
+
190
+    for (c = 0; c < s->nb_channels; c++) {
191
+        ChannelStats *p = &s->chstats[c];
192
+
193
+        if (p->nb_samples < s->tc_samples)
194
+            p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
195
+
196
+        min = FFMIN(min, p->min);
197
+        max = FFMAX(max, p->max);
198
+        min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
199
+        max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
200
+        sigma_x += p->sigma_x;
201
+        sigma_x2 += p->sigma_x2;
202
+        min_count += p->min_count;
203
+        max_count += p->max_count;
204
+        min_runs += p->min_runs;
205
+        max_runs += p->max_runs;
206
+        nb_samples += p->nb_samples;
207
+        if (fabs(p->sigma_x) > fabs(max_sigma_x))
208
+            max_sigma_x = p->sigma_x;
209
+
210
+        av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
211
+        av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
212
+        av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
213
+        av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
214
+        av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->min, p->max)));
215
+        av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
216
+        av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
217
+        if (p->min_sigma_x2 != 1)
218
+            av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
219
+        av_log(ctx, AV_LOG_INFO, "Crest factor: %f\n", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
220
+        av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
221
+        av_log(ctx, AV_LOG_INFO, "Peak count: %lld\n", p->min_count + p->max_count);
222
+    }
223
+
224
+    av_log(ctx, AV_LOG_INFO, "Overall\n");
225
+    av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
226
+    av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
227
+    av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
228
+    av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-min, max)));
229
+    av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
230
+    av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
231
+    if (min_sigma_x2 != 1)
232
+        av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
233
+    av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
234
+    av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
235
+    av_log(ctx, AV_LOG_INFO, "Number of samples: %lld\n", nb_samples / s->nb_channels);
236
+}
237
+
238
+static void uninit(AVFilterContext *ctx)
239
+{
240
+    AudioStatsContext *s = ctx->priv;
241
+
242
+    print_stats(ctx);
243
+    av_freep(&s->chstats);
244
+}
245
+
246
+static const AVFilterPad astats_inputs[] = {
247
+    {
248
+        .name         = "default",
249
+        .type         = AVMEDIA_TYPE_AUDIO,
250
+        .filter_frame = filter_frame,
251
+    },
252
+    { NULL }
253
+};
254
+
255
+static const AVFilterPad astats_outputs[] = {
256
+    {
257
+        .name         = "default",
258
+        .type         = AVMEDIA_TYPE_AUDIO,
259
+        .config_props = config_output,
260
+    },
261
+    { NULL }
262
+};
263
+
264
+AVFilter avfilter_af_astats = {
265
+    .name          = "astats",
266
+    .description   = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
267
+    .query_formats = query_formats,
268
+    .priv_size     = sizeof(AudioStatsContext),
269
+    .priv_class    = &astats_class,
270
+    .uninit        = uninit,
271
+    .inputs        = astats_inputs,
272
+    .outputs       = astats_outputs,
273
+};
... ...
@@ -67,6 +67,7 @@ void avfilter_register_all(void)
67 67
     REGISTER_FILTER(ASETTB,         asettb,         af);
68 68
     REGISTER_FILTER(ASHOWINFO,      ashowinfo,      af);
69 69
     REGISTER_FILTER(ASPLIT,         asplit,         af);
70
+    REGISTER_FILTER(ASTATS,         astats,         af);
70 71
     REGISTER_FILTER(ASTREAMSYNC,    astreamsync,    af);
71 72
     REGISTER_FILTER(ASYNCTS,        asyncts,        af);
72 73
     REGISTER_FILTER(ATEMPO,         atempo,         af);
... ...
@@ -29,8 +29,8 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32
-#define LIBAVFILTER_VERSION_MINOR  60
33
-#define LIBAVFILTER_VERSION_MICRO 102
32
+#define LIBAVFILTER_VERSION_MINOR  61
33
+#define LIBAVFILTER_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \