FFmpeg
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 
30 typedef struct ChanStats {
31  double u;
32  double v;
33  double uv;
34 } ChanStats;
35 
36 typedef struct AudioSDRContext {
37  int channels;
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) \
49 static 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 = (channels * jobnr) / nb_jobs; \
56  const int end = (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 
78 SDR_FILTER(fltp, float)
79 SDR_FILTER(dblp, double)
80 
81 #define SISDR_FILTER(name, type) \
82 static 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 = (channels * jobnr) / nb_jobs; \
89  const int end = (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 
114 SISDR_FILTER(fltp, float)
115 SISDR_FILTER(dblp, double)
116 
117 #define PSNR_FILTER(name, type) \
118 static 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 = (channels * jobnr) / nb_jobs; \
125  const int end = (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 
143 PSNR_FILTER(fltp, float)
144 PSNR_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 
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 
199 static 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 
250 static 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 
261 static const AVFilterPad outputs[] = {
262  {
263  .name = "default",
264  .type = AVMEDIA_TYPE_AUDIO,
265  .config_props = config_output,
266  },
267 };
268 
270  .name = "asdr",
271  .description = NULL_IF_CONFIG_SMALL("Measure Audio Signal-to-Distortion Ratio."),
272  .priv_size = sizeof(AudioSDRContext),
273  .activate = activate,
274  .uninit = uninit,
282 };
283 
285  .name = "apsnr",
286  .description = NULL_IF_CONFIG_SMALL("Measure Audio Peak Signal-to-Noise Ratio."),
287  .priv_size = sizeof(AudioSDRContext),
288  .activate = activate,
289  .uninit = uninit,
297 };
298 
300  .name = "asisdr",
301  .description = NULL_IF_CONFIG_SMALL("Measure Audio Scale-Invariant Signal-to-Distortion Ratio."),
302  .priv_size = sizeof(AudioSDRContext),
303  .activate = activate,
304  .uninit = uninit,
312 };
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
INFINITY
#define INFINITY
Definition: mathematics.h:118
AudioSDRContext::nb_samples
uint64_t nb_samples
Definition: af_asdr.c:38
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ChanStats::v
double v
Definition: af_asdr.c:32
out
FILE * out
Definition: movenc.c:55
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1025
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
int64_t
long long int64_t
Definition: coverity.c:34
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
normalize.log
log
Definition: normalize.py:21
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AudioSDRContext::filter
int(* filter)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_asdr.c:45
float.h
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
psnr
static double psnr(double d)
Definition: ffmpeg_enc.c:534
config_output
static int config_output(AVFilterLink *outlink)
Definition: af_asdr.c:199
FF_FILTER_FORWARD_STATUS_BACK_ALL
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition: filters.h:447
AudioSDRContext::max
double max
Definition: af_asdr.c:39
pts
static int64_t pts
Definition: transcode_aac.c:644
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
SDR_FILTER
#define SDR_FILTER(name, type)
Definition: af_asdr.c:48
av_cold
#define av_cold
Definition: attributes.h:90
PSNR_FILTER
#define PSNR_FILTER(name, type)
Definition: af_asdr.c:117
FILTER_SAMPLEFMTS
#define FILTER_SAMPLEFMTS(...)
Definition: filters.h:250
ChanStats::uv
double uv
Definition: af_asdr.c:33
ff_outlink_set_status
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:424
ff_inlink_request_frame
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1580
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
activate
static int activate(AVFilterContext *ctx)
Definition: af_asdr.c:146
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
arg
const char * arg
Definition: jacosubdec.c:67
ff_inlink_consume_samples
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:1473
NULL
#define NULL
Definition: coverity.c:32
AudioSDRContext
Definition: af_asdr.c:36
AudioSDRContext::channels
int channels
Definition: af_asdr.c:37
ff_inlink_acknowledge_status
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:1400
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:220
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
available
if no frame is available
Definition: filter_design.txt:166
AudioSDRContext::cache
AVFrame * cache[2]
Definition: af_asdr.c:43
common.h
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:836
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ff_af_asisdr
const AVFilter ff_af_asisdr
Definition: af_asdr.c:299
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
ff_inlink_queued_samples
int ff_inlink_queued_samples(AVFilterLink *link)
Definition: avfilter.c:1428
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
outputs
static const AVFilterPad outputs[]
Definition: af_asdr.c:261
AVFilter
Filter definition.
Definition: avfilter.h:201
SISDR_FILTER
#define SISDR_FILTER(name, type)
Definition: af_asdr.c:81
ret
ret
Definition: filter_design.txt:187
fmax
double fmax(double, double)
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_asdr.c:222
status
ov_status_e status
Definition: dnn_backend_openvino.c:100
ChanStats
Definition: af_asdr.c:30
channel_layout.h
ff_af_asdr
const AVFilter ff_af_asdr
Definition: af_asdr.c:269
ff_filter_execute
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: avfilter.c:1653
avfilter.h
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:168
AudioSDRContext::chs
ChanStats * chs
Definition: af_asdr.c:41
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:152
mem.h
ChanStats::u
double u
Definition: af_asdr.c:31
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: intra.c:291
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
#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:190
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
inputs
static const AVFilterPad inputs[]
Definition: af_asdr.c:250
ff_outlink_frame_wanted
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
ff_af_apsnr
const AVFilter ff_af_apsnr
Definition: af_asdr.c:284