FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_astats.c
Date: 2022-12-05 03:11:11
Exec Total Coverage
Lines: 0 515 0.0%
Functions: 0 14 0.0%
Branches: 0 589 0.0%

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