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 #define MEASURE_ALL UINT_MAX
30 #define MEASURE_NONE 0
32 #define MEASURE_DC_OFFSET (1 << 0)
33 #define MEASURE_MIN_LEVEL (1 << 1)
34 #define MEASURE_MAX_LEVEL (1 << 2)
35 #define MEASURE_MIN_DIFFERENCE (1 << 3)
36 #define MEASURE_MAX_DIFFERENCE (1 << 4)
37 #define MEASURE_MEAN_DIFFERENCE (1 << 5)
38 #define MEASURE_RMS_DIFFERENCE (1 << 6)
39 #define MEASURE_PEAK_LEVEL (1 << 7)
40 #define MEASURE_RMS_LEVEL (1 << 8)
41 #define MEASURE_RMS_PEAK (1 << 9)
42 #define MEASURE_RMS_TROUGH (1 << 10)
43 #define MEASURE_CREST_FACTOR (1 << 11)
44 #define MEASURE_FLAT_FACTOR (1 << 12)
45 #define MEASURE_PEAK_COUNT (1 << 13)
46 #define MEASURE_BIT_DEPTH (1 << 14)
47 #define MEASURE_DYNAMIC_RANGE (1 << 15)
48 #define MEASURE_ZERO_CROSSINGS (1 << 16)
49 #define MEASURE_ZERO_CROSSINGS_RATE (1 << 17)
50 #define MEASURE_NUMBER_OF_SAMPLES (1 << 18)
52 typedef struct ChannelStats {
56 double sigma_x, sigma_x2;
57 double avg_sigma_x2, min_sigma_x2, max_sigma_x2;
60 double min_run, max_run;
61 double min_runs, max_runs;
62 double min_diff, max_diff;
66 uint64_t min_count, max_count;
71 typedef struct AudioStatsContext {
73 ChannelStats *chstats;
82 int measure_perchannel;
86 #define OFFSET(x) offsetof(AudioStatsContext, x)
87 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
89 static const AVOption astats_options[] = {
90 { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=.05}, .01, 10, FLAGS },
91 { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
92 { "reset", "recalculate stats after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
93 { "measure_perchannel", "only measure_perchannel these per-channel statistics", OFFSET(measure_perchannel), AV_OPT_TYPE_FLAGS, {.i64=MEASURE_ALL}, 0, UINT_MAX, FLAGS, "measure" },
94 { "none" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NONE }, 0, 0, FLAGS, "measure" },
95 { "all" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ALL }, 0, 0, FLAGS, "measure" },
96 { "DC_offset" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_DC_OFFSET }, 0, 0, FLAGS, "measure" },
97 { "Min_level" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MIN_LEVEL }, 0, 0, FLAGS, "measure" },
98 { "Max_level" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MAX_LEVEL }, 0, 0, FLAGS, "measure" },
99 { "Min_difference" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MIN_DIFFERENCE }, 0, 0, FLAGS, "measure" },
100 { "Max_difference" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MAX_DIFFERENCE }, 0, 0, FLAGS, "measure" },
101 { "Mean_difference" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MEAN_DIFFERENCE }, 0, 0, FLAGS, "measure" },
102 { "RMS_difference" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_DIFFERENCE }, 0, 0, FLAGS, "measure" },
103 { "Peak_level" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_PEAK_LEVEL }, 0, 0, FLAGS, "measure" },
104 { "RMS_level" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_LEVEL }, 0, 0, FLAGS, "measure" },
105 { "RMS_peak" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_PEAK }, 0, 0, FLAGS, "measure" },
106 { "RMS_trough" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_TROUGH }, 0, 0, FLAGS, "measure" },
107 { "Crest_factor" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_CREST_FACTOR }, 0, 0, FLAGS, "measure" },
108 { "Flat_factor" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_FLAT_FACTOR }, 0, 0, FLAGS, "measure" },
109 { "Peak_count" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_PEAK_COUNT }, 0, 0, FLAGS, "measure" },
110 { "Bit_depth" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_BIT_DEPTH }, 0, 0, FLAGS, "measure" },
111 { "Dynamic_range" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_DYNAMIC_RANGE }, 0, 0, FLAGS, "measure" },
112 { "Zero_crossings" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ZERO_CROSSINGS }, 0, 0, FLAGS, "measure" },
113 { "Zero_crossings_rate" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ZERO_CROSSINGS_RATE }, 0, 0, FLAGS, "measure" },
114 { "Number_of_samples" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_SAMPLES }, 0, 0, FLAGS, "measure" },
115 { "measure_overall", "only measure_perchannel these overall statistics", OFFSET(measure_overall), AV_OPT_TYPE_FLAGS, {.i64=MEASURE_ALL}, 0, UINT_MAX, FLAGS, "measure" },
119 AVFILTER_DEFINE_CLASS(astats);
121 static int query_formats(AVFilterContext *ctx)
123 AVFilterFormats *formats;
124 AVFilterChannelLayouts *layouts;
125 static const enum AVSampleFormat sample_fmts[] = {
126 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
127 AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
128 AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64P,
129 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
130 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
135 layouts = ff_all_channel_counts();
137 return AVERROR(ENOMEM);
138 ret = ff_set_common_channel_layouts(ctx, layouts);
142 formats = ff_make_format_list(sample_fmts);
144 return AVERROR(ENOMEM);
145 ret = ff_set_common_formats(ctx, formats);
149 formats = ff_all_samplerates();
151 return AVERROR(ENOMEM);
152 return ff_set_common_samplerates(ctx, formats);
155 static void reset_stats(AudioStatsContext *s)
159 for (c = 0; c < s->nb_channels; c++) {
160 ChannelStats *p = &s->chstats[c];
162 p->min = p->nmin = p->min_sigma_x2 = DBL_MAX;
163 p->max = p->nmax = p->max_sigma_x2 = DBL_MIN;
164 p->min_non_zero = DBL_MAX;
165 p->min_diff = DBL_MAX;
166 p->max_diff = DBL_MIN;
177 p->imask = 0xFFFFFFFFFFFFFFFF;
185 static int config_output(AVFilterLink *outlink)
187 AudioStatsContext *s = outlink->src->priv;
189 s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
191 return AVERROR(ENOMEM);
192 s->nb_channels = outlink->channels;
193 s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
194 s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5;
196 s->maxbitdepth = av_get_bytes_per_sample(outlink->format) * 8;
203 static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
205 unsigned result = s->maxbitdepth;
207 mask = mask & (~imask);
209 for (; result && !(mask & 1); --result, mask >>= 1);
214 for (; result; --result, mask >>= 1)
219 static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d, double nd, int64_t i)
227 } else if (d == p->min) {
229 p->min_run = d == p->last ? p->min_run + 1 : 1;
230 } else if (p->last == p->min) {
231 p->min_runs += p->min_run * p->min_run;
234 if (d != 0 && FFABS(d) < p->min_non_zero)
235 p->min_non_zero = FFABS(d);
243 } else if (d == p->max) {
245 p->max_run = d == p->last ? p->max_run + 1 : 1;
246 } else if (p->last == p->max) {
247 p->max_runs += p->max_run * p->max_run;
251 p->zero_runs += FFSIGN(d) != FFSIGN(p->last_non_zero);
252 p->last_non_zero = d;
256 p->sigma_x2 += nd * nd;
257 p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * nd * nd;
258 p->min_diff = FFMIN(p->min_diff, fabs(d - p->last));
259 p->max_diff = FFMAX(p->max_diff, fabs(d - p->last));
260 p->diff1_sum += fabs(d - p->last);
261 p->diff1_sum_x2 += (d - p->last) * (d - p->last);
266 if (p->nb_samples >= s->tc_samples) {
267 p->max_sigma_x2 = FFMAX(p->max_sigma_x2, p->avg_sigma_x2);
268 p->min_sigma_x2 = FFMIN(p->min_sigma_x2, p->avg_sigma_x2);
273 static void set_meta(AVDictionary **metadata, int chan, const char *key,
274 const char *fmt, double val)
279 snprintf(value, sizeof(value), fmt, val);
281 snprintf(key2, sizeof(key2), "lavfi.astats.%d.%s", chan, key);
283 snprintf(key2, sizeof(key2), "lavfi.astats.%s", key);
284 av_dict_set(metadata, key2, value, 0);
287 #define LINEAR_TO_DB(x) (log10(x) * 20)
289 static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
291 uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0;
292 double min_runs = 0, max_runs = 0,
293 min = DBL_MAX, max = DBL_MIN, min_diff = DBL_MAX, max_diff = 0,
294 nmin = DBL_MAX, nmax = DBL_MIN,
300 min_sigma_x2 = DBL_MAX,
301 max_sigma_x2 = DBL_MIN;
305 for (c = 0; c < s->nb_channels; c++) {
306 ChannelStats *p = &s->chstats[c];
308 if (p->nb_samples < s->tc_samples)
309 p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
311 min = FFMIN(min, p->min);
312 max = FFMAX(max, p->max);
313 nmin = FFMIN(nmin, p->nmin);
314 nmax = FFMAX(nmax, p->nmax);
315 min_diff = FFMIN(min_diff, p->min_diff);
316 max_diff = FFMAX(max_diff, p->max_diff);
317 diff1_sum += p->diff1_sum;
318 diff1_sum_x2 += p->diff1_sum_x2;
319 min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
320 max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
321 sigma_x += p->sigma_x;
322 sigma_x2 += p->sigma_x2;
323 min_count += p->min_count;
324 max_count += p->max_count;
325 min_runs += p->min_runs;
326 max_runs += p->max_runs;
329 nb_samples += p->nb_samples;
330 if (fabs(p->sigma_x) > fabs(max_sigma_x))
331 max_sigma_x = p->sigma_x;
333 if (s->measure_perchannel & MEASURE_DC_OFFSET)
334 set_meta(metadata, c + 1, "DC_offset", "%f", p->sigma_x / p->nb_samples);
335 if (s->measure_perchannel & MEASURE_MIN_LEVEL)
336 set_meta(metadata, c + 1, "Min_level", "%f", p->min);
337 if (s->measure_perchannel & MEASURE_MAX_LEVEL)
338 set_meta(metadata, c + 1, "Max_level", "%f", p->max);
339 if (s->measure_perchannel & MEASURE_MIN_DIFFERENCE)
340 set_meta(metadata, c + 1, "Min_difference", "%f", p->min_diff);
341 if (s->measure_perchannel & MEASURE_MAX_DIFFERENCE)
342 set_meta(metadata, c + 1, "Max_difference", "%f", p->max_diff);
343 if (s->measure_perchannel & MEASURE_MEAN_DIFFERENCE)
344 set_meta(metadata, c + 1, "Mean_difference", "%f", p->diff1_sum / (p->nb_samples - 1));
345 if (s->measure_perchannel & MEASURE_RMS_DIFFERENCE)
346 set_meta(metadata, c + 1, "RMS_difference", "%f", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
347 if (s->measure_perchannel & MEASURE_PEAK_LEVEL)
348 set_meta(metadata, c + 1, "Peak_level", "%f", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
349 if (s->measure_perchannel & MEASURE_RMS_LEVEL)
350 set_meta(metadata, c + 1, "RMS_level", "%f", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
351 if (s->measure_perchannel & MEASURE_RMS_PEAK)
352 set_meta(metadata, c + 1, "RMS_peak", "%f", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
353 if (s->measure_perchannel & MEASURE_RMS_TROUGH)
354 set_meta(metadata, c + 1, "RMS_trough", "%f", LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
355 if (s->measure_perchannel & MEASURE_CREST_FACTOR)
356 set_meta(metadata, c + 1, "Crest_factor", "%f", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
357 if (s->measure_perchannel & MEASURE_FLAT_FACTOR)
358 set_meta(metadata, c + 1, "Flat_factor", "%f", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
359 if (s->measure_perchannel & MEASURE_PEAK_COUNT)
360 set_meta(metadata, c + 1, "Peak_count", "%f", (float)(p->min_count + p->max_count));
361 if (s->measure_perchannel & MEASURE_BIT_DEPTH) {
362 bit_depth(s, p->mask, p->imask, &depth);
363 set_meta(metadata, c + 1, "Bit_depth", "%f", depth.num);
364 set_meta(metadata, c + 1, "Bit_depth2", "%f", depth.den);
366 if (s->measure_perchannel & MEASURE_DYNAMIC_RANGE)
367 set_meta(metadata, c + 1, "Dynamic_range", "%f", LINEAR_TO_DB(2 * FFMAX(FFABS(p->min), FFABS(p->max))/ p->min_non_zero));
368 if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS)
369 set_meta(metadata, c + 1, "Zero_crossings", "%f", p->zero_runs);
370 if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS_RATE)
371 set_meta(metadata, c + 1, "Zero_crossings_rate", "%f", p->zero_runs/(double)p->nb_samples);
374 if (s->measure_overall & MEASURE_DC_OFFSET)
375 set_meta(metadata, 0, "Overall.DC_offset", "%f", max_sigma_x / (nb_samples / s->nb_channels));
376 if (s->measure_overall & MEASURE_MIN_LEVEL)
377 set_meta(metadata, 0, "Overall.Min_level", "%f", min);
378 if (s->measure_overall & MEASURE_MAX_LEVEL)
379 set_meta(metadata, 0, "Overall.Max_level", "%f", max);
380 if (s->measure_overall & MEASURE_MIN_DIFFERENCE)
381 set_meta(metadata, 0, "Overall.Min_difference", "%f", min_diff);
382 if (s->measure_overall & MEASURE_MAX_DIFFERENCE)
383 set_meta(metadata, 0, "Overall.Max_difference", "%f", max_diff);
384 if (s->measure_overall & MEASURE_MEAN_DIFFERENCE)
385 set_meta(metadata, 0, "Overall.Mean_difference", "%f", diff1_sum / (nb_samples - s->nb_channels));
386 if (s->measure_overall & MEASURE_RMS_DIFFERENCE)
387 set_meta(metadata, 0, "Overall.RMS_difference", "%f", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
388 if (s->measure_overall & MEASURE_PEAK_LEVEL)
389 set_meta(metadata, 0, "Overall.Peak_level", "%f", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
390 if (s->measure_overall & MEASURE_RMS_LEVEL)
391 set_meta(metadata, 0, "Overall.RMS_level", "%f", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
392 if (s->measure_overall & MEASURE_RMS_PEAK)
393 set_meta(metadata, 0, "Overall.RMS_peak", "%f", LINEAR_TO_DB(sqrt(max_sigma_x2)));
394 if (s->measure_overall & MEASURE_RMS_TROUGH)
395 set_meta(metadata, 0, "Overall.RMS_trough", "%f", LINEAR_TO_DB(sqrt(min_sigma_x2)));
396 if (s->measure_overall & MEASURE_FLAT_FACTOR)
397 set_meta(metadata, 0, "Overall.Flat_factor", "%f", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
398 if (s->measure_overall & MEASURE_PEAK_COUNT)
399 set_meta(metadata, 0, "Overall.Peak_count", "%f", (float)(min_count + max_count) / (double)s->nb_channels);
400 if (s->measure_overall & MEASURE_BIT_DEPTH) {
401 bit_depth(s, mask, imask, &depth);
402 set_meta(metadata, 0, "Overall.Bit_depth", "%f", depth.num);
403 set_meta(metadata, 0, "Overall.Bit_depth2", "%f", depth.den);
405 if (s->measure_overall & MEASURE_NUMBER_OF_SAMPLES)
406 set_meta(metadata, 0, "Overall.Number_of_samples", "%f", nb_samples / s->nb_channels);
409 #define UPDATE_STATS_P(type, double_sample, normalized_sample, int_sample) \
410 for (int c = 0; c < channels; c++) { \
411 ChannelStats *p = &s->chstats[c]; \
412 const type *src = (const type *)data[c]; \
413 for (int i = 0; i < samples; i++, src++) \
414 update_stat(s, p, double_sample, normalized_sample, int_sample); \
417 #define UPDATE_STATS_I(type, double_sample, normalized_sample, int_sample) \
419 const type *src = (const type *)data[0]; \
420 for (int i = 0; i < samples; i++) { \
421 for (int c = 0; c < channels; c++, src++) \
422 update_stat(s, &s->chstats[c], double_sample, normalized_sample, int_sample); \
426 #define UPDATE_STATS(planar, type, sample, normalizer_suffix, int_sample) \
427 UPDATE_STATS_##planar(type, sample, sample normalizer_suffix, int_sample);
429 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
431 AudioStatsContext *s = inlink->dst->priv;
432 AVDictionary **metadata = &buf->metadata;
433 const int channels = s->nb_channels;
434 const int samples = buf->nb_samples;
435 const uint8_t * const * const data = (const uint8_t * const *)buf->extended_data;
437 if (s->reset_count > 0) {
438 if (s->nb_frames >= s->reset_count) {
445 switch (inlink->format) {
446 case AV_SAMPLE_FMT_DBLP:
447 UPDATE_STATS(P, double, *src, , llrint(*src * (UINT64_C(1) << 63)));
449 case AV_SAMPLE_FMT_DBL:
450 UPDATE_STATS(I, double, *src, , llrint(*src * (UINT64_C(1) << 63)));
452 case AV_SAMPLE_FMT_FLTP:
453 UPDATE_STATS(P, float, *src, , llrint(*src * (UINT64_C(1) << 31)));
455 case AV_SAMPLE_FMT_FLT:
456 UPDATE_STATS(I, float, *src, , llrint(*src * (UINT64_C(1) << 31)));
458 case AV_SAMPLE_FMT_S64P:
459 UPDATE_STATS(P, int64_t, *src, / (double)INT64_MAX, *src);
461 case AV_SAMPLE_FMT_S64:
462 UPDATE_STATS(I, int64_t, *src, / (double)INT64_MAX, *src);
464 case AV_SAMPLE_FMT_S32P:
465 UPDATE_STATS(P, int32_t, *src, / (double)INT32_MAX, *src);
467 case AV_SAMPLE_FMT_S32:
468 UPDATE_STATS(I, int32_t, *src, / (double)INT32_MAX, *src);
470 case AV_SAMPLE_FMT_S16P:
471 UPDATE_STATS(P, int16_t, *src, / (double)INT16_MAX, *src);
473 case AV_SAMPLE_FMT_S16:
474 UPDATE_STATS(I, int16_t, *src, / (double)INT16_MAX, *src);
479 set_metadata(s, metadata);
481 return ff_filter_frame(inlink->dst->outputs[0], buf);
484 static void print_stats(AVFilterContext *ctx)
486 AudioStatsContext *s = ctx->priv;
487 uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0;
488 double min_runs = 0, max_runs = 0,
489 min = DBL_MAX, max = DBL_MIN, min_diff = DBL_MAX, max_diff = 0,
490 nmin = DBL_MAX, nmax = DBL_MIN,
496 min_sigma_x2 = DBL_MAX,
497 max_sigma_x2 = DBL_MIN;
501 for (c = 0; c < s->nb_channels; c++) {
502 ChannelStats *p = &s->chstats[c];
504 if (p->nb_samples < s->tc_samples)
505 p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
507 min = FFMIN(min, p->min);
508 max = FFMAX(max, p->max);
509 nmin = FFMIN(nmin, p->nmin);
510 nmax = FFMAX(nmax, p->nmax);
511 min_diff = FFMIN(min_diff, p->min_diff);
512 max_diff = FFMAX(max_diff, p->max_diff);
513 diff1_sum_x2 += p->diff1_sum_x2;
514 diff1_sum += p->diff1_sum;
515 min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
516 max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
517 sigma_x += p->sigma_x;
518 sigma_x2 += p->sigma_x2;
519 min_count += p->min_count;
520 max_count += p->max_count;
521 min_runs += p->min_runs;
522 max_runs += p->max_runs;
525 nb_samples += p->nb_samples;
526 if (fabs(p->sigma_x) > fabs(max_sigma_x))
527 max_sigma_x = p->sigma_x;
529 av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
530 if (s->measure_perchannel & MEASURE_DC_OFFSET)
531 av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
532 if (s->measure_perchannel & MEASURE_MIN_LEVEL)
533 av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
534 if (s->measure_perchannel & MEASURE_MAX_LEVEL)
535 av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
536 if (s->measure_perchannel & MEASURE_MIN_DIFFERENCE)
537 av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", p->min_diff);
538 if (s->measure_perchannel & MEASURE_MAX_DIFFERENCE)
539 av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", p->max_diff);
540 if (s->measure_perchannel & MEASURE_MEAN_DIFFERENCE)
541 av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", p->diff1_sum / (p->nb_samples - 1));
542 if (s->measure_perchannel & MEASURE_RMS_DIFFERENCE)
543 av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
544 if (s->measure_perchannel & MEASURE_PEAK_LEVEL)
545 av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
546 if (s->measure_perchannel & MEASURE_RMS_LEVEL)
547 av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
548 if (s->measure_perchannel & MEASURE_RMS_PEAK)
549 av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
550 if (s->measure_perchannel & MEASURE_RMS_TROUGH)
551 if (p->min_sigma_x2 != 1)
552 av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
553 if (s->measure_perchannel & MEASURE_CREST_FACTOR)
554 av_log(ctx, AV_LOG_INFO, "Crest factor: %f\n", p->sigma_x2 ? FFMAX(-p->nmin, p->nmax) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
555 if (s->measure_perchannel & MEASURE_FLAT_FACTOR)
556 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)));
557 if (s->measure_perchannel & MEASURE_PEAK_COUNT)
558 av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
559 if (s->measure_perchannel & MEASURE_BIT_DEPTH) {
560 bit_depth(s, p->mask, p->imask, &depth);
561 av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
563 if (s->measure_perchannel & MEASURE_DYNAMIC_RANGE)
564 av_log(ctx, AV_LOG_INFO, "Dynamic range: %f\n", LINEAR_TO_DB(2 * FFMAX(FFABS(p->min), FFABS(p->max))/ p->min_non_zero));
565 if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS)
566 av_log(ctx, AV_LOG_INFO, "Zero crossings: %"PRId64"\n", p->zero_runs);
567 if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS_RATE)
568 av_log(ctx, AV_LOG_INFO, "Zero crossings rate: %f\n", p->zero_runs/(double)p->nb_samples);
571 av_log(ctx, AV_LOG_INFO, "Overall\n");
572 if (s->measure_overall & MEASURE_DC_OFFSET)
573 av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
574 if (s->measure_overall & MEASURE_MIN_LEVEL)
575 av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
576 if (s->measure_overall & MEASURE_MAX_LEVEL)
577 av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
578 if (s->measure_overall & MEASURE_MIN_DIFFERENCE)
579 av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", min_diff);
580 if (s->measure_overall & MEASURE_MAX_DIFFERENCE)
581 av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", max_diff);
582 if (s->measure_overall & MEASURE_MEAN_DIFFERENCE)
583 av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", diff1_sum / (nb_samples - s->nb_channels));
584 if (s->measure_overall & MEASURE_RMS_DIFFERENCE)
585 av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
586 if (s->measure_overall & MEASURE_PEAK_LEVEL)
587 av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
588 if (s->measure_overall & MEASURE_RMS_LEVEL)
589 av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
590 if (s->measure_overall & MEASURE_RMS_PEAK)
591 av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
592 if (s->measure_overall & MEASURE_RMS_TROUGH)
593 if (min_sigma_x2 != 1)
594 av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
595 if (s->measure_overall & MEASURE_FLAT_FACTOR)
596 av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
597 if (s->measure_overall & MEASURE_PEAK_COUNT)
598 av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
599 if (s->measure_overall & MEASURE_BIT_DEPTH) {
600 bit_depth(s, mask, imask, &depth);
601 av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
603 if (s->measure_overall & MEASURE_NUMBER_OF_SAMPLES)
604 av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
607 static av_cold void uninit(AVFilterContext *ctx)
609 AudioStatsContext *s = ctx->priv;
613 av_freep(&s->chstats);
616 static const AVFilterPad astats_inputs[] = {
619 .type = AVMEDIA_TYPE_AUDIO,
620 .filter_frame = filter_frame,
625 static const AVFilterPad astats_outputs[] = {
628 .type = AVMEDIA_TYPE_AUDIO,
629 .config_props = config_output,
634 AVFilter ff_af_astats = {
636 .description = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
637 .query_formats = query_formats,
638 .priv_size = sizeof(AudioStatsContext),
639 .priv_class = &astats_class,
641 .inputs = astats_inputs,
642 .outputs = astats_outputs,