History

avfilter: add asisdr filter

Paul B Mahol committed 3 years ago

Commit e41d5221 parent 951def84 files · +72 −1Browse files

Showing the changes to libavfilter (3 of 4 files). Show all

Unified · Split

3 files
  1. libavfilter/Makefile +1 −0
  2. libavfilter/af_asdr.c +63 −1
  3. libavfilter/allfilters.c +1 −0
libavfilter/Makefile+1−0
@@ -102,6 +102,7 @@ OBJS-$(CONFIG_ASETRATE_FILTER) += af_asetrate.o · 101 unmodified lines
102102 OBJS-$(CONFIG_ASETTB_FILTER) += settb.o
103103 OBJS-$(CONFIG_ASHOWINFO_FILTER) += af_ashowinfo.o
104104 OBJS-$(CONFIG_ASIDEDATA_FILTER) += f_sidedata.o
105+OBJS-$(CONFIG_ASISDR_FILTER) += af_asdr.o
105106 OBJS-$(CONFIG_ASOFTCLIP_FILTER) += af_asoftclip.o
106107 OBJS-$(CONFIG_ASPECTRALSTATS_FILTER) += af_aspectralstats.o
107108 OBJS-$(CONFIG_ASPLIT_FILTER) += split.o
libavfilter/af_asdr.c+63−1
@@ -32,6 +32,7 @@ typedef struct AudioSDRContext { · 31 unmodified lines
3232 uint64_t nb_samples;
3333 double max;
3434 double *sum_u;
35+ double *sum_v;
3536 double *sum_uv;
3637
3738 AVFrame *cache[2];
@@ -71,6 +72,41 @@ static int sdr_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)\ · 33 unmodified lines
7172 SDR_FILTER(fltp, float)
7273 SDR_FILTER(dblp, double)
7374
75+#define SISDR_FILTER(name, type) \
76+static int sisdr_##name(AVFilterContext *ctx, void *arg,int jobnr,int nb_jobs)\
77+{ \
78+ AudioSDRContext *s = ctx->priv; \
79+ AVFrame *u = s->cache[0]; \
80+ AVFrame *v = s->cache[1]; \
81+ const int channels = u->ch_layout.nb_channels; \
82+ const int start = (channels * jobnr) / nb_jobs; \
83+ const int end = (channels * (jobnr+1)) / nb_jobs; \
84+ const int nb_samples = u->nb_samples; \
85+ \
86+ for (int ch = start; ch < end; ch++) { \
87+ const type *const us = (type *)u->extended_data[ch]; \
88+ const type *const vs = (type *)v->extended_data[ch]; \
89+ double sum_uv = 0.; \
90+ double sum_u = 0.; \
91+ double sum_v = 0.; \
92+ \
93+ for (int n = 0; n < nb_samples; n++) { \
94+ sum_u += us[n] * us[n]; \
95+ sum_v += vs[n] * vs[n]; \
96+ sum_uv += us[n] * vs[n]; \
97+ } \
98+ \
99+ s->sum_uv[ch] += sum_uv; \
100+ s->sum_u[ch] += sum_u; \
101+ s->sum_v[ch] += sum_v; \
102+ } \
103+ \
104+ return 0; \
105+}
106+
107+SISDR_FILTER(fltp, float)
108+SISDR_FILTER(dblp, double)
109+
74110 #define PSNR_FILTER(name, type) \
75111 static int psnr_##name(AVFilterContext *ctx, void *arg, int jobnr,int nb_jobs)\
76112 { \
@@ -162,13 +198,16 @@ static int config_output(AVFilterLink *outlink) · 85 unmodified lines
162198
163199 if (!strcmp(ctx->filter->name, "asdr"))
164200 s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? sdr_fltp : sdr_dblp;
201+ else if (!strcmp(ctx->filter->name, "asisdr"))
202+ s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? sisdr_fltp : sisdr_dblp;
165203 else
166204 s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? psnr_fltp : psnr_dblp;
167205 s->max = inlink->format == AV_SAMPLE_FMT_FLTP ? FLT_MAX : DBL_MAX;
168206
169207 s->sum_u = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->sum_u));
208+ s->sum_v = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->sum_v));
170209 s->sum_uv = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->sum_uv));
171− if (!s->sum_u || !s->sum_uv)
210+ if (!s->sum_u || !s->sum_uv || !s->sum_v)
172211 return AVERROR(ENOMEM);
173212
174213 return 0;
@@ -181,6 +220,13 @@ static av_cold void uninit(AVFilterContext *ctx) · 6 unmodified lines
181220 if (!strcmp(ctx->filter->name, "asdr")) {
182221 for (int ch = 0; ch < s->channels; ch++)
183222 av_log(ctx, AV_LOG_INFO, "SDR ch%d: %g dB\n", ch, 20. * log10(s->sum_u[ch] / s->sum_uv[ch]));
223+ } else if (!strcmp(ctx->filter->name, "asisdr")) {
224+ for (int ch = 0; ch < s->channels; ch++) {
225+ double scale = s->sum_uv[ch] / s->sum_v[ch];
226+ double sisdr = s->sum_u[ch] / (s->sum_u[ch] + scale*scale*s->sum_v[ch] - 2.0*scale*s->sum_uv[ch]);
227+
228+ av_log(ctx, AV_LOG_INFO, "SI-SDR ch%d: %g dB\n", ch, 10. * log10(sisdr));
229+ }
184230 } else {
185231 for (int ch = 0; ch < s->channels; ch++) {
186232 double psnr = s->sum_uv[ch] > 0.0 ? 2.0 * log(s->max) - log(s->nb_samples / s->sum_uv[ch]) : INFINITY;
@@ -193,6 +239,7 @@ static av_cold void uninit(AVFilterContext *ctx) · 6 unmodified lines
193239 av_frame_free(&s->cache[1]);
194240
195241 av_freep(&s->sum_u);
242+ av_freep(&s->sum_v);
196243 av_freep(&s->sum_uv);
197244 }
198245
@@ -244,3 +291,18 @@ const AVFilter ff_af_apsnr = { · 45 unmodified lines
244291 FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP,
245292 AV_SAMPLE_FMT_DBLP),
246293 };
294+
295+const AVFilter ff_af_asisdr = {
296+ .name = "asisdr",
297+ .description = NULL_IF_CONFIG_SMALL("Measure Audio Scale-Invariant Signal-to-Distortion Ratio."),
298+ .priv_size = sizeof(AudioSDRContext),
299+ .activate = activate,
300+ .uninit = uninit,
301+ .flags = AVFILTER_FLAG_METADATA_ONLY |
302+ AVFILTER_FLAG_SLICE_THREADS |
303+ AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
304+ FILTER_INPUTS(inputs),
305+ FILTER_OUTPUTS(outputs),
306+ FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP,
307+ AV_SAMPLE_FMT_DBLP),
308+};
libavfilter/allfilters.c+1−0
@@ -88,6 +88,7 @@ extern const AVFilter ff_af_asetrate; · 87 unmodified lines
8888 extern const AVFilter ff_af_asettb;
8989 extern const AVFilter ff_af_ashowinfo;
9090 extern const AVFilter ff_af_asidedata;
91+extern const AVFilter ff_af_asisdr;
9192 extern const AVFilter ff_af_asoftclip;
9293 extern const AVFilter ff_af_aspectralstats;
9394 extern const AVFilter ff_af_asplit;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t