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 #include "internal.h"
30 
31 typedef struct ChanStats {
32  double u;
33  double v;
34  double uv;
35 } ChanStats;
36 
37 typedef struct AudioSDRContext {
38  int channels;
39  uint64_t nb_samples;
40  double max;
41 
43 
45 
46  int (*filter)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
48 
49 #define SDR_FILTER(name, type) \
50 static int sdr_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)\
51 { \
52  AudioSDRContext *s = ctx->priv; \
53  AVFrame *u = s->cache[0]; \
54  AVFrame *v = s->cache[1]; \
55  const int channels = u->ch_layout.nb_channels; \
56  const int start = (channels * jobnr) / nb_jobs; \
57  const int end = (channels * (jobnr+1)) / nb_jobs; \
58  const int nb_samples = u->nb_samples; \
59  \
60  for (int ch = start; ch < end; ch++) { \
61  ChanStats *chs = &s->chs[ch]; \
62  const type *const us = (type *)u->extended_data[ch]; \
63  const type *const vs = (type *)v->extended_data[ch]; \
64  double sum_uv = 0.; \
65  double sum_u = 0.; \
66  \
67  for (int n = 0; n < nb_samples; n++) { \
68  sum_u += us[n] * us[n]; \
69  sum_uv += (us[n] - vs[n]) * (us[n] - vs[n]); \
70  } \
71  \
72  chs->uv += sum_uv; \
73  chs->u += sum_u; \
74  } \
75  \
76  return 0; \
77 }
78 
79 SDR_FILTER(fltp, float)
80 SDR_FILTER(dblp, double)
81 
82 #define SISDR_FILTER(name, type) \
83 static int sisdr_##name(AVFilterContext *ctx, void *arg,int jobnr,int nb_jobs)\
84 { \
85  AudioSDRContext *s = ctx->priv; \
86  AVFrame *u = s->cache[0]; \
87  AVFrame *v = s->cache[1]; \
88  const int channels = u->ch_layout.nb_channels; \
89  const int start = (channels * jobnr) / nb_jobs; \
90  const int end = (channels * (jobnr+1)) / nb_jobs; \
91  const int nb_samples = u->nb_samples; \
92  \
93  for (int ch = start; ch < end; ch++) { \
94  ChanStats *chs = &s->chs[ch]; \
95  const type *const us = (type *)u->extended_data[ch]; \
96  const type *const vs = (type *)v->extended_data[ch]; \
97  double sum_uv = 0.; \
98  double sum_u = 0.; \
99  double sum_v = 0.; \
100  \
101  for (int n = 0; n < nb_samples; n++) { \
102  sum_u += us[n] * us[n]; \
103  sum_v += vs[n] * vs[n]; \
104  sum_uv += us[n] * vs[n]; \
105  } \
106  \
107  chs->uv += sum_uv; \
108  chs->u += sum_u; \
109  chs->v += sum_v; \
110  } \
111  \
112  return 0; \
113 }
114 
115 SISDR_FILTER(fltp, float)
116 SISDR_FILTER(dblp, double)
117 
118 #define PSNR_FILTER(name, type) \
119 static int psnr_##name(AVFilterContext *ctx, void *arg, int jobnr,int nb_jobs)\
120 { \
121  AudioSDRContext *s = ctx->priv; \
122  AVFrame *u = s->cache[0]; \
123  AVFrame *v = s->cache[1]; \
124  const int channels = u->ch_layout.nb_channels; \
125  const int start = (channels * jobnr) / nb_jobs; \
126  const int end = (channels * (jobnr+1)) / nb_jobs; \
127  const int nb_samples = u->nb_samples; \
128  \
129  for (int ch = start; ch < end; ch++) { \
130  ChanStats *chs = &s->chs[ch]; \
131  const type *const us = (type *)u->extended_data[ch]; \
132  const type *const vs = (type *)v->extended_data[ch]; \
133  double sum_uv = 0.; \
134  \
135  for (int n = 0; n < nb_samples; n++) \
136  sum_uv += (us[n] - vs[n]) * (us[n] - vs[n]); \
137  \
138  chs->uv += sum_uv; \
139  } \
140  \
141  return 0; \
142 }
143 
144 PSNR_FILTER(fltp, float)
145 PSNR_FILTER(dblp, double)
146 
148 {
149  AudioSDRContext *s = ctx->priv;
150  AVFilterLink *outlink = ctx->outputs[0];
151  int ret, status, available;
152  int64_t pts;
153 
155 
157  if (available > 0) {
158  AVFrame *out;
159 
160  for (int i = 0; i < 2; i++) {
161  ret = ff_inlink_consume_samples(ctx->inputs[i], available, available, &s->cache[i]);
162  if (ret < 0) {
163  av_frame_free(&s->cache[0]);
164  av_frame_free(&s->cache[1]);
165  return ret;
166  }
167  }
168 
169  if (!ctx->is_disabled)
170  ff_filter_execute(ctx, s->filter, NULL, NULL,
172 
173  av_frame_free(&s->cache[1]);
174  out = s->cache[0];
175  s->cache[0] = NULL;
176 
177  s->nb_samples += available;
178  return ff_filter_frame(outlink, out);
179  }
180 
181  for (int i = 0; i < 2; i++) {
182  if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
183  ff_outlink_set_status(outlink, status, pts);
184  return 0;
185  }
186  }
187 
188  if (ff_outlink_frame_wanted(outlink)) {
189  for (int i = 0; i < 2; i++) {
190  if (s->cache[i] || ff_inlink_queued_samples(ctx->inputs[i]) > 0)
191  continue;
192  ff_inlink_request_frame(ctx->inputs[i]);
193  return 0;
194  }
195  }
196 
197  return FFERROR_NOT_READY;
198 }
199 
200 static int config_output(AVFilterLink *outlink)
201 {
202  AVFilterContext *ctx = outlink->src;
203  AVFilterLink *inlink = ctx->inputs[0];
204  AudioSDRContext *s = ctx->priv;
205 
206  s->channels = inlink->ch_layout.nb_channels;
207 
208  if (!strcmp(ctx->filter->name, "asdr"))
209  s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? sdr_fltp : sdr_dblp;
210  else if (!strcmp(ctx->filter->name, "asisdr"))
211  s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? sisdr_fltp : sisdr_dblp;
212  else
213  s->filter = inlink->format == AV_SAMPLE_FMT_FLTP ? psnr_fltp : psnr_dblp;
214  s->max = inlink->format == AV_SAMPLE_FMT_FLTP ? FLT_MAX : DBL_MAX;
215 
216  s->chs = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->chs));
217  if (!s->chs)
218  return AVERROR(ENOMEM);
219 
220  return 0;
221 }
222 
224 {
225  AudioSDRContext *s = ctx->priv;
226 
227  if (!strcmp(ctx->filter->name, "asdr")) {
228  for (int ch = 0; ch < s->channels; ch++)
229  av_log(ctx, AV_LOG_INFO, "SDR ch%d: %g dB\n", ch, 10. * log10(s->chs[ch].u / s->chs[ch].uv));
230  } else if (!strcmp(ctx->filter->name, "asisdr")) {
231  for (int ch = 0; ch < s->channels; ch++) {
232  double scale = s->chs[ch].uv / s->chs[ch].v;
233  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);
234 
235  av_log(ctx, AV_LOG_INFO, "SI-SDR ch%d: %g dB\n", ch, 10. * log10(sisdr));
236  }
237  } else {
238  for (int ch = 0; ch < s->channels; ch++) {
239  double psnr = s->chs[ch].uv > 0.0 ? 2.0 * log(s->max) - log(s->nb_samples / s->chs[ch].uv) : INFINITY;
240 
241  av_log(ctx, AV_LOG_INFO, "PSNR ch%d: %g dB\n", ch, psnr);
242  }
243  }
244 
245  av_frame_free(&s->cache[0]);
246  av_frame_free(&s->cache[1]);
247 
248  av_freep(&s->chs);
249 }
250 
251 static const AVFilterPad inputs[] = {
252  {
253  .name = "input0",
254  .type = AVMEDIA_TYPE_AUDIO,
255  },
256  {
257  .name = "input1",
258  .type = AVMEDIA_TYPE_AUDIO,
259  },
260 };
261 
262 static const AVFilterPad outputs[] = {
263  {
264  .name = "default",
265  .type = AVMEDIA_TYPE_AUDIO,
266  .config_props = config_output,
267  },
268 };
269 
271  .name = "asdr",
272  .description = NULL_IF_CONFIG_SMALL("Measure Audio Signal-to-Distortion Ratio."),
273  .priv_size = sizeof(AudioSDRContext),
274  .activate = activate,
275  .uninit = uninit,
283 };
284 
286  .name = "apsnr",
287  .description = NULL_IF_CONFIG_SMALL("Measure Audio Peak Signal-to-Noise Ratio."),
288  .priv_size = sizeof(AudioSDRContext),
289  .activate = activate,
290  .uninit = uninit,
298 };
299 
301  .name = "asisdr",
302  .description = NULL_IF_CONFIG_SMALL("Measure Audio Scale-Invariant Signal-to-Distortion Ratio."),
303  .priv_size = sizeof(AudioSDRContext),
304  .activate = activate,
305  .uninit = uninit,
313 };
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:39
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:33
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:1015
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
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:160
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
AudioSDRContext::filter
int(* filter)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_asdr.c:46
float.h
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
psnr
static double psnr(double d)
Definition: ffmpeg_enc.c:538
config_output
static int config_output(AVFilterLink *outlink)
Definition: af_asdr.c:200
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:212
AudioSDRContext::max
double max
Definition: af_asdr.c:40
pts
static int64_t pts
Definition: transcode_aac.c:644
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
SDR_FILTER
#define SDR_FILTER(name, type)
Definition: af_asdr.c:49
av_cold
#define av_cold
Definition: attributes.h:90
PSNR_FILTER
#define PSNR_FILTER(name, type)
Definition: af_asdr.c:118
ChanStats::uv
double uv
Definition: af_asdr.c:34
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:189
ff_inlink_request_frame
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1568
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:147
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
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:1462
NULL
#define NULL
Definition: coverity.c:32
AudioSDRContext
Definition: af_asdr.c:37
AudioSDRContext::channels
int channels
Definition: af_asdr.c:38
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:1389
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:191
internal.h
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:44
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:827
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ff_af_asisdr
const AVFilter ff_af_asisdr
Definition: af_asdr.c:300
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
ff_inlink_queued_samples
int ff_inlink_queued_samples(AVFilterLink *link)
Definition: avfilter.c:1417
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
outputs
static const AVFilterPad outputs[]
Definition: af_asdr.c:262
AVFilter
Filter definition.
Definition: avfilter.h:166
SISDR_FILTER
#define SISDR_FILTER(name, type)
Definition: af_asdr.c:82
ret
ret
Definition: filter_design.txt:187
fmax
double fmax(double, double)
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_asdr.c:223
status
ov_status_e status
Definition: dnn_backend_openvino.c:121
ChanStats
Definition: af_asdr.c:31
channel_layout.h
ff_af_asdr
const AVFilter ff_af_asdr
Definition: af_asdr.c:270
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:133
AudioSDRContext::chs
ChanStats * chs
Definition: af_asdr.c:42
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
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:117
mem.h
ChanStats::u
double u
Definition: af_asdr.c:32
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: intra.c:291
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
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:155
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
inputs
static const AVFilterPad inputs[]
Definition: af_asdr.c:251
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_filter_execute
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: internal.h:134
int
int
Definition: ffmpeg_filter.c:424
ff_af_apsnr
const AVFilter ff_af_apsnr
Definition: af_asdr.c:285
FILTER_SAMPLEFMTS
#define FILTER_SAMPLEFMTS(...)
Definition: internal.h:170