History

avfilter: add apsnr filter

Paul B Mahol committed 3 years ago

Commit 951def81 parent a1928df4 files · +73 −3Browse 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 +64 −3
  3. libavfilter/allfilters.c +1 −0
libavfilter/Makefile+1−0
@@ -84,6 +84,7 @@ OBJS-$(CONFIG_APAD_FILTER) += af_apad.o · 83 unmodified lines
8484 OBJS-$(CONFIG_APERMS_FILTER) += f_perms.o
8585 OBJS-$(CONFIG_APHASER_FILTER) += af_aphaser.o generate_wave_table.o
8686 OBJS-$(CONFIG_APHASESHIFT_FILTER) += af_afreqshift.o
87+OBJS-$(CONFIG_APSNR_FILTER) += af_asdr.o
8788 OBJS-$(CONFIG_APSYCLIP_FILTER) += af_apsyclip.o
8889 OBJS-$(CONFIG_APULSATOR_FILTER) += af_apulsator.o
8990 OBJS-$(CONFIG_AREALTIME_FILTER) += f_realtime.o
libavfilter/af_asdr.c+64−3
@@ -18,6 +18,8 @@ · 17 unmodified lines
1818 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1919 */
2020
21+#include <float.h>
22+
2123 #include "libavutil/channel_layout.h"
2224 #include "libavutil/common.h"
2325
@@ -27,6 +29,8 @@ · 3 unmodified lines
2729
2830 typedef struct AudioSDRContext {
2931 int channels;
32+ uint64_t nb_samples;
33+ double max;
3034 double *sum_u;
3135 double *sum_uv;
3236
@@ -67,6 +71,34 @@ static int sdr_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)\ · 34 unmodified lines
6771 SDR_FILTER(fltp, float)
6872 SDR_FILTER(dblp, double)
6973
74+#define PSNR_FILTER(name, type) \
75+static int psnr_##name(AVFilterContext *ctx, void *arg, int jobnr,int nb_jobs)\
76+{ \
77+ AudioSDRContext *s = ctx->priv; \
78+ AVFrame *u = s->cache[0]; \
79+ AVFrame *v = s->cache[1]; \
80+ const int channels = u->ch_layout.nb_channels; \
81+ const int start = (channels * jobnr) / nb_jobs; \
82+ const int end = (channels * (jobnr+1)) / nb_jobs; \
83+ const int nb_samples = u->nb_samples; \
84+ \
85+ for (int ch = start; ch < end; ch++) { \
86+ const type *const us = (type *)u->extended_data[ch]; \
87+ const type *const vs = (type *)v->extended_data[ch]; \
88+ double sum_uv = 0.; \
89+ \
90+ for (int n = 0; n < nb_samples; n++) \
91+ sum_uv += (us[n] - vs[n]) * (us[n] - vs[n]); \
92+ \
93+ s->sum_uv[ch] += sum_uv; \
94+ } \
95+ \
96+ return 0; \
97+}
98+
99+PSNR_FILTER(fltp, float)
100+PSNR_FILTER(dblp, double)
101+
70102 static int activate(AVFilterContext *ctx)
71103 {
72104 AudioSDRContext *s = ctx->priv;
@@ -97,6 +129,7 @@ static int activate(AVFilterContext *ctx) · 24 unmodified lines
97129 out = s->cache[0];
98130 s->cache[0] = NULL;
99131
132+ s->nb_samples += available;
100133 return ff_filter_frame(outlink, out);
101134 }
102135
@@ -126,7 +159,12 @@ static int config_output(AVFilterLink *outlink) · 23 unmodified lines
126159 AudioSDRContext *s = ctx->priv;
127160
128161 s->channels = inlink->ch_layout.nb_channels;
129− s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? sdr_fltp : sdr_dblp;
162+
163+ if (!strcmp(ctx->filter->name, "asdr"))
164+ s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? sdr_fltp : sdr_dblp;
165+ else
166+ s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? psnr_fltp : psnr_dblp;
167+ s->max = inlink->format == AV_SAMPLE_FMT_FLTP ? FLT_MAX : DBL_MAX;
130168
131169 s->sum_u = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->sum_u));
132170 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) · 7 unmodified lines
140178 {
141179 AudioSDRContext *s = ctx->priv;
142180
143− for (int ch = 0; ch < s->channels; ch++)
144− av_log(ctx, AV_LOG_INFO, "SDR ch%d: %g dB\n", ch, 20. * log10(s->sum_u[ch] / s->sum_uv[ch]));
181+ if (!strcmp(ctx->filter->name, "asdr")) {
182+ for (int ch = 0; ch < s->channels; ch++)
183+ av_log(ctx, AV_LOG_INFO, "SDR ch%d: %g dB\n", ch, 20. * log10(s->sum_u[ch] / s->sum_uv[ch]));
184+ } else {
185+ for (int ch = 0; ch < s->channels; ch++) {
186+ double psnr = s->sum_uv[ch] > 0.0 ? 2.0 * log(s->max) - log(s->nb_samples / s->sum_uv[ch]) : INFINITY;
187+
188+ av_log(ctx, AV_LOG_INFO, "PSNR ch%d: %g dB\n", ch, psnr);
189+ }
190+ }
145191
146192 av_frame_free(&s->cache[0]);
147193 av_frame_free(&s->cache[1]);
@@ -183,3 +229,18 @@ const AVFilter ff_af_asdr = { · 35 unmodified lines
183229 FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP,
184230 AV_SAMPLE_FMT_DBLP),
185231 };
232+
233+const AVFilter ff_af_apsnr = {
234+ .name = "apsnr",
235+ .description = NULL_IF_CONFIG_SMALL("Measure Audio Peak Signal-to-Noise Ratio."),
236+ .priv_size = sizeof(AudioSDRContext),
237+ .activate = activate,
238+ .uninit = uninit,
239+ .flags = AVFILTER_FLAG_METADATA_ONLY |
240+ AVFILTER_FLAG_SLICE_THREADS |
241+ AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
242+ FILTER_INPUTS(inputs),
243+ FILTER_OUTPUTS(outputs),
244+ FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP,
245+ AV_SAMPLE_FMT_DBLP),
246+};
libavfilter/allfilters.c+1−0
@@ -70,6 +70,7 @@ extern const AVFilter ff_af_apad; · 69 unmodified lines
7070 extern const AVFilter ff_af_aperms;
7171 extern const AVFilter ff_af_aphaser;
7272 extern const AVFilter ff_af_aphaseshift;
73+extern const AVFilter ff_af_apsnr;
7374 extern const AVFilter ff_af_apsyclip;
7475 extern const AVFilter ff_af_apulsator;
7576 extern const AVFilter ff_af_arealtime;

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