• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <math.h>
24 
25 #include "libavutil/opt.h"
26 #include "audio.h"
27 #include "avfilter.h"
28 #include "internal.h"
29 
30 #define HISTOGRAM_SIZE                  8192
31 #define HISTOGRAM_MAX                   (HISTOGRAM_SIZE-1)
32 
33 #define MEASURE_ALL                     UINT_MAX
34 #define MEASURE_NONE                           0
35 
36 #define MEASURE_DC_OFFSET               (1 <<  0)
37 #define MEASURE_MIN_LEVEL               (1 <<  1)
38 #define MEASURE_MAX_LEVEL               (1 <<  2)
39 #define MEASURE_MIN_DIFFERENCE          (1 <<  3)
40 #define MEASURE_MAX_DIFFERENCE          (1 <<  4)
41 #define MEASURE_MEAN_DIFFERENCE         (1 <<  5)
42 #define MEASURE_RMS_DIFFERENCE          (1 <<  6)
43 #define MEASURE_PEAK_LEVEL              (1 <<  7)
44 #define MEASURE_RMS_LEVEL               (1 <<  8)
45 #define MEASURE_RMS_PEAK                (1 <<  9)
46 #define MEASURE_RMS_TROUGH              (1 << 10)
47 #define MEASURE_CREST_FACTOR            (1 << 11)
48 #define MEASURE_FLAT_FACTOR             (1 << 12)
49 #define MEASURE_PEAK_COUNT              (1 << 13)
50 #define MEASURE_BIT_DEPTH               (1 << 14)
51 #define MEASURE_DYNAMIC_RANGE           (1 << 15)
52 #define MEASURE_ZERO_CROSSINGS          (1 << 16)
53 #define MEASURE_ZERO_CROSSINGS_RATE     (1 << 17)
54 #define MEASURE_NUMBER_OF_SAMPLES       (1 << 18)
55 #define MEASURE_NUMBER_OF_NANS          (1 << 19)
56 #define MEASURE_NUMBER_OF_INFS          (1 << 20)
57 #define MEASURE_NUMBER_OF_DENORMALS     (1 << 21)
58 #define MEASURE_NOISE_FLOOR             (1 << 22)
59 #define MEASURE_NOISE_FLOOR_COUNT       (1 << 23)
60 #define MEASURE_ENTROPY                 (1 << 24)
61 
62 #define MEASURE_MINMAXPEAK              (MEASURE_MIN_LEVEL | MEASURE_MAX_LEVEL | MEASURE_PEAK_LEVEL)
63 
64 typedef struct ChannelStats {
65     double last;
66     double last_non_zero;
67     double min_non_zero;
68     double sigma_x, sigma_x2;
69     double avg_sigma_x2, min_sigma_x2, max_sigma_x2;
70     double min, max;
71     double nmin, nmax;
72     double min_run, max_run;
73     double min_runs, max_runs;
74     double min_diff, max_diff;
75     double diff1_sum;
76     double diff1_sum_x2;
77     uint64_t mask, imask;
78     uint64_t min_count, max_count;
79     uint64_t noise_floor_count;
80     uint64_t zero_runs;
81     uint64_t nb_samples;
82     uint64_t nb_nans;
83     uint64_t nb_infs;
84     uint64_t nb_denormals;
85     double *win_samples;
86     uint64_t histogram[HISTOGRAM_SIZE];
87     uint64_t ehistogram[HISTOGRAM_SIZE];
88     int win_pos;
89     int max_index;
90     double noise_floor;
91     double entropy;
92 } ChannelStats;
93 
94 typedef struct AudioStatsContext {
95     const AVClass *class;
96     ChannelStats *chstats;
97     int nb_channels;
98     uint64_t tc_samples;
99     double time_constant;
100     double mult;
101     int metadata;
102     int reset_count;
103     int nb_frames;
104     int maxbitdepth;
105     int measure_perchannel;
106     int measure_overall;
107     int is_float;
108     int is_double;
109 } AudioStatsContext;
110 
111 #define OFFSET(x) offsetof(AudioStatsContext, x)
112 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
113 
114 static const AVOption astats_options[] = {
115     { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=.05}, 0, 10, FLAGS },
116     { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
117     { "reset", "Set the number of frames over which cumulative stats are calculated before being reset", OFFSET(reset_count), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
118     { "measure_perchannel", "Select the parameters which are measured per channel", OFFSET(measure_perchannel), AV_OPT_TYPE_FLAGS, {.i64=MEASURE_ALL}, 0, UINT_MAX, FLAGS, "measure" },
119       { "none"                      , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NONE                }, 0, 0, FLAGS, "measure" },
120       { "all"                       , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ALL                 }, 0, 0, FLAGS, "measure" },
121       { "DC_offset"                 , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_DC_OFFSET           }, 0, 0, FLAGS, "measure" },
122       { "Min_level"                 , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MIN_LEVEL           }, 0, 0, FLAGS, "measure" },
123       { "Max_level"                 , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MAX_LEVEL           }, 0, 0, FLAGS, "measure" },
124       { "Min_difference"            , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MIN_DIFFERENCE      }, 0, 0, FLAGS, "measure" },
125       { "Max_difference"            , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MAX_DIFFERENCE      }, 0, 0, FLAGS, "measure" },
126       { "Mean_difference"           , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MEAN_DIFFERENCE     }, 0, 0, FLAGS, "measure" },
127       { "RMS_difference"            , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_DIFFERENCE      }, 0, 0, FLAGS, "measure" },
128       { "Peak_level"                , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_PEAK_LEVEL          }, 0, 0, FLAGS, "measure" },
129       { "RMS_level"                 , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_LEVEL           }, 0, 0, FLAGS, "measure" },
130       { "RMS_peak"                  , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_PEAK            }, 0, 0, FLAGS, "measure" },
131       { "RMS_trough"                , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_TROUGH          }, 0, 0, FLAGS, "measure" },
132       { "Crest_factor"              , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_CREST_FACTOR        }, 0, 0, FLAGS, "measure" },
133       { "Flat_factor"               , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_FLAT_FACTOR         }, 0, 0, FLAGS, "measure" },
134       { "Peak_count"                , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_PEAK_COUNT          }, 0, 0, FLAGS, "measure" },
135       { "Bit_depth"                 , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_BIT_DEPTH           }, 0, 0, FLAGS, "measure" },
136       { "Dynamic_range"             , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_DYNAMIC_RANGE       }, 0, 0, FLAGS, "measure" },
137       { "Zero_crossings"            , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ZERO_CROSSINGS      }, 0, 0, FLAGS, "measure" },
138       { "Zero_crossings_rate"       , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ZERO_CROSSINGS_RATE }, 0, 0, FLAGS, "measure" },
139       { "Noise_floor"               , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NOISE_FLOOR         }, 0, 0, FLAGS, "measure" },
140       { "Noise_floor_count"         , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NOISE_FLOOR_COUNT   }, 0, 0, FLAGS, "measure" },
141       { "Entropy"                   , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ENTROPY             }, 0, 0, FLAGS, "measure" },
142       { "Number_of_samples"         , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_SAMPLES   }, 0, 0, FLAGS, "measure" },
143       { "Number_of_NaNs"            , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_NANS      }, 0, 0, FLAGS, "measure" },
144       { "Number_of_Infs"            , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_INFS      }, 0, 0, FLAGS, "measure" },
145       { "Number_of_denormals"       , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_DENORMALS }, 0, 0, FLAGS, "measure" },
146     { "measure_overall", "Select the parameters which are measured overall", OFFSET(measure_overall), AV_OPT_TYPE_FLAGS, {.i64=MEASURE_ALL}, 0, UINT_MAX, FLAGS, "measure" },
147     { NULL }
148 };
149 
150 AVFILTER_DEFINE_CLASS(astats);
151 
reset_stats(AudioStatsContext * s)152 static void reset_stats(AudioStatsContext *s)
153 {
154     int c;
155 
156     for (c = 0; c < s->nb_channels; c++) {
157         ChannelStats *p = &s->chstats[c];
158 
159         p->min = p->nmin = p->min_sigma_x2 = DBL_MAX;
160         p->max = p->nmax = p->max_sigma_x2 =-DBL_MAX;
161         p->min_non_zero = DBL_MAX;
162         p->min_diff = DBL_MAX;
163         p->max_diff = 0;
164         p->sigma_x = 0;
165         p->sigma_x2 = 0;
166         p->avg_sigma_x2 = 0;
167         p->min_run = 0;
168         p->max_run = 0;
169         p->min_runs = 0;
170         p->max_runs = 0;
171         p->diff1_sum = 0;
172         p->diff1_sum_x2 = 0;
173         p->mask = 0;
174         p->imask = 0xFFFFFFFFFFFFFFFF;
175         p->min_count = 0;
176         p->max_count = 0;
177         p->zero_runs = 0;
178         p->nb_samples = 0;
179         p->nb_nans = 0;
180         p->nb_infs = 0;
181         p->nb_denormals = 0;
182         p->last = NAN;
183         p->noise_floor = NAN;
184         p->noise_floor_count = 0;
185         p->entropy = 0;
186         p->win_pos = 0;
187         memset(p->win_samples, 0, s->tc_samples * sizeof(*p->win_samples));
188         memset(p->histogram, 0, sizeof(p->histogram));
189         memset(p->ehistogram, 0, sizeof(p->ehistogram));
190     }
191 }
192 
config_output(AVFilterLink * outlink)193 static int config_output(AVFilterLink *outlink)
194 {
195     AudioStatsContext *s = outlink->src->priv;
196 
197     s->chstats = av_calloc(sizeof(*s->chstats), outlink->ch_layout.nb_channels);
198     if (!s->chstats)
199         return AVERROR(ENOMEM);
200 
201     s->tc_samples = FFMAX(s->time_constant * outlink->sample_rate + .5, 1);
202     s->nb_channels = outlink->ch_layout.nb_channels;
203 
204     for (int i = 0; i < s->nb_channels; i++) {
205         ChannelStats *p = &s->chstats[i];
206 
207         p->win_samples = av_calloc(s->tc_samples, sizeof(*p->win_samples));
208         if (!p->win_samples)
209             return AVERROR(ENOMEM);
210     }
211 
212     s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
213     s->nb_frames = 0;
214     s->maxbitdepth = av_get_bytes_per_sample(outlink->format) * 8;
215     s->is_double = outlink->format == AV_SAMPLE_FMT_DBL  ||
216                    outlink->format == AV_SAMPLE_FMT_DBLP;
217 
218     s->is_float = outlink->format == AV_SAMPLE_FMT_FLT  ||
219                   outlink->format == AV_SAMPLE_FMT_FLTP;
220 
221     reset_stats(s);
222 
223     return 0;
224 }
225 
bit_depth(AudioStatsContext * s,uint64_t mask,uint64_t imask,AVRational * depth)226 static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
227 {
228     unsigned result = s->maxbitdepth;
229 
230     mask = mask & (~imask);
231 
232     for (; result && !(mask & 1); --result, mask >>= 1);
233 
234     depth->den = result;
235     depth->num = 0;
236 
237     for (; result; --result, mask >>= 1)
238         if (mask & 1)
239             depth->num++;
240 }
241 
calc_entropy(AudioStatsContext * s,ChannelStats * p)242 static double calc_entropy(AudioStatsContext *s, ChannelStats *p)
243 {
244     double entropy = 0.;
245 
246     for (int i = 0; i < HISTOGRAM_SIZE; i++) {
247         double entry = p->ehistogram[i] / ((double)p->nb_samples);
248 
249         if (entry > 1e-8)
250             entropy += entry * log2(entry);
251     }
252 
253     return -entropy / log2(HISTOGRAM_SIZE);
254 }
255 
update_minmax(AudioStatsContext * s,ChannelStats * p,double d)256 static inline void update_minmax(AudioStatsContext *s, ChannelStats *p, double d)
257 {
258     if (d < p->min)
259         p->min = d;
260     if (d > p->max)
261         p->max = d;
262 }
263 
update_stat(AudioStatsContext * s,ChannelStats * p,double d,double nd,int64_t i)264 static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d, double nd, int64_t i)
265 {
266     double drop;
267     int index;
268 
269     if (d < p->min) {
270         p->min = d;
271         p->nmin = nd;
272         p->min_run = 1;
273         p->min_runs = 0;
274         p->min_count = 1;
275     } else if (d == p->min) {
276         p->min_count++;
277         p->min_run = d == p->last ? p->min_run + 1 : 1;
278     } else if (p->last == p->min) {
279         p->min_runs += p->min_run * p->min_run;
280     }
281 
282     if (d != 0 && FFABS(d) < p->min_non_zero)
283         p->min_non_zero = FFABS(d);
284 
285     if (d > p->max) {
286         p->max = d;
287         p->nmax = nd;
288         p->max_run = 1;
289         p->max_runs = 0;
290         p->max_count = 1;
291     } else if (d == p->max) {
292         p->max_count++;
293         p->max_run = d == p->last ? p->max_run + 1 : 1;
294     } else if (p->last == p->max) {
295         p->max_runs += p->max_run * p->max_run;
296     }
297 
298     if (d != 0) {
299         p->zero_runs += FFSIGN(d) != FFSIGN(p->last_non_zero);
300         p->last_non_zero = d;
301     }
302 
303     p->sigma_x += nd;
304     p->sigma_x2 += nd * nd;
305     p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * nd * nd;
306     if (!isnan(p->last)) {
307         p->min_diff = FFMIN(p->min_diff, fabs(d - p->last));
308         p->max_diff = FFMAX(p->max_diff, fabs(d - p->last));
309         p->diff1_sum += fabs(d - p->last);
310         p->diff1_sum_x2 += (d - p->last) * (d - p->last);
311     }
312     p->last = d;
313     p->mask |= i;
314     p->imask &= i;
315 
316     drop = p->win_samples[p->win_pos];
317     p->win_samples[p->win_pos] = nd;
318     index = av_clip(lrint(av_clipd(FFABS(nd), 0.0, 1.0) * HISTOGRAM_MAX), 0, HISTOGRAM_MAX);
319     p->max_index = FFMAX(p->max_index, index);
320     p->histogram[index]++;
321     p->ehistogram[index]++;
322     if (!isnan(p->noise_floor))
323         p->histogram[av_clip(lrint(av_clipd(FFABS(drop), 0.0, 1.0) * HISTOGRAM_MAX), 0, HISTOGRAM_MAX)]--;
324     p->win_pos++;
325 
326     while (p->histogram[p->max_index] == 0)
327         p->max_index--;
328     if (p->win_pos >= s->tc_samples || !isnan(p->noise_floor)) {
329         double noise_floor = 1.;
330 
331         for (int i = p->max_index; i >= 0; i--) {
332             if (p->histogram[i]) {
333                 noise_floor = i / (double)HISTOGRAM_MAX;
334                 break;
335             }
336         }
337 
338         if (isnan(p->noise_floor)) {
339             p->noise_floor = noise_floor;
340             p->noise_floor_count = 1;
341         } else {
342             if (noise_floor < p->noise_floor) {
343                 p->noise_floor = noise_floor;
344                 p->noise_floor_count = 1;
345             } else if (noise_floor == p->noise_floor) {
346                 p->noise_floor_count++;
347             }
348         }
349     }
350 
351     if (p->win_pos >= s->tc_samples) {
352         p->win_pos = 0;
353     }
354 
355     if (p->nb_samples >= s->tc_samples) {
356         p->max_sigma_x2 = FFMAX(p->max_sigma_x2, p->avg_sigma_x2);
357         p->min_sigma_x2 = FFMIN(p->min_sigma_x2, p->avg_sigma_x2);
358     }
359     p->nb_samples++;
360 }
361 
update_float_stat(AudioStatsContext * s,ChannelStats * p,float d)362 static inline void update_float_stat(AudioStatsContext *s, ChannelStats *p, float d)
363 {
364     int type = fpclassify(d);
365 
366     p->nb_nans      += type == FP_NAN;
367     p->nb_infs      += type == FP_INFINITE;
368     p->nb_denormals += type == FP_SUBNORMAL;
369 }
370 
update_double_stat(AudioStatsContext * s,ChannelStats * p,double d)371 static inline void update_double_stat(AudioStatsContext *s, ChannelStats *p, double d)
372 {
373     int type = fpclassify(d);
374 
375     p->nb_nans      += type == FP_NAN;
376     p->nb_infs      += type == FP_INFINITE;
377     p->nb_denormals += type == FP_SUBNORMAL;
378 }
379 
set_meta(AVDictionary ** metadata,int chan,const char * key,const char * fmt,double val)380 static void set_meta(AVDictionary **metadata, int chan, const char *key,
381                      const char *fmt, double val)
382 {
383     uint8_t value[128];
384     uint8_t key2[128];
385 
386     snprintf(value, sizeof(value), fmt, val);
387     if (chan)
388         snprintf(key2, sizeof(key2), "lavfi.astats.%d.%s", chan, key);
389     else
390         snprintf(key2, sizeof(key2), "lavfi.astats.%s", key);
391     av_dict_set(metadata, key2, value, 0);
392 }
393 
394 #define LINEAR_TO_DB(x) (log10(x) * 20)
395 
set_metadata(AudioStatsContext * s,AVDictionary ** metadata)396 static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
397 {
398     uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0, noise_floor_count = 0;
399     uint64_t nb_nans = 0, nb_infs = 0, nb_denormals = 0;
400     double min_runs = 0, max_runs = 0,
401            min = DBL_MAX, max =-DBL_MAX, min_diff = DBL_MAX, max_diff = 0,
402            nmin = DBL_MAX, nmax =-DBL_MAX,
403            max_sigma_x = 0,
404            diff1_sum = 0,
405            diff1_sum_x2 = 0,
406            sigma_x2 = 0,
407            noise_floor = 0,
408            entropy = 0,
409            min_sigma_x2 = DBL_MAX,
410            max_sigma_x2 =-DBL_MAX;
411     AVRational depth;
412     int c;
413 
414     for (c = 0; c < s->nb_channels; c++) {
415         ChannelStats *p = &s->chstats[c];
416 
417         if (p->nb_samples < s->tc_samples)
418             p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
419 
420         min = FFMIN(min, p->min);
421         max = FFMAX(max, p->max);
422         nmin = FFMIN(nmin, p->nmin);
423         nmax = FFMAX(nmax, p->nmax);
424         min_diff = FFMIN(min_diff, p->min_diff);
425         max_diff = FFMAX(max_diff, p->max_diff);
426         diff1_sum += p->diff1_sum;
427         diff1_sum_x2 += p->diff1_sum_x2;
428         min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
429         max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
430         sigma_x2 += p->sigma_x2;
431         noise_floor = FFMAX(noise_floor, p->noise_floor);
432         noise_floor_count += p->noise_floor_count;
433         p->entropy = calc_entropy(s, p);
434         entropy += p->entropy;
435         min_count += p->min_count;
436         max_count += p->max_count;
437         min_runs += p->min_runs;
438         max_runs += p->max_runs;
439         mask |= p->mask;
440         imask &= p->imask;
441         nb_samples += p->nb_samples;
442         nb_nans += p->nb_nans;
443         nb_infs += p->nb_infs;
444         nb_denormals += p->nb_denormals;
445         if (fabs(p->sigma_x) > fabs(max_sigma_x))
446             max_sigma_x = p->sigma_x;
447 
448         if (s->measure_perchannel & MEASURE_DC_OFFSET)
449             set_meta(metadata, c + 1, "DC_offset", "%f", p->sigma_x / p->nb_samples);
450         if (s->measure_perchannel & MEASURE_MIN_LEVEL)
451             set_meta(metadata, c + 1, "Min_level", "%f", p->min);
452         if (s->measure_perchannel & MEASURE_MAX_LEVEL)
453             set_meta(metadata, c + 1, "Max_level", "%f", p->max);
454         if (s->measure_perchannel & MEASURE_MIN_DIFFERENCE)
455             set_meta(metadata, c + 1, "Min_difference", "%f", p->min_diff);
456         if (s->measure_perchannel & MEASURE_MAX_DIFFERENCE)
457             set_meta(metadata, c + 1, "Max_difference", "%f", p->max_diff);
458         if (s->measure_perchannel & MEASURE_MEAN_DIFFERENCE)
459             set_meta(metadata, c + 1, "Mean_difference", "%f", p->diff1_sum / (p->nb_samples - 1));
460         if (s->measure_perchannel & MEASURE_RMS_DIFFERENCE)
461             set_meta(metadata, c + 1, "RMS_difference", "%f", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
462         if (s->measure_perchannel & MEASURE_PEAK_LEVEL)
463             set_meta(metadata, c + 1, "Peak_level", "%f", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
464         if (s->measure_perchannel & MEASURE_RMS_LEVEL)
465             set_meta(metadata, c + 1, "RMS_level", "%f", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
466         if (s->measure_perchannel & MEASURE_RMS_PEAK)
467             set_meta(metadata, c + 1, "RMS_peak", "%f", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
468         if (s->measure_perchannel & MEASURE_RMS_TROUGH)
469             set_meta(metadata, c + 1, "RMS_trough", "%f", LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
470         if (s->measure_perchannel & MEASURE_CREST_FACTOR)
471             set_meta(metadata, c + 1, "Crest_factor", "%f", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
472         if (s->measure_perchannel & MEASURE_FLAT_FACTOR)
473             set_meta(metadata, c + 1, "Flat_factor", "%f", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
474         if (s->measure_perchannel & MEASURE_PEAK_COUNT)
475             set_meta(metadata, c + 1, "Peak_count", "%f", (float)(p->min_count + p->max_count));
476         if (s->measure_perchannel & MEASURE_NOISE_FLOOR)
477             set_meta(metadata, c + 1, "Noise_floor", "%f", LINEAR_TO_DB(p->noise_floor));
478         if (s->measure_perchannel & MEASURE_NOISE_FLOOR_COUNT)
479             set_meta(metadata, c + 1, "Noise_floor_count", "%f", p->noise_floor_count);
480         if (s->measure_perchannel & MEASURE_ENTROPY)
481             set_meta(metadata, c + 1, "Entropy", "%f", p->entropy);
482         if (s->measure_perchannel & MEASURE_BIT_DEPTH) {
483             bit_depth(s, p->mask, p->imask, &depth);
484             set_meta(metadata, c + 1, "Bit_depth", "%f", depth.num);
485             set_meta(metadata, c + 1, "Bit_depth2", "%f", depth.den);
486         }
487         if (s->measure_perchannel & MEASURE_DYNAMIC_RANGE)
488             set_meta(metadata, c + 1, "Dynamic_range", "%f", LINEAR_TO_DB(2 * FFMAX(FFABS(p->min), FFABS(p->max))/ p->min_non_zero));
489         if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS)
490             set_meta(metadata, c + 1, "Zero_crossings", "%f", p->zero_runs);
491         if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS_RATE)
492             set_meta(metadata, c + 1, "Zero_crossings_rate", "%f", p->zero_runs/(double)p->nb_samples);
493         if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_NANS)
494             set_meta(metadata, c + 1, "Number of NaNs", "%f", p->nb_nans);
495         if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_INFS)
496             set_meta(metadata, c + 1, "Number of Infs", "%f", p->nb_infs);
497         if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_DENORMALS)
498             set_meta(metadata, c + 1, "Number of denormals", "%f", p->nb_denormals);
499     }
500 
501     if (s->measure_overall & MEASURE_DC_OFFSET)
502         set_meta(metadata, 0, "Overall.DC_offset", "%f", max_sigma_x / (nb_samples / s->nb_channels));
503     if (s->measure_overall & MEASURE_MIN_LEVEL)
504         set_meta(metadata, 0, "Overall.Min_level", "%f", min);
505     if (s->measure_overall & MEASURE_MAX_LEVEL)
506         set_meta(metadata, 0, "Overall.Max_level", "%f", max);
507     if (s->measure_overall & MEASURE_MIN_DIFFERENCE)
508         set_meta(metadata, 0, "Overall.Min_difference", "%f", min_diff);
509     if (s->measure_overall & MEASURE_MAX_DIFFERENCE)
510         set_meta(metadata, 0, "Overall.Max_difference", "%f", max_diff);
511     if (s->measure_overall & MEASURE_MEAN_DIFFERENCE)
512         set_meta(metadata, 0, "Overall.Mean_difference", "%f", diff1_sum / (nb_samples - s->nb_channels));
513     if (s->measure_overall & MEASURE_RMS_DIFFERENCE)
514         set_meta(metadata, 0, "Overall.RMS_difference", "%f", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
515     if (s->measure_overall & MEASURE_PEAK_LEVEL)
516         set_meta(metadata, 0, "Overall.Peak_level", "%f", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
517     if (s->measure_overall & MEASURE_RMS_LEVEL)
518         set_meta(metadata, 0, "Overall.RMS_level", "%f", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
519     if (s->measure_overall & MEASURE_RMS_PEAK)
520         set_meta(metadata, 0, "Overall.RMS_peak", "%f", LINEAR_TO_DB(sqrt(max_sigma_x2)));
521     if (s->measure_overall & MEASURE_RMS_TROUGH)
522         set_meta(metadata, 0, "Overall.RMS_trough", "%f", LINEAR_TO_DB(sqrt(min_sigma_x2)));
523     if (s->measure_overall & MEASURE_FLAT_FACTOR)
524         set_meta(metadata, 0, "Overall.Flat_factor", "%f", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
525     if (s->measure_overall & MEASURE_PEAK_COUNT)
526         set_meta(metadata, 0, "Overall.Peak_count", "%f", (float)(min_count + max_count) / (double)s->nb_channels);
527     if (s->measure_overall & MEASURE_NOISE_FLOOR)
528         set_meta(metadata, 0, "Overall.Noise_floor", "%f", LINEAR_TO_DB(noise_floor));
529     if (s->measure_overall & MEASURE_NOISE_FLOOR_COUNT)
530         set_meta(metadata, 0, "Overall.Noise_floor_count", "%f", noise_floor_count / (double)s->nb_channels);
531     if (s->measure_overall & MEASURE_ENTROPY)
532         set_meta(metadata, 0, "Overall.Entropy", "%f", entropy / (double)s->nb_channels);
533     if (s->measure_overall & MEASURE_BIT_DEPTH) {
534         bit_depth(s, mask, imask, &depth);
535         set_meta(metadata, 0, "Overall.Bit_depth", "%f", depth.num);
536         set_meta(metadata, 0, "Overall.Bit_depth2", "%f", depth.den);
537     }
538     if (s->measure_overall & MEASURE_NUMBER_OF_SAMPLES)
539         set_meta(metadata, 0, "Overall.Number_of_samples", "%f", nb_samples / s->nb_channels);
540     if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_NANS)
541         set_meta(metadata, 0, "Number of NaNs", "%f", nb_nans / (float)s->nb_channels);
542     if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_INFS)
543         set_meta(metadata, 0, "Number of Infs", "%f", nb_infs / (float)s->nb_channels);
544     if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_DENORMALS)
545         set_meta(metadata, 0, "Number of denormals", "%f", nb_denormals / (float)s->nb_channels);
546 }
547 
548 #define UPDATE_STATS_P(type, update_func, update_float, channel_func)           \
549     for (int c = start; c < end; c++) {                                         \
550         ChannelStats *p = &s->chstats[c];                                       \
551         const type *src = (const type *)data[c];                                \
552         const type * const srcend = src + samples;                              \
553         for (; src < srcend; src++) {                                           \
554             update_func;                                                        \
555             update_float;                                                       \
556         }                                                                       \
557         channel_func;                                                           \
558     }
559 
560 #define UPDATE_STATS_I(type, update_func, update_float, channel_func)           \
561     for (int c = start; c < end; c++) {                                         \
562         ChannelStats *p = &s->chstats[c];                                       \
563         const type *src = (const type *)data[0];                                \
564         const type * const srcend = src + samples * channels;                   \
565         for (src += c; src < srcend; src += channels) {                         \
566             update_func;                                                        \
567             update_float;                                                       \
568         }                                                                       \
569         channel_func;                                                           \
570     }
571 
572 #define UPDATE_STATS(planar, type, sample, normalizer_suffix, int_sample) \
573     if ((s->measure_overall | s->measure_perchannel) & ~MEASURE_MINMAXPEAK) {                          \
574         UPDATE_STATS_##planar(type, update_stat(s, p, sample, sample normalizer_suffix, int_sample), s->is_float ? update_float_stat(s, p, sample) : s->is_double ? update_double_stat(s, p, sample) : (void)NULL, ); \
575     } else {                                                                                           \
576         UPDATE_STATS_##planar(type, update_minmax(s, p, sample), , p->nmin = p->min normalizer_suffix; p->nmax = p->max normalizer_suffix;); \
577     }
578 
filter_channel(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)579 static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
580 {
581     AudioStatsContext *s = ctx->priv;
582     AVFilterLink *inlink = ctx->inputs[0];
583     AVFrame *buf = arg;
584     const uint8_t * const * const data = (const uint8_t * const *)buf->extended_data;
585     const int channels = s->nb_channels;
586     const int samples = buf->nb_samples;
587     const int start = (buf->ch_layout.nb_channels * jobnr) / nb_jobs;
588     const int end = (buf->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
589 
590     switch (inlink->format) {
591     case AV_SAMPLE_FMT_DBLP:
592         UPDATE_STATS(P, double, *src, , llrint(*src * (UINT64_C(1) << 63)));
593         break;
594     case AV_SAMPLE_FMT_DBL:
595         UPDATE_STATS(I, double, *src, , llrint(*src * (UINT64_C(1) << 63)));
596         break;
597     case AV_SAMPLE_FMT_FLTP:
598         UPDATE_STATS(P, float, *src, , llrint(*src * (UINT64_C(1) << 31)));
599         break;
600     case AV_SAMPLE_FMT_FLT:
601         UPDATE_STATS(I, float, *src, , llrint(*src * (UINT64_C(1) << 31)));
602         break;
603     case AV_SAMPLE_FMT_S64P:
604         UPDATE_STATS(P, int64_t, *src, / (double)INT64_MAX, *src);
605         break;
606     case AV_SAMPLE_FMT_S64:
607         UPDATE_STATS(I, int64_t, *src, / (double)INT64_MAX, *src);
608         break;
609     case AV_SAMPLE_FMT_S32P:
610         UPDATE_STATS(P, int32_t, *src, / (double)INT32_MAX, *src);
611         break;
612     case AV_SAMPLE_FMT_S32:
613         UPDATE_STATS(I, int32_t, *src, / (double)INT32_MAX, *src);
614         break;
615     case AV_SAMPLE_FMT_S16P:
616         UPDATE_STATS(P, int16_t, *src, / (double)INT16_MAX, *src);
617         break;
618     case AV_SAMPLE_FMT_S16:
619         UPDATE_STATS(I, int16_t, *src, / (double)INT16_MAX, *src);
620         break;
621     }
622 
623     return 0;
624 }
625 
filter_frame(AVFilterLink * inlink,AVFrame * buf)626 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
627 {
628     AVFilterContext *ctx = inlink->dst;
629     AudioStatsContext *s = ctx->priv;
630     AVDictionary **metadata = &buf->metadata;
631 
632     if (s->reset_count > 0) {
633         if (s->nb_frames >= s->reset_count) {
634             reset_stats(s);
635             s->nb_frames = 0;
636         }
637         s->nb_frames++;
638     }
639 
640     ff_filter_execute(ctx, filter_channel, buf, NULL,
641                       FFMIN(inlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
642 
643     if (s->metadata)
644         set_metadata(s, metadata);
645 
646     return ff_filter_frame(inlink->dst->outputs[0], buf);
647 }
648 
print_stats(AVFilterContext * ctx)649 static void print_stats(AVFilterContext *ctx)
650 {
651     AudioStatsContext *s = ctx->priv;
652     uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0, noise_floor_count = 0;
653     uint64_t nb_nans = 0, nb_infs = 0, nb_denormals = 0;
654     double min_runs = 0, max_runs = 0,
655            min = DBL_MAX, max =-DBL_MAX, min_diff = DBL_MAX, max_diff = 0,
656            nmin = DBL_MAX, nmax =-DBL_MAX,
657            max_sigma_x = 0,
658            diff1_sum_x2 = 0,
659            diff1_sum = 0,
660            sigma_x2 = 0,
661            noise_floor = 0,
662            entropy = 0,
663            min_sigma_x2 = DBL_MAX,
664            max_sigma_x2 =-DBL_MAX;
665     AVRational depth;
666     int c;
667 
668     for (c = 0; c < s->nb_channels; c++) {
669         ChannelStats *p = &s->chstats[c];
670 
671         if (p->nb_samples < s->tc_samples)
672             p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
673 
674         min = FFMIN(min, p->min);
675         max = FFMAX(max, p->max);
676         nmin = FFMIN(nmin, p->nmin);
677         nmax = FFMAX(nmax, p->nmax);
678         min_diff = FFMIN(min_diff, p->min_diff);
679         max_diff = FFMAX(max_diff, p->max_diff);
680         diff1_sum_x2 += p->diff1_sum_x2;
681         diff1_sum += p->diff1_sum;
682         min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
683         max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
684         sigma_x2 += p->sigma_x2;
685         noise_floor = FFMAX(noise_floor, p->noise_floor);
686         p->entropy = calc_entropy(s, p);
687         entropy += p->entropy;
688         min_count += p->min_count;
689         max_count += p->max_count;
690         noise_floor_count += p->noise_floor_count;
691         min_runs += p->min_runs;
692         max_runs += p->max_runs;
693         mask |= p->mask;
694         imask &= p->imask;
695         nb_samples += p->nb_samples;
696         nb_nans += p->nb_nans;
697         nb_infs += p->nb_infs;
698         nb_denormals += p->nb_denormals;
699         if (fabs(p->sigma_x) > fabs(max_sigma_x))
700             max_sigma_x = p->sigma_x;
701 
702         if (s->measure_perchannel != MEASURE_NONE)
703             av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
704         if (s->measure_perchannel & MEASURE_DC_OFFSET)
705             av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
706         if (s->measure_perchannel & MEASURE_MIN_LEVEL)
707             av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
708         if (s->measure_perchannel & MEASURE_MAX_LEVEL)
709             av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
710         if (s->measure_perchannel & MEASURE_MIN_DIFFERENCE)
711             av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", p->min_diff);
712         if (s->measure_perchannel & MEASURE_MAX_DIFFERENCE)
713             av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", p->max_diff);
714         if (s->measure_perchannel & MEASURE_MEAN_DIFFERENCE)
715             av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", p->diff1_sum / (p->nb_samples - 1));
716         if (s->measure_perchannel & MEASURE_RMS_DIFFERENCE)
717             av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
718         if (s->measure_perchannel & MEASURE_PEAK_LEVEL)
719             av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
720         if (s->measure_perchannel & MEASURE_RMS_LEVEL)
721             av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
722         if (s->measure_perchannel & MEASURE_RMS_PEAK)
723             av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
724         if (s->measure_perchannel & MEASURE_RMS_TROUGH)
725             if (p->min_sigma_x2 != 1)
726                 av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
727         if (s->measure_perchannel & MEASURE_CREST_FACTOR)
728             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);
729         if (s->measure_perchannel & MEASURE_FLAT_FACTOR)
730             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)));
731         if (s->measure_perchannel & MEASURE_PEAK_COUNT)
732             av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
733         if (s->measure_perchannel & MEASURE_NOISE_FLOOR)
734             av_log(ctx, AV_LOG_INFO, "Noise floor dB: %f\n", LINEAR_TO_DB(p->noise_floor));
735         if (s->measure_perchannel & MEASURE_NOISE_FLOOR_COUNT)
736             av_log(ctx, AV_LOG_INFO, "Noise floor count: %"PRId64"\n", p->noise_floor_count);
737         if (s->measure_perchannel & MEASURE_ENTROPY)
738             av_log(ctx, AV_LOG_INFO, "Entropy: %f\n", p->entropy);
739         if (s->measure_perchannel & MEASURE_BIT_DEPTH) {
740             bit_depth(s, p->mask, p->imask, &depth);
741             av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
742         }
743         if (s->measure_perchannel & MEASURE_DYNAMIC_RANGE)
744             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));
745         if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS)
746             av_log(ctx, AV_LOG_INFO, "Zero crossings: %"PRId64"\n", p->zero_runs);
747         if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS_RATE)
748             av_log(ctx, AV_LOG_INFO, "Zero crossings rate: %f\n", p->zero_runs/(double)p->nb_samples);
749         if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_NANS)
750             av_log(ctx, AV_LOG_INFO, "Number of NaNs: %"PRId64"\n", p->nb_nans);
751         if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_INFS)
752             av_log(ctx, AV_LOG_INFO, "Number of Infs: %"PRId64"\n", p->nb_infs);
753         if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_DENORMALS)
754             av_log(ctx, AV_LOG_INFO, "Number of denormals: %"PRId64"\n", p->nb_denormals);
755     }
756 
757     if (s->measure_overall != MEASURE_NONE)
758         av_log(ctx, AV_LOG_INFO, "Overall\n");
759     if (s->measure_overall & MEASURE_DC_OFFSET)
760         av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
761     if (s->measure_overall & MEASURE_MIN_LEVEL)
762         av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
763     if (s->measure_overall & MEASURE_MAX_LEVEL)
764         av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
765     if (s->measure_overall & MEASURE_MIN_DIFFERENCE)
766         av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", min_diff);
767     if (s->measure_overall & MEASURE_MAX_DIFFERENCE)
768         av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", max_diff);
769     if (s->measure_overall & MEASURE_MEAN_DIFFERENCE)
770         av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", diff1_sum / (nb_samples - s->nb_channels));
771     if (s->measure_overall & MEASURE_RMS_DIFFERENCE)
772         av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
773     if (s->measure_overall & MEASURE_PEAK_LEVEL)
774         av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
775     if (s->measure_overall & MEASURE_RMS_LEVEL)
776         av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
777     if (s->measure_overall & MEASURE_RMS_PEAK)
778         av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
779     if (s->measure_overall & MEASURE_RMS_TROUGH)
780         if (min_sigma_x2 != 1)
781             av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
782     if (s->measure_overall & MEASURE_FLAT_FACTOR)
783         av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
784     if (s->measure_overall & MEASURE_PEAK_COUNT)
785         av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
786     if (s->measure_overall & MEASURE_NOISE_FLOOR)
787         av_log(ctx, AV_LOG_INFO, "Noise floor dB: %f\n", LINEAR_TO_DB(noise_floor));
788     if (s->measure_overall & MEASURE_NOISE_FLOOR_COUNT)
789         av_log(ctx, AV_LOG_INFO, "Noise floor count: %f\n", noise_floor_count / (double)s->nb_channels);
790     if (s->measure_overall & MEASURE_ENTROPY)
791         av_log(ctx, AV_LOG_INFO, "Entropy: %f\n", entropy / (double)s->nb_channels);
792     if (s->measure_overall & MEASURE_BIT_DEPTH) {
793         bit_depth(s, mask, imask, &depth);
794         av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
795     }
796     if (s->measure_overall & MEASURE_NUMBER_OF_SAMPLES)
797         av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
798     if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_NANS)
799         av_log(ctx, AV_LOG_INFO, "Number of NaNs: %f\n", nb_nans / (float)s->nb_channels);
800     if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_INFS)
801         av_log(ctx, AV_LOG_INFO, "Number of Infs: %f\n", nb_infs / (float)s->nb_channels);
802     if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_DENORMALS)
803         av_log(ctx, AV_LOG_INFO, "Number of denormals: %f\n", nb_denormals / (float)s->nb_channels);
804 }
805 
uninit(AVFilterContext * ctx)806 static av_cold void uninit(AVFilterContext *ctx)
807 {
808     AudioStatsContext *s = ctx->priv;
809 
810     if (s->nb_channels)
811         print_stats(ctx);
812     if (s->chstats) {
813         for (int i = 0; i < s->nb_channels; i++) {
814             ChannelStats *p = &s->chstats[i];
815 
816             av_freep(&p->win_samples);
817         }
818     }
819     av_freep(&s->chstats);
820 }
821 
822 static const AVFilterPad astats_inputs[] = {
823     {
824         .name         = "default",
825         .type         = AVMEDIA_TYPE_AUDIO,
826         .filter_frame = filter_frame,
827     },
828 };
829 
830 static const AVFilterPad astats_outputs[] = {
831     {
832         .name         = "default",
833         .type         = AVMEDIA_TYPE_AUDIO,
834         .config_props = config_output,
835     },
836 };
837 
838 const AVFilter ff_af_astats = {
839     .name          = "astats",
840     .description   = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
841     .priv_size     = sizeof(AudioStatsContext),
842     .priv_class    = &astats_class,
843     .uninit        = uninit,
844     FILTER_INPUTS(astats_inputs),
845     FILTER_OUTPUTS(astats_outputs),
846     FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
847                       AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
848                       AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64P,
849                       AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
850                       AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP),
851     .flags         = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_METADATA_ONLY,
852 };
853