FFmpeg
af_drmeter.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <float.h>
22 
23 #include "libavutil/ffmath.h"
24 #include "libavutil/opt.h"
25 #include "audio.h"
26 #include "avfilter.h"
27 #include "internal.h"
28 
29 typedef struct ChannelStats {
30  uint64_t nb_samples;
31  uint64_t blknum;
32  float peak;
33  float sum;
34  uint32_t peaks[10001];
35  uint32_t rms[10001];
36 } ChannelStats;
37 
38 typedef struct DRMeterContext {
39  const AVClass *class;
42  uint64_t tc_samples;
43  double time_constant;
45 
46 #define OFFSET(x) offsetof(DRMeterContext, x)
47 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
48 
49 static const AVOption drmeter_options[] = {
50  { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=3}, .01, 10, FLAGS },
51  { NULL }
52 };
53 
54 AVFILTER_DEFINE_CLASS(drmeter);
55 
56 static int config_output(AVFilterLink *outlink)
57 {
58  DRMeterContext *s = outlink->src->priv;
59 
60  s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
61  if (!s->chstats)
62  return AVERROR(ENOMEM);
63  s->nb_channels = outlink->channels;
64  s->tc_samples = s->time_constant * outlink->sample_rate + .5;
65 
66  return 0;
67 }
68 
69 static void finish_block(ChannelStats *p)
70 {
71  int peak_bin, rms_bin;
72  float peak, rms;
73 
74  rms = sqrt(2 * p->sum / p->nb_samples);
75  peak = p->peak;
76  rms_bin = av_clip(rms * 10000, 0, 10000);
77  peak_bin = av_clip(peak * 10000, 0, 10000);
78  p->rms[rms_bin]++;
79  p->peaks[peak_bin]++;
80 
81  p->peak = 0;
82  p->sum = 0;
83  p->nb_samples = 0;
84  p->blknum++;
85 }
86 
88 {
89  if (p->nb_samples >= s->tc_samples) {
90  finish_block(p);
91  }
92 
93  p->peak = FFMAX(FFABS(sample), p->peak);
94  p->sum += sample * sample;
95  p->nb_samples++;
96 }
97 
99 {
100  DRMeterContext *s = inlink->dst->priv;
101  const int channels = s->nb_channels;
102  int i, c;
103 
104  switch (inlink->format) {
105  case AV_SAMPLE_FMT_FLTP:
106  for (c = 0; c < channels; c++) {
107  ChannelStats *p = &s->chstats[c];
108  const float *src = (const float *)buf->extended_data[c];
109 
110  for (i = 0; i < buf->nb_samples; i++, src++)
111  update_stat(s, p, *src);
112  }
113  break;
114  case AV_SAMPLE_FMT_FLT: {
115  const float *src = (const float *)buf->extended_data[0];
116 
117  for (i = 0; i < buf->nb_samples; i++) {
118  for (c = 0; c < channels; c++, src++)
119  update_stat(s, &s->chstats[c], *src);
120  }}
121  break;
122  }
123 
124  return ff_filter_frame(inlink->dst->outputs[0], buf);
125 }
126 
127 #define SQR(a) ((a)*(a))
128 
130 {
131  DRMeterContext *s = ctx->priv;
132  float dr = 0;
133  int ch;
134 
135  for (ch = 0; ch < s->nb_channels; ch++) {
136  ChannelStats *p = &s->chstats[ch];
137  float chdr, secondpeak, rmssum = 0;
138  int i, j, first = 0;
139 
140  if (!p->nb_samples) {
141  av_log(ctx, AV_LOG_INFO, "No data, dynamic range not meassurable\n");
142  return;
143  }
144 
145  finish_block(p);
146 
147  for (i = 0; i <= 10000; i++) {
148  if (p->peaks[10000 - i]) {
149  if (first)
150  break;
151  first = 1;
152  }
153  }
154 
155  secondpeak = (10000 - i) / 10000.;
156 
157  for (i = 10000, j = 0; i >= 0 && j < 0.2 * p->blknum; i--) {
158  if (p->rms[i]) {
159  rmssum += SQR(i / 10000.) * p->rms[i];
160  j += p->rms[i];
161  }
162  }
163 
164  chdr = 20 * log10(secondpeak / sqrt(rmssum / (0.2 * p->blknum)));
165  dr += chdr;
166  av_log(ctx, AV_LOG_INFO, "Channel %d: DR: %.1f\n", ch + 1, chdr);
167  }
168 
169  av_log(ctx, AV_LOG_INFO, "Overall DR: %.1f\n", dr / s->nb_channels);
170 }
171 
173 {
174  DRMeterContext *s = ctx->priv;
175 
176  if (s->nb_channels)
177  print_stats(ctx);
178  av_freep(&s->chstats);
179 }
180 
181 static const AVFilterPad drmeter_inputs[] = {
182  {
183  .name = "default",
184  .type = AVMEDIA_TYPE_AUDIO,
185  .filter_frame = filter_frame,
186  },
187 };
188 
189 static const AVFilterPad drmeter_outputs[] = {
190  {
191  .name = "default",
192  .type = AVMEDIA_TYPE_AUDIO,
193  .config_props = config_output,
194  },
195 };
196 
198  .name = "drmeter",
199  .description = NULL_IF_CONFIG_SMALL("Measure audio dynamic range."),
200  .priv_size = sizeof(DRMeterContext),
201  .priv_class = &drmeter_class,
202  .uninit = uninit,
206 };
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_drmeter.c:172
av_clip
#define av_clip
Definition: common.h:96
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
finish_block
static void finish_block(ChannelStats *p)
Definition: af_drmeter.c:69
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
DRMeterContext::tc_samples
uint64_t tc_samples
Definition: af_drmeter.c:42
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
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Definition: af_drmeter.c:98
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
AVOption
AVOption.
Definition: opt.h:247
float.h
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:153
DRMeterContext::time_constant
double time_constant
Definition: af_drmeter.c:43
ChannelStats::rms
uint32_t rms[10001]
Definition: af_drmeter.c:35
DRMeterContext::chstats
ChannelStats * chstats
Definition: af_drmeter.c:40
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(drmeter)
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:401
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
update_stat
static void update_stat(DRMeterContext *s, ChannelStats *p, float sample)
Definition: af_drmeter.c:87
first
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But first
Definition: rate_distortion.txt:12
av_cold
#define av_cold
Definition: attributes.h:90
print_stats
static void print_stats(AVFilterContext *ctx)
Definition: af_drmeter.c:129
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:226
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ChannelStats::peaks
uint32_t peaks[10001]
Definition: af_drmeter.c:34
ctx
AVFormatContext * ctx
Definition: movenc.c:48
channels
channels
Definition: aptx.h:33
SQR
#define SQR(a)
Definition: af_drmeter.c:127
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:65
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
src
#define src
Definition: vp8dsp.c:255
config_output
static int config_output(AVFilterLink *outlink)
Definition: af_drmeter.c:56
DRMeterContext
Definition: af_drmeter.c:38
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
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
drmeter_options
static const AVOption drmeter_options[]
Definition: af_drmeter.c:49
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
ff_af_drmeter
const AVFilter ff_af_drmeter
Definition: af_drmeter.c:197
sample
#define sample
Definition: flacdsp_template.c:44
ChannelStats::nb_samples
uint64_t nb_samples
Definition: af_astats.c:81
FLAGS
#define FLAGS
Definition: af_drmeter.c:47
OFFSET
#define OFFSET(x)
Definition: af_drmeter.c:46
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
internal.h
DRMeterContext::nb_channels
int nb_channels
Definition: af_drmeter.c:41
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:369
ChannelStats
Definition: af_astats.c:64
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:350
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
AVFilter
Filter definition.
Definition: avfilter.h:149
avfilter.h
ffmath.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:386
audio.h
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
ChannelStats::peak
float peak
Definition: af_drmeter.c:32
ChannelStats::blknum
uint64_t blknum
Definition: af_drmeter.c:31
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
drmeter_outputs
static const AVFilterPad drmeter_outputs[]
Definition: af_drmeter.c:189
ChannelStats::sum
float sum
Definition: af_drmeter.c:33
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:63
drmeter_inputs
static const AVFilterPad drmeter_inputs[]
Definition: af_drmeter.c:181
FILTER_SAMPLEFMTS
#define FILTER_SAMPLEFMTS(...)
Definition: internal.h:179