FFmpeg
Loading...
Searching...
No Matches
af_asdr.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <float.h>
22
24#include "libavutil/common.h"
25#include "libavutil/mem.h"
26
27#include "avfilter.h"
28#include "filters.h"
29
30typedef struct ChanStats {
31 double u;
32 double v;
33 double uv;
34} ChanStats;
35
36typedef struct AudioSDRContext {
38 uint64_t nb_samples;
39 double max;
40
42
44
45 int (*filter)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
47
48#define SDR_FILTER(name, type) \
49static int sdr_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)\
50{ \
51 AudioSDRContext *s = ctx->priv; \
52 AVFrame *u = s->cache[0]; \
53 AVFrame *v = s->cache[1]; \
54 const int channels = u->ch_layout.nb_channels; \
55 const int start = ff_slice_pos(channels, jobnr, nb_jobs); \
56 const int end = ff_slice_pos(channels, jobnr + 1, nb_jobs); \
57 const int nb_samples = u->nb_samples; \
58 \
59 for (int ch = start; ch < end; ch++) { \
60 ChanStats *chs = &s->chs[ch]; \
61 const type *const us = (type *)u->extended_data[ch]; \
62 const type *const vs = (type *)v->extended_data[ch]; \
63 double sum_uv = 0.; \
64 double sum_u = 0.; \
65 \
66 for (int n = 0; n < nb_samples; n++) { \
67 sum_u += us[n] * us[n]; \
68 sum_uv += (us[n] - vs[n]) * (us[n] - vs[n]); \
69 } \
70 \
71 chs->uv += sum_uv; \
72 chs->u += sum_u; \
73 } \
74 \
75 return 0; \
76}
77
78SDR_FILTER(fltp, float)
79SDR_FILTER(dblp, double)
80
81#define SISDR_FILTER(name, type) \
82static int sisdr_##name(AVFilterContext *ctx, void *arg,int jobnr,int nb_jobs)\
83{ \
84 AudioSDRContext *s = ctx->priv; \
85 AVFrame *u = s->cache[0]; \
86 AVFrame *v = s->cache[1]; \
87 const int channels = u->ch_layout.nb_channels; \
88 const int start = ff_slice_pos(channels, jobnr, nb_jobs); \
89 const int end = ff_slice_pos(channels, jobnr + 1, nb_jobs); \
90 const int nb_samples = u->nb_samples; \
91 \
92 for (int ch = start; ch < end; ch++) { \
93 ChanStats *chs = &s->chs[ch]; \
94 const type *const us = (type *)u->extended_data[ch]; \
95 const type *const vs = (type *)v->extended_data[ch]; \
96 double sum_uv = 0.; \
97 double sum_u = 0.; \
98 double sum_v = 0.; \
99 \
100 for (int n = 0; n < nb_samples; n++) { \
101 sum_u += us[n] * us[n]; \
102 sum_v += vs[n] * vs[n]; \
103 sum_uv += us[n] * vs[n]; \
104 } \
105 \
106 chs->uv += sum_uv; \
107 chs->u += sum_u; \
108 chs->v += sum_v; \
109 } \
110 \
111 return 0; \
112}
113
114SISDR_FILTER(fltp, float)
115SISDR_FILTER(dblp, double)
116
117#define PSNR_FILTER(name, type) \
118static int psnr_##name(AVFilterContext *ctx, void *arg, int jobnr,int nb_jobs)\
119{ \
120 AudioSDRContext *s = ctx->priv; \
121 AVFrame *u = s->cache[0]; \
122 AVFrame *v = s->cache[1]; \
123 const int channels = u->ch_layout.nb_channels; \
124 const int start = ff_slice_pos(channels, jobnr, nb_jobs); \
125 const int end = ff_slice_pos(channels, jobnr + 1, nb_jobs); \
126 const int nb_samples = u->nb_samples; \
127 \
128 for (int ch = start; ch < end; ch++) { \
129 ChanStats *chs = &s->chs[ch]; \
130 const type *const us = (type *)u->extended_data[ch]; \
131 const type *const vs = (type *)v->extended_data[ch]; \
132 double sum_uv = 0.; \
133 \
134 for (int n = 0; n < nb_samples; n++) \
135 sum_uv += (us[n] - vs[n]) * (us[n] - vs[n]); \
136 \
137 chs->uv += sum_uv; \
138 } \
139 \
140 return 0; \
141}
142
143PSNR_FILTER(fltp, float)
144PSNR_FILTER(dblp, double)
145
147{
148 AudioSDRContext *s = ctx->priv;
149 AVFilterLink *outlink = ctx->outputs[0];
150 int ret, status, available;
151 int64_t pts;
152
154
155 available = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]), ff_inlink_queued_samples(ctx->inputs[1]));
156 if (available > 0) {
157 AVFrame *out;
158
159 for (int i = 0; i < 2; i++) {
160 ret = ff_inlink_consume_samples(ctx->inputs[i], available, available, &s->cache[i]);
161 if (ret < 0) {
162 av_frame_free(&s->cache[0]);
163 av_frame_free(&s->cache[1]);
164 return ret;
165 }
166 }
167
168 if (!ctx->is_disabled)
169 ff_filter_execute(ctx, s->filter, NULL, NULL,
171
172 av_frame_free(&s->cache[1]);
173 out = s->cache[0];
174 s->cache[0] = NULL;
175
176 s->nb_samples += available;
177 return ff_filter_frame(outlink, out);
178 }
179
180 for (int i = 0; i < 2; i++) {
181 if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
182 ff_outlink_set_status(outlink, status, pts);
183 return 0;
184 }
185 }
186
187 if (ff_outlink_frame_wanted(outlink)) {
188 for (int i = 0; i < 2; i++) {
189 if (s->cache[i] || ff_inlink_queued_samples(ctx->inputs[i]) > 0)
190 continue;
191 ff_inlink_request_frame(ctx->inputs[i]);
192 return 0;
193 }
194 }
195
196 return FFERROR_NOT_READY;
197}
198
199static int config_output(AVFilterLink *outlink)
200{
201 AVFilterContext *ctx = outlink->src;
202 AVFilterLink *inlink = ctx->inputs[0];
203 AudioSDRContext *s = ctx->priv;
204
205 s->channels = inlink->ch_layout.nb_channels;
206
207 if (!strcmp(ctx->filter->name, "asdr"))
208 s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? sdr_fltp : sdr_dblp;
209 else if (!strcmp(ctx->filter->name, "asisdr"))
210 s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? sisdr_fltp : sisdr_dblp;
211 else
212 s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? psnr_fltp : psnr_dblp;
213 s->max = inlink->format == AV_SAMPLE_FMT_FLTP ? FLT_MAX : DBL_MAX;
214
215 s->chs = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->chs));
216 if (!s->chs)
217 return AVERROR(ENOMEM);
218
219 return 0;
220}
221
223{
224 AudioSDRContext *s = ctx->priv;
225
226 if (!strcmp(ctx->filter->name, "asdr")) {
227 for (int ch = 0; ch < s->channels; ch++)
228 av_log(ctx, AV_LOG_INFO, "SDR ch%d: %g dB\n", ch, 10. * log10(s->chs[ch].u / s->chs[ch].uv));
229 } else if (!strcmp(ctx->filter->name, "asisdr")) {
230 for (int ch = 0; ch < s->channels; ch++) {
231 double scale = s->chs[ch].uv / s->chs[ch].v;
232 double sisdr = scale * scale * s->chs[ch].v / fmax(0., s->chs[ch].u + scale*scale*s->chs[ch].v - 2.0*scale*s->chs[ch].uv);
233
234 av_log(ctx, AV_LOG_INFO, "SI-SDR ch%d: %g dB\n", ch, 10. * log10(sisdr));
235 }
236 } else {
237 for (int ch = 0; ch < s->channels; ch++) {
238 double psnr = s->chs[ch].uv > 0.0 ? 2.0 * log(s->max) - log(s->nb_samples / s->chs[ch].uv) : INFINITY;
239
240 av_log(ctx, AV_LOG_INFO, "PSNR ch%d: %g dB\n", ch, psnr);
241 }
242 }
243
244 av_frame_free(&s->cache[0]);
245 av_frame_free(&s->cache[1]);
246
247 av_freep(&s->chs);
248}
249
250static const AVFilterPad inputs[] = {
251 {
252 .name = "input0",
253 .type = AVMEDIA_TYPE_AUDIO,
254 },
255 {
256 .name = "input1",
257 .type = AVMEDIA_TYPE_AUDIO,
258 },
259};
260
261static const AVFilterPad outputs[] = {
262 {
263 .name = "default",
264 .type = AVMEDIA_TYPE_AUDIO,
265 .config_props = config_output,
266 },
267};
268
270 .p.name = "asdr",
271 .p.description = NULL_IF_CONFIG_SMALL("Measure Audio Signal-to-Distortion Ratio."),
272 .p.flags = AVFILTER_FLAG_METADATA_ONLY |
275 .priv_size = sizeof(AudioSDRContext),
277 .uninit = uninit,
282};
283
285 .p.name = "apsnr",
286 .p.description = NULL_IF_CONFIG_SMALL("Measure Audio Peak Signal-to-Noise Ratio."),
287 .p.flags = AVFILTER_FLAG_METADATA_ONLY |
290 .priv_size = sizeof(AudioSDRContext),
292 .uninit = uninit,
297};
298
300 .p.name = "asisdr",
301 .p.description = NULL_IF_CONFIG_SMALL("Measure Audio Scale-Invariant Signal-to-Distortion Ratio."),
302 .p.flags = AVFILTER_FLAG_METADATA_ONLY |
305 .priv_size = sizeof(AudioSDRContext),
307 .uninit = uninit,
312};
static const AVFilterPad inputs[]
Definition af_aap.c:299
static const AVFilterPad outputs[]
Definition af_aap.c:310
const FFFilter ff_af_apsnr
Definition af_asdr.c:284
const FFFilter ff_af_asdr
Definition af_asdr.c:269
static av_cold void uninit(AVFilterContext *ctx)
Definition af_asdr.c:222
#define SISDR_FILTER(name, type)
Definition af_asdr.c:81
#define SDR_FILTER(name, type)
Definition af_asdr.c:48
static int config_output(AVFilterLink *outlink)
Definition af_asdr.c:199
#define PSNR_FILTER(name, type)
Definition af_asdr.c:117
const FFFilter ff_af_asisdr
Definition af_asdr.c:299
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition avfilter.c:1467
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition avfilter.c:1690
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
Definition avfilter.c:1540
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
int ff_inlink_queued_samples(AVFilterLink *link)
Definition avfilter.c:1495
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition avfilter.c:1623
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
common internal and external API header
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
double fmax(double, double)
static double psnr(double d)
Definition ffmpeg_enc.c:624
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition avfilter.h:204
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition avfilter.h:182
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
#define AV_LOG_INFO
Standard information.
Definition log.h:221
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition samplefmt.h:67
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_SAMPLEFMTS(...)
Definition filters.h:252
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition filters.h:629
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition filters.h:652
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
#define INFINITY
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
int nb_channels
Number of channels in this layout.
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVFrame * cache[2]
Definition af_asdr.c:43
uint64_t nb_samples
Definition af_asdr.c:38
ChanStats * chs
Definition af_asdr.c:41
int(* filter)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition af_asdr.c:45
double u
Definition af_asdr.c:31
double v
Definition af_asdr.c:32
double uv
Definition af_asdr.c:33
#define av_freep(p)
#define av_log(a,...)
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static int64_t pts