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: pre_proc.cpp
35 Funtions: Pre_Process_init
40 ------------------------------------------------------------------------------
43 These modules handle the preprocessing of input speech.
45 ------------------------------------------------------------------------------
49 /*----------------------------------------------------------------------------
51 ----------------------------------------------------------------------------*/
56 /*----------------------------------------------------------------------------
58 ; Define module specific macros here
59 ----------------------------------------------------------------------------*/
62 /*----------------------------------------------------------------------------
64 ; Include all pre-processor statements here. Include conditional
65 ; compile variables also.
66 ----------------------------------------------------------------------------*/
69 /*----------------------------------------------------------------------------
70 ; LOCAL FUNCTION DEFINITIONS
71 ; Function Prototype declaration
72 ----------------------------------------------------------------------------*/
74 /*----------------------------------------------------------------------------
75 ; LOCAL VARIABLE DEFINITIONS
76 ; Variable declaration - defined here and used outside this module
77 ----------------------------------------------------------------------------*/
83 ------------------------------------------------------------------------------
84 FUNCTION NAME: Pre_Process_init
85 ------------------------------------------------------------------------------
86 INPUT AND OUTPUT DEFINITIONS
89 state = pointer to an array of pointer to structures of type
93 Structure pointed to by the pointer pointed to by state is
94 initialized to its reset value
95 state points to the allocated memory
98 return_value = 0 if memory was successfully initialized,
101 Global Variables Used:
104 Local Variables Needed:
107 ------------------------------------------------------------------------------
110 Allocates state memory and initializes state memory.
112 ------------------------------------------------------------------------------
117 ------------------------------------------------------------------------------
120 pre_proc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
122 ------------------------------------------------------------------------------
125 int Pre_Process_init (Pre_ProcessState **state)
129 if (state == (Pre_ProcessState **) NULL){
130 fprintf(stderr, "Pre_Process_init: invalid parameter\n");
136 if ((s= (Pre_ProcessState *) malloc(sizeof(Pre_ProcessState))) == NULL){
137 fprintf(stderr, "Pre_Process_init: can not malloc state structure\n");
141 Pre_Process_reset(s);
147 ------------------------------------------------------------------------------
149 [State any special notes, constraints or cautions for users of this function]
151 ------------------------------------------------------------------------------
154 Word16 Pre_Process_init(Pre_ProcessState **state)
158 if (state == (Pre_ProcessState **) NULL)
160 /* fprintf(stderr, "Pre_Process_init: invalid parameter\n"); */
165 /* allocate memory */
166 if ((s = (Pre_ProcessState *) oscl_malloc(sizeof(Pre_ProcessState))) == NULL)
168 /* fprintf(stderr, "Pre_Process_init:
169 can not malloc state structure\n"); */
173 Pre_Process_reset(s);
179 /****************************************************************************/
183 ------------------------------------------------------------------------------
184 FUNCTION NAME: Pre_Process_reset
185 ------------------------------------------------------------------------------
186 INPUT AND OUTPUT DEFINITIONS
189 state = pointer to structure of type Pre_ProcessState
192 Structure pointed to by state is initialized to zero.
195 return_value = 0 if memory was successfully reset,
196 otherwise returns -1.
198 Global Variables Used:
201 Local Variables Needed:
204 ------------------------------------------------------------------------------
207 Initializes state memory to zero.
209 ------------------------------------------------------------------------------
214 ------------------------------------------------------------------------------
217 pre_proc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
219 ------------------------------------------------------------------------------
222 int Pre_Process_reset (Pre_ProcessState *state)
224 if (state == (Pre_ProcessState *) NULL){
225 fprintf(stderr, "Pre_Process_reset: invalid parameter\n");
239 ------------------------------------------------------------------------------
241 [State any special notes, constraints or cautions for users of this function]
243 ------------------------------------------------------------------------------
246 Word16 Pre_Process_reset(Pre_ProcessState *state)
248 if (state == (Pre_ProcessState *) NULL)
250 /* fprintf(stderr, "Pre_Process_reset: invalid parameter\n"); */
264 /****************************************************************************/
268 ------------------------------------------------------------------------------
269 FUNCTION NAME: Pre_Process_exit
270 ------------------------------------------------------------------------------
271 INPUT AND OUTPUT DEFINITIONS
274 state = a pointer to an array of pointers to structures of
275 type Pre_ProcessState
278 state points to a NULL address
283 Global Variables Used:
286 Local Variables Needed:
289 ------------------------------------------------------------------------------
292 The memory used for state memory is freed.
294 ------------------------------------------------------------------------------
299 ------------------------------------------------------------------------------
302 pre_proc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
304 ------------------------------------------------------------------------------
307 void Pre_Process_exit (Pre_ProcessState **state)
309 if (state == NULL || *state == NULL)
319 ------------------------------------------------------------------------------
321 [State any special notes, constraints or cautions for users of this function]
323 ------------------------------------------------------------------------------
326 void Pre_Process_exit(Pre_ProcessState **state)
328 if (state == NULL || *state == NULL)
333 /* deallocate memory */
340 /****************************************************************************/
343 ------------------------------------------------------------------------------
344 FUNCTION NAME: Pre_Process
345 ------------------------------------------------------------------------------
346 INPUT AND OUTPUT DEFINITIONS
349 st = a pointer to a structure of type Pre_ProcessState
350 signal = input/output signal (Word16)
351 lg = length of signal (Word16)
354 st points to the updated structure
357 return_value = 0 (int)
359 Global Variables Used:
360 a = points to a buffer of filter coefficients
361 b = points to a buffer of filter coefficients
363 Local Variables Needed:
366 ------------------------------------------------------------------------------
369 This module performs the preprocessing of the input speech.
370 The signal is passed through a 2nd order high pass filtering with cut off
371 frequency at 80 Hz. The input is divided by two in the filtering process.
373 y[i] = b[0]*x[i]/2 + b[1]*x[i-1]/2 + b[2]*x[i-2]/2
374 + a[1]*y[i-1] + a[2]*y[i-2];
376 ------------------------------------------------------------------------------
381 ------------------------------------------------------------------------------
384 pre_proc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
386 ------------------------------------------------------------------------------
390 Pre_ProcessState *st,
391 Word16 signal[], // input/output signal
392 Word16 lg) // lenght of signal
397 for (i = 0; i < lg; i++)
403 // y[i] = b[0]*x[i]/2 + b[1]*x[i-1]/2 + b140[2]*x[i-2]/2
404 // + a[1]*y[i-1] + a[2] * y[i-2];
406 L_tmp = Mpy_32_16 (st->y1_hi, st->y1_lo, a[1]);
407 L_tmp = L_add (L_tmp, Mpy_32_16 (st->y2_hi, st->y2_lo, a[2]));
408 L_tmp = L_mac (L_tmp, st->x0, b[0]);
409 L_tmp = L_mac (L_tmp, st->x1, b[1]);
410 L_tmp = L_mac (L_tmp, x2, b[2]);
411 L_tmp = L_shl (L_tmp, 3);
412 signal[i] = pv_round (L_tmp);
414 st->y2_hi = st->y1_hi;
415 st->y2_lo = st->y1_lo;
416 L_Extract (L_tmp, &st->y1_hi, &st->y1_lo);
421 ------------------------------------------------------------------------------
423 [State any special notes, constraints or cautions for users of this function]
425 ------------------------------------------------------------------------------
428 filter coefficients (fc = 80 Hz, coeff. b[] is divided by 2)
429 const Word16 b[3] = {1899, -3798, 1899};
430 const Word16 a[3] = {4096, 7807, -3733};
435 Pre_ProcessState *st,
436 Word16 signal[], /* input/output signal */
437 Word16 lg) /* length of signal */
443 Word16 *p_signal = signal;
448 for (i = lg; i != 0; i--)
452 /* y[i] = b[0]*x[i]/2 + b[1]*x[i-1]/2 + b140[2]*x[i-2]/2 */
453 /* + a[1]*y[i-1] + a[2] * y[i-2]; */
455 L_tmp = ((Word32) st->y1_hi) * 7807;
456 L_tmp += (Word32)(((Word32) st->y1_lo * 7807) >> 15);
458 L_tmp += ((Word32) st->y2_hi) * (-3733);
459 st->y2_hi = st->y1_hi;
460 L_tmp += (Word32)(((Word32) st->y2_lo * (-3733)) >> 15);
461 st->y2_lo = st->y1_lo;
463 L_tmp += ((Word32) x_n_2) * 1899;
465 L_tmp += ((Word32) x_n_1) * (-3798);
467 L_tmp += ((Word32) x_n_1) * 1899;
470 *(p_signal++) = (Word16)((L_tmp + 0x0000800L) >> 12);
472 st->y1_hi = (Word16)(L_tmp >> 12);
473 st->y1_lo = (Word16)((L_tmp << 3) - ((Word32)(st->y1_hi) << 15));