History

avfilter: add apsnr filter

Paul B Mahol committed 3 years ago

Commit 951def81 parent a1928df4 files · +73 −3Browse files

Unified · Split

4 files
  1. doc/filters.texi +7 −0
  2. libavfilter/Makefile +1 −0
  3. libavfilter/af_asdr.c +64 −3
  4. libavfilter/allfilters.c +1 −0
doc/filters.texi+7−0
@@ -2836,6 +2836,13 @@ Default value is 8. · 2835 unmodified lines
2836 2836
2837This filter supports the all above options as @ref{commands}.2837This filter supports the all above options as @ref{commands}.
2838 2838
2839@section apsnr
2840Measure Audio Peak Signal-to-Noise Ratio.
2841
2842This filter takes two audio streams for input, and outputs first
2843audio stream.
2844Results are in dB per channel at end of either input.
2845
2839@section apsyclip2846@section apsyclip
2840Apply Psychoacoustic clipper to input audio stream.2847Apply Psychoacoustic clipper to input audio stream.
2841 2848
libavfilter/Makefile+1−0
@@ -84,6 +84,7 @@ OBJS-$(CONFIG_APAD_FILTER) += af_apad.o · 83 unmodified lines
84OBJS-$(CONFIG_APERMS_FILTER) += f_perms.o84OBJS-$(CONFIG_APERMS_FILTER) += f_perms.o
85OBJS-$(CONFIG_APHASER_FILTER) += af_aphaser.o generate_wave_table.o85OBJS-$(CONFIG_APHASER_FILTER) += af_aphaser.o generate_wave_table.o
86OBJS-$(CONFIG_APHASESHIFT_FILTER) += af_afreqshift.o86OBJS-$(CONFIG_APHASESHIFT_FILTER) += af_afreqshift.o
87OBJS-$(CONFIG_APSNR_FILTER) += af_asdr.o
87OBJS-$(CONFIG_APSYCLIP_FILTER) += af_apsyclip.o88OBJS-$(CONFIG_APSYCLIP_FILTER) += af_apsyclip.o
88OBJS-$(CONFIG_APULSATOR_FILTER) += af_apulsator.o89OBJS-$(CONFIG_APULSATOR_FILTER) += af_apulsator.o
89OBJS-$(CONFIG_AREALTIME_FILTER) += f_realtime.o90OBJS-$(CONFIG_AREALTIME_FILTER) += f_realtime.o
libavfilter/af_asdr.c+64−3
@@ -18,6 +18,8 @@ · 17 unmodified lines
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */19 */
20 20
21#include <float.h>
22
21#include "libavutil/channel_layout.h"23#include "libavutil/channel_layout.h"
22#include "libavutil/common.h"24#include "libavutil/common.h"
23 25
@@ -27,6 +29,8 @@ · 3 unmodified lines
27 29
28typedef struct AudioSDRContext {30typedef struct AudioSDRContext {
29 int channels;31 int channels;
32 uint64_t nb_samples;
33 double max;
30 double *sum_u;34 double *sum_u;
31 double *sum_uv;35 double *sum_uv;
32 36
@@ -67,6 +71,34 @@ static int sdr_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)\ · 34 unmodified lines
67SDR_FILTER(fltp, float)71SDR_FILTER(fltp, float)
68SDR_FILTER(dblp, double)72SDR_FILTER(dblp, double)
69 73
74#define PSNR_FILTER(name, type) \
75static 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
99PSNR_FILTER(fltp, float)
100PSNR_FILTER(dblp, double)
101
70static int activate(AVFilterContext *ctx)102static int activate(AVFilterContext *ctx)
71{103{
72 AudioSDRContext *s = ctx->priv;104 AudioSDRContext *s = ctx->priv;
@@ -97,6 +129,7 @@ static int activate(AVFilterContext *ctx) · 24 unmodified lines
97 out = s->cache[0];129 out = s->cache[0];
98 s->cache[0] = NULL;130 s->cache[0] = NULL;
99 131
132 s->nb_samples += available;
100 return ff_filter_frame(outlink, out);133 return ff_filter_frame(outlink, out);
101 }134 }
102 135
@@ -126,7 +159,12 @@ static int config_output(AVFilterLink *outlink) · 23 unmodified lines
126 AudioSDRContext *s = ctx->priv;159 AudioSDRContext *s = ctx->priv;
127 160
128 s->channels = inlink->ch_layout.nb_channels;161 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;
130 168
131 s->sum_u = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->sum_u));169 s->sum_u = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->sum_u));
132 s->sum_uv = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->sum_uv));170 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
140{178{
141 AudioSDRContext *s = ctx->priv;179 AudioSDRContext *s = ctx->priv;
142 180
143 for (int ch = 0; ch < s->channels; ch++)181 if (!strcmp(ctx->filter->name, "asdr")) {
144 av_log(ctx, AV_LOG_INFO, "SDR ch%d: %g dB\n", ch, 20. * log10(s->sum_u[ch] / s->sum_uv[ch]));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 }
145 191
146 av_frame_free(&s->cache[0]);192 av_frame_free(&s->cache[0]);
147 av_frame_free(&s->cache[1]);193 av_frame_free(&s->cache[1]);
@@ -183,3 +229,18 @@ const AVFilter ff_af_asdr = { · 35 unmodified lines
183 FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP,229 FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP,
184 AV_SAMPLE_FMT_DBLP),230 AV_SAMPLE_FMT_DBLP),
185};231};
232
233const 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
70extern const AVFilter ff_af_aperms;70extern const AVFilter ff_af_aperms;
71extern const AVFilter ff_af_aphaser;71extern const AVFilter ff_af_aphaser;
72extern const AVFilter ff_af_aphaseshift;72extern const AVFilter ff_af_aphaseshift;
73extern const AVFilter ff_af_apsnr;
73extern const AVFilter ff_af_apsyclip;74extern const AVFilter ff_af_apsyclip;
74extern const AVFilter ff_af_apulsator;75extern const AVFilter ff_af_apulsator;
75extern const AVFilter ff_af_arealtime;76extern 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