FFmpeg  4.0
af_afir.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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 /**
22  * @file
23  * An arbitrary audio FIR filter
24  */
25 
26 #include "libavutil/audio_fifo.h"
27 #include "libavutil/common.h"
28 #include "libavutil/float_dsp.h"
29 #include "libavutil/opt.h"
30 #include "libavcodec/avfft.h"
31 
32 #include "audio.h"
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "internal.h"
36 #include "af_afir.h"
37 
38 static void fcmul_add_c(float *sum, const float *t, const float *c, ptrdiff_t len)
39 {
40  int n;
41 
42  for (n = 0; n < len; n++) {
43  const float cre = c[2 * n ];
44  const float cim = c[2 * n + 1];
45  const float tre = t[2 * n ];
46  const float tim = t[2 * n + 1];
47 
48  sum[2 * n ] += tre * cre - tim * cim;
49  sum[2 * n + 1] += tre * cim + tim * cre;
50  }
51 
52  sum[2 * n] += t[2 * n] * c[2 * n];
53 }
54 
55 static int fir_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
56 {
57  AudioFIRContext *s = ctx->priv;
58  const float *src = (const float *)s->in[0]->extended_data[ch];
59  int index1 = (s->index + 1) % 3;
60  int index2 = (s->index + 2) % 3;
61  float *sum = s->sum[ch];
62  AVFrame *out = arg;
63  float *block;
64  float *dst;
65  int n, i, j;
66 
67  memset(sum, 0, sizeof(*sum) * s->fft_length);
68  block = s->block[ch] + s->part_index * s->block_size;
69  memset(block, 0, sizeof(*block) * s->fft_length);
70 
71  s->fdsp->vector_fmul_scalar(block + s->part_size, src, s->dry_gain, FFALIGN(s->nb_samples, 4));
72  emms_c();
73 
74  av_rdft_calc(s->rdft[ch], block);
75  block[2 * s->part_size] = block[1];
76  block[1] = 0;
77 
78  j = s->part_index;
79 
80  for (i = 0; i < s->nb_partitions; i++) {
81  const int coffset = i * s->coeff_size;
82  const FFTComplex *coeff = s->coeff[ch * !s->one2many] + coffset;
83 
84  block = s->block[ch] + j * s->block_size;
85  s->fcmul_add(sum, block, (const float *)coeff, s->part_size);
86 
87  if (j == 0)
88  j = s->nb_partitions;
89  j--;
90  }
91 
92  sum[1] = sum[2 * s->part_size];
93  av_rdft_calc(s->irdft[ch], sum);
94 
95  dst = (float *)s->buffer->extended_data[ch] + index1 * s->part_size;
96  for (n = 0; n < s->part_size; n++) {
97  dst[n] += sum[n];
98  }
99 
100  dst = (float *)s->buffer->extended_data[ch] + index2 * s->part_size;
101 
102  memcpy(dst, sum + s->part_size, s->part_size * sizeof(*dst));
103 
104  dst = (float *)s->buffer->extended_data[ch] + s->index * s->part_size;
105 
106  if (out) {
107  float *ptr = (float *)out->extended_data[ch];
108  s->fdsp->vector_fmul_scalar(ptr, dst, s->wet_gain, FFALIGN(out->nb_samples, 4));
109  emms_c();
110  }
111 
112  return 0;
113 }
114 
115 static int fir_frame(AudioFIRContext *s, AVFilterLink *outlink)
116 {
117  AVFilterContext *ctx = outlink->src;
118  AVFrame *out = NULL;
119  int ret;
120 
122 
123  if (!s->want_skip) {
124  out = ff_get_audio_buffer(outlink, s->nb_samples);
125  if (!out)
126  return AVERROR(ENOMEM);
127  }
128 
129  s->in[0] = ff_get_audio_buffer(ctx->inputs[0], s->nb_samples);
130  if (!s->in[0]) {
131  av_frame_free(&out);
132  return AVERROR(ENOMEM);
133  }
134 
135  av_audio_fifo_peek(s->fifo[0], (void **)s->in[0]->extended_data, s->nb_samples);
136 
137  ctx->internal->execute(ctx, fir_channel, out, NULL, outlink->channels);
138 
139  s->part_index = (s->part_index + 1) % s->nb_partitions;
140 
142 
143  if (!s->want_skip) {
144  out->pts = s->pts;
145  if (s->pts != AV_NOPTS_VALUE)
146  s->pts += av_rescale_q(out->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
147  }
148 
149  s->index++;
150  if (s->index == 3)
151  s->index = 0;
152 
153  av_frame_free(&s->in[0]);
154 
155  if (s->want_skip == 1) {
156  s->want_skip = 0;
157  ret = 0;
158  } else {
159  ret = ff_filter_frame(outlink, out);
160  }
161 
162  return ret;
163 }
164 
166 {
167  AudioFIRContext *s = ctx->priv;
168  int i, ch, n, N;
169 
170  s->nb_taps = av_audio_fifo_size(s->fifo[1]);
171  if (s->nb_taps <= 0)
172  return AVERROR(EINVAL);
173 
174  for (n = 4; (1 << n) < s->nb_taps; n++);
175  N = FFMIN(n, 16);
176  s->ir_length = 1 << n;
177  s->fft_length = (1 << (N + 1)) + 1;
178  s->part_size = 1 << (N - 1);
179  s->block_size = FFALIGN(s->fft_length, 32);
180  s->coeff_size = FFALIGN(s->part_size + 1, 32);
181  s->nb_partitions = (s->nb_taps + s->part_size - 1) / s->part_size;
182  s->nb_coeffs = s->ir_length + s->nb_partitions;
183 
184  for (ch = 0; ch < ctx->inputs[0]->channels; ch++) {
185  s->sum[ch] = av_calloc(s->fft_length, sizeof(**s->sum));
186  if (!s->sum[ch])
187  return AVERROR(ENOMEM);
188  }
189 
190  for (ch = 0; ch < ctx->inputs[1]->channels; ch++) {
191  s->coeff[ch] = av_calloc(s->nb_partitions * s->coeff_size, sizeof(**s->coeff));
192  if (!s->coeff[ch])
193  return AVERROR(ENOMEM);
194  }
195 
196  for (ch = 0; ch < ctx->inputs[0]->channels; ch++) {
197  s->block[ch] = av_calloc(s->nb_partitions * s->block_size, sizeof(**s->block));
198  if (!s->block[ch])
199  return AVERROR(ENOMEM);
200  }
201 
202  for (ch = 0; ch < ctx->inputs[0]->channels; ch++) {
203  s->rdft[ch] = av_rdft_init(N, DFT_R2C);
204  s->irdft[ch] = av_rdft_init(N, IDFT_C2R);
205  if (!s->rdft[ch] || !s->irdft[ch])
206  return AVERROR(ENOMEM);
207  }
208 
209  s->in[1] = ff_get_audio_buffer(ctx->inputs[1], s->nb_taps);
210  if (!s->in[1])
211  return AVERROR(ENOMEM);
212 
213  s->buffer = ff_get_audio_buffer(ctx->inputs[0], s->part_size * 3);
214  if (!s->buffer)
215  return AVERROR(ENOMEM);
216 
217  av_audio_fifo_read(s->fifo[1], (void **)s->in[1]->extended_data, s->nb_taps);
218 
219  if (s->again) {
220  float power = 0;
221 
222  for (ch = 0; ch < ctx->inputs[1]->channels; ch++) {
223  float *time = (float *)s->in[1]->extended_data[!s->one2many * ch];
224 
225  for (i = 0; i < s->nb_taps; i++)
226  power += FFABS(time[i]);
227  }
228 
229  s->gain = sqrtf(1.f / (ctx->inputs[1]->channels * power)) / (sqrtf(ctx->inputs[1]->channels));
230  for (ch = 0; ch < ctx->inputs[1]->channels; ch++) {
231  float *time = (float *)s->in[1]->extended_data[!s->one2many * ch];
232 
233  s->fdsp->vector_fmul_scalar(time, time, s->gain, FFALIGN(s->nb_taps, 4));
234  }
235  }
236 
237  for (ch = 0; ch < ctx->inputs[1]->channels; ch++) {
238  float *time = (float *)s->in[1]->extended_data[!s->one2many * ch];
239  float *block = s->block[ch];
240  FFTComplex *coeff = s->coeff[ch];
241 
242  for (i = FFMAX(1, s->length * s->nb_taps); i < s->nb_taps; i++)
243  time[i] = 0;
244 
245  for (i = 0; i < s->nb_partitions; i++) {
246  const float scale = 1.f / s->part_size;
247  const int toffset = i * s->part_size;
248  const int coffset = i * s->coeff_size;
249  const int boffset = s->part_size;
250  const int remaining = s->nb_taps - (i * s->part_size);
251  const int size = remaining >= s->part_size ? s->part_size : remaining;
252 
253  memset(block, 0, sizeof(*block) * s->fft_length);
254  memcpy(block + boffset, time + toffset, size * sizeof(*block));
255 
256  av_rdft_calc(s->rdft[0], block);
257 
258  coeff[coffset].re = block[0] * scale;
259  coeff[coffset].im = 0;
260  for (n = 1; n < s->part_size; n++) {
261  coeff[coffset + n].re = block[2 * n] * scale;
262  coeff[coffset + n].im = block[2 * n + 1] * scale;
263  }
264  coeff[coffset + s->part_size].re = block[1] * scale;
265  coeff[coffset + s->part_size].im = 0;
266  }
267  }
268 
269  av_frame_free(&s->in[1]);
270  av_log(ctx, AV_LOG_DEBUG, "nb_taps: %d\n", s->nb_taps);
271  av_log(ctx, AV_LOG_DEBUG, "nb_partitions: %d\n", s->nb_partitions);
272  av_log(ctx, AV_LOG_DEBUG, "partition size: %d\n", s->part_size);
273  av_log(ctx, AV_LOG_DEBUG, "ir_length: %d\n", s->ir_length);
274 
275  s->have_coeffs = 1;
276 
277  return 0;
278 }
279 
280 static int read_ir(AVFilterLink *link, AVFrame *frame)
281 {
282  AVFilterContext *ctx = link->dst;
283  AudioFIRContext *s = ctx->priv;
284  int nb_taps, max_nb_taps, ret;
285 
286  ret = av_audio_fifo_write(s->fifo[1], (void **)frame->extended_data,
287  frame->nb_samples);
288  av_frame_free(&frame);
289  if (ret < 0)
290  return ret;
291 
292  nb_taps = av_audio_fifo_size(s->fifo[1]);
293  max_nb_taps = MAX_IR_DURATION * ctx->outputs[0]->sample_rate;
294  if (nb_taps > max_nb_taps) {
295  av_log(ctx, AV_LOG_ERROR, "Too big number of coefficients: %d > %d.\n", nb_taps, max_nb_taps);
296  return AVERROR(EINVAL);
297  }
298 
299  return 0;
300 }
301 
303 {
304  AVFilterContext *ctx = link->dst;
305  AudioFIRContext *s = ctx->priv;
306  AVFilterLink *outlink = ctx->outputs[0];
307  int ret;
308 
309  ret = av_audio_fifo_write(s->fifo[0], (void **)frame->extended_data,
310  frame->nb_samples);
311  if (ret > 0 && s->pts == AV_NOPTS_VALUE)
312  s->pts = frame->pts;
313 
314  av_frame_free(&frame);
315 
316  if (ret < 0)
317  return ret;
318 
319  if (!s->have_coeffs && s->eof_coeffs) {
320  ret = convert_coeffs(ctx);
321  if (ret < 0)
322  return ret;
323  }
324 
325  if (s->have_coeffs) {
326  while (av_audio_fifo_size(s->fifo[0]) >= s->part_size) {
327  ret = fir_frame(s, outlink);
328  if (ret < 0)
329  return ret;
330  }
331  }
332  return 0;
333 }
334 
335 static int request_frame(AVFilterLink *outlink)
336 {
337  AVFilterContext *ctx = outlink->src;
338  AudioFIRContext *s = ctx->priv;
339  int ret;
340 
341  if (!s->eof_coeffs) {
342  ret = ff_request_frame(ctx->inputs[1]);
343  if (ret == AVERROR_EOF) {
344  s->eof_coeffs = 1;
345  ret = 0;
346  }
347  return ret;
348  }
349  ret = ff_request_frame(ctx->inputs[0]);
350  if (ret == AVERROR_EOF && s->have_coeffs) {
351  if (s->need_padding) {
352  AVFrame *silence = ff_get_audio_buffer(outlink, s->part_size);
353 
354  if (!silence)
355  return AVERROR(ENOMEM);
356  ret = av_audio_fifo_write(s->fifo[0], (void **)silence->extended_data,
357  silence->nb_samples);
358  av_frame_free(&silence);
359  if (ret < 0)
360  return ret;
361  s->need_padding = 0;
362  }
363 
364  while (av_audio_fifo_size(s->fifo[0]) > 0) {
365  ret = fir_frame(s, outlink);
366  if (ret < 0)
367  return ret;
368  }
369  ret = AVERROR_EOF;
370  }
371  return ret;
372 }
373 
375 {
378  static const enum AVSampleFormat sample_fmts[] = {
381  };
382  int ret, i;
383 
384  layouts = ff_all_channel_counts();
385  if ((ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
386  return ret;
387 
388  for (i = 0; i < 2; i++) {
389  layouts = ff_all_channel_counts();
390  if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
391  return ret;
392  }
393 
394  formats = ff_make_format_list(sample_fmts);
395  if ((ret = ff_set_common_formats(ctx, formats)) < 0)
396  return ret;
397 
398  formats = ff_all_samplerates();
399  return ff_set_common_samplerates(ctx, formats);
400 }
401 
402 static int config_output(AVFilterLink *outlink)
403 {
404  AVFilterContext *ctx = outlink->src;
405  AudioFIRContext *s = ctx->priv;
406 
407  if (ctx->inputs[0]->channels != ctx->inputs[1]->channels &&
408  ctx->inputs[1]->channels != 1) {
409  av_log(ctx, AV_LOG_ERROR,
410  "Second input must have same number of channels as first input or "
411  "exactly 1 channel.\n");
412  return AVERROR(EINVAL);
413  }
414 
415  s->one2many = ctx->inputs[1]->channels == 1;
416  outlink->sample_rate = ctx->inputs[0]->sample_rate;
417  outlink->time_base = ctx->inputs[0]->time_base;
418  outlink->channel_layout = ctx->inputs[0]->channel_layout;
419  outlink->channels = ctx->inputs[0]->channels;
420 
421  s->fifo[0] = av_audio_fifo_alloc(ctx->inputs[0]->format, ctx->inputs[0]->channels, 1024);
422  s->fifo[1] = av_audio_fifo_alloc(ctx->inputs[1]->format, ctx->inputs[1]->channels, 1024);
423  if (!s->fifo[0] || !s->fifo[1])
424  return AVERROR(ENOMEM);
425 
426  s->sum = av_calloc(outlink->channels, sizeof(*s->sum));
427  s->coeff = av_calloc(ctx->inputs[1]->channels, sizeof(*s->coeff));
428  s->block = av_calloc(ctx->inputs[0]->channels, sizeof(*s->block));
429  s->rdft = av_calloc(outlink->channels, sizeof(*s->rdft));
430  s->irdft = av_calloc(outlink->channels, sizeof(*s->irdft));
431  if (!s->sum || !s->coeff || !s->block || !s->rdft || !s->irdft)
432  return AVERROR(ENOMEM);
433 
434  s->nb_channels = outlink->channels;
435  s->nb_coef_channels = ctx->inputs[1]->channels;
436  s->want_skip = 1;
437  s->need_padding = 1;
438  s->pts = AV_NOPTS_VALUE;
439 
440  return 0;
441 }
442 
444 {
445  AudioFIRContext *s = ctx->priv;
446  int ch;
447 
448  if (s->sum) {
449  for (ch = 0; ch < s->nb_channels; ch++) {
450  av_freep(&s->sum[ch]);
451  }
452  }
453  av_freep(&s->sum);
454 
455  if (s->coeff) {
456  for (ch = 0; ch < s->nb_coef_channels; ch++) {
457  av_freep(&s->coeff[ch]);
458  }
459  }
460  av_freep(&s->coeff);
461 
462  if (s->block) {
463  for (ch = 0; ch < s->nb_channels; ch++) {
464  av_freep(&s->block[ch]);
465  }
466  }
467  av_freep(&s->block);
468 
469  if (s->rdft) {
470  for (ch = 0; ch < s->nb_channels; ch++) {
471  av_rdft_end(s->rdft[ch]);
472  }
473  }
474  av_freep(&s->rdft);
475 
476  if (s->irdft) {
477  for (ch = 0; ch < s->nb_channels; ch++) {
478  av_rdft_end(s->irdft[ch]);
479  }
480  }
481  av_freep(&s->irdft);
482 
483  av_frame_free(&s->in[0]);
484  av_frame_free(&s->in[1]);
485  av_frame_free(&s->buffer);
486 
487  av_audio_fifo_free(s->fifo[0]);
488  av_audio_fifo_free(s->fifo[1]);
489 
490  av_freep(&s->fdsp);
491 }
492 
494 {
495  AudioFIRContext *s = ctx->priv;
496 
497  s->fcmul_add = fcmul_add_c;
498 
500  if (!s->fdsp)
501  return AVERROR(ENOMEM);
502 
503  if (ARCH_X86)
504  ff_afir_init_x86(s);
505 
506  return 0;
507 }
508 
509 static const AVFilterPad afir_inputs[] = {
510  {
511  .name = "main",
512  .type = AVMEDIA_TYPE_AUDIO,
513  .filter_frame = filter_frame,
514  },{
515  .name = "ir",
516  .type = AVMEDIA_TYPE_AUDIO,
517  .filter_frame = read_ir,
518  },
519  { NULL }
520 };
521 
522 static const AVFilterPad afir_outputs[] = {
523  {
524  .name = "default",
525  .type = AVMEDIA_TYPE_AUDIO,
526  .config_props = config_output,
527  .request_frame = request_frame,
528  },
529  { NULL }
530 };
531 
532 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
533 #define OFFSET(x) offsetof(AudioFIRContext, x)
534 
535 static const AVOption afir_options[] = {
536  { "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AF },
537  { "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AF },
538  { "length", "set IR length", OFFSET(length), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AF },
539  { "again", "enable auto gain", OFFSET(again), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AF },
540  { NULL }
541 };
542 
544 
546  .name = "afir",
547  .description = NULL_IF_CONFIG_SMALL("Apply Finite Impulse Response filter with supplied coefficients in 2nd stream."),
548  .priv_size = sizeof(AudioFIRContext),
549  .priv_class = &afir_class,
551  .init = init,
552  .uninit = uninit,
553  .inputs = afir_inputs,
554  .outputs = afir_outputs,
556 };
float, planar
Definition: samplefmt.h:69
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
AVFrame * in[2]
Definition: af_afir.h:71
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:59
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:181
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
AVOption.
Definition: opt.h:246
int nb_coef_channels
Definition: af_afir.h:59
int nb_channels
Definition: af_afir.h:56
Main libavfilter public API header.
static const AVOption afir_options[]
Definition: af_afir.c:535
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
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 &&HAVE_MMX) 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
static int convert_coeffs(AVFilterContext *ctx)
Definition: af_afir.c:165
RDFTContext ** irdft
Definition: af_afir.h:65
#define src
Definition: vp8dsp.c:254
#define N
Definition: af_mcompand.c:54
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:244
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
static int config_output(AVFilterLink *outlink)
Definition: af_afir.c:402
int block_size
Definition: af_afir.h:54
static int16_t block[64]
Definition: dct.c:115
const char * name
Pad name.
Definition: internal.h:60
static int fir_frame(AudioFIRContext *s, AVFilterLink *outlink)
Definition: af_afir.c:115
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int eof_coeffs
Definition: af_afir.h:47
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:435
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
#define av_cold
Definition: attributes.h:82
float dry_gain
Definition: af_afir.h:41
AVOptions.
int coeff_size
Definition: af_afir.h:53
int fft_length
Definition: af_afir.h:58
#define emms_c()
Definition: internal.h:55
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:311
static AVFrame * frame
void(* fcmul_add)(float *sum, const float *t, const float *c, ptrdiff_t len)
Definition: af_afir.h:77
static int flags
Definition: log.c:55
static av_cold int init(AVFilterContext *ctx)
Definition: af_afir.c:493
#define AVERROR_EOF
End of file.
Definition: error.h:55
void ff_afir_init_x86(AudioFIRContext *s)
Definition: af_afir_init.c:28
static int read_ir(AVFilterLink *link, AVFrame *frame)
Definition: af_afir.c:280
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
static int request_frame(AVFilterLink *outlink)
Definition: af_afir.c:335
static int query_formats(AVFilterContext *ctx)
Definition: af_afir.c:374
A filter pad used for either input or output.
Definition: internal.h:54
RDFTContext ** rdft
Definition: af_afir.h:65
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:127
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
#define ARCH_X86
Definition: config.h:38
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:116
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static const AVFilterPad afir_outputs[]
Definition: af_afir.c:522
const char * arg
Definition: jacosubdec.c:66
float ** block
Definition: af_afir.h:67
Definition: avfft.h:73
static void fcmul_add_c(float *sum, const float *t, const float *c, ptrdiff_t len)
Definition: af_afir.c:38
#define FFMAX(a, b)
Definition: common.h:94
void av_rdft_calc(RDFTContext *s, FFTSample *data)
static int fir_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
Definition: af_afir.c:55
#define AF
Definition: af_afir.c:532
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:228
#define FFMIN(a, b)
Definition: common.h:96
AVFormatContext * ctx
Definition: movenc.c:48
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
AVFilter ff_af_afir
Definition: af_afir.c:545
Definition: avfft.h:72
void av_rdft_end(RDFTContext *s)
int n
Definition: avisynth_c.h:684
RDFTContext * av_rdft_init(int nbits, enum RDFTransformType trans)
Set up a real FFT.
FFTComplex ** coeff
Definition: af_afir.h:68
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
if(ret< 0)
Definition: vf_mcdeint.c:279
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
A list of supported channel layouts.
Definition: formats.h:85
int64_t pts
Definition: af_afir.h:73
AVFloatDSPContext * fdsp
Definition: af_afir.h:76
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
FFT functions.
float ** sum
Definition: af_afir.h:66
#define MAX_IR_DURATION
Definition: af_afir.h:35
AVFrame * buffer
Definition: af_afir.h:72
Filter definition.
Definition: avfilter.h:144
int have_coeffs
Definition: af_afir.h:48
Rational number (pair of numerator and denominator).
Definition: rational.h:58
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_afir.c:443
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
Definition: float_dsp.h:85
AVFILTER_DEFINE_CLASS(afir)
const char * name
Filter name.
Definition: avfilter.h:148
int nb_samples
Definition: af_afir.h:61
int need_padding
Definition: af_afir.h:63
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
float length
Definition: af_afir.h:42
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:378
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:112
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:201
common internal and external API header
static double c[64]
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: af_afir.c:302
int part_index
Definition: af_afir.h:52
#define OFFSET(x)
Definition: af_afir.c:533
avfilter_execute_func * execute
Definition: internal.h:155
float gain
Definition: af_afir.h:45
int nb_partitions
Definition: af_afir.h:55
AVAudioFifo * fifo[2]
Definition: af_afir.h:70
Audio FIFO Buffer.
int len
float wet_gain
Definition: af_afir.h:40
A list of supported formats for one end of a filter link.
Definition: formats.h:64
static const double coeff[2][5]
Definition: vf_owdenoise.c:72
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:138
An instance of a filter.
Definition: avfilter.h:338
static const AVFilterPad afir_inputs[]
Definition: af_afir.c:509
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
FILE * out
Definition: movenc.c:54
#define av_freep(p)
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:407
formats
Definition: signature.h:48
const char int length
Definition: avisynth_c.h:768
internal API functions
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition...
Definition: formats.c:410
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:265
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:284
for(j=16;j >0;--j)
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248