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 ------------------------------------------------------------------------------
40 ------------------------------------------------------------------------------
43 *************************************************************************
45 * FUNCTION: code_2i40_11bits()
47 * PURPOSE: Searches a 11 bit algebraic codebook containing 2 pulses
48 * in a frame of 40 samples.
51 * The code length is 40, containing 2 nonzero pulses: i0...i1.
52 * All pulses can have two possible amplitudes: +1 or -1.
53 * Pulse i0 can have 2x8=16 possible positions, pulse i1 can have
56 * i0 : 1, 6, 11, 16, 21, 26, 31, 36.
57 * 3, 8, 13, 18, 23, 28, 33, 38.
58 * i1 : 0, 5, 10, 15, 20, 25, 30, 35.
59 * 1, 6, 11, 16, 21, 26, 31, 36.
60 * 2, 7, 12, 17, 22, 27, 32, 37.
61 * 4, 9, 14, 19, 24, 29, 34, 39.
63 *************************************************************************
64 ------------------------------------------------------------------------------
67 /*----------------------------------------------------------------------------
69 ----------------------------------------------------------------------------*/
78 /*----------------------------------------------------------------------------
80 ; Define module specific macros here
81 ----------------------------------------------------------------------------*/
83 /*----------------------------------------------------------------------------
85 ; Include all pre-processor statements here. Include conditional
86 ; compile variables also.
87 ----------------------------------------------------------------------------*/
90 #define _1_2 (Word16)(32768L/2)
91 #define _1_4 (Word16)(32768L/4)
92 #define _1_8 (Word16)(32768L/8)
93 #define _1_16 (Word16)(32768L/16)
95 /*----------------------------------------------------------------------------
96 ; LOCAL FUNCTION DEFINITIONS
97 ; Function Prototype declaration
98 ----------------------------------------------------------------------------*/
99 static void search_2i40(
100 Word16 dn[], /* i : correlation between target and h[] */
101 Word16 rr[][L_CODE],/* i : matrix of autocorrelation */
102 Word16 codvec[], /* o : algebraic codebook vector */
106 static Word16 build_code(
107 Word16 codvec[], /* i : algebraic codebook vector */
108 Word16 dn_sign[], /* i : sign of dn[] */
109 Word16 cod[], /* o : algebraic (fixed) codebook excitation */
110 Word16 h[], /* i : impulse response of weighted synthesis filter */
111 Word16 y[], /* o : filtered fixed codebook excitation */
112 Word16 sign[], /* o : sign of 2 pulses */
116 /*----------------------------------------------------------------------------
117 ; LOCAL VARIABLE DEFINITIONS
118 ; Variable declaration - defined here and used outside this module
119 ----------------------------------------------------------------------------*/
121 const Word16 startPos1[2] = {1, 3};
122 const Word16 startPos2[4] = {0, 1, 2, 4};
125 ------------------------------------------------------------------------------
126 FUNCTION NAME: code_2i40_11bits
127 ------------------------------------------------------------------------------
128 INPUT AND OUTPUT DEFINITIONS
131 x, target vector, array of type Word16
132 h, impulse response of weighted synthesis filter, array of type Word16
133 T0, Pitch lag, variable of type Word16
134 pitch_sharp, Last quantized pitch gain, variable of type Word16
137 code[], Innovative codebook, array of type Word16
138 y[], filtered fixed codebook excitation, array of type Word16
139 sign, Signs of 2 pulses, pointer of type Word16 *
140 pOverflow Flag set when overflow occurs, pointer of type Flag *
145 Global Variables Used:
148 Local Variables Needed:
151 ------------------------------------------------------------------------------
154 Searches a 11 bit algebraic codebook containing 2 pulses
155 in a frame of 40 samples.
157 The code length is 40, containing 2 nonzero pulses: i0...i1.
158 All pulses can have two possible amplitudes: +1 or -1.
159 Pulse i0 can have 2x8=16 possible positions, pulse i1 can have
162 i0 : 1, 6, 11, 16, 21, 26, 31, 36.
163 3, 8, 13, 18, 23, 28, 33, 38.
164 i1 : 0, 5, 10, 15, 20, 25, 30, 35.
165 1, 6, 11, 16, 21, 26, 31, 36.
166 2, 7, 12, 17, 22, 27, 32, 37.
167 4, 9, 14, 19, 24, 29, 34, 39.
169 ------------------------------------------------------------------------------
174 ------------------------------------------------------------------------------
177 c2_11pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
179 ------------------------------------------------------------------------------
183 ------------------------------------------------------------------------------
185 [State any special notes, constraints or cautions for users of this function]
187 ------------------------------------------------------------------------------
189 Word16 code_2i40_11bits(
190 Word16 x[], /* i : target vector */
191 Word16 h[], /* i : impulse response of weighted synthesis filter */
192 /* h[-L_subfr..-1] must be set to zero. */
193 Word16 T0, /* i : Pitch lag */
194 Word16 pitch_sharp, /* i : Last quantized pitch gain */
195 Word16 code[], /* o : Innovative codebook */
196 Word16 y[], /* o : filtered fixed codebook excitation */
197 Word16 * sign, /* o : Signs of 2 pulses */
198 Flag * pOverflow /* o : Flag set when overflow occurs */
201 Word16 codvec[NB_PULSE];
204 Word16 dn_sign[L_CODE];
206 Word16 rr[L_CODE][L_CODE];
213 sharp = pitch_sharp << 1;
217 for (i = T0; i < L_CODE; i++)
245 8); /* dn2[] not used in this codebook search */
259 /* function result */
272 * Compute innovation vector gain.
273 * Include fixed-gain pitch contribution into code[].
278 for (i = T0; i < L_CODE; i++)
297 /****************************************************************************/
301 ------------------------------------------------------------------------------
302 FUNCTION NAME: search_2i40
303 ------------------------------------------------------------------------------
304 INPUT AND OUTPUT DEFINITIONS
307 dn, correlation between target and h[], array of type Word16
308 rr, matrix of autocorrelation, double-array of type Word16
311 codvec[], algebraic codebook vector, array of type Word16
312 pOverflow, Flag set when overflow occurs, pointer of type Flag *
317 Global Variables Used:
320 Local Variables Needed:
323 ------------------------------------------------------------------------------
326 Search the best codevector; determine positions of the 2 pulses
327 in the 40-sample frame.
329 ------------------------------------------------------------------------------
334 ------------------------------------------------------------------------------
337 c2_11pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
339 ------------------------------------------------------------------------------
343 ------------------------------------------------------------------------------
345 [State any special notes, constraints or cautions for users of this function]
347 ------------------------------------------------------------------------------
350 static void search_2i40(
351 Word16 dn[], /* i : correlation between target and h[] */
352 Word16 rr[][L_CODE], /* i : matrix of autocorrelation */
353 Word16 codvec[], /* o : algebraic codebook vector */
354 Flag * pOverflow /* o : Flag set when overflow occurs */
359 Word16 ix = 0; /* initialization only needed to keep gcc silent */
362 Word16 ipos[NB_PULSE];
379 Word16 *p_codvec = &codvec[0];
384 for (i = 0; i < NB_PULSE; i++)
389 /*------------------------------------------------------------------*
390 * main loop: try 2x4 tracks. *
391 *------------------------------------------------------------------*/
393 for (track1 = 0; track1 < 2; track1++)
395 for (track2 = 0; track2 < 4; track2++)
397 /* fix starting position */
398 ipos[0] = startPos1[track1];
399 ipos[1] = startPos2[track2];
401 /*----------------------------------------------------------------*
402 * i0 loop: try 8 positions. *
403 *----------------------------------------------------------------*/
404 for (i0 = ipos[0]; i0 < L_CODE; i0 += STEP)
408 /* alp0 = L_mult(rr[i0][i0], _1_4, pOverflow); */
409 alp0 = (Word32) rr[i0][i0] << 14;
411 /*-------------------------------------------------------------*
412 * i1 loop: 8 positions. *
413 *-------------------------------------------------------------*/
419 /*---------------------------------------------------------------*
420 * These index have low complexity address computation because *
421 * they are, in fact, pointers with fixed increment. For example,*
422 * "rr[i0][i2]" is a pointer initialized to "&rr[i0][ipos[2]]" *
423 * and incremented by "STEP". *
424 *---------------------------------------------------------------*/
426 for (i1 = ipos[1]; i1 < L_CODE; i1 += STEP)
428 /* idx increment = STEP */
429 ps1 = add_16(ps0, dn[i1], pOverflow);
431 /* alp1 = alp0 + rr[i0][i1] + 1/2*rr[i1][i1]; */
433 /* idx incr = STEP */
434 /* alp1 = L_mac(alp0, rr[i1][i1], _1_4, pOverflow); */
435 alp1 = alp0 + ((Word32) rr[i1][i1] << 14);
437 /* idx incr = STEP */
438 /* alp1 = L_mac(alp1, rr[i0][i1], _1_2, pOverflow); */
439 alp1 += (Word32) rr[i0][i1] << 15;
441 /* sq1 = mult(ps1, ps1, pOverflow); */
442 sq1 = (Word16)(((Word32) ps1 * ps1) >> 15);
444 /* alp_16 = pv_round(alp1, pOverflow); */
445 alp_16 = (Word16)((alp1 + (Word32) 0x00008000L) >> 16);
447 /* s = L_mult(alp, sq1, pOverflow); */
448 s = ((Word32) alp * sq1) << 1;
450 /* s =L_msu(s, sq, alp_16, pOverflow); */
451 s -= (((Word32) sq * alp_16) << 1);
460 } /* for (i1 = ipos[1]; i1 < L_CODE; i1 += STEP) */
462 /* memorize codevector if this one is better than the last one. */
464 /* s = L_mult(alpk, sq, pOverflow); */
465 s = ((Word32) alpk * sq) << 1;
467 /* s = L_msu(s, psk, alp, pOverflow); */
468 s -= (((Word32) psk * alp) << 1);
474 p_codvec = &codvec[0];
480 } /* for (i0 = ipos[0]; i0 < L_CODE; i0 += STEP) */
482 } /* for (track2 = 0; track2 < 4; track2++) */
484 } /* for (track1 = 0; track1 < 2; track1++) */
490 /****************************************************************************/
494 ------------------------------------------------------------------------------
495 FUNCTION NAME: build_code
496 ------------------------------------------------------------------------------
497 INPUT AND OUTPUT DEFINITIONS
500 codvec, position of pulses, array of type Word16
501 dn_sign, sign of pulses, array of type Word16
502 h, impulse response of weighted synthesis filter, Word16 array
506 cod, innovative code vector, array of type Word16
507 y[], filtered innovative code, array of type Word16
508 sign[], sign of 2 pulses, array of type Word16
509 pOverflow, Flag set when overflow occurs, pointer of type Flag *
513 Global Variables Used:
516 Local Variables Needed:
519 ------------------------------------------------------------------------------
522 Builds the codeword, the filtered codeword and index of the
523 codevector, based on the signs and positions of 2 pulses.
525 ------------------------------------------------------------------------------
530 ------------------------------------------------------------------------------
533 c2_11pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
535 ------------------------------------------------------------------------------
538 ------------------------------------------------------------------------------
540 [State any special notes, constraints or cautions for users of this function]
542 ------------------------------------------------------------------------------
544 static Word16 build_code(
545 Word16 codvec[], /* i : position of pulses */
546 Word16 dn_sign[], /* i : sign of pulses */
547 Word16 cod[], /* o : innovative code vector */
548 Word16 h[], /* i : impulse response of weighted synthesis filter */
549 Word16 y[], /* o : filtered innovative code */
550 Word16 sign[], /* o : sign of 2 pulses */
551 Flag * pOverflow /* o : Flag set when overflow occurs */
559 Word16 _sign[NB_PULSE];
569 for (i = 0; i < L_CODE; i++)
577 for (k = 0; k < NB_PULSE; k++)
579 i = codvec[k]; /* read pulse position */
580 j = dn_sign[i]; /* read sign */
583 /* index = mult(i, 6554, pOverflow); */
584 index = (Word16)(((Word32) i * 6554) >> 15);
592 tempWord = (index << 3) + (index << 1);
607 track = i - tempWord;
645 tempWord = index << 6;
652 index = tempWord + 16;
664 tempWord = index << 6;
671 index = tempWord + 32;
682 tempWord = index << 1;
689 index = tempWord + 1;
700 tempWord = index << 6;
707 index = tempWord + 48;
730 _sign[k] = (Word16) - 32768L;
744 for (i = 0; i < L_CODE; i++)