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