FFmpeg
af_anlms.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 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 {
38 };
39 
40 typedef struct AudioNLMSContext {
41  const AVClass *class;
42 
43  int order;
44  float mu;
45  float eps;
46  float leakage;
48 
54 
56 
57  int anlmf;
58 
61 
62 #define OFFSET(x) offsetof(AudioNLMSContext, x)
63 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
64 #define AT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
65 
66 static const AVOption anlms_options[] = {
67  { "order", "set the filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=256}, 1, INT16_MAX, A },
68  { "mu", "set the filter mu", OFFSET(mu), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 2, AT },
69  { "eps", "set the filter eps", OFFSET(eps), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AT },
70  { "leakage", "set the filter leakage", OFFSET(leakage), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, AT },
71  { "out_mode", "set output mode", OFFSET(output_mode), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_OMODES-1, AT, "mode" },
72  { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AT, "mode" },
73  { "d", "desired", 0, AV_OPT_TYPE_CONST, {.i64=DESIRED_MODE}, 0, 0, AT, "mode" },
74  { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AT, "mode" },
75  { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE}, 0, 0, AT, "mode" },
76  { NULL }
77 };
78 
79 AVFILTER_DEFINE_CLASS_EXT(anlms, "anlm(f|s)", anlms_options);
80 
82 {
83  static const enum AVSampleFormat sample_fmts[] = {
86  };
88  if (ret < 0)
89  return ret;
90 
92  if (ret < 0)
93  return ret;
94 
96 }
97 
98 static float fir_sample(AudioNLMSContext *s, float sample, float *delay,
99  float *coeffs, float *tmp, int *offset)
100 {
101  const int order = s->order;
102  float output;
103 
104  delay[*offset] = sample;
105 
106  memcpy(tmp, coeffs + order - *offset, order * sizeof(float));
107 
108  output = s->fdsp->scalarproduct_float(delay, tmp, s->kernel_size);
109 
110  if (--(*offset) < 0)
111  *offset = order - 1;
112 
113  return output;
114 }
115 
116 static float process_sample(AudioNLMSContext *s, float input, float desired,
117  float *delay, float *coeffs, float *tmp, int *offsetp)
118 {
119  const int order = s->order;
120  const float leakage = s->leakage;
121  const float mu = s->mu;
122  const float a = 1.f - leakage * mu;
123  float sum, output, e, norm, b;
124  int offset = *offsetp;
125 
126  delay[offset + order] = input;
127 
128  output = fir_sample(s, input, delay, coeffs, tmp, offsetp);
129  e = desired - output;
130 
131  sum = s->fdsp->scalarproduct_float(delay, delay, s->kernel_size);
132 
133  norm = s->eps + sum;
134  b = mu * e / norm;
135  if (s->anlmf)
136  b *= 4.f * e * e;
137 
138  memcpy(tmp, delay + offset, order * sizeof(float));
139 
140  s->fdsp->vector_fmul_scalar(coeffs, coeffs, a, s->kernel_size);
141 
142  s->fdsp->vector_fmac_scalar(coeffs, tmp, b, s->kernel_size);
143 
144  memcpy(coeffs + order, coeffs, order * sizeof(float));
145 
146  switch (s->output_mode) {
147  case IN_MODE: output = input; break;
148  case DESIRED_MODE: output = desired; break;
149  case OUT_MODE: /*output = output;*/ break;
150  case NOISE_MODE: output = desired - output; break;
151  }
152  return output;
153 }
154 
155 static int process_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
156 {
157  AudioNLMSContext *s = ctx->priv;
158  AVFrame *out = arg;
159  const int start = (out->channels * jobnr) / nb_jobs;
160  const int end = (out->channels * (jobnr+1)) / nb_jobs;
161 
162  for (int c = start; c < end; c++) {
163  const float *input = (const float *)s->frame[0]->extended_data[c];
164  const float *desired = (const float *)s->frame[1]->extended_data[c];
165  float *delay = (float *)s->delay->extended_data[c];
166  float *coeffs = (float *)s->coeffs->extended_data[c];
167  float *tmp = (float *)s->tmp->extended_data[c];
168  int *offset = (int *)s->offset->extended_data[c];
169  float *output = (float *)out->extended_data[c];
170 
171  for (int n = 0; n < out->nb_samples; n++)
172  output[n] = process_sample(s, input[n], desired[n], delay, coeffs, tmp, offset);
173  }
174 
175  return 0;
176 }
177 
179 {
180  AudioNLMSContext *s = ctx->priv;
181  int i, ret, status;
182  int nb_samples;
183  int64_t pts;
184 
186 
187  nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]),
188  ff_inlink_queued_samples(ctx->inputs[1]));
189  for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) {
190  if (s->frame[i])
191  continue;
192 
193  if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
194  ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frame[i]);
195  if (ret < 0)
196  return ret;
197  }
198  }
199 
200  if (s->frame[0] && s->frame[1]) {
201  AVFrame *out;
202 
203  out = ff_get_audio_buffer(ctx->outputs[0], s->frame[0]->nb_samples);
204  if (!out) {
205  av_frame_free(&s->frame[0]);
206  av_frame_free(&s->frame[1]);
207  return AVERROR(ENOMEM);
208  }
209 
211  FFMIN(ctx->outputs[0]->channels, ff_filter_get_nb_threads(ctx)));
212 
213  out->pts = s->frame[0]->pts;
214 
215  av_frame_free(&s->frame[0]);
216  av_frame_free(&s->frame[1]);
217 
218  ret = ff_filter_frame(ctx->outputs[0], out);
219  if (ret < 0)
220  return ret;
221  }
222 
223  if (!nb_samples) {
224  for (i = 0; i < 2; i++) {
225  if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
226  ff_outlink_set_status(ctx->outputs[0], status, pts);
227  return 0;
228  }
229  }
230  }
231 
232  if (ff_outlink_frame_wanted(ctx->outputs[0])) {
233  for (i = 0; i < 2; i++) {
234  if (ff_inlink_queued_samples(ctx->inputs[i]) > 0)
235  continue;
236  ff_inlink_request_frame(ctx->inputs[i]);
237  return 0;
238  }
239  }
240  return 0;
241 }
242 
243 static int config_output(AVFilterLink *outlink)
244 {
245  AVFilterContext *ctx = outlink->src;
246  AudioNLMSContext *s = ctx->priv;
247 
248  s->anlmf = !strcmp(ctx->filter->name, "anlmf");
249  s->kernel_size = FFALIGN(s->order, 16);
250 
251  if (!s->offset)
252  s->offset = ff_get_audio_buffer(outlink, 1);
253  if (!s->delay)
254  s->delay = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
255  if (!s->coeffs)
256  s->coeffs = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
257  if (!s->tmp)
258  s->tmp = ff_get_audio_buffer(outlink, s->kernel_size);
259  if (!s->delay || !s->coeffs || !s->offset || !s->tmp)
260  return AVERROR(ENOMEM);
261 
262  return 0;
263 }
264 
266 {
267  AudioNLMSContext *s = ctx->priv;
268 
269  s->fdsp = avpriv_float_dsp_alloc(0);
270  if (!s->fdsp)
271  return AVERROR(ENOMEM);
272 
273  return 0;
274 }
275 
277 {
278  AudioNLMSContext *s = ctx->priv;
279 
280  av_freep(&s->fdsp);
281  av_frame_free(&s->delay);
282  av_frame_free(&s->coeffs);
283  av_frame_free(&s->offset);
284  av_frame_free(&s->tmp);
285 }
286 
287 static const AVFilterPad inputs[] = {
288  {
289  .name = "input",
290  .type = AVMEDIA_TYPE_AUDIO,
291  },
292  {
293  .name = "desired",
294  .type = AVMEDIA_TYPE_AUDIO,
295  },
296 };
297 
298 static const AVFilterPad outputs[] = {
299  {
300  .name = "default",
301  .type = AVMEDIA_TYPE_AUDIO,
302  .config_props = config_output,
303  },
304 };
305 
307  .name = "anlms",
308  .description = NULL_IF_CONFIG_SMALL("Apply Normalized Least-Mean-Squares algorithm to first audio stream."),
309  .priv_size = sizeof(AudioNLMSContext),
310  .priv_class = &anlms_class,
311  .init = init,
312  .uninit = uninit,
313  .activate = activate,
318  .process_command = ff_filter_process_command,
319 };
320 
322  .name = "anlmf",
323  .description = NULL_IF_CONFIG_SMALL("Apply Normalized Least-Mean-Fourth algorithm to first audio stream."),
324  .priv_size = sizeof(AudioNLMSContext),
325  .priv_class = &anlms_class,
326  .init = init,
327  .uninit = uninit,
328  .activate = activate,
333  .process_command = ff_filter_process_command,
334 };
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:88
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
process_sample
static float process_sample(AudioNLMSContext *s, float input, float desired, float *delay, float *coeffs, float *tmp, int *offsetp)
Definition: af_anlms.c:116
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
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
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:109
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
AVOption
AVOption.
Definition: opt.h:247
b
#define b
Definition: input.c:40
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:168
OUT_MODE
@ OUT_MODE
Definition: af_anlms.c:35
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:689
AudioNLMSContext::mu
float mu
Definition: af_anlms.c:44
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_anlms.c:265
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
IN_MODE
@ IN_MODE
Definition: af_anlms.c:33
ff_af_anlms
const AVFilter ff_af_anlms
Definition: af_anlms.c:306
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
AudioNLMSContext::fdsp
AVFloatDSPContext * fdsp
Definition: af_anlms.c:59
anlms_options
static const AVOption anlms_options[]
Definition: af_anlms.c:66
pts
static int64_t pts
Definition: transcode_aac.c:653
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
OFFSET
#define OFFSET(x)
Definition: af_anlms.c:62
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:1401
av_cold
#define av_cold
Definition: attributes.h:90
AudioNLMSContext::offset
AVFrame * offset
Definition: af_anlms.c:50
DESIRED_MODE
@ DESIRED_MODE
Definition: af_anlms.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:1534
AudioNLMSContext::tmp
AVFrame * tmp
Definition: af_anlms.c:53
s
#define s(width, name)
Definition: cbs_vp9.c:257
AudioNLMSContext::frame
AVFrame * frame[2]
Definition: af_anlms.c:55
AudioNLMSContext::kernel_size
int kernel_size
Definition: af_anlms.c:49
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:705
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AudioNLMSContext::leakage
float leakage
Definition: af_anlms.c:46
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
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:1436
NULL
#define NULL
Definition: coverity.c:32
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_anlms.c:276
AudioNLMSContext::anlmf
int anlmf
Definition: af_anlms.c:57
AudioNLMSContext::eps
float eps
Definition: af_anlms.c:45
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:671
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:1371
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
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
OutModes
OutModes
Definition: af_afftdn.c:37
ff_af_anlmf
const AVFilter ff_af_anlmf
Definition: af_anlms.c:321
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_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
sample
#define sample
Definition: flacdsp_template.c:44
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_anlms.c:81
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:882
AudioNLMSContext::coeffs
AVFrame * coeffs
Definition: af_anlms.c:52
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
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
NB_OMODES
@ NB_OMODES
Definition: af_anlms.c:37
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:227
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AudioNLMSContext::order
int order
Definition: af_anlms.c:43
activate
static int activate(AVFilterContext *ctx)
Definition: af_anlms.c:178
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:803
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AudioNLMSContext::output_mode
int output_mode
Definition: af_anlms.c:47
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
ff_inlink_queued_samples
int ff_inlink_queued_samples(AVFilterLink *link)
Definition: avfilter.c:1396
AVFilter
Filter definition.
Definition: avfilter.h:165
ret
ret
Definition: filter_design.txt:187
fir_sample
static float fir_sample(AudioNLMSContext *s, float sample, float *delay, float *coeffs, float *tmp, int *offset)
Definition: af_anlms.c:98
NOISE_MODE
@ NOISE_MODE
Definition: af_anlms.c:36
AudioNLMSContext
Definition: af_anlms.c:40
AudioNLMSContext::delay
AVFrame * delay
Definition: af_anlms.c:51
channel_layout.h
inputs
static const AVFilterPad inputs[]
Definition: af_anlms.c:287
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
avfilter.h
A
#define A
Definition: af_anlms.c:63
AVFILTER_DEFINE_CLASS_EXT
AVFILTER_DEFINE_CLASS_EXT(anlms, "anlm(f|s)", anlms_options)
config_output
static int config_output(AVFilterLink *outlink)
Definition: af_anlms.c:243
process_channels
static int process_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_anlms.c:155
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
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:121
audio.h
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
avpriv_float_dsp_alloc
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
outputs
static const AVFilterPad outputs[]
Definition: af_anlms.c:298
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
AT
#define AT
Definition: af_anlms.c:64
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:143
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233