FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
24 #include "libavutil/opt.h"
25 #include "audio.h"
26 #include "avfilter.h"
27 #include "internal.h"
28 
29 typedef struct ChannelStats {
30  double last;
31  double last_non_zero;
32  double min_non_zero;
33  double sigma_x, sigma_x2;
35  double min, max;
36  double nmin, nmax;
37  double min_run, max_run;
38  double min_runs, max_runs;
39  double min_diff, max_diff;
40  double diff1_sum;
41  double diff1_sum_x2;
42  uint64_t mask, imask;
43  uint64_t min_count, max_count;
44  uint64_t zero_runs;
45  uint64_t nb_samples;
46 } ChannelStats;
47 
48 typedef struct AudioStatsContext {
49  const AVClass *class;
52  uint64_t tc_samples;
53  double time_constant;
54  double mult;
55  int metadata;
57  int nb_frames;
60 
61 #define OFFSET(x) offsetof(AudioStatsContext, x)
62 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
63 
64 static const AVOption astats_options[] = {
65  { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=.05}, .01, 10, FLAGS },
66  { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
67  { "reset", "recalculate stats after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
68  { NULL }
69 };
70 
71 AVFILTER_DEFINE_CLASS(astats);
72 
74 {
77  static const enum AVSampleFormat sample_fmts[] = {
84  };
85  int ret;
86 
87  layouts = ff_all_channel_counts();
88  if (!layouts)
89  return AVERROR(ENOMEM);
90  ret = ff_set_common_channel_layouts(ctx, layouts);
91  if (ret < 0)
92  return ret;
93 
94  formats = ff_make_format_list(sample_fmts);
95  if (!formats)
96  return AVERROR(ENOMEM);
97  ret = ff_set_common_formats(ctx, formats);
98  if (ret < 0)
99  return ret;
100 
101  formats = ff_all_samplerates();
102  if (!formats)
103  return AVERROR(ENOMEM);
104  return ff_set_common_samplerates(ctx, formats);
105 }
106 
108 {
109  int c;
110 
111  for (c = 0; c < s->nb_channels; c++) {
112  ChannelStats *p = &s->chstats[c];
113 
114  p->min = p->nmin = p->min_sigma_x2 = DBL_MAX;
115  p->max = p->nmax = p->max_sigma_x2 = DBL_MIN;
116  p->min_non_zero = DBL_MAX;
117  p->min_diff = DBL_MAX;
118  p->max_diff = DBL_MIN;
119  p->sigma_x = 0;
120  p->sigma_x2 = 0;
121  p->avg_sigma_x2 = 0;
122  p->min_run = 0;
123  p->max_run = 0;
124  p->min_runs = 0;
125  p->max_runs = 0;
126  p->diff1_sum = 0;
127  p->diff1_sum_x2 = 0;
128  p->mask = 0;
129  p->imask = 0xFFFFFFFFFFFFFFFF;
130  p->min_count = 0;
131  p->max_count = 0;
132  p->zero_runs = 0;
133  p->nb_samples = 0;
134  }
135 }
136 
137 static int config_output(AVFilterLink *outlink)
138 {
139  AudioStatsContext *s = outlink->src->priv;
140 
141  s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
142  if (!s->chstats)
143  return AVERROR(ENOMEM);
144  s->nb_channels = outlink->channels;
145  s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
146  s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5;
147  s->nb_frames = 0;
148  s->maxbitdepth = av_get_bytes_per_sample(outlink->format) * 8;
149 
150  reset_stats(s);
151 
152  return 0;
153 }
154 
155 static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
156 {
157  unsigned result = s->maxbitdepth;
158 
159  mask = mask & (~imask);
160 
161  for (; result && !(mask & 1); --result, mask >>= 1);
162 
163  depth->den = result;
164  depth->num = 0;
165 
166  for (; result; --result, mask >>= 1)
167  if (mask & 1)
168  depth->num++;
169 }
170 
171 static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d, double nd, int64_t i)
172 {
173  if (d < p->min) {
174  p->min = d;
175  p->nmin = nd;
176  p->min_run = 1;
177  p->min_runs = 0;
178  p->min_count = 1;
179  } else if (d == p->min) {
180  p->min_count++;
181  p->min_run = d == p->last ? p->min_run + 1 : 1;
182  } else if (p->last == p->min) {
183  p->min_runs += p->min_run * p->min_run;
184  }
185 
186  if (d != 0 && FFABS(d) < p->min_non_zero)
187  p->min_non_zero = FFABS(d);
188 
189  if (d > p->max) {
190  p->max = d;
191  p->nmax = nd;
192  p->max_run = 1;
193  p->max_runs = 0;
194  p->max_count = 1;
195  } else if (d == p->max) {
196  p->max_count++;
197  p->max_run = d == p->last ? p->max_run + 1 : 1;
198  } else if (p->last == p->max) {
199  p->max_runs += p->max_run * p->max_run;
200  }
201 
202  if (d != 0) {
203  p->zero_runs += FFSIGN(d) != FFSIGN(p->last_non_zero);
204  p->last_non_zero = d;
205  }
206 
207  p->sigma_x += nd;
208  p->sigma_x2 += nd * nd;
209  p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * nd * nd;
210  p->min_diff = FFMIN(p->min_diff, fabs(d - p->last));
211  p->max_diff = FFMAX(p->max_diff, fabs(d - p->last));
212  p->diff1_sum += fabs(d - p->last);
213  p->diff1_sum_x2 += (d - p->last) * (d - p->last);
214  p->last = d;
215  p->mask |= i;
216  p->imask &= i;
217 
218  if (p->nb_samples >= s->tc_samples) {
221  }
222  p->nb_samples++;
223 }
224 
225 static void set_meta(AVDictionary **metadata, int chan, const char *key,
226  const char *fmt, double val)
227 {
228  uint8_t value[128];
229  uint8_t key2[128];
230 
231  snprintf(value, sizeof(value), fmt, val);
232  if (chan)
233  snprintf(key2, sizeof(key2), "lavfi.astats.%d.%s", chan, key);
234  else
235  snprintf(key2, sizeof(key2), "lavfi.astats.%s", key);
236  av_dict_set(metadata, key2, value, 0);
237 }
238 
239 #define LINEAR_TO_DB(x) (log10(x) * 20)
240 
241 static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
242 {
243  uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0;
244  double min_runs = 0, max_runs = 0,
245  min = DBL_MAX, max = DBL_MIN, min_diff = DBL_MAX, max_diff = 0,
246  nmin = DBL_MAX, nmax = DBL_MIN,
247  max_sigma_x = 0,
248  diff1_sum = 0,
249  diff1_sum_x2 = 0,
250  sigma_x = 0,
251  sigma_x2 = 0,
252  min_sigma_x2 = DBL_MAX,
253  max_sigma_x2 = DBL_MIN;
254  AVRational depth;
255  int c;
256 
257  for (c = 0; c < s->nb_channels; c++) {
258  ChannelStats *p = &s->chstats[c];
259 
260  if (p->nb_samples < s->tc_samples)
261  p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
262 
263  min = FFMIN(min, p->min);
264  max = FFMAX(max, p->max);
265  nmin = FFMIN(nmin, p->nmin);
266  nmax = FFMAX(nmax, p->nmax);
267  min_diff = FFMIN(min_diff, p->min_diff);
268  max_diff = FFMAX(max_diff, p->max_diff);
269  diff1_sum += p->diff1_sum;
270  diff1_sum_x2 += p->diff1_sum_x2;
271  min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
272  max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
273  sigma_x += p->sigma_x;
274  sigma_x2 += p->sigma_x2;
275  min_count += p->min_count;
276  max_count += p->max_count;
277  min_runs += p->min_runs;
278  max_runs += p->max_runs;
279  mask |= p->mask;
280  imask &= p->imask;
281  nb_samples += p->nb_samples;
282  if (fabs(p->sigma_x) > fabs(max_sigma_x))
283  max_sigma_x = p->sigma_x;
284 
285  set_meta(metadata, c + 1, "DC_offset", "%f", p->sigma_x / p->nb_samples);
286  set_meta(metadata, c + 1, "Min_level", "%f", p->min);
287  set_meta(metadata, c + 1, "Max_level", "%f", p->max);
288  set_meta(metadata, c + 1, "Min_difference", "%f", p->min_diff);
289  set_meta(metadata, c + 1, "Max_difference", "%f", p->max_diff);
290  set_meta(metadata, c + 1, "Mean_difference", "%f", p->diff1_sum / (p->nb_samples - 1));
291  set_meta(metadata, c + 1, "RMS_difference", "%f", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
292  set_meta(metadata, c + 1, "Peak_level", "%f", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
293  set_meta(metadata, c + 1, "RMS_level", "%f", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
294  set_meta(metadata, c + 1, "RMS_peak", "%f", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
295  set_meta(metadata, c + 1, "RMS_trough", "%f", LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
296  set_meta(metadata, c + 1, "Crest_factor", "%f", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
297  set_meta(metadata, c + 1, "Flat_factor", "%f", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
298  set_meta(metadata, c + 1, "Peak_count", "%f", (float)(p->min_count + p->max_count));
299  bit_depth(s, p->mask, p->imask, &depth);
300  set_meta(metadata, c + 1, "Bit_depth", "%f", depth.num);
301  set_meta(metadata, c + 1, "Bit_depth2", "%f", depth.den);
302  set_meta(metadata, c + 1, "Dynamic_range", "%f", LINEAR_TO_DB(2 * FFMAX(FFABS(p->min), FFABS(p->max))/ p->min_non_zero));
303  set_meta(metadata, c + 1, "Zero_crossings", "%f", p->zero_runs);
304  set_meta(metadata, c + 1, "Zero_crossings_rate", "%f", p->zero_runs/(double)p->nb_samples);
305  }
306 
307  set_meta(metadata, 0, "Overall.DC_offset", "%f", max_sigma_x / (nb_samples / s->nb_channels));
308  set_meta(metadata, 0, "Overall.Min_level", "%f", min);
309  set_meta(metadata, 0, "Overall.Max_level", "%f", max);
310  set_meta(metadata, 0, "Overall.Min_difference", "%f", min_diff);
311  set_meta(metadata, 0, "Overall.Max_difference", "%f", max_diff);
312  set_meta(metadata, 0, "Overall.Mean_difference", "%f", diff1_sum / (nb_samples - s->nb_channels));
313  set_meta(metadata, 0, "Overall.RMS_difference", "%f", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
314  set_meta(metadata, 0, "Overall.Peak_level", "%f", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
315  set_meta(metadata, 0, "Overall.RMS_level", "%f", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
316  set_meta(metadata, 0, "Overall.RMS_peak", "%f", LINEAR_TO_DB(sqrt(max_sigma_x2)));
317  set_meta(metadata, 0, "Overall.RMS_trough", "%f", LINEAR_TO_DB(sqrt(min_sigma_x2)));
318  set_meta(metadata, 0, "Overall.Flat_factor", "%f", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
319  set_meta(metadata, 0, "Overall.Peak_count", "%f", (float)(min_count + max_count) / (double)s->nb_channels);
320  bit_depth(s, mask, imask, &depth);
321  set_meta(metadata, 0, "Overall.Bit_depth", "%f", depth.num);
322  set_meta(metadata, 0, "Overall.Bit_depth2", "%f", depth.den);
323  set_meta(metadata, 0, "Overall.Number_of_samples", "%f", nb_samples / s->nb_channels);
324 }
325 
326 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
327 {
328  AudioStatsContext *s = inlink->dst->priv;
329  AVDictionary **metadata = &buf->metadata;
330  const int channels = s->nb_channels;
331  int i, c;
332 
333  if (s->reset_count > 0) {
334  if (s->nb_frames >= s->reset_count) {
335  reset_stats(s);
336  s->nb_frames = 0;
337  }
338  s->nb_frames++;
339  }
340 
341  switch (inlink->format) {
342  case AV_SAMPLE_FMT_DBLP:
343  for (c = 0; c < channels; c++) {
344  ChannelStats *p = &s->chstats[c];
345  const double *src = (const double *)buf->extended_data[c];
346 
347  for (i = 0; i < buf->nb_samples; i++, src++)
348  update_stat(s, p, *src, *src, llrint(*src * (UINT64_C(1) << 63)));
349  }
350  break;
351  case AV_SAMPLE_FMT_DBL: {
352  const double *src = (const double *)buf->extended_data[0];
353 
354  for (i = 0; i < buf->nb_samples; i++) {
355  for (c = 0; c < channels; c++, src++)
356  update_stat(s, &s->chstats[c], *src, *src, llrint(*src * (UINT64_C(1) << 63)));
357  }}
358  break;
359  case AV_SAMPLE_FMT_FLTP:
360  for (c = 0; c < channels; c++) {
361  ChannelStats *p = &s->chstats[c];
362  const float *src = (const float *)buf->extended_data[c];
363 
364  for (i = 0; i < buf->nb_samples; i++, src++)
365  update_stat(s, p, *src, *src, llrint(*src * (UINT64_C(1) << 31)));
366  }
367  break;
368  case AV_SAMPLE_FMT_FLT: {
369  const float *src = (const float *)buf->extended_data[0];
370 
371  for (i = 0; i < buf->nb_samples; i++) {
372  for (c = 0; c < channels; c++, src++)
373  update_stat(s, &s->chstats[c], *src, *src, llrint(*src * (UINT64_C(1) << 31)));
374  }}
375  break;
376  case AV_SAMPLE_FMT_S64P:
377  for (c = 0; c < channels; c++) {
378  ChannelStats *p = &s->chstats[c];
379  const int64_t *src = (const int64_t *)buf->extended_data[c];
380 
381  for (i = 0; i < buf->nb_samples; i++, src++)
382  update_stat(s, p, *src, *src / (double)INT64_MAX, *src);
383  }
384  break;
385  case AV_SAMPLE_FMT_S64: {
386  const int64_t *src = (const int64_t *)buf->extended_data[0];
387 
388  for (i = 0; i < buf->nb_samples; i++) {
389  for (c = 0; c < channels; c++, src++)
390  update_stat(s, &s->chstats[c], *src, *src / (double)INT64_MAX, *src);
391  }}
392  break;
393  case AV_SAMPLE_FMT_S32P:
394  for (c = 0; c < channels; c++) {
395  ChannelStats *p = &s->chstats[c];
396  const int32_t *src = (const int32_t *)buf->extended_data[c];
397 
398  for (i = 0; i < buf->nb_samples; i++, src++)
399  update_stat(s, p, *src, *src / (double)INT32_MAX, *src);
400  }
401  break;
402  case AV_SAMPLE_FMT_S32: {
403  const int32_t *src = (const int32_t *)buf->extended_data[0];
404 
405  for (i = 0; i < buf->nb_samples; i++) {
406  for (c = 0; c < channels; c++, src++)
407  update_stat(s, &s->chstats[c], *src, *src / (double)INT32_MAX, *src);
408  }}
409  break;
410  case AV_SAMPLE_FMT_S16P:
411  for (c = 0; c < channels; c++) {
412  ChannelStats *p = &s->chstats[c];
413  const int16_t *src = (const int16_t *)buf->extended_data[c];
414 
415  for (i = 0; i < buf->nb_samples; i++, src++)
416  update_stat(s, p, *src, *src / (double)INT16_MAX, *src);
417  }
418  break;
419  case AV_SAMPLE_FMT_S16: {
420  const int16_t *src = (const int16_t *)buf->extended_data[0];
421 
422  for (i = 0; i < buf->nb_samples; i++) {
423  for (c = 0; c < channels; c++, src++)
424  update_stat(s, &s->chstats[c], *src, *src / (double)INT16_MAX, *src);
425  }}
426  break;
427  }
428 
429  if (s->metadata)
430  set_metadata(s, metadata);
431 
432  return ff_filter_frame(inlink->dst->outputs[0], buf);
433 }
434 
436 {
437  AudioStatsContext *s = ctx->priv;
438  uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0;
439  double min_runs = 0, max_runs = 0,
440  min = DBL_MAX, max = DBL_MIN, min_diff = DBL_MAX, max_diff = 0,
441  nmin = DBL_MAX, nmax = DBL_MIN,
442  max_sigma_x = 0,
443  diff1_sum_x2 = 0,
444  diff1_sum = 0,
445  sigma_x = 0,
446  sigma_x2 = 0,
447  min_sigma_x2 = DBL_MAX,
448  max_sigma_x2 = DBL_MIN;
449  AVRational depth;
450  int c;
451 
452  for (c = 0; c < s->nb_channels; c++) {
453  ChannelStats *p = &s->chstats[c];
454 
455  if (p->nb_samples < s->tc_samples)
456  p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
457 
458  min = FFMIN(min, p->min);
459  max = FFMAX(max, p->max);
460  nmin = FFMIN(nmin, p->nmin);
461  nmax = FFMAX(nmax, p->nmax);
462  min_diff = FFMIN(min_diff, p->min_diff);
463  max_diff = FFMAX(max_diff, p->max_diff);
464  diff1_sum_x2 += p->diff1_sum_x2;
465  diff1_sum += p->diff1_sum;
466  min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
467  max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
468  sigma_x += p->sigma_x;
469  sigma_x2 += p->sigma_x2;
470  min_count += p->min_count;
471  max_count += p->max_count;
472  min_runs += p->min_runs;
473  max_runs += p->max_runs;
474  mask |= p->mask;
475  imask &= p->imask;
476  nb_samples += p->nb_samples;
477  if (fabs(p->sigma_x) > fabs(max_sigma_x))
478  max_sigma_x = p->sigma_x;
479 
480  av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
481  av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
482  av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
483  av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
484  av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", p->min_diff);
485  av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", p->max_diff);
486  av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", p->diff1_sum / (p->nb_samples - 1));
487  av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
488  av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
489  av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
490  av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
491  if (p->min_sigma_x2 != 1)
492  av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
493  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);
494  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)));
495  av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
496  bit_depth(s, p->mask, p->imask, &depth);
497  av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
498  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));
499  av_log(ctx, AV_LOG_INFO, "Zero crossings: %"PRId64"\n", p->zero_runs);
500  av_log(ctx, AV_LOG_INFO, "Zero crossings rate: %f\n", p->zero_runs/(double)p->nb_samples);
501  }
502 
503  av_log(ctx, AV_LOG_INFO, "Overall\n");
504  av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
505  av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
506  av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
507  av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", min_diff);
508  av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", max_diff);
509  av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", diff1_sum / (nb_samples - s->nb_channels));
510  av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
511  av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
512  av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
513  av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
514  if (min_sigma_x2 != 1)
515  av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
516  av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
517  av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
518  bit_depth(s, mask, imask, &depth);
519  av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
520  av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
521 }
522 
524 {
525  AudioStatsContext *s = ctx->priv;
526 
527  if (s->nb_channels)
528  print_stats(ctx);
529  av_freep(&s->chstats);
530 }
531 
532 static const AVFilterPad astats_inputs[] = {
533  {
534  .name = "default",
535  .type = AVMEDIA_TYPE_AUDIO,
536  .filter_frame = filter_frame,
537  },
538  { NULL }
539 };
540 
541 static const AVFilterPad astats_outputs[] = {
542  {
543  .name = "default",
544  .type = AVMEDIA_TYPE_AUDIO,
545  .config_props = config_output,
546  },
547  { NULL }
548 };
549 
551  .name = "astats",
552  .description = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
553  .query_formats = query_formats,
554  .priv_size = sizeof(AudioStatsContext),
555  .priv_class = &astats_class,
556  .uninit = uninit,
557  .inputs = astats_inputs,
558  .outputs = astats_outputs,
559 };
float, planar
Definition: samplefmt.h:69
#define NULL
Definition: coverity.c:32
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:549
const char const char void * val
Definition: avisynth_c.h:771
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
AVOption.
Definition: opt.h:246
const char * fmt
Definition: avisynth_c.h:769
static int query_formats(AVFilterContext *ctx)
Definition: af_astats.c:73
AVFilter ff_af_astats
Definition: af_astats.c:550
Main libavfilter public API header.
#define OFFSET(x)
Definition: af_astats.c:61
double min_run
Definition: af_astats.c:37
channels
Definition: aptx.c:30
double min
Definition: af_astats.c:35
int num
Numerator.
Definition: rational.h:59
double, planar
Definition: samplefmt.h:70
double max_sigma_x2
Definition: af_astats.c:34
const char * key
#define src
Definition: vp8dsp.c:254
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:244
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
ChannelStats * chstats
Definition: af_astats.c:50
const char * name
Pad name.
Definition: internal.h:60
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
uint8_t
#define av_cold
Definition: attributes.h:82
AVOptions.
double nmin
Definition: af_astats.c:36
static const AVFilterPad astats_inputs[]
Definition: af_astats.c:532
#define LINEAR_TO_DB(x)
Definition: af_astats.c:239
double diff1_sum_x2
Definition: af_astats.c:41
static void update_stat(AudioStatsContext *s, ChannelStats *p, double d, double nd, int64_t i)
Definition: af_astats.c:171
AVDictionary * metadata
metadata.
Definition: frame.h:513
signed 32 bits
Definition: samplefmt.h:62
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Definition: af_astats.c:326
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_astats.c:523
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:568
static const uint16_t mask[17]
Definition: lzw.c:38
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
AVFILTER_DEFINE_CLASS(astats)
uint64_t tc_samples
Definition: af_astats.c:52
double max
Definition: af_astats.c:35
#define FFMAX(a, b)
Definition: common.h:94
int8_t exp
Definition: eval.c:72
double last_non_zero
Definition: af_astats.c:31
double sigma_x2
Definition: af_astats.c:33
static void print_stats(AVFilterContext *ctx)
Definition: af_astats.c:435
#define FFMIN(a, b)
Definition: common.h:96
signed 32 bits, planar
Definition: samplefmt.h:68
double min_sigma_x2
Definition: af_astats.c:34
signed 64 bits
Definition: samplefmt.h:71
#define FFSIGN(a)
Definition: common.h:73
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
Definition: af_astats.c:155
double max_runs
Definition: af_astats.c:38
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
static int config_output(AVFilterLink *outlink)
Definition: af_astats.c:137
static void reset_stats(AudioStatsContext *s)
Definition: af_astats.c:107
uint64_t max_count
Definition: af_astats.c:43
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
double sigma_x
Definition: af_astats.c:33
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
double nmax
Definition: af_astats.c:36
A list of supported channel layouts.
Definition: formats.h:85
static const AVOption astats_options[]
Definition: af_astats.c:64
double min_diff
Definition: af_astats.c:39
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
uint64_t mask
Definition: af_astats.c:42
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
void * buf
Definition: avisynth_c.h:690
#define llrint(x)
Definition: libm.h:394
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
double avg_sigma_x2
Definition: af_astats.c:34
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
double max_run
Definition: af_astats.c:37
Rational number (pair of numerator and denominator).
Definition: rational.h:58
double max_diff
Definition: af_astats.c:39
double min_non_zero
Definition: af_astats.c:32
const char * name
Filter name.
Definition: avfilter.h:148
#define snprintf
Definition: snprintf.h:34
#define FLAGS
Definition: af_astats.c:62
double last
Definition: af_astats.c:30
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
double time_constant
Definition: af_astats.c:53
uint64_t nb_samples
Definition: af_astats.c:45
signed 16 bits
Definition: samplefmt.h:61
static double c[64]
static void set_meta(AVDictionary **metadata, int chan, const char *key, const char *fmt, double val)
Definition: af_astats.c:225
int den
Denominator.
Definition: rational.h:60
uint64_t min_count
Definition: af_astats.c:43
double min_runs
Definition: af_astats.c:38
double diff1_sum
Definition: af_astats.c:40
A list of supported formats for one end of a filter link.
Definition: formats.h:64
signed 64 bits, planar
Definition: samplefmt.h:72
An instance of a filter.
Definition: avfilter.h:338
static const AVFilterPad astats_outputs[]
Definition: af_astats.c:541
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define av_freep(p)
signed 16 bits, planar
Definition: samplefmt.h:67
formats
Definition: signature.h:48
internal API functions
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition...
Definition: formats.c:410
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:273
float min
uint64_t imask
Definition: af_astats.c:42
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:292
uint64_t zero_runs
Definition: af_astats.c:44
for(j=16;j >0;--j)
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556
static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
Definition: af_astats.c:241