FFmpeg
af_arls.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 
21 #include "libavutil/common.h"
22 #include "libavutil/float_dsp.h"
23 #include "libavutil/opt.h"
24 
25 #include "audio.h"
26 #include "avfilter.h"
27 #include "filters.h"
28 #include "internal.h"
29 
30 enum OutModes {
37 };
38 
39 typedef struct AudioRLSContext {
40  const AVClass *class;
41 
42  int order;
43  float lambda;
44  float delta;
46 
51  AVFrame *p, *dp;
54 
56 
59 
60 #define OFFSET(x) offsetof(AudioRLSContext, x)
61 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
62 #define AT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
63 
64 static const AVOption arls_options[] = {
65  { "order", "set the filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=16}, 1, INT16_MAX, A },
66  { "lambda", "set the filter lambda", OFFSET(lambda), AV_OPT_TYPE_FLOAT, {.dbl=1.f}, 0, 1, AT },
67  { "delta", "set the filter delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=2.f}, 0, INT16_MAX, A },
68  { "out_mode", "set output mode", OFFSET(output_mode), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_OMODES-1, AT, "mode" },
69  { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AT, "mode" },
70  { "d", "desired", 0, AV_OPT_TYPE_CONST, {.i64=DESIRED_MODE}, 0, 0, AT, "mode" },
71  { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AT, "mode" },
72  { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE}, 0, 0, AT, "mode" },
73  { "e", "error", 0, AV_OPT_TYPE_CONST, {.i64=ERROR_MODE}, 0, 0, AT, "mode" },
74  { NULL }
75 };
76 
78 
79 static float fir_sample(AudioRLSContext *s, float sample, float *delay,
80  float *coeffs, float *tmp, int *offset)
81 {
82  const int order = s->order;
83  float output;
84 
85  delay[*offset] = sample;
86 
87  memcpy(tmp, coeffs + order - *offset, order * sizeof(float));
88 
89  output = s->fdsp->scalarproduct_float(delay, tmp, s->kernel_size);
90 
91  if (--(*offset) < 0)
92  *offset = order - 1;
93 
94  return output;
95 }
96 
97 static float process_sample(AudioRLSContext *s, float input, float desired, int ch)
98 {
99  float *coeffs = (float *)s->coeffs->extended_data[ch];
100  float *delay = (float *)s->delay->extended_data[ch];
101  float *gains = (float *)s->gains->extended_data[ch];
102  float *tmp = (float *)s->tmp->extended_data[ch];
103  float *u = (float *)s->u->extended_data[ch];
104  float *p = (float *)s->p->extended_data[ch];
105  float *dp = (float *)s->dp->extended_data[ch];
106  int *offsetp = (int *)s->offset->extended_data[ch];
107  const int kernel_size = s->kernel_size;
108  const int order = s->order;
109  const float lambda = s->lambda;
110  int offset = *offsetp;
111  float g = lambda;
112  float output, e;
113 
114  delay[offset + order] = input;
115 
116  output = fir_sample(s, input, delay, coeffs, tmp, offsetp);
117  e = desired - output;
118 
119  for (int i = 0, pos = offset; i < order; i++, pos++) {
120  const int ikernel_size = i * kernel_size;
121 
122  u[i] = 0.f;
123  for (int k = 0, pos = offset; k < order; k++, pos++)
124  u[i] += p[ikernel_size + k] * delay[pos];
125 
126  g += u[i] * delay[pos];
127  }
128 
129  g = 1.f / g;
130 
131  for (int i = 0; i < order; i++) {
132  const int ikernel_size = i * kernel_size;
133 
134  gains[i] = u[i] * g;
135  coeffs[i] = coeffs[order + i] = coeffs[i] + gains[i] * e;
136  tmp[i] = 0.f;
137  for (int k = 0, pos = offset; k < order; k++, pos++)
138  tmp[i] += p[ikernel_size + k] * delay[pos];
139  }
140 
141  for (int i = 0; i < order; i++) {
142  const int ikernel_size = i * kernel_size;
143 
144  for (int k = 0; k < order; k++)
145  dp[ikernel_size + k] = gains[i] * tmp[k];
146  }
147 
148  for (int i = 0; i < order; i++) {
149  const int ikernel_size = i * kernel_size;
150 
151  for (int k = 0; k < order; k++)
152  p[ikernel_size + k] = (p[ikernel_size + k] - (dp[ikernel_size + k] + dp[kernel_size * k + i]) * 0.5f) * lambda;
153  }
154 
155  switch (s->output_mode) {
156  case IN_MODE: output = input; break;
157  case DESIRED_MODE: output = desired; break;
158  case OUT_MODE: output = desired - output; break;
159  case NOISE_MODE: output = input - output; break;
160  case ERROR_MODE: break;
161  }
162  return output;
163 }
164 
165 static int process_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
166 {
167  AudioRLSContext *s = ctx->priv;
168  AVFrame *out = arg;
169  const int start = (out->ch_layout.nb_channels * jobnr) / nb_jobs;
170  const int end = (out->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
171 
172  for (int c = start; c < end; c++) {
173  const float *input = (const float *)s->frame[0]->extended_data[c];
174  const float *desired = (const float *)s->frame[1]->extended_data[c];
175  float *output = (float *)out->extended_data[c];
176 
177  for (int n = 0; n < out->nb_samples; n++) {
178  output[n] = process_sample(s, input[n], desired[n], c);
179  if (ctx->is_disabled)
180  output[n] = input[n];
181  }
182  }
183 
184  return 0;
185 }
186 
188 {
189  AudioRLSContext *s = ctx->priv;
190  int i, ret, status;
191  int nb_samples;
192  int64_t pts;
193 
195 
196  nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]),
197  ff_inlink_queued_samples(ctx->inputs[1]));
198  for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) {
199  if (s->frame[i])
200  continue;
201 
202  if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
203  ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frame[i]);
204  if (ret < 0)
205  return ret;
206  }
207  }
208 
209  if (s->frame[0] && s->frame[1]) {
210  AVFrame *out;
211 
212  out = ff_get_audio_buffer(ctx->outputs[0], s->frame[0]->nb_samples);
213  if (!out) {
214  av_frame_free(&s->frame[0]);
215  av_frame_free(&s->frame[1]);
216  return AVERROR(ENOMEM);
217  }
218 
220  FFMIN(ctx->outputs[0]->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
221 
222  out->pts = s->frame[0]->pts;
223 
224  av_frame_free(&s->frame[0]);
225  av_frame_free(&s->frame[1]);
226 
227  ret = ff_filter_frame(ctx->outputs[0], out);
228  if (ret < 0)
229  return ret;
230  }
231 
232  if (!nb_samples) {
233  for (i = 0; i < 2; i++) {
234  if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
235  ff_outlink_set_status(ctx->outputs[0], status, pts);
236  return 0;
237  }
238  }
239  }
240 
241  if (ff_outlink_frame_wanted(ctx->outputs[0])) {
242  for (i = 0; i < 2; i++) {
243  if (ff_inlink_queued_samples(ctx->inputs[i]) > 0)
244  continue;
245  ff_inlink_request_frame(ctx->inputs[i]);
246  return 0;
247  }
248  }
249  return 0;
250 }
251 
252 static int config_output(AVFilterLink *outlink)
253 {
254  AVFilterContext *ctx = outlink->src;
255  AudioRLSContext *s = ctx->priv;
256 
257  s->kernel_size = FFALIGN(s->order, 16);
258 
259  if (!s->offset)
260  s->offset = ff_get_audio_buffer(outlink, 1);
261  if (!s->delay)
262  s->delay = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
263  if (!s->coeffs)
264  s->coeffs = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
265  if (!s->gains)
266  s->gains = ff_get_audio_buffer(outlink, s->kernel_size);
267  if (!s->p)
268  s->p = ff_get_audio_buffer(outlink, s->kernel_size * s->kernel_size);
269  if (!s->dp)
270  s->dp = ff_get_audio_buffer(outlink, s->kernel_size * s->kernel_size);
271  if (!s->u)
272  s->u = ff_get_audio_buffer(outlink, s->kernel_size);
273  if (!s->tmp)
274  s->tmp = ff_get_audio_buffer(outlink, s->kernel_size);
275 
276  if (!s->delay || !s->coeffs || !s->p || !s->dp || !s->gains || !s->offset || !s->u || !s->tmp)
277  return AVERROR(ENOMEM);
278 
279  for (int ch = 0; ch < s->offset->ch_layout.nb_channels; ch++) {
280  int *dst = (int *)s->offset->extended_data[ch];
281 
282  for (int i = 0; i < s->kernel_size; i++)
283  dst[0] = s->kernel_size - 1;
284  }
285 
286  for (int ch = 0; ch < s->p->ch_layout.nb_channels; ch++) {
287  float *dst = (float *)s->p->extended_data[ch];
288 
289  for (int i = 0; i < s->kernel_size; i++)
290  dst[i * s->kernel_size + i] = s->delta;
291  }
292 
293  return 0;
294 }
295 
297 {
298  AudioRLSContext *s = ctx->priv;
299 
300  s->fdsp = avpriv_float_dsp_alloc(0);
301  if (!s->fdsp)
302  return AVERROR(ENOMEM);
303 
304  return 0;
305 }
306 
308 {
309  AudioRLSContext *s = ctx->priv;
310 
311  av_freep(&s->fdsp);
312  av_frame_free(&s->delay);
313  av_frame_free(&s->coeffs);
314  av_frame_free(&s->gains);
315  av_frame_free(&s->offset);
316  av_frame_free(&s->p);
317  av_frame_free(&s->dp);
318  av_frame_free(&s->u);
319  av_frame_free(&s->tmp);
320 }
321 
322 static const AVFilterPad inputs[] = {
323  {
324  .name = "input",
325  .type = AVMEDIA_TYPE_AUDIO,
326  },
327  {
328  .name = "desired",
329  .type = AVMEDIA_TYPE_AUDIO,
330  },
331 };
332 
333 static const AVFilterPad outputs[] = {
334  {
335  .name = "default",
336  .type = AVMEDIA_TYPE_AUDIO,
337  .config_props = config_output,
338  },
339 };
340 
342  .name = "arls",
343  .description = NULL_IF_CONFIG_SMALL("Apply Recursive Least Squares algorithm to first audio stream."),
344  .priv_size = sizeof(AudioRLSContext),
345  .priv_class = &arls_class,
346  .init = init,
347  .uninit = uninit,
348  .activate = activate,
354  .process_command = ff_filter_process_command,
355 };
IN_MODE
@ IN_MODE
Definition: af_arls.c:31
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:107
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
AudioRLSContext::u
AVFrame * u
Definition: af_arls.c:53
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
outputs
static const AVFilterPad outputs[]
Definition: af_arls.c:333
out
FILE * out
Definition: movenc.c:54
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:250
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:978
FILTER_SINGLE_SAMPLEFMT
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition: internal.h:185
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
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
arls_options
static const AVOption arls_options[]
Definition: af_arls.c:64
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
AudioRLSContext::order
int order
Definition: af_arls.c:42
AVOption
AVOption.
Definition: opt.h:251
process_sample
static float process_sample(AudioRLSContext *s, float input, float desired, int ch)
Definition: af_arls.c:97
config_output
static int config_output(AVFilterLink *outlink)
Definition: af_arls.c:252
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
AT
#define AT
Definition: af_arls.c:62
process_channels
static int process_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_arls.c:165
inputs
static const AVFilterPad inputs[]
Definition: af_arls.c:322
AudioRLSContext::coeffs
AVFrame * coeffs
Definition: af_arls.c:50
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
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_arls.c:296
pts
static int64_t pts
Definition: transcode_aac.c:643
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:47
AudioRLSContext::frame
AVFrame * frame[2]
Definition: af_arls.c:55
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:1367
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
DESIRED_MODE
@ DESIRED_MODE
Definition: af_arls.c:32
ff_inlink_request_frame
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1506
s
#define s(width, name)
Definition: cbs_vp9.c:198
NB_OMODES
@ NB_OMODES
Definition: af_arls.c:36
g
const char * g
Definition: vf_curves.c:127
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
NOISE_MODE
@ NOISE_MODE
Definition: af_arls.c:34
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:192
arg
const char * arg
Definition: jacosubdec.c:67
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:1402
AudioRLSContext::delta
float delta
Definition: af_arls.c:44
NULL
#define NULL
Definition: coverity.c:32
OFFSET
#define OFFSET(x)
Definition: af_arls.c:60
AudioRLSContext::tmp
AVFrame * tmp
Definition: af_arls.c:53
OUT_MODE
@ OUT_MODE
Definition: af_arls.c:33
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:1337
AudioRLSContext::p
AVFrame * p
Definition: af_arls.c:51
ERROR_MODE
@ ERROR_MODE
Definition: af_arls.c:35
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
float_dsp.h
OutModes
OutModes
Definition: af_afftdn.c:42
AudioRLSContext::gains
AVFrame * gains
Definition: af_arls.c:52
f
f
Definition: af_crystalizer.c:121
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
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
sample
#define sample
Definition: flacdsp_template.c:44
AVFloatDSPContext
Definition: float_dsp.h:24
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:851
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
AudioRLSContext::output_mode
int output_mode
Definition: af_arls.c:45
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
internal.h
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
ff_af_arls
const AVFilter ff_af_arls
Definition: af_arls.c:341
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AudioRLSContext::fdsp
AVFloatDSPContext * fdsp
Definition: af_arls.c:57
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:786
delta
float delta
Definition: vorbis_enc_data.h:430
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AudioRLSContext::dp
AVFrame * dp
Definition: af_arls.c:51
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:53
ff_inlink_queued_samples
int ff_inlink_queued_samples(AVFilterLink *link)
Definition: avfilter.c:1362
AVFilter
Filter definition.
Definition: avfilter.h:166
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_arls.c:307
ret
ret
Definition: filter_design.txt:187
pos
unsigned int pos
Definition: spdifenc.c:413
A
#define A
Definition: af_arls.c:61
AudioRLSContext::kernel_size
int kernel_size
Definition: af_arls.c:47
AudioRLSContext::delay
AVFrame * delay
Definition: af_arls.c:49
status
ov_status_e status
Definition: dnn_backend_openvino.c:119
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
fir_sample
static float fir_sample(AudioRLSContext *s, float sample, float *delay, float *coeffs, float *tmp, int *offset)
Definition: af_arls.c:79
AVFilterContext
An instance of a filter.
Definition: avfilter.h:397
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
AudioRLSContext::lambda
float lambda
Definition: af_arls.c:43
activate
static int activate(AVFilterContext *ctx)
Definition: af_arls.c:187
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:193
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_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:144
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(arls)
AudioRLSContext
Definition: af_arls.c:39
AudioRLSContext::offset
AVFrame * offset
Definition: af_arls.c:48