]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_astats.c
Merge commit '2a9e1c122eed66be1b26b747342b848300b226c7'
[ffmpeg] / libavfilter / af_astats.c
1 /*
2  * Copyright (c) 2009 Rob Sykes <robs@users.sourceforge.net>
3  * Copyright (c) 2013 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <float.h>
23
24 #include "libavutil/opt.h"
25 #include "audio.h"
26 #include "avfilter.h"
27 #include "internal.h"
28
29 typedef struct ChannelStats {
30     double last;
31     double min_non_zero;
32     double sigma_x, sigma_x2;
33     double avg_sigma_x2, min_sigma_x2, max_sigma_x2;
34     double min, max;
35     double nmin, nmax;
36     double min_run, max_run;
37     double min_runs, max_runs;
38     double min_diff, max_diff;
39     double diff1_sum;
40     double diff1_sum_x2;
41     uint64_t mask, imask;
42     uint64_t min_count, max_count;
43     uint64_t nb_samples;
44 } ChannelStats;
45
46 typedef struct AudioStatsContext {
47     const AVClass *class;
48     ChannelStats *chstats;
49     int nb_channels;
50     uint64_t tc_samples;
51     double time_constant;
52     double mult;
53     int metadata;
54     int reset_count;
55     int nb_frames;
56     int maxbitdepth;
57 } AudioStatsContext;
58
59 #define OFFSET(x) offsetof(AudioStatsContext, x)
60 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
61
62 static const AVOption astats_options[] = {
63     { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=.05}, .01, 10, FLAGS },
64     { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
65     { "reset", "recalculate stats after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
66     { NULL }
67 };
68
69 AVFILTER_DEFINE_CLASS(astats);
70
71 static int query_formats(AVFilterContext *ctx)
72 {
73     AVFilterFormats *formats;
74     AVFilterChannelLayouts *layouts;
75     static const enum AVSampleFormat sample_fmts[] = {
76         AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
77         AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
78         AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64P,
79         AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
80         AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
81         AV_SAMPLE_FMT_NONE
82     };
83     int ret;
84
85     layouts = ff_all_channel_counts();
86     if (!layouts)
87         return AVERROR(ENOMEM);
88     ret = ff_set_common_channel_layouts(ctx, layouts);
89     if (ret < 0)
90         return ret;
91
92     formats = ff_make_format_list(sample_fmts);
93     if (!formats)
94         return AVERROR(ENOMEM);
95     ret = ff_set_common_formats(ctx, formats);
96     if (ret < 0)
97         return ret;
98
99     formats = ff_all_samplerates();
100     if (!formats)
101         return AVERROR(ENOMEM);
102     return ff_set_common_samplerates(ctx, formats);
103 }
104
105 static void reset_stats(AudioStatsContext *s)
106 {
107     int c;
108
109     for (c = 0; c < s->nb_channels; c++) {
110         ChannelStats *p = &s->chstats[c];
111
112         p->min = p->nmin = p->min_sigma_x2 = DBL_MAX;
113         p->max = p->nmax = p->max_sigma_x2 = DBL_MIN;
114         p->min_non_zero = DBL_MAX;
115         p->min_diff = DBL_MAX;
116         p->max_diff = DBL_MIN;
117         p->sigma_x = 0;
118         p->sigma_x2 = 0;
119         p->avg_sigma_x2 = 0;
120         p->min_run = 0;
121         p->max_run = 0;
122         p->min_runs = 0;
123         p->max_runs = 0;
124         p->diff1_sum = 0;
125         p->diff1_sum_x2 = 0;
126         p->mask = 0;
127         p->imask = 0xFFFFFFFFFFFFFFFF;
128         p->min_count = 0;
129         p->max_count = 0;
130         p->nb_samples = 0;
131     }
132 }
133
134 static int config_output(AVFilterLink *outlink)
135 {
136     AudioStatsContext *s = outlink->src->priv;
137
138     s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
139     if (!s->chstats)
140         return AVERROR(ENOMEM);
141     s->nb_channels = outlink->channels;
142     s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
143     s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5;
144     s->nb_frames = 0;
145     s->maxbitdepth = av_get_bytes_per_sample(outlink->format) * 8;
146
147     reset_stats(s);
148
149     return 0;
150 }
151
152 static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
153 {
154     unsigned result = s->maxbitdepth;
155
156     mask = mask & (~imask);
157
158     for (; result && !(mask & 1); --result, mask >>= 1);
159
160     depth->den = result;
161     depth->num = 0;
162
163     for (; result; --result, mask >>= 1)
164         if (mask & 1)
165             depth->num++;
166 }
167
168 static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d, double nd, int64_t i)
169 {
170     if (d < p->min) {
171         p->min = d;
172         p->nmin = nd;
173         p->min_run = 1;
174         p->min_runs = 0;
175         p->min_count = 1;
176     } else if (d == p->min) {
177         p->min_count++;
178         p->min_run = d == p->last ? p->min_run + 1 : 1;
179     } else if (p->last == p->min) {
180         p->min_runs += p->min_run * p->min_run;
181     }
182
183     if (d != 0 && FFABS(d) < p->min_non_zero)
184         p->min_non_zero = FFABS(d);
185
186     if (d > p->max) {
187         p->max = d;
188         p->nmax = nd;
189         p->max_run = 1;
190         p->max_runs = 0;
191         p->max_count = 1;
192     } else if (d == p->max) {
193         p->max_count++;
194         p->max_run = d == p->last ? p->max_run + 1 : 1;
195     } else if (p->last == p->max) {
196         p->max_runs += p->max_run * p->max_run;
197     }
198
199     p->sigma_x += nd;
200     p->sigma_x2 += nd * nd;
201     p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * nd * nd;
202     p->min_diff = FFMIN(p->min_diff, fabs(d - p->last));
203     p->max_diff = FFMAX(p->max_diff, fabs(d - p->last));
204     p->diff1_sum += fabs(d - p->last);
205     p->diff1_sum_x2 += (d - p->last) * (d - p->last);
206     p->last = d;
207     p->mask |= i;
208     p->imask &= i;
209
210     if (p->nb_samples >= s->tc_samples) {
211         p->max_sigma_x2 = FFMAX(p->max_sigma_x2, p->avg_sigma_x2);
212         p->min_sigma_x2 = FFMIN(p->min_sigma_x2, p->avg_sigma_x2);
213     }
214     p->nb_samples++;
215 }
216
217 static void set_meta(AVDictionary **metadata, int chan, const char *key,
218                      const char *fmt, double val)
219 {
220     uint8_t value[128];
221     uint8_t key2[128];
222
223     snprintf(value, sizeof(value), fmt, val);
224     if (chan)
225         snprintf(key2, sizeof(key2), "lavfi.astats.%d.%s", chan, key);
226     else
227         snprintf(key2, sizeof(key2), "lavfi.astats.%s", key);
228     av_dict_set(metadata, key2, value, 0);
229 }
230
231 #define LINEAR_TO_DB(x) (log10(x) * 20)
232
233 static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
234 {
235     uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0;
236     double min_runs = 0, max_runs = 0,
237            min = DBL_MAX, max = DBL_MIN, min_diff = DBL_MAX, max_diff = 0,
238            nmin = DBL_MAX, nmax = DBL_MIN,
239            max_sigma_x = 0,
240            diff1_sum = 0,
241            diff1_sum_x2 = 0,
242            sigma_x = 0,
243            sigma_x2 = 0,
244            min_sigma_x2 = DBL_MAX,
245            max_sigma_x2 = DBL_MIN;
246     AVRational depth;
247     int c;
248
249     for (c = 0; c < s->nb_channels; c++) {
250         ChannelStats *p = &s->chstats[c];
251
252         if (p->nb_samples < s->tc_samples)
253             p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
254
255         min = FFMIN(min, p->min);
256         max = FFMAX(max, p->max);
257         nmin = FFMIN(nmin, p->nmin);
258         nmax = FFMAX(nmax, p->nmax);
259         min_diff = FFMIN(min_diff, p->min_diff);
260         max_diff = FFMAX(max_diff, p->max_diff);
261         diff1_sum += p->diff1_sum;
262         diff1_sum_x2 += p->diff1_sum_x2;
263         min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
264         max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
265         sigma_x += p->sigma_x;
266         sigma_x2 += p->sigma_x2;
267         min_count += p->min_count;
268         max_count += p->max_count;
269         min_runs += p->min_runs;
270         max_runs += p->max_runs;
271         mask |= p->mask;
272         imask &= p->imask;
273         nb_samples += p->nb_samples;
274         if (fabs(p->sigma_x) > fabs(max_sigma_x))
275             max_sigma_x = p->sigma_x;
276
277         set_meta(metadata, c + 1, "DC_offset", "%f", p->sigma_x / p->nb_samples);
278         set_meta(metadata, c + 1, "Min_level", "%f", p->min);
279         set_meta(metadata, c + 1, "Max_level", "%f", p->max);
280         set_meta(metadata, c + 1, "Min_difference", "%f", p->min_diff);
281         set_meta(metadata, c + 1, "Max_difference", "%f", p->max_diff);
282         set_meta(metadata, c + 1, "Mean_difference", "%f", p->diff1_sum / (p->nb_samples - 1));
283         set_meta(metadata, c + 1, "RMS_difference", "%f", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
284         set_meta(metadata, c + 1, "Peak_level", "%f", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
285         set_meta(metadata, c + 1, "RMS_level", "%f", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
286         set_meta(metadata, c + 1, "RMS_peak", "%f", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
287         set_meta(metadata, c + 1, "RMS_trough", "%f", LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
288         set_meta(metadata, c + 1, "Crest_factor", "%f", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
289         set_meta(metadata, c + 1, "Flat_factor", "%f", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
290         set_meta(metadata, c + 1, "Peak_count", "%f", (float)(p->min_count + p->max_count));
291         bit_depth(s, p->mask, p->imask, &depth);
292         set_meta(metadata, c + 1, "Bit_depth", "%f", depth.num);
293         set_meta(metadata, c + 1, "Bit_depth2", "%f", depth.den);
294         set_meta(metadata, c + 1, "Dynamic_range", "%f", LINEAR_TO_DB(2 * FFMAX(FFABS(p->min), FFABS(p->max))/ p->min_non_zero));
295     }
296
297     set_meta(metadata, 0, "Overall.DC_offset", "%f", max_sigma_x / (nb_samples / s->nb_channels));
298     set_meta(metadata, 0, "Overall.Min_level", "%f", min);
299     set_meta(metadata, 0, "Overall.Max_level", "%f", max);
300     set_meta(metadata, 0, "Overall.Min_difference", "%f", min_diff);
301     set_meta(metadata, 0, "Overall.Max_difference", "%f", max_diff);
302     set_meta(metadata, 0, "Overall.Mean_difference", "%f", diff1_sum / (nb_samples - s->nb_channels));
303     set_meta(metadata, 0, "Overall.RMS_difference", "%f", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
304     set_meta(metadata, 0, "Overall.Peak_level", "%f", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
305     set_meta(metadata, 0, "Overall.RMS_level", "%f", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
306     set_meta(metadata, 0, "Overall.RMS_peak", "%f", LINEAR_TO_DB(sqrt(max_sigma_x2)));
307     set_meta(metadata, 0, "Overall.RMS_trough", "%f", LINEAR_TO_DB(sqrt(min_sigma_x2)));
308     set_meta(metadata, 0, "Overall.Flat_factor", "%f", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
309     set_meta(metadata, 0, "Overall.Peak_count", "%f", (float)(min_count + max_count) / (double)s->nb_channels);
310     bit_depth(s, mask, imask, &depth);
311     set_meta(metadata, 0, "Overall.Bit_depth", "%f", depth.num);
312     set_meta(metadata, 0, "Overall.Bit_depth2", "%f", depth.den);
313     set_meta(metadata, 0, "Overall.Number_of_samples", "%f", nb_samples / s->nb_channels);
314 }
315
316 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
317 {
318     AudioStatsContext *s = inlink->dst->priv;
319     AVDictionary **metadata = &buf->metadata;
320     const int channels = s->nb_channels;
321     int i, c;
322
323     if (s->reset_count > 0) {
324         if (s->nb_frames >= s->reset_count) {
325             reset_stats(s);
326             s->nb_frames = 0;
327         }
328         s->nb_frames++;
329     }
330
331     switch (inlink->format) {
332     case AV_SAMPLE_FMT_DBLP:
333         for (c = 0; c < channels; c++) {
334             ChannelStats *p = &s->chstats[c];
335             const double *src = (const double *)buf->extended_data[c];
336
337             for (i = 0; i < buf->nb_samples; i++, src++)
338                 update_stat(s, p, *src, *src, llrint(*src * (UINT64_C(1) << 63)));
339         }
340         break;
341     case AV_SAMPLE_FMT_DBL: {
342         const double *src = (const double *)buf->extended_data[0];
343
344         for (i = 0; i < buf->nb_samples; i++) {
345             for (c = 0; c < channels; c++, src++)
346                 update_stat(s, &s->chstats[c], *src, *src, llrint(*src * (UINT64_C(1) << 63)));
347         }}
348         break;
349     case AV_SAMPLE_FMT_FLTP:
350         for (c = 0; c < channels; c++) {
351             ChannelStats *p = &s->chstats[c];
352             const float *src = (const float *)buf->extended_data[c];
353
354             for (i = 0; i < buf->nb_samples; i++, src++)
355                 update_stat(s, p, *src, *src, llrint(*src * (UINT64_C(1) << 31)));
356         }
357         break;
358     case AV_SAMPLE_FMT_FLT: {
359         const float *src = (const float *)buf->extended_data[0];
360
361         for (i = 0; i < buf->nb_samples; i++) {
362             for (c = 0; c < channels; c++, src++)
363                 update_stat(s, &s->chstats[c], *src, *src, llrint(*src * (UINT64_C(1) << 31)));
364         }}
365         break;
366     case AV_SAMPLE_FMT_S64P:
367         for (c = 0; c < channels; c++) {
368             ChannelStats *p = &s->chstats[c];
369             const int64_t *src = (const int64_t *)buf->extended_data[c];
370
371             for (i = 0; i < buf->nb_samples; i++, src++)
372                 update_stat(s, p, *src, *src / (double)INT64_MAX, *src);
373         }
374         break;
375     case AV_SAMPLE_FMT_S64: {
376         const int64_t *src = (const int64_t *)buf->extended_data[0];
377
378         for (i = 0; i < buf->nb_samples; i++) {
379             for (c = 0; c < channels; c++, src++)
380                 update_stat(s, &s->chstats[c], *src, *src / (double)INT64_MAX, *src);
381         }}
382         break;
383     case AV_SAMPLE_FMT_S32P:
384         for (c = 0; c < channels; c++) {
385             ChannelStats *p = &s->chstats[c];
386             const int32_t *src = (const int32_t *)buf->extended_data[c];
387
388             for (i = 0; i < buf->nb_samples; i++, src++)
389                 update_stat(s, p, *src, *src / (double)INT32_MAX, *src);
390         }
391         break;
392     case AV_SAMPLE_FMT_S32: {
393         const int32_t *src = (const int32_t *)buf->extended_data[0];
394
395         for (i = 0; i < buf->nb_samples; i++) {
396             for (c = 0; c < channels; c++, src++)
397                 update_stat(s, &s->chstats[c], *src, *src / (double)INT32_MAX, *src);
398         }}
399         break;
400     case AV_SAMPLE_FMT_S16P:
401         for (c = 0; c < channels; c++) {
402             ChannelStats *p = &s->chstats[c];
403             const int16_t *src = (const int16_t *)buf->extended_data[c];
404
405             for (i = 0; i < buf->nb_samples; i++, src++)
406                 update_stat(s, p, *src, *src / (double)INT16_MAX, *src);
407         }
408         break;
409     case AV_SAMPLE_FMT_S16: {
410         const int16_t *src = (const int16_t *)buf->extended_data[0];
411
412         for (i = 0; i < buf->nb_samples; i++) {
413             for (c = 0; c < channels; c++, src++)
414                 update_stat(s, &s->chstats[c], *src, *src / (double)INT16_MAX, *src);
415         }}
416         break;
417     }
418
419     if (s->metadata)
420         set_metadata(s, metadata);
421
422     return ff_filter_frame(inlink->dst->outputs[0], buf);
423 }
424
425 static void print_stats(AVFilterContext *ctx)
426 {
427     AudioStatsContext *s = ctx->priv;
428     uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0;
429     double min_runs = 0, max_runs = 0,
430            min = DBL_MAX, max = DBL_MIN, min_diff = DBL_MAX, max_diff = 0,
431            nmin = DBL_MAX, nmax = DBL_MIN,
432            max_sigma_x = 0,
433            diff1_sum_x2 = 0,
434            diff1_sum = 0,
435            sigma_x = 0,
436            sigma_x2 = 0,
437            min_sigma_x2 = DBL_MAX,
438            max_sigma_x2 = DBL_MIN;
439     AVRational depth;
440     int c;
441
442     for (c = 0; c < s->nb_channels; c++) {
443         ChannelStats *p = &s->chstats[c];
444
445         if (p->nb_samples < s->tc_samples)
446             p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
447
448         min = FFMIN(min, p->min);
449         max = FFMAX(max, p->max);
450         nmin = FFMIN(nmin, p->nmin);
451         nmax = FFMAX(nmax, p->nmax);
452         min_diff = FFMIN(min_diff, p->min_diff);
453         max_diff = FFMAX(max_diff, p->max_diff);
454         diff1_sum_x2 += p->diff1_sum_x2;
455         diff1_sum += p->diff1_sum;
456         min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
457         max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
458         sigma_x += p->sigma_x;
459         sigma_x2 += p->sigma_x2;
460         min_count += p->min_count;
461         max_count += p->max_count;
462         min_runs += p->min_runs;
463         max_runs += p->max_runs;
464         mask |= p->mask;
465         imask &= p->imask;
466         nb_samples += p->nb_samples;
467         if (fabs(p->sigma_x) > fabs(max_sigma_x))
468             max_sigma_x = p->sigma_x;
469
470         av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
471         av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
472         av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
473         av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
474         av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", p->min_diff);
475         av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", p->max_diff);
476         av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", p->diff1_sum / (p->nb_samples - 1));
477         av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
478         av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
479         av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
480         av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
481         if (p->min_sigma_x2 != 1)
482             av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
483         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);
484         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)));
485         av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
486         bit_depth(s, p->mask, p->imask, &depth);
487         av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
488         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));
489     }
490
491     av_log(ctx, AV_LOG_INFO, "Overall\n");
492     av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
493     av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
494     av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
495     av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", min_diff);
496     av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", max_diff);
497     av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", diff1_sum / (nb_samples - s->nb_channels));
498     av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
499     av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
500     av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
501     av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
502     if (min_sigma_x2 != 1)
503         av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
504     av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
505     av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
506     bit_depth(s, mask, imask, &depth);
507     av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
508     av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
509 }
510
511 static av_cold void uninit(AVFilterContext *ctx)
512 {
513     AudioStatsContext *s = ctx->priv;
514
515     if (s->nb_channels)
516         print_stats(ctx);
517     av_freep(&s->chstats);
518 }
519
520 static const AVFilterPad astats_inputs[] = {
521     {
522         .name         = "default",
523         .type         = AVMEDIA_TYPE_AUDIO,
524         .filter_frame = filter_frame,
525     },
526     { NULL }
527 };
528
529 static const AVFilterPad astats_outputs[] = {
530     {
531         .name         = "default",
532         .type         = AVMEDIA_TYPE_AUDIO,
533         .config_props = config_output,
534     },
535     { NULL }
536 };
537
538 AVFilter ff_af_astats = {
539     .name          = "astats",
540     .description   = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
541     .query_formats = query_formats,
542     .priv_size     = sizeof(AudioStatsContext),
543     .priv_class    = &astats_class,
544     .uninit        = uninit,
545     .inputs        = astats_inputs,
546     .outputs       = astats_outputs,
547 };