1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
30 ------------------------------------------------------------------------------
34 Filename: cbsearch.cpp
37 ------------------------------------------------------------------------------
38 INPUT AND OUTPUT DEFINITIONS
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
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
53 code[] -- array of type Word16 -- Innovative codebook, Q13
54 y[] -- array of type Word16 -- filtered fixed codebook excitation
57 anap -- Double pointer to Word16 -- Signs of the pulses
60 pOverflow -- pointer to Flag -- Flag set when overflow occurs
65 Global Variables Used:
68 Local Variables Needed:
71 ------------------------------------------------------------------------------
74 Purpose : Inovative codebook search (find index and gain)
76 ------------------------------------------------------------------------------
81 ------------------------------------------------------------------------------
84 cbsearch.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
86 ------------------------------------------------------------------------------
91 ------------------------------------------------------------------------------
95 /*----------------------------------------------------------------------------
97 ----------------------------------------------------------------------------*/
108 #include "basic_op.h"
111 /*----------------------------------------------------------------------------
113 ; Define module specific macros here
114 ----------------------------------------------------------------------------*/
117 /*----------------------------------------------------------------------------
119 ; Include all pre-processor statements here. Include conditional
120 ; compile variables also.
121 ----------------------------------------------------------------------------*/
123 /*----------------------------------------------------------------------------
124 ; LOCAL FUNCTION DEFINITIONS
125 ; Function Prototype declaration
126 ----------------------------------------------------------------------------*/
129 /*----------------------------------------------------------------------------
130 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
131 ; Variable declaration - defined here and used outside this module
132 ----------------------------------------------------------------------------*/
134 /*----------------------------------------------------------------------------
135 ; EXTERNAL FUNCTION REFERENCES
136 ; Declare functions defined elsewhere and referenced in this module
137 ----------------------------------------------------------------------------*/
139 /*----------------------------------------------------------------------------
140 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
141 ; Declare variables used in this module but defined elsewhere
142 ----------------------------------------------------------------------------*/
144 /*----------------------------------------------------------------------------
146 ----------------------------------------------------------------------------*/
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 */
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 */
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 */
170 /* For MR74, the pre and post CB pitch sharpening is included in the
171 * codebook search routine, while for MR122 is it not.
174 if ((mode == MR475) || (mode == MR515))
187 common_amr_tbls->startPos_ptr,
190 *(*anap)++ = index; /* sign index */
192 else if (mode == MR59)
205 *(*anap)++ = index; /* sign index */
207 else if (mode == MR67)
220 *(*anap)++ = index; /* sign index */
222 else if ((mode == MR74) || (mode == MR795))
233 common_amr_tbls->gray_ptr,
236 *(*anap)++ = index; /* sign index */
238 else if (mode == 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; */
252 for (i = T0; i < L_SUBFR; i++)
267 /*--------------------------------------------------------------*
268 * - Innovative codebook search (find index and gain) *
269 *--------------------------------------------------------------*/
281 /*-------------------------------------------------------*
282 * - Add the pitch contribution to code[]. *
283 *-------------------------------------------------------*/
284 for (i = T0; i < L_SUBFR; i++)
301 /*-------------------------------------------------------------*
302 * - include pitch contribution into impulse resp. h1[] *
303 *-------------------------------------------------------------*/
305 /* pit_sharpTmp = gain_pit; */
306 /* if (pit_sharpTmp > 1.0) pit_sharpTmp = 1.0; */
308 pit_sharpTmp = shl(gain_pit, 1, pOverflow);
310 for (i = T0; i < L_SUBFR; i++)
312 temp = ((Word32)h[i - T0] * pit_sharpTmp) >> 15;
325 /*--------------------------------------------------------------*
326 * - Innovative codebook search (find index and gain) *
327 *--------------------------------------------------------------*/
336 common_amr_tbls->gray_ptr,
341 /*-------------------------------------------------------*
342 * - Add the pitch contribution to code[]. *
343 *-------------------------------------------------------*/
344 for (i = T0; i < L_SUBFR; i++)