]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_drmeter.c
avfilter: Constify all AVFilters
[ffmpeg] / libavfilter / af_drmeter.c
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;
40     ChannelStats *chstats;
41     int nb_channels;
42     uint64_t tc_samples;
43     double time_constant;
44 } DRMeterContext;
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 query_formats(AVFilterContext *ctx)
57 {
58     AVFilterFormats *formats;
59     AVFilterChannelLayouts *layouts;
60     static const enum AVSampleFormat sample_fmts[] = {
61         AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_FLT,
62         AV_SAMPLE_FMT_NONE
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
117 static void update_stat(DRMeterContext *s, ChannelStats *p, float sample)
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
159 static void print_stats(AVFilterContext *ctx)
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
197 static av_cold void uninit(AVFilterContext *ctx)
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
224 const AVFilter ff_af_drmeter = {
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 };