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/spstproc.c
35 Functions: subframePostProc
36
37     Date: 02/06/2002
38
39------------------------------------------------------------------------------
40 REVISION HISTORY
41
42 Description: Updated template used to PV coding template.
43 Changed to accept the pOverflow flag for EPOC compatibility.
44
45 Description:
46              1. Eliminated unused include files.
47              2. Replaced array addressing by pointers
48              3. Eliminated math operations that unnecessary checked for
49                 saturation
50              4. Replaced loop counter with decrement loops
51
52 Description:  Added casting to eliminate warnings
53
54 Description:  Replaced "int" and/or "char" with OSCL defined types.
55
56 Description:
57
58------------------------------------------------------------------------------
59 MODULE DESCRIPTION
60
61    Subframe post processing
62------------------------------------------------------------------------------
63*/
64
65/*----------------------------------------------------------------------------
66; INCLUDES
67----------------------------------------------------------------------------*/
68#include "spstproc.h"
69#include "syn_filt.h"
70#include "cnst.h"
71
72/*----------------------------------------------------------------------------
73; MACROS
74; Define module specific macros here
75----------------------------------------------------------------------------*/
76
77/*----------------------------------------------------------------------------
78; DEFINES
79; Include all pre-processor statements here. Include conditional
80; compile variables also.
81----------------------------------------------------------------------------*/
82
83/*----------------------------------------------------------------------------
84; LOCAL FUNCTION DEFINITIONS
85; Function Prototype declaration
86----------------------------------------------------------------------------*/
87
88/*----------------------------------------------------------------------------
89; LOCAL VARIABLE DEFINITIONS
90; Variable declaration - defined here and used outside this module
91----------------------------------------------------------------------------*/
92
93/*
94------------------------------------------------------------------------------
95 FUNCTION NAME: subframePostProc
96------------------------------------------------------------------------------
97 INPUT AND OUTPUT DEFINITIONS
98
99
100 Inputs:
101    speech    -- Pointer to Word16 -- speech segment
102    mode      -- enum Mode         -- coder mode
103    i_subfr   -- Word16 -- Subframe nr
104    gain_pit  -- Word16 -- Pitch gain  Q14
105    gain_code -- Word16 -- Decoded innovation gain
106    Aq        -- Pointer to Word16 -- A(z) quantized for the 4 subframes
107    synth     -- Word16 Array -- Local synthesis
108    xn        -- Word16 Array -- Target vector for pitch search
109    code      -- Word16 Array -- Fixed codebook exitation
110    y1        -- Word16 Array -- Filtered adaptive exitation
111    y2        -- Word16 Array -- Filtered fixed codebook excitation
112    mem_syn   -- Pointer to Word16 -- memory of synthesis filter
113
114 Outputs:
115    mem_syn -- Pointer to Word16 -- memory of synthesis filter
116    mem_err -- Pointer to Word16 -- pointer to error signal
117    mem_w0  -- Pointer to Word16 -- memory of weighting filter
118    exc     -- Pointer to Word16 -- long term prediction residual
119    sharp   -- Pointer to Word16 -- pitch sharpening value
120    pOverflow -- Pointer to Flag -- overflow indicator
121
122 Returns:
123    None
124
125 Global Variables Used:
126    None
127
128 Local Variables Needed:
129    None
130
131------------------------------------------------------------------------------
132 FUNCTION DESCRIPTION
133
134
135------------------------------------------------------------------------------
136 REQUIREMENTS
137
138 None
139
140------------------------------------------------------------------------------
141 REFERENCES
142
143 spstproc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
144
145------------------------------------------------------------------------------
146 PSEUDO-CODE
147
148
149------------------------------------------------------------------------------
150 RESOURCES USED [optional]
151
152 When the code is written for a specific target processor the
153 the resources used should be documented below.
154
155 HEAP MEMORY USED: x bytes
156
157 STACK MEMORY USED: x bytes
158
159 CLOCK CYCLES: (cycle count equation for this function) + (variable
160                used to represent cycle count for each subroutine
161                called)
162     where: (cycle count variable) = cycle count for [subroutine
163                                     name]
164
165------------------------------------------------------------------------------
166 CAUTION [optional]
167 [State any special notes, constraints or cautions for users of this function]
168
169------------------------------------------------------------------------------
170*/
171
172void subframePostProc(
173    Word16 *speech,   /* i   : speech segment                        */
174    enum Mode mode,   /* i   : coder mode                            */
175    Word16 i_subfr,   /* i   : Subframe nr                           */
176    Word16 gain_pit,  /* i   : Pitch gain                       Q14  */
177    Word16 gain_code, /* i   : Decoded innovation gain               */
178    Word16 *Aq,       /* i   : A(z) quantized for the 4 subframes    */
179    Word16 synth[],   /* i   : Local snthesis                        */
180    Word16 xn[],      /* i   : Target vector for pitch search        */
181    Word16 code[],    /* i   : Fixed codebook exitation              */
182    Word16 y1[],      /* i   : Filtered adaptive exitation           */
183    Word16 y2[],      /* i   : Filtered fixed codebook excitation    */
184    Word16 *mem_syn,  /* i/o : memory of synthesis filter            */
185    Word16 *mem_err,  /* o   : pointer to error signal               */
186    Word16 *mem_w0,   /* o   : memory of weighting filter            */
187    Word16 *exc,      /* o   : long term prediction residual         */
188    Word16 *sharp,    /* o   : pitch sharpening value                */
189    Flag   *pOverflow /* o   : overflow indicator                    */
190)
191{
192    Word16 i;
193    Word16 j;
194    Word16 temp;
195    Word32 L_temp;
196    Word32 L_temp2;
197    Word16 tempShift;
198    Word16 kShift;
199    Word16 pitch_fac;
200    Word16 *p_exc;
201    Word16 *p_code;
202
203    OSCL_UNUSED_ARG(pOverflow);
204
205    if (mode != MR122)
206    {
207        tempShift = 1;
208        kShift = 16 - 2 - 1;
209        pitch_fac = gain_pit;
210    }
211    else
212    {
213        tempShift = 2;
214        kShift = 16 - 4 - 1;
215        pitch_fac = gain_pit >> 1;
216    }
217
218    /*------------------------------------------------------------*
219     * - Update pitch sharpening "sharp" with quantized gain_pit  *
220     *------------------------------------------------------------*/
221
222    if (gain_pit < SHARPMAX)
223    {
224        *sharp = gain_pit;
225    }
226    else
227    {
228        *sharp = SHARPMAX;
229    }
230
231    /*------------------------------------------------------*
232     * - Find the total excitation                          *
233     * - find synthesis speech corresponding to exc[]       *
234     * - update filters memories for finding the target     *
235     *   vector in the next subframe                        *
236     *   (update error[-m..-1] and mem_w_err[])             *
237     *------------------------------------------------------*/
238
239    p_exc  = &exc[ i_subfr];
240    p_code = &code[0];
241
242    for (i = L_SUBFR >> 1; i != 0 ; i--)
243    {
244        /* exc[i] = gain_pit*exc[i] + gain_code*code[i]; */
245
246        /*
247         *                      12k2  others
248         * ---------------------------------
249         * exc                   Q0      Q0
250         * gain_pit              Q14     Q14
251         * pitch_fac             Q13     Q14
252         *    product:           Q14     Q15
253         *
254         * code                  Q12     Q13
255         * gain_code             Q1      Q1
256         *    product            Q14     Q15
257         *    sum                Q14     Q15
258         *
259         * tempShift             2       1
260         *    sum<<tempShift     Q16     Q16
261         * result -> exc         Q0      Q0
262         */
263        L_temp     = ((Word32) * (p_exc++) * pitch_fac) << 1;
264        L_temp2    = ((Word32) * (p_exc--) * pitch_fac) << 1;
265        L_temp    += ((Word32) * (p_code++) * gain_code) << 1;
266        L_temp2   += ((Word32) * (p_code++) * gain_code) << 1;
267        L_temp   <<=  tempShift;
268        L_temp2  <<=  tempShift;
269        *(p_exc++) = (Word16)((L_temp  + 0x08000L) >> 16);
270        *(p_exc++) = (Word16)((L_temp2 + 0x08000L) >> 16);
271
272    }
273
274    Syn_filt(
275        Aq,
276        &exc[i_subfr],
277        &synth[i_subfr],
278        L_SUBFR,
279        mem_syn,
280        1);
281
282    for (i = L_SUBFR - M, j = 0; i < L_SUBFR; i++, j++)
283    {
284        mem_err[j] = speech[i_subfr + i] - synth[i_subfr + i];
285
286        /*
287         *                      12k2  others
288         * ---------------------------------
289         * y1                    Q0      Q0
290         * gain_pit              Q14     Q14
291         *    product            Q15     Q15
292         *    shifted prod.      Q16     Q16
293         * temp                  Q0      Q0
294         *
295         * y2                    Q10     Q12
296         * gain_code             Q1      Q1
297         *    product            Q12     Q14
298         * kshift                 4       2
299         *    shifted prod.      Q16     Q16
300         * k                     Q0      Q0
301         * mem_w0,xn,sum         Q0      Q0
302         */
303
304        L_temp = ((Word32)y1[i] * gain_pit);
305        temp  = (Word16)(L_temp >> 14);
306
307        L_temp = ((Word32)y2[i] * gain_code);
308        temp += (Word16)(L_temp >> kShift);
309
310        mem_w0[j] = xn[i] - temp;
311    }
312
313    return;
314}
315