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/cbsearch.c
35 Functions: D_plsf_3
36
37     Date: 01/31/2002
38
39------------------------------------------------------------------------------
40 REVISION HISTORY
41
42 Description:
43 (1) Removed "count.h" and "basic_op.h" and replaced with individual include
44     files (add.h, sub.h, etc.)
45 (2) Added pOverflow parameter to code_10i40_35bits()
46
47 Description:  Replaced "int" and/or "char" with OSCL defined types.
48
49 Description:
50
51 ------------------------------------------------------------------------------
52 INPUT AND OUTPUT DEFINITIONS
53
54 Inputs:
55    x[] -- array of type Word16 -- target vector, Q0
56    h[] -- array of type Word16 -- impulse response of weighted synthesis
57                                   filter h[-L_subfr..-1] must be set to
58                                   zero. Q12
59    T0  -- Word16 -- Pitch lag
60    pitch_sharp -- Word16 -- Last quantized pitch gain, Q14
61    gain_pit --  Word16 gain_pit -- Pitch gain, Q14
62    res2[] -- array of type Word16 -- Long term prediction residual, Q0
63    mode -- enum Mode --  coder mode
64    subNr -- Word16 -- subframe number
65
66 Outputs:
67    code[] -- array of type Word16 -- Innovative codebook, Q13
68    y[] -- array of type Word16 -- filtered fixed codebook excitation
69                                   Q12
70
71    anap -- Double pointer to Word16 -- Signs of the pulses
72
73
74    pOverflow -- pointer to Flag -- Flag set when overflow occurs
75
76 Returns:
77    Zero
78
79 Global Variables Used:
80    None
81
82 Local Variables Needed:
83    None
84
85------------------------------------------------------------------------------
86 FUNCTION DESCRIPTION
87
88    Purpose          : Inovative codebook search (find index and gain)
89
90------------------------------------------------------------------------------
91 REQUIREMENTS
92
93
94
95------------------------------------------------------------------------------
96 REFERENCES
97
98 cbsearch.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
99
100------------------------------------------------------------------------------
101 PSEUDO-CODE
102
103
104
105------------------------------------------------------------------------------
106 RESOURCES USED
107   When the code is written for a specific target processor the
108     the resources used should be documented below.
109
110 STACK USAGE: [stack count for this module] + [variable to represent
111          stack usage for each subroutine called]
112
113     where: [stack usage variable] = stack usage for [subroutine
114         name] (see [filename].ext)
115
116 DATA MEMORY USED: x words
117
118 PROGRAM MEMORY USED: x words
119
120 CLOCK CYCLES: [cycle count equation for this module] + [variable
121           used to represent cycle count for each subroutine
122           called]
123
124     where: [cycle count variable] = cycle count for [subroutine
125        name] (see [filename].ext)
126
127------------------------------------------------------------------------------
128*/
129
130
131/*----------------------------------------------------------------------------
132; INCLUDES
133----------------------------------------------------------------------------*/
134#include "cbsearch.h"
135
136#include "typedef.h"
137#include "c2_9pf.h"
138#include "c2_11pf.h"
139#include "c3_14pf.h"
140#include "c4_17pf.h"
141#include "c8_31pf.h"
142#include "c1035pf.h"
143#include "mode.h"
144#include "basic_op.h"
145#include "cnst.h"
146
147/*----------------------------------------------------------------------------
148; MACROS
149; Define module specific macros here
150----------------------------------------------------------------------------*/
151
152
153/*----------------------------------------------------------------------------
154; DEFINES
155; Include all pre-processor statements here. Include conditional
156; compile variables also.
157----------------------------------------------------------------------------*/
158
159/*----------------------------------------------------------------------------
160; LOCAL FUNCTION DEFINITIONS
161; Function Prototype declaration
162----------------------------------------------------------------------------*/
163
164
165/*----------------------------------------------------------------------------
166; LOCAL STORE/BUFFER/POINTER DEFINITIONS
167; Variable declaration - defined here and used outside this module
168----------------------------------------------------------------------------*/
169
170/*----------------------------------------------------------------------------
171; EXTERNAL FUNCTION REFERENCES
172; Declare functions defined elsewhere and referenced in this module
173----------------------------------------------------------------------------*/
174
175/*----------------------------------------------------------------------------
176; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
177; Declare variables used in this module but defined elsewhere
178----------------------------------------------------------------------------*/
179
180/*----------------------------------------------------------------------------
181; FUNCTION CODE
182----------------------------------------------------------------------------*/
183
184void cbsearch(Word16 x[],        /* i : target vector, Q0                     */
185              Word16 h[],        /* i : impulse response of weighted synthesis*/
186              /*     filter h[-L_subfr..-1] must be set to */
187              /*     zero. Q12                             */
188              Word16 T0,         /* i : Pitch lag                             */
189              Word16 pitch_sharp,/* i : Last quantized pitch gain, Q14        */
190              Word16 gain_pit,   /* i : Pitch gain, Q14                       */
191              Word16 res2[],     /* i : Long term prediction residual, Q0     */
192              Word16 code[],     /* o : Innovative codebook, Q13              */
193              Word16 y[],        /* o : filtered fixed codebook excitation    */
194              /*     Q12                                   */
195              Word16 **anap,     /* o : Signs of the pulses                   */
196              enum Mode mode,    /* i : coder mode                            */
197              Word16 subNr,      /* i : subframe number                       */
198              Flag  *pOverflow)  /* o : Flag set when overflow occurs         */
199{
200    Word16 index;
201    Word16 i;
202    Word16 temp;
203    Word16 pit_sharpTmp;
204
205    /* For MR74, the pre and post CB pitch sharpening is included in the
206     * codebook search routine, while for MR122 is it not.
207     */
208
209    if ((mode == MR475) || (mode == MR515))
210    {
211        /* MR475, MR515 */
212        *(*anap)++ =
213            code_2i40_9bits(
214                subNr,
215                x,
216                h,
217                T0,
218                pitch_sharp,
219                code,
220                y,
221                &index,
222                pOverflow);
223
224        *(*anap)++ = index;    /* sign index */
225    }
226    else if (mode == MR59)
227    {   /* MR59 */
228        *(*anap)++ =
229            code_2i40_11bits(
230                x,
231                h,
232                T0,
233                pitch_sharp,
234                code,
235                y,
236                &index,
237                pOverflow);
238
239        *(*anap)++ = index;    /* sign index */
240    }
241    else if (mode == MR67)
242    {   /* MR67 */
243        *(*anap)++ =
244            code_3i40_14bits(
245                x,
246                h,
247                T0,
248                pitch_sharp,
249                code,
250                y,
251                &index,
252                pOverflow);
253
254        *(*anap)++ = index;    /* sign index */
255    }
256    else if ((mode == MR74) || (mode == MR795))
257    {   /* MR74, MR795 */
258        *(*anap)++ =
259            code_4i40_17bits(
260                x,
261                h,
262                T0,
263                pitch_sharp,
264                code,
265                y,
266                &index,
267                pOverflow);
268
269        *(*anap)++ = index;    /* sign index */
270    }
271    else if (mode == MR102)
272    {   /* MR102 */
273        /*-------------------------------------------------------------*
274         * - include pitch contribution into impulse resp. h1[]        *
275         *-------------------------------------------------------------*/
276        /* pit_sharpTmp = pit_sharp;                     */
277        /* if (pit_sharpTmp > 1.0) pit_sharpTmp = 1.0;   */
278
279        pit_sharpTmp =
280            shl(
281                pitch_sharp,
282                1,
283                pOverflow);
284
285        for (i = T0; i < L_SUBFR; i++)
286        {
287            temp =
288                mult(
289                    h[i - T0],
290                    pit_sharpTmp,
291                    pOverflow);
292
293            h[i] =
294                add(
295                    h[i],
296                    temp,
297                    pOverflow);
298        }
299
300        /*--------------------------------------------------------------*
301         * - Innovative codebook search (find index and gain)           *
302         *--------------------------------------------------------------*/
303        code_8i40_31bits(
304            x,
305            res2,
306            h,
307            code,
308            y,
309            *anap,
310            pOverflow);
311
312        *anap += 7;
313
314        /*-------------------------------------------------------*
315         * - Add the pitch contribution to code[].               *
316         *-------------------------------------------------------*/
317        for (i = T0; i < L_SUBFR; i++)
318        {
319            temp =
320                mult(
321                    code[i - T0],
322                    pit_sharpTmp,
323                    pOverflow);
324
325            code[i] =
326                add(
327                    code[i],
328                    temp,
329                    pOverflow);
330        }
331    }
332    else
333    {  /* MR122 */
334        /*-------------------------------------------------------------*
335         * - include pitch contribution into impulse resp. h1[]        *
336         *-------------------------------------------------------------*/
337
338        /* pit_sharpTmp = gain_pit;                      */
339        /* if (pit_sharpTmp > 1.0) pit_sharpTmp = 1.0;   */
340
341        pit_sharpTmp = shl(gain_pit, 1, pOverflow);
342
343        for (i = T0; i < L_SUBFR; i++)
344        {
345            temp = ((Word32)h[i - T0] * pit_sharpTmp) >> 15;
346            /*
347                     mult(
348                            h[i - T0],
349                            ,
350                            pOverflow);
351            */
352            h[i] =
353                add(
354                    h[i],
355                    temp,
356                    pOverflow);
357        }
358        /*--------------------------------------------------------------*
359         * - Innovative codebook search (find index and gain)           *
360         *--------------------------------------------------------------*/
361
362        code_10i40_35bits(
363            x,
364            res2,
365            h,
366            code,
367            y,
368            *anap,
369            pOverflow);
370
371        *anap += 10;
372
373        /*-------------------------------------------------------*
374         * - Add the pitch contribution to code[].               *
375         *-------------------------------------------------------*/
376        for (i = T0; i < L_SUBFR; i++)
377        {
378            temp =
379                mult(
380                    code[i - T0],
381                    pit_sharpTmp,
382                    pOverflow);
383
384            code[i] =
385                add(
386                    code[i],
387                    temp,
388                    pOverflow);
389        }
390    }
391
392}
393