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 double min_diff, max_diff;
39 uint64_t min_count, max_count;
45 ChannelStats *chstats;
55 #define OFFSET(x) offsetof(AudioStatsContext, x)
56 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
58 static const AVOption astats_options[] = {
59 { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=.05}, .01, 10, FLAGS },
60 { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
61 { "reset", "recalculate stats after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
65 AVFILTER_DEFINE_CLASS(astats);
67 static int query_formats(AVFilterContext *ctx)
69 AVFilterFormats *formats;
70 AVFilterChannelLayouts *layouts;
71 static const enum AVSampleFormat sample_fmts[] = {
72 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
77 layouts = ff_all_channel_layouts();
79 return AVERROR(ENOMEM);
80 ret = ff_set_common_channel_layouts(ctx, layouts);
84 formats = ff_make_format_list(sample_fmts);
86 return AVERROR(ENOMEM);
87 ret = ff_set_common_formats(ctx, formats);
91 formats = ff_all_samplerates();
93 return AVERROR(ENOMEM);
94 return ff_set_common_samplerates(ctx, formats);
97 static void reset_stats(AudioStatsContext *s)
101 memset(s->chstats, 0, sizeof(*s->chstats));
103 for (c = 0; c < s->nb_channels; c++) {
104 ChannelStats *p = &s->chstats[c];
106 p->min = p->min_sigma_x2 = DBL_MAX;
107 p->max = p->max_sigma_x2 = DBL_MIN;
108 p->min_diff = p->max_diff = -1;
112 static int config_output(AVFilterLink *outlink)
114 AudioStatsContext *s = outlink->src->priv;
116 s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
118 return AVERROR(ENOMEM);
119 s->nb_channels = outlink->channels;
120 s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
121 s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5;
128 static unsigned bit_depth(uint64_t mask)
130 unsigned result = 64;
132 for (; result && !(mask & 1); --result, mask >>= 1);
137 static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d)
144 } else if (d == p->min) {
146 p->min_run = d == p->last ? p->min_run + 1 : 1;
147 } else if (p->last == p->min) {
148 p->min_runs += p->min_run * p->min_run;
156 } else if (d == p->max) {
158 p->max_run = d == p->last ? p->max_run + 1 : 1;
159 } else if (p->last == p->max) {
160 p->max_runs += p->max_run * p->max_run;
164 p->sigma_x2 += d * d;
165 p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * d * d;
166 p->min_diff = FFMIN(p->min_diff == -1 ? DBL_MAX : p->min_diff, FFABS(d - (p->min_diff == -1 ? DBL_MAX : p->last)));
167 p->max_diff = FFMAX(p->max_diff, FFABS(d - (p->max_diff == -1 ? d : p->last)));
168 p->diff1_sum += FFABS(d - p->last);
170 p->mask |= llrint(d * (1LLU<<63));
172 if (p->nb_samples >= s->tc_samples) {
173 p->max_sigma_x2 = FFMAX(p->max_sigma_x2, p->avg_sigma_x2);
174 p->min_sigma_x2 = FFMIN(p->min_sigma_x2, p->avg_sigma_x2);
179 static void set_meta(AVDictionary **metadata, int chan, const char *key,
180 const char *fmt, double val)
185 snprintf(value, sizeof(value), fmt, val);
187 snprintf(key2, sizeof(key2), "lavfi.astats.%d.%s", chan, key);
189 snprintf(key2, sizeof(key2), "lavfi.astats.%s", key);
190 av_dict_set(metadata, key2, value, 0);
193 #define LINEAR_TO_DB(x) (log10(x) * 20)
195 static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
197 uint64_t mask = 0, min_count = 0, max_count = 0, nb_samples = 0;
198 double min_runs = 0, max_runs = 0,
199 min = DBL_MAX, max = DBL_MIN, min_diff = DBL_MAX, max_diff = 0,
204 min_sigma_x2 = DBL_MAX,
205 max_sigma_x2 = DBL_MIN;
208 for (c = 0; c < s->nb_channels; c++) {
209 ChannelStats *p = &s->chstats[c];
211 if (p->nb_samples < s->tc_samples)
212 p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
214 min = FFMIN(min, p->min);
215 max = FFMAX(max, p->max);
216 min_diff = FFMIN(min_diff, p->min_diff);
217 max_diff = FFMAX(max_diff, p->max_diff);
218 diff1_sum += p->diff1_sum,
219 min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
220 max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
221 sigma_x += p->sigma_x;
222 sigma_x2 += p->sigma_x2;
223 min_count += p->min_count;
224 max_count += p->max_count;
225 min_runs += p->min_runs;
226 max_runs += p->max_runs;
228 nb_samples += p->nb_samples;
229 if (fabs(p->sigma_x) > fabs(max_sigma_x))
230 max_sigma_x = p->sigma_x;
232 set_meta(metadata, c + 1, "DC_offset", "%f", p->sigma_x / p->nb_samples);
233 set_meta(metadata, c + 1, "Min_level", "%f", p->min);
234 set_meta(metadata, c + 1, "Max_level", "%f", p->max);
235 set_meta(metadata, c + 1, "Min_difference", "%f", p->min_diff);
236 set_meta(metadata, c + 1, "Max_difference", "%f", p->max_diff);
237 set_meta(metadata, c + 1, "Mean_difference", "%f", p->diff1_sum / (p->nb_samples - 1));
238 set_meta(metadata, c + 1, "Peak_level", "%f", LINEAR_TO_DB(FFMAX(-p->min, p->max)));
239 set_meta(metadata, c + 1, "RMS_level", "%f", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
240 set_meta(metadata, c + 1, "RMS_peak", "%f", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
241 set_meta(metadata, c + 1, "RMS_trough", "%f", LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
242 set_meta(metadata, c + 1, "Crest_factor", "%f", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
243 set_meta(metadata, c + 1, "Flat_factor", "%f", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
244 set_meta(metadata, c + 1, "Peak_count", "%f", (float)(p->min_count + p->max_count));
245 set_meta(metadata, c + 1, "Bit_depth", "%f", bit_depth(p->mask));
248 set_meta(metadata, 0, "Overall.DC_offset", "%f", max_sigma_x / (nb_samples / s->nb_channels));
249 set_meta(metadata, 0, "Overall.Min_level", "%f", min);
250 set_meta(metadata, 0, "Overall.Max_level", "%f", max);
251 set_meta(metadata, 0, "Overall.Min_difference", "%f", min_diff);
252 set_meta(metadata, 0, "Overall.Max_difference", "%f", max_diff);
253 set_meta(metadata, 0, "Overall.Mean_difference", "%f", diff1_sum / (nb_samples - s->nb_channels));
254 set_meta(metadata, 0, "Overall.Peak_level", "%f", LINEAR_TO_DB(FFMAX(-min, max)));
255 set_meta(metadata, 0, "Overall.RMS_level", "%f", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
256 set_meta(metadata, 0, "Overall.RMS_peak", "%f", LINEAR_TO_DB(sqrt(max_sigma_x2)));
257 set_meta(metadata, 0, "Overall.RMS_trough", "%f", LINEAR_TO_DB(sqrt(min_sigma_x2)));
258 set_meta(metadata, 0, "Overall.Flat_factor", "%f", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
259 set_meta(metadata, 0, "Overall.Peak_count", "%f", (float)(min_count + max_count) / (double)s->nb_channels);
260 set_meta(metadata, 0, "Overall.Bit_depth", "%f", bit_depth(mask));
261 set_meta(metadata, 0, "Overall.Number_of_samples", "%f", nb_samples / s->nb_channels);
264 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
266 AudioStatsContext *s = inlink->dst->priv;
267 AVDictionary **metadata = avpriv_frame_get_metadatap(buf);
268 const int channels = s->nb_channels;
272 switch (inlink->format) {
273 case AV_SAMPLE_FMT_DBLP:
274 for (c = 0; c < channels; c++) {
275 ChannelStats *p = &s->chstats[c];
276 src = (const double *)buf->extended_data[c];
278 for (i = 0; i < buf->nb_samples; i++, src++)
279 update_stat(s, p, *src);
282 case AV_SAMPLE_FMT_DBL:
283 src = (const double *)buf->extended_data[0];
285 for (i = 0; i < buf->nb_samples; i++) {
286 for (c = 0; c < channels; c++, src++)
287 update_stat(s, &s->chstats[c], *src);
293 set_metadata(s, metadata);
295 if (s->reset_count > 0) {
297 if (s->nb_frames >= s->reset_count) {
303 return ff_filter_frame(inlink->dst->outputs[0], buf);
306 static void print_stats(AVFilterContext *ctx)
308 AudioStatsContext *s = ctx->priv;
309 uint64_t mask = 0, min_count = 0, max_count = 0, nb_samples = 0;
310 double min_runs = 0, max_runs = 0,
311 min = DBL_MAX, max = DBL_MIN, min_diff = DBL_MAX, max_diff = 0,
316 min_sigma_x2 = DBL_MAX,
317 max_sigma_x2 = DBL_MIN;
320 for (c = 0; c < s->nb_channels; c++) {
321 ChannelStats *p = &s->chstats[c];
323 if (p->nb_samples < s->tc_samples)
324 p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
326 min = FFMIN(min, p->min);
327 max = FFMAX(max, p->max);
328 min_diff = FFMIN(min_diff, p->min_diff);
329 max_diff = FFMAX(max_diff, p->max_diff);
330 diff1_sum += p->diff1_sum,
331 min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
332 max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
333 sigma_x += p->sigma_x;
334 sigma_x2 += p->sigma_x2;
335 min_count += p->min_count;
336 max_count += p->max_count;
337 min_runs += p->min_runs;
338 max_runs += p->max_runs;
340 nb_samples += p->nb_samples;
341 if (fabs(p->sigma_x) > fabs(max_sigma_x))
342 max_sigma_x = p->sigma_x;
344 av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
345 av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
346 av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
347 av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
348 av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", p->min_diff);
349 av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", p->max_diff);
350 av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", p->diff1_sum / (p->nb_samples - 1));
351 av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->min, p->max)));
352 av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
353 av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
354 if (p->min_sigma_x2 != 1)
355 av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
356 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);
357 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)));
358 av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
359 av_log(ctx, AV_LOG_INFO, "Bit depth: %u\n", bit_depth(p->mask));
362 av_log(ctx, AV_LOG_INFO, "Overall\n");
363 av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
364 av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
365 av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
366 av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", min_diff);
367 av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", max_diff);
368 av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", diff1_sum / (nb_samples - s->nb_channels));
369 av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-min, max)));
370 av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
371 av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
372 if (min_sigma_x2 != 1)
373 av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
374 av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
375 av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
376 av_log(ctx, AV_LOG_INFO, "Bit depth: %u\n", bit_depth(mask));
377 av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
380 static av_cold void uninit(AVFilterContext *ctx)
382 AudioStatsContext *s = ctx->priv;
386 av_freep(&s->chstats);
389 static const AVFilterPad astats_inputs[] = {
392 .type = AVMEDIA_TYPE_AUDIO,
393 .filter_frame = filter_frame,
398 static const AVFilterPad astats_outputs[] = {
401 .type = AVMEDIA_TYPE_AUDIO,
402 .config_props = config_output,
407 AVFilter ff_af_astats = {
409 .description = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
410 .query_formats = query_formats,
411 .priv_size = sizeof(AudioStatsContext),
412 .priv_class = &astats_class,
414 .inputs = astats_inputs,
415 .outputs = astats_outputs,