Debian Codegraph @

plugins/libaudiodec/xa_gsm.c

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