FFmpeg
af_anlmdn.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 
21 #include <float.h>
22 
23 #include "libavutil/avassert.h"
24 #include "libavutil/audio_fifo.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/opt.h"
27 #include "avfilter.h"
28 #include "audio.h"
29 #include "formats.h"
30 
31 #include "af_anlmdndsp.h"
32 
33 #define WEIGHT_LUT_NBITS 20
34 #define WEIGHT_LUT_SIZE (1<<WEIGHT_LUT_NBITS)
35 
36 #define SQR(x) ((x) * (x))
37 
38 typedef struct AudioNLMeansContext {
39  const AVClass *class;
40 
41  float a;
42  int64_t pd;
43  int64_t rd;
44  float m;
45  int om;
46 
49 
50  int K;
51  int S;
52  int N;
53  int H;
54 
55  int offset;
58 
59  int64_t pts;
60 
62  int eof_left;
63 
66 
67 enum OutModes {
72 };
73 
74 #define OFFSET(x) offsetof(AudioNLMeansContext, x)
75 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
76 
77 static const AVOption anlmdn_options[] = {
78  { "s", "set denoising strength", OFFSET(a), AV_OPT_TYPE_FLOAT, {.dbl=0.00001},0.00001, 10, AF },
79  { "p", "set patch duration", OFFSET(pd), AV_OPT_TYPE_DURATION, {.i64=2000}, 1000, 100000, AF },
80  { "r", "set research duration", OFFSET(rd), AV_OPT_TYPE_DURATION, {.i64=6000}, 2000, 300000, AF },
81  { "o", "set output mode", OFFSET(om), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_MODES-1, AF, "mode" },
82  { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AF, "mode" },
83  { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AF, "mode" },
84  { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE},0, 0, AF, "mode" },
85  { "m", "set smooth factor", OFFSET(m), AV_OPT_TYPE_FLOAT, {.dbl=11.}, 1, 15, AF },
86  { NULL }
87 };
88 
89 AVFILTER_DEFINE_CLASS(anlmdn);
90 
92 {
95  static const enum AVSampleFormat sample_fmts[] = {
98  };
99  int ret;
100 
102  if (!formats)
103  return AVERROR(ENOMEM);
105  if (ret < 0)
106  return ret;
107 
109  if (!layouts)
110  return AVERROR(ENOMEM);
111 
113  if (ret < 0)
114  return ret;
115 
118 }
119 
120 static float compute_distance_ssd_c(const float *f1, const float *f2, ptrdiff_t K)
121 {
122  float distance = 0.;
123 
124  for (int k = -K; k <= K; k++)
125  distance += SQR(f1[k] - f2[k]);
126 
127  return distance;
128 }
129 
130 static void compute_cache_c(float *cache, const float *f,
131  ptrdiff_t S, ptrdiff_t K,
132  ptrdiff_t i, ptrdiff_t jj)
133 {
134  int v = 0;
135 
136  for (int j = jj; j < jj + S; j++, v++)
137  cache[v] += -SQR(f[i - K - 1] - f[j - K - 1]) + SQR(f[i + K] - f[j + K]);
138 }
139 
141 {
144 
145  if (ARCH_X86)
146  ff_anlmdn_init_x86(dsp);
147 }
148 
149 static int config_output(AVFilterLink *outlink)
150 {
151  AVFilterContext *ctx = outlink->src;
152  AudioNLMeansContext *s = ctx->priv;
153  int ret;
154 
155  s->K = av_rescale(s->pd, outlink->sample_rate, AV_TIME_BASE);
156  s->S = av_rescale(s->rd, outlink->sample_rate, AV_TIME_BASE);
157 
158  s->eof_left = -1;
159  s->pts = AV_NOPTS_VALUE;
160  s->H = s->K * 2 + 1;
161  s->N = s->H + (s->K + s->S) * 2;
162 
163  av_log(ctx, AV_LOG_DEBUG, "K:%d S:%d H:%d N:%d\n", s->K, s->S, s->H, s->N);
164 
165  av_frame_free(&s->in);
166  av_frame_free(&s->cache);
167  s->in = ff_get_audio_buffer(outlink, s->N);
168  if (!s->in)
169  return AVERROR(ENOMEM);
170 
171  s->cache = ff_get_audio_buffer(outlink, s->S * 2);
172  if (!s->cache)
173  return AVERROR(ENOMEM);
174 
175  s->fifo = av_audio_fifo_alloc(outlink->format, outlink->channels, s->N);
176  if (!s->fifo)
177  return AVERROR(ENOMEM);
178 
179  ret = av_audio_fifo_write(s->fifo, (void **)s->in->extended_data, s->K + s->S);
180  if (ret < 0)
181  return ret;
182 
183  s->pdiff_lut_scale = 1.f / s->m * WEIGHT_LUT_SIZE;
184  for (int i = 0; i < WEIGHT_LUT_SIZE; i++) {
185  float w = -i / s->pdiff_lut_scale;
186 
187  s->weight_lut[i] = expf(w);
188  }
189 
190  ff_anlmdn_init(&s->dsp);
191 
192  return 0;
193 }
194 
195 static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
196 {
197  AudioNLMeansContext *s = ctx->priv;
198  AVFrame *out = arg;
199  const int S = s->S;
200  const int K = s->K;
201  const int om = s->om;
202  const float *f = (const float *)(s->in->extended_data[ch]) + K;
203  float *cache = (float *)s->cache->extended_data[ch];
204  const float sw = (65536.f / (4 * K + 2)) / sqrtf(s->a);
205  float *dst = (float *)out->extended_data[ch] + s->offset;
206  const float smooth = s->m;
207 
208  for (int i = S; i < s->H + S; i++) {
209  float P = 0.f, Q = 0.f;
210  int v = 0;
211 
212  if (i == S) {
213  for (int j = i - S; j <= i + S; j++) {
214  if (i == j)
215  continue;
216  cache[v++] = s->dsp.compute_distance_ssd(f + i, f + j, K);
217  }
218  } else {
219  s->dsp.compute_cache(cache, f, S, K, i, i - S);
220  s->dsp.compute_cache(cache + S, f, S, K, i, i + 1);
221  }
222 
223  for (int j = 0; j < 2 * S && !ctx->is_disabled; j++) {
224  const float distance = cache[j];
225  unsigned weight_lut_idx;
226  float w;
227 
228  if (distance < 0.f) {
229  cache[j] = 0.f;
230  continue;
231  }
232  w = distance * sw;
233  if (w >= smooth)
234  continue;
235  weight_lut_idx = w * s->pdiff_lut_scale;
236  av_assert2(weight_lut_idx < WEIGHT_LUT_SIZE);
237  w = s->weight_lut[weight_lut_idx];
238  P += w * f[i - S + j + (j >= S)];
239  Q += w;
240  }
241 
242  P += f[i];
243  Q += 1;
244 
245  switch (om) {
246  case IN_MODE: dst[i - S] = f[i]; break;
247  case OUT_MODE: dst[i - S] = P / Q; break;
248  case NOISE_MODE: dst[i - S] = f[i] - (P / Q); break;
249  }
250  }
251 
252  return 0;
253 }
254 
256 {
257  AVFilterContext *ctx = inlink->dst;
258  AVFilterLink *outlink = ctx->outputs[0];
259  AudioNLMeansContext *s = ctx->priv;
260  AVFrame *out = NULL;
261  int available, wanted, ret;
262 
263  if (s->pts == AV_NOPTS_VALUE)
264  s->pts = in->pts;
265 
266  ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
267  in->nb_samples);
268  av_frame_free(&in);
269 
270  s->offset = 0;
271  available = av_audio_fifo_size(s->fifo);
272  wanted = (available / s->H) * s->H;
273 
274  if (wanted >= s->H && available >= s->N) {
275  out = ff_get_audio_buffer(outlink, wanted);
276  if (!out)
277  return AVERROR(ENOMEM);
278  }
279 
280  while (available >= s->N) {
281  ret = av_audio_fifo_peek(s->fifo, (void **)s->in->extended_data, s->N);
282  if (ret < 0)
283  break;
284 
285  ctx->internal->execute(ctx, filter_channel, out, NULL, inlink->channels);
286 
287  av_audio_fifo_drain(s->fifo, s->H);
288 
289  s->offset += s->H;
290  available -= s->H;
291  }
292 
293  if (out) {
294  out->pts = s->pts;
295  out->nb_samples = s->offset;
296  if (s->eof_left >= 0) {
297  out->nb_samples = FFMIN(s->eof_left, s->offset);
298  s->eof_left -= out->nb_samples;
299  }
300  s->pts += s->offset;
301 
302  return ff_filter_frame(outlink, out);
303  }
304 
305  return ret;
306 }
307 
308 static int request_frame(AVFilterLink *outlink)
309 {
310  AVFilterContext *ctx = outlink->src;
311  AudioNLMeansContext *s = ctx->priv;
312  int ret;
313 
314  ret = ff_request_frame(ctx->inputs[0]);
315 
316  if (ret == AVERROR_EOF && s->eof_left != 0) {
317  AVFrame *in;
318 
319  if (s->eof_left < 0)
320  s->eof_left = av_audio_fifo_size(s->fifo) - (s->S + s->K);
321  if (s->eof_left <= 0)
322  return AVERROR_EOF;
323  in = ff_get_audio_buffer(outlink, s->H);
324  if (!in)
325  return AVERROR(ENOMEM);
326 
327  return filter_frame(ctx->inputs[0], in);
328  }
329 
330  return ret;
331 }
332 
334 {
335  AudioNLMeansContext *s = ctx->priv;
336 
337  av_audio_fifo_free(s->fifo);
338  av_frame_free(&s->in);
339  av_frame_free(&s->cache);
340 }
341 
342 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
343  char *res, int res_len, int flags)
344 {
345  AudioNLMeansContext *s = ctx->priv;
346 
347  if (!strcmp(cmd, "s")) {
348  float a;
349 
350  if (av_sscanf(args, "%f", &a) == 1)
351  s->a = av_clipf(a, 0.00001, 10);
352  } else if (!strcmp(cmd, "o")) {
353  if (!strcmp(args, "i")) {
354  s->om = IN_MODE;
355  } else if (!strcmp(args, "o")) {
356  s->om = OUT_MODE;
357  } else if (!strcmp(args, "n")) {
358  s->om = NOISE_MODE;
359  }
360  }
361 
362  return 0;
363 }
364 
365 static const AVFilterPad inputs[] = {
366  {
367  .name = "default",
368  .type = AVMEDIA_TYPE_AUDIO,
369  .filter_frame = filter_frame,
370  },
371  { NULL }
372 };
373 
374 static const AVFilterPad outputs[] = {
375  {
376  .name = "default",
377  .type = AVMEDIA_TYPE_AUDIO,
378  .config_props = config_output,
379  .request_frame = request_frame,
380  },
381  { NULL }
382 };
383 
385  .name = "anlmdn",
386  .description = NULL_IF_CONFIG_SMALL("Reduce broadband noise from stream using Non-Local Means."),
387  .query_formats = query_formats,
388  .priv_size = sizeof(AudioNLMeansContext),
389  .priv_class = &anlmdn_class,
390  .uninit = uninit,
391  .inputs = inputs,
392  .outputs = outputs,
396 };
av_audio_fifo_free
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
formats
formats
Definition: signature.h:48
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:86
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
ff_anlmdn_init
void ff_anlmdn_init(AudioNLMDNDSPContext *dsp)
Definition: af_anlmdn.c:140
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
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_anlmdn.c:255
out
FILE * out
Definition: movenc.c:54
OUT_MODE
@ OUT_MODE
Definition: af_anlmdn.c:69
AudioNLMDNDSPContext::compute_distance_ssd
float(* compute_distance_ssd)(const float *f1, const float *f2, ptrdiff_t K)
Definition: af_anlmdndsp.h:32
ff_set_common_channel_layouts
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates.
Definition: formats.c:549
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:686
layouts
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
ch
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(INT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={ FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){ in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);} ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map) { switch(av_get_bytes_per_sample(in_fmt)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;} if(ctx->out_simd_align_mask) { int planes=out->planar ? out->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;} if(ctx->simd_f &&!ctx->ch_map &&!misaligned){ off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){ if(out->planar==in->planar){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
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:202
ff_all_channel_counts
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition.
Definition: formats.c:410
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
w
uint8_t w
Definition: llviddspenc.c:38
af_anlmdndsp.h
AVOption
AVOption.
Definition: opt.h:246
WEIGHT_LUT_SIZE
#define WEIGHT_LUT_SIZE
Definition: af_anlmdn.c:34
request_frame
static int request_frame(AVFilterLink *outlink)
Definition: af_anlmdn.c:308
AV_OPT_TYPE_DURATION
@ AV_OPT_TYPE_DURATION
Definition: opt.h:237
expf
#define expf(x)
Definition: libm.h:283
ff_request_frame
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:407
AudioNLMeansContext::N
int N
Definition: af_anlmdn.c:52
AudioNLMeansContext::S
int S
Definition: af_anlmdn.c:51
float.h
config_output
static int config_output(AVFilterLink *outlink)
Definition: af_anlmdn.c:149
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:148
AudioNLMeansContext::pdiff_lut_scale
float pdiff_lut_scale
Definition: af_anlmdn.c:47
AVFormatContext::internal
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1795
outputs
static const AVFilterPad outputs[]
Definition: af_anlmdn.c:374
AudioNLMeansContext::in
AVFrame * in
Definition: af_anlmdn.c:56
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
S
#define S(s, c, i)
Definition: flacdsp_template.c:46
AVAudioFifo
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
av_audio_fifo_drain
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:201
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
AudioNLMeansContext::om
int om
Definition: af_anlmdn.c:45
avassert.h
AF
#define AF
Definition: af_anlmdn.c:75
av_cold
#define av_cold
Definition: attributes.h:84
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:568
anlmdn_options
static const AVOption anlmdn_options[]
Definition: af_anlmdn.c:77
NOISE_MODE
@ NOISE_MODE
Definition: af_anlmdn.c:70
s
#define s(width, name)
Definition: cbs_vp9.c:257
AudioNLMeansContext::fifo
AVAudioFifo * fifo
Definition: af_anlmdn.c:61
AudioNLMeansContext::offset
int offset
Definition: af_anlmdn.c:55
AudioNLMeansContext::H
int H
Definition: af_anlmdn.c:53
av_audio_fifo_write
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:112
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ff_af_anlmdn
AVFilter ff_af_anlmdn
Definition: af_anlmdn.c:384
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
ctx
AVFormatContext * ctx
Definition: movenc.c:48
SQR
#define SQR(x)
Definition: af_anlmdn.c:36
AudioNLMeansContext
Definition: af_anlmdn.c:38
AudioNLMDNDSPContext
Definition: af_anlmdndsp.h:31
f
#define f(width, name)
Definition: cbs_vp9.c:255
arg
const char * arg
Definition: jacosubdec.c:66
if
if(ret)
Definition: filter_design.txt:179
AudioNLMeansContext::cache
AVFrame * cache
Definition: af_anlmdn.c:57
av_sscanf
int av_sscanf(const char *string, const char *format,...)
See libc sscanf manual for more information.
Definition: avsscanf.c:962
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
AudioNLMeansContext::dsp
AudioNLMDNDSPContext dsp
Definition: af_anlmdn.c:64
NULL
#define NULL
Definition: coverity.c:32
filter_channel
static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
Definition: af_anlmdn.c:195
av_audio_fifo_alloc
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:59
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(anlmdn)
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
OutModes
OutModes
Definition: af_afftdn.c:37
ff_anlmdn_init_x86
void ff_anlmdn_init_x86(AudioNLMDNDSPContext *s)
Definition: af_anlmdn_init.c:28
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:188
P
#define P
AudioNLMeansContext::m
float m
Definition: af_anlmdn.c:44
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_anlmdn.c:333
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
compute_distance_ssd_c
static float compute_distance_ssd_c(const float *f1, const float *f2, ptrdiff_t K)
Definition: af_anlmdn.c:120
AudioNLMDNDSPContext::compute_cache
void(* compute_cache)(float *cache, const float *f, ptrdiff_t S, ptrdiff_t K, ptrdiff_t i, ptrdiff_t jj)
Definition: af_anlmdndsp.h:33
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
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
process_command
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_anlmdn.c:342
AudioNLMeansContext::rd
int64_t rd
Definition: af_anlmdn.c:43
av_audio_fifo_size
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:228
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:226
AudioNLMeansContext::weight_lut
float weight_lut[WEIGHT_LUT_SIZE]
Definition: af_anlmdn.c:48
AudioNLMeansContext::pts
int64_t pts
Definition: af_anlmdn.c:59
in
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Definition: audio_convert.c:326
AudioNLMeansContext::pd
int64_t pd
Definition: af_anlmdn.c:42
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
available
if no frame is available
Definition: filter_design.txt:166
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
args
const char AVS_Value args
Definition: avisynth_c.h:873
audio_fifo.h
IN_MODE
@ IN_MODE
Definition: af_anlmdn.c:68
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
AVFilter
Filter definition.
Definition: avfilter.h:144
ret
ret
Definition: filter_design.txt:187
NB_MODES
@ NB_MODES
Definition: af_anlmdn.c:71
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_anlmdn.c:91
ff_all_samplerates
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:338
OFFSET
#define OFFSET(x)
Definition: af_anlmdn.c:74
compute_cache_c
static void compute_cache_c(float *cache, const float *f, ptrdiff_t S, ptrdiff_t K, ptrdiff_t i, ptrdiff_t jj)
Definition: af_anlmdn.c:130
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:116
audio.h
AudioNLMeansContext::a
float a
Definition: af_anlmdn.c:41
distance
static float distance(float x, float y, int band)
Definition: nellymoserenc.c:234
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:133
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ff_set_common_samplerates
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556
avstring.h
av_audio_fifo_peek
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:138
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:232
AudioNLMeansContext::eof_left
int eof_left
Definition: af_anlmdn.c:62
AudioNLMeansContext::K
int K
Definition: af_anlmdn.c:50
inputs
static const AVFilterPad inputs[]
Definition: af_anlmdn.c:365