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: b_cn_cod.cpp
35 Functions: pseudonoise
39 ------------------------------------------------------------------------------
42 This module contains functions for comfort noise(CN) generation.
44 ------------------------------------------------------------------------------
48 /*----------------------------------------------------------------------------
50 ----------------------------------------------------------------------------*/
55 /*----------------------------------------------------------------------------
57 ; [Define module specific macros here]
58 ----------------------------------------------------------------------------*/
61 /*----------------------------------------------------------------------------
63 ; [Include all pre-processor statements here. Include conditional
64 ; compile variables also.]
65 ----------------------------------------------------------------------------*/
66 #define NB_PULSE 10 /* number of random pulses in DTX operation */
68 /*----------------------------------------------------------------------------
69 ; LOCAL FUNCTION DEFINITIONS
70 ; [List function prototypes here]
71 ----------------------------------------------------------------------------*/
73 /*----------------------------------------------------------------------------
74 ; LOCAL VARIABLE DEFINITIONS
75 ; [Variable declaration - defined here and used outside this module]
76 ----------------------------------------------------------------------------*/
79 ------------------------------------------------------------------------------
80 FUNCTION NAME: pseudonoise
81 ------------------------------------------------------------------------------
82 INPUT AND OUTPUT DEFINITIONS
85 pShift_reg = pointer to Old CN generator shift register state (Word32)
86 no_bits = Number of bits (Word16)
89 pShift_reg -> Updated CN generator shift register state
92 noise_bits = Generated random integer value (Word16)
94 Global Variables Used:
97 Local Variables Needed:
100 ------------------------------------------------------------------------------
103 Generate a random integer value to use in comfort noise generation. The
104 algorithm uses polynomial x^31 + x^3 + 1. Length of the PN sequence
107 ------------------------------------------------------------------------------
112 ------------------------------------------------------------------------------
115 b_cn_cod.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
117 ------------------------------------------------------------------------------
121 Word32 *shift_reg, // i/o : Old CN generator shift register state
122 Word16 no_bits // i : Number of bits
125 Word16 noise_bits, Sn, i;
128 for (i = 0; i < no_bits; i++)
131 if ((*shift_reg & 0x00000001L) != 0)
141 if ((*shift_reg & 0x10000000L) != 0)
150 noise_bits = shl (noise_bits, 1);
151 noise_bits = noise_bits | (extract_l (*shift_reg) & 1);
153 *shift_reg = L_shr (*shift_reg, 1);
156 *shift_reg = *shift_reg | 0x40000000L;
162 ------------------------------------------------------------------------------
164 [State any special notes, constraints or cautions for users of this function]
166 ------------------------------------------------------------------------------
169 /*----------------------------------------------------------------------------
171 ----------------------------------------------------------------------------*/
174 Word32 *pShift_reg, /* i/o : Old CN generator shift register state */
175 Word16 no_bits /* i : Number of bits */
185 for (i = 0; i < no_bits; i++)
188 if ((*pShift_reg & 0x00000001L) != 0)
198 if ((*pShift_reg & 0x10000000L) != 0)
209 temp = (Word16)((*pShift_reg) & 1);
215 *pShift_reg |= 0x40000000L;
222 ------------------------------------------------------------------------------
223 FUNCTION NAME: build_CN_code
224 ------------------------------------------------------------------------------
225 INPUT AND OUTPUT DEFINITIONS
228 pSeed = pointer to the Old CN generator shift register state (Word32)
229 cod[] = array to hold the generated CN fixed code vector (Word16)
230 pOverflow = pointer to overflow flag (Flag)
233 cod[] = generated CN fixed code vector (Word16)
234 pSeed = Updated CN generator shift register state (Word16)
235 pOverflow -> 1 if overflow occured
240 Global Variables Used:
243 Local Variables Needed:
246 ------------------------------------------------------------------------------
249 This function computes the comfort noise fixed codebook excitation. The gains
250 of the pulses are always +/-1.
252 ------------------------------------------------------------------------------
257 ------------------------------------------------------------------------------
260 b_cn_cod.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
262 ------------------------------------------------------------------------------
266 Word32 *seed, // i/o : Old CN generator shift register state
267 Word16 cod[] // o : Generated CN fixed codebook vector
272 for (i = 0; i < L_SUBFR; i++)
277 // The reference ETSI code uses a global flag for Overflow. However in the
278 // actual implementation a pointer to the overflow flag is passed into the
279 // function so that it can be passed into the basic math functions L_mult()
282 for (k = 0; k < NB_PULSE; k++)
284 i = pseudonoise (seed, 2); // generate pulse position
285 i = shr (extract_l (L_mult (i, 10)), 1);
288 j = pseudonoise (seed, 1); // generate sign
302 ------------------------------------------------------------------------------
304 [State any special notes, constraints or cautions for users of this function]
306 ------------------------------------------------------------------------------
309 /*----------------------------------------------------------------------------
311 ----------------------------------------------------------------------------*/
313 Word32 *pSeed, /* i/o : Old CN generator shift register state */
314 Word16 cod[], /* o : Generated CN fixed codebook vector */
315 Flag *pOverflow /* i/o : Overflow flag */
321 for (i = 0; i < L_SUBFR; i++)
326 for (k = 0; k < NB_PULSE; k++)
328 i = pseudonoise(pSeed, 2); /* generate pulse position */
330 temp = (Word16)(L_mult(i, 10, pOverflow));
332 i = add_16(i, k, pOverflow);
334 j = pseudonoise(pSeed, 1); /* generate sign */
350 ------------------------------------------------------------------------------
351 FUNCTION NAME: build_CN_param
352 ------------------------------------------------------------------------------
353 INPUT AND OUTPUT DEFINITIONS
356 pSeed = pointer to the Old CN generator shift register state (Word32)
357 n_param = Number of parameters to randomize (Word16)
358 param_size_table = table holding paameter sizes (Word16)
359 param[] = array to hold CN generated paramters (Word16)
360 pOverflow = pointer to overflow flag (Flag)
363 param[] = CN generated parameters (Word16)
364 pSeed = Updated CN generator shift register state (Word16)
365 pOverflow -> 1 if overflow occured
370 Global Variables Used:
373 Local Variables Needed:
376 ------------------------------------------------------------------------------
379 This function randomizes the speech parameters, so that they do not produce
380 tonal artifacts if used by ECU.
382 ------------------------------------------------------------------------------
387 ------------------------------------------------------------------------------
390 b_cn_cod.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
392 ------------------------------------------------------------------------------
394 void build_CN_param (
395 Word16 *seed, // i/o : Old CN generator shift register state
396 const Word16 n_param, // i : number of params
397 const Word16 param_size_table[],// i : size of params
398 Word16 parm[] // o : CN Generated params
404 // The reference ETSI code uses a global flag for Overflow. However in the
405 // actual implementation a pointer to the overflow flag is passed into the
406 // function so that it can be passed into the basic math functions L_add()
409 *seed = extract_l(L_add(L_shr(L_mult(*seed, 31821), 1), 13849L));
411 p = &window_200_40[*seed & 0x7F];
412 for(i=0; i< n_param;i++){
413 parm[i] = *p++ & ~(0xFFFF<<param_size_table[i]);
418 ------------------------------------------------------------------------------
420 [State any special notes, constraints or cautions for users of this function]
422 ------------------------------------------------------------------------------
425 /*----------------------------------------------------------------------------
427 ----------------------------------------------------------------------------*/
429 Word16 *pSeed, /* i/o : Old CN generator shift register state */
430 const Word16 n_param, /* i : number of params */
431 const Word16 param_size_table[],/* i : size of params */
432 Word16 parm[], /* o : CN Generated params */
433 const Word16* window_200_40_ptr, /* i : ptr to read-only table */
434 Flag *pOverflow /* i/o : Overflow Flag */
443 L_temp = L_mult(*pSeed, 31821, pOverflow);
446 *pSeed = (Word16)(L_add(L_temp, 13849L, pOverflow));
448 pTemp = &window_200_40_ptr[*pSeed & 0x7F];
450 for (i = 0; i < n_param; i++)
452 temp = ~(0xFFFF << param_size_table[i]);
453 parm[i] = *pTemp++ & temp;