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 ------------------------------------------------------------------------------
39 ------------------------------------------------------------------------------
42 This file contains the function that searches a 35 bit algebraic codebook
43 containing 10 pulses in a frame of 40 samples.
45 ------------------------------------------------------------------------------
48 /*----------------------------------------------------------------------------
50 ----------------------------------------------------------------------------*/
61 /*----------------------------------------------------------------------------
63 ; [Define module specific macros here]
64 ----------------------------------------------------------------------------*/
67 /*----------------------------------------------------------------------------
69 ; [Include all pre-processor statements here. Include conditional
70 ; compile variables also.]
71 ----------------------------------------------------------------------------*/
74 /*----------------------------------------------------------------------------
75 ; LOCAL FUNCTION DEFINITIONS
76 ; [List function prototypes here]
77 ----------------------------------------------------------------------------*/
79 /*----------------------------------------------------------------------------
80 ; LOCAL VARIABLE DEFINITIONS
81 ; [Variable declaration - defined here and used outside this module]
82 ----------------------------------------------------------------------------*/
85 ------------------------------------------------------------------------------
87 ------------------------------------------------------------------------------
88 INPUT AND OUTPUT DEFINITIONS
91 pShift_reg = pointer to Old CN generator shift register state (Word32)
92 no_bits = Number of bits (Word16)
95 pShift_reg -> Updated CN generator shift register state
98 noise_bits = Generated random integer value (Word16)
100 Global Variables Used:
103 Local Variables Needed:
106 ------------------------------------------------------------------------------
109 This is a local function that determnes the index of the pulses by looking up
110 the gray encoder table
112 ------------------------------------------------------------------------------
117 ------------------------------------------------------------------------------
120 c1035pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
122 ------------------------------------------------------------------------------
126 Word16 *ind, // Pulse position
127 Word16 n // Pulse number
136 *ind = (tmp & 0x8) | gray[tmp & 0x7];
140 *ind = gray[tmp & 0x7];
144 ------------------------------------------------------------------------------
146 [State any special notes, constraints or cautions for users of this function]
148 ------------------------------------------------------------------------------
151 /*----------------------------------------------------------------------------
153 ----------------------------------------------------------------------------*/
156 Word16 *pInd, /* Pulse position */
157 Word16 n, /* Pulse number */
158 const Word16* gray_ptr
167 *pInd = (tmp & 0x8) | gray_ptr[tmp & 0x7];
171 *pInd = gray_ptr[tmp & 0x7];
176 ------------------------------------------------------------------------------
177 FUNCTION NAME: build_code
178 ------------------------------------------------------------------------------
179 INPUT AND OUTPUT DEFINITIONS
182 pSeed = pointer to the Old CN generator shift register state (Word32)
183 n_param = Number of parameters to randomize (Word16)
184 param_size_table = table holding paameter sizes (Word16)
185 param[] = array to hold CN generated paramters (Word16)
186 pOverflow = pointer to overflow flag (Flag)
189 param[] = CN generated parameters (Word16)
190 pSeed = Updated CN generator shift register state (Word16)
191 pOverflow -> 1 if overflow occured
196 Global Variables Used:
199 Local Variables Needed:
202 ------------------------------------------------------------------------------
205 This function builds the codeword, the filtered codeword and index of the
206 codevector, based on the signs and positions of 10 pulses.
207 ------------------------------------------------------------------------------
212 ------------------------------------------------------------------------------
215 c1035pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
217 ------------------------------------------------------------------------------
219 static void build_code (
220 Word16 codvec[], // (i) : position of pulses
221 Word16 sign[], // (i) : sign of d[n]
222 Word16 cod[], // (o) : innovative code vector
223 Word16 h[], // (i) : impulse response of weighted synthesis filter
224 Word16 y[], // (o) : filtered innovative code
225 Word16 indx[] // (o) : index of 10 pulses (sign+position)
228 Word16 i, j, k, track, index, _sign[NB_PULSE];
229 Word16 *p0, *p1, *p2, *p3, *p4, *p5, *p6, *p7, *p8, *p9;
232 for (i = 0; i < L_CODE; i++)
236 for (i = 0; i < NB_TRACK; i++)
241 for (k = 0; k < NB_PULSE; k++)
243 // read pulse position
248 index = mult (i, 6554); // index = pos/5
250 track = sub (i, extract_l (L_shr (L_mult (index, 5), 1)));
254 cod[i] = add (cod[i], 4096);
260 cod[i] = sub (cod[i], 4096);
262 index = add (index, 8);
271 if (((index ^ indx[track]) & 8) == 0)
273 // sign of 1st pulse == sign of 2nd pulse
275 if (sub (indx[track], index) <= 0)
277 indx[track + 5] = index;
281 indx[track + 5] = indx[track];
287 // sign of 1st pulse != sign of 2nd pulse
289 if (sub ((Word16)(indx[track] & 7), (Word16)(index & 7)) <= 0)
291 indx[track + 5] = indx[track];
296 indx[track + 5] = index;
313 for (i = 0; i < L_CODE; i++)
316 s = L_mac (s, *p0++, _sign[0]);
317 s = L_mac (s, *p1++, _sign[1]);
318 s = L_mac (s, *p2++, _sign[2]);
319 s = L_mac (s, *p3++, _sign[3]);
320 s = L_mac (s, *p4++, _sign[4]);
321 s = L_mac (s, *p5++, _sign[5]);
322 s = L_mac (s, *p6++, _sign[6]);
323 s = L_mac (s, *p7++, _sign[7]);
324 s = L_mac (s, *p8++, _sign[8]);
325 s = L_mac (s, *p9++, _sign[9]);
330 ------------------------------------------------------------------------------
332 [State any special notes, constraints or cautions for users of this function]
334 ------------------------------------------------------------------------------
337 /*----------------------------------------------------------------------------
339 ----------------------------------------------------------------------------*/
340 static void build_code(
341 Word16 codvec[], /* (i) : position of pulses */
342 Word16 sign[], /* (i) : sign of d[n] */
343 Word16 cod[], /* (o) : innovative code vector */
344 Word16 h[], /* (i) : impulse response of weighted synthesis filter*/
345 Word16 y[], /* (o) : filtered innovative code */
346 Word16 indx[], /* (o) : index of 10 pulses (sign+position) */
347 Flag *pOverflow /* i/o : overflow Flag */
350 Word16 i, k, track, index, _sign[NB_PULSE];
351 Word16 *p0, *p1, *p2, *p3, *p4, *p5, *p6, *p7, *p8, *p9;
358 OSCL_UNUSED_ARG(pOverflow);
360 oscl_memset(cod, 0, L_CODE*sizeof(*cod));
361 oscl_memset(indx, 0xFF, NB_TRACK*sizeof(*indx));
367 for (k = 0; k < NB_PULSE; k++)
369 /* read pulse position */
373 index = ((Word32)i * 6554) >> 15; /* index = pos/5 */
376 /* track = sub (i, extract_l (L_shr (L_mult (index, 5), 1))); */
377 track = i - (index + (index << 2));
388 *(p__sign++) = -8192;
389 /* index = add (index, 8); */
403 if (((index ^ temp) & 8) == 0)
405 /* sign of 1st pulse == sign of 2nd pulse */
407 /* if (sub (indx[track], index) <= 0) */
420 /* sign of 1st pulse != sign of 2nd pulse */
422 /* if (sub ((Word16)(indx[track] & 7), (Word16)(index & 7)) <= 0) */
423 if ((temp & 7) <= (index & 7))
436 p_codvec = &codvec[0];
438 p0 = h - *(p_codvec++);
439 p1 = h - *(p_codvec++);
440 p2 = h - *(p_codvec++);
441 p3 = h - *(p_codvec++);
442 p4 = h - *(p_codvec++);
443 p5 = h - *(p_codvec++);
444 p6 = h - *(p_codvec++);
445 p7 = h - *(p_codvec++);
446 p8 = h - *(p_codvec++);
447 p9 = h - *(p_codvec++);
451 for (i = L_CODE; i != 0; i--)
455 s = (*p0++ * *(p__sign++)) >> 7;
456 s += (*p1++ * *(p__sign++)) >> 7;
457 s += (*p2++ * *(p__sign++)) >> 7;
458 s += (*p3++ * *(p__sign++)) >> 7;
459 s += (*p4++ * *(p__sign++)) >> 7;
460 s += (*p5++ * *(p__sign++)) >> 7;
461 s += (*p6++ * *(p__sign++)) >> 7;
462 s += (*p7++ * *(p__sign++)) >> 7;
463 s += (*p8++ * *(p__sign++)) >> 7;
464 s += (*p9++ * *(p__sign++)) >> 7;
466 *(p_y++) = (s + 0x080) >> 8;
472 ------------------------------------------------------------------------------
473 FUNCTION NAME: code_10i40_35bits
474 ------------------------------------------------------------------------------
475 INPUT AND OUTPUT DEFINITIONS
478 pSeed = pointer to the Old CN generator shift register state (Word32)
479 n_param = Number of parameters to randomize (Word16)
480 param_size_table = table holding paameter sizes (Word16)
481 param[] = array to hold CN generated paramters (Word16)
482 pOverflow = pointer to overflow flag (Flag)
485 param[] = CN generated parameters (Word16)
486 pSeed = Updated CN generator shift register state (Word16)
487 pOverflow -> 1 if overflow occured
492 Global Variables Used:
495 Local Variables Needed:
498 ------------------------------------------------------------------------------
501 This function searches a 35 bit algebraic codebook containing 10 pulses in a
504 The code contains 10 nonzero pulses: i0...i9.
505 All pulses can have two possible amplitudes: +1 or -1.
506 The 40 positions in a subframe are divided into 5 tracks of
507 interleaved positions. Each track contains two pulses.
508 The pulses can have the following possible positions:
510 i0, i5 : 0, 5, 10, 15, 20, 25, 30, 35.
511 i1, i6 : 1, 6, 11, 16, 21, 26, 31, 36.
512 i2, i7 : 2, 7, 12, 17, 22, 27, 32, 37.
513 i3, i8 : 3, 8, 13, 18, 23, 28, 33, 38.
514 i4, i9 : 4, 9, 14, 19, 24, 29, 34, 39.
516 Each pair of pulses require 1 bit for their signs and 6 bits for their
517 positions (3 bits + 3 bits). This results in a 35 bit codebook.
518 The function determines the optimal pulse signs and positions, builds
519 the codevector, and computes the filtered codevector.
521 ------------------------------------------------------------------------------
526 ------------------------------------------------------------------------------
529 c1035pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
531 ------------------------------------------------------------------------------
533 void code_10i40_35bits (
534 Word16 x[], // (i) : target vector
535 Word16 cn[], // (i) : residual after long term prediction
536 Word16 h[], // (i) : impulse response of weighted synthesis filter
537 // h[-L_subfr..-1] must be set to zero
538 Word16 cod[], // (o) : algebraic (fixed) codebook excitation
539 Word16 y[], // (o) : filtered fixed codebook excitation
540 Word16 indx[] // (o) : index of 10 pulses (sign + position)
543 Word16 ipos[NB_PULSE], pos_max[NB_TRACK], codvec[NB_PULSE];
544 Word16 dn[L_CODE], sign[L_CODE];
545 Word16 rr[L_CODE][L_CODE], i;
547 cor_h_x (h, x, dn, 2);
548 set_sign12k2 (dn, cn, sign, pos_max, NB_TRACK, ipos, STEP);
551 search_10and8i40 (NB_PULSE, STEP, NB_TRACK,
552 dn, rr, ipos, pos_max, codvec);
554 build_code (codvec, sign, cod, h, y, indx);
555 for (i = 0; i < 10; i++)
562 ------------------------------------------------------------------------------
564 [State any special notes, constraints or cautions for users of this function]
566 ------------------------------------------------------------------------------
569 /*----------------------------------------------------------------------------
571 ----------------------------------------------------------------------------*/
572 void code_10i40_35bits(
573 Word16 x[], /* (i) : target vector */
574 Word16 cn[], /* (i) : residual after long term prediction */
575 Word16 h[], /* (i) : impulse response of weighted synthesis filter
576 h[-L_subfr..-1] must be set to zero */
577 Word16 cod[], /* (o) : algebraic (fixed) codebook excitation */
578 Word16 y[], /* (o) : filtered fixed codebook excitation */
579 Word16 indx[], /* (o) : index of 10 pulses (sign + position) */
580 const Word16* gray_ptr, /* (i) : ptr to read-only table */
581 Flag *pOverflow /* (i/o) : overflow Flag */
584 Word16 ipos[NB_PULSE], pos_max[NB_TRACK], codvec[NB_PULSE];
585 Word16 dn[L_CODE], sign[L_CODE];
586 Word16 rr[L_CODE][L_CODE], i;
588 cor_h_x(h, x, dn, 2, pOverflow);
589 set_sign12k2(dn, cn, sign, pos_max, NB_TRACK, ipos, STEP, pOverflow);
590 cor_h(h, sign, rr, pOverflow);
592 search_10and8i40(NB_PULSE, STEP, NB_TRACK,
593 dn, rr, ipos, pos_max, codvec, pOverflow);
595 build_code(codvec, sign, cod, h, y, indx, pOverflow);
596 for (i = 0; i < 10; i++)
598 q_p(&indx[i], i, gray_ptr);