2021-09-12 20:11:24 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 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
|
|
|
|
*/
|
|
|
|
|
2023-08-13 02:57:57 +02:00
|
|
|
#include <float.h>
|
|
|
|
|
2021-09-12 20:11:24 +02:00
|
|
|
#include "libavutil/channel_layout.h"
|
|
|
|
#include "libavutil/common.h"
|
|
|
|
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "filters.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
2023-08-13 05:03:00 +02:00
|
|
|
typedef struct ChanStats {
|
|
|
|
double u;
|
|
|
|
double v;
|
|
|
|
double uv;
|
|
|
|
} ChanStats;
|
|
|
|
|
2021-09-12 20:11:24 +02:00
|
|
|
typedef struct AudioSDRContext {
|
|
|
|
int channels;
|
2023-08-13 02:57:57 +02:00
|
|
|
uint64_t nb_samples;
|
|
|
|
double max;
|
2023-08-13 05:03:00 +02:00
|
|
|
|
|
|
|
ChanStats *chs;
|
2021-09-12 20:11:24 +02:00
|
|
|
|
|
|
|
AVFrame *cache[2];
|
|
|
|
|
2023-08-13 02:32:09 +02:00
|
|
|
int (*filter)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
|
|
|
|
} AudioSDRContext;
|
2023-04-30 12:32:37 +02:00
|
|
|
|
2023-08-13 02:32:09 +02:00
|
|
|
#define SDR_FILTER(name, type) \
|
|
|
|
static int sdr_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)\
|
|
|
|
{ \
|
|
|
|
AudioSDRContext *s = ctx->priv; \
|
|
|
|
AVFrame *u = s->cache[0]; \
|
|
|
|
AVFrame *v = s->cache[1]; \
|
|
|
|
const int channels = u->ch_layout.nb_channels; \
|
|
|
|
const int start = (channels * jobnr) / nb_jobs; \
|
|
|
|
const int end = (channels * (jobnr+1)) / nb_jobs; \
|
|
|
|
const int nb_samples = u->nb_samples; \
|
|
|
|
\
|
|
|
|
for (int ch = start; ch < end; ch++) { \
|
2023-08-13 05:03:00 +02:00
|
|
|
ChanStats *chs = &s->chs[ch]; \
|
2023-08-13 02:32:09 +02:00
|
|
|
const type *const us = (type *)u->extended_data[ch]; \
|
|
|
|
const type *const vs = (type *)v->extended_data[ch]; \
|
|
|
|
double sum_uv = 0.; \
|
|
|
|
double sum_u = 0.; \
|
|
|
|
\
|
|
|
|
for (int n = 0; n < nb_samples; n++) { \
|
|
|
|
sum_u += us[n] * us[n]; \
|
|
|
|
sum_uv += (us[n] - vs[n]) * (us[n] - vs[n]); \
|
|
|
|
} \
|
|
|
|
\
|
2023-08-13 05:03:00 +02:00
|
|
|
chs->uv += sum_uv; \
|
|
|
|
chs->u += sum_u; \
|
2023-08-13 02:32:09 +02:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
return 0; \
|
2021-09-12 20:11:24 +02:00
|
|
|
}
|
|
|
|
|
2023-08-13 02:32:09 +02:00
|
|
|
SDR_FILTER(fltp, float)
|
|
|
|
SDR_FILTER(dblp, double)
|
|
|
|
|
2023-08-13 04:19:08 +02:00
|
|
|
#define SISDR_FILTER(name, type) \
|
|
|
|
static int sisdr_##name(AVFilterContext *ctx, void *arg,int jobnr,int nb_jobs)\
|
|
|
|
{ \
|
|
|
|
AudioSDRContext *s = ctx->priv; \
|
|
|
|
AVFrame *u = s->cache[0]; \
|
|
|
|
AVFrame *v = s->cache[1]; \
|
|
|
|
const int channels = u->ch_layout.nb_channels; \
|
|
|
|
const int start = (channels * jobnr) / nb_jobs; \
|
|
|
|
const int end = (channels * (jobnr+1)) / nb_jobs; \
|
|
|
|
const int nb_samples = u->nb_samples; \
|
|
|
|
\
|
|
|
|
for (int ch = start; ch < end; ch++) { \
|
2023-08-13 05:03:00 +02:00
|
|
|
ChanStats *chs = &s->chs[ch]; \
|
2023-08-13 04:19:08 +02:00
|
|
|
const type *const us = (type *)u->extended_data[ch]; \
|
|
|
|
const type *const vs = (type *)v->extended_data[ch]; \
|
|
|
|
double sum_uv = 0.; \
|
|
|
|
double sum_u = 0.; \
|
|
|
|
double sum_v = 0.; \
|
|
|
|
\
|
|
|
|
for (int n = 0; n < nb_samples; n++) { \
|
|
|
|
sum_u += us[n] * us[n]; \
|
|
|
|
sum_v += vs[n] * vs[n]; \
|
|
|
|
sum_uv += us[n] * vs[n]; \
|
|
|
|
} \
|
|
|
|
\
|
2023-08-13 05:03:00 +02:00
|
|
|
chs->uv += sum_uv; \
|
|
|
|
chs->u += sum_u; \
|
|
|
|
chs->v += sum_v; \
|
2023-08-13 04:19:08 +02:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
return 0; \
|
|
|
|
}
|
|
|
|
|
|
|
|
SISDR_FILTER(fltp, float)
|
|
|
|
SISDR_FILTER(dblp, double)
|
|
|
|
|
2023-08-13 02:57:57 +02:00
|
|
|
#define PSNR_FILTER(name, type) \
|
|
|
|
static int psnr_##name(AVFilterContext *ctx, void *arg, int jobnr,int nb_jobs)\
|
|
|
|
{ \
|
|
|
|
AudioSDRContext *s = ctx->priv; \
|
|
|
|
AVFrame *u = s->cache[0]; \
|
|
|
|
AVFrame *v = s->cache[1]; \
|
|
|
|
const int channels = u->ch_layout.nb_channels; \
|
|
|
|
const int start = (channels * jobnr) / nb_jobs; \
|
|
|
|
const int end = (channels * (jobnr+1)) / nb_jobs; \
|
|
|
|
const int nb_samples = u->nb_samples; \
|
|
|
|
\
|
|
|
|
for (int ch = start; ch < end; ch++) { \
|
2023-08-13 05:03:00 +02:00
|
|
|
ChanStats *chs = &s->chs[ch]; \
|
2023-08-13 02:57:57 +02:00
|
|
|
const type *const us = (type *)u->extended_data[ch]; \
|
|
|
|
const type *const vs = (type *)v->extended_data[ch]; \
|
|
|
|
double sum_uv = 0.; \
|
|
|
|
\
|
|
|
|
for (int n = 0; n < nb_samples; n++) \
|
|
|
|
sum_uv += (us[n] - vs[n]) * (us[n] - vs[n]); \
|
|
|
|
\
|
2023-08-13 05:03:00 +02:00
|
|
|
chs->uv += sum_uv; \
|
2023-08-13 02:57:57 +02:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
return 0; \
|
|
|
|
}
|
|
|
|
|
|
|
|
PSNR_FILTER(fltp, float)
|
|
|
|
PSNR_FILTER(dblp, double)
|
|
|
|
|
2021-09-12 20:11:24 +02:00
|
|
|
static int activate(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AudioSDRContext *s = ctx->priv;
|
2023-04-30 12:16:02 +02:00
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2023-08-13 02:21:42 +02:00
|
|
|
int ret, status, available;
|
2021-09-12 20:11:24 +02:00
|
|
|
int64_t pts;
|
|
|
|
|
2023-04-30 12:16:02 +02:00
|
|
|
FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
|
2021-09-12 20:11:24 +02:00
|
|
|
|
|
|
|
available = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]), ff_inlink_queued_samples(ctx->inputs[1]));
|
|
|
|
if (available > 0) {
|
|
|
|
AVFrame *out;
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
ret = ff_inlink_consume_samples(ctx->inputs[i], available, available, &s->cache[i]);
|
2023-08-13 02:21:42 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
av_frame_free(&s->cache[0]);
|
|
|
|
av_frame_free(&s->cache[1]);
|
|
|
|
return ret;
|
2021-09-12 20:11:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 12:18:16 +02:00
|
|
|
if (!ctx->is_disabled)
|
2023-08-13 02:32:09 +02:00
|
|
|
ff_filter_execute(ctx, s->filter, NULL, NULL,
|
2023-04-30 12:32:37 +02:00
|
|
|
FFMIN(outlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
|
2021-09-12 20:11:24 +02:00
|
|
|
|
|
|
|
av_frame_free(&s->cache[1]);
|
|
|
|
out = s->cache[0];
|
|
|
|
s->cache[0] = NULL;
|
|
|
|
|
2023-08-13 02:57:57 +02:00
|
|
|
s->nb_samples += available;
|
2023-04-30 12:16:02 +02:00
|
|
|
return ff_filter_frame(outlink, out);
|
2021-09-12 20:11:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
|
2023-08-13 02:21:42 +02:00
|
|
|
ff_outlink_set_status(outlink, status, pts);
|
2021-09-12 20:11:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 12:16:02 +02:00
|
|
|
if (ff_outlink_frame_wanted(outlink)) {
|
2021-09-12 20:11:24 +02:00
|
|
|
for (int i = 0; i < 2; i++) {
|
2023-11-28 13:19:08 +02:00
|
|
|
if (s->cache[i] || ff_inlink_queued_samples(ctx->inputs[i]) > 0)
|
2021-09-12 20:11:24 +02:00
|
|
|
continue;
|
|
|
|
ff_inlink_request_frame(ctx->inputs[i]);
|
2023-11-28 13:19:08 +02:00
|
|
|
return 0;
|
2021-09-12 20:11:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FFERROR_NOT_READY;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AudioSDRContext *s = ctx->priv;
|
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
s->channels = inlink->ch_layout.nb_channels;
|
2023-08-13 02:57:57 +02:00
|
|
|
|
|
|
|
if (!strcmp(ctx->filter->name, "asdr"))
|
|
|
|
s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? sdr_fltp : sdr_dblp;
|
2023-08-13 04:19:08 +02:00
|
|
|
else if (!strcmp(ctx->filter->name, "asisdr"))
|
|
|
|
s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? sisdr_fltp : sisdr_dblp;
|
2023-08-13 02:57:57 +02:00
|
|
|
else
|
|
|
|
s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? psnr_fltp : psnr_dblp;
|
|
|
|
s->max = inlink->format == AV_SAMPLE_FMT_FLTP ? FLT_MAX : DBL_MAX;
|
2021-09-12 20:11:24 +02:00
|
|
|
|
2023-08-13 05:03:00 +02:00
|
|
|
s->chs = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->chs));
|
|
|
|
if (!s->chs)
|
2021-09-12 20:11:24 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AudioSDRContext *s = ctx->priv;
|
|
|
|
|
2023-08-13 02:57:57 +02:00
|
|
|
if (!strcmp(ctx->filter->name, "asdr")) {
|
|
|
|
for (int ch = 0; ch < s->channels; ch++)
|
2023-08-14 01:36:34 +02:00
|
|
|
av_log(ctx, AV_LOG_INFO, "SDR ch%d: %g dB\n", ch, 10. * log10(s->chs[ch].u / s->chs[ch].uv));
|
2023-08-13 04:19:08 +02:00
|
|
|
} else if (!strcmp(ctx->filter->name, "asisdr")) {
|
|
|
|
for (int ch = 0; ch < s->channels; ch++) {
|
2023-08-13 05:03:00 +02:00
|
|
|
double scale = s->chs[ch].uv / s->chs[ch].v;
|
2023-08-14 01:36:34 +02:00
|
|
|
double sisdr = scale * scale * s->chs[ch].v / fmax(0., s->chs[ch].u + scale*scale*s->chs[ch].v - 2.0*scale*s->chs[ch].uv);
|
2023-08-13 04:19:08 +02:00
|
|
|
|
|
|
|
av_log(ctx, AV_LOG_INFO, "SI-SDR ch%d: %g dB\n", ch, 10. * log10(sisdr));
|
|
|
|
}
|
2023-08-13 02:57:57 +02:00
|
|
|
} else {
|
|
|
|
for (int ch = 0; ch < s->channels; ch++) {
|
2023-08-13 05:03:00 +02:00
|
|
|
double psnr = s->chs[ch].uv > 0.0 ? 2.0 * log(s->max) - log(s->nb_samples / s->chs[ch].uv) : INFINITY;
|
2023-08-13 02:57:57 +02:00
|
|
|
|
|
|
|
av_log(ctx, AV_LOG_INFO, "PSNR ch%d: %g dB\n", ch, psnr);
|
|
|
|
}
|
|
|
|
}
|
2021-09-12 20:11:24 +02:00
|
|
|
|
|
|
|
av_frame_free(&s->cache[0]);
|
|
|
|
av_frame_free(&s->cache[1]);
|
|
|
|
|
2023-08-13 05:03:00 +02:00
|
|
|
av_freep(&s->chs);
|
2021-09-12 20:11:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "input0",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "input1",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_output,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const AVFilter ff_af_asdr = {
|
|
|
|
.name = "asdr",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Measure Audio Signal-to-Distortion Ratio."),
|
|
|
|
.priv_size = sizeof(AudioSDRContext),
|
|
|
|
.activate = activate,
|
|
|
|
.uninit = uninit,
|
2023-04-30 12:18:16 +02:00
|
|
|
.flags = AVFILTER_FLAG_METADATA_ONLY |
|
2023-04-30 12:32:37 +02:00
|
|
|
AVFILTER_FLAG_SLICE_THREADS |
|
2023-04-30 12:18:16 +02:00
|
|
|
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
|
2021-09-12 20:11:24 +02:00
|
|
|
FILTER_INPUTS(inputs),
|
|
|
|
FILTER_OUTPUTS(outputs),
|
2023-08-13 02:32:09 +02:00
|
|
|
FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP,
|
|
|
|
AV_SAMPLE_FMT_DBLP),
|
2021-09-12 20:11:24 +02:00
|
|
|
};
|
2023-08-13 02:57:57 +02:00
|
|
|
|
|
|
|
const AVFilter ff_af_apsnr = {
|
|
|
|
.name = "apsnr",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Measure Audio Peak Signal-to-Noise Ratio."),
|
|
|
|
.priv_size = sizeof(AudioSDRContext),
|
|
|
|
.activate = activate,
|
|
|
|
.uninit = uninit,
|
|
|
|
.flags = AVFILTER_FLAG_METADATA_ONLY |
|
|
|
|
AVFILTER_FLAG_SLICE_THREADS |
|
|
|
|
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
|
|
|
|
FILTER_INPUTS(inputs),
|
|
|
|
FILTER_OUTPUTS(outputs),
|
|
|
|
FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP,
|
|
|
|
AV_SAMPLE_FMT_DBLP),
|
|
|
|
};
|
2023-08-13 04:19:08 +02:00
|
|
|
|
|
|
|
const AVFilter ff_af_asisdr = {
|
|
|
|
.name = "asisdr",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Measure Audio Scale-Invariant Signal-to-Distortion Ratio."),
|
|
|
|
.priv_size = sizeof(AudioSDRContext),
|
|
|
|
.activate = activate,
|
|
|
|
.uninit = uninit,
|
|
|
|
.flags = AVFILTER_FLAG_METADATA_ONLY |
|
|
|
|
AVFILTER_FLAG_SLICE_THREADS |
|
|
|
|
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
|
|
|
|
FILTER_INPUTS(inputs),
|
|
|
|
FILTER_OUTPUTS(outputs),
|
|
|
|
FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP,
|
|
|
|
AV_SAMPLE_FMT_DBLP),
|
|
|
|
};
|