FFmpeg  4.4.9
af_arnndn.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Gregor Richards
3  * Copyright (c) 2017 Mozilla
4  * Copyright (c) 2005-2009 Xiph.Org Foundation
5  * Copyright (c) 2007-2008 CSIRO
6  * Copyright (c) 2008-2011 Octasic Inc.
7  * Copyright (c) Jean-Marc Valin
8  * Copyright (c) 2019 Paul B Mahol
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * - Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * - Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in the
19  * documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
25  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <float.h>
35 
36 #include "libavutil/avassert.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/float_dsp.h"
39 #include "libavutil/mem_internal.h"
40 #include "libavutil/opt.h"
41 #include "libavutil/tx.h"
42 #include "avfilter.h"
43 #include "audio.h"
44 #include "filters.h"
45 #include "formats.h"
46 
47 #define FRAME_SIZE_SHIFT 2
48 #define FRAME_SIZE (120<<FRAME_SIZE_SHIFT)
49 #define WINDOW_SIZE (2*FRAME_SIZE)
50 #define FREQ_SIZE (FRAME_SIZE + 1)
51 
52 #define PITCH_MIN_PERIOD 60
53 #define PITCH_MAX_PERIOD 768
54 #define PITCH_FRAME_SIZE 960
55 #define PITCH_BUF_SIZE (PITCH_MAX_PERIOD+PITCH_FRAME_SIZE)
56 
57 #define SQUARE(x) ((x)*(x))
58 
59 #define NB_BANDS 22
60 
61 #define CEPS_MEM 8
62 #define NB_DELTA_CEPS 6
63 
64 #define NB_FEATURES (NB_BANDS+3*NB_DELTA_CEPS+2)
65 
66 #define WEIGHTS_SCALE (1.f/256)
67 
68 #define MAX_NEURONS 128
69 
70 #define ACTIVATION_TANH 0
71 #define ACTIVATION_SIGMOID 1
72 #define ACTIVATION_RELU 2
73 
74 #define Q15ONE 1.0f
75 
76 typedef struct DenseLayer {
77  const float *bias;
78  const float *input_weights;
79  int nb_inputs;
82 } DenseLayer;
83 
84 typedef struct GRULayer {
85  const float *bias;
86  const float *input_weights;
87  const float *recurrent_weights;
88  int nb_inputs;
91 } GRULayer;
92 
93 typedef struct RNNModel {
96 
98  const GRULayer *vad_gru;
99 
102 
105 
108 
111 } RNNModel;
112 
113 typedef struct RNNState {
118 } RNNState;
119 
120 typedef struct DenoiseState {
123  int memid;
127  float last_gain;
129  float mem_hp_x[2];
130  float lastg[NB_BANDS];
135 } DenoiseState;
136 
137 typedef struct AudioRNNContext {
138  const AVClass *class;
139 
140  char *model_name;
141  float mix;
142 
143  int channels;
145 
148 
150 
153 
154 #define F_ACTIVATION_TANH 0
155 #define F_ACTIVATION_SIGMOID 1
156 #define F_ACTIVATION_RELU 2
157 
158 static void rnnoise_model_free(RNNModel *model)
159 {
160 #define FREE_MAYBE(ptr) do { if (ptr) free(ptr); } while (0)
161 #define FREE_DENSE(name) do { \
162  if (model->name) { \
163  av_free((void *) model->name->input_weights); \
164  av_free((void *) model->name->bias); \
165  av_free((void *) model->name); \
166  } \
167  } while (0)
168 #define FREE_GRU(name) do { \
169  if (model->name) { \
170  av_free((void *) model->name->input_weights); \
171  av_free((void *) model->name->recurrent_weights); \
172  av_free((void *) model->name->bias); \
173  av_free((void *) model->name); \
174  } \
175  } while (0)
176 
177  if (!model)
178  return;
179  FREE_DENSE(input_dense);
180  FREE_GRU(vad_gru);
181  FREE_GRU(noise_gru);
182  FREE_GRU(denoise_gru);
183  FREE_DENSE(denoise_output);
184  FREE_DENSE(vad_output);
185  av_free(model);
186 }
187 
188 static int rnnoise_model_from_file(FILE *f, RNNModel **rnn)
189 {
190  RNNModel *ret = NULL;
191  DenseLayer *input_dense;
192  GRULayer *vad_gru;
193  GRULayer *noise_gru;
194  GRULayer *denoise_gru;
195  DenseLayer *denoise_output;
196  DenseLayer *vad_output;
197  int in;
198 
199  if (fscanf(f, "rnnoise-nu model file version %d\n", &in) != 1 || in != 1)
200  return AVERROR_INVALIDDATA;
201 
202  ret = av_calloc(1, sizeof(RNNModel));
203  if (!ret)
204  return AVERROR(ENOMEM);
205 
206 #define ALLOC_LAYER(type, name) \
207  name = av_calloc(1, sizeof(type)); \
208  if (!name) { \
209  rnnoise_model_free(ret); \
210  return AVERROR(ENOMEM); \
211  } \
212  ret->name = name
213 
214  ALLOC_LAYER(DenseLayer, input_dense);
215  ALLOC_LAYER(GRULayer, vad_gru);
216  ALLOC_LAYER(GRULayer, noise_gru);
217  ALLOC_LAYER(GRULayer, denoise_gru);
218  ALLOC_LAYER(DenseLayer, denoise_output);
219  ALLOC_LAYER(DenseLayer, vad_output);
220 
221 #define INPUT_VAL(name, min) do { \
222  if (fscanf(f, "%d", &in) != 1 || in < (min) || in > 128) { \
223  rnnoise_model_free(ret); \
224  return AVERROR(EINVAL); \
225  } \
226  name = in; \
227  } while (0)
228 
229 #define INPUT_ACTIVATION(name) do { \
230  int activation; \
231  INPUT_VAL(activation, 0); \
232  switch (activation) { \
233  case F_ACTIVATION_SIGMOID: \
234  name = ACTIVATION_SIGMOID; \
235  break; \
236  case F_ACTIVATION_RELU: \
237  name = ACTIVATION_RELU; \
238  break; \
239  default: \
240  name = ACTIVATION_TANH; \
241  } \
242  } while (0)
243 
244 #define INPUT_ARRAY(name, len) do { \
245  float *values = av_calloc((len), sizeof(float)); \
246  if (!values) { \
247  rnnoise_model_free(ret); \
248  return AVERROR(ENOMEM); \
249  } \
250  name = values; \
251  for (int i = 0; i < (len); i++) { \
252  if (fscanf(f, "%d", &in) != 1) { \
253  rnnoise_model_free(ret); \
254  return AVERROR(EINVAL); \
255  } \
256  values[i] = in; \
257  } \
258  } while (0)
259 
260 #define INPUT_ARRAY3(name, len0, len1, len2) do { \
261  float *values = av_calloc(FFALIGN((len0), 4) * FFALIGN((len1), 4) * (len2), sizeof(float)); \
262  if (!values) { \
263  rnnoise_model_free(ret); \
264  return AVERROR(ENOMEM); \
265  } \
266  name = values; \
267  for (int k = 0; k < (len0); k++) { \
268  for (int i = 0; i < (len2); i++) { \
269  for (int j = 0; j < (len1); j++) { \
270  if (fscanf(f, "%d", &in) != 1) { \
271  rnnoise_model_free(ret); \
272  return AVERROR(EINVAL); \
273  } \
274  values[j * (len2) * FFALIGN((len0), 4) + i * FFALIGN((len0), 4) + k] = in; \
275  } \
276  } \
277  } \
278  } while (0)
279 
280 #define NEW_LINE() do { \
281  int c; \
282  while ((c = fgetc(f)) != EOF) { \
283  if (c == '\n') \
284  break; \
285  } \
286  } while (0)
287 
288 #define INPUT_DENSE(name) do { \
289  INPUT_VAL(name->nb_inputs, 1); \
290  INPUT_VAL(name->nb_neurons, 1); \
291  ret->name ## _size = name->nb_neurons; \
292  INPUT_ACTIVATION(name->activation); \
293  NEW_LINE(); \
294  INPUT_ARRAY(name->input_weights, name->nb_inputs * name->nb_neurons); \
295  NEW_LINE(); \
296  INPUT_ARRAY(name->bias, name->nb_neurons); \
297  NEW_LINE(); \
298  } while (0)
299 
300 #define INPUT_GRU(name) do { \
301  INPUT_VAL(name->nb_inputs, 1); \
302  INPUT_VAL(name->nb_neurons, 1); \
303  ret->name ## _size = name->nb_neurons; \
304  INPUT_ACTIVATION(name->activation); \
305  NEW_LINE(); \
306  INPUT_ARRAY3(name->input_weights, name->nb_inputs, name->nb_neurons, 3); \
307  NEW_LINE(); \
308  INPUT_ARRAY3(name->recurrent_weights, name->nb_neurons, name->nb_neurons, 3); \
309  NEW_LINE(); \
310  INPUT_ARRAY(name->bias, name->nb_neurons * 3); \
311  NEW_LINE(); \
312  } while (0)
313 
314  INPUT_DENSE(input_dense);
315  INPUT_GRU(vad_gru);
316  INPUT_GRU(noise_gru);
317  INPUT_GRU(denoise_gru);
318  INPUT_DENSE(denoise_output);
319  INPUT_DENSE(vad_output);
320 
321  if (input_dense->nb_inputs != NB_FEATURES ||
322  vad_gru->nb_inputs != input_dense->nb_neurons ||
323  noise_gru->nb_inputs != input_dense->nb_neurons + vad_gru->nb_neurons + NB_FEATURES ||
324  denoise_gru->nb_inputs != vad_gru->nb_neurons + noise_gru->nb_neurons + NB_FEATURES ||
325  denoise_output->nb_inputs != denoise_gru->nb_neurons ||
326  denoise_output->nb_neurons != NB_BANDS ||
327  vad_output->nb_inputs != vad_gru->nb_neurons ||
328  vad_output->nb_neurons != 1) {
329  rnnoise_model_free(ret);
330  return AVERROR(EINVAL);
331  }
332 
333  *rnn = ret;
334 
335  return 0;
336 }
337 
339 {
342  static const enum AVSampleFormat sample_fmts[] = {
345  };
346  int ret, sample_rates[] = { 48000, -1 };
347 
349  if (!formats)
350  return AVERROR(ENOMEM);
352  if (ret < 0)
353  return ret;
354 
356  if (!layouts)
357  return AVERROR(ENOMEM);
358 
360  if (ret < 0)
361  return ret;
362 
364  if (!formats)
365  return AVERROR(ENOMEM);
367 }
368 
369 static int config_input(AVFilterLink *inlink)
370 {
371  AVFilterContext *ctx = inlink->dst;
372  AudioRNNContext *s = ctx->priv;
373  int ret;
374 
375  s->channels = inlink->channels;
376 
377  if (!s->st)
378  s->st = av_calloc(s->channels, sizeof(DenoiseState));
379  if (!s->st)
380  return AVERROR(ENOMEM);
381 
382  for (int i = 0; i < s->channels; i++) {
383  DenoiseState *st = &s->st[i];
384 
385  st->rnn[0].model = s->model[0];
386  st->rnn[0].vad_gru_state = av_calloc(sizeof(float), FFALIGN(s->model[0]->vad_gru_size, 16));
387  st->rnn[0].noise_gru_state = av_calloc(sizeof(float), FFALIGN(s->model[0]->noise_gru_size, 16));
388  st->rnn[0].denoise_gru_state = av_calloc(sizeof(float), FFALIGN(s->model[0]->denoise_gru_size, 16));
389  if (!st->rnn[0].vad_gru_state ||
390  !st->rnn[0].noise_gru_state ||
391  !st->rnn[0].denoise_gru_state)
392  return AVERROR(ENOMEM);
393  }
394 
395  for (int i = 0; i < s->channels; i++) {
396  DenoiseState *st = &s->st[i];
397 
398  if (!st->tx)
399  ret = av_tx_init(&st->tx, &st->tx_fn, AV_TX_FLOAT_FFT, 0, WINDOW_SIZE, NULL, 0);
400  if (ret < 0)
401  return ret;
402 
403  if (!st->txi)
404  ret = av_tx_init(&st->txi, &st->txi_fn, AV_TX_FLOAT_FFT, 1, WINDOW_SIZE, NULL, 0);
405  if (ret < 0)
406  return ret;
407  }
408 
409  return 0;
410 }
411 
412 static void biquad(float *y, float mem[2], const float *x,
413  const float *b, const float *a, int N)
414 {
415  for (int i = 0; i < N; i++) {
416  float xi, yi;
417 
418  xi = x[i];
419  yi = x[i] + mem[0];
420  mem[0] = mem[1] + (b[0]*xi - a[0]*yi);
421  mem[1] = (b[1]*xi - a[1]*yi);
422  y[i] = yi;
423  }
424 }
425 
426 #define RNN_MOVE(dst, src, n) (memmove((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
427 #define RNN_CLEAR(dst, n) (memset((dst), 0, (n)*sizeof(*(dst))))
428 #define RNN_COPY(dst, src, n) (memcpy((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
429 
430 static void forward_transform(DenoiseState *st, AVComplexFloat *out, const float *in)
431 {
434 
435  for (int i = 0; i < WINDOW_SIZE; i++) {
436  x[i].re = in[i];
437  x[i].im = 0;
438  }
439 
440  st->tx_fn(st->tx, y, x, sizeof(float));
441 
442  RNN_COPY(out, y, FREQ_SIZE);
443 }
444 
445 static void inverse_transform(DenoiseState *st, float *out, const AVComplexFloat *in)
446 {
449 
450  RNN_COPY(x, in, FREQ_SIZE);
451 
452  for (int i = FREQ_SIZE; i < WINDOW_SIZE; i++) {
453  x[i].re = x[WINDOW_SIZE - i].re;
454  x[i].im = -x[WINDOW_SIZE - i].im;
455  }
456 
457  st->txi_fn(st->txi, y, x, sizeof(float));
458 
459  for (int i = 0; i < WINDOW_SIZE; i++)
460  out[i] = y[i].re / WINDOW_SIZE;
461 }
462 
463 static const uint8_t eband5ms[] = {
464 /*0 200 400 600 800 1k 1.2 1.4 1.6 2k 2.4 2.8 3.2 4k 4.8 5.6 6.8 8k 9.6 12k 15.6 20k*/
465  0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100
466 };
467 
468 static void compute_band_energy(float *bandE, const AVComplexFloat *X)
469 {
470  float sum[NB_BANDS] = {0};
471 
472  for (int i = 0; i < NB_BANDS - 1; i++) {
473  int band_size;
474 
475  band_size = (eband5ms[i + 1] - eband5ms[i]) << FRAME_SIZE_SHIFT;
476  for (int j = 0; j < band_size; j++) {
477  float tmp, frac = (float)j / band_size;
478 
479  tmp = SQUARE(X[(eband5ms[i] << FRAME_SIZE_SHIFT) + j].re);
480  tmp += SQUARE(X[(eband5ms[i] << FRAME_SIZE_SHIFT) + j].im);
481  sum[i] += (1.f - frac) * tmp;
482  sum[i + 1] += frac * tmp;
483  }
484  }
485 
486  sum[0] *= 2;
487  sum[NB_BANDS - 1] *= 2;
488 
489  for (int i = 0; i < NB_BANDS; i++)
490  bandE[i] = sum[i];
491 }
492 
493 static void compute_band_corr(float *bandE, const AVComplexFloat *X, const AVComplexFloat *P)
494 {
495  float sum[NB_BANDS] = { 0 };
496 
497  for (int i = 0; i < NB_BANDS - 1; i++) {
498  int band_size;
499 
500  band_size = (eband5ms[i + 1] - eband5ms[i]) << FRAME_SIZE_SHIFT;
501  for (int j = 0; j < band_size; j++) {
502  float tmp, frac = (float)j / band_size;
503 
504  tmp = X[(eband5ms[i]<<FRAME_SIZE_SHIFT) + j].re * P[(eband5ms[i]<<FRAME_SIZE_SHIFT) + j].re;
505  tmp += X[(eband5ms[i]<<FRAME_SIZE_SHIFT) + j].im * P[(eband5ms[i]<<FRAME_SIZE_SHIFT) + j].im;
506  sum[i] += (1 - frac) * tmp;
507  sum[i + 1] += frac * tmp;
508  }
509  }
510 
511  sum[0] *= 2;
512  sum[NB_BANDS-1] *= 2;
513 
514  for (int i = 0; i < NB_BANDS; i++)
515  bandE[i] = sum[i];
516 }
517 
518 static void frame_analysis(AudioRNNContext *s, DenoiseState *st, AVComplexFloat *X, float *Ex, const float *in)
519 {
520  LOCAL_ALIGNED_32(float, x, [WINDOW_SIZE]);
521 
525  s->fdsp->vector_fmul(x, x, s->window, WINDOW_SIZE);
526  forward_transform(st, X, x);
527  compute_band_energy(Ex, X);
528 }
529 
530 static void frame_synthesis(AudioRNNContext *s, DenoiseState *st, float *out, const AVComplexFloat *y)
531 {
532  LOCAL_ALIGNED_32(float, x, [WINDOW_SIZE]);
533  const float *src = st->history;
534  const float mix = s->mix;
535  const float imix = 1.f - FFMAX(mix, 0.f);
536 
537  inverse_transform(st, x, y);
538  s->fdsp->vector_fmul(x, x, s->window, WINDOW_SIZE);
539  s->fdsp->vector_fmac_scalar(x, st->synthesis_mem, 1.f, FRAME_SIZE);
540  RNN_COPY(out, x, FRAME_SIZE);
542 
543  for (int n = 0; n < FRAME_SIZE; n++)
544  out[n] = out[n] * mix + src[n] * imix;
545 }
546 
547 static inline void xcorr_kernel(const float *x, const float *y, float sum[4], int len)
548 {
549  float y_0, y_1, y_2, y_3 = 0;
550  int j;
551 
552  y_0 = *y++;
553  y_1 = *y++;
554  y_2 = *y++;
555 
556  for (j = 0; j < len - 3; j += 4) {
557  float tmp;
558 
559  tmp = *x++;
560  y_3 = *y++;
561  sum[0] += tmp * y_0;
562  sum[1] += tmp * y_1;
563  sum[2] += tmp * y_2;
564  sum[3] += tmp * y_3;
565  tmp = *x++;
566  y_0 = *y++;
567  sum[0] += tmp * y_1;
568  sum[1] += tmp * y_2;
569  sum[2] += tmp * y_3;
570  sum[3] += tmp * y_0;
571  tmp = *x++;
572  y_1 = *y++;
573  sum[0] += tmp * y_2;
574  sum[1] += tmp * y_3;
575  sum[2] += tmp * y_0;
576  sum[3] += tmp * y_1;
577  tmp = *x++;
578  y_2 = *y++;
579  sum[0] += tmp * y_3;
580  sum[1] += tmp * y_0;
581  sum[2] += tmp * y_1;
582  sum[3] += tmp * y_2;
583  }
584 
585  if (j++ < len) {
586  float tmp = *x++;
587 
588  y_3 = *y++;
589  sum[0] += tmp * y_0;
590  sum[1] += tmp * y_1;
591  sum[2] += tmp * y_2;
592  sum[3] += tmp * y_3;
593  }
594 
595  if (j++ < len) {
596  float tmp=*x++;
597 
598  y_0 = *y++;
599  sum[0] += tmp * y_1;
600  sum[1] += tmp * y_2;
601  sum[2] += tmp * y_3;
602  sum[3] += tmp * y_0;
603  }
604 
605  if (j < len) {
606  float tmp=*x++;
607 
608  y_1 = *y++;
609  sum[0] += tmp * y_2;
610  sum[1] += tmp * y_3;
611  sum[2] += tmp * y_0;
612  sum[3] += tmp * y_1;
613  }
614 }
615 
616 static inline float celt_inner_prod(const float *x,
617  const float *y, int N)
618 {
619  float xy = 0.f;
620 
621  for (int i = 0; i < N; i++)
622  xy += x[i] * y[i];
623 
624  return xy;
625 }
626 
627 static void celt_pitch_xcorr(const float *x, const float *y,
628  float *xcorr, int len, int max_pitch)
629 {
630  int i;
631 
632  for (i = 0; i < max_pitch - 3; i += 4) {
633  float sum[4] = { 0, 0, 0, 0};
634 
635  xcorr_kernel(x, y + i, sum, len);
636 
637  xcorr[i] = sum[0];
638  xcorr[i + 1] = sum[1];
639  xcorr[i + 2] = sum[2];
640  xcorr[i + 3] = sum[3];
641  }
642  /* In case max_pitch isn't a multiple of 4, do non-unrolled version. */
643  for (; i < max_pitch; i++) {
644  xcorr[i] = celt_inner_prod(x, y + i, len);
645  }
646 }
647 
648 static int celt_autocorr(const float *x, /* in: [0...n-1] samples x */
649  float *ac, /* out: [0...lag-1] ac values */
650  const float *window,
651  int overlap,
652  int lag,
653  int n)
654 {
655  int fastN = n - lag;
656  int shift;
657  const float *xptr;
658  float xx[PITCH_BUF_SIZE>>1];
659 
660  if (overlap == 0) {
661  xptr = x;
662  } else {
663  for (int i = 0; i < n; i++)
664  xx[i] = x[i];
665  for (int i = 0; i < overlap; i++) {
666  xx[i] = x[i] * window[i];
667  xx[n-i-1] = x[n-i-1] * window[i];
668  }
669  xptr = xx;
670  }
671 
672  shift = 0;
673  celt_pitch_xcorr(xptr, xptr, ac, fastN, lag+1);
674 
675  for (int k = 0; k <= lag; k++) {
676  float d = 0.f;
677 
678  for (int i = k + fastN; i < n; i++)
679  d += xptr[i] * xptr[i-k];
680  ac[k] += d;
681  }
682 
683  return shift;
684 }
685 
686 static void celt_lpc(float *lpc, /* out: [0...p-1] LPC coefficients */
687  const float *ac, /* in: [0...p] autocorrelation values */
688  int p)
689 {
690  float r, error = ac[0];
691 
692  RNN_CLEAR(lpc, p);
693  if (ac[0] != 0) {
694  for (int i = 0; i < p; i++) {
695  /* Sum up this iteration's reflection coefficient */
696  float rr = 0;
697  for (int j = 0; j < i; j++)
698  rr += (lpc[j] * ac[i - j]);
699  rr += ac[i + 1];
700  r = -rr/error;
701  /* Update LPC coefficients and total error */
702  lpc[i] = r;
703  for (int j = 0; j < (i + 1) >> 1; j++) {
704  float tmp1, tmp2;
705  tmp1 = lpc[j];
706  tmp2 = lpc[i-1-j];
707  lpc[j] = tmp1 + (r*tmp2);
708  lpc[i-1-j] = tmp2 + (r*tmp1);
709  }
710 
711  error = error - (r * r *error);
712  /* Bail out once we get 30 dB gain */
713  if (error < .001f * ac[0])
714  break;
715  }
716  }
717 }
718 
719 static void celt_fir5(const float *x,
720  const float *num,
721  float *y,
722  int N,
723  float *mem)
724 {
725  float num0, num1, num2, num3, num4;
726  float mem0, mem1, mem2, mem3, mem4;
727 
728  num0 = num[0];
729  num1 = num[1];
730  num2 = num[2];
731  num3 = num[3];
732  num4 = num[4];
733  mem0 = mem[0];
734  mem1 = mem[1];
735  mem2 = mem[2];
736  mem3 = mem[3];
737  mem4 = mem[4];
738 
739  for (int i = 0; i < N; i++) {
740  float sum = x[i];
741 
742  sum += (num0*mem0);
743  sum += (num1*mem1);
744  sum += (num2*mem2);
745  sum += (num3*mem3);
746  sum += (num4*mem4);
747  mem4 = mem3;
748  mem3 = mem2;
749  mem2 = mem1;
750  mem1 = mem0;
751  mem0 = x[i];
752  y[i] = sum;
753  }
754 
755  mem[0] = mem0;
756  mem[1] = mem1;
757  mem[2] = mem2;
758  mem[3] = mem3;
759  mem[4] = mem4;
760 }
761 
762 static void pitch_downsample(float *x[], float *x_lp,
763  int len, int C)
764 {
765  float ac[5];
766  float tmp=Q15ONE;
767  float lpc[4], mem[5]={0,0,0,0,0};
768  float lpc2[5];
769  float c1 = .8f;
770 
771  for (int i = 1; i < len >> 1; i++)
772  x_lp[i] = .5f * (.5f * (x[0][(2*i-1)]+x[0][(2*i+1)])+x[0][2*i]);
773  x_lp[0] = .5f * (.5f * (x[0][1])+x[0][0]);
774  if (C==2) {
775  for (int i = 1; i < len >> 1; i++)
776  x_lp[i] += (.5f * (.5f * (x[1][(2*i-1)]+x[1][(2*i+1)])+x[1][2*i]));
777  x_lp[0] += .5f * (.5f * (x[1][1])+x[1][0]);
778  }
779 
780  celt_autocorr(x_lp, ac, NULL, 0, 4, len>>1);
781 
782  /* Noise floor -40 dB */
783  ac[0] *= 1.0001f;
784  /* Lag windowing */
785  for (int i = 1; i <= 4; i++) {
786  /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
787  ac[i] -= ac[i]*(.008f*i)*(.008f*i);
788  }
789 
790  celt_lpc(lpc, ac, 4);
791  for (int i = 0; i < 4; i++) {
792  tmp = .9f * tmp;
793  lpc[i] = (lpc[i] * tmp);
794  }
795  /* Add a zero */
796  lpc2[0] = lpc[0] + .8f;
797  lpc2[1] = lpc[1] + (c1 * lpc[0]);
798  lpc2[2] = lpc[2] + (c1 * lpc[1]);
799  lpc2[3] = lpc[3] + (c1 * lpc[2]);
800  lpc2[4] = (c1 * lpc[3]);
801  celt_fir5(x_lp, lpc2, x_lp, len>>1, mem);
802 }
803 
804 static inline void dual_inner_prod(const float *x, const float *y01, const float *y02,
805  int N, float *xy1, float *xy2)
806 {
807  float xy01 = 0, xy02 = 0;
808 
809  for (int i = 0; i < N; i++) {
810  xy01 += (x[i] * y01[i]);
811  xy02 += (x[i] * y02[i]);
812  }
813 
814  *xy1 = xy01;
815  *xy2 = xy02;
816 }
817 
818 static float compute_pitch_gain(float xy, float xx, float yy)
819 {
820  return xy / sqrtf(1.f + xx * yy);
821 }
822 
823 static const uint8_t second_check[16] = {0, 0, 3, 2, 3, 2, 5, 2, 3, 2, 3, 2, 5, 2, 3, 2};
824 static float remove_doubling(float *x, int maxperiod, int minperiod, int N,
825  int *T0_, int prev_period, float prev_gain)
826 {
827  int k, i, T, T0;
828  float g, g0;
829  float pg;
830  float xy,xx,yy,xy2;
831  float xcorr[3];
832  float best_xy, best_yy;
833  int offset;
834  int minperiod0;
835  float yy_lookup[PITCH_MAX_PERIOD+1];
836 
837  minperiod0 = minperiod;
838  maxperiod /= 2;
839  minperiod /= 2;
840  *T0_ /= 2;
841  prev_period /= 2;
842  N /= 2;
843  x += maxperiod;
844  if (*T0_>=maxperiod)
845  *T0_=maxperiod-1;
846 
847  T = T0 = *T0_;
848  dual_inner_prod(x, x, x-T0, N, &xx, &xy);
849  yy_lookup[0] = xx;
850  yy=xx;
851  for (i = 1; i <= maxperiod; i++) {
852  yy = yy+(x[-i] * x[-i])-(x[N-i] * x[N-i]);
853  yy_lookup[i] = FFMAX(0, yy);
854  }
855  yy = yy_lookup[T0];
856  best_xy = xy;
857  best_yy = yy;
858  g = g0 = compute_pitch_gain(xy, xx, yy);
859  /* Look for any pitch at T/k */
860  for (k = 2; k <= 15; k++) {
861  int T1, T1b;
862  float g1;
863  float cont=0;
864  float thresh;
865  T1 = (2*T0+k)/(2*k);
866  if (T1 < minperiod)
867  break;
868  /* Look for another strong correlation at T1b */
869  if (k==2)
870  {
871  if (T1+T0>maxperiod)
872  T1b = T0;
873  else
874  T1b = T0+T1;
875  } else
876  {
877  T1b = (2*second_check[k]*T0+k)/(2*k);
878  }
879  dual_inner_prod(x, &x[-T1], &x[-T1b], N, &xy, &xy2);
880  xy = .5f * (xy + xy2);
881  yy = .5f * (yy_lookup[T1] + yy_lookup[T1b]);
882  g1 = compute_pitch_gain(xy, xx, yy);
883  if (FFABS(T1-prev_period)<=1)
884  cont = prev_gain;
885  else if (FFABS(T1-prev_period)<=2 && 5 * k * k < T0)
886  cont = prev_gain * .5f;
887  else
888  cont = 0;
889  thresh = FFMAX(.3f, (.7f * g0) - cont);
890  /* Bias against very high pitch (very short period) to avoid false-positives
891  due to short-term correlation */
892  if (T1<3*minperiod)
893  thresh = FFMAX(.4f, (.85f * g0) - cont);
894  else if (T1<2*minperiod)
895  thresh = FFMAX(.5f, (.9f * g0) - cont);
896  if (g1 > thresh)
897  {
898  best_xy = xy;
899  best_yy = yy;
900  T = T1;
901  g = g1;
902  }
903  }
904  best_xy = FFMAX(0, best_xy);
905  if (best_yy <= best_xy)
906  pg = Q15ONE;
907  else
908  pg = best_xy/(best_yy + 1);
909 
910  for (k = 0; k < 3; k++)
911  xcorr[k] = celt_inner_prod(x, x-(T+k-1), N);
912  if ((xcorr[2]-xcorr[0]) > .7f * (xcorr[1]-xcorr[0]))
913  offset = 1;
914  else if ((xcorr[0]-xcorr[2]) > (.7f * (xcorr[1] - xcorr[2])))
915  offset = -1;
916  else
917  offset = 0;
918  if (pg > g)
919  pg = g;
920  *T0_ = 2*T+offset;
921 
922  if (*T0_<minperiod0)
923  *T0_=minperiod0;
924  return pg;
925 }
926 
927 static void find_best_pitch(float *xcorr, float *y, int len,
928  int max_pitch, int *best_pitch)
929 {
930  float best_num[2];
931  float best_den[2];
932  float Syy = 1.f;
933 
934  best_num[0] = -1;
935  best_num[1] = -1;
936  best_den[0] = 0;
937  best_den[1] = 0;
938  best_pitch[0] = 0;
939  best_pitch[1] = 1;
940 
941  for (int j = 0; j < len; j++)
942  Syy += y[j] * y[j];
943 
944  for (int i = 0; i < max_pitch; i++) {
945  if (xcorr[i]>0) {
946  float num;
947  float xcorr16;
948 
949  xcorr16 = xcorr[i];
950  /* Considering the range of xcorr16, this should avoid both underflows
951  and overflows (inf) when squaring xcorr16 */
952  xcorr16 *= 1e-12f;
953  num = xcorr16 * xcorr16;
954  if ((num * best_den[1]) > (best_num[1] * Syy)) {
955  if ((num * best_den[0]) > (best_num[0] * Syy)) {
956  best_num[1] = best_num[0];
957  best_den[1] = best_den[0];
958  best_pitch[1] = best_pitch[0];
959  best_num[0] = num;
960  best_den[0] = Syy;
961  best_pitch[0] = i;
962  } else {
963  best_num[1] = num;
964  best_den[1] = Syy;
965  best_pitch[1] = i;
966  }
967  }
968  }
969  Syy += y[i+len]*y[i+len] - y[i] * y[i];
970  Syy = FFMAX(1, Syy);
971  }
972 }
973 
974 static void pitch_search(const float *x_lp, float *y,
975  int len, int max_pitch, int *pitch)
976 {
977  int lag;
978  int best_pitch[2]={0,0};
979  int offset;
980 
981  float x_lp4[WINDOW_SIZE];
982  float y_lp4[WINDOW_SIZE];
983  float xcorr[WINDOW_SIZE];
984 
985  lag = len+max_pitch;
986 
987  /* Downsample by 2 again */
988  for (int j = 0; j < len >> 2; j++)
989  x_lp4[j] = x_lp[2*j];
990  for (int j = 0; j < lag >> 2; j++)
991  y_lp4[j] = y[2*j];
992 
993  /* Coarse search with 4x decimation */
994 
995  celt_pitch_xcorr(x_lp4, y_lp4, xcorr, len>>2, max_pitch>>2);
996 
997  find_best_pitch(xcorr, y_lp4, len>>2, max_pitch>>2, best_pitch);
998 
999  /* Finer search with 2x decimation */
1000  for (int i = 0; i < max_pitch >> 1; i++) {
1001  float sum;
1002  xcorr[i] = 0;
1003  if (FFABS(i-2*best_pitch[0])>2 && FFABS(i-2*best_pitch[1])>2)
1004  continue;
1005  sum = celt_inner_prod(x_lp, y+i, len>>1);
1006  xcorr[i] = FFMAX(-1, sum);
1007  }
1008 
1009  find_best_pitch(xcorr, y, len>>1, max_pitch>>1, best_pitch);
1010 
1011  /* Refine by pseudo-interpolation */
1012  if (best_pitch[0] > 0 && best_pitch[0] < (max_pitch >> 1) - 1) {
1013  float a, b, c;
1014 
1015  a = xcorr[best_pitch[0] - 1];
1016  b = xcorr[best_pitch[0]];
1017  c = xcorr[best_pitch[0] + 1];
1018  if (c - a > .7f * (b - a))
1019  offset = 1;
1020  else if (a - c > .7f * (b-c))
1021  offset = -1;
1022  else
1023  offset = 0;
1024  } else {
1025  offset = 0;
1026  }
1027 
1028  *pitch = 2 * best_pitch[0] - offset;
1029 }
1030 
1031 static void dct(AudioRNNContext *s, float *out, const float *in)
1032 {
1033  for (int i = 0; i < NB_BANDS; i++) {
1034  float sum;
1035 
1036  sum = s->fdsp->scalarproduct_float(in, s->dct_table[i], FFALIGN(NB_BANDS, 4));
1037  out[i] = sum * sqrtf(2.f / 22);
1038  }
1039 }
1040 
1042  float *Ex, float *Ep, float *Exp, float *features, const float *in)
1043 {
1044  float E = 0;
1045  float *ceps_0, *ceps_1, *ceps_2;
1046  float spec_variability = 0;
1047  LOCAL_ALIGNED_32(float, Ly, [FFALIGN(NB_BANDS, 4)]);
1048  LOCAL_ALIGNED_32(float, p, [WINDOW_SIZE]);
1049  float pitch_buf[PITCH_BUF_SIZE>>1];
1050  int pitch_index;
1051  float gain;
1052  float *(pre[1]);
1053  float tmp[NB_BANDS];
1054  float follow, logMax;
1055 
1056  frame_analysis(s, st, X, Ex, in);
1059  pre[0] = &st->pitch_buf[0];
1060  pitch_downsample(pre, pitch_buf, PITCH_BUF_SIZE, 1);
1061  pitch_search(pitch_buf+(PITCH_MAX_PERIOD>>1), pitch_buf, PITCH_FRAME_SIZE,
1062  PITCH_MAX_PERIOD-3*PITCH_MIN_PERIOD, &pitch_index);
1063  pitch_index = PITCH_MAX_PERIOD-pitch_index;
1064 
1066  PITCH_FRAME_SIZE, &pitch_index, st->last_period, st->last_gain);
1067  st->last_period = pitch_index;
1068  st->last_gain = gain;
1069 
1070  for (int i = 0; i < WINDOW_SIZE; i++)
1071  p[i] = st->pitch_buf[PITCH_BUF_SIZE-WINDOW_SIZE-pitch_index+i];
1072 
1073  s->fdsp->vector_fmul(p, p, s->window, WINDOW_SIZE);
1074  forward_transform(st, P, p);
1075  compute_band_energy(Ep, P);
1076  compute_band_corr(Exp, X, P);
1077 
1078  for (int i = 0; i < NB_BANDS; i++)
1079  Exp[i] = Exp[i] / sqrtf(.001f+Ex[i]*Ep[i]);
1080 
1081  memset(Exp + NB_BANDS, 0, (FFALIGN(NB_BANDS, 4) - NB_BANDS) * sizeof(*Exp));
1082  dct(s, tmp, Exp);
1083 
1084  for (int i = 0; i < NB_DELTA_CEPS; i++)
1085  features[NB_BANDS+2*NB_DELTA_CEPS+i] = tmp[i];
1086 
1087  features[NB_BANDS+2*NB_DELTA_CEPS] -= 1.3;
1088  features[NB_BANDS+2*NB_DELTA_CEPS+1] -= 0.9;
1089  features[NB_BANDS+3*NB_DELTA_CEPS] = .01*(pitch_index-300);
1090  logMax = -2;
1091  follow = -2;
1092 
1093  for (int i = 0; i < NB_BANDS; i++) {
1094  Ly[i] = log10f(1e-2f + Ex[i]);
1095  Ly[i] = FFMAX(logMax-7, FFMAX(follow-1.5, Ly[i]));
1096  logMax = FFMAX(logMax, Ly[i]);
1097  follow = FFMAX(follow-1.5, Ly[i]);
1098  E += Ex[i];
1099  }
1100 
1101  if (E < 0.04f) {
1102  /* If there's no audio, avoid messing up the state. */
1103  RNN_CLEAR(features, NB_FEATURES);
1104  return 1;
1105  }
1106 
1107  memset(Ly + NB_BANDS, 0, (FFALIGN(NB_BANDS, 4) - NB_BANDS) * sizeof(*Ly));
1108  dct(s, features, Ly);
1109  features[0] -= 12;
1110  features[1] -= 4;
1111  ceps_0 = st->cepstral_mem[st->memid];
1112  ceps_1 = (st->memid < 1) ? st->cepstral_mem[CEPS_MEM+st->memid-1] : st->cepstral_mem[st->memid-1];
1113  ceps_2 = (st->memid < 2) ? st->cepstral_mem[CEPS_MEM+st->memid-2] : st->cepstral_mem[st->memid-2];
1114 
1115  for (int i = 0; i < NB_BANDS; i++)
1116  ceps_0[i] = features[i];
1117 
1118  st->memid++;
1119  for (int i = 0; i < NB_DELTA_CEPS; i++) {
1120  features[i] = ceps_0[i] + ceps_1[i] + ceps_2[i];
1121  features[NB_BANDS+i] = ceps_0[i] - ceps_2[i];
1122  features[NB_BANDS+NB_DELTA_CEPS+i] = ceps_0[i] - 2*ceps_1[i] + ceps_2[i];
1123  }
1124  /* Spectral variability features. */
1125  if (st->memid == CEPS_MEM)
1126  st->memid = 0;
1127 
1128  for (int i = 0; i < CEPS_MEM; i++) {
1129  float mindist = 1e15f;
1130  for (int j = 0; j < CEPS_MEM; j++) {
1131  float dist = 0.f;
1132  for (int k = 0; k < NB_BANDS; k++) {
1133  float tmp;
1134 
1135  tmp = st->cepstral_mem[i][k] - st->cepstral_mem[j][k];
1136  dist += tmp*tmp;
1137  }
1138 
1139  if (j != i)
1140  mindist = FFMIN(mindist, dist);
1141  }
1142 
1143  spec_variability += mindist;
1144  }
1145 
1146  features[NB_BANDS+3*NB_DELTA_CEPS+1] = spec_variability/CEPS_MEM-2.1;
1147 
1148  return 0;
1149 }
1150 
1151 static void interp_band_gain(float *g, const float *bandE)
1152 {
1153  memset(g, 0, sizeof(*g) * FREQ_SIZE);
1154 
1155  for (int i = 0; i < NB_BANDS - 1; i++) {
1156  const int band_size = (eband5ms[i + 1] - eband5ms[i]) << FRAME_SIZE_SHIFT;
1157 
1158  for (int j = 0; j < band_size; j++) {
1159  float frac = (float)j / band_size;
1160 
1161  g[(eband5ms[i] << FRAME_SIZE_SHIFT) + j] = (1.f - frac) * bandE[i] + frac * bandE[i + 1];
1162  }
1163  }
1164 }
1165 
1166 static void pitch_filter(AVComplexFloat *X, const AVComplexFloat *P, const float *Ex, const float *Ep,
1167  const float *Exp, const float *g)
1168 {
1169  float newE[NB_BANDS];
1170  float r[NB_BANDS];
1171  float norm[NB_BANDS];
1172  float rf[FREQ_SIZE] = {0};
1173  float normf[FREQ_SIZE]={0};
1174 
1175  for (int i = 0; i < NB_BANDS; i++) {
1176  if (Exp[i]>g[i]) r[i] = 1;
1177  else r[i] = SQUARE(Exp[i])*(1-SQUARE(g[i]))/(.001 + SQUARE(g[i])*(1-SQUARE(Exp[i])));
1178  r[i] = sqrtf(av_clipf(r[i], 0, 1));
1179  r[i] *= sqrtf(Ex[i]/(1e-8+Ep[i]));
1180  }
1181  interp_band_gain(rf, r);
1182  for (int i = 0; i < FREQ_SIZE; i++) {
1183  X[i].re += rf[i]*P[i].re;
1184  X[i].im += rf[i]*P[i].im;
1185  }
1186  compute_band_energy(newE, X);
1187  for (int i = 0; i < NB_BANDS; i++) {
1188  norm[i] = sqrtf(Ex[i] / (1e-8+newE[i]));
1189  }
1190  interp_band_gain(normf, norm);
1191  for (int i = 0; i < FREQ_SIZE; i++) {
1192  X[i].re *= normf[i];
1193  X[i].im *= normf[i];
1194  }
1195 }
1196 
1197 static const float tansig_table[201] = {
1198  0.000000f, 0.039979f, 0.079830f, 0.119427f, 0.158649f,
1199  0.197375f, 0.235496f, 0.272905f, 0.309507f, 0.345214f,
1200  0.379949f, 0.413644f, 0.446244f, 0.477700f, 0.507977f,
1201  0.537050f, 0.564900f, 0.591519f, 0.616909f, 0.641077f,
1202  0.664037f, 0.685809f, 0.706419f, 0.725897f, 0.744277f,
1203  0.761594f, 0.777888f, 0.793199f, 0.807569f, 0.821040f,
1204  0.833655f, 0.845456f, 0.856485f, 0.866784f, 0.876393f,
1205  0.885352f, 0.893698f, 0.901468f, 0.908698f, 0.915420f,
1206  0.921669f, 0.927473f, 0.932862f, 0.937863f, 0.942503f,
1207  0.946806f, 0.950795f, 0.954492f, 0.957917f, 0.961090f,
1208  0.964028f, 0.966747f, 0.969265f, 0.971594f, 0.973749f,
1209  0.975743f, 0.977587f, 0.979293f, 0.980869f, 0.982327f,
1210  0.983675f, 0.984921f, 0.986072f, 0.987136f, 0.988119f,
1211  0.989027f, 0.989867f, 0.990642f, 0.991359f, 0.992020f,
1212  0.992631f, 0.993196f, 0.993718f, 0.994199f, 0.994644f,
1213  0.995055f, 0.995434f, 0.995784f, 0.996108f, 0.996407f,
1214  0.996682f, 0.996937f, 0.997172f, 0.997389f, 0.997590f,
1215  0.997775f, 0.997946f, 0.998104f, 0.998249f, 0.998384f,
1216  0.998508f, 0.998623f, 0.998728f, 0.998826f, 0.998916f,
1217  0.999000f, 0.999076f, 0.999147f, 0.999213f, 0.999273f,
1218  0.999329f, 0.999381f, 0.999428f, 0.999472f, 0.999513f,
1219  0.999550f, 0.999585f, 0.999617f, 0.999646f, 0.999673f,
1220  0.999699f, 0.999722f, 0.999743f, 0.999763f, 0.999781f,
1221  0.999798f, 0.999813f, 0.999828f, 0.999841f, 0.999853f,
1222  0.999865f, 0.999875f, 0.999885f, 0.999893f, 0.999902f,
1223  0.999909f, 0.999916f, 0.999923f, 0.999929f, 0.999934f,
1224  0.999939f, 0.999944f, 0.999948f, 0.999952f, 0.999956f,
1225  0.999959f, 0.999962f, 0.999965f, 0.999968f, 0.999970f,
1226  0.999973f, 0.999975f, 0.999977f, 0.999978f, 0.999980f,
1227  0.999982f, 0.999983f, 0.999984f, 0.999986f, 0.999987f,
1228  0.999988f, 0.999989f, 0.999990f, 0.999990f, 0.999991f,
1229  0.999992f, 0.999992f, 0.999993f, 0.999994f, 0.999994f,
1230  0.999994f, 0.999995f, 0.999995f, 0.999996f, 0.999996f,
1231  0.999996f, 0.999997f, 0.999997f, 0.999997f, 0.999997f,
1232  0.999997f, 0.999998f, 0.999998f, 0.999998f, 0.999998f,
1233  0.999998f, 0.999998f, 0.999999f, 0.999999f, 0.999999f,
1234  0.999999f, 0.999999f, 0.999999f, 0.999999f, 0.999999f,
1235  0.999999f, 0.999999f, 0.999999f, 0.999999f, 0.999999f,
1236  1.000000f, 1.000000f, 1.000000f, 1.000000f, 1.000000f,
1237  1.000000f, 1.000000f, 1.000000f, 1.000000f, 1.000000f,
1238  1.000000f,
1239 };
1240 
1241 static inline float tansig_approx(float x)
1242 {
1243  float y, dy;
1244  float sign=1;
1245  int i;
1246 
1247  /* Tests are reversed to catch NaNs */
1248  if (!(x<8))
1249  return 1;
1250  if (!(x>-8))
1251  return -1;
1252  /* Another check in case of -ffast-math */
1253 
1254  if (isnan(x))
1255  return 0;
1256 
1257  if (x < 0) {
1258  x=-x;
1259  sign=-1;
1260  }
1261  i = (int)floor(.5f+25*x);
1262  x -= .04f*i;
1263  y = tansig_table[i];
1264  dy = 1-y*y;
1265  y = y + x*dy*(1 - y*x);
1266  return sign*y;
1267 }
1268 
1269 static inline float sigmoid_approx(float x)
1270 {
1271  return .5f + .5f*tansig_approx(.5f*x);
1272 }
1273 
1274 static void compute_dense(const DenseLayer *layer, float *output, const float *input)
1275 {
1276  const int N = layer->nb_neurons, M = layer->nb_inputs, stride = N;
1277 
1278  for (int i = 0; i < N; i++) {
1279  /* Compute update gate. */
1280  float sum = layer->bias[i];
1281 
1282  for (int j = 0; j < M; j++)
1283  sum += layer->input_weights[j * stride + i] * input[j];
1284 
1285  output[i] = WEIGHTS_SCALE * sum;
1286  }
1287 
1288  if (layer->activation == ACTIVATION_SIGMOID) {
1289  for (int i = 0; i < N; i++)
1290  output[i] = sigmoid_approx(output[i]);
1291  } else if (layer->activation == ACTIVATION_TANH) {
1292  for (int i = 0; i < N; i++)
1293  output[i] = tansig_approx(output[i]);
1294  } else if (layer->activation == ACTIVATION_RELU) {
1295  for (int i = 0; i < N; i++)
1296  output[i] = FFMAX(0, output[i]);
1297  } else {
1298  av_assert0(0);
1299  }
1300 }
1301 
1302 static void compute_gru(AudioRNNContext *s, const GRULayer *gru, float *state, const float *input)
1303 {
1304  LOCAL_ALIGNED_32(float, z, [MAX_NEURONS]);
1305  LOCAL_ALIGNED_32(float, r, [MAX_NEURONS]);
1306  LOCAL_ALIGNED_32(float, h, [MAX_NEURONS]);
1307  const int M = gru->nb_inputs;
1308  const int N = gru->nb_neurons;
1309  const int AN = FFALIGN(N, 4);
1310  const int AM = FFALIGN(M, 4);
1311  const int stride = 3 * AN, istride = 3 * AM;
1312 
1313  for (int i = 0; i < N; i++) {
1314  /* Compute update gate. */
1315  float sum = gru->bias[i];
1316 
1317  sum += s->fdsp->scalarproduct_float(gru->input_weights + i * istride, input, AM);
1318  sum += s->fdsp->scalarproduct_float(gru->recurrent_weights + i * stride, state, AN);
1319  z[i] = sigmoid_approx(WEIGHTS_SCALE * sum);
1320  }
1321 
1322  for (int i = 0; i < N; i++) {
1323  /* Compute reset gate. */
1324  float sum = gru->bias[N + i];
1325 
1326  sum += s->fdsp->scalarproduct_float(gru->input_weights + AM + i * istride, input, AM);
1327  sum += s->fdsp->scalarproduct_float(gru->recurrent_weights + AN + i * stride, state, AN);
1328  r[i] = sigmoid_approx(WEIGHTS_SCALE * sum);
1329  }
1330 
1331  for (int i = 0; i < N; i++) {
1332  /* Compute output. */
1333  float sum = gru->bias[2 * N + i];
1334 
1335  sum += s->fdsp->scalarproduct_float(gru->input_weights + 2 * AM + i * istride, input, AM);
1336  for (int j = 0; j < N; j++)
1337  sum += gru->recurrent_weights[2 * AN + i * stride + j] * state[j] * r[j];
1338 
1339  if (gru->activation == ACTIVATION_SIGMOID)
1340  sum = sigmoid_approx(WEIGHTS_SCALE * sum);
1341  else if (gru->activation == ACTIVATION_TANH)
1342  sum = tansig_approx(WEIGHTS_SCALE * sum);
1343  else if (gru->activation == ACTIVATION_RELU)
1344  sum = FFMAX(0, WEIGHTS_SCALE * sum);
1345  else
1346  av_assert0(0);
1347  h[i] = z[i] * state[i] + (1.f - z[i]) * sum;
1348  }
1349 
1350  RNN_COPY(state, h, N);
1351 }
1352 
1353 static void compute_rnn(AudioRNNContext *s, RNNState *rnn, float *gains, float *vad, const float *input)
1354 {
1355  LOCAL_ALIGNED_32(float, dense_out, [MAX_NEURONS]);
1356  LOCAL_ALIGNED_32(float, noise_input, [MAX_NEURONS * 3]);
1357  LOCAL_ALIGNED_32(float, denoise_input, [MAX_NEURONS * 3]);
1358 
1359  memset(dense_out, 0, MAX_NEURONS * sizeof(float));
1360  memset(noise_input, 0, MAX_NEURONS * 3 * sizeof(float));
1361  memset(denoise_input, 0, MAX_NEURONS * 3 * sizeof(float));
1362  compute_dense(rnn->model->input_dense, dense_out, input);
1363  compute_gru(s, rnn->model->vad_gru, rnn->vad_gru_state, dense_out);
1364  compute_dense(rnn->model->vad_output, vad, rnn->vad_gru_state);
1365 
1366  memcpy(noise_input, dense_out, rnn->model->input_dense_size * sizeof(float));
1367  memcpy(noise_input + rnn->model->input_dense_size,
1368  rnn->vad_gru_state, rnn->model->vad_gru_size * sizeof(float));
1369  memcpy(noise_input + rnn->model->input_dense_size + rnn->model->vad_gru_size,
1370  input, NB_FEATURES * sizeof(float));
1371 
1372  compute_gru(s, rnn->model->noise_gru, rnn->noise_gru_state, noise_input);
1373 
1374  memcpy(denoise_input, rnn->vad_gru_state, rnn->model->vad_gru_size * sizeof(float));
1375  memcpy(denoise_input + rnn->model->vad_gru_size,
1376  rnn->noise_gru_state, rnn->model->noise_gru_size * sizeof(float));
1377  memcpy(denoise_input + rnn->model->vad_gru_size + rnn->model->noise_gru_size,
1378  input, NB_FEATURES * sizeof(float));
1379 
1380  compute_gru(s, rnn->model->denoise_gru, rnn->denoise_gru_state, denoise_input);
1382 }
1383 
1384 static float rnnoise_channel(AudioRNNContext *s, DenoiseState *st, float *out, const float *in,
1385  int disabled)
1386 {
1389  float x[FRAME_SIZE];
1390  float Ex[NB_BANDS], Ep[NB_BANDS];
1391  LOCAL_ALIGNED_32(float, Exp, [FFALIGN(NB_BANDS, 4)]);
1392  float features[NB_FEATURES];
1393  float g[NB_BANDS];
1394  float gf[FREQ_SIZE];
1395  float vad_prob = 0;
1396  float *history = st->history;
1397  static const float a_hp[2] = {-1.99599, 0.99600};
1398  static const float b_hp[2] = {-2, 1};
1399  int silence;
1400 
1401  biquad(x, st->mem_hp_x, in, b_hp, a_hp, FRAME_SIZE);
1402  silence = compute_frame_features(s, st, X, P, Ex, Ep, Exp, features, x);
1403 
1404  if (!silence && !disabled) {
1405  compute_rnn(s, &st->rnn[0], g, &vad_prob, features);
1406  pitch_filter(X, P, Ex, Ep, Exp, g);
1407  for (int i = 0; i < NB_BANDS; i++) {
1408  float alpha = .6f;
1409 
1410  g[i] = FFMAX(g[i], alpha * st->lastg[i]);
1411  st->lastg[i] = g[i];
1412  }
1413 
1414  interp_band_gain(gf, g);
1415 
1416  for (int i = 0; i < FREQ_SIZE; i++) {
1417  X[i].re *= gf[i];
1418  X[i].im *= gf[i];
1419  }
1420  }
1421 
1422  frame_synthesis(s, st, out, X);
1423  memcpy(history, in, FRAME_SIZE * sizeof(*history));
1424 
1425  return vad_prob;
1426 }
1427 
1428 typedef struct ThreadData {
1429  AVFrame *in, *out;
1430 } ThreadData;
1431 
1432 static int rnnoise_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1433 {
1434  AudioRNNContext *s = ctx->priv;
1435  ThreadData *td = arg;
1436  AVFrame *in = td->in;
1437  AVFrame *out = td->out;
1438  const int start = (out->channels * jobnr) / nb_jobs;
1439  const int end = (out->channels * (jobnr+1)) / nb_jobs;
1440 
1441  for (int ch = start; ch < end; ch++) {
1442  rnnoise_channel(s, &s->st[ch],
1443  (float *)out->extended_data[ch],
1444  (const float *)in->extended_data[ch],
1445  ctx->is_disabled);
1446  }
1447 
1448  return 0;
1449 }
1450 
1451 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
1452 {
1453  AVFilterContext *ctx = inlink->dst;
1454  AVFilterLink *outlink = ctx->outputs[0];
1455  AVFrame *out = NULL;
1456  ThreadData td;
1457 
1458  out = ff_get_audio_buffer(outlink, FRAME_SIZE);
1459  if (!out) {
1460  av_frame_free(&in);
1461  return AVERROR(ENOMEM);
1462  }
1463  out->pts = in->pts;
1464 
1465  td.in = in; td.out = out;
1466  ctx->internal->execute(ctx, rnnoise_channels, &td, NULL, FFMIN(outlink->channels,
1468 
1469  av_frame_free(&in);
1470  return ff_filter_frame(outlink, out);
1471 }
1472 
1474 {
1475  AVFilterLink *inlink = ctx->inputs[0];
1476  AVFilterLink *outlink = ctx->outputs[0];
1477  AVFrame *in = NULL;
1478  int ret;
1479 
1480  FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
1481 
1483  if (ret < 0)
1484  return ret;
1485 
1486  if (ret > 0)
1487  return filter_frame(inlink, in);
1488 
1489  FF_FILTER_FORWARD_STATUS(inlink, outlink);
1490  FF_FILTER_FORWARD_WANTED(outlink, inlink);
1491 
1492  return FFERROR_NOT_READY;
1493 }
1494 
1496 {
1497  AudioRNNContext *s = ctx->priv;
1498  int ret;
1499  FILE *f;
1500 
1501  if (!s->model_name)
1502  return AVERROR(EINVAL);
1503  f = av_fopen_utf8(s->model_name, "r");
1504  if (!f) {
1505  av_log(ctx, AV_LOG_ERROR, "Failed to open model file: %s\n", s->model_name);
1506  return AVERROR(EINVAL);
1507  }
1508 
1509  ret = rnnoise_model_from_file(f, model);
1510  fclose(f);
1511  if (!*model || ret < 0)
1512  return ret;
1513 
1514  return 0;
1515 }
1516 
1518 {
1519  AudioRNNContext *s = ctx->priv;
1520  int ret;
1521 
1522  s->fdsp = avpriv_float_dsp_alloc(0);
1523  if (!s->fdsp)
1524  return AVERROR(ENOMEM);
1525 
1526  ret = open_model(ctx, &s->model[0]);
1527  if (ret < 0)
1528  return ret;
1529 
1530  for (int i = 0; i < FRAME_SIZE; i++) {
1531  s->window[i] = sin(.5*M_PI*sin(.5*M_PI*(i+.5)/FRAME_SIZE) * sin(.5*M_PI*(i+.5)/FRAME_SIZE));
1532  s->window[WINDOW_SIZE - 1 - i] = s->window[i];
1533  }
1534 
1535  for (int i = 0; i < NB_BANDS; i++) {
1536  for (int j = 0; j < NB_BANDS; j++) {
1537  s->dct_table[j][i] = cosf((i + .5f) * j * M_PI / NB_BANDS);
1538  if (j == 0)
1539  s->dct_table[j][i] *= sqrtf(.5);
1540  }
1541  }
1542 
1543  return 0;
1544 }
1545 
1546 static void free_model(AVFilterContext *ctx, int n)
1547 {
1548  AudioRNNContext *s = ctx->priv;
1549 
1550  rnnoise_model_free(s->model[n]);
1551  s->model[n] = NULL;
1552 
1553  for (int ch = 0; ch < s->channels && s->st; ch++) {
1554  av_freep(&s->st[ch].rnn[n].vad_gru_state);
1555  av_freep(&s->st[ch].rnn[n].noise_gru_state);
1556  av_freep(&s->st[ch].rnn[n].denoise_gru_state);
1557  }
1558 }
1559 
1560 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
1561  char *res, int res_len, int flags)
1562 {
1563  AudioRNNContext *s = ctx->priv;
1564  int ret;
1565 
1566  ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
1567  if (ret < 0)
1568  return ret;
1569 
1570  ret = open_model(ctx, &s->model[1]);
1571  if (ret < 0)
1572  return ret;
1573 
1574  FFSWAP(RNNModel *, s->model[0], s->model[1]);
1575  for (int ch = 0; ch < s->channels; ch++)
1576  FFSWAP(RNNState, s->st[ch].rnn[0], s->st[ch].rnn[1]);
1577 
1578  ret = config_input(ctx->inputs[0]);
1579  if (ret < 0) {
1580  for (int ch = 0; ch < s->channels; ch++)
1581  FFSWAP(RNNState, s->st[ch].rnn[0], s->st[ch].rnn[1]);
1582  FFSWAP(RNNModel *, s->model[0], s->model[1]);
1583  return ret;
1584  }
1585 
1586  free_model(ctx, 1);
1587  return 0;
1588 }
1589 
1591 {
1592  AudioRNNContext *s = ctx->priv;
1593 
1594  av_freep(&s->fdsp);
1595  free_model(ctx, 0);
1596  for (int ch = 0; ch < s->channels && s->st; ch++) {
1597  av_tx_uninit(&s->st[ch].tx);
1598  av_tx_uninit(&s->st[ch].txi);
1599  }
1600  av_freep(&s->st);
1601 }
1602 
1603 static const AVFilterPad inputs[] = {
1604  {
1605  .name = "default",
1606  .type = AVMEDIA_TYPE_AUDIO,
1607  .config_props = config_input,
1608  },
1609  { NULL }
1610 };
1611 
1612 static const AVFilterPad outputs[] = {
1613  {
1614  .name = "default",
1615  .type = AVMEDIA_TYPE_AUDIO,
1616  },
1617  { NULL }
1618 };
1619 
1620 #define OFFSET(x) offsetof(AudioRNNContext, x)
1621 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
1622 
1623 static const AVOption arnndn_options[] = {
1624  { "model", "set model name", OFFSET(model_name), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, AF },
1625  { "m", "set model name", OFFSET(model_name), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, AF },
1626  { "mix", "set output vs input mix", OFFSET(mix), AV_OPT_TYPE_FLOAT, {.dbl=1.0},-1, 1, AF },
1627  { NULL }
1628 };
1629 
1631 
1633  .name = "arnndn",
1634  .description = NULL_IF_CONFIG_SMALL("Reduce noise from speech using Recurrent Neural Networks."),
1635  .query_formats = query_formats,
1636  .priv_size = sizeof(AudioRNNContext),
1637  .priv_class = &arnndn_class,
1638  .activate = activate,
1639  .init = init,
1640  .uninit = uninit,
1641  .inputs = inputs,
1642  .outputs = outputs,
1646 };
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:925
static float compute_pitch_gain(float xy, float xx, float yy)
Definition: af_arnndn.c:818
static int compute_frame_features(AudioRNNContext *s, DenoiseState *st, AVComplexFloat *X, AVComplexFloat *P, float *Ex, float *Ep, float *Exp, float *features, const float *in)
Definition: af_arnndn.c:1041
AVFilter ff_af_arnndn
Definition: af_arnndn.c:1632
#define WEIGHTS_SCALE
Definition: af_arnndn.c:66
static const uint8_t second_check[16]
Definition: af_arnndn.c:823
static void dual_inner_prod(const float *x, const float *y01, const float *y02, int N, float *xy1, float *xy2)
Definition: af_arnndn.c:804
#define ACTIVATION_TANH
Definition: af_arnndn.c:70
static float remove_doubling(float *x, int maxperiod, int minperiod, int N, int *T0_, int prev_period, float prev_gain)
Definition: af_arnndn.c:824
static void inverse_transform(DenoiseState *st, float *out, const AVComplexFloat *in)
Definition: af_arnndn.c:445
#define CEPS_MEM
Definition: af_arnndn.c:61
static int celt_autocorr(const float *x, float *ac, const float *window, int overlap, int lag, int n)
Definition: af_arnndn.c:648
static void forward_transform(DenoiseState *st, AVComplexFloat *out, const float *in)
Definition: af_arnndn.c:430
static void pitch_downsample(float *x[], float *x_lp, int len, int C)
Definition: af_arnndn.c:762
#define FREE_DENSE(name)
AVFILTER_DEFINE_CLASS(arnndn)
static int rnnoise_model_from_file(FILE *f, RNNModel **rnn)
Definition: af_arnndn.c:188
#define ACTIVATION_SIGMOID
Definition: af_arnndn.c:71
static int open_model(AVFilterContext *ctx, RNNModel **model)
Definition: af_arnndn.c:1495
#define ALLOC_LAYER(type, name)
#define FREQ_SIZE
Definition: af_arnndn.c:50
#define FRAME_SIZE_SHIFT
Definition: af_arnndn.c:47
static int query_formats(AVFilterContext *ctx)
Definition: af_arnndn.c:338
static float celt_inner_prod(const float *x, const float *y, int N)
Definition: af_arnndn.c:616
static void free_model(AVFilterContext *ctx, int n)
Definition: af_arnndn.c:1546
static void pitch_filter(AVComplexFloat *X, const AVComplexFloat *P, const float *Ex, const float *Ep, const float *Exp, const float *g)
Definition: af_arnndn.c:1166
static int config_input(AVFilterLink *inlink)
Definition: af_arnndn.c:369
#define RNN_CLEAR(dst, n)
Definition: af_arnndn.c:427
#define PITCH_MAX_PERIOD
Definition: af_arnndn.c:53
static void frame_synthesis(AudioRNNContext *s, DenoiseState *st, float *out, const AVComplexFloat *y)
Definition: af_arnndn.c:530
static float sigmoid_approx(float x)
Definition: af_arnndn.c:1269
#define NB_BANDS
Definition: af_arnndn.c:59
#define Q15ONE
Definition: af_arnndn.c:74
#define MAX_NEURONS
Definition: af_arnndn.c:68
static const AVFilterPad inputs[]
Definition: af_arnndn.c:1603
static void compute_gru(AudioRNNContext *s, const GRULayer *gru, float *state, const float *input)
Definition: af_arnndn.c:1302
#define INPUT_DENSE(name)
#define AF
Definition: af_arnndn.c:1621
static void interp_band_gain(float *g, const float *bandE)
Definition: af_arnndn.c:1151
#define RNN_COPY(dst, src, n)
Definition: af_arnndn.c:428
static const AVFilterPad outputs[]
Definition: af_arnndn.c:1612
#define PITCH_MIN_PERIOD
Definition: af_arnndn.c:52
#define INPUT_GRU(name)
static void compute_band_corr(float *bandE, const AVComplexFloat *X, const AVComplexFloat *P)
Definition: af_arnndn.c:493
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_arnndn.c:1451
#define PITCH_FRAME_SIZE
Definition: af_arnndn.c:54
static void celt_lpc(float *lpc, const float *ac, int p)
Definition: af_arnndn.c:686
static void dct(AudioRNNContext *s, float *out, const float *in)
Definition: af_arnndn.c:1031
#define SQUARE(x)
Definition: af_arnndn.c:57
static const AVOption arnndn_options[]
Definition: af_arnndn.c:1623
static int rnnoise_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_arnndn.c:1432
#define WINDOW_SIZE
Definition: af_arnndn.c:49
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_arnndn.c:1560
static int activate(AVFilterContext *ctx)
Definition: af_arnndn.c:1473
static void compute_band_energy(float *bandE, const AVComplexFloat *X)
Definition: af_arnndn.c:468
static av_cold int init(AVFilterContext *ctx)
Definition: af_arnndn.c:1517
static void frame_analysis(AudioRNNContext *s, DenoiseState *st, AVComplexFloat *X, float *Ex, const float *in)
Definition: af_arnndn.c:518
#define NB_DELTA_CEPS
Definition: af_arnndn.c:62
#define RNN_MOVE(dst, src, n)
Definition: af_arnndn.c:426
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_arnndn.c:1590
static float rnnoise_channel(AudioRNNContext *s, DenoiseState *st, float *out, const float *in, int disabled)
Definition: af_arnndn.c:1384
static void rnnoise_model_free(RNNModel *model)
Definition: af_arnndn.c:158
static void celt_fir5(const float *x, const float *num, float *y, int N, float *mem)
Definition: af_arnndn.c:719
#define ACTIVATION_RELU
Definition: af_arnndn.c:72
#define OFFSET(x)
Definition: af_arnndn.c:1620
static void compute_dense(const DenseLayer *layer, float *output, const float *input)
Definition: af_arnndn.c:1274
#define FREE_GRU(name)
static void compute_rnn(AudioRNNContext *s, RNNState *rnn, float *gains, float *vad, const float *input)
Definition: af_arnndn.c:1353
static void celt_pitch_xcorr(const float *x, const float *y, float *xcorr, int len, int max_pitch)
Definition: af_arnndn.c:627
static float tansig_approx(float x)
Definition: af_arnndn.c:1241
#define PITCH_BUF_SIZE
Definition: af_arnndn.c:55
static void xcorr_kernel(const float *x, const float *y, float sum[4], int len)
Definition: af_arnndn.c:547
static void find_best_pitch(float *xcorr, float *y, int len, int max_pitch, int *best_pitch)
Definition: af_arnndn.c:927
static void biquad(float *y, float mem[2], const float *x, const float *b, const float *a, int N)
Definition: af_arnndn.c:412
static void pitch_search(const float *x_lp, float *y, int len, int max_pitch, int *pitch)
Definition: af_arnndn.c:974
#define NB_FEATURES
Definition: af_arnndn.c:64
static const uint8_t eband5ms[]
Definition: af_arnndn.c:463
static const float tansig_table[201]
Definition: af_arnndn.c:1197
#define FRAME_SIZE
Definition: af_arnndn.c:48
#define N
Definition: af_mcompand.c:54
#define T(x)
Definition: vp56_arith.h:29
#define av_cold
Definition: attributes.h:88
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
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
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define E
Definition: avdct.c:32
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1099
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:885
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:1513
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:805
Main libavfilter public API header.
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define xi(width, name, var, range_min, range_max, subs,...)
Definition: cbs_h2645.c:414
#define s(width, name)
Definition: cbs_vp9.c:257
#define f(width, name)
Definition: cbs_vp9.c:255
static struct @322 state
#define FFSWAP(type, a, b)
Definition: common.h:108
#define FFMIN(a, b)
Definition: common.h:105
#define FFMAX(a, b)
Definition: common.h:103
#define av_clipf
Definition: common.h:170
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define NULL
Definition: coverity.c:32
static __device__ float floor(float a)
Definition: cuda_runtime.h:173
#define M(chr)
Definition: exr.c:175
sample_rates
int
static SDL_Window * window
Definition: ffplay.c:366
float im
Definition: fft.c:82
float re
Definition: fft.c:82
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition: filters.h:265
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition: filters.h:237
#define FFERROR_NOT_READY
Filters implementation helper functions.
Definition: filters.h:34
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:210
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition.
Definition: formats.c:436
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:587
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:575
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates.
Definition: formats.c:568
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
#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:134
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#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:203
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:117
FILE * av_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
Definition: file_open.c:158
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
static const int16_t alpha[]
Definition: ilbcdata.h:55
int i
Definition: input.c:407
static int mix(int c0, int c1)
Definition: 4xm.c:716
#define C
const char * arg
Definition: jacosubdec.c:67
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define isnan(x)
Definition: libm.h:340
#define cosf(x)
Definition: libm.h:78
#define log10f(x)
Definition: libm.h:414
int stride
Definition: mace.c:144
#define FFALIGN(x, a)
Definition: macros.h:48
#define M_PI
Definition: mathematics.h:52
#define LOCAL_ALIGNED_32(t, v,...)
Definition: mem_internal.h:117
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
#define P
static const uint64_t c1
Definition: murmur3.c:51
AVOptions.
#define td
Definition: regdef.h:70
formats
Definition: signature.h:48
static int shift(int a, int b)
Definition: sonic.c:82
Describe the class of an AVClass context structure.
Definition: log.h:67
float im
Definition: tx.h:28
float re
Definition: tx.h:28
A list of supported channel layouts.
Definition: formats.h:86
An instance of a filter.
Definition: avfilter.h:341
A list of supported formats for one end of a filter link.
Definition: formats.h:65
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
AVOption.
Definition: opt.h:248
RNNModel * model[2]
Definition: af_arnndn.c:149
AVFloatDSPContext * fdsp
Definition: af_arnndn.c:151
char * model_name
Definition: af_arnndn.c:140
DenoiseState * st
Definition: af_arnndn.c:144
float window[WINDOW_SIZE]
Definition: af_arnndn.c:146
float dct_table[FFALIGN(NB_BANDS, 4)][FFALIGN(NB_BANDS, 4)]
Definition: af_arnndn.c:147
float mem_hp_x[2]
Definition: af_arnndn.c:129
float pitch_enh_buf[PITCH_BUF_SIZE]
Definition: af_arnndn.c:126
float analysis_mem[FRAME_SIZE]
Definition: af_arnndn.c:121
float cepstral_mem[CEPS_MEM][NB_BANDS]
Definition: af_arnndn.c:122
RNNState rnn[2]
Definition: af_arnndn.c:132
float synthesis_mem[FRAME_SIZE]
Definition: af_arnndn.c:124
float lastg[NB_BANDS]
Definition: af_arnndn.c:130
AVTXContext * txi
Definition: af_arnndn.c:133
float last_gain
Definition: af_arnndn.c:127
float pitch_buf[PITCH_BUF_SIZE]
Definition: af_arnndn.c:125
av_tx_fn txi_fn
Definition: af_arnndn.c:134
float history[FRAME_SIZE]
Definition: af_arnndn.c:131
AVTXContext * tx
Definition: af_arnndn.c:133
int last_period
Definition: af_arnndn.c:128
av_tx_fn tx_fn
Definition: af_arnndn.c:134
int nb_inputs
Definition: af_arnndn.c:79
const float * input_weights
Definition: af_arnndn.c:78
int activation
Definition: af_arnndn.c:81
int nb_neurons
Definition: af_arnndn.c:80
const float * bias
Definition: af_arnndn.c:77
const float * recurrent_weights
Definition: af_arnndn.c:87
const float * input_weights
Definition: af_arnndn.c:86
int nb_inputs
Definition: af_arnndn.c:88
int nb_neurons
Definition: af_arnndn.c:89
const float * bias
Definition: af_arnndn.c:85
int activation
Definition: af_arnndn.c:90
int vad_gru_size
Definition: af_arnndn.c:97
const DenseLayer * denoise_output
Definition: af_arnndn.c:107
const DenseLayer * input_dense
Definition: af_arnndn.c:95
int denoise_gru_size
Definition: af_arnndn.c:103
int vad_output_size
Definition: af_arnndn.c:109
const DenseLayer * vad_output
Definition: af_arnndn.c:110
int denoise_output_size
Definition: af_arnndn.c:106
int input_dense_size
Definition: af_arnndn.c:94
const GRULayer * vad_gru
Definition: af_arnndn.c:98
const GRULayer * noise_gru
Definition: af_arnndn.c:101
const GRULayer * denoise_gru
Definition: af_arnndn.c:104
int noise_gru_size
Definition: af_arnndn.c:100
float * denoise_gru_state
Definition: af_arnndn.c:116
float * noise_gru_state
Definition: af_arnndn.c:115
float * vad_gru_state
Definition: af_arnndn.c:114
RNNModel * model
Definition: af_arnndn.c:117
Used for passing data between threads.
Definition: dsddec.c:67
AVFrame * out
Definition: af_adeclick.c:505
AVFrame * in
Definition: af_adenorm.c:223
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
static uint8_t tmp[11]
Definition: aes_ctr.c:27
#define src
Definition: vp8dsp.c:255
FILE * out
Definition: movenc.c:54
AVFormatContext * ctx
Definition: movenc.c:48
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets ctx to NULL, does nothing when ctx == NULL.
Definition: tx.c:146
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition: tx.c:160
@ AV_TX_FLOAT_FFT
Standard complex to complex FFT with sample data type AVComplexFloat.
Definition: tx.h:45
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition: tx.h:99
@ X
Definition: vf_addroi.c:26
const char * b
Definition: vf_curves.c:119
const char * g
Definition: vf_curves.c:118
const char * r
Definition: vf_curves.c:117
if(ret< 0)
Definition: vf_mcdeint.c:282
static const uint8_t offset[127][2]
Definition: vf_spp.c:107
int len
static double c[64]