FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
57 {
60  static const enum AVSampleFormat sample_fmts[] = {
63  };
64  int ret;
65 
66  layouts = ff_all_channel_counts();
67  if (!layouts)
68  return AVERROR(ENOMEM);
69  ret = ff_set_common_channel_layouts(ctx, layouts);
70  if (ret < 0)
71  return ret;
72 
73  formats = ff_make_format_list(sample_fmts);
74  if (!formats)
75  return AVERROR(ENOMEM);
76  ret = ff_set_common_formats(ctx, formats);
77  if (ret < 0)
78  return ret;
79 
80  formats = ff_all_samplerates();
81  if (!formats)
82  return AVERROR(ENOMEM);
83  return ff_set_common_samplerates(ctx, formats);
84 }
85 
86 static int config_output(AVFilterLink *outlink)
87 {
88  DRMeterContext *s = outlink->src->priv;
89 
90  s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
91  if (!s->chstats)
92  return AVERROR(ENOMEM);
93  s->nb_channels = outlink->channels;
94  s->tc_samples = s->time_constant * outlink->sample_rate + .5;
95 
96  return 0;
97 }
98 
99 static void finish_block(ChannelStats *p)
100 {
101  int peak_bin, rms_bin;
102  float peak, rms;
103 
104  rms = sqrt(2 * p->sum / p->nb_samples);
105  peak = p->peak;
106  rms_bin = av_clip(rms * 10000, 0, 10000);
107  peak_bin = av_clip(peak * 10000, 0, 10000);
108  p->rms[rms_bin]++;
109  p->peaks[peak_bin]++;
110 
111  p->peak = 0;
112  p->sum = 0;
113  p->nb_samples = 0;
114  p->blknum++;
115 }
116 
118 {
119  if (p->nb_samples >= s->tc_samples) {
120  finish_block(p);
121  }
122 
123  p->peak = FFMAX(FFABS(sample), p->peak);
124  p->sum += sample * sample;
125  p->nb_samples++;
126 }
127 
128 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
129 {
130  DRMeterContext *s = inlink->dst->priv;
131  const int channels = s->nb_channels;
132  int i, c;
133 
134  switch (inlink->format) {
135  case AV_SAMPLE_FMT_FLTP:
136  for (c = 0; c < channels; c++) {
137  ChannelStats *p = &s->chstats[c];
138  const float *src = (const float *)buf->extended_data[c];
139 
140  for (i = 0; i < buf->nb_samples; i++, src++)
141  update_stat(s, p, *src);
142  }
143  break;
144  case AV_SAMPLE_FMT_FLT: {
145  const float *src = (const float *)buf->extended_data[0];
146 
147  for (i = 0; i < buf->nb_samples; i++) {
148  for (c = 0; c < channels; c++, src++)
149  update_stat(s, &s->chstats[c], *src);
150  }}
151  break;
152  }
153 
154  return ff_filter_frame(inlink->dst->outputs[0], buf);
155 }
156 
157 #define SQR(a) ((a)*(a))
158 
160 {
161  DRMeterContext *s = ctx->priv;
162  float dr = 0;
163  int ch;
164 
165  for (ch = 0; ch < s->nb_channels; ch++) {
166  ChannelStats *p = &s->chstats[ch];
167  float chdr, secondpeak, rmssum = 0;
168  int i, j, first = 0;
169 
170  finish_block(p);
171 
172  for (i = 0; i <= 10000; i++) {
173  if (p->peaks[10000 - i]) {
174  if (first)
175  break;
176  first = 1;
177  }
178  }
179 
180  secondpeak = (10000 - i) / 10000.;
181 
182  for (i = 10000, j = 0; i >= 0 && j < 0.2 * p->blknum; i--) {
183  if (p->rms[i]) {
184  rmssum += SQR(i / 10000.) * p->rms[i];
185  j += p->rms[i];
186  }
187  }
188 
189  chdr = 20 * log10(secondpeak / sqrt(rmssum / (0.2 * p->blknum)));
190  dr += chdr;
191  av_log(ctx, AV_LOG_INFO, "Channel %d: DR: %.1f\n", ch + 1, chdr);
192  }
193 
194  av_log(ctx, AV_LOG_INFO, "Overall DR: %.1f\n", dr / s->nb_channels);
195 }
196 
198 {
199  DRMeterContext *s = ctx->priv;
200 
201  if (s->nb_channels)
202  print_stats(ctx);
203  av_freep(&s->chstats);
204 }
205 
206 static const AVFilterPad drmeter_inputs[] = {
207  {
208  .name = "default",
209  .type = AVMEDIA_TYPE_AUDIO,
210  .filter_frame = filter_frame,
211  },
212  { NULL }
213 };
214 
215 static const AVFilterPad drmeter_outputs[] = {
216  {
217  .name = "default",
218  .type = AVMEDIA_TYPE_AUDIO,
219  .config_props = config_output,
220  },
221  { NULL }
222 };
223 
225  .name = "drmeter",
226  .description = NULL_IF_CONFIG_SMALL("Measure audio dynamic range."),
227  .query_formats = query_formats,
228  .priv_size = sizeof(DRMeterContext),
229  .priv_class = &drmeter_class,
230  .uninit = uninit,
231  .inputs = drmeter_inputs,
232  .outputs = drmeter_outputs,
233 };
float, planar
Definition: samplefmt.h:69
static void update_stat(DRMeterContext *s, ChannelStats *p, float sample)
Definition: af_drmeter.c:117
#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
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
static int config_output(AVFilterLink *outlink)
Definition: af_drmeter.c:86
AVOption.
Definition: opt.h:246
Main libavfilter public API header.
channels
Definition: aptx.c:30
AVFilter ff_af_drmeter
Definition: af_drmeter.c:224
#define OFFSET(x)
Definition: af_drmeter.c:46
#define src
Definition: vp8dsp.c:254
#define sample
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
static void print_stats(AVFilterContext *ctx)
Definition: af_drmeter.c:159
const char * name
Pad name.
Definition: internal.h:60
#define SQR(a)
Definition: af_drmeter.c:157
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
#define av_cold
Definition: attributes.h:82
static void finish_block(ChannelStats *p)
Definition: af_drmeter.c:99
AVOptions.
uint64_t tc_samples
Definition: af_drmeter.c:42
double time_constant
Definition: af_drmeter.c:43
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
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
#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
#define FFMAX(a, b)
Definition: common.h:94
AVFormatContext * ctx
Definition: movenc.c:48
AVFILTER_DEFINE_CLASS(drmeter)
#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
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
static int query_formats(AVFilterContext *ctx)
Definition: af_drmeter.c:56
A list of supported channel layouts.
Definition: formats.h:85
uint32_t rms[10001]
Definition: af_drmeter.c:35
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
void * buf
Definition: avisynth_c.h:690
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
static const AVOption drmeter_options[]
Definition: af_drmeter.c:49
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
#define FLAGS
Definition: af_drmeter.c:47
internal math functions header
ChannelStats * chstats
Definition: af_drmeter.c:40
uint64_t nb_samples
Definition: af_astats.c:45
static double c[64]
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Definition: af_drmeter.c:128
A list of supported formats for one end of a filter link.
Definition: formats.h:64
static const AVFilterPad drmeter_inputs[]
Definition: af_drmeter.c:206
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_drmeter.c:197
An instance of a filter.
Definition: avfilter.h:338
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define av_freep(p)
formats
Definition: signature.h:48
static const AVFilterPad drmeter_outputs[]
Definition: af_drmeter.c:215
float peak
Definition: af_drmeter.c:32
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
uint64_t blknum
Definition: af_drmeter.c:31
uint32_t peaks[10001]
Definition: af_drmeter.c:34
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:292
for(j=16;j >0;--j)
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_F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AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56