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 
22 #include "libavutil/common.h"
23 #include "libavutil/opt.h"
24 
25 #include "audio.h"
26 #include "avfilter.h"
27 #include "formats.h"
28 #include "filters.h"
29 #include "internal.h"
30 
31 typedef struct AudioSDRContext {
32  int channels;
33  int64_t pts;
34  double *sum_u;
35  double *sum_uv;
36 
39 
40 static void sdr(AVFilterContext *ctx, const AVFrame *u, const AVFrame *v)
41 {
42  AudioSDRContext *s = ctx->priv;
43 
44  for (int ch = 0; ch < u->ch_layout.nb_channels; ch++) {
45  const double *const us = (double *)u->extended_data[ch];
46  const double *const vs = (double *)v->extended_data[ch];
47  double sum_uv = s->sum_uv[ch];
48  double sum_u = s->sum_u[ch];
49 
50  for (int n = 0; n < u->nb_samples; n++) {
51  sum_u += us[n] * us[n];
52  sum_uv += (us[n] - vs[n]) * (us[n] - vs[n]);
53  }
54 
55  s->sum_uv[ch] = sum_uv;
56  s->sum_u[ch] = sum_u;
57  }
58 }
59 
61 {
62  AudioSDRContext *s = ctx->priv;
63  int ret, status;
64  int available;
65  int64_t pts;
66 
68 
70  if (available > 0) {
71  AVFrame *out;
72 
73  for (int i = 0; i < 2; i++) {
74  ret = ff_inlink_consume_samples(ctx->inputs[i], available, available, &s->cache[i]);
75  if (ret > 0) {
76  if (s->pts == AV_NOPTS_VALUE)
77  s->pts = s->cache[i]->pts;
78  }
79  }
80 
81  sdr(ctx, s->cache[0], s->cache[1]);
82 
83  av_frame_free(&s->cache[1]);
84  out = s->cache[0];
85  out->nb_samples = available;
86  out->pts = s->pts;
87  s->pts += available;
88  s->cache[0] = NULL;
89 
90  return ff_filter_frame(ctx->outputs[0], out);
91  }
92 
93  for (int i = 0; i < 2; i++) {
94  if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
95  ff_outlink_set_status(ctx->outputs[0], status, s->pts);
96  return 0;
97  }
98  }
99 
100  if (ff_outlink_frame_wanted(ctx->outputs[0])) {
101  for (int i = 0; i < 2; i++) {
102  if (ff_inlink_queued_samples(ctx->inputs[i]) > 0)
103  continue;
104  ff_inlink_request_frame(ctx->inputs[i]);
105  }
106  return 0;
107  }
108 
109  return FFERROR_NOT_READY;
110 }
111 
112 static int config_output(AVFilterLink *outlink)
113 {
114  AVFilterContext *ctx = outlink->src;
115  AVFilterLink *inlink = ctx->inputs[0];
116  AudioSDRContext *s = ctx->priv;
117 
118  s->pts = AV_NOPTS_VALUE;
119 
120  s->channels = inlink->ch_layout.nb_channels;
121 
122  s->sum_u = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->sum_u));
123  s->sum_uv = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->sum_uv));
124  if (!s->sum_u || !s->sum_uv)
125  return AVERROR(ENOMEM);
126 
127  return 0;
128 }
129 
131 {
132  AudioSDRContext *s = ctx->priv;
133 
134  for (int ch = 0; ch < s->channels; ch++)
135  av_log(ctx, AV_LOG_INFO, "SDR ch%d: %g dB\n", ch, 20. * log10(s->sum_u[ch] / s->sum_uv[ch]));
136 
137  av_frame_free(&s->cache[0]);
138  av_frame_free(&s->cache[1]);
139 
140  av_freep(&s->sum_u);
141  av_freep(&s->sum_uv);
142 }
143 
144 static const AVFilterPad inputs[] = {
145  {
146  .name = "input0",
147  .type = AVMEDIA_TYPE_AUDIO,
148  },
149  {
150  .name = "input1",
151  .type = AVMEDIA_TYPE_AUDIO,
152  },
153 };
154 
155 static const AVFilterPad outputs[] = {
156  {
157  .name = "default",
158  .type = AVMEDIA_TYPE_AUDIO,
159  .config_props = config_output,
160  },
161 };
162 
164  .name = "asdr",
165  .description = NULL_IF_CONFIG_SMALL("Measure Audio Signal-to-Distortion Ratio."),
166  .priv_size = sizeof(AudioSDRContext),
167  .activate = activate,
168  .uninit = uninit,
173 };
status
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
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
opt.h
out
FILE * out
Definition: movenc.c:54
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:262
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:999
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
FILTER_SINGLE_SAMPLEFMT
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition: internal.h:183
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
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:111
AudioSDRContext::sum_uv
double * sum_uv
Definition: af_asdr.c:35
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
AudioSDRContext::sum_u
double * sum_u
Definition: af_asdr.c:34
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:175
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:300
config_output
static int config_output(AVFilterLink *outlink)
Definition: af_asdr.c:112
formats.h
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
pts
static int64_t pts
Definition: transcode_aac.c:654
us
#define us(width, name, range_min, range_max, subs,...)
Definition: cbs_h2645.c:276
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
av_cold
#define av_cold
Definition: attributes.h:90
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:1511
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
activate
static int activate(AVFilterContext *ctx)
Definition: af_asdr.c:60
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:190
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:1413
NULL
#define NULL
Definition: coverity.c:32
AudioSDRContext
Definition: af_asdr.c:31
AudioSDRContext::channels
int channels
Definition: af_asdr.c:32
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:1348
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:117
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
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:269
sdr
static void sdr(AVFilterContext *ctx, const AVFrame *u, const AVFrame *v)
Definition: af_asdr.c:40
available
if no frame is available
Definition: filter_design.txt:166
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:386
AudioSDRContext::cache
AVFrame * cache[2]
Definition: af_asdr.c:37
common.h
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
ff_inlink_queued_samples
int ff_inlink_queued_samples(AVFilterLink *link)
Definition: avfilter.c:1373
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:272
outputs
static const AVFilterPad outputs[]
Definition: af_asdr.c:155
AVFilter
Filter definition.
Definition: avfilter.h:171
ret
ret
Definition: filter_design.txt:187
AudioSDRContext::pts
int64_t pts
Definition: af_asdr.c:33
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_asdr.c:130
channel_layout.h
ff_af_asdr
const AVFilter ff_af_asdr
Definition: af_asdr.c:163
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:143
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
AVFilterContext
An instance of a filter.
Definition: avfilter.h:408
audio.h
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:191
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
inputs
static const AVFilterPad inputs[]
Definition: af_asdr.c:144
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