FFmpeg
Loading...
Searching...
No Matches
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/mem.h"
25#include "libavutil/opt.h"
26#include "audio.h"
27#include "avfilter.h"
28#include "filters.h"
29
30#define BINS 32768
31
32typedef struct ChannelStats {
33 uint64_t nb_samples;
34 uint64_t blknum;
35 float peak;
36 float sum;
37 uint32_t peaks[BINS+1];
38 uint32_t rms[BINS+1];
40
48
49#define OFFSET(x) offsetof(DRMeterContext, x)
50#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51
52static const AVOption drmeter_options[] = {
53 { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=3}, .01, 10, FLAGS },
54 { NULL }
55};
56
58
59static int config_output(AVFilterLink *outlink)
60{
61 DRMeterContext *s = outlink->src->priv;
62
63 s->chstats = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->chstats));
64 if (!s->chstats)
65 return AVERROR(ENOMEM);
66 s->nb_channels = outlink->ch_layout.nb_channels;
67 s->tc_samples = lrint(s->time_constant * outlink->sample_rate);
68
69 return 0;
70}
71
73{
74 int peak_bin, rms_bin;
75 float peak, rms;
76
77 rms = sqrtf(2.f * p->sum / p->nb_samples);
78 peak = p->peak;
79 rms_bin = av_clip(lrintf(rms * BINS), 0, BINS);
80 peak_bin = av_clip(lrintf(peak * BINS), 0, BINS);
81 p->rms[rms_bin]++;
82 p->peaks[peak_bin]++;
83
84 p->peak = 0;
85 p->sum = 0;
86 p->nb_samples = 0;
87 p->blknum++;
88}
89
91{
92 p->peak = fmaxf(fabsf(sample), p->peak);
93 p->sum += sample * sample;
94 p->nb_samples++;
95 if (p->nb_samples >= s->tc_samples)
96 finish_block(p);
97}
98
99static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
100{
101 DRMeterContext *s = inlink->dst->priv;
102 const int channels = s->nb_channels;
103
104 switch (inlink->format) {
106 for (int c = 0; c < channels; c++) {
107 ChannelStats *p = &s->chstats[c];
108 const float *src = (const float *)buf->extended_data[c];
109
110 for (int 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 (int i = 0; i < buf->nb_samples; i++) {
118 for (int 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.f;
133
134 for (int ch = 0; ch < s->nb_channels; ch++) {
135 ChannelStats *p = &s->chstats[ch];
136 float chdr, secondpeak, rmssum = 0.f;
137 int first = 0, last = lrintf(0.2f * p->blknum);
138 int peak_bin = BINS;
139
140 if (!p->nb_samples) {
141 av_log(ctx, AV_LOG_INFO, "No data, dynamic range not measurable\n");
142 return;
143 }
144
145 if (p->nb_samples)
146 finish_block(p);
147
148 for (int i = BINS; i >= 0; i--) {
149 if (p->peaks[i]) {
150 if (first || p->peaks[i] > 1) {
151 peak_bin = i;
152 break;
153 }
154 first = 1;
155 }
156 }
157
158 secondpeak = peak_bin / (float)BINS;
159
160 for (int64_t i = BINS, j = 0; i >= 0 && j < last; i--) {
161 if (p->rms[i]) {
162 rmssum += SQR(i / (float)BINS) * p->rms[i];
163 j += p->rms[i];
164 }
165 }
166
167 chdr = 20.f * log10f(secondpeak / sqrtf(rmssum / (float)last));
168 dr += chdr;
169 av_log(ctx, AV_LOG_INFO, "Channel %d: DR: %g\n", ch + 1, chdr);
170 }
171
172 av_log(ctx, AV_LOG_INFO, "Overall DR: %g\n", dr / s->nb_channels);
173}
174
176{
177 DRMeterContext *s = ctx->priv;
178
179 if (s->nb_channels)
181 av_freep(&s->chstats);
182}
183
184static const AVFilterPad drmeter_inputs[] = {
185 {
186 .name = "default",
187 .type = AVMEDIA_TYPE_AUDIO,
188 .filter_frame = filter_frame,
189 },
190};
191
192static const AVFilterPad drmeter_outputs[] = {
193 {
194 .name = "default",
195 .type = AVMEDIA_TYPE_AUDIO,
196 .config_props = config_output,
197 },
198};
199
201 .p.name = "drmeter",
202 .p.description = NULL_IF_CONFIG_SMALL("Measure audio dynamic range."),
203 .p.priv_class = &drmeter_class,
205 .priv_size = sizeof(DRMeterContext),
206 .uninit = uninit,
210};
static void update_stat(DRMeterContext *s, ChannelStats *p, float sample)
Definition af_drmeter.c:90
static const AVFilterPad drmeter_outputs[]
Definition af_drmeter.c:192
const FFFilter ff_af_drmeter
Definition af_drmeter.c:200
static void finish_block(ChannelStats *p)
Definition af_drmeter.c:72
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Definition af_drmeter.c:99
static const AVFilterPad drmeter_inputs[]
Definition af_drmeter.c:184
static const AVOption drmeter_options[]
Definition af_drmeter.c:52
static av_cold void uninit(AVFilterContext *ctx)
Definition af_drmeter.c:175
#define OFFSET(x)
Definition af_drmeter.c:49
static int config_output(AVFilterLink *outlink)
Definition af_drmeter.c:59
#define SQR(a)
Definition af_drmeter.c:127
#define BINS
Definition af_drmeter.c:30
channels
Definition aptx.h:31
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define av_clip
Definition common.h:100
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static __device__ float sqrtf(float a)
static __device__ float fabsf(float a)
float fmaxf(float, float)
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
internal math functions header
int print_stats
Definition ffmpeg_opt.c:70
#define sample
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition avfilter.h:182
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_INFO
Standard information.
Definition log.h:221
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_SAMPLEFMTS(...)
Definition filters.h:252
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define log10f(x)
Definition libm.h:416
#define lrintf(x)
Definition libm_mips.h:72
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
AVOptions.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:533
AVOption.
Definition opt.h:428
uint64_t nb_samples
Definition af_astats.c:85
uint64_t blknum
Definition af_drmeter.c:34
uint32_t rms[BINS+1]
Definition af_drmeter.c:38
uint32_t peaks[BINS+1]
Definition af_drmeter.c:37
ChannelStats * chstats
Definition af_drmeter.c:43
int64_t tc_samples
Definition af_drmeter.c:45
double time_constant
Definition af_drmeter.c:46
#define lrint
Definition tablegen.h:53
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static AVFormatContext * ctx
Definition movenc.c:49
static double c[64]