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: q_gain_p.cpp
35 Functions: q_gain_pitch
37 ------------------------------------------------------------------------------
41 ------------------------------------------------------------------------------
44 /*----------------------------------------------------------------------------
46 ----------------------------------------------------------------------------*/
54 /*--------------------------------------------------------------------------*/
60 /*----------------------------------------------------------------------------
62 ; Define module specific macros here
63 ----------------------------------------------------------------------------*/
65 /*----------------------------------------------------------------------------
67 ; Include all pre-processor statements here. Include conditional
68 ; compile variables also.
69 ----------------------------------------------------------------------------*/
70 #define NB_QUA_PITCH 16
72 /*----------------------------------------------------------------------------
73 ; LOCAL FUNCTION DEFINITIONS
74 ; Function Prototype declaration
75 ----------------------------------------------------------------------------*/
77 /*----------------------------------------------------------------------------
78 ; LOCAL VARIABLE DEFINITIONS
79 ; Variable declaration - defined here and used outside this module
80 ----------------------------------------------------------------------------*/
82 /*----------------------------------------------------------------------------
83 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
84 ; Declare variables used in this module but defined elsewhere
85 ----------------------------------------------------------------------------*/
87 /*--------------------------------------------------------------------------*/
93 ------------------------------------------------------------------------------
94 FUNCTION NAME: q_gain_pitch
95 ------------------------------------------------------------------------------
96 INPUT AND OUTPUT DEFINITIONS
99 mode -- enum Mode -- AMR mode
100 gp_limit -- Word16 -- pitch gain limit
101 gain -- Pointer to Word16 -- Pitch gain (unquant/quant), Q14
104 gain -- Pointer to Word16 -- Pitch gain (unquant/quant), Q14
106 gain_cand -- Array of type Word16 -- pitch gain candidates (3),
109 gain_cind -- Array of type Word16 -- pitch gain cand. indices (3),
112 pOverflow -- Pointer to Flag -- overflow indicator
115 Word16 -- index of quantization
117 Global Variables Used:
120 Local Variables Needed:
123 ------------------------------------------------------------------------------
127 ------------------------------------------------------------------------------
132 ------------------------------------------------------------------------------
135 q_gain_p.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
137 ------------------------------------------------------------------------------
141 ------------------------------------------------------------------------------
143 [State any special notes, constraints or cautions for users of this function]
145 ------------------------------------------------------------------------------
148 Word16 q_gain_pitch( /* Return index of quantization */
149 enum Mode mode, /* i : AMR mode */
150 Word16 gp_limit, /* i : pitch gain limit */
151 Word16 *gain, /* i/o: Pitch gain (unquant/quant), Q14 */
152 Word16 gain_cand[], /* o : pitch gain candidates (3), MR795 only, Q14 */
153 Word16 gain_cind[], /* o : pitch gain cand. indices (3),MR795 only, Q0 */
154 const Word16* qua_gain_pitch_ptr, /* i : ptr to read-only table */
163 err_min = sub(*gain, qua_gain_pitch_ptr[0], pOverflow);
164 err_min = abs_s(err_min);
168 for (i = 1; i < NB_QUA_PITCH; i++)
170 if (qua_gain_pitch_ptr[i] <= gp_limit)
172 err = sub(*gain, qua_gain_pitch_ptr[i], pOverflow);
185 /* in MR795 mode, compute three gain_pit candidates around the index
186 * found in the quantization loop: the index found and the two direct
187 * neighbours, except for the extreme cases (i=0 or i=NB_QUA_PITCH-1),
188 * where the direct neighbour and the neighbour to that is used.
198 if (index == (NB_QUA_PITCH - 1) ||
199 (qua_gain_pitch_ptr[index+1] > gp_limit))
209 /* store candidate indices and values */
210 for (i = 0; i < 3; i++)
213 gain_cand[i] = qua_gain_pitch_ptr[ii];
218 *gain = qua_gain_pitch_ptr[index];
222 /* in MR122 mode, just return the index and gain pitch found.
223 * If bitexactness is required, mask away the two LSBs (because
224 * in the original EFR, gain_pit was scaled Q12)
229 *gain = qua_gain_pitch_ptr[index] & 0xFFFC;
233 *gain = qua_gain_pitch_ptr[index];