1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/*
* Copyright (c) 2021 Paul B Mahol
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/channel_layout.h"
#include "libavutil/common.h"
#include "avfilter.h"
#include "filters.h"
#include "internal.h"
typedef struct AudioSDRContext {
int channels;
int64_t pts;
double *sum_u;
double *sum_uv;
AVFrame *cache[2];
} AudioSDRContext;
static int sdr(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 double *const us = (double *)u->extended_data[ch];
const double *const vs = (double *)v->extended_data[ch];
double sum_uv = 0.;
double sum_u = 0.;
for (int n = 0; n < nb_samples; n++) {
sum_u += us[n] * us[n];
sum_uv += (us[n] - vs[n]) * (us[n] - vs[n]);
}
s->sum_uv[ch] += sum_uv;
s->sum_u[ch] += sum_u;
}
return 0;
}
static int activate(AVFilterContext *ctx)
{
AudioSDRContext *s = ctx->priv;
AVFilterLink *outlink = ctx->outputs[0];
int ret, status;
int available;
int64_t pts;
FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
available = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]), ff_inlink_queued_samples(ctx->inputs[1]));
if (available > 0) {
AVFrame *out;
for (int i = 0; i < 2; i++) {
ret = ff_inlink_consume_samples(ctx->inputs[i], available, available, &s->cache[i]);
if (ret > 0) {
if (s->pts == AV_NOPTS_VALUE)
s->pts = s->cache[i]->pts;
}
}
if (!ctx->is_disabled)
ff_filter_execute(ctx, sdr, NULL, NULL,
FFMIN(outlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
av_frame_free(&s->cache[1]);
out = s->cache[0];
out->nb_samples = available;
out->pts = av_rescale_q(s->pts, av_make_q(1, outlink->sample_rate), outlink->time_base);
out->duration = av_rescale_q(out->nb_samples, av_make_q(1, outlink->sample_rate), outlink->time_base);
s->pts += available;
s->cache[0] = NULL;
return ff_filter_frame(outlink, out);
}
for (int i = 0; i < 2; i++) {
if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
ff_outlink_set_status(outlink, status, s->pts);
return 0;
}
}
if (ff_outlink_frame_wanted(outlink)) {
for (int i = 0; i < 2; i++) {
if (ff_inlink_queued_samples(ctx->inputs[i]) > 0)
continue;
ff_inlink_request_frame(ctx->inputs[i]);
}
return 0;
}
return FFERROR_NOT_READY;
}
static int config_output(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
AVFilterLink *inlink = ctx->inputs[0];
AudioSDRContext *s = ctx->priv;
s->pts = AV_NOPTS_VALUE;
s->channels = inlink->ch_layout.nb_channels;
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));
if (!s->sum_u || !s->sum_uv)
return AVERROR(ENOMEM);
return 0;
}
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]));
av_frame_free(&s->cache[0]);
av_frame_free(&s->cache[1]);
av_freep(&s->sum_u);
av_freep(&s->sum_uv);
}
static const AVFilterPad inputs[] = {
{
.name = "input0",
.type = AVMEDIA_TYPE_AUDIO,
},
{
.name = "input1",
.type = AVMEDIA_TYPE_AUDIO,
},
};
static const AVFilterPad outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.config_props = config_output,
},
};
const AVFilter ff_af_asdr = {
.name = "asdr",
.description = NULL_IF_CONFIG_SMALL("Measure Audio Signal-to-Distortion 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_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBLP),
};
|