FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_drmeter.c
Date: 2024-03-29 11:55:30
Exec Total Coverage
Lines: 0 79 0.0%
Functions: 0 6 0.0%
Branches: 0 37 0.0%

Line Branch Exec Source
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 #define BINS 32768
30
31 typedef struct ChannelStats {
32 uint64_t nb_samples;
33 uint64_t blknum;
34 float peak;
35 float sum;
36 uint32_t peaks[BINS+1];
37 uint32_t rms[BINS+1];
38 } ChannelStats;
39
40 typedef struct DRMeterContext {
41 const AVClass *class;
42 ChannelStats *chstats;
43 int nb_channels;
44 int64_t tc_samples;
45 double time_constant;
46 } DRMeterContext;
47
48 #define OFFSET(x) offsetof(DRMeterContext, x)
49 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50
51 static const AVOption drmeter_options[] = {
52 { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=3}, .01, 10, FLAGS },
53 { NULL }
54 };
55
56 AVFILTER_DEFINE_CLASS(drmeter);
57
58 static int config_output(AVFilterLink *outlink)
59 {
60 DRMeterContext *s = outlink->src->priv;
61
62 s->chstats = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->chstats));
63 if (!s->chstats)
64 return AVERROR(ENOMEM);
65 s->nb_channels = outlink->ch_layout.nb_channels;
66 s->tc_samples = lrint(s->time_constant * outlink->sample_rate);
67
68 return 0;
69 }
70
71 static void finish_block(ChannelStats *p)
72 {
73 int peak_bin, rms_bin;
74 float peak, rms;
75
76 rms = sqrtf(2.f * p->sum / p->nb_samples);
77 peak = p->peak;
78 rms_bin = av_clip(lrintf(rms * BINS), 0, BINS);
79 peak_bin = av_clip(lrintf(peak * BINS), 0, BINS);
80 p->rms[rms_bin]++;
81 p->peaks[peak_bin]++;
82
83 p->peak = 0;
84 p->sum = 0;
85 p->nb_samples = 0;
86 p->blknum++;
87 }
88
89 static void update_stat(DRMeterContext *s, ChannelStats *p, float sample)
90 {
91 p->peak = fmaxf(fabsf(sample), p->peak);
92 p->sum += sample * sample;
93 p->nb_samples++;
94 if (p->nb_samples >= s->tc_samples)
95 finish_block(p);
96 }
97
98 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
99 {
100 DRMeterContext *s = inlink->dst->priv;
101 const int channels = s->nb_channels;
102
103 switch (inlink->format) {
104 case AV_SAMPLE_FMT_FLTP:
105 for (int c = 0; c < channels; c++) {
106 ChannelStats *p = &s->chstats[c];
107 const float *src = (const float *)buf->extended_data[c];
108
109 for (int i = 0; i < buf->nb_samples; i++, src++)
110 update_stat(s, p, *src);
111 }
112 break;
113 case AV_SAMPLE_FMT_FLT: {
114 const float *src = (const float *)buf->extended_data[0];
115
116 for (int i = 0; i < buf->nb_samples; i++) {
117 for (int c = 0; c < channels; c++, src++)
118 update_stat(s, &s->chstats[c], *src);
119 }}
120 break;
121 }
122
123 return ff_filter_frame(inlink->dst->outputs[0], buf);
124 }
125
126 #define SQR(a) ((a)*(a))
127
128 static void print_stats(AVFilterContext *ctx)
129 {
130 DRMeterContext *s = ctx->priv;
131 float dr = 0.f;
132
133 for (int ch = 0; ch < s->nb_channels; ch++) {
134 ChannelStats *p = &s->chstats[ch];
135 float chdr, secondpeak, rmssum = 0.f;
136 int first = 0, last = lrintf(0.2f * p->blknum);
137 int peak_bin = BINS;
138
139 if (!p->nb_samples) {
140 av_log(ctx, AV_LOG_INFO, "No data, dynamic range not meassurable\n");
141 return;
142 }
143
144 if (p->nb_samples)
145 finish_block(p);
146
147 for (int i = BINS; i >= 0; i--) {
148 if (p->peaks[i]) {
149 if (first || p->peaks[i] > 1) {
150 peak_bin = i;
151 break;
152 }
153 first = 1;
154 }
155 }
156
157 secondpeak = peak_bin / (float)BINS;
158
159 for (int64_t i = BINS, j = 0; i >= 0 && j < last; i--) {
160 if (p->rms[i]) {
161 rmssum += SQR(i / (float)BINS) * p->rms[i];
162 j += p->rms[i];
163 }
164 }
165
166 chdr = 20.f * log10f(secondpeak / sqrtf(rmssum / (float)last));
167 dr += chdr;
168 av_log(ctx, AV_LOG_INFO, "Channel %d: DR: %g\n", ch + 1, chdr);
169 }
170
171 av_log(ctx, AV_LOG_INFO, "Overall DR: %g\n", dr / s->nb_channels);
172 }
173
174 static av_cold void uninit(AVFilterContext *ctx)
175 {
176 DRMeterContext *s = ctx->priv;
177
178 if (s->nb_channels)
179 print_stats(ctx);
180 av_freep(&s->chstats);
181 }
182
183 static const AVFilterPad drmeter_inputs[] = {
184 {
185 .name = "default",
186 .type = AVMEDIA_TYPE_AUDIO,
187 .filter_frame = filter_frame,
188 },
189 };
190
191 static const AVFilterPad drmeter_outputs[] = {
192 {
193 .name = "default",
194 .type = AVMEDIA_TYPE_AUDIO,
195 .config_props = config_output,
196 },
197 };
198
199 const AVFilter ff_af_drmeter = {
200 .name = "drmeter",
201 .description = NULL_IF_CONFIG_SMALL("Measure audio dynamic range."),
202 .priv_size = sizeof(DRMeterContext),
203 .priv_class = &drmeter_class,
204 .uninit = uninit,
205 .flags = AVFILTER_FLAG_METADATA_ONLY,
206 FILTER_INPUTS(drmeter_inputs),
207 FILTER_OUTPUTS(drmeter_outputs),
208 FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_FLT),
209 };
210