1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
30 ------------------------------------------------------------------------------
36 ------------------------------------------------------------------------------
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 ------------------------------------------------------------------------------
45 /*----------------------------------------------------------------------------
47 ----------------------------------------------------------------------------*/
58 /*----------------------------------------------------------------------------
60 ; Define module specific macros here
61 ----------------------------------------------------------------------------*/
63 /*----------------------------------------------------------------------------
65 ; Include all pre-processor statements here. Include conditional
66 ; compile variables also.
67 ----------------------------------------------------------------------------*/
70 /* define values/representation for output codevector and sign */
73 #define POS_SIGN 32767
74 #define NEG_SIGN (Word16) (-32768L)
76 /*----------------------------------------------------------------------------
77 ; LOCAL FUNCTION DEFINITIONS
78 ; Function Prototype declaration
79 ----------------------------------------------------------------------------*/
81 /*----------------------------------------------------------------------------
82 ; LOCAL VARIABLE DEFINITIONS
83 ; Variable declaration - defined here and used outside this module
84 ----------------------------------------------------------------------------*/
86 /*----------------------------------------------------------------------------
87 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
88 ; Declare variables used in this module but defined elsewhere
89 ----------------------------------------------------------------------------*/
92 ------------------------------------------------------------------------------
94 ------------------------------------------------------------------------------
95 INPUT AND OUTPUT DEFINITIONS
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
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)
109 pOverflow Pointer to Flag -- set when overflow occurs
114 Global Variables Used:
117 Local Variables Needed:
119 ------------------------------------------------------------------------------
123 ------------------------------------------------------------------------------
128 ------------------------------------------------------------------------------
131 [1] c8_31pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
133 ------------------------------------------------------------------------------
136 ------------------------------------------------------------------------------
138 [State any special notes, constraints or cautions for users of this function]
140 ------------------------------------------------------------------------------
143 /*************************************************************************
145 * FUNCTION: build_code()
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.
151 *************************************************************************/
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 */
170 Word16 _sign[NB_PULSE];
181 Word16 *p_cod = &cod[0];
182 Word16 *p_codvec = &codvec[0];
186 for (i = 0; i < L_CODE; i++)
191 for (i = 0; i < NB_TRACK_MR102; i++)
197 for (k = 0; k < NB_PULSE; k++)
199 /* read pulse position */
204 pos_index = i >> 2; /* index = pos/4 */
206 track = i & 3; /* track = pos%4 */
210 cod[i] = (Word16)((Word32) cod[i] + POS_CODE);
213 sign_index = 0; /* bit=0 -> positive pulse */
217 cod[i] = (Word16)((Word32) cod[i] - NEG_CODE);
220 sign_index = 1; /* bit=1 => negative pulse */
221 /* index = add (index, 8); 1 = negative old code */
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;
230 { /* 2nd row of pulses , test if positions needs to be switched */
231 if (((sign_index ^ sign_indx[track]) & 1) == 0)
233 /* sign of 1st pulse == sign of 2nd pulse */
235 if (pos_indx[track] <= pos_index)
237 pos_indx[track + NB_TRACK_MR102] = pos_index;
241 pos_indx[track + NB_TRACK_MR102] = pos_indx[track];
243 pos_indx[track] = pos_index;
244 sign_indx[track] = sign_index;
249 /* sign of 1st pulse != sign of 2nd pulse */
251 if (pos_indx[track] <= pos_index)
253 pos_indx[track + NB_TRACK_MR102] = pos_indx[track];
255 pos_indx[track] = pos_index;
256 sign_indx[track] = sign_index;
260 pos_indx[track + NB_TRACK_MR102] = pos_index;
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);
275 for (i = 0; i < L_CODE; i++)
333 } /* for (i = 0; i < L_CODE; i++) */
337 /****************************************************************************/
340 ------------------------------------------------------------------------------
341 FUNCTION NAME: compress_code()
342 ------------------------------------------------------------------------------
343 INPUT AND OUTPUT DEFINITIONS
352 Global Variables Used:
355 Local Variables Needed:
357 ------------------------------------------------------------------------------
362 PURPOSE: compression of three indeces [0..9] to one 10 bit index
363 minimizing the phase shift of a bit error.
365 ------------------------------------------------------------------------------
370 ------------------------------------------------------------------------------
373 [1] c8_31pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
375 ------------------------------------------------------------------------------
378 ------------------------------------------------------------------------------
380 [State any special notes, constraints or cautions for users of this function]
382 ------------------------------------------------------------------------------
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 */
398 OSCL_UNUSED_ARG(pOverflow);
404 tempWord32 = ((Word32) ib * 5) << 1;
406 tempWord32 = tempWord32 >> 1;
408 ib = (Word16) tempWord32;
412 tempWord32 = ((Word32) ic * 25) << 1;
414 tempWord32 = tempWord32 >> 1;
416 ic = (Word16) tempWord32;
426 ib = ((Word16)(pos_indxB & 1)) << 1;
428 ic = ((Word16)(pos_indxC & 1)) << 2;
440 /****************************************************************************/
443 ------------------------------------------------------------------------------
444 FUNCTION NAME: compress_code()
445 ------------------------------------------------------------------------------
446 INPUT AND OUTPUT DEFINITIONS
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
454 indx Array of type Word16 -- position and sign of 8 pulses
456 pOverflow Pointer to Flag -- set when overflow occurs
461 Global Variables Used:
464 Local Variables Needed:
467 ------------------------------------------------------------------------------
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
478 ------------------------------------------------------------------------------
483 ------------------------------------------------------------------------------
486 [1] c3_14pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
488 ------------------------------------------------------------------------------
491 ------------------------------------------------------------------------------
493 [State any special notes, constraints or cautions for users of this function]
495 ------------------------------------------------------------------------------
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 */
509 Word16 *p_indx = &indx[0];
510 Word16 *p_sign_indx = &sign_indx[0];
514 for (i = 0; i < NB_TRACK_MR102; i++)
516 *(p_indx++) = *(p_sign_indx++);
520 indx[NB_TRACK] = (ia/2+(ib/2)*5 +(ic/2)*25)*8 + ia%2 + (ib%2)*2 + (ic%2)*4; */
522 indx[NB_TRACK_MR102] =
530 indx[NB_TRACK+1] = (ia/2+(ib/2)*5 +(ic/2)*25)*8 + ia%2 + (ib%2)*2 + (ic%2)*4; */
532 indx[NB_TRACK_MR102+1] =
542 indx[NB_TRACK+2] = ((((4-ia/2) + (ib/2)*5)*32+12)/25)*4 + ia%2 + (ib%2)*2;
544 indx[NB_TRACK+2] = ((((ia/2) + (ib/2)*5)*32+12)/25)*4 + ia%2 + (ib%2)*2;
547 ib = pos_indx[7] >> 1;
551 ia = pos_indx[3] >> 1;
558 ib = pos_indx[7] >> 1;
560 tempWord32 = ((Word32) ib * 5) << 1;
562 tempWord32 = tempWord32 >> 1;
564 ib = (Word16) tempWord32;
572 ic = (Word16)(((Word32) ib * 1311) >> 15);
576 ia = pos_indx[3] & 1;
578 ib = ((Word16)(pos_indx[7] & 1)) << 1;
584 indx[NB_TRACK_MR102+2] = ib;
586 } /* compress_code */
589 /****************************************************************************/
592 ------------------------------------------------------------------------------
593 FUNCTION NAME: code_8i40_31bits()
594 ------------------------------------------------------------------------------
595 INPUT AND OUTPUT DEFINITIONS
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
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
612 Global Variables Used:
615 Local Variables Needed:
618 ------------------------------------------------------------------------------
623 PURPOSE: Searches a 31 bit algebraic codebook containing 8 pulses
624 in a frame of 40 samples.
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:
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
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.
645 ------------------------------------------------------------------------------
650 ------------------------------------------------------------------------------
653 [1] c8_31pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
655 ------------------------------------------------------------------------------
658 ------------------------------------------------------------------------------
660 [State any special notes, constraints or cautions for users of this function]
662 ------------------------------------------------------------------------------
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
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 */
675 Word16 ipos[NB_PULSE];
676 Word16 pos_max[NB_TRACK_MR102];
677 Word16 codvec[NB_PULSE];
682 Word16 rr[L_CODE][L_CODE];
683 Word16 linear_signs[NB_TRACK_MR102];
684 Word16 linear_codewords[NB_PULSE];
695 /* 2 = use GSMEFR scaling */
707 /* same setsign alg as GSM-EFR new constants though*/
742 } /* code_8i40_31bits */