summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libopus/mlp.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libopus/mlp.c')
-rw-r--r--lib/rbcodec/codecs/libopus/mlp.c144
1 files changed, 144 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libopus/mlp.c b/lib/rbcodec/codecs/libopus/mlp.c
new file mode 100644
index 0000000000..964c6a98f6
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/mlp.c
@@ -0,0 +1,144 @@
1/* Copyright (c) 2008-2011 Octasic Inc.
2 2012-2017 Jean-Marc Valin */
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
19 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
32#include <math.h>
33#include "opus_types.h"
34#include "opus_defines.h"
35#include "arch.h"
36#include "tansig_table.h"
37#include "mlp.h"
38
39static OPUS_INLINE float tansig_approx(float x)
40{
41 int i;
42 float y, dy;
43 float sign=1;
44 /* Tests are reversed to catch NaNs */
45 if (!(x<8))
46 return 1;
47 if (!(x>-8))
48 return -1;
49#ifndef FIXED_POINT
50 /* Another check in case of -ffast-math */
51 if (celt_isnan(x))
52 return 0;
53#endif
54 if (x<0)
55 {
56 x=-x;
57 sign=-1;
58 }
59 i = (int)floor(.5f+25*x);
60 x -= .04f*i;
61 y = tansig_table[i];
62 dy = 1-y*y;
63 y = y + x*dy*(1 - y*x);
64 return sign*y;
65}
66
67static OPUS_INLINE float sigmoid_approx(float x)
68{
69 return .5f + .5f*tansig_approx(.5f*x);
70}
71
72static void gemm_accum(float *out, const opus_int8 *weights, int rows, int cols, int col_stride, const float *x)
73{
74 int i, j;
75 for (i=0;i<rows;i++)
76 {
77 for (j=0;j<cols;j++)
78 out[i] += weights[j*col_stride + i]*x[j];
79 }
80}
81
82void compute_dense(const DenseLayer *layer, float *output, const float *input)
83{
84 int i;
85 int N, M;
86 int stride;
87 M = layer->nb_inputs;
88 N = layer->nb_neurons;
89 stride = N;
90 for (i=0;i<N;i++)
91 output[i] = layer->bias[i];
92 gemm_accum(output, layer->input_weights, N, M, stride, input);
93 for (i=0;i<N;i++)
94 output[i] *= WEIGHTS_SCALE;
95 if (layer->sigmoid) {
96 for (i=0;i<N;i++)
97 output[i] = sigmoid_approx(output[i]);
98 } else {
99 for (i=0;i<N;i++)
100 output[i] = tansig_approx(output[i]);
101 }
102}
103
104void compute_gru(const GRULayer *gru, float *state, const float *input)
105{
106 int i;
107 int N, M;
108 int stride;
109 float tmp[MAX_NEURONS];
110 float z[MAX_NEURONS];
111 float r[MAX_NEURONS];
112 float h[MAX_NEURONS];
113 M = gru->nb_inputs;
114 N = gru->nb_neurons;
115 stride = 3*N;
116 /* Compute update gate. */
117 for (i=0;i<N;i++)
118 z[i] = gru->bias[i];
119 gemm_accum(z, gru->input_weights, N, M, stride, input);
120 gemm_accum(z, gru->recurrent_weights, N, N, stride, state);
121 for (i=0;i<N;i++)
122 z[i] = sigmoid_approx(WEIGHTS_SCALE*z[i]);
123
124 /* Compute reset gate. */
125 for (i=0;i<N;i++)
126 r[i] = gru->bias[N + i];
127 gemm_accum(r, &gru->input_weights[N], N, M, stride, input);
128 gemm_accum(r, &gru->recurrent_weights[N], N, N, stride, state);
129 for (i=0;i<N;i++)
130 r[i] = sigmoid_approx(WEIGHTS_SCALE*r[i]);
131
132 /* Compute output. */
133 for (i=0;i<N;i++)
134 h[i] = gru->bias[2*N + i];
135 for (i=0;i<N;i++)
136 tmp[i] = state[i] * r[i];
137 gemm_accum(h, &gru->input_weights[2*N], N, M, stride, input);
138 gemm_accum(h, &gru->recurrent_weights[2*N], N, N, stride, tmp);
139 for (i=0;i<N;i++)
140 h[i] = z[i]*state[i] + (1-z[i])*tansig_approx(WEIGHTS_SCALE*h[i]);
141 for (i=0;i<N;i++)
142 state[i] = h[i];
143}
144