From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 9C02744FCD for ; Mon, 14 Aug 2023 08:56:51 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CD70468C5F3; Mon, 14 Aug 2023 11:56:48 +0300 (EEST) Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E278868C334 for ; Mon, 14 Aug 2023 11:56:42 +0300 (EEST) Received: by mail.gandi.net (Postfix) with ESMTPSA id 484A51C0003 for ; Mon, 14 Aug 2023 08:56:42 +0000 (UTC) Date: Mon, 14 Aug 2023 10:56:41 +0200 From: Michael Niedermayer To: FFmpeg development discussions and patches Message-ID: <20230814085641.GO7802@pb2> References: MIME-Version: 1.0 In-Reply-To: X-GND-Sasl: michael@niedermayer.cc Subject: Re: [FFmpeg-devel] [PATCH] apsnr and asisdr filter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Content-Type: multipart/mixed; boundary="===============1776205741499527677==" Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: --===============1776205741499527677== Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="sy7aSIEYqaSVeJ/x" Content-Disposition: inline --sy7aSIEYqaSVeJ/x Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sun, Aug 13, 2023 at 05:14:48AM +0200, Paul B Mahol wrote: > Attached. > af_asdr.c | 44 ++++++++++++++++++++++++-------------------- > 1 file changed, 24 insertions(+), 20 deletions(-) > f0f248ba7e893a63a684b92a6d82ab246fc1995c 0003-avfilter-af_asdr-use-singl= e-structure-for-sums.patch > From ad9def7fad58a75450176413564543a16965d165 Mon Sep 17 00:00:00 2001 > From: Paul B Mahol > Date: Sun, 13 Aug 2023 05:03:00 +0200 > Subject: [PATCH 3/3] avfilter/af_asdr: use single structure for sums >=20 > Signed-off-by: Paul B Mahol > --- > libavfilter/af_asdr.c | 44 +++++++++++++++++++++++-------------------- > 1 file changed, 24 insertions(+), 20 deletions(-) >=20 > diff --git a/libavfilter/af_asdr.c b/libavfilter/af_asdr.c > index 53069427bf..dbbb7e3419 100644 > --- a/libavfilter/af_asdr.c > +++ b/libavfilter/af_asdr.c > @@ -27,13 +27,18 @@ > #include "filters.h" > #include "internal.h" > =20 > +typedef struct ChanStats { > + double u; > + double v; > + double uv; > +} ChanStats; > + > typedef struct AudioSDRContext { > int channels; > uint64_t nb_samples; > double max; > - double *sum_u; > - double *sum_v; > - double *sum_uv; > + > + ChanStats *chs; > =20 > AVFrame *cache[2]; > =20 > @@ -52,6 +57,7 @@ static int sdr_##name(AVFilterContext *ctx, void *arg, = int jobnr, int nb_jobs)\ > const int nb_samples =3D u->nb_samples; = \ > = \ > for (int ch =3D start; ch < end; ch++) { = \ > + ChanStats *chs =3D &s->chs[ch]; = \ > const type *const us =3D (type *)u->extended_data[ch]; = \ > const type *const vs =3D (type *)v->extended_data[ch]; = \ > double sum_uv =3D 0.; = \ > @@ -62,8 +68,8 @@ static int sdr_##name(AVFilterContext *ctx, void *arg, = int jobnr, int nb_jobs)\ > sum_uv +=3D (us[n] - vs[n]) * (us[n] - vs[n]); = \ > } = \ > = \ > - s->sum_uv[ch] +=3D sum_uv; = \ > - s->sum_u[ch] +=3D sum_u; = \ > + chs->uv +=3D sum_uv; = \ > + chs->u +=3D sum_u; = \ > } = \ > = \ > return 0; = \ > @@ -84,6 +90,7 @@ static int sisdr_##name(AVFilterContext *ctx, void *arg= ,int jobnr,int nb_jobs)\ > const int nb_samples =3D u->nb_samples; = \ > = \ > for (int ch =3D start; ch < end; ch++) { = \ > + ChanStats *chs =3D &s->chs[ch]; = \ > const type *const us =3D (type *)u->extended_data[ch]; = \ > const type *const vs =3D (type *)v->extended_data[ch]; = \ > double sum_uv =3D 0.; = \ > @@ -96,9 +103,9 @@ static int sisdr_##name(AVFilterContext *ctx, void *ar= g,int jobnr,int nb_jobs)\ > sum_uv +=3D us[n] * vs[n]; = \ > } = \ > = \ > - s->sum_uv[ch] +=3D sum_uv; = \ > - s->sum_u[ch] +=3D sum_u; = \ > - s->sum_v[ch] +=3D sum_v; = \ > + chs->uv +=3D sum_uv; = \ > + chs->u +=3D sum_u; = \ > + chs->v +=3D sum_v; = \ > } = \ > = \ > return 0; = \ > @@ -119,6 +126,7 @@ static int psnr_##name(AVFilterContext *ctx, void *ar= g, int jobnr,int nb_jobs)\ > const int nb_samples =3D u->nb_samples; = \ > = \ > for (int ch =3D start; ch < end; ch++) { = \ > + ChanStats *chs =3D &s->chs[ch]; = \ > const type *const us =3D (type *)u->extended_data[ch]; = \ > const type *const vs =3D (type *)v->extended_data[ch]; = \ > double sum_uv =3D 0.; = \ > @@ -126,7 +134,7 @@ static int psnr_##name(AVFilterContext *ctx, void *ar= g, int jobnr,int nb_jobs)\ > for (int n =3D 0; n < nb_samples; n++) = \ > sum_uv +=3D (us[n] - vs[n]) * (us[n] - vs[n]); = \ > = \ > - s->sum_uv[ch] +=3D sum_uv; = \ > + chs->uv +=3D sum_uv; = \ > } = \ > = \ > return 0; = \ > @@ -204,10 +212,8 @@ static int config_output(AVFilterLink *outlink) > s->filter =3D inlink->format =3D=3D AV_SAMPLE_FMT_FLTP ? psnr_fl= tp : psnr_dblp; > s->max =3D inlink->format =3D=3D AV_SAMPLE_FMT_FLTP ? FLT_MAX : DBL_= MAX; > =20 > - s->sum_u =3D av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->s= um_u)); > - s->sum_v =3D av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->s= um_v)); > - s->sum_uv =3D av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->s= um_uv)); > - if (!s->sum_u || !s->sum_uv || !s->sum_v) > + s->chs =3D av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->chs= )); > + if (!s->chs) > return AVERROR(ENOMEM); > =20 > return 0; > @@ -219,17 +225,17 @@ static av_cold void uninit(AVFilterContext *ctx) > =20 > if (!strcmp(ctx->filter->name, "asdr")) { > for (int ch =3D 0; ch < s->channels; ch++) > - av_log(ctx, AV_LOG_INFO, "SDR ch%d: %g dB\n", ch, 20. * log1= 0(s->sum_u[ch] / s->sum_uv[ch])); > + av_log(ctx, AV_LOG_INFO, "SDR ch%d: %g dB\n", ch, 20. * log1= 0(s->chs[ch].u / s->chs[ch].uv)); > } else if (!strcmp(ctx->filter->name, "asisdr")) { > for (int ch =3D 0; ch < s->channels; ch++) { > - double scale =3D s->sum_uv[ch] / s->sum_v[ch]; > - double sisdr =3D s->sum_u[ch] / (s->sum_u[ch] + scale*scale*= s->sum_v[ch] - 2.0*scale*s->sum_uv[ch]); > + double scale =3D s->chs[ch].uv / s->chs[ch].v; > + double sisdr =3D s->chs[ch].u / fmax(0., s->chs[ch].u + scal= e*scale*s->chs[ch].v - 2.0*scale*s->chs[ch].uv); > =20 > av_log(ctx, AV_LOG_INFO, "SI-SDR ch%d: %g dB\n", ch, 10. * l= og10(sisdr)); > } > } else { > for (int ch =3D 0; ch < s->channels; ch++) { > - double psnr =3D s->sum_uv[ch] > 0.0 ? 2.0 * log(s->max) - lo= g(s->nb_samples / s->sum_uv[ch]) : INFINITY; > + double psnr =3D s->chs[ch].uv > 0.0 ? 2.0 * log(s->max) - lo= g(s->nb_samples / s->chs[ch].uv) : INFINITY; > =20 > av_log(ctx, AV_LOG_INFO, "PSNR ch%d: %g dB\n", ch, psnr); > } > @@ -238,9 +244,7 @@ static av_cold void uninit(AVFilterContext *ctx) > av_frame_free(&s->cache[0]); > av_frame_free(&s->cache[1]); > =20 > - av_freep(&s->sum_u); > - av_freep(&s->sum_v); > - av_freep(&s->sum_uv); > + av_freep(&s->chs); > } > =20 > static const AVFilterPad inputs[] =3D { > --=20 > 2.39.1 >=20 > doc/filters.texi | 7 +++++ > libavfilter/Makefile | 1=20 > libavfilter/af_asdr.c | 64 ++++++++++++++++++++++++++++++++++++++++= ++++++- > libavfilter/allfilters.c | 1=20 > 4 files changed, 72 insertions(+), 1 deletion(-) > 8ed085a8902fa86f0a99838bbd6c4b2645e187d6 0002-avfilter-add-asisdr-filter= =2Epatch > From dfb20b0f4d08428a43b38185776baf6819fc4336 Mon Sep 17 00:00:00 2001 > From: Paul B Mahol > Date: Sun, 13 Aug 2023 04:19:08 +0200 > Subject: [PATCH 2/3] avfilter: add asisdr filter >=20 > Signed-off-by: Paul B Mahol > --- > doc/filters.texi | 7 +++++ > libavfilter/Makefile | 1 + > libavfilter/af_asdr.c | 64 +++++++++++++++++++++++++++++++++++++++- > libavfilter/allfilters.c | 1 + > 4 files changed, 72 insertions(+), 1 deletion(-) >=20 > diff --git a/doc/filters.texi b/doc/filters.texi > index 1025917c63..159764bcb6 100644 > --- a/doc/filters.texi > +++ b/doc/filters.texi > @@ -3197,6 +3197,13 @@ audio, the data is treated as if all the planes we= re concatenated. > A list of Adler-32 checksums for each data plane. > @end table > =20 > +@section asisdr > +Measure Audio Scaled-Invariant Signal-to-Distortion 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 asoftclip > Apply audio soft clipping. > =20 > diff --git a/libavfilter/Makefile b/libavfilter/Makefile > index 90c30e3388..ba07f4ab1e 100644 > --- a/libavfilter/Makefile > +++ b/libavfilter/Makefile > @@ -103,6 +103,7 @@ OBJS-$(CONFIG_ASETRATE_FILTER) +=3D af_= asetrate.o > OBJS-$(CONFIG_ASETTB_FILTER) +=3D settb.o > OBJS-$(CONFIG_ASHOWINFO_FILTER) +=3D af_ashowinfo.o > OBJS-$(CONFIG_ASIDEDATA_FILTER) +=3D f_sidedata.o > +OBJS-$(CONFIG_ASISDR_FILTER) +=3D af_asdr.o > OBJS-$(CONFIG_ASOFTCLIP_FILTER) +=3D af_asoftclip.o > OBJS-$(CONFIG_ASPECTRALSTATS_FILTER) +=3D af_aspectralstats.o > OBJS-$(CONFIG_ASPLIT_FILTER) +=3D split.o > diff --git a/libavfilter/af_asdr.c b/libavfilter/af_asdr.c > index b0401804f6..53069427bf 100644 > --- a/libavfilter/af_asdr.c > +++ b/libavfilter/af_asdr.c > @@ -32,6 +32,7 @@ typedef struct AudioSDRContext { > uint64_t nb_samples; > double max; > double *sum_u; > + double *sum_v; > double *sum_uv; > =20 > AVFrame *cache[2]; > @@ -71,6 +72,41 @@ static int sdr_##name(AVFilterContext *ctx, void *arg,= int jobnr, int nb_jobs)\ > SDR_FILTER(fltp, float) > SDR_FILTER(dblp, double) > =20 > +#define SISDR_FILTER(name, type) = \ > +static int sisdr_##name(AVFilterContext *ctx, void *arg,int jobnr,int nb= _jobs)\ > +{ = \ > + AudioSDRContext *s =3D ctx->priv; = \ > + AVFrame *u =3D s->cache[0]; = \ > + AVFrame *v =3D s->cache[1]; = \ > + const int channels =3D u->ch_layout.nb_channels; = \ > + const int start =3D (channels * jobnr) / nb_jobs; = \ > + const int end =3D (channels * (jobnr+1)) / nb_jobs; = \ > + const int nb_samples =3D u->nb_samples; = \ > + = \ > + for (int ch =3D start; ch < end; ch++) { = \ > + const type *const us =3D (type *)u->extended_data[ch]; = \ > + const type *const vs =3D (type *)v->extended_data[ch]; = \ > + double sum_uv =3D 0.; = \ > + double sum_u =3D 0.; = \ > + double sum_v =3D 0.; = \ > + \ > + for (int n =3D 0; n < nb_samples; n++) { = \ > + sum_u +=3D us[n] * us[n]; = \ > + sum_v +=3D vs[n] * vs[n]; = \ > + sum_uv +=3D us[n] * vs[n]; = \ > + } = \ These should be handled by DSP code which can be asm optimized > + = \ > + s->sum_uv[ch] +=3D sum_uv; = \ > + s->sum_u[ch] +=3D sum_u; = \ > + s->sum_v[ch] +=3D sum_v; = \ > + } = \ > + = \ > + return 0; = \ > +} > + > +SISDR_FILTER(fltp, float) > +SISDR_FILTER(dblp, double) [...] > @@ -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) > =20 > +#define PSNR_FILTER(name, type) = \ > +static int psnr_##name(AVFilterContext *ctx, void *arg, int jobnr,int nb= _jobs)\ > +{ = \ > + AudioSDRContext *s =3D ctx->priv; = \ > + AVFrame *u =3D s->cache[0]; = \ > + AVFrame *v =3D s->cache[1]; = \ > + const int channels =3D u->ch_layout.nb_channels; = \ > + const int start =3D (channels * jobnr) / nb_jobs; = \ > + const int end =3D (channels * (jobnr+1)) / nb_jobs; = \ > + const int nb_samples =3D u->nb_samples; = \ > + = \ > + for (int ch =3D start; ch < end; ch++) { = \ > + const type *const us =3D (type *)u->extended_data[ch]; = \ > + const type *const vs =3D (type *)v->extended_data[ch]; = \ > + double sum_uv =3D 0.; = \ > + = \ > + for (int n =3D 0; n < nb_samples; n++) = \ > + sum_uv +=3D (us[n] - vs[n]) * (us[n] - vs[n]); = \ These should be handled by DSP code which can be asm optimized > + = \ > + s->sum_uv[ch] +=3D sum_uv; = \ > + } = \ > + = \ > + return 0; = \ > +} > + > +PSNR_FILTER(fltp, float) > +PSNR_FILTER(dblp, double) > + > static int activate(AVFilterContext *ctx) > { > AudioSDRContext *s =3D ctx->priv; [...] Thx --=20 Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Any man who breaks a law that conscience tells him is unjust and willingly= =20 accepts the penalty by staying in jail in order to arouse the conscience of= =20 the community on the injustice of the law is at that moment expressing the= =20 very highest respect for law. - Martin Luther King Jr --sy7aSIEYqaSVeJ/x Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iF0EABEIAB0WIQSf8hKLFH72cwut8TNhHseHBAsPqwUCZNnsSQAKCRBhHseHBAsP qxkcAJ0ZFrk194RjGaZIifZPIqBFExEVBACff4tooXzKOWnkEUw3picG1Eb11KQ= =7CLO -----END PGP SIGNATURE----- --sy7aSIEYqaSVeJ/x-- --===============1776205741499527677== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". --===============1776205741499527677==--