Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / convolve.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
32
33
34  Filename: convolve.cpp
35
36 ------------------------------------------------------------------------------
37 */
38
39 /*----------------------------------------------------------------------------
40 ; INCLUDES
41 ----------------------------------------------------------------------------*/
42 #include "typedef.h"
43 #include "convolve.h"
44 #include "basic_op.h"
45
46 /*----------------------------------------------------------------------------
47 ; MACROS
48 ; Define module specific macros here
49 ----------------------------------------------------------------------------*/
50
51
52 /*----------------------------------------------------------------------------
53 ; DEFINES
54 ; Include all pre-processor statements here. Include conditional
55 ; compile variables also.
56 ----------------------------------------------------------------------------*/
57
58
59 /*----------------------------------------------------------------------------
60 ; LOCAL FUNCTION DEFINITIONS
61 ; Function Prototype declaration
62 ----------------------------------------------------------------------------*/
63
64 /*----------------------------------------------------------------------------
65 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
66 ; Variable declaration - defined here and used outside this module
67 ----------------------------------------------------------------------------*/
68
69
70 /*
71 ------------------------------------------------------------------------------
72  FUNCTION NAME: Convolve
73 ------------------------------------------------------------------------------
74  INPUT AND OUTPUT DEFINITIONS
75
76  Inputs:
77     x = pointer to input vector of L elements of type Word16
78     h = pointer to the filter's impulse response vector of L elements
79         of type Word16
80     y = pointer to the output vector of L elements of type Word16 used for
81         storing the convolution of x and h;
82     L = Length of the convolution; type definition is Word16
83
84  Outputs:
85     y buffer contains the new convolution output
86
87  Returns:
88     None
89
90  Global Variables Used:
91     None
92
93  Local Variables Needed:
94     None
95
96 ------------------------------------------------------------------------------
97  FUNCTION DESCRIPTION
98
99  Perform the convolution between two vectors x[] and h[] and write the result
100  in the vector y[]. All vectors are of length L and only the first L samples
101  of the convolution are computed.
102
103  The convolution is given by:
104
105     y[n] = sum_{i=0}^{n} x[i] h[n-i],        n=0,...,L-1
106
107 ------------------------------------------------------------------------------
108  REQUIREMENTS
109
110  None
111
112 ------------------------------------------------------------------------------
113  REFERENCES
114
115  convolve.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
116
117 ------------------------------------------------------------------------------
118  PSEUDO-CODE
119
120 void Convolve (
121     Word16 x[],        // (i)     : input vector
122     Word16 h[],        // (i)     : impulse response
123     Word16 y[],        // (o)     : output vector
124     Word16 L           // (i)     : vector size
125 )
126 {
127     Word16 i, n;
128     Word32 s;
129
130     for (n = 0; n < L; n++)
131     {
132         s = 0;                  move32 ();
133         for (i = 0; i <= n; i++)
134         {
135             s = L_mac (s, x[i], h[n - i]);
136         }
137         s = L_shl (s, 3);
138         y[n] = extract_h (s);   move16 ();
139     }
140
141     return;
142 }
143
144 ------------------------------------------------------------------------------
145  CAUTION [optional]
146  [State any special notes, constraints or cautions for users of this function]
147
148 ------------------------------------------------------------------------------
149 */
150
151 void Convolve(
152     Word16 x[],        /* (i)     : input vector                           */
153     Word16 h[],        /* (i)     : impulse response                       */
154     Word16 y[],        /* (o)     : output vector                          */
155     Word16 L           /* (i)     : vector size                            */
156 )
157 {
158     register Word16 i, n;
159     Word32 s1, s2;
160
161
162     for (n = 1; n < L; n = n + 2)
163     {
164
165         h = h + n;
166
167         s2 = ((Word32) * (x)) * *(h--);
168         s1 = ((Word32) * (x++)) * *(h);
169
170         for (i = (n - 1) >> 1; i != 0; i--)
171         {
172             s2 = amrnb_fxp_mac_16_by_16bb((Word32) * (x), (Word32) * (h--), s2);
173             s1 = amrnb_fxp_mac_16_by_16bb((Word32) * (x++), (Word32) * (h), s1);
174             s2 = amrnb_fxp_mac_16_by_16bb((Word32) * (x), (Word32) * (h--), s2);
175             s1 = amrnb_fxp_mac_16_by_16bb((Word32) * (x++), (Word32) * (h), s1);
176         }
177
178         s2 = amrnb_fxp_mac_16_by_16bb((Word32) * (x), (Word32) * (h), s2);
179
180         *(y++) = (Word16)(s1 >> 12);
181         *(y++) = (Word16)(s2 >> 12);
182
183         x = x - n;
184
185     }
186
187     return;
188 }