2 * Copyright (c) 2018 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/ffmath.h"
24 #include "libavutil/opt.h"
29 typedef struct ChannelStats {
34 uint32_t peaks[10001];
38 typedef struct DRMeterContext {
40 ChannelStats *chstats;
46 #define OFFSET(x) offsetof(DRMeterContext, x)
47 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
49 static const AVOption drmeter_options[] = {
50 { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=3}, .01, 10, FLAGS },
54 AVFILTER_DEFINE_CLASS(drmeter);
56 static int query_formats(AVFilterContext *ctx)
58 AVFilterFormats *formats;
59 AVFilterChannelLayouts *layouts;
60 static const enum AVSampleFormat sample_fmts[] = {
61 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_FLT,
66 layouts = ff_all_channel_counts();
68 return AVERROR(ENOMEM);
69 ret = ff_set_common_channel_layouts(ctx, layouts);
73 formats = ff_make_format_list(sample_fmts);
75 return AVERROR(ENOMEM);
76 ret = ff_set_common_formats(ctx, formats);
80 formats = ff_all_samplerates();
82 return AVERROR(ENOMEM);
83 return ff_set_common_samplerates(ctx, formats);
86 static int config_output(AVFilterLink *outlink)
88 DRMeterContext *s = outlink->src->priv;
90 s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
92 return AVERROR(ENOMEM);
93 s->nb_channels = outlink->channels;
94 s->tc_samples = s->time_constant * outlink->sample_rate + .5;
99 static void finish_block(ChannelStats *p)
101 int peak_bin, rms_bin;
104 rms = sqrt(2 * p->sum / p->nb_samples);
106 rms_bin = av_clip(rms * 10000, 0, 10000);
107 peak_bin = av_clip(peak * 10000, 0, 10000);
109 p->peaks[peak_bin]++;
117 static void update_stat(DRMeterContext *s, ChannelStats *p, float sample)
119 if (p->nb_samples >= s->tc_samples) {
123 p->peak = FFMAX(FFABS(sample), p->peak);
124 p->sum += sample * sample;
128 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
130 DRMeterContext *s = inlink->dst->priv;
131 const int channels = s->nb_channels;
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];
140 for (i = 0; i < buf->nb_samples; i++, src++)
141 update_stat(s, p, *src);
144 case AV_SAMPLE_FMT_FLT: {
145 const float *src = (const float *)buf->extended_data[0];
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);
154 return ff_filter_frame(inlink->dst->outputs[0], buf);
157 #define SQR(a) ((a)*(a))
159 static void print_stats(AVFilterContext *ctx)
161 DRMeterContext *s = ctx->priv;
165 for (ch = 0; ch < s->nb_channels; ch++) {
166 ChannelStats *p = &s->chstats[ch];
167 float chdr, secondpeak, rmssum = 0;
172 for (i = 0; i <= 10000; i++) {
173 if (p->peaks[10000 - i]) {
180 secondpeak = (10000 - i) / 10000.;
182 for (i = 10000, j = 0; i >= 0 && j < 0.2 * p->blknum; i--) {
184 rmssum += SQR(i / 10000.) * p->rms[i];
189 chdr = 20 * log10(secondpeak / sqrt(rmssum / (0.2 * p->blknum)));
191 av_log(ctx, AV_LOG_INFO, "Channel %d: DR: %.1f\n", ch + 1, chdr);
194 av_log(ctx, AV_LOG_INFO, "Overall DR: %.1f\n", dr / s->nb_channels);
197 static av_cold void uninit(AVFilterContext *ctx)
199 DRMeterContext *s = ctx->priv;
203 av_freep(&s->chstats);
206 static const AVFilterPad drmeter_inputs[] = {
209 .type = AVMEDIA_TYPE_AUDIO,
210 .filter_frame = filter_frame,
215 static const AVFilterPad drmeter_outputs[] = {
218 .type = AVMEDIA_TYPE_AUDIO,
219 .config_props = config_output,
224 const AVFilter ff_af_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,
231 .inputs = drmeter_inputs,
232 .outputs = drmeter_outputs,