2 * Copyright (c) 2009 Rob Sykes <robs@users.sourceforge.net>
3 * Copyright (c) 2013 Paul B Mahol
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/opt.h"
29 typedef struct ChannelStats {
31 double sigma_x, sigma_x2;
32 double avg_sigma_x2, min_sigma_x2, max_sigma_x2;
34 double min_run, max_run;
35 double min_runs, max_runs;
36 uint64_t min_count, max_count;
42 ChannelStats *chstats;
49 #define OFFSET(x) offsetof(AudioStatsContext, x)
50 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
52 static const AVOption astats_options[] = {
53 { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=.05}, .01, 10, FLAGS },
57 AVFILTER_DEFINE_CLASS(astats);
59 static int query_formats(AVFilterContext *ctx)
61 AVFilterFormats *formats;
62 AVFilterChannelLayouts *layouts;
63 static const enum AVSampleFormat sample_fmts[] = {
64 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
69 layouts = ff_all_channel_layouts();
71 return AVERROR(ENOMEM);
72 ret = ff_set_common_channel_layouts(ctx, layouts);
76 formats = ff_make_format_list(sample_fmts);
78 return AVERROR(ENOMEM);
79 ret = ff_set_common_formats(ctx, formats);
83 formats = ff_all_samplerates();
85 return AVERROR(ENOMEM);
86 return ff_set_common_samplerates(ctx, formats);
89 static int config_output(AVFilterLink *outlink)
91 AudioStatsContext *s = outlink->src->priv;
94 s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
96 return AVERROR(ENOMEM);
97 s->nb_channels = outlink->channels;
98 s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
99 s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5;
101 for (c = 0; c < s->nb_channels; c++) {
102 ChannelStats *p = &s->chstats[c];
104 p->min = p->min_sigma_x2 = DBL_MAX;
105 p->max = p->max_sigma_x2 = DBL_MIN;
111 static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d)
118 } else if (d == p->min) {
120 p->min_run = d == p->last ? p->min_run + 1 : 1;
121 } else if (p->last == p->min) {
122 p->min_runs += p->min_run * p->min_run;
130 } else if (d == p->max) {
132 p->max_run = d == p->last ? p->max_run + 1 : 1;
133 } else if (p->last == p->max) {
134 p->max_runs += p->max_run * p->max_run;
138 p->sigma_x2 += d * d;
139 p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * d * d;
142 if (p->nb_samples >= s->tc_samples) {
143 p->max_sigma_x2 = FFMAX(p->max_sigma_x2, p->avg_sigma_x2);
144 p->min_sigma_x2 = FFMIN(p->min_sigma_x2, p->avg_sigma_x2);
149 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
151 AudioStatsContext *s = inlink->dst->priv;
152 const int channels = s->nb_channels;
156 switch (inlink->format) {
157 case AV_SAMPLE_FMT_DBLP:
158 for (c = 0; c < channels; c++) {
159 ChannelStats *p = &s->chstats[c];
160 src = (const double *)buf->extended_data[c];
162 for (i = 0; i < buf->nb_samples; i++, src++)
163 update_stat(s, p, *src);
166 case AV_SAMPLE_FMT_DBL:
167 src = (const double *)buf->extended_data[0];
169 for (i = 0; i < buf->nb_samples; i++) {
170 for (c = 0; c < channels; c++, src++)
171 update_stat(s, &s->chstats[c], *src);
176 return ff_filter_frame(inlink->dst->outputs[0], buf);
179 #define LINEAR_TO_DB(x) (log10(x) * 20)
181 static void print_stats(AVFilterContext *ctx)
183 AudioStatsContext *s = ctx->priv;
184 uint64_t min_count = 0, max_count = 0, nb_samples = 0;
185 double min_runs = 0, max_runs = 0,
186 min = DBL_MAX, max = DBL_MIN,
190 min_sigma_x2 = DBL_MAX,
191 max_sigma_x2 = DBL_MIN;
194 for (c = 0; c < s->nb_channels; c++) {
195 ChannelStats *p = &s->chstats[c];
197 if (p->nb_samples < s->tc_samples)
198 p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
200 min = FFMIN(min, p->min);
201 max = FFMAX(max, p->max);
202 min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
203 max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
204 sigma_x += p->sigma_x;
205 sigma_x2 += p->sigma_x2;
206 min_count += p->min_count;
207 max_count += p->max_count;
208 min_runs += p->min_runs;
209 max_runs += p->max_runs;
210 nb_samples += p->nb_samples;
211 if (fabs(p->sigma_x) > fabs(max_sigma_x))
212 max_sigma_x = p->sigma_x;
214 av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
215 av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
216 av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
217 av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
218 av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->min, p->max)));
219 av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
220 av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
221 if (p->min_sigma_x2 != 1)
222 av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
223 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);
224 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)));
225 av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
228 av_log(ctx, AV_LOG_INFO, "Overall\n");
229 av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
230 av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
231 av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
232 av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-min, max)));
233 av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
234 av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
235 if (min_sigma_x2 != 1)
236 av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
237 av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
238 av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
239 av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
242 static av_cold void uninit(AVFilterContext *ctx)
244 AudioStatsContext *s = ctx->priv;
248 av_freep(&s->chstats);
251 static const AVFilterPad astats_inputs[] = {
254 .type = AVMEDIA_TYPE_AUDIO,
255 .filter_frame = filter_frame,
260 static const AVFilterPad astats_outputs[] = {
263 .type = AVMEDIA_TYPE_AUDIO,
264 .config_props = config_output,
269 AVFilter ff_af_astats = {
271 .description = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
272 .query_formats = query_formats,
273 .priv_size = sizeof(AudioStatsContext),
274 .priv_class = &astats_class,
276 .inputs = astats_inputs,
277 .outputs = astats_outputs,