Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / src / residu.cpp
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
20
21     3GPP TS 26.073
22     ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23     Available from http://www.3gpp.org
24
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 ****************************************************************************************/
29 /*
30
31  Filename: residu.cpp
32
33 ------------------------------------------------------------------------------
34 */
35
36 /*----------------------------------------------------------------------------
37 ; INCLUDES
38 ----------------------------------------------------------------------------*/
39 #include "residu.h"
40 #include "typedef.h"
41 #include "cnst.h"
42
43 /*----------------------------------------------------------------------------
44 ; MACROS
45 ; Define module specific macros here
46 ----------------------------------------------------------------------------*/
47
48 /*----------------------------------------------------------------------------
49 ; DEFINES
50 ; Include all pre-processor statements here. Include conditional
51 ; compile variables also.
52 ----------------------------------------------------------------------------*/
53
54 /*----------------------------------------------------------------------------
55 ; LOCAL FUNCTION DEFINITIONS
56 ; Function Prototype declaration
57 ----------------------------------------------------------------------------*/
58
59 /*----------------------------------------------------------------------------
60 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
61 ; Variable declaration - defined here and used outside this module
62 ----------------------------------------------------------------------------*/
63
64
65 /*
66 ------------------------------------------------------------------------------
67  FUNCTION NAME: Residu
68 ------------------------------------------------------------------------------
69  INPUT AND OUTPUT DEFINITIONS
70
71  Inputs:
72     coef_ptr = pointer to buffer containing the prediction coefficients
73     input_ptr = pointer to buffer containing the speech signal
74     input_len = filter order
75     residual_ptr = pointer to buffer of residual signal
76
77  Outputs:
78     residual_ptr buffer contains the newly calculated the residual signal
79
80  Returns:
81     None
82
83  Global Variables Used:
84     None
85
86  Local Variables Needed:
87     None
88
89 ------------------------------------------------------------------------------
90  FUNCTION DESCRIPTION
91
92  This function computes the LP residual by filtering the input speech through
93  the LP inverse filter A(z).
94
95 ------------------------------------------------------------------------------
96  REQUIREMENTS
97
98  None
99
100 ------------------------------------------------------------------------------
101  REFERENCES
102
103  residu.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
104
105 ------------------------------------------------------------------------------
106  PSEUDO-CODE
107
108  Note: Input argument names were changed to be more descriptive. Shown below
109        are the original names. Shown below are the name changes:
110            a[]  <-->  coef_ptr[]
111            x[]  <-->  input_ptr[]
112            y[]  <-->  residual_ptr[]
113            lg   <-->  input_len
114
115
116 void Residu (
117     Word16 a[], // (i)     : prediction coefficients
118     Word16 x[], // (i)     : speech signal
119     Word16 y[], // (o)     : residual signal
120     Word16 lg   // (i)     : size of filtering
121 )
122 {
123     Word16 i, j;
124     Word32 s;
125
126     for (i = 0; i < lg; i++)
127     {
128         s = L_mult (x[i], a[0]);
129         for (j = 1; j <= M; j++)
130         {
131             s = L_mac (s, a[j], x[i - j]);
132         }
133         s = L_shl (s, 3);
134         y[i] = pv_round (s);
135     }
136     return;
137 }
138
139 ------------------------------------------------------------------------------
140  CAUTION [optional]
141  [State any special notes, constraints or cautions for users of this function]
142
143 ------------------------------------------------------------------------------
144 */
145
146 OSCL_EXPORT_REF void Residu(
147     Word16 coef_ptr[],      /* (i)     : prediction coefficients*/
148     Word16 input_ptr[],     /* (i)     : speech signal          */
149     Word16 residual_ptr[],  /* (o)     : residual signal        */
150     Word16 input_len        /* (i)     : size of filtering      */
151 )
152 {
153
154
155     register Word16 i, j;
156     Word32 s1;
157     Word32 s2;
158     Word32 s3;
159     Word32 s4;
160     Word16 *p_input1;
161     Word16 *p_input2;
162     Word16 *p_input3;
163     Word16 *p_input4;
164     Word16 *p_coef;
165     Word16 *p_residual_ptr = &residual_ptr[input_len-1];
166     Word16 *p_input_ptr    = &input_ptr[input_len-1-M];
167
168     for (i = input_len >> 2; i != 0; i--)
169     {
170         s1       = 0x0000800L;
171         s2       = 0x0000800L;
172         s3       = 0x0000800L;
173         s4       = 0x0000800L;
174         p_coef  = &coef_ptr[M];
175         p_input1 = p_input_ptr--;
176         p_input2 = p_input_ptr--;
177         p_input3 = p_input_ptr--;
178         p_input4 = p_input_ptr--;
179
180         for (j = M >> 1; j != 0; j--)
181         {
182             s1 += ((Word32) * (p_coef) * *(p_input1++));
183             s2 += ((Word32) * (p_coef) * *(p_input2++));
184             s3 += ((Word32) * (p_coef) * *(p_input3++));
185             s4 += ((Word32) * (p_coef--) * *(p_input4++));
186             s1 += ((Word32) * (p_coef) * *(p_input1++));
187             s2 += ((Word32) * (p_coef) * *(p_input2++));
188             s3 += ((Word32) * (p_coef) * *(p_input3++));
189             s4 += ((Word32) * (p_coef--) * *(p_input4++));
190         }
191
192         s1 += (((Word32) * (p_coef)) * *(p_input1));
193         s2 += (((Word32) * (p_coef)) * *(p_input2));
194         s3 += (((Word32) * (p_coef)) * *(p_input3));
195         s4 += (((Word32) * (p_coef)) * *(p_input4));
196
197         *(p_residual_ptr--) = (Word16)(s1 >> 12);
198         *(p_residual_ptr--) = (Word16)(s2 >> 12);
199         *(p_residual_ptr--) = (Word16)(s3 >> 12);
200         *(p_residual_ptr--) = (Word16)(s4 >> 12);
201
202     }
203
204     return;
205 }