1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libopus/src/mlp.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,140 @@ 1.4 +/* Copyright (c) 2008-2011 Octasic Inc. 1.5 + Written by Jean-Marc Valin */ 1.6 +/* 1.7 + Redistribution and use in source and binary forms, with or without 1.8 + modification, are permitted provided that the following conditions 1.9 + are met: 1.10 + 1.11 + - Redistributions of source code must retain the above copyright 1.12 + notice, this list of conditions and the following disclaimer. 1.13 + 1.14 + - Redistributions in binary form must reproduce the above copyright 1.15 + notice, this list of conditions and the following disclaimer in the 1.16 + documentation and/or other materials provided with the distribution. 1.17 + 1.18 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.19 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.20 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.21 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR 1.22 + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.23 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.24 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.25 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.26 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.27 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.28 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.29 +*/ 1.30 + 1.31 +#ifdef HAVE_CONFIG_H 1.32 +#include "config.h" 1.33 +#endif 1.34 + 1.35 +#include "opus_types.h" 1.36 +#include "opus_defines.h" 1.37 + 1.38 +#include <math.h> 1.39 +#include "mlp.h" 1.40 +#include "arch.h" 1.41 +#include "tansig_table.h" 1.42 +#define MAX_NEURONS 100 1.43 + 1.44 +#if 0 1.45 +static OPUS_INLINE opus_val16 tansig_approx(opus_val32 _x) /* Q19 */ 1.46 +{ 1.47 + int i; 1.48 + opus_val16 xx; /* Q11 */ 1.49 + /*double x, y;*/ 1.50 + opus_val16 dy, yy; /* Q14 */ 1.51 + /*x = 1.9073e-06*_x;*/ 1.52 + if (_x>=QCONST32(8,19)) 1.53 + return QCONST32(1.,14); 1.54 + if (_x<=-QCONST32(8,19)) 1.55 + return -QCONST32(1.,14); 1.56 + xx = EXTRACT16(SHR32(_x, 8)); 1.57 + /*i = lrint(25*x);*/ 1.58 + i = SHR32(ADD32(1024,MULT16_16(25, xx)),11); 1.59 + /*x -= .04*i;*/ 1.60 + xx -= EXTRACT16(SHR32(MULT16_16(20972,i),8)); 1.61 + /*x = xx*(1./2048);*/ 1.62 + /*y = tansig_table[250+i];*/ 1.63 + yy = tansig_table[250+i]; 1.64 + /*y = yy*(1./16384);*/ 1.65 + dy = 16384-MULT16_16_Q14(yy,yy); 1.66 + yy = yy + MULT16_16_Q14(MULT16_16_Q11(xx,dy),(16384 - MULT16_16_Q11(yy,xx))); 1.67 + return yy; 1.68 +} 1.69 +#else 1.70 +/*extern const float tansig_table[501];*/ 1.71 +static OPUS_INLINE float tansig_approx(float x) 1.72 +{ 1.73 + int i; 1.74 + float y, dy; 1.75 + float sign=1; 1.76 + /* Tests are reversed to catch NaNs */ 1.77 + if (!(x<8)) 1.78 + return 1; 1.79 + if (!(x>-8)) 1.80 + return -1; 1.81 + if (x<0) 1.82 + { 1.83 + x=-x; 1.84 + sign=-1; 1.85 + } 1.86 + i = (int)floor(.5f+25*x); 1.87 + x -= .04f*i; 1.88 + y = tansig_table[i]; 1.89 + dy = 1-y*y; 1.90 + y = y + x*dy*(1 - y*x); 1.91 + return sign*y; 1.92 +} 1.93 +#endif 1.94 + 1.95 +#if 0 1.96 +void mlp_process(const MLP *m, const opus_val16 *in, opus_val16 *out) 1.97 +{ 1.98 + int j; 1.99 + opus_val16 hidden[MAX_NEURONS]; 1.100 + const opus_val16 *W = m->weights; 1.101 + /* Copy to tmp_in */ 1.102 + for (j=0;j<m->topo[1];j++) 1.103 + { 1.104 + int k; 1.105 + opus_val32 sum = SHL32(EXTEND32(*W++),8); 1.106 + for (k=0;k<m->topo[0];k++) 1.107 + sum = MAC16_16(sum, in[k],*W++); 1.108 + hidden[j] = tansig_approx(sum); 1.109 + } 1.110 + for (j=0;j<m->topo[2];j++) 1.111 + { 1.112 + int k; 1.113 + opus_val32 sum = SHL32(EXTEND32(*W++),14); 1.114 + for (k=0;k<m->topo[1];k++) 1.115 + sum = MAC16_16(sum, hidden[k], *W++); 1.116 + out[j] = tansig_approx(EXTRACT16(PSHR32(sum,17))); 1.117 + } 1.118 +} 1.119 +#else 1.120 +void mlp_process(const MLP *m, const float *in, float *out) 1.121 +{ 1.122 + int j; 1.123 + float hidden[MAX_NEURONS]; 1.124 + const float *W = m->weights; 1.125 + /* Copy to tmp_in */ 1.126 + for (j=0;j<m->topo[1];j++) 1.127 + { 1.128 + int k; 1.129 + float sum = *W++; 1.130 + for (k=0;k<m->topo[0];k++) 1.131 + sum = sum + in[k]**W++; 1.132 + hidden[j] = tansig_approx(sum); 1.133 + } 1.134 + for (j=0;j<m->topo[2];j++) 1.135 + { 1.136 + int k; 1.137 + float sum = *W++; 1.138 + for (k=0;k<m->topo[1];k++) 1.139 + sum = sum + hidden[k]**W++; 1.140 + out[j] = tansig_approx(sum); 1.141 + } 1.142 +} 1.143 +#endif