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;
48 
49 #define OFFSET(x) offsetof(AudioStatsContext, x)
50 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51 
52 static const AVOption astats_options[] = {
53  { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=.05}, .01, 10, FLAGS },
54  { NULL }
55 };
56 
57 AVFILTER_DEFINE_CLASS(astats);
58 
60 {
63  static const enum AVSampleFormat sample_fmts[] = {
66  };
67  int ret;
68 
69  layouts = ff_all_channel_layouts();
70  if (!layouts)
71  return AVERROR(ENOMEM);
72  ret = ff_set_common_channel_layouts(ctx, layouts);
73  if (ret < 0)
74  return ret;
75 
76  formats = ff_make_format_list(sample_fmts);
77  if (!formats)
78  return AVERROR(ENOMEM);
79  ret = ff_set_common_formats(ctx, formats);
80  if (ret < 0)
81  return ret;
82 
83  formats = ff_all_samplerates();
84  if (!formats)
85  return AVERROR(ENOMEM);
86  return ff_set_common_samplerates(ctx, formats);
87 }
88 
89 static int config_output(AVFilterLink *outlink)
90 {
91  AudioStatsContext *s = outlink->src->priv;
92  int c;
93 
94  s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
95  if (!s->chstats)
96  return AVERROR(ENOMEM);
97  s->nb_channels = outlink->channels;
98  s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
99  s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5;
100 
101  for (c = 0; c < s->nb_channels; c++) {
102  ChannelStats *p = &s->chstats[c];
103 
104  p->min = p->min_sigma_x2 = DBL_MAX;
105  p->max = p->max_sigma_x2 = DBL_MIN;
106  }
107 
108  return 0;
109 }
110 
111 static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d)
112 {
113  if (d < p->min) {
114  p->min = d;
115  p->min_run = 1;
116  p->min_runs = 0;
117  p->min_count = 1;
118  } else if (d == p->min) {
119  p->min_count++;
120  p->min_run = d == p->last ? p->min_run + 1 : 1;
121  } else if (p->last == p->min) {
122  p->min_runs += p->min_run * p->min_run;
123  }
124 
125  if (d > p->max) {
126  p->max = d;
127  p->max_run = 1;
128  p->max_runs = 0;
129  p->max_count = 1;
130  } else if (d == p->max) {
131  p->max_count++;
132  p->max_run = d == p->last ? p->max_run + 1 : 1;
133  } else if (p->last == p->max) {
134  p->max_runs += p->max_run * p->max_run;
135  }
136 
137  p->sigma_x += d;
138  p->sigma_x2 += d * d;
139  p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * d * d;
140  p->last = d;
141 
142  if (p->nb_samples >= s->tc_samples) {
145  }
146  p->nb_samples++;
147 }
148 
149 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
150 {
151  AudioStatsContext *s = inlink->dst->priv;
152  const int channels = s->nb_channels;
153  const double *src;
154  int i, c;
155 
156  switch (inlink->format) {
157  case AV_SAMPLE_FMT_DBLP:
158  for (c = 0; c < channels; c++) {
159  ChannelStats *p = &s->chstats[c];
160  src = (const double *)buf->extended_data[c];
161 
162  for (i = 0; i < buf->nb_samples; i++, src++)
163  update_stat(s, p, *src);
164  }
165  break;
166  case AV_SAMPLE_FMT_DBL:
167  src = (const double *)buf->extended_data[0];
168 
169  for (i = 0; i < buf->nb_samples; i++) {
170  for (c = 0; c < channels; c++, src++)
171  update_stat(s, &s->chstats[c], *src);
172  }
173  break;
174  }
175 
176  return ff_filter_frame(inlink->dst->outputs[0], buf);
177 }
178 
179 #define LINEAR_TO_DB(x) (log10(x) * 20)
180 
181 static void print_stats(AVFilterContext *ctx)
182 {
183  AudioStatsContext *s = ctx->priv;
184  uint64_t min_count = 0, max_count = 0, nb_samples = 0;
185  double min_runs = 0, max_runs = 0,
186  min = DBL_MAX, max = DBL_MIN,
187  max_sigma_x = 0,
188  sigma_x = 0,
189  sigma_x2 = 0,
190  min_sigma_x2 = DBL_MAX,
191  max_sigma_x2 = DBL_MIN;
192  int c;
193 
194  for (c = 0; c < s->nb_channels; c++) {
195  ChannelStats *p = &s->chstats[c];
196 
197  if (p->nb_samples < s->tc_samples)
198  p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
199 
200  min = FFMIN(min, p->min);
201  max = FFMAX(max, p->max);
202  min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
203  max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
204  sigma_x += p->sigma_x;
205  sigma_x2 += p->sigma_x2;
206  min_count += p->min_count;
207  max_count += p->max_count;
208  min_runs += p->min_runs;
209  max_runs += p->max_runs;
210  nb_samples += p->nb_samples;
211  if (fabs(p->sigma_x) > fabs(max_sigma_x))
212  max_sigma_x = p->sigma_x;
213 
214  av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
215  av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
216  av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
217  av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
218  av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->min, p->max)));
219  av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
220  av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
221  if (p->min_sigma_x2 != 1)
222  av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
223  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);
224  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)));
225  av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
226  }
227 
228  av_log(ctx, AV_LOG_INFO, "Overall\n");
229  av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
230  av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
231  av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
232  av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-min, max)));
233  av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
234  av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
235  if (min_sigma_x2 != 1)
236  av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
237  av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
238  av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
239  av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
240 }
241 
242 static av_cold void uninit(AVFilterContext *ctx)
243 {
244  AudioStatsContext *s = ctx->priv;
245 
246  if (s->nb_channels)
247  print_stats(ctx);
248  av_freep(&s->chstats);
249 }
250 
251 static const AVFilterPad astats_inputs[] = {
252  {
253  .name = "default",
254  .type = AVMEDIA_TYPE_AUDIO,
255  .filter_frame = filter_frame,
256  },
257  { NULL }
258 };
259 
260 static const AVFilterPad astats_outputs[] = {
261  {
262  .name = "default",
263  .type = AVMEDIA_TYPE_AUDIO,
264  .config_props = config_output,
265  },
266  { NULL }
267 };
268 
270  .name = "astats",
271  .description = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
272  .query_formats = query_formats,
273  .priv_size = sizeof(AudioStatsContext),
274  .priv_class = &astats_class,
275  .uninit = uninit,
276  .inputs = astats_inputs,
277  .outputs = astats_outputs,
278 };
#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 * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVOption.
Definition: opt.h:255
static int query_formats(AVFilterContext *ctx)
Definition: af_astats.c:59
AVFilter ff_af_astats
Definition: af_astats.c:269
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
static void update_stat(AudioStatsContext *s, ChannelStats *p, double d)
Definition: af_astats.c:111
Main libavfilter public API header.
#define OFFSET(x)
Definition: af_astats.c:49
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
#define av_cold
Definition: attributes.h:74
AVOptions.
static const AVFilterPad astats_inputs[]
Definition: af_astats.c:251
#define LINEAR_TO_DB(x)
Definition: af_astats.c:179
#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:149
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_astats.c:242
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:181
#define FFMIN(a, b)
Definition: common.h:66
double min_sigma_x2
Definition: af_astats.c:32
ret
Definition: avfilter.c:974
double max_runs
Definition: af_astats.c:35
static int config_output(AVFilterLink *outlink)
Definition: af_astats.c:89
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:52
#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
void * buf
Definition: avisynth_c.h:553
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 FLAGS
Definition: af_astats.c:50
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]
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:260
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