FFmpeg coverage


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