| @@ -18,6 +18,8 @@ · 17 unmodified lines |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 18 | * 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 | |
| 28 | typedef struct AudioSDRContext { | 30 | typedef 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 |
| 67 | SDR_FILTER(fltp, float) | 71 | SDR_FILTER(fltp, float) |
| 68 | SDR_FILTER(dblp, double) | 72 | SDR_FILTER(dblp, double) |
| 69 | | 73 | |
| | 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 | |
| 70 | static int activate(AVFilterContext *ctx) | 102 | static 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 | |
| | 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 | }; |