Mercurial > mplayer.hg
comparison xa/xa_gsm.c @ 1:3b5f5d1c5041
Initial revision
author | arpi_esp |
---|---|
date | Sat, 24 Feb 2001 20:28:24 +0000 |
parents | |
children | d2d534253395 |
comparison
equal
deleted
inserted
replaced
0:c1bb2c071d63 | 1:3b5f5d1c5041 |
---|---|
1 | |
2 /******************************************************************** | |
3 Copyright 1992 by Jutta Degener and Carsten Bormann, | |
4 Technische Universitaet Berlin | |
5 | |
6 Any use of this software is permitted provided that this notice is not | |
7 removed and that neither the authors nor the Technische Universitaet Berlin | |
8 are deemed to have made any representations as to the suitability of this | |
9 software for any purpose nor are held responsible for any defects of | |
10 this software. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE. | |
11 | |
12 As a matter of courtesy, the authors request to be informed about uses | |
13 this software has found, about bugs in this software, and about any | |
14 improvements that may be of general interest. | |
15 | |
16 Berlin, 15.09.1992 | |
17 Jutta Degener | |
18 Carsten Bormann | |
19 ********************************************************************/ | |
20 | |
21 | |
22 #include <stdio.h> | |
23 #include <assert.h> /* POD optional */ | |
24 #include "xa_gsm_int.h" | |
25 | |
26 //void XA_MSGSM_Decoder(); | |
27 static void GSM_Decode(); | |
28 static void Gsm_RPE_Decoding(); | |
29 | |
30 //static short gsm_buf[320]; | |
31 static XA_GSM_STATE gsm_state; | |
32 | |
33 unsigned char xa_sign_2_ulaw[256]; | |
34 | |
35 unsigned char XA_Signed_To_uLaw(long ch) | |
36 { | |
37 long mask; | |
38 if (ch < 0) { ch = -ch; mask = 0x7f; } | |
39 else { mask = 0xff; } | |
40 if (ch < 32) { ch = 0xF0 | (15 - (ch / 2)); } | |
41 else if (ch < 96) { ch = 0xE0 | (15 - (ch - 32) / 4); } | |
42 else if (ch < 224) { ch = 0xD0 | (15 - (ch - 96) / 8); } | |
43 else if (ch < 480) { ch = 0xC0 | (15 - (ch - 224) / 16); } | |
44 else if (ch < 992) { ch = 0xB0 | (15 - (ch - 480) / 32); } | |
45 else if (ch < 2016) { ch = 0xA0 | (15 - (ch - 992) / 64); } | |
46 else if (ch < 4064) { ch = 0x90 | (15 - (ch - 2016) / 128); } | |
47 else if (ch < 8160) { ch = 0x80 | (15 - (ch - 4064) / 256); } | |
48 else { ch = 0x80; } | |
49 return (mask & ch); | |
50 } | |
51 | |
52 void Gen_Signed_2_uLaw() | |
53 { | |
54 unsigned long i; | |
55 for(i=0;i<256;i++) | |
56 { unsigned char d; | |
57 char ch = i; | |
58 long chr = ch; | |
59 d = XA_Signed_To_uLaw(chr * 16); | |
60 xa_sign_2_ulaw[i] = d; | |
61 } | |
62 } | |
63 | |
64 | |
65 void GSM_Init() | |
66 { | |
67 memset((char *)(&gsm_state), 0, sizeof(XA_GSM_STATE)); | |
68 gsm_state.nrp = 40; | |
69 Gen_Signed_2_uLaw(); | |
70 } | |
71 | |
72 | |
73 /* Table 4.3b Quantization levels of the LTP gain quantizer | |
74 */ | |
75 /* bc 0 1 2 3 */ | |
76 static word gsm_QLB[4] = { 3277, 11469, 21299, 32767 }; | |
77 | |
78 /* Table 4.6 Normalized direct mantissa used to compute xM/xmax | |
79 */ | |
80 /* i 0 1 2 3 4 5 6 7 */ | |
81 static word gsm_FAC[8] = { 18431, 20479, 22527, 24575, 26623, 28671, 30719, 32767 }; | |
82 | |
83 | |
84 | |
85 /****************/ | |
86 #define saturate(x) \ | |
87 ((x) < MIN_WORD ? MIN_WORD : (x) > MAX_WORD ? MAX_WORD: (x)) | |
88 | |
89 /****************/ | |
90 static word gsm_sub (a,b) | |
91 word a; | |
92 word b; | |
93 { | |
94 longword diff = (longword)a - (longword)b; | |
95 return saturate(diff); | |
96 } | |
97 | |
98 /****************/ | |
99 static word gsm_asr (a,n) | |
100 word a; | |
101 int n; | |
102 { | |
103 if (n >= 16) return -(a < 0); | |
104 if (n <= -16) return 0; | |
105 if (n < 0) return a << -n; | |
106 | |
107 # ifdef SASR | |
108 return a >> n; | |
109 # else | |
110 if (a >= 0) return a >> n; | |
111 else return -(word)( -(uword)a >> n ); | |
112 # endif | |
113 } | |
114 | |
115 /****************/ | |
116 static word gsm_asl (a,n) | |
117 word a; | |
118 int n; | |
119 { | |
120 if (n >= 16) return 0; | |
121 if (n <= -16) return -(a < 0); | |
122 if (n < 0) return gsm_asr(a, -n); | |
123 return a << n; | |
124 } | |
125 | |
126 | |
127 /* | |
128 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische | |
129 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for | |
130 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE. | |
131 */ | |
132 | |
133 /**** 4.2.17 */ | |
134 static void RPE_grid_positioning(Mc,xMp,ep) | |
135 word Mc; /* grid position IN */ | |
136 register word * xMp; /* [0..12] IN */ | |
137 register word * ep; /* [0..39] OUT */ | |
138 /* | |
139 * This procedure computes the reconstructed long term residual signal | |
140 * ep[0..39] for the LTP analysis filter. The inputs are the Mc | |
141 * which is the grid position selection and the xMp[0..12] decoded | |
142 * RPE samples which are upsampled by a factor of 3 by inserting zero | |
143 * values. | |
144 */ | |
145 { | |
146 int i = 13; | |
147 | |
148 assert(0 <= Mc && Mc <= 3); | |
149 | |
150 switch (Mc) { | |
151 case 3: *ep++ = 0; | |
152 case 2: do { | |
153 *ep++ = 0; | |
154 case 1: *ep++ = 0; | |
155 case 0: *ep++ = *xMp++; | |
156 } while (--i); | |
157 } | |
158 while (++Mc < 4) *ep++ = 0; | |
159 | |
160 /* | |
161 | |
162 int i, k; | |
163 for (k = 0; k <= 39; k++) ep[k] = 0; | |
164 for (i = 0; i <= 12; i++) { | |
165 ep[ Mc + (3*i) ] = xMp[i]; | |
166 } | |
167 */ | |
168 } | |
169 | |
170 | |
171 /**** 4.2.16 */ | |
172 static void APCM_inverse_quantization (xMc,mant,exp,xMp) | |
173 register word * xMc; /* [0..12] IN */ | |
174 word mant; | |
175 word exp; | |
176 register word * xMp; /* [0..12] OUT */ | |
177 /* | |
178 * This part is for decoding the RPE sequence of coded xMc[0..12] | |
179 * samples to obtain the xMp[0..12] array. Table 4.6 is used to get | |
180 * the mantissa of xmaxc (FAC[0..7]). | |
181 */ | |
182 { | |
183 int i; | |
184 word temp, temp1, temp2, temp3; | |
185 longword ltmp; | |
186 | |
187 assert( mant >= 0 && mant <= 7 ); | |
188 | |
189 temp1 = gsm_FAC[ mant ]; /* see 4.2-15 for mant */ | |
190 temp2 = gsm_sub( 6, exp ); /* see 4.2-15 for exp */ | |
191 temp3 = gsm_asl( 1, gsm_sub( temp2, 1 )); | |
192 | |
193 for (i = 13; i--;) { | |
194 | |
195 assert( *xMc <= 7 && *xMc >= 0 ); /* 3 bit unsigned */ | |
196 | |
197 /* temp = gsm_sub( *xMc++ << 1, 7 ); */ | |
198 temp = (*xMc++ << 1) - 7; /* restore sign */ | |
199 assert( temp <= 7 && temp >= -7 ); /* 4 bit signed */ | |
200 | |
201 temp <<= 12; /* 16 bit signed */ | |
202 temp = GSM_MULT_R( temp1, temp ); | |
203 temp = GSM_ADD( temp, temp3 ); | |
204 *xMp++ = gsm_asr( temp, temp2 ); | |
205 } | |
206 } | |
207 | |
208 | |
209 /**** 4.12.15 */ | |
210 static void APCM_quantization_xmaxc_to_exp_mant (xmaxc,exp_out,mant_out) | |
211 word xmaxc; /* IN */ | |
212 word * exp_out; /* OUT */ | |
213 word * mant_out; /* OUT */ | |
214 { | |
215 word exp, mant; | |
216 | |
217 /* Compute exponent and mantissa of the decoded version of xmaxc | |
218 */ | |
219 | |
220 exp = 0; | |
221 if (xmaxc > 15) exp = SASR(xmaxc, 3) - 1; | |
222 mant = xmaxc - (exp << 3); | |
223 | |
224 if (mant == 0) { | |
225 exp = -4; | |
226 mant = 7; | |
227 } | |
228 else { | |
229 while (mant <= 7) { | |
230 mant = mant << 1 | 1; | |
231 exp--; | |
232 } | |
233 mant -= 8; | |
234 } | |
235 | |
236 assert( exp >= -4 && exp <= 6 ); | |
237 assert( mant >= 0 && mant <= 7 ); | |
238 | |
239 *exp_out = exp; | |
240 *mant_out = mant; | |
241 } | |
242 | |
243 static void Gsm_RPE_Decoding (S, xmaxcr, Mcr, xMcr, erp) | |
244 XA_GSM_STATE * S; | |
245 word xmaxcr; | |
246 word Mcr; | |
247 word * xMcr; /* [0..12], 3 bits IN */ | |
248 word * erp; /* [0..39] OUT */ | |
249 | |
250 { | |
251 word exp, mant; | |
252 word xMp[ 13 ]; | |
253 | |
254 APCM_quantization_xmaxc_to_exp_mant( xmaxcr, &exp, &mant ); | |
255 APCM_inverse_quantization( xMcr, mant, exp, xMp ); | |
256 RPE_grid_positioning( Mcr, xMp, erp ); | |
257 | |
258 } | |
259 | |
260 | |
261 /* | |
262 * 4.3 FIXED POINT IMPLEMENTATION OF THE RPE-LTP DECODER | |
263 */ | |
264 | |
265 static void Postprocessing(S,s) | |
266 XA_GSM_STATE * S; | |
267 register word * s; | |
268 { | |
269 register int k; | |
270 register word msr = S->msr; | |
271 register longword ltmp; /* for GSM_ADD */ | |
272 register word tmp; | |
273 | |
274 for (k = 160; k--; s++) | |
275 { | |
276 tmp = GSM_MULT_R( msr, 28180 ); | |
277 msr = GSM_ADD(*s, tmp); /* Deemphasis */ | |
278 *s = GSM_ADD(msr, msr) & 0xFFF8; /* Truncation & Upscaling */ | |
279 } | |
280 S->msr = msr; | |
281 } | |
282 | |
283 /**** 4.3.2 */ | |
284 void Gsm_Long_Term_Synthesis_Filtering (S,Ncr,bcr,erp,drp) | |
285 XA_GSM_STATE * S; | |
286 word Ncr; | |
287 word bcr; | |
288 register word * erp; /* [0..39] IN */ | |
289 register word * drp; /* [-120..-1] IN, [-120..40] OUT */ | |
290 | |
291 /* | |
292 * This procedure uses the bcr and Ncr parameter to realize the | |
293 * long term synthesis filtering. The decoding of bcr needs | |
294 * table 4.3b. | |
295 */ | |
296 { | |
297 register longword ltmp; /* for ADD */ | |
298 register int k; | |
299 word brp, drpp, Nr; | |
300 | |
301 /* Check the limits of Nr. | |
302 */ | |
303 Nr = Ncr < 40 || Ncr > 120 ? S->nrp : Ncr; | |
304 S->nrp = Nr; | |
305 assert(Nr >= 40 && Nr <= 120); | |
306 | |
307 /* Decoding of the LTP gain bcr | |
308 */ | |
309 brp = gsm_QLB[ bcr ]; | |
310 | |
311 /* Computation of the reconstructed short term residual | |
312 * signal drp[0..39] | |
313 */ | |
314 assert(brp != MIN_WORD); | |
315 | |
316 for (k = 0; k <= 39; k++) { | |
317 drpp = GSM_MULT_R( brp, drp[ k - Nr ] ); | |
318 drp[k] = GSM_ADD( erp[k], drpp ); | |
319 } | |
320 | |
321 /* | |
322 * Update of the reconstructed short term residual signal | |
323 * drp[ -1..-120 ] | |
324 */ | |
325 | |
326 for (k = 0; k <= 119; k++) drp[ -120 + k ] = drp[ -80 + k ]; | |
327 } | |
328 | |
329 static void Short_term_synthesis_filtering (S,rrp,k,wt,sr) | |
330 XA_GSM_STATE *S; | |
331 register word *rrp; /* [0..7] IN */ | |
332 register int k; /* k_end - k_start */ | |
333 register word *wt; /* [0..k-1] IN */ | |
334 register word *sr; /* [0..k-1] OUT */ | |
335 { | |
336 register word * v = S->v; | |
337 register int i; | |
338 register word sri, tmp1, tmp2; | |
339 register longword ltmp; /* for GSM_ADD & GSM_SUB */ | |
340 | |
341 while (k--) { | |
342 sri = *wt++; | |
343 for (i = 8; i--;) { | |
344 | |
345 /* sri = GSM_SUB( sri, gsm_mult_r( rrp[i], v[i] ) ); | |
346 */ | |
347 tmp1 = rrp[i]; | |
348 tmp2 = v[i]; | |
349 tmp2 = ( tmp1 == MIN_WORD && tmp2 == MIN_WORD | |
350 ? MAX_WORD | |
351 : 0x0FFFF & (( (longword)tmp1 * (longword)tmp2 | |
352 + 16384) >> 15)) ; | |
353 | |
354 sri = GSM_SUB( sri, tmp2 ); | |
355 | |
356 /* v[i+1] = GSM_ADD( v[i], gsm_mult_r( rrp[i], sri ) ); | |
357 */ | |
358 tmp1 = ( tmp1 == MIN_WORD && sri == MIN_WORD | |
359 ? MAX_WORD | |
360 : 0x0FFFF & (( (longword)tmp1 * (longword)sri | |
361 + 16384) >> 15)) ; | |
362 | |
363 v[i+1] = GSM_ADD( v[i], tmp1); | |
364 } | |
365 *sr++ = v[0] = sri; | |
366 } | |
367 } | |
368 | |
369 /* 4.2.8 */ | |
370 | |
371 static void Decoding_of_the_coded_Log_Area_Ratios (LARc,LARpp) | |
372 word * LARc; /* coded log area ratio [0..7] IN */ | |
373 word * LARpp; /* out: decoded .. */ | |
374 { | |
375 register word temp1 /* , temp2 */; | |
376 register long ltmp; /* for GSM_ADD */ | |
377 | |
378 /* This procedure requires for efficient implementation | |
379 * two tables. | |
380 * | |
381 * INVA[1..8] = integer( (32768 * 8) / real_A[1..8]) | |
382 * MIC[1..8] = minimum value of the LARc[1..8] | |
383 */ | |
384 | |
385 /* Compute the LARpp[1..8] | |
386 */ | |
387 | |
388 /* for (i = 1; i <= 8; i++, B++, MIC++, INVA++, LARc++, LARpp++) { | |
389 * | |
390 * temp1 = GSM_ADD( *LARc, *MIC ) << 10; | |
391 * temp2 = *B << 1; | |
392 * temp1 = GSM_SUB( temp1, temp2 ); | |
393 * | |
394 * assert(*INVA != MIN_WORD); | |
395 * | |
396 * temp1 = GSM_MULT_R( *INVA, temp1 ); | |
397 * *LARpp = GSM_ADD( temp1, temp1 ); | |
398 * } | |
399 */ | |
400 | |
401 #undef STEP | |
402 #define STEP( B, MIC, INVA ) \ | |
403 temp1 = GSM_ADD( *LARc++, MIC ) << 10; \ | |
404 temp1 = GSM_SUB( temp1, B << 1 ); \ | |
405 temp1 = GSM_MULT_R( INVA, temp1 ); \ | |
406 *LARpp++ = GSM_ADD( temp1, temp1 ); | |
407 | |
408 STEP( 0, -32, 13107 ); | |
409 STEP( 0, -32, 13107 ); | |
410 STEP( 2048, -16, 13107 ); | |
411 STEP( -2560, -16, 13107 ); | |
412 | |
413 STEP( 94, -8, 19223 ); | |
414 STEP( -1792, -8, 17476 ); | |
415 STEP( -341, -4, 31454 ); | |
416 STEP( -1144, -4, 29708 ); | |
417 | |
418 /* NOTE: the addition of *MIC is used to restore | |
419 * the sign of *LARc. | |
420 */ | |
421 } | |
422 | |
423 /* 4.2.9 */ | |
424 /* Computation of the quantized reflection coefficients | |
425 */ | |
426 | |
427 /* 4.2.9.1 Interpolation of the LARpp[1..8] to get the LARp[1..8] | |
428 */ | |
429 | |
430 /* | |
431 * Within each frame of 160 analyzed speech samples the short term | |
432 * analysis and synthesis filters operate with four different sets of | |
433 * coefficients, derived from the previous set of decoded LARs(LARpp(j-1)) | |
434 * and the actual set of decoded LARs (LARpp(j)) | |
435 * | |
436 * (Initial value: LARpp(j-1)[1..8] = 0.) | |
437 */ | |
438 | |
439 static void Coefficients_0_12 (LARpp_j_1, LARpp_j, LARp) | |
440 register word * LARpp_j_1; | |
441 register word * LARpp_j; | |
442 register word * LARp; | |
443 { | |
444 register int i; | |
445 register longword ltmp; | |
446 | |
447 for (i = 1; i <= 8; i++, LARp++, LARpp_j_1++, LARpp_j++) { | |
448 *LARp = GSM_ADD( SASR( *LARpp_j_1, 2 ), SASR( *LARpp_j, 2 )); | |
449 *LARp = GSM_ADD( *LARp, SASR( *LARpp_j_1, 1)); | |
450 } | |
451 } | |
452 | |
453 static void Coefficients_13_26 (LARpp_j_1, LARpp_j, LARp) | |
454 register word * LARpp_j_1; | |
455 register word * LARpp_j; | |
456 register word * LARp; | |
457 { | |
458 register int i; | |
459 register longword ltmp; | |
460 for (i = 1; i <= 8; i++, LARpp_j_1++, LARpp_j++, LARp++) { | |
461 *LARp = GSM_ADD( SASR( *LARpp_j_1, 1), SASR( *LARpp_j, 1 )); | |
462 } | |
463 } | |
464 | |
465 static void Coefficients_27_39 (LARpp_j_1, LARpp_j, LARp) | |
466 register word * LARpp_j_1; | |
467 register word * LARpp_j; | |
468 register word * LARp; | |
469 { | |
470 register int i; | |
471 register longword ltmp; | |
472 | |
473 for (i = 1; i <= 8; i++, LARpp_j_1++, LARpp_j++, LARp++) { | |
474 *LARp = GSM_ADD( SASR( *LARpp_j_1, 2 ), SASR( *LARpp_j, 2 )); | |
475 *LARp = GSM_ADD( *LARp, SASR( *LARpp_j, 1 )); | |
476 } | |
477 } | |
478 | |
479 | |
480 static void Coefficients_40_159 (LARpp_j, LARp) | |
481 register word * LARpp_j; | |
482 register word * LARp; | |
483 { | |
484 register int i; | |
485 | |
486 for (i = 1; i <= 8; i++, LARp++, LARpp_j++) | |
487 *LARp = *LARpp_j; | |
488 } | |
489 /* 4.2.9.2 */ | |
490 | |
491 static void LARp_to_rp (LARp) | |
492 register word * LARp; /* [0..7] IN/OUT */ | |
493 /* | |
494 * The input of this procedure is the interpolated LARp[0..7] array. | |
495 * The reflection coefficients, rp[i], are used in the analysis | |
496 * filter and in the synthesis filter. | |
497 */ | |
498 { | |
499 register int i; | |
500 register word temp; | |
501 register longword ltmp; | |
502 | |
503 for (i = 1; i <= 8; i++, LARp++) { | |
504 | |
505 /* temp = GSM_ABS( *LARp ); | |
506 * | |
507 * if (temp < 11059) temp <<= 1; | |
508 * else if (temp < 20070) temp += 11059; | |
509 * else temp = GSM_ADD( temp >> 2, 26112 ); | |
510 * | |
511 * *LARp = *LARp < 0 ? -temp : temp; | |
512 */ | |
513 | |
514 if (*LARp < 0) { | |
515 temp = *LARp == MIN_WORD ? MAX_WORD : -(*LARp); | |
516 *LARp = - ((temp < 11059) ? temp << 1 | |
517 : ((temp < 20070) ? temp + 11059 | |
518 : GSM_ADD( temp >> 2, 26112 ))); | |
519 } else { | |
520 temp = *LARp; | |
521 *LARp = (temp < 11059) ? temp << 1 | |
522 : ((temp < 20070) ? temp + 11059 | |
523 : GSM_ADD( temp >> 2, 26112 )); | |
524 } | |
525 } | |
526 } | |
527 | |
528 | |
529 | |
530 | |
531 | |
532 /**** */ | |
533 static void Gsm_Short_Term_Synthesis_Filter (S, LARcr, wt, s) | |
534 XA_GSM_STATE * S; | |
535 word * LARcr; /* received log area ratios [0..7] IN */ | |
536 word * wt; /* received d [0..159] IN */ | |
537 word * s; /* signal s [0..159] OUT */ | |
538 { | |
539 word * LARpp_j = S->LARpp[ S->j ]; | |
540 word * LARpp_j_1 = S->LARpp[ S->j ^=1 ]; | |
541 | |
542 word LARp[8]; | |
543 | |
544 #undef FILTER | |
545 #if defined(FAST) && defined(USE_FLOAT_MUL) | |
546 | |
547 # define FILTER (* (S->fast \ | |
548 ? Fast_Short_term_synthesis_filtering \ | |
549 : Short_term_synthesis_filtering )) | |
550 #else | |
551 # define FILTER Short_term_synthesis_filtering | |
552 #endif | |
553 | |
554 Decoding_of_the_coded_Log_Area_Ratios( LARcr, LARpp_j ); | |
555 | |
556 Coefficients_0_12( LARpp_j_1, LARpp_j, LARp ); | |
557 LARp_to_rp( LARp ); | |
558 FILTER( S, LARp, 13, wt, s ); | |
559 | |
560 Coefficients_13_26( LARpp_j_1, LARpp_j, LARp); | |
561 LARp_to_rp( LARp ); | |
562 FILTER( S, LARp, 14, wt + 13, s + 13 ); | |
563 | |
564 Coefficients_27_39( LARpp_j_1, LARpp_j, LARp); | |
565 LARp_to_rp( LARp ); | |
566 FILTER( S, LARp, 13, wt + 27, s + 27 ); | |
567 | |
568 Coefficients_40_159( LARpp_j, LARp ); | |
569 LARp_to_rp( LARp ); | |
570 FILTER(S, LARp, 120, wt + 40, s + 40); | |
571 } | |
572 | |
573 | |
574 | |
575 | |
576 static void GSM_Decode(S,LARcr, Ncr,bcr,Mcr,xmaxcr,xMcr,s) | |
577 XA_GSM_STATE *S; | |
578 word *LARcr; /* [0..7] IN */ | |
579 word *Ncr; /* [0..3] IN */ | |
580 word *bcr; /* [0..3] IN */ | |
581 word *Mcr; /* [0..3] IN */ | |
582 word *xmaxcr; /* [0..3] IN */ | |
583 word *xMcr; /* [0..13*4] IN */ | |
584 word *s; /* [0..159] OUT */ | |
585 { | |
586 int j, k; | |
587 word erp[40], wt[160]; | |
588 word *drp = S->dp0 + 120; | |
589 | |
590 for (j=0; j <= 3; j++, xmaxcr++, bcr++, Ncr++, Mcr++, xMcr += 13) | |
591 { | |
592 Gsm_RPE_Decoding( S, *xmaxcr, *Mcr, xMcr, erp ); | |
593 Gsm_Long_Term_Synthesis_Filtering( S, *Ncr, *bcr, erp, drp ); | |
594 for (k = 0; k <= 39; k++) wt[ j * 40 + k ] = drp[ k ]; | |
595 } | |
596 | |
597 Gsm_Short_Term_Synthesis_Filter( S, LARcr, wt, s ); | |
598 Postprocessing(S, s); | |
599 } | |
600 | |
601 | |
602 | |
603 /****-------------------------------------------------------------------**** | |
604 **** Podlipec: For AVI/WAV files GSM 6.10 combines two 33 bytes frames | |
605 **** into one 65 byte frame. | |
606 ****-------------------------------------------------------------------****/ | |
607 void XA_MSGSM_Decoder(unsigned char *ibuf,unsigned short *obuf) | |
608 { word sr; | |
609 word LARc[8], Nc[4], Mc[4], bc[4], xmaxc[4], xmc[13*4]; | |
610 | |
611 sr = *ibuf++; | |
612 | |
613 LARc[0] = sr & 0x3f; sr >>= 6; | |
614 sr |= (word)*ibuf++ << 2; | |
615 LARc[1] = sr & 0x3f; sr >>= 6; | |
616 sr |= (word)*ibuf++ << 4; | |
617 LARc[2] = sr & 0x1f; sr >>= 5; | |
618 LARc[3] = sr & 0x1f; sr >>= 5; | |
619 sr |= (word)*ibuf++ << 2; | |
620 LARc[4] = sr & 0xf; sr >>= 4; | |
621 LARc[5] = sr & 0xf; sr >>= 4; | |
622 sr |= (word)*ibuf++ << 2; /* 5 */ | |
623 LARc[6] = sr & 0x7; sr >>= 3; | |
624 LARc[7] = sr & 0x7; sr >>= 3; | |
625 sr |= (word)*ibuf++ << 4; | |
626 Nc[0] = sr & 0x7f; sr >>= 7; | |
627 bc[0] = sr & 0x3; sr >>= 2; | |
628 Mc[0] = sr & 0x3; sr >>= 2; | |
629 sr |= (word)*ibuf++ << 1; | |
630 xmaxc[0] = sr & 0x3f; sr >>= 6; | |
631 xmc[0] = sr & 0x7; sr >>= 3; | |
632 sr = *ibuf++; | |
633 xmc[1] = sr & 0x7; sr >>= 3; | |
634 xmc[2] = sr & 0x7; sr >>= 3; | |
635 sr |= (word)*ibuf++ << 2; | |
636 xmc[3] = sr & 0x7; sr >>= 3; | |
637 xmc[4] = sr & 0x7; sr >>= 3; | |
638 xmc[5] = sr & 0x7; sr >>= 3; | |
639 sr |= (word)*ibuf++ << 1; /* 10 */ | |
640 xmc[6] = sr & 0x7; sr >>= 3; | |
641 xmc[7] = sr & 0x7; sr >>= 3; | |
642 xmc[8] = sr & 0x7; sr >>= 3; | |
643 sr = *ibuf++; | |
644 xmc[9] = sr & 0x7; sr >>= 3; | |
645 xmc[10] = sr & 0x7; sr >>= 3; | |
646 sr |= (word)*ibuf++ << 2; | |
647 xmc[11] = sr & 0x7; sr >>= 3; | |
648 xmc[12] = sr & 0x7; sr >>= 3; | |
649 sr |= (word)*ibuf++ << 4; | |
650 Nc[1] = sr & 0x7f; sr >>= 7; | |
651 bc[1] = sr & 0x3; sr >>= 2; | |
652 Mc[1] = sr & 0x3; sr >>= 2; | |
653 sr |= (word)*ibuf++ << 1; | |
654 xmaxc[1] = sr & 0x3f; sr >>= 6; | |
655 xmc[13] = sr & 0x7; sr >>= 3; | |
656 sr = *ibuf++; /* 15 */ | |
657 xmc[14] = sr & 0x7; sr >>= 3; | |
658 xmc[15] = sr & 0x7; sr >>= 3; | |
659 sr |= (word)*ibuf++ << 2; | |
660 xmc[16] = sr & 0x7; sr >>= 3; | |
661 xmc[17] = sr & 0x7; sr >>= 3; | |
662 xmc[18] = sr & 0x7; sr >>= 3; | |
663 sr |= (word)*ibuf++ << 1; | |
664 xmc[19] = sr & 0x7; sr >>= 3; | |
665 xmc[20] = sr & 0x7; sr >>= 3; | |
666 xmc[21] = sr & 0x7; sr >>= 3; | |
667 sr = *ibuf++; | |
668 xmc[22] = sr & 0x7; sr >>= 3; | |
669 xmc[23] = sr & 0x7; sr >>= 3; | |
670 sr |= (word)*ibuf++ << 2; | |
671 xmc[24] = sr & 0x7; sr >>= 3; | |
672 xmc[25] = sr & 0x7; sr >>= 3; | |
673 sr |= (word)*ibuf++ << 4; /* 20 */ | |
674 Nc[2] = sr & 0x7f; sr >>= 7; | |
675 bc[2] = sr & 0x3; sr >>= 2; | |
676 Mc[2] = sr & 0x3; sr >>= 2; | |
677 sr |= (word)*ibuf++ << 1; | |
678 xmaxc[2] = sr & 0x3f; sr >>= 6; | |
679 xmc[26] = sr & 0x7; sr >>= 3; | |
680 sr = *ibuf++; | |
681 xmc[27] = sr & 0x7; sr >>= 3; | |
682 xmc[28] = sr & 0x7; sr >>= 3; | |
683 sr |= (word)*ibuf++ << 2; | |
684 xmc[29] = sr & 0x7; sr >>= 3; | |
685 xmc[30] = sr & 0x7; sr >>= 3; | |
686 xmc[31] = sr & 0x7; sr >>= 3; | |
687 sr |= (word)*ibuf++ << 1; | |
688 xmc[32] = sr & 0x7; sr >>= 3; | |
689 xmc[33] = sr & 0x7; sr >>= 3; | |
690 xmc[34] = sr & 0x7; sr >>= 3; | |
691 sr = *ibuf++; /* 25 */ | |
692 xmc[35] = sr & 0x7; sr >>= 3; | |
693 xmc[36] = sr & 0x7; sr >>= 3; | |
694 sr |= (word)*ibuf++ << 2; | |
695 xmc[37] = sr & 0x7; sr >>= 3; | |
696 xmc[38] = sr & 0x7; sr >>= 3; | |
697 sr |= (word)*ibuf++ << 4; | |
698 Nc[3] = sr & 0x7f; sr >>= 7; | |
699 bc[3] = sr & 0x3; sr >>= 2; | |
700 Mc[3] = sr & 0x3; sr >>= 2; | |
701 sr |= (word)*ibuf++ << 1; | |
702 xmaxc[3] = sr & 0x3f; sr >>= 6; | |
703 xmc[39] = sr & 0x7; sr >>= 3; | |
704 sr = *ibuf++; | |
705 xmc[40] = sr & 0x7; sr >>= 3; | |
706 xmc[41] = sr & 0x7; sr >>= 3; | |
707 sr |= (word)*ibuf++ << 2; /* 30 */ | |
708 xmc[42] = sr & 0x7; sr >>= 3; | |
709 xmc[43] = sr & 0x7; sr >>= 3; | |
710 xmc[44] = sr & 0x7; sr >>= 3; | |
711 sr |= (word)*ibuf++ << 1; | |
712 xmc[45] = sr & 0x7; sr >>= 3; | |
713 xmc[46] = sr & 0x7; sr >>= 3; | |
714 xmc[47] = sr & 0x7; sr >>= 3; | |
715 sr = *ibuf++; | |
716 xmc[48] = sr & 0x7; sr >>= 3; | |
717 xmc[49] = sr & 0x7; sr >>= 3; | |
718 sr |= (word)*ibuf++ << 2; | |
719 xmc[50] = sr & 0x7; sr >>= 3; | |
720 xmc[51] = sr & 0x7; sr >>= 3; | |
721 | |
722 GSM_Decode(&gsm_state, LARc, Nc, bc, Mc, xmaxc, xmc, obuf); | |
723 | |
724 /* | |
725 carry = sr & 0xf; | |
726 sr = carry; | |
727 */ | |
728 /* 2nd frame */ | |
729 sr &= 0xf; | |
730 sr |= (word)*ibuf++ << 4; /* 1 */ | |
731 LARc[0] = sr & 0x3f; sr >>= 6; | |
732 LARc[1] = sr & 0x3f; sr >>= 6; | |
733 sr = *ibuf++; | |
734 LARc[2] = sr & 0x1f; sr >>= 5; | |
735 sr |= (word)*ibuf++ << 3; | |
736 LARc[3] = sr & 0x1f; sr >>= 5; | |
737 LARc[4] = sr & 0xf; sr >>= 4; | |
738 sr |= (word)*ibuf++ << 2; | |
739 LARc[5] = sr & 0xf; sr >>= 4; | |
740 LARc[6] = sr & 0x7; sr >>= 3; | |
741 LARc[7] = sr & 0x7; sr >>= 3; | |
742 sr = *ibuf++; /* 5 */ | |
743 Nc[0] = sr & 0x7f; sr >>= 7; | |
744 sr |= (word)*ibuf++ << 1; | |
745 bc[0] = sr & 0x3; sr >>= 2; | |
746 Mc[0] = sr & 0x3; sr >>= 2; | |
747 sr |= (word)*ibuf++ << 5; | |
748 xmaxc[0] = sr & 0x3f; sr >>= 6; | |
749 xmc[0] = sr & 0x7; sr >>= 3; | |
750 xmc[1] = sr & 0x7; sr >>= 3; | |
751 sr |= (word)*ibuf++ << 1; | |
752 xmc[2] = sr & 0x7; sr >>= 3; | |
753 xmc[3] = sr & 0x7; sr >>= 3; | |
754 xmc[4] = sr & 0x7; sr >>= 3; | |
755 sr = *ibuf++; | |
756 xmc[5] = sr & 0x7; sr >>= 3; | |
757 xmc[6] = sr & 0x7; sr >>= 3; | |
758 sr |= (word)*ibuf++ << 2; /* 10 */ | |
759 xmc[7] = sr & 0x7; sr >>= 3; | |
760 xmc[8] = sr & 0x7; sr >>= 3; | |
761 xmc[9] = sr & 0x7; sr >>= 3; | |
762 sr |= (word)*ibuf++ << 1; | |
763 xmc[10] = sr & 0x7; sr >>= 3; | |
764 xmc[11] = sr & 0x7; sr >>= 3; | |
765 xmc[12] = sr & 0x7; sr >>= 3; | |
766 sr = *ibuf++; | |
767 Nc[1] = sr & 0x7f; sr >>= 7; | |
768 sr |= (word)*ibuf++ << 1; | |
769 bc[1] = sr & 0x3; sr >>= 2; | |
770 Mc[1] = sr & 0x3; sr >>= 2; | |
771 sr |= (word)*ibuf++ << 5; | |
772 xmaxc[1] = sr & 0x3f; sr >>= 6; | |
773 xmc[13] = sr & 0x7; sr >>= 3; | |
774 xmc[14] = sr & 0x7; sr >>= 3; | |
775 sr |= (word)*ibuf++ << 1; /* 15 */ | |
776 xmc[15] = sr & 0x7; sr >>= 3; | |
777 xmc[16] = sr & 0x7; sr >>= 3; | |
778 xmc[17] = sr & 0x7; sr >>= 3; | |
779 sr = *ibuf++; | |
780 xmc[18] = sr & 0x7; sr >>= 3; | |
781 xmc[19] = sr & 0x7; sr >>= 3; | |
782 sr |= (word)*ibuf++ << 2; | |
783 xmc[20] = sr & 0x7; sr >>= 3; | |
784 xmc[21] = sr & 0x7; sr >>= 3; | |
785 xmc[22] = sr & 0x7; sr >>= 3; | |
786 sr |= (word)*ibuf++ << 1; | |
787 xmc[23] = sr & 0x7; sr >>= 3; | |
788 xmc[24] = sr & 0x7; sr >>= 3; | |
789 xmc[25] = sr & 0x7; sr >>= 3; | |
790 sr = *ibuf++; | |
791 Nc[2] = sr & 0x7f; sr >>= 7; | |
792 sr |= (word)*ibuf++ << 1; /* 20 */ | |
793 bc[2] = sr & 0x3; sr >>= 2; | |
794 Mc[2] = sr & 0x3; sr >>= 2; | |
795 sr |= (word)*ibuf++ << 5; | |
796 xmaxc[2] = sr & 0x3f; sr >>= 6; | |
797 xmc[26] = sr & 0x7; sr >>= 3; | |
798 xmc[27] = sr & 0x7; sr >>= 3; | |
799 sr |= (word)*ibuf++ << 1; | |
800 xmc[28] = sr & 0x7; sr >>= 3; | |
801 xmc[29] = sr & 0x7; sr >>= 3; | |
802 xmc[30] = sr & 0x7; sr >>= 3; | |
803 sr = *ibuf++; | |
804 xmc[31] = sr & 0x7; sr >>= 3; | |
805 xmc[32] = sr & 0x7; sr >>= 3; | |
806 sr |= (word)*ibuf++ << 2; | |
807 xmc[33] = sr & 0x7; sr >>= 3; | |
808 xmc[34] = sr & 0x7; sr >>= 3; | |
809 xmc[35] = sr & 0x7; sr >>= 3; | |
810 sr |= (word)*ibuf++ << 1; /* 25 */ | |
811 xmc[36] = sr & 0x7; sr >>= 3; | |
812 xmc[37] = sr & 0x7; sr >>= 3; | |
813 xmc[38] = sr & 0x7; sr >>= 3; | |
814 sr = *ibuf++; | |
815 Nc[3] = sr & 0x7f; sr >>= 7; | |
816 sr |= (word)*ibuf++ << 1; | |
817 bc[3] = sr & 0x3; sr >>= 2; | |
818 Mc[3] = sr & 0x3; sr >>= 2; | |
819 sr |= (word)*ibuf++ << 5; | |
820 xmaxc[3] = sr & 0x3f; sr >>= 6; | |
821 xmc[39] = sr & 0x7; sr >>= 3; | |
822 xmc[40] = sr & 0x7; sr >>= 3; | |
823 sr |= (word)*ibuf++ << 1; | |
824 xmc[41] = sr & 0x7; sr >>= 3; | |
825 xmc[42] = sr & 0x7; sr >>= 3; | |
826 xmc[43] = sr & 0x7; sr >>= 3; | |
827 sr = (word)*ibuf++; /* 30 */ | |
828 xmc[44] = sr & 0x7; sr >>= 3; | |
829 xmc[45] = sr & 0x7; sr >>= 3; | |
830 sr |= (word)*ibuf++ << 2; | |
831 xmc[46] = sr & 0x7; sr >>= 3; | |
832 xmc[47] = sr & 0x7; sr >>= 3; | |
833 xmc[48] = sr & 0x7; sr >>= 3; | |
834 sr |= (word)*ibuf++ << 1; | |
835 xmc[49] = sr & 0x7; sr >>= 3; | |
836 xmc[50] = sr & 0x7; sr >>= 3; | |
837 xmc[51] = sr & 0x7; sr >>= 3; | |
838 | |
839 GSM_Decode(&gsm_state, LARc, Nc, bc, Mc, xmaxc, xmc, &obuf[160]); | |
840 | |
841 /* Return number of source bytes consumed and output samples produced */ | |
842 // *icnt = 65; | |
843 // *ocnt = 320; | |
844 return; | |
845 } | |
846 | |
847 #define GSM_MAGIC 0xd | |
848 | |
849 void XA_GSM_Decoder(unsigned char *ibuf,unsigned short *obuf) | |
850 { word LARc[8], Nc[4], Mc[4], bc[4], xmaxc[4], xmc[13*4]; | |
851 | |
852 /* Sanity */ | |
853 if (((*ibuf >> 4) & 0x0F) != GSM_MAGIC) | |
854 { int i; | |
855 for(i=0;i<160;i++) obuf[i] = 0; | |
856 // *icnt = 33; | |
857 // *ocnt = 160; | |
858 return; | |
859 } | |
860 | |
861 LARc[0] = (*ibuf++ & 0xF) << 2; /* 1 */ | |
862 LARc[0] |= (*ibuf >> 6) & 0x3; | |
863 LARc[1] = *ibuf++ & 0x3F; | |
864 LARc[2] = (*ibuf >> 3) & 0x1F; | |
865 LARc[3] = (*ibuf++ & 0x7) << 2; | |
866 LARc[3] |= (*ibuf >> 6) & 0x3; | |
867 LARc[4] = (*ibuf >> 2) & 0xF; | |
868 LARc[5] = (*ibuf++ & 0x3) << 2; | |
869 LARc[5] |= (*ibuf >> 6) & 0x3; | |
870 LARc[6] = (*ibuf >> 3) & 0x7; | |
871 LARc[7] = *ibuf++ & 0x7; | |
872 | |
873 Nc[0] = (*ibuf >> 1) & 0x7F; | |
874 | |
875 bc[0] = (*ibuf++ & 0x1) << 1; | |
876 bc[0] |= (*ibuf >> 7) & 0x1; | |
877 | |
878 Mc[0] = (*ibuf >> 5) & 0x3; | |
879 | |
880 xmaxc[0] = (*ibuf++ & 0x1F) << 1; | |
881 xmaxc[0] |= (*ibuf >> 7) & 0x1; | |
882 | |
883 xmc[0] = (*ibuf >> 4) & 0x7; | |
884 xmc[1] = (*ibuf >> 1) & 0x7; | |
885 xmc[2] = (*ibuf++ & 0x1) << 2; | |
886 xmc[2] |= (*ibuf >> 6) & 0x3; | |
887 xmc[3] = (*ibuf >> 3) & 0x7; | |
888 xmc[4] = *ibuf++ & 0x7; | |
889 xmc[5] = (*ibuf >> 5) & 0x7; | |
890 xmc[6] = (*ibuf >> 2) & 0x7; | |
891 xmc[7] = (*ibuf++ & 0x3) << 1; /* 10 */ | |
892 xmc[7] |= (*ibuf >> 7) & 0x1; | |
893 xmc[8] = (*ibuf >> 4) & 0x7; | |
894 xmc[9] = (*ibuf >> 1) & 0x7; | |
895 xmc[10] = (*ibuf++ & 0x1) << 2; | |
896 xmc[10] |= (*ibuf >> 6) & 0x3; | |
897 xmc[11] = (*ibuf >> 3) & 0x7; | |
898 xmc[12] = *ibuf++ & 0x7; | |
899 | |
900 Nc[1] = (*ibuf >> 1) & 0x7F; | |
901 | |
902 bc[1] = (*ibuf++ & 0x1) << 1; | |
903 bc[1] |= (*ibuf >> 7) & 0x1; | |
904 | |
905 Mc[1] = (*ibuf >> 5) & 0x3; | |
906 | |
907 xmaxc[1] = (*ibuf++ & 0x1F) << 1; | |
908 xmaxc[1] |= (*ibuf >> 7) & 0x1; | |
909 | |
910 | |
911 xmc[13] = (*ibuf >> 4) & 0x7; | |
912 xmc[14] = (*ibuf >> 1) & 0x7; | |
913 xmc[15] = (*ibuf++ & 0x1) << 2; | |
914 xmc[15] |= (*ibuf >> 6) & 0x3; | |
915 xmc[16] = (*ibuf >> 3) & 0x7; | |
916 xmc[17] = *ibuf++ & 0x7; | |
917 xmc[18] = (*ibuf >> 5) & 0x7; | |
918 xmc[19] = (*ibuf >> 2) & 0x7; | |
919 xmc[20] = (*ibuf++ & 0x3) << 1; | |
920 xmc[20] |= (*ibuf >> 7) & 0x1; | |
921 xmc[21] = (*ibuf >> 4) & 0x7; | |
922 xmc[22] = (*ibuf >> 1) & 0x7; | |
923 xmc[23] = (*ibuf++ & 0x1) << 2; | |
924 xmc[23] |= (*ibuf >> 6) & 0x3; | |
925 xmc[24] = (*ibuf >> 3) & 0x7; | |
926 xmc[25] = *ibuf++ & 0x7; | |
927 | |
928 Nc[2] = (*ibuf >> 1) & 0x7F; | |
929 | |
930 bc[2] = (*ibuf++ & 0x1) << 1; /* 20 */ | |
931 bc[2] |= (*ibuf >> 7) & 0x1; | |
932 | |
933 Mc[2] = (*ibuf >> 5) & 0x3; | |
934 | |
935 xmaxc[2] = (*ibuf++ & 0x1F) << 1; | |
936 xmaxc[2] |= (*ibuf >> 7) & 0x1; | |
937 | |
938 | |
939 xmc[26] = (*ibuf >> 4) & 0x7; | |
940 xmc[27] = (*ibuf >> 1) & 0x7; | |
941 xmc[28] = (*ibuf++ & 0x1) << 2; | |
942 xmc[28] |= (*ibuf >> 6) & 0x3; | |
943 xmc[29] = (*ibuf >> 3) & 0x7; | |
944 xmc[30] = *ibuf++ & 0x7; | |
945 xmc[31] = (*ibuf >> 5) & 0x7; | |
946 xmc[32] = (*ibuf >> 2) & 0x7; | |
947 xmc[33] = (*ibuf++ & 0x3) << 1; | |
948 xmc[33] |= (*ibuf >> 7) & 0x1; | |
949 xmc[34] = (*ibuf >> 4) & 0x7; | |
950 xmc[35] = (*ibuf >> 1) & 0x7; | |
951 xmc[36] = (*ibuf++ & 0x1) << 2; | |
952 xmc[36] |= (*ibuf >> 6) & 0x3; | |
953 xmc[37] = (*ibuf >> 3) & 0x7; | |
954 xmc[38] = *ibuf++ & 0x7; | |
955 | |
956 Nc[3] = (*ibuf >> 1) & 0x7F; | |
957 | |
958 bc[3] = (*ibuf++ & 0x1) << 1; | |
959 bc[3] |= (*ibuf >> 7) & 0x1; | |
960 | |
961 Mc[3] = (*ibuf >> 5) & 0x3; | |
962 | |
963 xmaxc[3] = (*ibuf++ & 0x1F) << 1; | |
964 xmaxc[3] |= (*ibuf >> 7) & 0x1; | |
965 | |
966 xmc[39] = (*ibuf >> 4) & 0x7; | |
967 xmc[40] = (*ibuf >> 1) & 0x7; | |
968 xmc[41] = (*ibuf++ & 0x1) << 2; | |
969 xmc[41] |= (*ibuf >> 6) & 0x3; | |
970 xmc[42] = (*ibuf >> 3) & 0x7; | |
971 xmc[43] = *ibuf++ & 0x7; /* 30 */ | |
972 xmc[44] = (*ibuf >> 5) & 0x7; | |
973 xmc[45] = (*ibuf >> 2) & 0x7; | |
974 xmc[46] = (*ibuf++ & 0x3) << 1; | |
975 xmc[46] |= (*ibuf >> 7) & 0x1; | |
976 xmc[47] = (*ibuf >> 4) & 0x7; | |
977 xmc[48] = (*ibuf >> 1) & 0x7; | |
978 xmc[49] = (*ibuf++ & 0x1) << 2; | |
979 xmc[49] |= (*ibuf >> 6) & 0x3; | |
980 xmc[50] = (*ibuf >> 3) & 0x7; | |
981 xmc[51] = *ibuf & 0x7; /* 33 */ | |
982 | |
983 GSM_Decode(&gsm_state, LARc, Nc, bc, Mc, xmaxc, xmc, obuf); | |
984 | |
985 /* Return number of source bytes consumed and output samples produced */ | |
986 // *icnt = 33; | |
987 // *ocnt = 160; | |
988 } |