2018-03-06 20:47:29 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Paul B Mahol
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <float.h>
|
|
|
|
|
|
|
|
#include "libavutil/ffmath.h"
|
2024-03-25 02:30:37 +02:00
|
|
|
#include "libavutil/mem.h"
|
2018-03-06 20:47:29 +02:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "avfilter.h"
|
2024-08-12 15:29:00 +02:00
|
|
|
#include "filters.h"
|
2018-03-06 20:47:29 +02:00
|
|
|
|
2021-11-18 23:14:11 +02:00
|
|
|
#define BINS 32768
|
2021-11-18 22:33:46 +02:00
|
|
|
|
2018-03-06 20:47:29 +02:00
|
|
|
typedef struct ChannelStats {
|
|
|
|
uint64_t nb_samples;
|
|
|
|
uint64_t blknum;
|
|
|
|
float peak;
|
|
|
|
float sum;
|
2021-11-18 22:33:46 +02:00
|
|
|
uint32_t peaks[BINS+1];
|
|
|
|
uint32_t rms[BINS+1];
|
2018-03-06 20:47:29 +02:00
|
|
|
} ChannelStats;
|
|
|
|
|
|
|
|
typedef struct DRMeterContext {
|
|
|
|
const AVClass *class;
|
|
|
|
ChannelStats *chstats;
|
|
|
|
int nb_channels;
|
2023-06-18 02:10:11 +02:00
|
|
|
int64_t tc_samples;
|
2018-03-06 20:47:29 +02:00
|
|
|
double time_constant;
|
|
|
|
} DRMeterContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(DRMeterContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
|
|
|
|
|
|
|
static const AVOption drmeter_options[] = {
|
|
|
|
{ "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=3}, .01, 10, FLAGS },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(drmeter);
|
|
|
|
|
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
DRMeterContext *s = outlink->src->priv;
|
|
|
|
|
2023-06-18 02:10:11 +02:00
|
|
|
s->chstats = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->chstats));
|
2018-03-06 20:47:29 +02:00
|
|
|
if (!s->chstats)
|
|
|
|
return AVERROR(ENOMEM);
|
2021-08-31 16:03:14 +02:00
|
|
|
s->nb_channels = outlink->ch_layout.nb_channels;
|
2023-06-18 02:10:11 +02:00
|
|
|
s->tc_samples = lrint(s->time_constant * outlink->sample_rate);
|
2018-03-06 20:47:29 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void finish_block(ChannelStats *p)
|
|
|
|
{
|
|
|
|
int peak_bin, rms_bin;
|
|
|
|
float peak, rms;
|
|
|
|
|
2023-06-18 02:10:11 +02:00
|
|
|
rms = sqrtf(2.f * p->sum / p->nb_samples);
|
2018-03-06 20:47:29 +02:00
|
|
|
peak = p->peak;
|
2021-11-18 23:14:11 +02:00
|
|
|
rms_bin = av_clip(lrintf(rms * BINS), 0, BINS);
|
|
|
|
peak_bin = av_clip(lrintf(peak * BINS), 0, BINS);
|
2018-03-06 20:47:29 +02:00
|
|
|
p->rms[rms_bin]++;
|
|
|
|
p->peaks[peak_bin]++;
|
|
|
|
|
|
|
|
p->peak = 0;
|
|
|
|
p->sum = 0;
|
|
|
|
p->nb_samples = 0;
|
|
|
|
p->blknum++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_stat(DRMeterContext *s, ChannelStats *p, float sample)
|
|
|
|
{
|
2023-06-18 02:10:11 +02:00
|
|
|
p->peak = fmaxf(fabsf(sample), p->peak);
|
2018-03-06 20:47:29 +02:00
|
|
|
p->sum += sample * sample;
|
|
|
|
p->nb_samples++;
|
2023-06-18 02:10:11 +02:00
|
|
|
if (p->nb_samples >= s->tc_samples)
|
|
|
|
finish_block(p);
|
2018-03-06 20:47:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
|
|
|
|
{
|
|
|
|
DRMeterContext *s = inlink->dst->priv;
|
|
|
|
const int channels = s->nb_channels;
|
|
|
|
|
|
|
|
switch (inlink->format) {
|
|
|
|
case AV_SAMPLE_FMT_FLTP:
|
2023-06-18 02:10:11 +02:00
|
|
|
for (int c = 0; c < channels; c++) {
|
2018-03-06 20:47:29 +02:00
|
|
|
ChannelStats *p = &s->chstats[c];
|
|
|
|
const float *src = (const float *)buf->extended_data[c];
|
|
|
|
|
2023-06-18 02:10:11 +02:00
|
|
|
for (int i = 0; i < buf->nb_samples; i++, src++)
|
2018-03-06 20:47:29 +02:00
|
|
|
update_stat(s, p, *src);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_FLT: {
|
|
|
|
const float *src = (const float *)buf->extended_data[0];
|
|
|
|
|
2023-06-18 02:10:11 +02:00
|
|
|
for (int i = 0; i < buf->nb_samples; i++) {
|
|
|
|
for (int c = 0; c < channels; c++, src++)
|
2018-03-06 20:47:29 +02:00
|
|
|
update_stat(s, &s->chstats[c], *src);
|
|
|
|
}}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ff_filter_frame(inlink->dst->outputs[0], buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SQR(a) ((a)*(a))
|
|
|
|
|
|
|
|
static void print_stats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
DRMeterContext *s = ctx->priv;
|
2023-06-18 02:10:11 +02:00
|
|
|
float dr = 0.f;
|
2018-03-06 20:47:29 +02:00
|
|
|
|
2023-06-18 02:10:11 +02:00
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++) {
|
2018-03-06 20:47:29 +02:00
|
|
|
ChannelStats *p = &s->chstats[ch];
|
2023-06-18 02:10:11 +02:00
|
|
|
float chdr, secondpeak, rmssum = 0.f;
|
|
|
|
int first = 0, last = lrintf(0.2f * p->blknum);
|
|
|
|
int peak_bin = BINS;
|
2018-03-06 20:47:29 +02:00
|
|
|
|
2021-06-05 20:28:24 +02:00
|
|
|
if (!p->nb_samples) {
|
|
|
|
av_log(ctx, AV_LOG_INFO, "No data, dynamic range not meassurable\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-18 02:10:11 +02:00
|
|
|
if (p->nb_samples)
|
|
|
|
finish_block(p);
|
2018-03-06 20:47:29 +02:00
|
|
|
|
2023-06-18 02:10:11 +02:00
|
|
|
for (int i = BINS; i >= 0; i--) {
|
|
|
|
if (p->peaks[i]) {
|
|
|
|
if (first || p->peaks[i] > 1) {
|
|
|
|
peak_bin = i;
|
2018-03-06 20:47:29 +02:00
|
|
|
break;
|
2023-06-18 02:10:11 +02:00
|
|
|
}
|
2018-03-06 20:47:29 +02:00
|
|
|
first = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-18 02:10:11 +02:00
|
|
|
secondpeak = peak_bin / (float)BINS;
|
2018-03-06 20:47:29 +02:00
|
|
|
|
2023-06-18 02:10:11 +02:00
|
|
|
for (int64_t i = BINS, j = 0; i >= 0 && j < last; i--) {
|
2018-03-06 20:47:29 +02:00
|
|
|
if (p->rms[i]) {
|
2023-06-18 02:10:11 +02:00
|
|
|
rmssum += SQR(i / (float)BINS) * p->rms[i];
|
2018-03-06 20:47:29 +02:00
|
|
|
j += p->rms[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-18 02:10:11 +02:00
|
|
|
chdr = 20.f * log10f(secondpeak / sqrtf(rmssum / (float)last));
|
2018-03-06 20:47:29 +02:00
|
|
|
dr += chdr;
|
2021-11-18 22:33:46 +02:00
|
|
|
av_log(ctx, AV_LOG_INFO, "Channel %d: DR: %g\n", ch + 1, chdr);
|
2018-03-06 20:47:29 +02:00
|
|
|
}
|
|
|
|
|
2021-11-18 22:33:46 +02:00
|
|
|
av_log(ctx, AV_LOG_INFO, "Overall DR: %g\n", dr / s->nb_channels);
|
2018-03-06 20:47:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
DRMeterContext *s = ctx->priv;
|
|
|
|
|
|
|
|
if (s->nb_channels)
|
|
|
|
print_stats(ctx);
|
|
|
|
av_freep(&s->chstats);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad drmeter_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad drmeter_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_output,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_af_drmeter = {
|
2018-03-06 20:47:29 +02:00
|
|
|
.name = "drmeter",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Measure audio dynamic range."),
|
|
|
|
.priv_size = sizeof(DRMeterContext),
|
|
|
|
.priv_class = &drmeter_class,
|
|
|
|
.uninit = uninit,
|
2021-11-22 15:39:11 +02:00
|
|
|
.flags = AVFILTER_FLAG_METADATA_ONLY,
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(drmeter_inputs),
|
|
|
|
FILTER_OUTPUTS(drmeter_outputs),
|
2021-09-27 13:10:11 +02:00
|
|
|
FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_FLT),
|
2018-03-06 20:47:29 +02:00
|
|
|
};
|