FFmpeg
af_aap.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 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/float_dsp.h"
24 #include "libavutil/opt.h"
25 
26 #include "audio.h"
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "filters.h"
30 #include "internal.h"
31 
32 enum OutModes {
39 };
40 
41 typedef struct AudioAPContext {
42  const AVClass *class;
43 
44  int order;
46  float mu;
47  float delta;
49  int precision;
50 
63 
64  void **tmpmp;
65  void **itmpmp;
66 
68 
69  int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
70 
73 
74 #define OFFSET(x) offsetof(AudioAPContext, x)
75 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
76 #define AT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
77 
78 static const AVOption aap_options[] = {
79  { "order", "set the filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=16}, 1, INT16_MAX, A },
80  { "projection", "set the filter projection", OFFSET(projection), AV_OPT_TYPE_INT, {.i64=2}, 1, 256, A },
81  { "mu", "set the filter mu", OFFSET(mu), AV_OPT_TYPE_FLOAT, {.dbl=0.0001},0,1, AT },
82  { "delta", "set the filter delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0.001},0, 1, AT },
83  { "out_mode", "set output mode", OFFSET(output_mode), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_OMODES-1, AT, .unit = "mode" },
84  { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AT, .unit = "mode" },
85  { "d", "desired", 0, AV_OPT_TYPE_CONST, {.i64=DESIRED_MODE}, 0, 0, AT, .unit = "mode" },
86  { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AT, .unit = "mode" },
87  { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE}, 0, 0, AT, .unit = "mode" },
88  { "e", "error", 0, AV_OPT_TYPE_CONST, {.i64=ERROR_MODE}, 0, 0, AT, .unit = "mode" },
89  { "precision", "set processing precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, A, .unit = "precision" },
90  { "auto", "set auto processing precision", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, .unit = "precision" },
91  { "float", "set single-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, .unit = "precision" },
92  { "double","set double-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, A, .unit = "precision" },
93  { NULL }
94 };
95 
97 
99 {
100  AudioAPContext *s = ctx->priv;
101  static const enum AVSampleFormat sample_fmts[3][3] = {
105  };
106  int ret;
107 
109  return ret;
110 
111  if ((ret = ff_set_common_formats_from_list(ctx, sample_fmts[s->precision])) < 0)
112  return ret;
113 
115 }
116 
118 {
119  AudioAPContext *s = ctx->priv;
120  int i, ret, status;
121  int nb_samples;
122  int64_t pts;
123 
125 
126  nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]),
127  ff_inlink_queued_samples(ctx->inputs[1]));
128  for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) {
129  if (s->frame[i])
130  continue;
131 
132  if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
133  ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frame[i]);
134  if (ret < 0)
135  return ret;
136  }
137  }
138 
139  if (s->frame[0] && s->frame[1]) {
140  AVFrame *out;
141 
142  out = ff_get_audio_buffer(ctx->outputs[0], s->frame[0]->nb_samples);
143  if (!out) {
144  av_frame_free(&s->frame[0]);
145  av_frame_free(&s->frame[1]);
146  return AVERROR(ENOMEM);
147  }
148 
149  ff_filter_execute(ctx, s->filter_channels, out, NULL,
150  FFMIN(ctx->outputs[0]->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
151 
152  out->pts = s->frame[0]->pts;
153  out->duration = s->frame[0]->duration;
154 
155  av_frame_free(&s->frame[0]);
156  av_frame_free(&s->frame[1]);
157 
158  ret = ff_filter_frame(ctx->outputs[0], out);
159  if (ret < 0)
160  return ret;
161  }
162 
163  if (!nb_samples) {
164  for (i = 0; i < 2; i++) {
165  if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
166  ff_outlink_set_status(ctx->outputs[0], status, pts);
167  return 0;
168  }
169  }
170  }
171 
172  if (ff_outlink_frame_wanted(ctx->outputs[0])) {
173  for (i = 0; i < 2; i++) {
174  if (s->frame[i] || ff_inlink_queued_samples(ctx->inputs[i]) > 0)
175  continue;
176  ff_inlink_request_frame(ctx->inputs[i]);
177  return 0;
178  }
179  }
180  return 0;
181 }
182 
183 #define DEPTH 32
184 #include "aap_template.c"
185 
186 #undef DEPTH
187 #define DEPTH 64
188 #include "aap_template.c"
189 
190 static int config_output(AVFilterLink *outlink)
191 {
192  const int channels = outlink->ch_layout.nb_channels;
193  AVFilterContext *ctx = outlink->src;
194  AudioAPContext *s = ctx->priv;
195 
196  s->kernel_size = FFALIGN(s->order, 16);
197 
198  if (!s->offset)
199  s->offset = ff_get_audio_buffer(outlink, 3);
200  if (!s->delay)
201  s->delay = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
202  if (!s->dcoeffs)
203  s->dcoeffs = ff_get_audio_buffer(outlink, s->kernel_size);
204  if (!s->coeffs)
205  s->coeffs = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
206  if (!s->e)
207  s->e = ff_get_audio_buffer(outlink, 2 * s->projection);
208  if (!s->p)
209  s->p = ff_get_audio_buffer(outlink, s->projection + 1);
210  if (!s->x)
211  s->x = ff_get_audio_buffer(outlink, 2 * (s->projection + s->order));
212  if (!s->w)
213  s->w = ff_get_audio_buffer(outlink, s->projection);
214  if (!s->tmp)
215  s->tmp = ff_get_audio_buffer(outlink, s->kernel_size);
216  if (!s->tmpm)
217  s->tmpm = ff_get_audio_buffer(outlink, s->projection * s->projection);
218  if (!s->itmpm)
219  s->itmpm = ff_get_audio_buffer(outlink, s->projection * s->projection);
220 
221  if (!s->tmpmp)
222  s->tmpmp = av_calloc(s->projection * channels, sizeof(*s->tmpmp));
223  if (!s->itmpmp)
224  s->itmpmp = av_calloc(s->projection * channels, sizeof(*s->itmpmp));
225 
226  if (!s->offset || !s->delay || !s->dcoeffs || !s->coeffs || !s->tmpmp || !s->itmpmp ||
227  !s->e || !s->p || !s->x || !s->w || !s->tmp || !s->tmpm || !s->itmpm)
228  return AVERROR(ENOMEM);
229 
230  switch (outlink->format) {
231  case AV_SAMPLE_FMT_DBLP:
232  for (int ch = 0; ch < channels; ch++) {
233  double *itmpm = (double *)s->itmpm->extended_data[ch];
234  double *tmpm = (double *)s->tmpm->extended_data[ch];
235  double **itmpmp = (double **)&s->itmpmp[s->projection * ch];
236  double **tmpmp = (double **)&s->tmpmp[s->projection * ch];
237 
238  for (int i = 0; i < s->projection; i++) {
239  itmpmp[i] = &itmpm[i * s->projection];
240  tmpmp[i] = &tmpm[i * s->projection];
241  }
242  }
243 
244  s->filter_channels = filter_channels_double;
245  break;
246  case AV_SAMPLE_FMT_FLTP:
247  for (int ch = 0; ch < channels; ch++) {
248  float *itmpm = (float *)s->itmpm->extended_data[ch];
249  float *tmpm = (float *)s->tmpm->extended_data[ch];
250  float **itmpmp = (float **)&s->itmpmp[s->projection * ch];
251  float **tmpmp = (float **)&s->tmpmp[s->projection * ch];
252 
253  for (int i = 0; i < s->projection; i++) {
254  itmpmp[i] = &itmpm[i * s->projection];
255  tmpmp[i] = &tmpm[i * s->projection];
256  }
257  }
258 
259  s->filter_channels = filter_channels_float;
260  break;
261  }
262 
263  return 0;
264 }
265 
267 {
268  AudioAPContext *s = ctx->priv;
269 
270  s->fdsp = avpriv_float_dsp_alloc(0);
271  if (!s->fdsp)
272  return AVERROR(ENOMEM);
273 
274  return 0;
275 }
276 
278 {
279  AudioAPContext *s = ctx->priv;
280 
281  av_freep(&s->fdsp);
282 
283  av_frame_free(&s->offset);
284  av_frame_free(&s->delay);
285  av_frame_free(&s->dcoeffs);
286  av_frame_free(&s->coeffs);
287  av_frame_free(&s->e);
288  av_frame_free(&s->p);
289  av_frame_free(&s->w);
290  av_frame_free(&s->x);
291  av_frame_free(&s->tmp);
292  av_frame_free(&s->tmpm);
293  av_frame_free(&s->itmpm);
294 
295  av_freep(&s->tmpmp);
296  av_freep(&s->itmpmp);
297 }
298 
299 static const AVFilterPad inputs[] = {
300  {
301  .name = "input",
302  .type = AVMEDIA_TYPE_AUDIO,
303  },
304  {
305  .name = "desired",
306  .type = AVMEDIA_TYPE_AUDIO,
307  },
308 };
309 
310 static const AVFilterPad outputs[] = {
311  {
312  .name = "default",
313  .type = AVMEDIA_TYPE_AUDIO,
314  .config_props = config_output,
315  },
316 };
317 
319  .name = "aap",
320  .description = NULL_IF_CONFIG_SMALL("Apply Affine Projection algorithm to first audio stream."),
321  .priv_size = sizeof(AudioAPContext),
322  .priv_class = &aap_class,
323  .init = init,
324  .uninit = uninit,
325  .activate = activate,
331  .process_command = ff_filter_process_command,
332 };
ff_get_audio_buffer
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:109
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
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
AudioAPContext::order
int order
Definition: af_aap.c:44
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1007
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:947
AudioAPContext::frame
AVFrame * frame[2]
Definition: af_aap.c:67
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
OutModes
OutModes
Definition: af_aap.c:32
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AudioAPContext::p
AVFrame * p
Definition: af_aap.c:56
AudioAPContext::tmpm
AVFrame * tmpm
Definition: af_aap.c:61
AVOption
AVOption.
Definition: opt.h:251
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:159
ff_set_common_all_samplerates
int ff_set_common_all_samplerates(AVFilterContext *ctx)
Equivalent to ff_set_common_samplerates(ctx, ff_all_samplerates())
Definition: formats.c:821
AudioAPContext::itmpmp
void ** itmpmp
Definition: af_aap.c:65
AudioAPContext::delta
float delta
Definition: af_aap.c:47
activate
static int activate(AVFilterContext *ctx)
Definition: af_aap.c:117
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
inputs
static const AVFilterPad inputs[]
Definition: af_aap.c:299
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:323
AudioAPContext::precision
int precision
Definition: af_aap.c:49
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:643
aap_options
static const AVOption aap_options[]
Definition: af_aap.c:78
AudioAPContext::filter_channels
int(* filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_aap.c:69
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
ff_inlink_check_available_samples
int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min)
Test if enough samples are available on the link.
Definition: avfilter.c:1420
av_cold
#define av_cold
Definition: attributes.h:90
ERROR_MODE
@ ERROR_MODE
Definition: af_aap.c:37
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:1564
AudioAPContext::delay
AVFrame * delay
Definition: af_aap.c:53
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ff_set_common_formats_from_list
int ff_set_common_formats_from_list(AVFilterContext *ctx, const int *fmts)
Equivalent to ff_set_common_formats(ctx, ff_make_format_list(fmts))
Definition: formats.c:873
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
channels
channels
Definition: aptx.h:31
NB_OMODES
@ NB_OMODES
Definition: af_aap.c:38
DESIRED_MODE
@ DESIRED_MODE
Definition: af_aap.c:34
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
AudioAPContext::dcoeffs
AVFrame * dcoeffs
Definition: af_aap.c:59
arg
const char * arg
Definition: jacosubdec.c:67
AudioAPContext::e
AVFrame * e
Definition: af_aap.c:55
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
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:1458
NULL
#define NULL
Definition: coverity.c:32
AudioAPContext::tmp
AVFrame * tmp
Definition: af_aap.c:60
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(aap)
AudioAPContext::w
AVFrame * w
Definition: af_aap.c:58
aap_template.c
ff_set_common_all_channel_counts
int ff_set_common_all_channel_counts(AVFilterContext *ctx)
Equivalent to ff_set_common_channel_layouts(ctx, ff_all_channel_counts())
Definition: formats.c:803
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:1386
float_dsp.h
AudioAPContext::fdsp
AVFloatDSPContext * fdsp
Definition: af_aap.c:71
AudioAPContext::tmpmp
void ** tmpmp
Definition: af_aap.c:64
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:106
config_output
static int config_output(AVFilterLink *outlink)
Definition: af_aap.c:190
AudioAPContext::offset
AVFrame * offset
Definition: af_aap.c:52
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AVFloatDSPContext
Definition: float_dsp.h:24
AudioAPContext::itmpm
AVFrame * itmpm
Definition: af_aap.c:62
ff_filter_process_command
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition: avfilter.c:879
AudioAPContext::x
AVFrame * x
Definition: af_aap.c:57
internal.h
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
A
#define A
Definition: af_aap.c:75
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_aap.c:98
OFFSET
#define OFFSET(x)
Definition: af_aap.c:74
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:814
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
delta
float delta
Definition: vorbis_enc_data.h:430
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_aap.c:266
AudioAPContext::mu
float mu
Definition: af_aap.c:46
AudioAPContext
Definition: af_aap.c:41
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:1414
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
outputs
static const AVFilterPad outputs[]
Definition: af_aap.c:310
AudioAPContext::output_mode
int output_mode
Definition: af_aap.c:48
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
NOISE_MODE
@ NOISE_MODE
Definition: af_aap.c:36
IN_MODE
@ IN_MODE
Definition: af_aap.c:33
status
ov_status_e status
Definition: dnn_backend_openvino.c:120
channel_layout.h
AT
#define AT
Definition: af_aap.c:76
OUT_MODE
@ OUT_MODE
Definition: af_aap.c:35
AudioAPContext::kernel_size
int kernel_size
Definition: af_aap.c:51
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
AVFilterContext
An instance of a filter.
Definition: avfilter.h:409
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
audio.h
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
avpriv_float_dsp_alloc
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
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
ff_af_aap
const AVFilter ff_af_aap
Definition: af_aap.c:318
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
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
AudioAPContext::coeffs
AVFrame * coeffs
Definition: af_aap.c:54
AudioAPContext::projection
int projection
Definition: af_aap.c:45
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_aap.c:277