FFmpeg
af_astats.c
Go to the documentation of this file.
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;
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;
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;
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;
98  uint64_t tc_samples;
99  double time_constant;
100  double mult;
101  int metadata;
107  int is_float;
110 
111 #define OFFSET(x) offsetof(AudioStatsContext, x)
112 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
113 
114 static const AVOption astats_options[] = {
115  { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=.05}, 0, 10, FLAGS },
116  { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
117  { "reset", "recalculate stats after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
118  { "measure_perchannel", "only measure_perchannel these per-channel statistics", OFFSET(measure_perchannel), AV_OPT_TYPE_FLAGS, {.i64=MEASURE_ALL}, 0, UINT_MAX, FLAGS, "measure" },
119  { "none" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NONE }, 0, 0, FLAGS, "measure" },
120  { "all" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ALL }, 0, 0, FLAGS, "measure" },
121  { "DC_offset" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_DC_OFFSET }, 0, 0, FLAGS, "measure" },
122  { "Min_level" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MIN_LEVEL }, 0, 0, FLAGS, "measure" },
123  { "Max_level" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MAX_LEVEL }, 0, 0, FLAGS, "measure" },
124  { "Min_difference" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MIN_DIFFERENCE }, 0, 0, FLAGS, "measure" },
125  { "Max_difference" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MAX_DIFFERENCE }, 0, 0, FLAGS, "measure" },
126  { "Mean_difference" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MEAN_DIFFERENCE }, 0, 0, FLAGS, "measure" },
127  { "RMS_difference" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_DIFFERENCE }, 0, 0, FLAGS, "measure" },
128  { "Peak_level" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_PEAK_LEVEL }, 0, 0, FLAGS, "measure" },
129  { "RMS_level" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_LEVEL }, 0, 0, FLAGS, "measure" },
130  { "RMS_peak" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_PEAK }, 0, 0, FLAGS, "measure" },
131  { "RMS_trough" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_TROUGH }, 0, 0, FLAGS, "measure" },
132  { "Crest_factor" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_CREST_FACTOR }, 0, 0, FLAGS, "measure" },
133  { "Flat_factor" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_FLAT_FACTOR }, 0, 0, FLAGS, "measure" },
134  { "Peak_count" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_PEAK_COUNT }, 0, 0, FLAGS, "measure" },
135  { "Bit_depth" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_BIT_DEPTH }, 0, 0, FLAGS, "measure" },
136  { "Dynamic_range" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_DYNAMIC_RANGE }, 0, 0, FLAGS, "measure" },
137  { "Zero_crossings" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ZERO_CROSSINGS }, 0, 0, FLAGS, "measure" },
138  { "Zero_crossings_rate" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ZERO_CROSSINGS_RATE }, 0, 0, FLAGS, "measure" },
139  { "Noise_floor" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NOISE_FLOOR }, 0, 0, FLAGS, "measure" },
140  { "Noise_floor_count" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NOISE_FLOOR_COUNT }, 0, 0, FLAGS, "measure" },
141  { "Entropy" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ENTROPY }, 0, 0, FLAGS, "measure" },
142  { "Number_of_samples" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_SAMPLES }, 0, 0, FLAGS, "measure" },
143  { "Number_of_NaNs" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_NANS }, 0, 0, FLAGS, "measure" },
144  { "Number_of_Infs" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_INFS }, 0, 0, FLAGS, "measure" },
145  { "Number_of_denormals" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_DENORMALS }, 0, 0, FLAGS, "measure" },
146  { "measure_overall", "only measure_perchannel these overall statistics", OFFSET(measure_overall), AV_OPT_TYPE_FLAGS, {.i64=MEASURE_ALL}, 0, UINT_MAX, FLAGS, "measure" },
147  { NULL }
148 };
149 
150 AVFILTER_DEFINE_CLASS(astats);
151 
153 {
154  static const enum AVSampleFormat sample_fmts[] = {
161  };
163  if (ret < 0)
164  return ret;
165 
167  if (ret < 0)
168  return ret;
169 
171 }
172 
174 {
175  int c;
176 
177  for (c = 0; c < s->nb_channels; c++) {
178  ChannelStats *p = &s->chstats[c];
179 
180  p->min = p->nmin = p->min_sigma_x2 = DBL_MAX;
181  p->max = p->nmax = p->max_sigma_x2 =-DBL_MAX;
182  p->min_non_zero = DBL_MAX;
183  p->min_diff = DBL_MAX;
184  p->max_diff = 0;
185  p->sigma_x = 0;
186  p->sigma_x2 = 0;
187  p->avg_sigma_x2 = 0;
188  p->min_run = 0;
189  p->max_run = 0;
190  p->min_runs = 0;
191  p->max_runs = 0;
192  p->diff1_sum = 0;
193  p->diff1_sum_x2 = 0;
194  p->mask = 0;
195  p->imask = 0xFFFFFFFFFFFFFFFF;
196  p->min_count = 0;
197  p->max_count = 0;
198  p->zero_runs = 0;
199  p->nb_samples = 0;
200  p->nb_nans = 0;
201  p->nb_infs = 0;
202  p->nb_denormals = 0;
203  p->last = NAN;
204  p->noise_floor = NAN;
205  p->noise_floor_count = 0;
206  p->entropy = 0;
207  p->win_pos = 0;
208  memset(p->win_samples, 0, s->tc_samples * sizeof(*p->win_samples));
209  memset(p->histogram, 0, sizeof(p->histogram));
210  memset(p->ehistogram, 0, sizeof(p->ehistogram));
211  }
212 }
213 
214 static int config_output(AVFilterLink *outlink)
215 {
216  AudioStatsContext *s = outlink->src->priv;
217 
218  s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
219  if (!s->chstats)
220  return AVERROR(ENOMEM);
221 
222  s->tc_samples = FFMAX(s->time_constant * outlink->sample_rate + .5, 1);
223  s->nb_channels = outlink->channels;
224 
225  for (int i = 0; i < s->nb_channels; i++) {
226  ChannelStats *p = &s->chstats[i];
227 
228  p->win_samples = av_calloc(s->tc_samples, sizeof(*p->win_samples));
229  if (!p->win_samples)
230  return AVERROR(ENOMEM);
231  }
232 
233  s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
234  s->nb_frames = 0;
235  s->maxbitdepth = av_get_bytes_per_sample(outlink->format) * 8;
236  s->is_double = outlink->format == AV_SAMPLE_FMT_DBL ||
237  outlink->format == AV_SAMPLE_FMT_DBLP;
238 
239  s->is_float = outlink->format == AV_SAMPLE_FMT_FLT ||
240  outlink->format == AV_SAMPLE_FMT_FLTP;
241 
242  reset_stats(s);
243 
244  return 0;
245 }
246 
247 static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
248 {
249  unsigned result = s->maxbitdepth;
250 
251  mask = mask & (~imask);
252 
253  for (; result && !(mask & 1); --result, mask >>= 1);
254 
255  depth->den = result;
256  depth->num = 0;
257 
258  for (; result; --result, mask >>= 1)
259  if (mask & 1)
260  depth->num++;
261 }
262 
264 {
265  double entropy = 0.;
266 
267  for (int i = 0; i < HISTOGRAM_SIZE; i++) {
268  double entry = p->ehistogram[i] / ((double)p->nb_samples);
269 
270  if (entry > 1e-8)
271  entropy += entry * log2(entry);
272  }
273 
274  return -entropy / log2(HISTOGRAM_SIZE);
275 }
276 
277 static inline void update_minmax(AudioStatsContext *s, ChannelStats *p, double d)
278 {
279  if (d < p->min)
280  p->min = d;
281  if (d > p->max)
282  p->max = d;
283 }
284 
285 static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d, double nd, int64_t i)
286 {
287  double drop;
288  int index;
289 
290  if (d < p->min) {
291  p->min = d;
292  p->nmin = nd;
293  p->min_run = 1;
294  p->min_runs = 0;
295  p->min_count = 1;
296  } else if (d == p->min) {
297  p->min_count++;
298  p->min_run = d == p->last ? p->min_run + 1 : 1;
299  } else if (p->last == p->min) {
300  p->min_runs += p->min_run * p->min_run;
301  }
302 
303  if (d != 0 && FFABS(d) < p->min_non_zero)
304  p->min_non_zero = FFABS(d);
305 
306  if (d > p->max) {
307  p->max = d;
308  p->nmax = nd;
309  p->max_run = 1;
310  p->max_runs = 0;
311  p->max_count = 1;
312  } else if (d == p->max) {
313  p->max_count++;
314  p->max_run = d == p->last ? p->max_run + 1 : 1;
315  } else if (p->last == p->max) {
316  p->max_runs += p->max_run * p->max_run;
317  }
318 
319  if (d != 0) {
320  p->zero_runs += FFSIGN(d) != FFSIGN(p->last_non_zero);
321  p->last_non_zero = d;
322  }
323 
324  p->sigma_x += nd;
325  p->sigma_x2 += nd * nd;
326  p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * nd * nd;
327  if (!isnan(p->last)) {
328  p->min_diff = FFMIN(p->min_diff, fabs(d - p->last));
329  p->max_diff = FFMAX(p->max_diff, fabs(d - p->last));
330  p->diff1_sum += fabs(d - p->last);
331  p->diff1_sum_x2 += (d - p->last) * (d - p->last);
332  }
333  p->last = d;
334  p->mask |= i;
335  p->imask &= i;
336 
337  drop = p->win_samples[p->win_pos];
338  p->win_samples[p->win_pos] = nd;
339  index = av_clip(lrint(av_clipd(FFABS(nd), 0.0, 1.0) * HISTOGRAM_MAX), 0, HISTOGRAM_MAX);
340  p->max_index = FFMAX(p->max_index, index);
341  p->histogram[index]++;
342  p->ehistogram[index]++;
343  if (!isnan(p->noise_floor))
344  p->histogram[av_clip(lrint(av_clipd(FFABS(drop), 0.0, 1.0) * HISTOGRAM_MAX), 0, HISTOGRAM_MAX)]--;
345  p->win_pos++;
346 
347  while (p->histogram[p->max_index] == 0)
348  p->max_index--;
349  if (p->win_pos >= s->tc_samples || !isnan(p->noise_floor)) {
350  double noise_floor = 1.;
351 
352  for (int i = p->max_index; i >= 0; i--) {
353  if (p->histogram[i]) {
354  noise_floor = i / (double)HISTOGRAM_MAX;
355  break;
356  }
357  }
358 
359  if (isnan(p->noise_floor)) {
360  p->noise_floor = noise_floor;
361  p->noise_floor_count = 1;
362  } else {
363  if (noise_floor < p->noise_floor) {
364  p->noise_floor = noise_floor;
365  p->noise_floor_count = 1;
366  } else if (noise_floor == p->noise_floor) {
367  p->noise_floor_count++;
368  }
369  }
370  }
371 
372  if (p->win_pos >= s->tc_samples) {
373  p->win_pos = 0;
374  }
375 
376  if (p->nb_samples >= s->tc_samples) {
379  }
380  p->nb_samples++;
381 }
382 
383 static inline void update_float_stat(AudioStatsContext *s, ChannelStats *p, float d)
384 {
385  int type = fpclassify(d);
386 
387  p->nb_nans += type == FP_NAN;
388  p->nb_infs += type == FP_INFINITE;
389  p->nb_denormals += type == FP_SUBNORMAL;
390 }
391 
392 static inline void update_double_stat(AudioStatsContext *s, ChannelStats *p, double d)
393 {
394  int type = fpclassify(d);
395 
396  p->nb_nans += type == FP_NAN;
397  p->nb_infs += type == FP_INFINITE;
398  p->nb_denormals += type == FP_SUBNORMAL;
399 }
400 
401 static void set_meta(AVDictionary **metadata, int chan, const char *key,
402  const char *fmt, double val)
403 {
404  uint8_t value[128];
405  uint8_t key2[128];
406 
407  snprintf(value, sizeof(value), fmt, val);
408  if (chan)
409  snprintf(key2, sizeof(key2), "lavfi.astats.%d.%s", chan, key);
410  else
411  snprintf(key2, sizeof(key2), "lavfi.astats.%s", key);
412  av_dict_set(metadata, key2, value, 0);
413 }
414 
415 #define LINEAR_TO_DB(x) (log10(x) * 20)
416 
417 static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
418 {
419  uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0, noise_floor_count = 0;
420  uint64_t nb_nans = 0, nb_infs = 0, nb_denormals = 0;
421  double min_runs = 0, max_runs = 0,
422  min = DBL_MAX, max =-DBL_MAX, min_diff = DBL_MAX, max_diff = 0,
423  nmin = DBL_MAX, nmax =-DBL_MAX,
424  max_sigma_x = 0,
425  diff1_sum = 0,
426  diff1_sum_x2 = 0,
427  sigma_x = 0,
428  sigma_x2 = 0,
429  noise_floor = 0,
430  entropy = 0,
431  min_sigma_x2 = DBL_MAX,
432  max_sigma_x2 =-DBL_MAX;
433  AVRational depth;
434  int c;
435 
436  for (c = 0; c < s->nb_channels; c++) {
437  ChannelStats *p = &s->chstats[c];
438 
439  if (p->nb_samples < s->tc_samples)
440  p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
441 
442  min = FFMIN(min, p->min);
443  max = FFMAX(max, p->max);
444  nmin = FFMIN(nmin, p->nmin);
445  nmax = FFMAX(nmax, p->nmax);
446  min_diff = FFMIN(min_diff, p->min_diff);
447  max_diff = FFMAX(max_diff, p->max_diff);
448  diff1_sum += p->diff1_sum;
449  diff1_sum_x2 += p->diff1_sum_x2;
450  min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
451  max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
452  sigma_x += p->sigma_x;
453  sigma_x2 += p->sigma_x2;
454  noise_floor = FFMAX(noise_floor, p->noise_floor);
455  noise_floor_count += p->noise_floor_count;
456  p->entropy = calc_entropy(s, p);
457  entropy += p->entropy;
458  min_count += p->min_count;
459  max_count += p->max_count;
460  min_runs += p->min_runs;
461  max_runs += p->max_runs;
462  mask |= p->mask;
463  imask &= p->imask;
464  nb_samples += p->nb_samples;
465  nb_nans += p->nb_nans;
466  nb_infs += p->nb_infs;
467  nb_denormals += p->nb_denormals;
468  if (fabs(p->sigma_x) > fabs(max_sigma_x))
469  max_sigma_x = p->sigma_x;
470 
471  if (s->measure_perchannel & MEASURE_DC_OFFSET)
472  set_meta(metadata, c + 1, "DC_offset", "%f", p->sigma_x / p->nb_samples);
473  if (s->measure_perchannel & MEASURE_MIN_LEVEL)
474  set_meta(metadata, c + 1, "Min_level", "%f", p->min);
475  if (s->measure_perchannel & MEASURE_MAX_LEVEL)
476  set_meta(metadata, c + 1, "Max_level", "%f", p->max);
477  if (s->measure_perchannel & MEASURE_MIN_DIFFERENCE)
478  set_meta(metadata, c + 1, "Min_difference", "%f", p->min_diff);
479  if (s->measure_perchannel & MEASURE_MAX_DIFFERENCE)
480  set_meta(metadata, c + 1, "Max_difference", "%f", p->max_diff);
481  if (s->measure_perchannel & MEASURE_MEAN_DIFFERENCE)
482  set_meta(metadata, c + 1, "Mean_difference", "%f", p->diff1_sum / (p->nb_samples - 1));
483  if (s->measure_perchannel & MEASURE_RMS_DIFFERENCE)
484  set_meta(metadata, c + 1, "RMS_difference", "%f", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
485  if (s->measure_perchannel & MEASURE_PEAK_LEVEL)
486  set_meta(metadata, c + 1, "Peak_level", "%f", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
487  if (s->measure_perchannel & MEASURE_RMS_LEVEL)
488  set_meta(metadata, c + 1, "RMS_level", "%f", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
489  if (s->measure_perchannel & MEASURE_RMS_PEAK)
490  set_meta(metadata, c + 1, "RMS_peak", "%f", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
491  if (s->measure_perchannel & MEASURE_RMS_TROUGH)
492  set_meta(metadata, c + 1, "RMS_trough", "%f", LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
493  if (s->measure_perchannel & MEASURE_CREST_FACTOR)
494  set_meta(metadata, c + 1, "Crest_factor", "%f", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
495  if (s->measure_perchannel & MEASURE_FLAT_FACTOR)
496  set_meta(metadata, c + 1, "Flat_factor", "%f", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
497  if (s->measure_perchannel & MEASURE_PEAK_COUNT)
498  set_meta(metadata, c + 1, "Peak_count", "%f", (float)(p->min_count + p->max_count));
499  if (s->measure_perchannel & MEASURE_NOISE_FLOOR)
500  set_meta(metadata, c + 1, "Noise_floor", "%f", LINEAR_TO_DB(p->noise_floor));
501  if (s->measure_perchannel & MEASURE_NOISE_FLOOR_COUNT)
502  set_meta(metadata, c + 1, "Noise_floor_count", "%f", p->noise_floor_count);
503  if (s->measure_perchannel & MEASURE_ENTROPY)
504  set_meta(metadata, c + 1, "Entropy", "%f", p->entropy);
505  if (s->measure_perchannel & MEASURE_BIT_DEPTH) {
506  bit_depth(s, p->mask, p->imask, &depth);
507  set_meta(metadata, c + 1, "Bit_depth", "%f", depth.num);
508  set_meta(metadata, c + 1, "Bit_depth2", "%f", depth.den);
509  }
510  if (s->measure_perchannel & MEASURE_DYNAMIC_RANGE)
511  set_meta(metadata, c + 1, "Dynamic_range", "%f", LINEAR_TO_DB(2 * FFMAX(FFABS(p->min), FFABS(p->max))/ p->min_non_zero));
512  if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS)
513  set_meta(metadata, c + 1, "Zero_crossings", "%f", p->zero_runs);
514  if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS_RATE)
515  set_meta(metadata, c + 1, "Zero_crossings_rate", "%f", p->zero_runs/(double)p->nb_samples);
516  if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_NANS)
517  set_meta(metadata, c + 1, "Number of NaNs", "%f", p->nb_nans);
518  if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_INFS)
519  set_meta(metadata, c + 1, "Number of Infs", "%f", p->nb_infs);
520  if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_DENORMALS)
521  set_meta(metadata, c + 1, "Number of denormals", "%f", p->nb_denormals);
522  }
523 
524  if (s->measure_overall & MEASURE_DC_OFFSET)
525  set_meta(metadata, 0, "Overall.DC_offset", "%f", max_sigma_x / (nb_samples / s->nb_channels));
526  if (s->measure_overall & MEASURE_MIN_LEVEL)
527  set_meta(metadata, 0, "Overall.Min_level", "%f", min);
528  if (s->measure_overall & MEASURE_MAX_LEVEL)
529  set_meta(metadata, 0, "Overall.Max_level", "%f", max);
530  if (s->measure_overall & MEASURE_MIN_DIFFERENCE)
531  set_meta(metadata, 0, "Overall.Min_difference", "%f", min_diff);
532  if (s->measure_overall & MEASURE_MAX_DIFFERENCE)
533  set_meta(metadata, 0, "Overall.Max_difference", "%f", max_diff);
534  if (s->measure_overall & MEASURE_MEAN_DIFFERENCE)
535  set_meta(metadata, 0, "Overall.Mean_difference", "%f", diff1_sum / (nb_samples - s->nb_channels));
536  if (s->measure_overall & MEASURE_RMS_DIFFERENCE)
537  set_meta(metadata, 0, "Overall.RMS_difference", "%f", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
538  if (s->measure_overall & MEASURE_PEAK_LEVEL)
539  set_meta(metadata, 0, "Overall.Peak_level", "%f", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
540  if (s->measure_overall & MEASURE_RMS_LEVEL)
541  set_meta(metadata, 0, "Overall.RMS_level", "%f", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
542  if (s->measure_overall & MEASURE_RMS_PEAK)
543  set_meta(metadata, 0, "Overall.RMS_peak", "%f", LINEAR_TO_DB(sqrt(max_sigma_x2)));
544  if (s->measure_overall & MEASURE_RMS_TROUGH)
545  set_meta(metadata, 0, "Overall.RMS_trough", "%f", LINEAR_TO_DB(sqrt(min_sigma_x2)));
546  if (s->measure_overall & MEASURE_FLAT_FACTOR)
547  set_meta(metadata, 0, "Overall.Flat_factor", "%f", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
548  if (s->measure_overall & MEASURE_PEAK_COUNT)
549  set_meta(metadata, 0, "Overall.Peak_count", "%f", (float)(min_count + max_count) / (double)s->nb_channels);
550  if (s->measure_overall & MEASURE_NOISE_FLOOR)
551  set_meta(metadata, 0, "Overall.Noise_floor", "%f", LINEAR_TO_DB(noise_floor));
552  if (s->measure_overall & MEASURE_NOISE_FLOOR_COUNT)
553  set_meta(metadata, 0, "Overall.Noise_floor_count", "%f", noise_floor_count / (double)s->nb_channels);
554  if (s->measure_overall & MEASURE_ENTROPY)
555  set_meta(metadata, 0, "Overall.Entropy", "%f", entropy / (double)s->nb_channels);
556  if (s->measure_overall & MEASURE_BIT_DEPTH) {
557  bit_depth(s, mask, imask, &depth);
558  set_meta(metadata, 0, "Overall.Bit_depth", "%f", depth.num);
559  set_meta(metadata, 0, "Overall.Bit_depth2", "%f", depth.den);
560  }
561  if (s->measure_overall & MEASURE_NUMBER_OF_SAMPLES)
562  set_meta(metadata, 0, "Overall.Number_of_samples", "%f", nb_samples / s->nb_channels);
563  if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_NANS)
564  set_meta(metadata, 0, "Number of NaNs", "%f", nb_nans / (float)s->nb_channels);
565  if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_INFS)
566  set_meta(metadata, 0, "Number of Infs", "%f", nb_infs / (float)s->nb_channels);
567  if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_DENORMALS)
568  set_meta(metadata, 0, "Number of denormals", "%f", nb_denormals / (float)s->nb_channels);
569 }
570 
571 #define UPDATE_STATS_P(type, update_func, update_float, channel_func) \
572  for (int c = start; c < end; c++) { \
573  ChannelStats *p = &s->chstats[c]; \
574  const type *src = (const type *)data[c]; \
575  const type * const srcend = src + samples; \
576  for (; src < srcend; src++) { \
577  update_func; \
578  update_float; \
579  } \
580  channel_func; \
581  }
582 
583 #define UPDATE_STATS_I(type, update_func, update_float, channel_func) \
584  for (int c = start; c < end; c++) { \
585  ChannelStats *p = &s->chstats[c]; \
586  const type *src = (const type *)data[0]; \
587  const type * const srcend = src + samples * channels; \
588  for (src += c; src < srcend; src += channels) { \
589  update_func; \
590  update_float; \
591  } \
592  channel_func; \
593  }
594 
595 #define UPDATE_STATS(planar, type, sample, normalizer_suffix, int_sample) \
596  if ((s->measure_overall | s->measure_perchannel) & ~MEASURE_MINMAXPEAK) { \
597  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, ); \
598  } else { \
599  UPDATE_STATS_##planar(type, update_minmax(s, p, sample), , p->nmin = p->min normalizer_suffix; p->nmax = p->max normalizer_suffix;); \
600  }
601 
602 static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
603 {
604  AudioStatsContext *s = ctx->priv;
605  AVFilterLink *inlink = ctx->inputs[0];
606  AVFrame *buf = arg;
607  const uint8_t * const * const data = (const uint8_t * const *)buf->extended_data;
608  const int channels = s->nb_channels;
609  const int samples = buf->nb_samples;
610  const int start = (buf->channels * jobnr) / nb_jobs;
611  const int end = (buf->channels * (jobnr+1)) / nb_jobs;
612 
613  switch (inlink->format) {
614  case AV_SAMPLE_FMT_DBLP:
615  UPDATE_STATS(P, double, *src, , llrint(*src * (UINT64_C(1) << 63)));
616  break;
617  case AV_SAMPLE_FMT_DBL:
618  UPDATE_STATS(I, double, *src, , llrint(*src * (UINT64_C(1) << 63)));
619  break;
620  case AV_SAMPLE_FMT_FLTP:
621  UPDATE_STATS(P, float, *src, , llrint(*src * (UINT64_C(1) << 31)));
622  break;
623  case AV_SAMPLE_FMT_FLT:
624  UPDATE_STATS(I, float, *src, , llrint(*src * (UINT64_C(1) << 31)));
625  break;
626  case AV_SAMPLE_FMT_S64P:
627  UPDATE_STATS(P, int64_t, *src, / (double)INT64_MAX, *src);
628  break;
629  case AV_SAMPLE_FMT_S64:
630  UPDATE_STATS(I, int64_t, *src, / (double)INT64_MAX, *src);
631  break;
632  case AV_SAMPLE_FMT_S32P:
633  UPDATE_STATS(P, int32_t, *src, / (double)INT32_MAX, *src);
634  break;
635  case AV_SAMPLE_FMT_S32:
636  UPDATE_STATS(I, int32_t, *src, / (double)INT32_MAX, *src);
637  break;
638  case AV_SAMPLE_FMT_S16P:
639  UPDATE_STATS(P, int16_t, *src, / (double)INT16_MAX, *src);
640  break;
641  case AV_SAMPLE_FMT_S16:
642  UPDATE_STATS(I, int16_t, *src, / (double)INT16_MAX, *src);
643  break;
644  }
645 
646  return 0;
647 }
648 
650 {
651  AVFilterContext *ctx = inlink->dst;
652  AudioStatsContext *s = ctx->priv;
653  AVDictionary **metadata = &buf->metadata;
654 
655  if (s->reset_count > 0) {
656  if (s->nb_frames >= s->reset_count) {
657  reset_stats(s);
658  s->nb_frames = 0;
659  }
660  s->nb_frames++;
661  }
662 
665 
666  if (s->metadata)
667  set_metadata(s, metadata);
668 
669  return ff_filter_frame(inlink->dst->outputs[0], buf);
670 }
671 
673 {
674  AudioStatsContext *s = ctx->priv;
675  uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0, noise_floor_count = 0;
676  uint64_t nb_nans = 0, nb_infs = 0, nb_denormals = 0;
677  double min_runs = 0, max_runs = 0,
678  min = DBL_MAX, max =-DBL_MAX, min_diff = DBL_MAX, max_diff = 0,
679  nmin = DBL_MAX, nmax =-DBL_MAX,
680  max_sigma_x = 0,
681  diff1_sum_x2 = 0,
682  diff1_sum = 0,
683  sigma_x = 0,
684  sigma_x2 = 0,
685  noise_floor = 0,
686  entropy = 0,
687  min_sigma_x2 = DBL_MAX,
688  max_sigma_x2 =-DBL_MAX;
689  AVRational depth;
690  int c;
691 
692  for (c = 0; c < s->nb_channels; c++) {
693  ChannelStats *p = &s->chstats[c];
694 
695  if (p->nb_samples < s->tc_samples)
696  p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
697 
698  min = FFMIN(min, p->min);
699  max = FFMAX(max, p->max);
700  nmin = FFMIN(nmin, p->nmin);
701  nmax = FFMAX(nmax, p->nmax);
702  min_diff = FFMIN(min_diff, p->min_diff);
703  max_diff = FFMAX(max_diff, p->max_diff);
704  diff1_sum_x2 += p->diff1_sum_x2;
705  diff1_sum += p->diff1_sum;
706  min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
707  max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
708  sigma_x += p->sigma_x;
709  sigma_x2 += p->sigma_x2;
710  noise_floor = FFMAX(noise_floor, p->noise_floor);
711  p->entropy = calc_entropy(s, p);
712  entropy += p->entropy;
713  min_count += p->min_count;
714  max_count += p->max_count;
715  noise_floor_count += p->noise_floor_count;
716  min_runs += p->min_runs;
717  max_runs += p->max_runs;
718  mask |= p->mask;
719  imask &= p->imask;
720  nb_samples += p->nb_samples;
721  nb_nans += p->nb_nans;
722  nb_infs += p->nb_infs;
723  nb_denormals += p->nb_denormals;
724  if (fabs(p->sigma_x) > fabs(max_sigma_x))
725  max_sigma_x = p->sigma_x;
726 
727  if (s->measure_perchannel != MEASURE_NONE)
728  av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
729  if (s->measure_perchannel & MEASURE_DC_OFFSET)
730  av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
731  if (s->measure_perchannel & MEASURE_MIN_LEVEL)
732  av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
733  if (s->measure_perchannel & MEASURE_MAX_LEVEL)
734  av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
735  if (s->measure_perchannel & MEASURE_MIN_DIFFERENCE)
736  av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", p->min_diff);
737  if (s->measure_perchannel & MEASURE_MAX_DIFFERENCE)
738  av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", p->max_diff);
739  if (s->measure_perchannel & MEASURE_MEAN_DIFFERENCE)
740  av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", p->diff1_sum / (p->nb_samples - 1));
741  if (s->measure_perchannel & MEASURE_RMS_DIFFERENCE)
742  av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
743  if (s->measure_perchannel & MEASURE_PEAK_LEVEL)
744  av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
745  if (s->measure_perchannel & MEASURE_RMS_LEVEL)
746  av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
747  if (s->measure_perchannel & MEASURE_RMS_PEAK)
748  av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
749  if (s->measure_perchannel & MEASURE_RMS_TROUGH)
750  if (p->min_sigma_x2 != 1)
751  av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
752  if (s->measure_perchannel & MEASURE_CREST_FACTOR)
753  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);
754  if (s->measure_perchannel & MEASURE_FLAT_FACTOR)
755  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)));
756  if (s->measure_perchannel & MEASURE_PEAK_COUNT)
757  av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
758  if (s->measure_perchannel & MEASURE_NOISE_FLOOR)
759  av_log(ctx, AV_LOG_INFO, "Noise floor dB: %f\n", LINEAR_TO_DB(p->noise_floor));
760  if (s->measure_perchannel & MEASURE_NOISE_FLOOR_COUNT)
761  av_log(ctx, AV_LOG_INFO, "Noise floor count: %"PRId64"\n", p->noise_floor_count);
762  if (s->measure_perchannel & MEASURE_ENTROPY)
763  av_log(ctx, AV_LOG_INFO, "Entropy: %f\n", p->entropy);
764  if (s->measure_perchannel & MEASURE_BIT_DEPTH) {
765  bit_depth(s, p->mask, p->imask, &depth);
766  av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
767  }
768  if (s->measure_perchannel & MEASURE_DYNAMIC_RANGE)
769  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));
770  if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS)
771  av_log(ctx, AV_LOG_INFO, "Zero crossings: %"PRId64"\n", p->zero_runs);
772  if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS_RATE)
773  av_log(ctx, AV_LOG_INFO, "Zero crossings rate: %f\n", p->zero_runs/(double)p->nb_samples);
774  if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_NANS)
775  av_log(ctx, AV_LOG_INFO, "Number of NaNs: %"PRId64"\n", p->nb_nans);
776  if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_INFS)
777  av_log(ctx, AV_LOG_INFO, "Number of Infs: %"PRId64"\n", p->nb_infs);
778  if ((s->is_float || s->is_double) && s->measure_perchannel & MEASURE_NUMBER_OF_DENORMALS)
779  av_log(ctx, AV_LOG_INFO, "Number of denormals: %"PRId64"\n", p->nb_denormals);
780  }
781 
782  if (s->measure_overall != MEASURE_NONE)
783  av_log(ctx, AV_LOG_INFO, "Overall\n");
784  if (s->measure_overall & MEASURE_DC_OFFSET)
785  av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
786  if (s->measure_overall & MEASURE_MIN_LEVEL)
787  av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
788  if (s->measure_overall & MEASURE_MAX_LEVEL)
789  av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
790  if (s->measure_overall & MEASURE_MIN_DIFFERENCE)
791  av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", min_diff);
792  if (s->measure_overall & MEASURE_MAX_DIFFERENCE)
793  av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", max_diff);
794  if (s->measure_overall & MEASURE_MEAN_DIFFERENCE)
795  av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", diff1_sum / (nb_samples - s->nb_channels));
796  if (s->measure_overall & MEASURE_RMS_DIFFERENCE)
797  av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
798  if (s->measure_overall & MEASURE_PEAK_LEVEL)
799  av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
800  if (s->measure_overall & MEASURE_RMS_LEVEL)
801  av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
802  if (s->measure_overall & MEASURE_RMS_PEAK)
803  av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
804  if (s->measure_overall & MEASURE_RMS_TROUGH)
805  if (min_sigma_x2 != 1)
806  av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
807  if (s->measure_overall & MEASURE_FLAT_FACTOR)
808  av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
809  if (s->measure_overall & MEASURE_PEAK_COUNT)
810  av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
811  if (s->measure_overall & MEASURE_NOISE_FLOOR)
812  av_log(ctx, AV_LOG_INFO, "Noise floor dB: %f\n", LINEAR_TO_DB(noise_floor));
813  if (s->measure_overall & MEASURE_NOISE_FLOOR_COUNT)
814  av_log(ctx, AV_LOG_INFO, "Noise floor count: %f\n", noise_floor_count / (double)s->nb_channels);
815  if (s->measure_overall & MEASURE_ENTROPY)
816  av_log(ctx, AV_LOG_INFO, "Entropy: %f\n", entropy / (double)s->nb_channels);
817  if (s->measure_overall & MEASURE_BIT_DEPTH) {
818  bit_depth(s, mask, imask, &depth);
819  av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
820  }
821  if (s->measure_overall & MEASURE_NUMBER_OF_SAMPLES)
822  av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
823  if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_NANS)
824  av_log(ctx, AV_LOG_INFO, "Number of NaNs: %f\n", nb_nans / (float)s->nb_channels);
825  if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_INFS)
826  av_log(ctx, AV_LOG_INFO, "Number of Infs: %f\n", nb_infs / (float)s->nb_channels);
827  if ((s->is_float || s->is_double) && s->measure_overall & MEASURE_NUMBER_OF_DENORMALS)
828  av_log(ctx, AV_LOG_INFO, "Number of denormals: %f\n", nb_denormals / (float)s->nb_channels);
829 }
830 
832 {
833  AudioStatsContext *s = ctx->priv;
834 
835  if (s->nb_channels)
836  print_stats(ctx);
837  if (s->chstats) {
838  for (int i = 0; i < s->nb_channels; i++) {
839  ChannelStats *p = &s->chstats[i];
840 
841  av_freep(&p->win_samples);
842  }
843  }
844  av_freep(&s->chstats);
845 }
846 
847 static const AVFilterPad astats_inputs[] = {
848  {
849  .name = "default",
850  .type = AVMEDIA_TYPE_AUDIO,
851  .filter_frame = filter_frame,
852  },
853 };
854 
855 static const AVFilterPad astats_outputs[] = {
856  {
857  .name = "default",
858  .type = AVMEDIA_TYPE_AUDIO,
859  .config_props = config_output,
860  },
861 };
862 
864  .name = "astats",
865  .description = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
866  .query_formats = query_formats,
867  .priv_size = sizeof(AudioStatsContext),
868  .priv_class = &astats_class,
869  .uninit = uninit,
873 };
ChannelStats::nmax
double nmax
Definition: af_astats.c:71
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
bit_depth
static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
Definition: af_astats.c:247
av_clip
#define av_clip
Definition: common.h:96
AudioStatsContext::nb_channels
int nb_channels
Definition: af_astats.c:97
set_meta
static void set_meta(AVDictionary **metadata, int chan, const char *key, const char *fmt, double val)
Definition: af_astats.c:401
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
MEASURE_PEAK_COUNT
#define MEASURE_PEAK_COUNT
Definition: af_astats.c:49
AudioStatsContext::is_double
int is_double
Definition: af_astats.c:108
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1019
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
astats_options
static const AVOption astats_options[]
Definition: af_astats.c:114
AudioStatsContext::maxbitdepth
int maxbitdepth
Definition: af_astats.c:104
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
MEASURE_RMS_TROUGH
#define MEASURE_RMS_TROUGH
Definition: af_astats.c:46
MEASURE_MIN_LEVEL
#define MEASURE_MIN_LEVEL
Definition: af_astats.c:37
ChannelStats::diff1_sum
double diff1_sum
Definition: af_astats.c:75
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
MEASURE_ALL
#define MEASURE_ALL
Definition: af_astats.c:33
index
fg index
Definition: ffmpeg_filter.c:168
MEASURE_NONE
#define MEASURE_NONE
Definition: af_astats.c:34
AudioStatsContext::is_float
int is_float
Definition: af_astats.c:107
AVOption
AVOption.
Definition: opt.h:247
ChannelStats::diff1_sum_x2
double diff1_sum_x2
Definition: af_astats.c:76
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:68
data
const char data[16]
Definition: mxf.c:143
config_output
static int config_output(AVFilterLink *outlink)
Definition: af_astats.c:214
AudioStatsContext::tc_samples
uint64_t tc_samples
Definition: af_astats.c:98
ff_set_common_all_samplerates
int ff_set_common_all_samplerates(AVFilterContext *ctx)
Equivalent to ff_set_common_samplerates(ctx, ff_all_samplerates())
Definition: formats.c:687
float.h
max
#define max(a, b)
Definition: cuda_runtime.h:33
AVDictionary
Definition: dict.c:30
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:153
ChannelStats::max_sigma_x2
double max_sigma_x2
Definition: af_astats.c:69
reset_stats
static void reset_stats(AudioStatsContext *s)
Definition: af_astats.c:173
ChannelStats::last_non_zero
double last_non_zero
Definition: af_astats.c:66
update_stat
static void update_stat(AudioStatsContext *s, ChannelStats *p, double d, double nd, int64_t i)
Definition: af_astats.c:285
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:361
FFSIGN
#define FFSIGN(a)
Definition: common.h:66
ChannelStats::nb_nans
uint64_t nb_nans
Definition: af_astats.c:82
update_minmax
static void update_minmax(AudioStatsContext *s, ChannelStats *p, double d)
Definition: af_astats.c:277
val
static double val(void *priv, double ch)
Definition: aeval.c:76
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AV_SAMPLE_FMT_S64P
@ AV_SAMPLE_FMT_S64P
signed 64 bits, planar
Definition: samplefmt.h:72
ChannelStats::ehistogram
uint64_t ehistogram[HISTOGRAM_SIZE]
Definition: af_astats.c:87
OFFSET
#define OFFSET(x)
Definition: af_astats.c:111
ChannelStats::avg_sigma_x2
double avg_sigma_x2
Definition: af_astats.c:69
AVRational::num
int num
Numerator.
Definition: rational.h:59
ChannelStats::mask
uint64_t mask
Definition: af_astats.c:77
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
ChannelStats::nmin
double nmin
Definition: af_astats.c:71
LINEAR_TO_DB
#define LINEAR_TO_DB(x)
Definition: af_astats.c:415
lrint
#define lrint
Definition: tablegen.h:53
av_cold
#define av_cold
Definition: attributes.h:90
ChannelStats::win_pos
int win_pos
Definition: af_astats.c:88
mask
static const uint16_t mask[17]
Definition: lzw.c:38
ChannelStats::entropy
double entropy
Definition: af_astats.c:91
ChannelStats::last
double last
Definition: af_astats.c:65
AudioStatsContext::mult
double mult
Definition: af_astats.c:100
s
#define s(width, name)
Definition: cbs_vp9.c:257
AudioStatsContext::measure_overall
int measure_overall
Definition: af_astats.c:106
AVFrame::channels
int channels
number of audio channels, only used for audio.
Definition: frame.h:592
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:226
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Definition: af_astats.c:649
ff_set_common_formats_from_list
int ff_set_common_formats_from_list(AVFilterContext *ctx, const int *fmts)
Equivalent to ff_set_common_formats(ctx, ff_make_format_list(fmts))
Definition: formats.c:703
calc_entropy
static double calc_entropy(AudioStatsContext *s, ChannelStats *p)
Definition: af_astats.c:263
ctx
AVFormatContext * ctx
Definition: movenc.c:48
UPDATE_STATS
#define UPDATE_STATS(planar, type, sample, normalizer_suffix, int_sample)
Definition: af_astats.c:595
channels
channels
Definition: aptx.h:33
ChannelStats::max
double max
Definition: af_astats.c:70
update_double_stat
static void update_double_stat(AudioStatsContext *s, ChannelStats *p, double d)
Definition: af_astats.c:392
ChannelStats::max_index
int max_index
Definition: af_astats.c:89
HISTOGRAM_MAX
#define HISTOGRAM_MAX
Definition: af_astats.c:31
update_float_stat
static void update_float_stat(AudioStatsContext *s, ChannelStats *p, float d)
Definition: af_astats.c:383
key
const char * key
Definition: hwcontext_opencl.c:168
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_astats.c:831
NAN
#define NAN
Definition: mathematics.h:64
ChannelStats::min_diff
double min_diff
Definition: af_astats.c:74
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:152
arg
const char * arg
Definition: jacosubdec.c:67
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:65
ChannelStats::min_count
uint64_t min_count
Definition: af_astats.c:78
MEASURE_RMS_DIFFERENCE
#define MEASURE_RMS_DIFFERENCE
Definition: af_astats.c:42
ChannelStats::sigma_x
double sigma_x
Definition: af_astats.c:68
ChannelStats::noise_floor_count
uint64_t noise_floor_count
Definition: af_astats.c:79
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
NULL
#define NULL
Definition: coverity.c:32
MEASURE_NOISE_FLOOR_COUNT
#define MEASURE_NOISE_FLOOR_COUNT
Definition: af_astats.c:59
ChannelStats::win_samples
double * win_samples
Definition: af_astats.c:85
MEASURE_ZERO_CROSSINGS_RATE
#define MEASURE_ZERO_CROSSINGS_RATE
Definition: af_astats.c:53
MEASURE_DC_OFFSET
#define MEASURE_DC_OFFSET
Definition: af_astats.c:36
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
isnan
#define isnan(x)
Definition: libm.h:340
ChannelStats::min_runs
double min_runs
Definition: af_astats.c:73
src
#define src
Definition: vp8dsp.c:255
ff_set_common_all_channel_counts
int ff_set_common_all_channel_counts(AVFilterContext *ctx)
Equivalent to ff_set_common_channel_layouts(ctx, ff_all_channel_counts())
Definition: formats.c:669
exp
int8_t exp
Definition: eval.c:72
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
FLAGS
#define FLAGS
Definition: af_astats.c:112
MEASURE_FLAT_FACTOR
#define MEASURE_FLAT_FACTOR
Definition: af_astats.c:48
AudioStatsContext::measure_perchannel
int measure_perchannel
Definition: af_astats.c:105
HISTOGRAM_SIZE
#define HISTOGRAM_SIZE
Definition: af_astats.c:30
AudioStatsContext
Definition: af_astats.c:94
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
P
#define P
av_clipd
#define av_clipd
Definition: common.h:147
MEASURE_DYNAMIC_RANGE
#define MEASURE_DYNAMIC_RANGE
Definition: af_astats.c:51
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
ChannelStats::nb_samples
uint64_t nb_samples
Definition: af_astats.c:81
MEASURE_NUMBER_OF_SAMPLES
#define MEASURE_NUMBER_OF_SAMPLES
Definition: af_astats.c:54
MEASURE_ENTROPY
#define MEASURE_ENTROPY
Definition: af_astats.c:60
MEASURE_RMS_PEAK
#define MEASURE_RMS_PEAK
Definition: af_astats.c:45
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:67
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
internal.h
ChannelStats::max_diff
double max_diff
Definition: af_astats.c:74
astats_inputs
static const AVFilterPad astats_inputs[]
Definition: af_astats.c:847
ChannelStats::min_sigma_x2
double min_sigma_x2
Definition: af_astats.c:69
MEASURE_MEAN_DIFFERENCE
#define MEASURE_MEAN_DIFFERENCE
Definition: af_astats.c:41
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:369
i
int i
Definition: input.c:406
ChannelStats
Definition: af_astats.c:64
MEASURE_NUMBER_OF_DENORMALS
#define MEASURE_NUMBER_OF_DENORMALS
Definition: af_astats.c:57
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
ff_af_astats
const AVFilter ff_af_astats
Definition: af_astats.c:863
ChannelStats::min_run
double min_run
Definition: af_astats.c:72
filter_channel
static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_astats.c:602
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:350
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:804
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_astats.c:152
MEASURE_PEAK_LEVEL
#define MEASURE_PEAK_LEVEL
Definition: af_astats.c:43
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ChannelStats::max_run
double max_run
Definition: af_astats.c:72
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
set_metadata
static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
Definition: af_astats.c:417
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:271
ChannelStats::max_runs
double max_runs
Definition: af_astats.c:73
log2
#define log2(x)
Definition: libm.h:404
AVFilter
Filter definition.
Definition: avfilter.h:149
MEASURE_RMS_LEVEL
#define MEASURE_RMS_LEVEL
Definition: af_astats.c:44
ret
ret
Definition: filter_design.txt:187
MEASURE_NUMBER_OF_NANS
#define MEASURE_NUMBER_OF_NANS
Definition: af_astats.c:55
MEASURE_BIT_DEPTH
#define MEASURE_BIT_DEPTH
Definition: af_astats.c:50
AudioStatsContext::nb_frames
int nb_frames
Definition: af_astats.c:103
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
avfilter.h
AVFrame::metadata
AVDictionary * metadata
metadata.
Definition: frame.h:572
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:70
AudioStatsContext::reset_count
int reset_count
Definition: af_astats.c:102
ChannelStats::histogram
uint64_t histogram[HISTOGRAM_SIZE]
Definition: af_astats.c:86
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
AVFilterContext
An instance of a filter.
Definition: avfilter.h:346
MEASURE_NUMBER_OF_INFS
#define MEASURE_NUMBER_OF_INFS
Definition: af_astats.c:56
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:121
ChannelStats::noise_floor
double noise_floor
Definition: af_astats.c:90
MEASURE_NOISE_FLOOR
#define MEASURE_NOISE_FLOOR
Definition: af_astats.c:58
audio.h
AudioStatsContext::metadata
int metadata
Definition: af_astats.c:101
llrint
#define llrint(x)
Definition: libm.h:394
ChannelStats::nb_denormals
uint64_t nb_denormals
Definition: af_astats.c:84
ChannelStats::sigma_x2
double sigma_x2
Definition: af_astats.c:68
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(astats)
print_stats
static void print_stats(AVFilterContext *ctx)
Definition: af_astats.c:672
MEASURE_MAX_LEVEL
#define MEASURE_MAX_LEVEL
Definition: af_astats.c:38
MEASURE_MIN_DIFFERENCE
#define MEASURE_MIN_DIFFERENCE
Definition: af_astats.c:39
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:241
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:153
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
AudioStatsContext::chstats
ChannelStats * chstats
Definition: af_astats.c:96
d
d
Definition: ffmpeg_filter.c:156
AV_OPT_TYPE_FLAGS
@ AV_OPT_TYPE_FLAGS
Definition: opt.h:223
int32_t
int32_t
Definition: audioconvert.c:56
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ChannelStats::zero_runs
uint64_t zero_runs
Definition: af_astats.c:80
MEASURE_CREST_FACTOR
#define MEASURE_CREST_FACTOR
Definition: af_astats.c:47
ChannelStats::imask
uint64_t imask
Definition: af_astats.c:77
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:64
ff_filter_execute
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: internal.h:143
ChannelStats::min
double min
Definition: af_astats.c:70
AudioStatsContext::time_constant
double time_constant
Definition: af_astats.c:99
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:62
ChannelStats::max_count
uint64_t max_count
Definition: af_astats.c:78
ChannelStats::min_non_zero
double min_non_zero
Definition: af_astats.c:67
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233
snprintf
#define snprintf
Definition: snprintf.h:34
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:63
MEASURE_MAX_DIFFERENCE
#define MEASURE_MAX_DIFFERENCE
Definition: af_astats.c:40
astats_outputs
static const AVFilterPad astats_outputs[]
Definition: af_astats.c:855
ChannelStats::nb_infs
uint64_t nb_infs
Definition: af_astats.c:83
AV_SAMPLE_FMT_S64
@ AV_SAMPLE_FMT_S64
signed 64 bits
Definition: samplefmt.h:71
MEASURE_ZERO_CROSSINGS
#define MEASURE_ZERO_CROSSINGS
Definition: af_astats.c:52
min
float min
Definition: vorbis_enc_data.h:429