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 sigma_x, sigma_x2;
33  double min, max;
34  double min_run, max_run;
35  double min_runs, max_runs;
36  uint64_t min_count, max_count;
37  uint64_t nb_samples;
38 } ChannelStats;
39 
40 typedef struct {
41  const AVClass *class;
44  uint64_t tc_samples;
45  double time_constant;
46  double mult;
47  int metadata;
49  int nb_frames;
51 
52 #define OFFSET(x) offsetof(AudioStatsContext, x)
53 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
54 
55 static const AVOption astats_options[] = {
56  { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=.05}, .01, 10, FLAGS },
57  { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
58  { "reset", "recalculate stats after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
59  { NULL }
60 };
61 
62 AVFILTER_DEFINE_CLASS(astats);
63 
65 {
68  static const enum AVSampleFormat sample_fmts[] = {
71  };
72  int ret;
73 
74  layouts = ff_all_channel_layouts();
75  if (!layouts)
76  return AVERROR(ENOMEM);
77  ret = ff_set_common_channel_layouts(ctx, layouts);
78  if (ret < 0)
79  return ret;
80 
81  formats = ff_make_format_list(sample_fmts);
82  if (!formats)
83  return AVERROR(ENOMEM);
84  ret = ff_set_common_formats(ctx, formats);
85  if (ret < 0)
86  return ret;
87 
88  formats = ff_all_samplerates();
89  if (!formats)
90  return AVERROR(ENOMEM);
91  return ff_set_common_samplerates(ctx, formats);
92 }
93 
95 {
96  int c;
97 
98  memset(s->chstats, 0, sizeof(*s->chstats));
99 
100  for (c = 0; c < s->nb_channels; c++) {
101  ChannelStats *p = &s->chstats[c];
102 
103  p->min = p->min_sigma_x2 = DBL_MAX;
104  p->max = p->max_sigma_x2 = DBL_MIN;
105  }
106 }
107 
108 static int config_output(AVFilterLink *outlink)
109 {
110  AudioStatsContext *s = outlink->src->priv;
111 
112  s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
113  if (!s->chstats)
114  return AVERROR(ENOMEM);
115  s->nb_channels = outlink->channels;
116  s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
117  s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5;
118 
119  reset_stats(s);
120 
121  return 0;
122 }
123 
124 static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d)
125 {
126  if (d < p->min) {
127  p->min = d;
128  p->min_run = 1;
129  p->min_runs = 0;
130  p->min_count = 1;
131  } else if (d == p->min) {
132  p->min_count++;
133  p->min_run = d == p->last ? p->min_run + 1 : 1;
134  } else if (p->last == p->min) {
135  p->min_runs += p->min_run * p->min_run;
136  }
137 
138  if (d > p->max) {
139  p->max = d;
140  p->max_run = 1;
141  p->max_runs = 0;
142  p->max_count = 1;
143  } else if (d == p->max) {
144  p->max_count++;
145  p->max_run = d == p->last ? p->max_run + 1 : 1;
146  } else if (p->last == p->max) {
147  p->max_runs += p->max_run * p->max_run;
148  }
149 
150  p->sigma_x += d;
151  p->sigma_x2 += d * d;
152  p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * d * d;
153  p->last = d;
154 
155  if (p->nb_samples >= s->tc_samples) {
158  }
159  p->nb_samples++;
160 }
161 
162 static void set_meta(AVDictionary **metadata, int chan, const char *key,
163  const char *fmt, double val)
164 {
165  uint8_t value[128];
166  uint8_t key2[128];
167 
168  snprintf(value, sizeof(value), fmt, val);
169  if (chan)
170  snprintf(key2, sizeof(key2), "lavfi.astats.%d.%s", chan, key);
171  else
172  snprintf(key2, sizeof(key2), "lavfi.astats.%s", key);
173  av_dict_set(metadata, key2, value, 0);
174 }
175 
176 #define LINEAR_TO_DB(x) (log10(x) * 20)
177 
178 static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
179 {
180  uint64_t min_count = 0, max_count = 0, nb_samples = 0;
181  double min_runs = 0, max_runs = 0,
182  min = DBL_MAX, max = DBL_MIN,
183  max_sigma_x = 0,
184  sigma_x = 0,
185  sigma_x2 = 0,
186  min_sigma_x2 = DBL_MAX,
187  max_sigma_x2 = DBL_MIN;
188  int c;
189 
190  for (c = 0; c < s->nb_channels; c++) {
191  ChannelStats *p = &s->chstats[c];
192 
193  if (p->nb_samples < s->tc_samples)
194  p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
195 
196  min = FFMIN(min, p->min);
197  max = FFMAX(max, p->max);
198  min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
199  max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
200  sigma_x += p->sigma_x;
201  sigma_x2 += p->sigma_x2;
202  min_count += p->min_count;
203  max_count += p->max_count;
204  min_runs += p->min_runs;
205  max_runs += p->max_runs;
206  nb_samples += p->nb_samples;
207  if (fabs(p->sigma_x) > fabs(max_sigma_x))
208  max_sigma_x = p->sigma_x;
209 
210  set_meta(metadata, c + 1, "DC_offset", "%f", p->sigma_x / p->nb_samples);
211  set_meta(metadata, c + 1, "Min_level", "%f", p->min);
212  set_meta(metadata, c + 1, "Max_level", "%f", p->max);
213  set_meta(metadata, c + 1, "Peak_level", "%f", LINEAR_TO_DB(FFMAX(-p->min, p->max)));
214  set_meta(metadata, c + 1, "RMS_level", "%f", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
215  set_meta(metadata, c + 1, "RMS_peak", "%f", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
216  set_meta(metadata, c + 1, "RMS_trough", "%f", LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
217  set_meta(metadata, c + 1, "Crest_factor", "%f", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
218  set_meta(metadata, c + 1, "Flat_factor", "%f", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
219  set_meta(metadata, c + 1, "Peak_count", "%f", (float)(p->min_count + p->max_count));
220  }
221 
222  set_meta(metadata, 0, "Overall.DC_offset", "%f", max_sigma_x / (nb_samples / s->nb_channels));
223  set_meta(metadata, 0, "Overall.Min_level", "%f", min);
224  set_meta(metadata, 0, "Overall.Max_level", "%f", max);
225  set_meta(metadata, 0, "Overall.Peak_level", "%f", LINEAR_TO_DB(FFMAX(-min, max)));
226  set_meta(metadata, 0, "Overall.RMS_level", "%f", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
227  set_meta(metadata, 0, "Overall.RMS_peak", "%f", LINEAR_TO_DB(sqrt(max_sigma_x2)));
228  set_meta(metadata, 0, "Overall.RMS_trough", "%f", LINEAR_TO_DB(sqrt(min_sigma_x2)));
229  set_meta(metadata, 0, "Overall.Flat_factor", "%f", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
230  set_meta(metadata, 0, "Overall.Peak_count", "%f", (float)(min_count + max_count) / (double)s->nb_channels);
231  set_meta(metadata, 0, "Overall.Number_of_samples", "%f", nb_samples / s->nb_channels);
232 }
233 
234 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
235 {
236  AudioStatsContext *s = inlink->dst->priv;
237  AVDictionary **metadata = avpriv_frame_get_metadatap(buf);
238  const int channels = s->nb_channels;
239  const double *src;
240  int i, c;
241 
242  switch (inlink->format) {
243  case AV_SAMPLE_FMT_DBLP:
244  for (c = 0; c < channels; c++) {
245  ChannelStats *p = &s->chstats[c];
246  src = (const double *)buf->extended_data[c];
247 
248  for (i = 0; i < buf->nb_samples; i++, src++)
249  update_stat(s, p, *src);
250  }
251  break;
252  case AV_SAMPLE_FMT_DBL:
253  src = (const double *)buf->extended_data[0];
254 
255  for (i = 0; i < buf->nb_samples; i++) {
256  for (c = 0; c < channels; c++, src++)
257  update_stat(s, &s->chstats[c], *src);
258  }
259  break;
260  }
261 
262  if (s->metadata)
263  set_metadata(s, metadata);
264 
265  if (s->reset_count > 0) {
266  s->nb_frames++;
267  if (s->nb_frames >= s->reset_count) {
268  reset_stats(s);
269  s->nb_frames = 0;
270  }
271  }
272 
273  return ff_filter_frame(inlink->dst->outputs[0], buf);
274 }
275 
276 static void print_stats(AVFilterContext *ctx)
277 {
278  AudioStatsContext *s = ctx->priv;
279  uint64_t min_count = 0, max_count = 0, nb_samples = 0;
280  double min_runs = 0, max_runs = 0,
281  min = DBL_MAX, max = DBL_MIN,
282  max_sigma_x = 0,
283  sigma_x = 0,
284  sigma_x2 = 0,
285  min_sigma_x2 = DBL_MAX,
286  max_sigma_x2 = DBL_MIN;
287  int c;
288 
289  for (c = 0; c < s->nb_channels; c++) {
290  ChannelStats *p = &s->chstats[c];
291 
292  if (p->nb_samples < s->tc_samples)
293  p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
294 
295  min = FFMIN(min, p->min);
296  max = FFMAX(max, p->max);
297  min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
298  max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
299  sigma_x += p->sigma_x;
300  sigma_x2 += p->sigma_x2;
301  min_count += p->min_count;
302  max_count += p->max_count;
303  min_runs += p->min_runs;
304  max_runs += p->max_runs;
305  nb_samples += p->nb_samples;
306  if (fabs(p->sigma_x) > fabs(max_sigma_x))
307  max_sigma_x = p->sigma_x;
308 
309  av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
310  av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
311  av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
312  av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
313  av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->min, p->max)));
314  av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
315  av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
316  if (p->min_sigma_x2 != 1)
317  av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
318  av_log(ctx, AV_LOG_INFO, "Crest factor: %f\n", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
319  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)));
320  av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
321  }
322 
323  av_log(ctx, AV_LOG_INFO, "Overall\n");
324  av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
325  av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
326  av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
327  av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-min, max)));
328  av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
329  av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
330  if (min_sigma_x2 != 1)
331  av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
332  av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
333  av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
334  av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
335 }
336 
337 static av_cold void uninit(AVFilterContext *ctx)
338 {
339  AudioStatsContext *s = ctx->priv;
340 
341  if (s->nb_channels)
342  print_stats(ctx);
343  av_freep(&s->chstats);
344 }
345 
346 static const AVFilterPad astats_inputs[] = {
347  {
348  .name = "default",
349  .type = AVMEDIA_TYPE_AUDIO,
350  .filter_frame = filter_frame,
351  },
352  { NULL }
353 };
354 
355 static const AVFilterPad astats_outputs[] = {
356  {
357  .name = "default",
358  .type = AVMEDIA_TYPE_AUDIO,
359  .config_props = config_output,
360  },
361  { NULL }
362 };
363 
365  .name = "astats",
366  .description = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
367  .query_formats = query_formats,
368  .priv_size = sizeof(AudioStatsContext),
369  .priv_class = &astats_class,
370  .uninit = uninit,
371  .inputs = astats_inputs,
372  .outputs = astats_outputs,
373 };
#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:523
const char const char void * val
Definition: avisynth_c.h:634
const char * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVOption.
Definition: opt.h:255
const char * fmt
Definition: avisynth_c.h:632
static int query_formats(AVFilterContext *ctx)
Definition: af_astats.c:64
AVFilter ff_af_astats
Definition: af_astats.c:364
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
static void update_stat(AudioStatsContext *s, ChannelStats *p, double d)
Definition: af_astats.c:124
Main libavfilter public API header.
#define OFFSET(x)
Definition: af_astats.c:52
double min_run
Definition: af_astats.c:34
double min
Definition: af_astats.c:33
double, planar
Definition: samplefmt.h:71
double max_sigma_x2
Definition: af_astats.c:32
static enum AVSampleFormat formats[]
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
ChannelStats * chstats
Definition: af_astats.c:42
const char * name
Pad name.
Definition: internal.h:67
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1145
uint8_t
#define av_cold
Definition: attributes.h:74
AVOptions.
static const AVFilterPad astats_inputs[]
Definition: af_astats.c:346
#define LINEAR_TO_DB(x)
Definition: af_astats.c:176
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:61
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Definition: af_astats.c:234
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_astats.c:337
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:542
#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:175
void * priv
private data for use by the filter
Definition: avfilter.h:654
AVFILTER_DEFINE_CLASS(astats)
uint64_t tc_samples
Definition: af_astats.c:44
double max
Definition: af_astats.c:33
#define FFMAX(a, b)
Definition: common.h:64
double sigma_x2
Definition: af_astats.c:31
static void print_stats(AVFilterContext *ctx)
Definition: af_astats.c:276
#define FFMIN(a, b)
Definition: common.h:66
double min_sigma_x2
Definition: af_astats.c:32
ret
Definition: avfilter.c:974
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
double max_runs
Definition: af_astats.c:35
static int config_output(AVFilterLink *outlink)
Definition: af_astats.c:108
static void reset_stats(AudioStatsContext *s)
Definition: af_astats.c:94
uint64_t max_count
Definition: af_astats.c:36
double sigma_x
Definition: af_astats.c:31
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (w...
Definition: formats.c:385
A list of supported channel layouts.
Definition: formats.h:85
static const AVOption astats_options[]
Definition: af_astats.c:55
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
AVS_Value src
Definition: avisynth_c.h:482
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
AVDictionary ** avpriv_frame_get_metadatap(AVFrame *frame)
Definition: frame.c:47
void * buf
Definition: avisynth_c.h:553
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:69
double avg_sigma_x2
Definition: af_astats.c:32
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:470
double max_run
Definition: af_astats.c:34
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:239
const char * name
Filter name.
Definition: avfilter.h:474
#define snprintf
Definition: snprintf.h:34
#define FLAGS
Definition: af_astats.c:53
double last
Definition: af_astats.c:30
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:379
void * av_calloc(size_t nmemb, size_t size)
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
Definition: mem.c:258
double time_constant
Definition: af_astats.c:45
uint64_t nb_samples
Definition: af_astats.c:37
static double c[64]
static void set_meta(AVDictionary **metadata, int chan, const char *key, const char *fmt, double val)
Definition: af_astats.c:162
uint64_t min_count
Definition: af_astats.c:36
double min_runs
Definition: af_astats.c:35
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:633
static const AVFilterPad astats_outputs[]
Definition: af_astats.c:355
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define av_freep(p)
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:215
float min
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225
for(j=16;j >0;--j)
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:530
static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
Definition: af_astats.c:178