1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-01-13 21:28:01 +02:00

avfilter: add apsnr filter

This commit is contained in:
Paul B Mahol 2023-08-13 02:57:57 +02:00
parent a1928dff2c
commit 951def850a
4 changed files with 73 additions and 3 deletions

View File

@ -2836,6 +2836,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.

View File

@ -84,6 +84,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

View File

@ -18,6 +18,8 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <float.h>
#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),
};

View File

@ -70,6 +70,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;