10260
|
1 #include "common1428.h"
|
|
2 #include "decode288.h"
|
|
3 #include "tables288.h"
|
|
4
|
|
5 /* Initialize internal variable structure */
|
|
6 Real_288 *init_288(void)
|
|
7 {
|
|
8 Real_internal *glob;
|
|
9
|
|
10 if ((glob=malloc(sizeof(Real_internal))))
|
|
11 memset(glob,0,sizeof(Real_internal));
|
|
12
|
|
13 return (Real_288 *)glob;
|
|
14 }
|
|
15
|
|
16 /* Free internal variable structure */
|
|
17 void free_288(Real_288 *global)
|
|
18 {
|
|
19 if (!global) return;
|
|
20 free(global);
|
|
21 }
|
|
22
|
|
23 /* Unlike the 14.4 format, 28.8 blocks are interleaved */
|
|
24 /* to dilute the effects of transmission errors */
|
|
25 void deinterleave(unsigned char *in, unsigned char *out, unsigned int size)
|
|
26 {
|
|
27 unsigned int x=0,y=0,z=0;
|
|
28 if (size>=38) z=size-38;
|
|
29 else return;
|
|
30
|
|
31 while (x<=z)
|
|
32 {
|
|
33 memcpy(out+y,in+x,38);
|
|
34 x+=38;y+=456;
|
|
35 if (y>z) y-=z;
|
|
36 }
|
|
37 }
|
|
38
|
|
39 /* Decode a block (celp) */
|
|
40 void decode_288(Real_288 *global, unsigned char *in, signed short int *out)
|
|
41 {
|
|
42 int x,y;
|
|
43 unsigned short int buffer[32];
|
|
44 Real_internal *glob;
|
|
45
|
|
46 if (!global) return;
|
|
47 glob = (Real_internal *)global;
|
|
48
|
|
49 unpack(buffer,in,32);
|
|
50 for (x=0;x<32;x++)
|
|
51 {
|
|
52 glob->phasep=(glob->phase=x&7)*5;
|
|
53 decode(glob,buffer[x]);
|
|
54 for (y=0;y<5;*(out++)=8*glob->output[glob->phasep+(y++)]);
|
|
55 if (glob->phase==3) update(glob);
|
|
56 }
|
|
57 }
|
|
58
|
|
59 /* initial decode */
|
|
60 static void unpack(unsigned short *tgt, unsigned char *src, int len)
|
|
61 {
|
|
62 int x,y,z;
|
|
63 int n,temp;
|
|
64 int buffer[38];
|
|
65
|
|
66 for (x=0;x<len;tgt[x++]=0)
|
|
67 buffer[x]=9+(x&1);
|
|
68
|
|
69 for (x=y=z=0;x<38;x++) {
|
|
70 n=buffer[y]-z;
|
|
71 temp=src[x];
|
|
72 if (n<8) temp&=255>>(8-n);
|
|
73 tgt[y]+=temp<<z;
|
|
74 if (n<=8) {
|
|
75 tgt[++y]+=src[x]>>n;
|
|
76 z=8-n;
|
|
77 } else z+=8;
|
|
78 }
|
|
79 }
|
|
80
|
|
81 static void update(Real_internal *glob)
|
|
82 {
|
|
83 int x,y;
|
|
84 float buffer1[40],temp1[37];
|
|
85 float buffer2[8],temp2[11];
|
|
86
|
|
87 for (x=0,y=glob->phasep+5;x<40;buffer1[x++]=glob->output[(y++)%40]);
|
|
88 co(36,40,35,buffer1,temp1,glob->st1a,glob->st1b,table1);
|
|
89 if (pred(temp1,glob->st1,36))
|
|
90 colmult(glob->pr1,glob->st1,table1a,36);
|
|
91
|
|
92 for (x=0,y=glob->phase+1;x<8;buffer2[x++]=glob->history[(y++)%8]);
|
|
93 co(10,8,20,buffer2,temp2,glob->st2a,glob->st2b,table2);
|
|
94 if (pred(temp2,glob->st2,10))
|
|
95 colmult(glob->pr2,glob->st2,table2a,10);
|
|
96 }
|
|
97
|
|
98 /* Decode and produce output */
|
|
99 static void decode(Real_internal *glob, unsigned int input)
|
|
100 {
|
|
101 unsigned int x,y;
|
|
102 float f;
|
|
103 double sum,sumsum;
|
|
104 float *p1,*p2;
|
|
105 float buffer[5];
|
|
106 const float *table;
|
|
107
|
|
108 for (x=36;x--;glob->sb[x+5]=glob->sb[x]);
|
|
109 for (x=5;x--;) {
|
|
110 p1=glob->sb+x;p2=glob->pr1;
|
|
111 for (sum=0,y=36;y--;sum-=(*(++p1))*(*(p2++)));
|
|
112 glob->sb[x]=sum;
|
|
113 }
|
|
114
|
|
115 f=amptable[input&7];
|
|
116 table=codetable+(input>>3)*5;
|
|
117
|
|
118 /* convert log and do rms */
|
|
119 for (sum=32,x=10;x--;sum-=glob->pr2[x]*glob->lhist[x]);
|
|
120 if (sum<0) sum=0; else if (sum>60) sum=60;
|
|
121
|
|
122 sumsum=exp(sum*0.1151292546497)*f; /* pow(10.0,sum/20)*f */
|
|
123 for (sum=0,x=5;x--;) { buffer[x]=table[x]*sumsum; sum+=buffer[x]*buffer[x]; }
|
|
124 if ((sum/=5)<1) sum=1;
|
|
125
|
|
126 /* shift and store */
|
|
127 for (x=10;--x;glob->lhist[x]=glob->lhist[x-1]);
|
|
128 *glob->lhist=glob->history[glob->phase]=10*log10(sum)-32;
|
|
129
|
|
130 for (x=1;x<5;x++) for (y=x;y--;buffer[x]-=glob->pr1[x-y-1]*buffer[y]);
|
|
131
|
|
132 /* output */
|
|
133 for (x=0;x<5;x++) {
|
|
134 f=glob->sb[4-x]+buffer[x];
|
|
135 if (f>4095) f=4095; else if (f<-4095) f=-4095;
|
|
136 glob->output[glob->phasep+x]=glob->sb[4-x]=f;
|
|
137 }
|
|
138 }
|
|
139
|
|
140 /* column multiply */
|
|
141 static void colmult(float *tgt, float *m1, const float *m2, int n)
|
|
142 {
|
|
143 while (n--)
|
|
144 *(tgt++)=(*(m1++))*(*(m2++));
|
|
145 }
|
|
146
|
|
147 static int pred(float *in, float *tgt, int n)
|
|
148 {
|
|
149 int x,y;
|
|
150 float *p1,*p2;
|
|
151 double f0,f1,f2;
|
|
152 float temp;
|
|
153
|
|
154 if (in[n]==0) return 0;
|
|
155 if ((f0=*in)<=0) return 0;
|
|
156
|
|
157 for (x=1;;x++) {
|
|
158 if (n<x) return 1;
|
|
159
|
|
160 p1=in+x;
|
|
161 p2=tgt;
|
|
162 f1=*(p1--);
|
|
163 for (y=x;--y;f1+=(*(p1--))*(*(p2++)));
|
|
164
|
|
165 p1=tgt+x-1;
|
|
166 p2=tgt;
|
|
167 *(p1--)=f2=-f1/f0;
|
|
168 for (y=x>>1;y--;) {
|
|
169 temp=*p2+*p1*f2;
|
|
170 *(p1--)+=*p2*f2;
|
|
171 *(p2++)=temp;
|
|
172 }
|
|
173 if ((f0+=f1*f2)<0) return 0;
|
|
174 }
|
|
175 }
|
|
176
|
|
177 static void co(int n, int i, int j, float *in, float *out, float *st1, float *st2, const float *table)
|
|
178 {
|
|
179 int a,b,c;
|
|
180 unsigned int x;
|
|
181 float *fp,*fp2;
|
|
182 float buffer1[37];
|
|
183 float buffer2[37];
|
|
184 float work[111];
|
|
185
|
|
186 /* rotate and multiply */
|
|
187 c=(b=(a=n+i)+j)-i;
|
|
188 fp=st1+i;
|
|
189 for (x=0;x<b;x++) {
|
|
190 if (x==c) fp=in;
|
|
191 work[x]=*(table++)*(*(st1++)=*(fp++));
|
|
192 }
|
|
193
|
|
194 prodsum(buffer1,work+n,i,n);
|
|
195 prodsum(buffer2,work+a,j,n);
|
|
196
|
|
197 for (x=0;x<=n;x++) {
|
|
198 *st2=*st2*(0.5625)+buffer1[x];
|
|
199 out[x]=*(st2++)+buffer2[x];
|
|
200 }
|
|
201 *out*=1.00390625; /* to prevent clipping */
|
|
202 }
|
|
203
|
|
204 /* product sum (lsf) */
|
|
205 static void prodsum(float *tgt, float *src, int len, int n)
|
|
206 {
|
|
207 unsigned int x;
|
|
208 float *p1,*p2;
|
|
209 double sum;
|
|
210
|
|
211 while (n>=0)
|
|
212 {
|
|
213 p1=(p2=src)-n;
|
|
214 for (sum=0,x=len;x--;sum+=(*p1++)*(*p2++));
|
|
215 tgt[n--]=sum;
|
|
216 }
|
|
217 }
|