From 8b457c83855ccc292a53be2bd716bf445d37d7e0 Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Sun, 13 Aug 2023 02:57:57 +0200 Subject: [PATCH 1/3] avfilter: add apsnr filter Signed-off-by: Paul B Mahol --- doc/filters.texi | 7 +++++ libavfilter/Makefile | 1 + libavfilter/af_asdr.c | 67 ++++++++++++++++++++++++++++++++++++++-- libavfilter/allfilters.c | 1 + 4 files changed, 73 insertions(+), 3 deletions(-) diff --git a/doc/filters.texi b/doc/filters.texi index e041adc40f..1025917c63 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -2892,6 +2892,13 @@ Default value is 8. This filter supports the all above options as @ref{commands}. +@section apsnr +Measure Audio Peak Signal-to-Noise Ratio. + +This filter takes two audio streams for input, and outputs first +audio stream. +Results are in dB per channel at end of either input. + @section apsyclip Apply Psychoacoustic clipper to input audio stream. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index b26a85a7d2..90c30e3388 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -85,6 +85,7 @@ OBJS-$(CONFIG_APAD_FILTER) += af_apad.o OBJS-$(CONFIG_APERMS_FILTER) += f_perms.o OBJS-$(CONFIG_APHASER_FILTER) += af_aphaser.o generate_wave_table.o OBJS-$(CONFIG_APHASESHIFT_FILTER) += af_afreqshift.o +OBJS-$(CONFIG_APSNR_FILTER) += af_asdr.o OBJS-$(CONFIG_APSYCLIP_FILTER) += af_apsyclip.o OBJS-$(CONFIG_APULSATOR_FILTER) += af_apulsator.o OBJS-$(CONFIG_AREALTIME_FILTER) += f_realtime.o diff --git a/libavfilter/af_asdr.c b/libavfilter/af_asdr.c index 7d778b7f6b..b0401804f6 100644 --- a/libavfilter/af_asdr.c +++ b/libavfilter/af_asdr.c @@ -18,6 +18,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include + #include "libavutil/channel_layout.h" #include "libavutil/common.h" @@ -27,6 +29,8 @@ typedef struct AudioSDRContext { int channels; + uint64_t nb_samples; + double max; double *sum_u; double *sum_uv; @@ -67,6 +71,34 @@ static int sdr_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)\ SDR_FILTER(fltp, float) SDR_FILTER(dblp, double) +#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++) { \ + 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]); \ + \ + s->sum_uv[ch] += sum_uv; \ + } \ + \ + return 0; \ +} + +PSNR_FILTER(fltp, float) +PSNR_FILTER(dblp, double) + static int activate(AVFilterContext *ctx) { AudioSDRContext *s = ctx->priv; @@ -97,6 +129,7 @@ static int activate(AVFilterContext *ctx) out = s->cache[0]; s->cache[0] = NULL; + s->nb_samples += available; return ff_filter_frame(outlink, out); } @@ -126,7 +159,12 @@ static int config_output(AVFilterLink *outlink) AudioSDRContext *s = ctx->priv; s->channels = inlink->ch_layout.nb_channels; - s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? sdr_fltp : sdr_dblp; + + if (!strcmp(ctx->filter->name, "asdr")) + s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? sdr_fltp : sdr_dblp; + 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; s->sum_u = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->sum_u)); s->sum_uv = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->sum_uv)); @@ -140,8 +178,16 @@ static av_cold void uninit(AVFilterContext *ctx) { AudioSDRContext *s = ctx->priv; - for (int ch = 0; ch < s->channels; ch++) - av_log(ctx, AV_LOG_INFO, "SDR ch%d: %g dB\n", ch, 20. * log10(s->sum_u[ch] / s->sum_uv[ch])); + if (!strcmp(ctx->filter->name, "asdr")) { + for (int ch = 0; ch < s->channels; ch++) + av_log(ctx, AV_LOG_INFO, "SDR ch%d: %g dB\n", ch, 20. * log10(s->sum_u[ch] / s->sum_uv[ch])); + } else { + for (int ch = 0; ch < s->channels; ch++) { + double psnr = s->sum_uv[ch] > 0.0 ? 2.0 * log(s->max) - log(s->nb_samples / s->sum_uv[ch]) : INFINITY; + + av_log(ctx, AV_LOG_INFO, "PSNR ch%d: %g dB\n", ch, psnr); + } + } av_frame_free(&s->cache[0]); av_frame_free(&s->cache[1]); @@ -183,3 +229,18 @@ const AVFilter ff_af_asdr = { FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP), }; + +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), +}; diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 286e601799..949c5d4992 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -71,6 +71,7 @@ extern const AVFilter ff_af_apad; extern const AVFilter ff_af_aperms; extern const AVFilter ff_af_aphaser; extern const AVFilter ff_af_aphaseshift; +extern const AVFilter ff_af_apsnr; extern const AVFilter ff_af_apsyclip; extern const AVFilter ff_af_apulsator; extern const AVFilter ff_af_arealtime; -- 2.39.1