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/****************************************************************************************
19Portions 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)
26Permission to distribute, modify and use this file under the standard license
27terms listed above has been obtained from the copyright holder.
28****************************************************************************************/
29/*
30------------------------------------------------------------------------------
31
32
33
34 Pathname: ./audio/gsm-amr/c/src/convolve.c
35
36     Date: 06/19/2000
37
38------------------------------------------------------------------------------
39 REVISION HISTORY
40
41 Description: Optimize for speed. Update to code template.
42
43 Description: Added author name and date, fixed tabs, and added missing
44          sections. Updated Input/Output section.
45
46 Description: Optimized code by calculating two convolution sums per iteration
47          of the outer loop, thereby, decreasing outer loop count by 2.
48          Updated input/output definitions to be the same as the assembly
49          file (convolve.asm). Left Pseudo-code section blank.
50
51 Description: Deleted semi-colon in the Pointers modified section.
52
53 Description: Synchronized file with UMTS version 3.2.0. Updated coding
54              template. Removed unnecessary include files.
55
56 Description: Made the following changes per comments from Phase 2/3 review:
57              1. Fixed typecasting issue with TI C compiler.
58              2. Modified FOR loop to count down, wherever applicable.
59
60 Description: Made the following changes
61              1. Unrolled the correlation loop.
62              2. Performed 2 correlation per pass per sample to avoid recalling
63                 the same data twice.
64              3. Eliminated math operations that check for saturation.
65
66 Description:
67              1. Modified loop counter, extra unrolling did speed up code
68
69 Description:  Replaced "int" and/or "char" with OSCL defined types.
70
71 Description: Using inlines from fxp_arithmetic.h .
72
73 Description: Replacing fxp_arithmetic.h with basic_op.h.
74
75 Description:
76
77------------------------------------------------------------------------------
78*/
79
80/*----------------------------------------------------------------------------
81; INCLUDES
82----------------------------------------------------------------------------*/
83#include "typedef.h"
84#include "convolve.h"
85#include "basic_op.h"
86
87/*----------------------------------------------------------------------------
88; MACROS
89; Define module specific macros here
90----------------------------------------------------------------------------*/
91
92
93/*----------------------------------------------------------------------------
94; DEFINES
95; Include all pre-processor statements here. Include conditional
96; compile variables also.
97----------------------------------------------------------------------------*/
98
99
100/*----------------------------------------------------------------------------
101; LOCAL FUNCTION DEFINITIONS
102; Function Prototype declaration
103----------------------------------------------------------------------------*/
104
105/*----------------------------------------------------------------------------
106; LOCAL STORE/BUFFER/POINTER DEFINITIONS
107; Variable declaration - defined here and used outside this module
108----------------------------------------------------------------------------*/
109
110
111/*
112------------------------------------------------------------------------------
113 FUNCTION NAME: Convolve
114------------------------------------------------------------------------------
115 INPUT AND OUTPUT DEFINITIONS
116
117 Inputs:
118    x = pointer to input vector of L elements of type Word16
119    h = pointer to the filter's impulse response vector of L elements
120        of type Word16
121    y = pointer to the output vector of L elements of type Word16 used for
122        storing the convolution of x and h;
123    L = Length of the convolution; type definition is Word16
124
125 Outputs:
126    y buffer contains the new convolution output
127
128 Returns:
129    None
130
131 Global Variables Used:
132    None
133
134 Local Variables Needed:
135    None
136
137------------------------------------------------------------------------------
138 FUNCTION DESCRIPTION
139
140 Perform the convolution between two vectors x[] and h[] and write the result
141 in the vector y[]. All vectors are of length L and only the first L samples
142 of the convolution are computed.
143
144 The convolution is given by:
145
146    y[n] = sum_{i=0}^{n} x[i] h[n-i],        n=0,...,L-1
147
148------------------------------------------------------------------------------
149 REQUIREMENTS
150
151 None
152
153------------------------------------------------------------------------------
154 REFERENCES
155
156 convolve.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
157
158------------------------------------------------------------------------------
159 PSEUDO-CODE
160
161void Convolve (
162    Word16 x[],        // (i)     : input vector
163    Word16 h[],        // (i)     : impulse response
164    Word16 y[],        // (o)     : output vector
165    Word16 L           // (i)     : vector size
166)
167{
168    Word16 i, n;
169    Word32 s;
170
171    for (n = 0; n < L; n++)
172    {
173        s = 0;                  move32 ();
174        for (i = 0; i <= n; i++)
175        {
176            s = L_mac (s, x[i], h[n - i]);
177        }
178        s = L_shl (s, 3);
179        y[n] = extract_h (s);   move16 ();
180    }
181
182    return;
183}
184
185------------------------------------------------------------------------------
186 RESOURCES USED [optional]
187
188 When the code is written for a specific target processor the
189 the resources used should be documented below.
190
191 HEAP MEMORY USED: x bytes
192
193 STACK MEMORY USED: x bytes
194
195 CLOCK CYCLES: (cycle count equation for this function) + (variable
196                used to represent cycle count for each subroutine
197                called)
198     where: (cycle count variable) = cycle count for [subroutine
199                                     name]
200
201------------------------------------------------------------------------------
202 CAUTION [optional]
203 [State any special notes, constraints or cautions for users of this function]
204
205------------------------------------------------------------------------------
206*/
207
208void Convolve(
209    Word16 x[],        /* (i)     : input vector                           */
210    Word16 h[],        /* (i)     : impulse response                       */
211    Word16 y[],        /* (o)     : output vector                          */
212    Word16 L           /* (i)     : vector size                            */
213)
214{
215    register Word16 i, n;
216    Word32 s1, s2;
217
218
219    for (n = 1; n < L; n = n + 2)
220    {
221
222        h = h + n;
223
224        s2 = ((Word32) * (x)) * *(h--);
225        s1 = ((Word32) * (x++)) * *(h);
226
227        for (i = (n - 1) >> 1; i != 0; i--)
228        {
229            s2 = amrnb_fxp_mac_16_by_16bb((Word32) * (x), (Word32) * (h--), s2);
230            s1 = amrnb_fxp_mac_16_by_16bb((Word32) * (x++), (Word32) * (h), s1);
231            s2 = amrnb_fxp_mac_16_by_16bb((Word32) * (x), (Word32) * (h--), s2);
232            s1 = amrnb_fxp_mac_16_by_16bb((Word32) * (x++), (Word32) * (h), s1);
233        }
234
235        s2 = amrnb_fxp_mac_16_by_16bb((Word32) * (x), (Word32) * (h), s2);
236
237        *(y++) = (Word16)(s1 >> 12);
238        *(y++) = (Word16)(s2 >> 12);
239
240        x = x - n;
241
242    }
243
244    return;
245}
246