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 ------------------------------------------------------------------------------
34 Filename: set_sign.cpp
38 ------------------------------------------------------------------------------
41 This module contains the functions set_sign and set_sign12k2.
42 These functions are used to build a sign vector according
43 to the values in the input arrays. These functions also
44 find the position in the input codes of the maximum correlation
45 and the starting position for each pulse.
47 ------------------------------------------------------------------------------
51 /*----------------------------------------------------------------------------
53 ----------------------------------------------------------------------------*/
60 /*----------------------------------------------------------------------------
62 ; Define module specific macros here
63 ----------------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------------
68 ; Include all pre-processor statements here. Include conditional
69 ; compile variables also.
70 ----------------------------------------------------------------------------*/
72 /*----------------------------------------------------------------------------
73 ; LOCAL FUNCTION DEFINITIONS
74 ; Function Prototype declaration
75 ----------------------------------------------------------------------------*/
77 /*----------------------------------------------------------------------------
78 ; LOCAL VARIABLE DEFINITIONS
79 ; Variable declaration - defined here and used outside this module
80 ----------------------------------------------------------------------------*/
84 ------------------------------------------------------------------------------
85 FUNCTION NAME: set_sign
86 ------------------------------------------------------------------------------
87 INPUT AND OUTPUT DEFINITIONS
90 dn = buffer of correlation values (Word16)
91 sign = buffer containing sign of dn elements (Word16)
92 dn2 = buffer containing the maximum of correlation in each track.(Word16)
93 n = number of maximum correlations in dn2 (Word16)
99 dn buffer is modified to contain the absolute value of its input
100 sign buffer is modified to contain the sign information for the
102 dn2 buffer is modified to denote the location of the maximum
103 correlation for each track.
105 Global Variables Used:
108 Local Variables Needed:
111 ------------------------------------------------------------------------------
115 This function builds sign vector according to dn buffer It also finds
116 the position of maximum of correlation in each track and the starting
117 position for each pulse.
119 ------------------------------------------------------------------------------
124 ------------------------------------------------------------------------------
127 set_sign.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
129 ------------------------------------------------------------------------------
132 void set_sign(Word16 dn[], i/o : correlation between target and h[]
133 Word16 sign[], o : sign of dn[]
134 Word16 dn2[], o : maximum of correlation in each track.
135 Word16 n i : # of maximum correlations in dn2[]
140 Word16 pos = 0; //initialization only needed to keep gcc silent
142 // set sign according to dn[]
144 for (i = 0; i < L_CODE; i++) {
153 dn[i] = val; // modify dn[] according to the fixed sign
157 // keep 8-n maximum positions/8 of each track and store it in dn2[]
159 for (i = 0; i < NB_TRACK; i++)
161 for (k = 0; k < (8-n); k++)
164 for (j = i; j < L_CODE; j += STEP)
168 val = sub(dn2[j], min);
184 ------------------------------------------------------------------------------
186 [State any special notes, constraints or cautions for users of this function]
188 ------------------------------------------------------------------------------
191 void set_sign(Word16 dn[], /* i/o : correlation between target and h[] */
192 Word16 sign[], /* o : sign of dn[] */
193 Word16 dn2[], /* o : maximum of correlation in each track. */
194 Word16 n /* i : # of maximum correlations in dn2[] */
197 register Word16 i, j, k;
199 Word16 pos = 0; /* initialization only needed to keep gcc silent */
201 /* set sign according to dn[] */
202 for (i = L_CODE - 1; i >= 0; i--)
214 dn[i] = val; /* modify dn[] according to the fixed sign */
220 /* keep 8-n maximum positions/8 of each track and store it in dn2[] */
222 for (i = 0; i < NB_TRACK; i++)
224 for (k = 0; k < (8 - n); k++)
227 for (j = i; j < L_CODE; j += STEP)
245 /****************************************************************************/
249 ------------------------------------------------------------------------------
250 FUNCTION NAME: set_sign12k2()
251 ------------------------------------------------------------------------------
252 INPUT AND OUTPUT DEFINITIONS
255 dn = buffer of correlation values (Word16)
256 cn = buffer of residual after long term prediction (Word16)
257 sign = sign of correlation buffer elements (Word16)
258 pos_max = buffer containing position of maximum correlation (Word16)
259 nb_track = number of tracks (Word16)
260 ipos = buffer containing the starting position for each pulse (Word16)
261 step = step size in the tracks (Word16)
262 pOverflow = pointer to Overflow flag (Flag)
265 sign buffer contains the sign of correlation values
266 dn buffer contains the sign-adjusted correlation values
267 pos_max buffer contains the maximum correlation position
268 ipos buffer contains the starting position of each pulse
269 pOverflow -> 1 if the math operations called by this function result in
276 Global Variables Used:
279 Local Variables Needed:
282 ------------------------------------------------------------------------------
285 This function builds the sign vector according to dn and cn, and modifies
286 dn to include the sign information (dn[i]=sign[i]*dn[i]). It also finds
287 the position of maximum of correlation in each track and the starting
288 position for each pulse.
290 ------------------------------------------------------------------------------
295 ------------------------------------------------------------------------------
298 set_sign.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
300 ------------------------------------------------------------------------------
304 Word16 dn[], //i/o : correlation between target and h[]
305 Word16 cn[], //i : residual after long term prediction
306 Word16 sign[], //o : sign of d[n]
307 Word16 pos_max[], //o : position of maximum correlation
308 Word16 nb_track, //i : number of tracks tracks
309 Word16 ipos[], //o : starting position for each pulse
310 Word16 step //i : the step size in the tracks
314 Word16 val, cor, k_cn, k_dn, max, max_of_all;
315 Word16 pos = 0; // initialization only needed to keep gcc silent
316 Word16 en[L_CODE]; // correlation vector
319 // The reference ETSI code uses a global flag for Overflow. However in the
320 // actual implementation a pointer to the overflow flag is passed in. This
321 // pointer is passed into the basic math functions called by this routine.
323 // calculate energy for normalization of cn[] and dn[]
326 for (i = 0; i < L_CODE; i++)
328 s = L_mac (s, cn[i], cn[i]);
331 k_cn = extract_h (L_shl (s, 5));
334 for (i = 0; i < L_CODE; i++)
336 s = L_mac (s, dn[i], dn[i]);
339 k_dn = extract_h (L_shl (s, 5));
341 for (i = 0; i < L_CODE; i++)
344 cor = pv_round (L_shl (L_mac (L_mult (k_cn, cn[i]), k_dn, val), 10));
348 sign[i] = 32767; // sign = +1
352 sign[i] = -32767; // sign = -1
356 // modify dn[] according to the fixed sign
362 for (i = 0; i < nb_track; i++)
366 for (j = i; j < L_CODE; j += step)
369 val = sub (cor, max);
377 // store maximum correlation position
379 val = sub (max, max_of_all);
384 // starting position for i0
390 // Set starting position of each pulse.
394 ipos[nb_track] = pos;
396 for (i = 1; i < nb_track; i++)
400 if (sub (pos, nb_track) >= 0)
405 ipos[add(i, nb_track)] = pos;
409 ------------------------------------------------------------------------------
411 [State any special notes, constraints or cautions for users of this function]
413 ------------------------------------------------------------------------------
417 Word16 dn[], /* i/o : correlation between target and h[] */
418 Word16 cn[], /* i : residual after long term prediction */
419 Word16 sign[], /* o : sign of d[n] */
420 Word16 pos_max[], /* o : position of maximum correlation */
421 Word16 nb_track, /* i : number of tracks tracks */
422 Word16 ipos[], /* o : starting position for each pulse */
423 Word16 step, /* i : the step size in the tracks */
424 Flag *pOverflow /* i/o: overflow flag */
434 Word16 pos = 0; /* initialization only needed to keep gcc silent */
435 Word16 en[L_CODE]; /* correlation vector */
444 /* calculate energy for normalization of cn[] and dn[] */
449 p_dn = dn; /* crosscorrelation values do not have strong peaks, so
450 scaling applied in cor_h_x (sf=2) guaranteed that the
451 mac of the energy for this vector will not overflow */
453 for (i = L_CODE; i != 0; i--)
456 s = L_mac(s, val, val, pOverflow);
458 t += ((Word32) val * val) << 1;
460 s = Inv_sqrt(s, pOverflow);
461 k_cn = (Word16)((L_shl(s, 5, pOverflow)) >> 16);
463 t = Inv_sqrt(t, pOverflow);
464 k_dn = (Word16)(t >> 11);
466 p_cn = &cn[L_CODE-1];
467 p_sign = &sign[L_CODE-1];
468 p_en = &en[L_CODE-1];
470 for (i = L_CODE - 1; i >= 0; i--)
472 L_temp = ((Word32)k_cn * *(p_cn--)) << 1;
474 s = L_mac(L_temp, k_dn, val, pOverflow);
475 L_temp = L_shl(s, 10, pOverflow);
476 cor = pv_round(L_temp, pOverflow);
480 *(p_sign--) = 32767; /* sign = +1 */
484 *(p_sign--) = -32767; /* sign = -1 */
487 /* modify dn[] according to the fixed sign */
495 for (i = 0; i < nb_track; i++)
499 for (j = i; j < L_CODE; j += step)
508 /* store maximum correlation position */
510 if (max > max_of_all)
513 /* starting position for i0 */
518 /*----------------------------------------------------------------*
519 * Set starting position of each pulse. *
520 *----------------------------------------------------------------*/
523 ipos[nb_track] = pos;
525 for (i = 1; i < nb_track; i++)
534 ipos[ i + nb_track] = pos;