Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / c8_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: c8_31pf.cpp
35
36 ------------------------------------------------------------------------------
37  MODULE DESCRIPTION
38
39  Purpose          : Searches a 31 bit algebraic codebook containing
40                   : 8 pulses in a frame of 40 samples.
41                   : in the same manner as GSM-EFR
42 ------------------------------------------------------------------------------
43 */
44
45 /*----------------------------------------------------------------------------
46 ; INCLUDES
47 ----------------------------------------------------------------------------*/
48 #include "c8_31pf.h"
49 #include "typedef.h"
50 #include "cnst.h"
51 #include "inv_sqrt.h"
52 #include "cor_h.h"
53 #include "cor_h_x2.h"
54 #include "set_sign.h"
55 #include "s10_8pf.h"
56 #include "basic_op.h"
57
58 /*----------------------------------------------------------------------------
59 ; MACROS
60 ; Define module specific macros here
61 ----------------------------------------------------------------------------*/
62
63 /*----------------------------------------------------------------------------
64 ; DEFINES
65 ; Include all pre-processor statements here. Include conditional
66 ; compile variables also.
67 ----------------------------------------------------------------------------*/
68 #define NB_PULSE 8
69
70 /* define values/representation for output codevector and sign */
71 #define POS_CODE  8191
72 #define NEG_CODE  8191
73 #define POS_SIGN  32767
74 #define NEG_SIGN  (Word16) (-32768L)
75
76 /*----------------------------------------------------------------------------
77 ; LOCAL FUNCTION DEFINITIONS
78 ; Function Prototype declaration
79 ----------------------------------------------------------------------------*/
80
81 /*----------------------------------------------------------------------------
82 ; LOCAL VARIABLE DEFINITIONS
83 ; Variable declaration - defined here and used outside this module
84 ----------------------------------------------------------------------------*/
85
86 /*----------------------------------------------------------------------------
87 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
88 ; Declare variables used in this module but defined elsewhere
89 ----------------------------------------------------------------------------*/
90
91 /*
92 ------------------------------------------------------------------------------
93  FUNCTION NAME:
94 ------------------------------------------------------------------------------
95  INPUT AND OUTPUT DEFINITIONS
96
97  Inputs:
98     codvec[]   Array of type Word16 -- position of pulses
99     sign[]     Array of type Word16 -- sign of pulses
100     h[]        Array of type Word16 -- impulse response of
101                                        weighted synthesis filter
102  Outputs:
103     cod[]       Array of type Word16 -- innovative code vector
104     y[]         Array of type Word16 -- filtered innovative code
105     sign_indx[] Array of type Word16 -- signs of 4 pulses (signs only)
106     pos_indx[]  Array of type Word16 --
107                              position index of 8 pulses(position only)
108
109     pOverflow  Pointer to Flag  -- set when overflow occurs
110
111  Returns:
112     indx
113
114  Global Variables Used:
115     None
116
117  Local Variables Needed:
118
119 ------------------------------------------------------------------------------
120  FUNCTION DESCRIPTION
121
122
123 ------------------------------------------------------------------------------
124  REQUIREMENTS
125
126  None
127
128 ------------------------------------------------------------------------------
129  REFERENCES
130
131  [1] c8_31pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
132
133 ------------------------------------------------------------------------------
134  PSEUDO-CODE
135
136 ------------------------------------------------------------------------------
137  CAUTION [optional]
138  [State any special notes, constraints or cautions for users of this function]
139
140 ------------------------------------------------------------------------------
141 */
142
143 /*************************************************************************
144  *
145  *  FUNCTION:  build_code()
146  *
147  *  PURPOSE: Builds the codeword, the filtered codeword and a
148  *   linear uncombined version of  the index of the
149  *           codevector, based on the signs and positions of 8  pulses.
150  *
151  *************************************************************************/
152
153 static void build_code(
154     Word16 codvec[],    /* i : position of pulses                           */
155     Word16 sign[],      /* i : sign of d[n]                                 */
156     Word16 cod[],       /* o : innovative code vector                       */
157     Word16 h[],         /* i : impulse response of weighted synthesis filter*/
158     Word16 y[],         /* o : filtered innovative code                     */
159     Word16 sign_indx[], /* o : signs of 4  pulses (signs only)              */
160     Word16 pos_indx[],  /* o : position index of 8 pulses(position only)    */
161     Flag   * pOverflow  /* o : Flag set when overflow occurs                */
162 )
163 {
164     Word16 i;
165     Word16 j;
166     Word16 k;
167     Word16 track;
168     Word16 sign_index;
169     Word16 pos_index;
170     Word16 _sign[NB_PULSE];
171
172     Word16 *p0;
173     Word16 *p1;
174     Word16 *p2;
175     Word16 *p3;
176     Word16 *p4;
177     Word16 *p5;
178     Word16 *p6;
179     Word16 *p7;
180
181     Word16 *p_cod = &cod[0];
182     Word16 *p_codvec = &codvec[0];
183
184     Word32 s;
185
186     for (i = 0; i < L_CODE; i++)
187     {
188         *(p_cod++) = 0;
189     }
190
191     for (i = 0; i < NB_TRACK_MR102; i++)
192     {
193         pos_indx[i] = -1;
194         sign_indx[i] = -1;
195     }
196
197     for (k = 0; k < NB_PULSE; k++)
198     {
199         /* read pulse position */
200         i = codvec[k];
201         /* read sign           */
202         j = sign[i];
203
204         pos_index = i >> 2; /* index = pos/4 */
205
206         track = i & 3;     /* track = pos%4 */
207
208         if (j > 0)
209         {
210             cod[i] = (Word16)((Word32) cod[i] + POS_CODE);
211
212             _sign[k] = POS_SIGN;
213             sign_index = 0;  /* bit=0 -> positive pulse */
214         }
215         else
216         {
217             cod[i] = (Word16)((Word32) cod[i] - NEG_CODE);
218
219             _sign[k] = NEG_SIGN;
220             sign_index = 1; /* bit=1 => negative pulse */
221             /* index = add (index, 8); 1 = negative  old code */
222         }
223
224         if (pos_indx[track] < 0)
225         {   /* first set first NB_TRACK pulses  */
226             pos_indx[track] = pos_index;
227             sign_indx[track] = sign_index;
228         }
229         else
230         {   /* 2nd row of pulses , test if positions needs to be switched */
231             if (((sign_index ^ sign_indx[track]) & 1) == 0)
232             {
233                 /* sign of 1st pulse == sign of 2nd pulse */
234
235                 if (pos_indx[track] <= pos_index)
236                 {   /* no swap */
237                     pos_indx[track + NB_TRACK_MR102] = pos_index;
238                 }
239                 else
240                 {   /* swap*/
241                     pos_indx[track + NB_TRACK_MR102] = pos_indx[track];
242
243                     pos_indx[track] = pos_index;
244                     sign_indx[track] = sign_index;
245                 }
246             }
247             else
248             {
249                 /* sign of 1st pulse != sign of 2nd pulse */
250
251                 if (pos_indx[track] <= pos_index)
252                 {  /*swap*/
253                     pos_indx[track + NB_TRACK_MR102] = pos_indx[track];
254
255                     pos_indx[track] = pos_index;
256                     sign_indx[track] = sign_index;
257                 }
258                 else
259                 {   /*no swap */
260                     pos_indx[track + NB_TRACK_MR102] = pos_index;
261                 }
262             }
263         }
264     }
265
266     p0 = h - *(p_codvec++);
267     p1 = h - *(p_codvec++);
268     p2 = h - *(p_codvec++);
269     p3 = h - *(p_codvec++);
270     p4 = h - *(p_codvec++);
271     p5 = h - *(p_codvec++);
272     p6 = h - *(p_codvec++);
273     p7 = h - *(p_codvec);
274
275     for (i = 0; i < L_CODE; i++)
276     {
277         s = 0;
278
279         s =
280             L_mac(
281                 s,
282                 *p0++,
283                 _sign[0],
284                 pOverflow);
285         s =
286             L_mac(
287                 s,
288                 *p1++,
289                 _sign[1],
290                 pOverflow);
291         s =
292             L_mac(
293                 s,
294                 *p2++,
295                 _sign[2],
296                 pOverflow);
297         s =
298             L_mac(
299                 s,
300                 *p3++,
301                 _sign[3],
302                 pOverflow);
303         s =
304             L_mac(
305                 s,
306                 *p4++,
307                 _sign[4],
308                 pOverflow);
309         s =
310             L_mac(
311                 s,
312                 *p5++,
313                 _sign[5],
314                 pOverflow);
315         s =
316             L_mac(
317                 s,
318                 *p6++,
319                 _sign[6],
320                 pOverflow);
321         s =
322             L_mac(
323                 s,
324                 *p7++,
325                 _sign[7],
326                 pOverflow);
327
328         y[i] =
329             pv_round(
330                 s,
331                 pOverflow);
332
333     } /* for (i = 0; i < L_CODE; i++) */
334
335 } /* build_code */
336
337 /****************************************************************************/
338
339 /*
340 ------------------------------------------------------------------------------
341  FUNCTION NAME: compress_code()
342 ------------------------------------------------------------------------------
343  INPUT AND OUTPUT DEFINITIONS
344
345  Inputs:
346
347  Outputs:
348
349  Returns:
350     None
351
352  Global Variables Used:
353     None
354
355  Local Variables Needed:
356
357 ------------------------------------------------------------------------------
358  FUNCTION DESCRIPTION
359
360  FUNCTION:
361
362  PURPOSE: compression of three indeces [0..9] to one 10 bit index
363           minimizing the phase shift of a bit error.
364
365 ------------------------------------------------------------------------------
366  REQUIREMENTS
367
368  None
369
370 ------------------------------------------------------------------------------
371  REFERENCES
372
373  [1] c8_31pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
374
375 ------------------------------------------------------------------------------
376  PSEUDO-CODE
377
378 ------------------------------------------------------------------------------
379  CAUTION [optional]
380  [State any special notes, constraints or cautions for users of this function]
381
382 ------------------------------------------------------------------------------
383 */
384
385 static Word16 compress10(
386     Word16 pos_indxA,  /* i : signs of 4 pulses (signs only)             */
387     Word16 pos_indxB,  /* i : position index of 8 pulses (pos only)      */
388     Word16 pos_indxC,  /* i : position and sign of 8 pulses (compressed) */
389     Flag  *pOverflow)  /* o : Flag set when overflow occurs              */
390 {
391     Word16 indx;
392     Word16 ia;
393     Word16 ib;
394     Word16 ic;
395
396     Word32 tempWord32;
397
398     OSCL_UNUSED_ARG(pOverflow);
399
400     ia = pos_indxA >> 1;
401
402     ib = pos_indxB >> 1;
403
404     tempWord32 = ((Word32) ib * 5) << 1;
405
406     tempWord32 = tempWord32 >> 1;
407
408     ib = (Word16) tempWord32;
409
410     ic = pos_indxC >> 1;
411
412     tempWord32 = ((Word32) ic * 25) << 1;
413
414     tempWord32 = tempWord32 >> 1;
415
416     ic = (Word16) tempWord32;
417
418     ib += ic;
419
420     ib += ia;
421
422     indx = ib << 3;
423
424     ia = pos_indxA & 1;
425
426     ib = ((Word16)(pos_indxB & 1)) << 1;
427
428     ic = ((Word16)(pos_indxC & 1)) << 2;
429
430     ib += ic;
431
432     ib += ia;
433
434     indx += ib;
435
436     return indx;
437
438 }
439
440 /****************************************************************************/
441
442 /*
443 ------------------------------------------------------------------------------
444  FUNCTION NAME: compress_code()
445 ------------------------------------------------------------------------------
446  INPUT AND OUTPUT DEFINITIONS
447
448  Inputs:
449     sign_indx   Array of type Word16 -- signs of 4 pulses (signs only)
450     pos_indx    Array of type Word16 -- position index of 8 pulses
451                                             (position only)
452
453  Outputs:
454     indx         Array of type Word16 -- position and sign of 8 pulses
455                                             (compressed)
456     pOverflow    Pointer to Flag      -- set when overflow occurs
457
458  Returns:
459     None
460
461  Global Variables Used:
462     None
463
464  Local Variables Needed:
465     None
466
467 ------------------------------------------------------------------------------
468  FUNCTION DESCRIPTION
469
470  PURPOSE: compression of the linear codewords to 4+three indeces
471           one bit from each pulse is made robust to errors by
472           minimizing the phase shift of a bit error.
473           4 signs (one for each track)
474           i0,i4,i1 => one index (7+3) bits, 3   LSBs more robust
475           i2,i6,i5 => one index (7+3) bits, 3   LSBs more robust
476           i3,i7    => one index (5+2) bits, 2-3 LSbs more robust
477
478 ------------------------------------------------------------------------------
479  REQUIREMENTS
480
481  None
482
483 ------------------------------------------------------------------------------
484  REFERENCES
485
486  [1] c3_14pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
487
488 ------------------------------------------------------------------------------
489  PSEUDO-CODE
490
491 ------------------------------------------------------------------------------
492  CAUTION [optional]
493  [State any special notes, constraints or cautions for users of this function]
494
495 ------------------------------------------------------------------------------
496 */
497
498 static void compress_code(
499     Word16 sign_indx[], /* i : signs of 4 pulses (signs only)             */
500     Word16 pos_indx[],  /* i : position index of 8 pulses (position only) */
501     Word16 indx[],      /* o : position and sign of 8 pulses (compressed) */
502     Flag  *pOverflow)   /* o : Flag set when overflow occurs              */
503 {
504     Word16 i;
505     Word16 ia;
506     Word16 ib;
507     Word16 ic;
508
509     Word16 *p_indx = &indx[0];
510     Word16 *p_sign_indx = &sign_indx[0];
511
512     Word32 tempWord32;
513
514     for (i = 0; i < NB_TRACK_MR102; i++)
515     {
516         *(p_indx++) = *(p_sign_indx++);
517     }
518
519     /* First index
520       indx[NB_TRACK] = (ia/2+(ib/2)*5 +(ic/2)*25)*8 + ia%2 + (ib%2)*2 + (ic%2)*4; */
521
522     indx[NB_TRACK_MR102] =
523         compress10(
524             pos_indx[0],
525             pos_indx[4],
526             pos_indx[1],
527             pOverflow);
528
529     /* Second index
530       indx[NB_TRACK+1] = (ia/2+(ib/2)*5 +(ic/2)*25)*8 + ia%2 + (ib%2)*2 + (ic%2)*4; */
531
532     indx[NB_TRACK_MR102+1] =
533         compress10(
534             pos_indx[2],
535             pos_indx[6],
536             pos_indx[5],
537             pOverflow);
538
539     /*
540       Third index
541       if ((ib/2)%2 == 1)
542         indx[NB_TRACK+2] = ((((4-ia/2) + (ib/2)*5)*32+12)/25)*4 + ia%2 + (ib%2)*2;
543       else
544         indx[NB_TRACK+2] = ((((ia/2) +   (ib/2)*5)*32+12)/25)*4 + ia%2 + (ib%2)*2;
545         */
546
547     ib = pos_indx[7] >> 1;
548
549     ib &= 1;
550
551     ia = pos_indx[3] >> 1;
552
553     if (ib == 1)
554     {
555         ia = 4 - ia;
556     }
557
558     ib = pos_indx[7] >> 1;
559
560     tempWord32 = ((Word32) ib * 5) << 1;
561
562     tempWord32 = tempWord32 >> 1;
563
564     ib = (Word16) tempWord32;
565
566     ib += ia;
567
568     ib <<= 5;
569
570     ib += 12;
571
572     ic = (Word16)(((Word32) ib * 1311) >> 15);
573
574     ic <<= 2;
575
576     ia = pos_indx[3] & 1;
577
578     ib = ((Word16)(pos_indx[7] & 1)) << 1;
579
580     ib += ic;
581
582     ib += ia;
583
584     indx[NB_TRACK_MR102+2] = ib;
585
586 } /* compress_code */
587
588
589 /****************************************************************************/
590
591 /*
592 ------------------------------------------------------------------------------
593  FUNCTION NAME: code_8i40_31bits()
594 ------------------------------------------------------------------------------
595  INPUT AND OUTPUT DEFINITIONS
596
597  Inputs:
598     x   Array of type Word16 -- target vector
599     cn  Array of type Word16 -- residual after long term prediction
600     h   Array of type Word16 -- impulse response of weighted synthesis filter
601
602
603  Outputs:
604     cod Array of type Word16 -- algebraic (fixed) codebook excitation
605     y   Array of type Word16 -- filtered fixed codebook excitation
606     indx Array of type Word16 -- index of 8 pulses (signs+positions)
607     pOverflow    Pointer to Flag      -- set when overflow occurs
608
609  Returns:
610     None
611
612  Global Variables Used:
613     None
614
615  Local Variables Needed:
616     None
617
618 ------------------------------------------------------------------------------
619  FUNCTION DESCRIPTION
620
621  FUNCTION:
622
623  PURPOSE:  Searches a 31 bit algebraic codebook containing 8 pulses
624            in a frame of 40 samples.
625
626  DESCRIPTION:
627    The code contains 8 nonzero pulses: i0...i7.
628    All pulses can have two possible amplitudes: +1 or -1.
629    The 40 positions in a subframe are divided into 4 tracks of
630    interleaved positions. Each track contains two pulses.
631    The pulses can have the following possible positions:
632
633       i0, i4 :  0, 4, 8,  12, 16, 20, 24, 28, 32, 36
634       i1, i5 :  1, 5, 9,  13, 17, 21, 25, 29, 33, 37
635       i2, i6 :  2, 6, 10, 14, 18, 22, 26, 30, 34, 38
636       i3, i7 :  3, 7, 11, 15, 19, 23, 27, 31, 35, 39
637
638    Each pair of pulses require 1 bit for their signs. The positions
639    are encoded together 3,3 and 2 resulting in
640    (7+3) + (7+3) + (5+2) bits for their
641    positions. This results in a 31 (4 sign and 27 pos) bit codebook.
642    The function determines the optimal pulse signs and positions, builds
643    the codevector, and computes the filtered codevector.
644
645 ------------------------------------------------------------------------------
646  REQUIREMENTS
647
648  None
649
650 ------------------------------------------------------------------------------
651  REFERENCES
652
653  [1] c8_31pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
654
655 ------------------------------------------------------------------------------
656  PSEUDO-CODE
657
658 ------------------------------------------------------------------------------
659  CAUTION [optional]
660  [State any special notes, constraints or cautions for users of this function]
661
662 ------------------------------------------------------------------------------
663 */
664 void code_8i40_31bits(
665     Word16 x[],        /* i : target vector                                  */
666     Word16 cn[],       /* i : residual after long term prediction            */
667     Word16 h[],        /* i : impulse response of weighted synthesis
668                              filter                                         */
669     Word16 cod[],      /* o : algebraic (fixed) codebook excitation          */
670     Word16 y[],        /* o : filtered fixed codebook excitation             */
671     Word16 indx[],     /* o : 7 Word16, index of 8 pulses (signs+positions)  */
672     Flag  *pOverflow   /* o : Flag set when overflow occurs                  */
673 )
674 {
675     Word16 ipos[NB_PULSE];
676     Word16 pos_max[NB_TRACK_MR102];
677     Word16 codvec[NB_PULSE];
678
679     Word16 dn[L_CODE];
680     Word16 sign[L_CODE];
681
682     Word16 rr[L_CODE][L_CODE];
683     Word16 linear_signs[NB_TRACK_MR102];
684     Word16 linear_codewords[NB_PULSE];
685
686     cor_h_x2(
687         h,
688         x,
689         dn,
690         2,
691         NB_TRACK_MR102,
692         STEP_MR102,
693         pOverflow);
694
695     /* 2 = use GSMEFR scaling */
696
697     set_sign12k2(
698         dn,
699         cn,
700         sign,
701         pos_max,
702         NB_TRACK_MR102,
703         ipos,
704         STEP_MR102,
705         pOverflow);
706
707     /* same setsign alg as GSM-EFR new constants though*/
708
709     cor_h(
710         h,
711         sign,
712         rr,
713         pOverflow);
714
715     search_10and8i40(
716         NB_PULSE,
717         STEP_MR102,
718         NB_TRACK_MR102,
719         dn,
720         rr,
721         ipos,
722         pos_max,
723         codvec,
724         pOverflow);
725
726     build_code(
727         codvec,
728         sign,
729         cod,
730         h,
731         y,
732         linear_signs,
733         linear_codewords,
734         pOverflow);
735
736     compress_code(
737         linear_signs,
738         linear_codewords,
739         indx,
740         pOverflow);
741
742 } /* code_8i40_31bits */
743
744
745