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 ------------------------------------------------------------------------------
37 ------------------------------------------------------------------------------
41 ------------------------------------------------------------------------------
44 /*----------------------------------------------------------------------------
46 ----------------------------------------------------------------------------*/
55 /*----------------------------------------------------------------------------
57 ; Define module specific macros here
58 ----------------------------------------------------------------------------*/
60 /*----------------------------------------------------------------------------
62 ; Include all pre-processor statements here. Include conditional
63 ; compile variables also.
64 ----------------------------------------------------------------------------*/
65 #define LTP_GAIN_THR1 2721 /* 2721 Q13 = 0.3322 ~= 1.0 / (10*log10(2)) */
66 #define LTP_GAIN_THR2 5443 /* 5443 Q13 = 0.6644 ~= 2.0 / (10*log10(2)) */
68 /*----------------------------------------------------------------------------
69 ; LOCAL FUNCTION DEFINITIONS
70 ; Function Prototype declaration
71 ----------------------------------------------------------------------------*/
73 /*----------------------------------------------------------------------------
74 ; LOCAL VARIABLE DEFINITIONS
75 ; Variable declaration - defined here and used outside this module
76 ----------------------------------------------------------------------------*/
80 ------------------------------------------------------------------------------
81 FUNCTION NAME: gain_adapt_init
82 ------------------------------------------------------------------------------
83 INPUT AND OUTPUT DEFINITIONS
86 st -- double pointer to GainAdaptState
89 st -- double ponter to GainAdaptState
92 -1 if an error occurs during memory initialization
95 Global Variables Used:
98 Local Variables Needed:
101 ------------------------------------------------------------------------------
104 Allocates state memory and initializes state memory
106 ------------------------------------------------------------------------------
111 ------------------------------------------------------------------------------
114 g_adapt.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
116 ------------------------------------------------------------------------------
120 ------------------------------------------------------------------------------
122 [State any special notes, constraints or cautions for users of this function]
124 ------------------------------------------------------------------------------
127 Word16 gain_adapt_init(GainAdaptState **st)
131 if (st == (GainAdaptState **) NULL)
133 /* fprintf(stderr, "gain_adapt_init: invalid parameter\n"); */
138 /* allocate memory */
139 if ((s = (GainAdaptState *) oscl_malloc(sizeof(GainAdaptState))) == NULL)
141 /* fprintf(stderr, "gain_adapt_init: can't malloc state structure\n"); */
151 ------------------------------------------------------------------------------
152 FUNCTION NAME: gain_adapt_reset
153 ------------------------------------------------------------------------------
154 INPUT AND OUTPUT DEFINITIONS
157 st -- double pointer to GainAdaptState
160 st -- double ponter to GainAdaptState
163 -1 if an error occurs
166 Global Variables Used:
169 Local Variables Needed:
172 ------------------------------------------------------------------------------
175 Initializes state memory to zero
176 ------------------------------------------------------------------------------
181 ------------------------------------------------------------------------------
184 g_adapt.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
186 ------------------------------------------------------------------------------
190 ------------------------------------------------------------------------------
192 [State any special notes, constraints or cautions for users of this function]
194 ------------------------------------------------------------------------------
197 Word16 gain_adapt_reset(GainAdaptState *st)
201 if (st == (GainAdaptState *) NULL)
203 /* fprintf(stderr, "gain_adapt_reset: invalid parameter\n"); */
211 for (i = 0; i < LTPG_MEM_SIZE; i++)
221 ------------------------------------------------------------------------------
222 FUNCTION NAME: gain_adapt_exit
223 ------------------------------------------------------------------------------
224 INPUT AND OUTPUT DEFINITIONS
227 st -- double pointer to GainAdaptState
235 Global Variables Used:
238 Local Variables Needed:
241 ------------------------------------------------------------------------------
244 The memory used for state memory is freed
245 ------------------------------------------------------------------------------
250 ------------------------------------------------------------------------------
253 g_adapt.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
255 ------------------------------------------------------------------------------
259 ------------------------------------------------------------------------------
261 [State any special notes, constraints or cautions for users of this function]
263 ------------------------------------------------------------------------------
266 void gain_adapt_exit(GainAdaptState **st)
268 if (st == NULL || *st == NULL)
271 /* deallocate memory */
279 ------------------------------------------------------------------------------
280 FUNCTION NAME: gain_adapt
281 ------------------------------------------------------------------------------
282 INPUT AND OUTPUT DEFINITIONS
285 st -- double pointer to GainAdaptState
286 ltpg -- Word16 -- ltp coding gain (log2()), Q13
287 gain_cod -- Word16 -- code gain, Q1
290 alpha -- Pointer to Word16 -- gain adaptation factor, Q15
291 pOverflow -- Pointer to Flag -- overflow indicator
296 Global Variables Used:
299 Local Variables Needed:
302 ------------------------------------------------------------------------------
305 Purpose: calculate pitch/codebook gain adaptation factor alpha
306 (and update the adaptor state)
308 ------------------------------------------------------------------------------
313 ------------------------------------------------------------------------------
316 g_adapt.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
318 ------------------------------------------------------------------------------
322 ------------------------------------------------------------------------------
324 [State any special notes, constraints or cautions for users of this function]
326 ------------------------------------------------------------------------------
330 GainAdaptState *st, /* i : state struct */
331 Word16 ltpg, /* i : ltp coding gain (log2()), Q13 */
332 Word16 gain_cod, /* i : code gain, Q1 */
333 Word16 *alpha, /* o : gain adaptation factor, Q15 */
337 Word16 adapt; /* adaptdation status; 0, 1, or 2 */
338 Word16 result; /* alpha factor, Q13 */
339 Word16 filt; /* median-filtered LTP coding gain, Q13 */
343 /* basic adaptation */
344 if (ltpg <= LTP_GAIN_THR1)
350 if (ltpg <= LTP_GAIN_THR2)
362 * if ((cbGain > onFact * cbGainMem[0]) && (cbGain > 100.0))
368 /* tmp = cbGain / onFact; onFact = 2.0; 200 Q1 = 100.0 */
369 tmp = shr_r(gain_cod, 1, pOverflow);
371 if ((tmp > st->prev_gc) && (gain_cod > 200))
384 * // if onset, increase adaptor state
385 * if (onset && (gainAdapt < 2)) gainAdapt++;
387 if ((st->onset != 0) && (adapt < 2))
392 st->ltpg_mem[0] = ltpg;
393 filt = gmed_n(st->ltpg_mem, 5); /* function result */
397 if (filt > 5443) /* 5443 Q13 = 0.66443... */
405 result = 16384; /* 16384 Q15 = 0.5 */
408 { /* result = 0.5 - 0.75257499*filt */
409 /* result (Q15) = 16384 - 24660 * (filt << 2) */
410 filt = shl(filt, 2, pOverflow); /* Q15 */
411 result = mult(24660, filt, pOverflow);
412 result = 16384 - result;
421 * if (prevAlpha == 0.0) result = 0.5 * (result + prevAlpha);
423 if (st->prev_alpha == 0)
425 result = shr(result, 1, pOverflow);
428 /* store the result */
431 /* update adapter state memory */
432 st->prev_alpha = result;
433 st->prev_gc = gain_cod;
435 for (i = LTPG_MEM_SIZE - 1; i > 0; i--)
437 st->ltpg_mem[i] = st->ltpg_mem[i-1];
439 /* mem[0] is just present for convenience in calling the gmed_n[5]
440 * function above. The memory depth is really LTPG_MEM_SIZE-1.