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 Pathname: ./audio/gsm-amr/c/src/residu.c
32
33------------------------------------------------------------------------------
34 REVISION HISTORY
35
36 Description: Updated template used to PV coding template. First attempt at
37          optimizing code.
38
39 Description: Deleted stores listed in the Local Stores Needed/Modified
40          section.
41
42 Description: Updated file per comments gathered from Phase 2/3 review.
43
44 Description: Updating to reflect variable name changes made in residu.asm
45
46 Description: Synchronized file with UMTS version 3.2.0. Updated coding
47              template. Removed unnecessary include files.
48
49 Description: Made the following changes per comments from Phase 2/3 review:
50              1. Modified FOR loops to count down.
51              2. Fixed typecasting issue with TI C compiler.
52
53 Description: Made the following changes
54              1. Unrolled the convolutional loop.
55              2. Performed 4 convolution per pass to avoid recalling the same
56                 filter coefficient as many times.
57              2. Eliminated math operations that check for saturation.
58
59 Description:  Replaced "int" and/or "char" with OSCL defined types.
60
61 Description: Changed round function name to pv_round to avoid conflict with
62              round function in C standard library.
63
64 Who:                           Date:
65 Description:
66
67------------------------------------------------------------------------------
68*/
69
70/*----------------------------------------------------------------------------
71; INCLUDES
72----------------------------------------------------------------------------*/
73#include "residu.h"
74#include "typedef.h"
75#include "cnst.h"
76
77/*----------------------------------------------------------------------------
78; MACROS
79; Define module specific macros here
80----------------------------------------------------------------------------*/
81
82/*----------------------------------------------------------------------------
83; DEFINES
84; Include all pre-processor statements here. Include conditional
85; compile variables also.
86----------------------------------------------------------------------------*/
87
88/*----------------------------------------------------------------------------
89; LOCAL FUNCTION DEFINITIONS
90; Function Prototype declaration
91----------------------------------------------------------------------------*/
92
93/*----------------------------------------------------------------------------
94; LOCAL STORE/BUFFER/POINTER DEFINITIONS
95; Variable declaration - defined here and used outside this module
96----------------------------------------------------------------------------*/
97
98
99/*
100------------------------------------------------------------------------------
101 FUNCTION NAME: Residu
102------------------------------------------------------------------------------
103 INPUT AND OUTPUT DEFINITIONS
104
105 Inputs:
106    coef_ptr = pointer to buffer containing the prediction coefficients
107    input_ptr = pointer to buffer containing the speech signal
108    input_len = filter order
109    residual_ptr = pointer to buffer of residual signal
110
111 Outputs:
112    residual_ptr buffer contains the newly calculated the residual signal
113
114 Returns:
115    None
116
117 Global Variables Used:
118    None
119
120 Local Variables Needed:
121    None
122
123------------------------------------------------------------------------------
124 FUNCTION DESCRIPTION
125
126 This function computes the LP residual by filtering the input speech through
127 the LP inverse filter A(z).
128
129------------------------------------------------------------------------------
130 REQUIREMENTS
131
132 None
133
134------------------------------------------------------------------------------
135 REFERENCES
136
137 residu.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
138
139------------------------------------------------------------------------------
140 PSEUDO-CODE
141
142 Note: Input argument names were changed to be more descriptive. Shown below
143       are the original names. Shown below are the name changes:
144           a[]  <-->  coef_ptr[]
145           x[]  <-->  input_ptr[]
146           y[]  <-->  residual_ptr[]
147           lg   <-->  input_len
148
149
150void Residu (
151    Word16 a[], // (i)     : prediction coefficients
152    Word16 x[], // (i)     : speech signal
153    Word16 y[], // (o)     : residual signal
154    Word16 lg   // (i)     : size of filtering
155)
156{
157    Word16 i, j;
158    Word32 s;
159
160    for (i = 0; i < lg; i++)
161    {
162        s = L_mult (x[i], a[0]);
163        for (j = 1; j <= M; j++)
164        {
165            s = L_mac (s, a[j], x[i - j]);
166        }
167        s = L_shl (s, 3);
168        y[i] = pv_round (s);
169    }
170    return;
171}
172
173------------------------------------------------------------------------------
174 RESOURCES USED [optional]
175
176 When the code is written for a specific target processor the
177 the resources used should be documented below.
178
179 HEAP MEMORY USED: x bytes
180
181 STACK MEMORY USED: x bytes
182
183 CLOCK CYCLES: (cycle count equation for this function) + (variable
184                used to represent cycle count for each subroutine
185                called)
186     where: (cycle count variable) = cycle count for [subroutine
187                                     name]
188
189------------------------------------------------------------------------------
190 CAUTION [optional]
191 [State any special notes, constraints or cautions for users of this function]
192
193------------------------------------------------------------------------------
194*/
195
196void Residu(
197    Word16 coef_ptr[],      /* (i)     : prediction coefficients*/
198    Word16 input_ptr[],     /* (i)     : speech signal          */
199    Word16 residual_ptr[],  /* (o)     : residual signal        */
200    Word16 input_len        /* (i)     : size of filtering      */
201)
202{
203
204
205    register Word16 i, j;
206    Word32 s1;
207    Word32 s2;
208    Word32 s3;
209    Word32 s4;
210    Word16 *p_input1;
211    Word16 *p_input2;
212    Word16 *p_input3;
213    Word16 *p_input4;
214    Word16 *p_coef;
215    Word16 *p_residual_ptr = &residual_ptr[input_len-1];
216    Word16 *p_input_ptr    = &input_ptr[input_len-1-M];
217
218    for (i = input_len >> 2; i != 0; i--)
219    {
220        s1       = 0x0000800L;
221        s2       = 0x0000800L;
222        s3       = 0x0000800L;
223        s4       = 0x0000800L;
224        p_coef  = &coef_ptr[M];
225        p_input1 = p_input_ptr--;
226        p_input2 = p_input_ptr--;
227        p_input3 = p_input_ptr--;
228        p_input4 = p_input_ptr--;
229
230        for (j = M >> 1; j != 0; j--)
231        {
232            s1 += ((Word32) * (p_coef) * *(p_input1++));
233            s2 += ((Word32) * (p_coef) * *(p_input2++));
234            s3 += ((Word32) * (p_coef) * *(p_input3++));
235            s4 += ((Word32) * (p_coef--) * *(p_input4++));
236            s1 += ((Word32) * (p_coef) * *(p_input1++));
237            s2 += ((Word32) * (p_coef) * *(p_input2++));
238            s3 += ((Word32) * (p_coef) * *(p_input3++));
239            s4 += ((Word32) * (p_coef--) * *(p_input4++));
240        }
241
242        s1 += (((Word32) * (p_coef)) * *(p_input1));
243        s2 += (((Word32) * (p_coef)) * *(p_input2));
244        s3 += (((Word32) * (p_coef)) * *(p_input3));
245        s4 += (((Word32) * (p_coef)) * *(p_input4));
246
247        *(p_residual_ptr--) = (Word16)(s1 >> 12);
248        *(p_residual_ptr--) = (Word16)(s2 >> 12);
249        *(p_residual_ptr--) = (Word16)(s3 >> 12);
250        *(p_residual_ptr--) = (Word16)(s4 >> 12);
251
252    }
253
254    return;
255}
256