FFmpeg coverage


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