Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / dec / src / d8_31pf.cpp
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
20
21     3GPP TS 26.073
22     ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23     Available from http://www.3gpp.org
24
25 (C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26 Permission to distribute, modify and use this file under the standard license
27 terms listed above has been obtained from the copyright holder.
28 ****************************************************************************************/
29 /*
30 ------------------------------------------------------------------------------
31
32
33
34  Filename: d8_31pf.cpp
35
36 ------------------------------------------------------------------------------
37
38  MODULE DESCRIPTION
39 */
40
41 /*----------------------------------------------------------------------------
42 ; INCLUDES
43 ----------------------------------------------------------------------------*/
44 #include "d8_31pf.h"
45 #include "typedef.h"
46 #include "basic_op.h"
47 #include "cnst.h"
48
49
50 /*----------------------------------------------------------------------------
51 ; MACROS
52 ; Define module specific macros here
53 ----------------------------------------------------------------------------*/
54
55 /*----------------------------------------------------------------------------
56 ; DEFINES
57 ; Include all pre-processor statements here. Include conditional
58 ; compile variables also.
59 ----------------------------------------------------------------------------*/
60 #define NB_PULSE  8           /* number of pulses  */
61
62 /* define values/representation for output codevector and sign */
63 #define POS_CODE  8191
64 #define NEG_CODE  8191
65
66
67 /*----------------------------------------------------------------------------
68 ; LOCAL FUNCTION DEFINITIONS
69 ; Function Prototype declaration
70 ----------------------------------------------------------------------------*/
71
72 /*----------------------------------------------------------------------------
73 ; LOCAL VARIABLE DEFINITIONS
74 ; Variable declaration - defined here and used outside this module
75 ----------------------------------------------------------------------------*/
76
77 /*
78 ------------------------------------------------------------------------------
79  FUNCTION NAME: decompress10
80 ------------------------------------------------------------------------------
81  INPUT AND OUTPUT DEFINITIONS
82
83  Inputs:
84    MSBs -- Word16 -- MSB part of the index
85    LSBs -- Word16 -- LSB part of the index
86    index1 -- Word16 -- index for first pos in pos_index[]
87    index2 -- Word16 -- index for second pos in pos_index[]
88    index3 -- Word16 -- index for third pos in pos_index[]
89
90  Outputs:
91    pos_indx[] -- array of type Word16 -- position of 3 pulses (decompressed)
92
93    pOverflow  Flag set when overflow occurs, pointer of type Flag *
94
95  Returns:
96     None
97
98  Global Variables Used:
99     None
100
101  Local Variables Needed:
102     None
103
104 ------------------------------------------------------------------------------
105  FUNCTION DESCRIPTION
106
107
108 ------------------------------------------------------------------------------
109  REQUIREMENTS
110
111  None
112
113 ------------------------------------------------------------------------------
114  REFERENCES
115
116  d8_31pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
117
118 ------------------------------------------------------------------------------
119  PSEUDO-CODE
120
121
122 ------------------------------------------------------------------------------
123  CAUTION [optional]
124  [State any special notes, constraints or cautions for users of this function]
125
126 ------------------------------------------------------------------------------
127 */
128
129 static void decompress10(
130     Word16 MSBs,        /* i : MSB part of the index                 */
131     Word16 LSBs,        /* i : LSB part of the index                 */
132     Word16 index1,      /* i : index for first pos in pos_index[]    */
133     Word16 index2,      /* i : index for second pos in pos_index[]   */
134     Word16 index3,      /* i : index for third pos in pos_index[]    */
135     Word16 pos_indx[],  /* o : position of 3 pulses (decompressed)   */
136     Flag  *pOverflow)   /* o : Flag set when overflow occurs         */
137 {
138     Word16 ia;
139     Word16 ib;
140     Word16 ic;
141     Word32 tempWord32;
142
143     /*
144       pos_indx[index1] = ((MSBs-25*(MSBs/25))%5)*2 + (LSBs-4*(LSBs/4))%2;
145       pos_indx[index2] = ((MSBs-25*(MSBs/25))/5)*2 + (LSBs-4*(LSBs/4))/2;
146       pos_indx[index3] = (MSBs/25)*2 + LSBs/4;
147     */
148
149     if (MSBs > 124)
150     {
151         MSBs = 124;
152     }
153
154     ia =
155         mult(
156             MSBs,
157             1311,
158             pOverflow);
159
160     tempWord32 =
161         L_mult(
162             ia,
163             25,
164             pOverflow);
165
166
167     ia = (Word16)(MSBs - (tempWord32 >> 1));
168     ib =
169         mult(
170             ia,
171             6554,
172             pOverflow);
173
174     tempWord32 =
175         L_mult(
176             ib,
177             5,
178             pOverflow);
179
180     ib = ia - (Word16)(tempWord32 >> 1);
181
182     ib =
183         shl(
184             ib,
185             1,
186             pOverflow);
187
188
189     ic = LSBs - ((LSBs >> 2) << 2);
190
191
192     pos_indx[index1] = ib + (ic & 1);
193
194
195     ib =
196         mult(
197             ia,
198             6554,
199             pOverflow);
200
201     ib =
202         shl(
203             ib,
204             1,
205             pOverflow);
206
207
208     pos_indx[index2] = ib + (ic >> 1);
209
210
211     ib = LSBs >> 2;
212
213     ic =
214         mult(
215             MSBs,
216             1311,
217             pOverflow);
218
219     ic =
220         shl(
221             ic,
222             1,
223             pOverflow);
224
225     pos_indx[index3] = add_16(ib, ic, pOverflow);
226
227     return;
228 }
229
230
231 /*
232 ------------------------------------------------------------------------------
233  FUNCTION NAME: decompress_code
234 ------------------------------------------------------------------------------
235  INPUT AND OUTPUT DEFINITIONS
236
237  Inputs:
238     indx[] -- array of type Word16 -- position and sign of
239                                       8 pulses (compressed)
240
241  Outputs:
242     sign_indx[] -- array of type Word16 -- signs of 4 pulses (signs only)
243     pos_indx[]  -- array of type Word16 -- position index of 8 pulses
244                                            (position only)
245     pOverflow pointer to type Flag -- Flag set when overflow occurs
246
247  Returns:
248     None
249
250  Global Variables Used:
251     None
252
253  Local Variables Needed:
254     None
255
256 ------------------------------------------------------------------------------
257  FUNCTION DESCRIPTION
258
259     PURPOSE: decompression of the linear codewords to 4+three indeces
260              one bit from each pulse is made robust to errors by
261              minimizing the phase shift of a bit error.
262              4 signs (one for each track)
263              i0,i4,i1 => one index (7+3) bits, 3   LSBs more robust
264              i2,i6,i5 => one index (7+3) bits, 3   LSBs more robust
265              i3,i7    => one index (5+2) bits, 2-3 LSbs more robust
266
267 ------------------------------------------------------------------------------
268  REQUIREMENTS
269
270  None
271
272 ------------------------------------------------------------------------------
273  REFERENCES
274
275  d8_31pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
276
277 ------------------------------------------------------------------------------
278  PSEUDO-CODE
279
280
281 ------------------------------------------------------------------------------
282  CAUTION [optional]
283  [State any special notes, constraints or cautions for users of this function]
284
285 ------------------------------------------------------------------------------
286 */
287
288 static void decompress_code(
289     Word16 indx[],      /* i : position and sign of 8 pulses (compressed) */
290     Word16 sign_indx[], /* o : signs of 4 pulses (signs only)             */
291     Word16 pos_indx[],  /* o : position index of 8 pulses (position only) */
292     Flag  *pOverflow    /* o : Flag set when overflow occurs              */
293 )
294 {
295     Word16 i;
296     Word16 ia;
297     Word16 ib;
298     Word16 MSBs;
299     Word16 LSBs;
300     Word16 MSBs0_24;
301     Word32 tempWord32;
302
303     for (i = 0; i < NB_TRACK_MR102; i++)
304     {
305         sign_indx[i] = indx[i];
306     }
307
308     /*
309       First index: 10x10x10 -> 2x5x2x5x2x5-> 125x2x2x2 -> 7+1x3 bits
310       MSBs = indx[NB_TRACK]/8;
311       LSBs = indx[NB_TRACK]%8;
312       */
313     MSBs = indx[NB_TRACK_MR102] >> 3;
314
315     LSBs = indx[NB_TRACK_MR102] & 0x7;
316
317     decompress10(
318         MSBs,
319         LSBs,
320         0,
321         4,
322         1,
323         pos_indx,
324         pOverflow);
325
326     /*
327       Second index: 10x10x10 -> 2x5x2x5x2x5-> 125x2x2x2 -> 7+1x3 bits
328       MSBs = indx[NB_TRACK+1]/8;
329       LSBs = indx[NB_TRACK+1]%8;
330       */
331     MSBs = indx[NB_TRACK_MR102+1] >> 3;
332
333     LSBs = indx[NB_TRACK_MR102+1] & 0x7;
334
335     decompress10(
336         MSBs,
337         LSBs,
338         2,
339         6,
340         5,
341         pos_indx,
342         pOverflow);
343
344     /*
345       Third index: 10x10 -> 2x5x2x5-> 25x2x2 -> 5+1x2 bits
346       MSBs = indx[NB_TRACK+2]/4;
347       LSBs = indx[NB_TRACK+2]%4;
348       MSBs0_24 = (MSBs*25+12)/32;
349       if ((MSBs0_24/5)%2==1)
350          pos_indx[3] = (4-(MSBs0_24%5))*2 + LSBs%2;
351       else
352          pos_indx[3] = (MSBs0_24%5)*2 + LSBs%2;
353       pos_indx[7] = (MSBs0_24/5)*2 + LSBs/2;
354       */
355
356     MSBs = indx[NB_TRACK_MR102+2] >> 2;
357
358     LSBs = indx[NB_TRACK_MR102+2] & 0x3;
359
360     tempWord32 =
361         L_mult(
362             MSBs,
363             25,
364             pOverflow);
365
366     ia =
367         (Word16)
368         L_shr(
369             tempWord32,
370             1,
371             pOverflow);
372
373     ia += 12;
374
375     MSBs0_24 = ia >> 5;
376
377
378     ia =
379         mult(
380             MSBs0_24,
381             6554,
382             pOverflow);
383
384     ia &= 1;
385
386
387     ib =
388         mult(
389             MSBs0_24,
390             6554,
391             pOverflow);
392
393     tempWord32 =
394         L_mult(
395             ib,
396             5,
397             pOverflow);
398
399
400     ib = MSBs0_24 - (Word16)(tempWord32 >> 1);
401
402     if (ia == 1)
403     {
404         ib = 4 - ib;
405
406     }
407
408
409     ib =
410         shl(
411             ib,
412             1,
413             pOverflow);
414
415     ia = LSBs & 0x1;
416
417     pos_indx[3] =
418         add_16(
419             ib,
420             ia,
421             pOverflow);
422
423     ia =
424         mult(
425             MSBs0_24,
426             6554,
427             pOverflow);
428
429     ia =
430         shl(
431             ia,
432             1,
433             pOverflow);
434
435     pos_indx[7] = ia + (LSBs >> 1);
436
437 }
438
439 /*
440 ------------------------------------------------------------------------------
441  FUNCTION NAME: dec_8i40_31bits
442 ------------------------------------------------------------------------------
443  INPUT AND OUTPUT DEFINITIONS
444
445  Inputs:
446     index   array of type Word16 --  index of 8 pulses (sign+position)
447
448  Outputs:
449     cod     array of type Word16 --  algebraic (fixed) codebook excitation
450     pOverflow pointer to type Flag -- Flag set when overflow occurs
451
452  Returns:
453     None
454
455  Global Variables Used:
456     None
457
458  Local Variables Needed:
459     None
460
461 ------------------------------------------------------------------------------
462  FUNCTION DESCRIPTION
463
464  PURPOSE:  Builds the innovative codevector from the received
465            index of algebraic codebook.
466
467 ------------------------------------------------------------------------------
468  REQUIREMENTS
469
470  None
471
472 ------------------------------------------------------------------------------
473  REFERENCES
474
475  d8_31pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
476
477 ------------------------------------------------------------------------------
478  PSEUDO-CODE
479
480
481 ------------------------------------------------------------------------------
482  CAUTION [optional]
483  [State any special notes, constraints or cautions for users of this function]
484
485 ------------------------------------------------------------------------------
486 */
487
488 void dec_8i40_31bits(
489     Word16 index[],    /* i : index of 8 pulses (sign+position)         */
490     Word16 cod[],      /* o : algebraic (fixed) codebook excitation     */
491     Flag  *pOverflow   /* o : Flag set when overflow occurs             */
492 )
493 {
494     Word16 i;
495     Word16 j;
496     Word16 pos1;
497     Word16 pos2;
498     Word16 sign;
499
500     Word16 linear_signs[NB_TRACK_MR102];
501     Word16 linear_codewords[NB_PULSE];
502
503     for (i = 0; i < L_CODE; i++)
504     {
505         cod[i] = 0;
506     }
507
508     decompress_code(
509         index,
510         linear_signs,
511         linear_codewords,
512         pOverflow);
513
514     /* decode the positions and signs of pulses and build the codeword */
515     for (j = 0; j < NB_TRACK_MR102; j++)    /* NB_TRACK_MR102 = 4 */
516     {
517         /* position of pulse "j" */
518
519         pos1 = (linear_codewords[j] << 2) + j;
520
521
522         if (linear_signs[j] == 0)
523         {
524             sign = POS_CODE; /* +1.0 */
525         }
526         else
527         {
528             sign = -NEG_CODE; /* -1.0 */
529         }
530
531         if (pos1 < L_SUBFR)
532         {
533             cod[pos1] = sign;    /* avoid buffer overflow */
534         }
535
536         /* compute index i */
537         /* position of pulse "j+4" */
538
539         pos2 = (linear_codewords[j + 4] << 2) + j;
540
541
542         if (pos2 < pos1)
543         {
544             sign = negate(sign);
545         }
546
547         if (pos2 < L_SUBFR)
548         {
549             cod[pos2] += sign;     /* avoid buffer overflow */
550         }
551
552
553     } /* for (j = 0; j < NB_TRACK_MR102; j++) */
554
555     return;
556 }