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 ------------------------------------------------------------------------------
36 ------------------------------------------------------------------------------
40 ------------------------------------------------------------------------------
43 /*----------------------------------------------------------------------------
45 ----------------------------------------------------------------------------*/
61 /*----------------------------------------------------------------------------
63 ; Define module specific macros here
64 ----------------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------------
68 ; Include all pre-processor statements here. Include conditional
69 ; compile variables also.
70 ----------------------------------------------------------------------------*/
71 #define NPRED 4 /* number of prediction taps */
73 /*----------------------------------------------------------------------------
74 ; LOCAL FUNCTION DEFINITIONS
75 ; Function Prototype declaration
76 ----------------------------------------------------------------------------*/
78 /*----------------------------------------------------------------------------
79 ; LOCAL VARIABLE DEFINITIONS
80 ; Variable declaration - defined here and used outside this module
81 ----------------------------------------------------------------------------*/
84 ------------------------------------------------------------------------------
85 FUNCTION NAME: gainQuant_init
86 ------------------------------------------------------------------------------
87 INPUT AND OUTPUT DEFINITIONS
90 st -- double pointer to gainQuantState
93 st -- double ponter to gainQuantState
96 -1 if an error occurs during memory initialization
99 Global Variables Used:
102 Local Variables Needed:
105 ------------------------------------------------------------------------------
108 Allocates state memory and initializes state memory
110 ------------------------------------------------------------------------------
115 ------------------------------------------------------------------------------
118 gain_q.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
120 ------------------------------------------------------------------------------
124 ------------------------------------------------------------------------------
126 [State any special notes, constraints or cautions for users of this function]
128 ------------------------------------------------------------------------------
131 Word16 gainQuant_init(gainQuantState **state)
135 if (state == (gainQuantState **) NULL)
137 /* fprintf(stderr, "gainQuant_init: invalid parameter\n"); */
142 /* allocate memory */
143 if ((s = (gainQuantState *) oscl_malloc(sizeof(gainQuantState))) == NULL)
145 /* fprintf(stderr, "gainQuant_init: can not malloc state structure\n"); */
149 s->gain_idx_ptr = NULL;
153 /* Init sub states */
154 if (gc_pred_reset(&s->gc_predSt)
155 || gc_pred_reset(&s->gc_predUnqSt)
156 || gain_adapt_init(&s->adaptSt))
169 ------------------------------------------------------------------------------
170 FUNCTION NAME: gainQuant_reset
171 ------------------------------------------------------------------------------
172 INPUT AND OUTPUT DEFINITIONS
175 st -- double pointer to gainQuantState
178 st -- double ponter to gainQuantState
181 -1 if an error occurs
184 Global Variables Used:
187 Local Variables Needed:
190 ------------------------------------------------------------------------------
193 Initializes state memory to zero
194 ------------------------------------------------------------------------------
199 ------------------------------------------------------------------------------
202 gain_q.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
204 ------------------------------------------------------------------------------
208 ------------------------------------------------------------------------------
210 [State any special notes, constraints or cautions for users of this function]
212 ------------------------------------------------------------------------------
215 Word16 gainQuant_reset(gainQuantState *state)
218 if (state == (gainQuantState *) NULL)
220 /* fprintf(stderr, "gainQuant_reset: invalid parameter\n"); */
224 state->sf0_exp_gcode0 = 0;
225 state->sf0_frac_gcode0 = 0;
226 state->sf0_exp_target_en = 0;
227 state->sf0_frac_target_en = 0;
230 oscl_memset((void *)state->sf0_exp_coeff, 0, 5*sizeof(*state->sf0_exp_coeff));
231 oscl_memset((void *)state->sf0_frac_coeff, 0, 5*sizeof(*state->sf0_frac_coeff));
233 state->gain_idx_ptr = NULL;
235 gc_pred_reset(&(state->gc_predSt));
236 gc_pred_reset(&(state->gc_predUnqSt));
237 gain_adapt_reset(state->adaptSt);
243 ------------------------------------------------------------------------------
244 FUNCTION NAME: gainQuant_exit
245 ------------------------------------------------------------------------------
246 INPUT AND OUTPUT DEFINITIONS
249 st -- double pointer to gainQuantState
257 Global Variables Used:
260 Local Variables Needed:
263 ------------------------------------------------------------------------------
266 The memory used for state memory is freed
267 ------------------------------------------------------------------------------
272 ------------------------------------------------------------------------------
275 gain_q.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
277 ------------------------------------------------------------------------------
281 ------------------------------------------------------------------------------
283 [State any special notes, constraints or cautions for users of this function]
285 ------------------------------------------------------------------------------
288 void gainQuant_exit(gainQuantState **state)
290 if (state == NULL || *state == NULL)
293 gain_adapt_exit(&(*state)->adaptSt);
295 /* deallocate memory */
305 ------------------------------------------------------------------------------
306 FUNCTION NAME: gainQuant
307 ------------------------------------------------------------------------------
308 INPUT AND OUTPUT DEFINITIONS
311 st -- pointer to gainQuantState
312 mode -- enum Mode -- coder mode
313 res -- Word16 array -- LP residual, Q0
314 exc -- Word16 array -- LTP excitation (unfiltered), Q0
315 code -- Word16 array -- CB innovation (unfiltered), Q13
316 (unsharpened for MR475)
317 xn -- Word16 array -- Target vector.
318 xn2 -- Word16 array -- Target vector.
319 y1 -- Word16 array -- Adaptive codebook.
320 Y2 -- Word16 array -- Filtered innovative vector.
321 g_coeff -- Word16 array -- Correlations <xn y1> <y1 y1>
322 Compute in G_pitch().
324 even_subframe -- Word16 -- even subframe indicator flag
325 gp_limit -- Word16 -- pitch gain limit
326 gain_pit -- Word16 Pointer -- Pitch gain.
329 st -- pointer to gainQuantState
330 sf0_gain_pit -- Word16 Pointer -- Pitch gain sf 0. MR475
331 sf0_gain_cod -- Word16 Pointer -- Code gain sf 0. MR475
332 gain_pit -- Word16 Pointer -- Pitch gain.
333 gain_cod -- Word16 Pointer -- Code gain.
334 MR475: gain_* unquantized in even
335 subframes, quantized otherwise
337 anap -- Word16 Double Pointer -- Index of quantization
339 pOverflow -- Flag Pointer -- overflow indicator
344 Global Variables Used:
347 Local Variables Needed:
350 ------------------------------------------------------------------------------
353 Quantazation of gains
355 ------------------------------------------------------------------------------
360 ------------------------------------------------------------------------------
363 gain_q.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
365 ------------------------------------------------------------------------------
369 ------------------------------------------------------------------------------
371 [State any special notes, constraints or cautions for users of this function]
373 ------------------------------------------------------------------------------
379 gainQuantState *st, /* i/o : State struct */
380 enum Mode mode, /* i : coder mode */
381 Word16 res[], /* i : LP residual, Q0 */
382 Word16 exc[], /* i : LTP excitation (unfiltered), Q0 */
383 Word16 code[], /* i : CB innovation (unfiltered), Q13 */
384 /* (unsharpened for MR475) */
385 Word16 xn[], /* i : Target vector. */
386 Word16 xn2[], /* i : Target vector. */
387 Word16 y1[], /* i : Adaptive codebook. */
388 Word16 Y2[], /* i : Filtered innovative vector. */
389 Word16 g_coeff[], /* i : Correlations <xn y1> <y1 y1> */
390 /* Compute in G_pitch(). */
391 Word16 even_subframe, /* i : even subframe indicator flag */
392 Word16 gp_limit, /* i : pitch gain limit */
393 Word16 *sf0_gain_pit, /* o : Pitch gain sf 0. MR475 */
394 Word16 *sf0_gain_cod, /* o : Code gain sf 0. MR475 */
395 Word16 *gain_pit, /* i/o : Pitch gain. */
396 Word16 *gain_cod, /* o : Code gain. */
397 /* MR475: gain_* unquantized in even */
398 /* subframes, quantized otherwise */
399 Word16 **anap, /* o : Index of quantization */
400 CommonAmrTbls* common_amr_tbls, /* i : ptr to struct of tbl ptrs */
401 Flag *pOverflow /* o : overflow indicator */
406 Word16 qua_ener_MR122;
408 Word16 frac_coeff[5];
413 Word16 cod_gain_frac;
418 if (even_subframe != 0)
420 /* save position in output parameter stream and current
421 state of codebook gain predictor */
422 st->gain_idx_ptr = (*anap)++;
424 // gc_pred_copy(&(st->gc_predSt), &(st->gc_predUnqSt));
426 oscl_memcpy(st->gc_predUnqSt.past_qua_en,
427 st->gc_predSt.past_qua_en,
428 NPRED*sizeof(Word16));
429 oscl_memcpy(st->gc_predUnqSt.past_qua_en_MR122,
430 st->gc_predSt.past_qua_en_MR122,
431 NPRED*sizeof(Word16));
434 /* predict codebook gain (using "unquantized" predictor)*/
435 /* (note that code[] is unsharpened in MR475) */
441 &st->sf0_frac_gcode0,
446 /* calculate energy coefficients for quantization
447 and store them in state structure (will be used
448 in next subframe when real quantizer is run) */
462 /* store optimum codebook gain (Q1) */
463 temp = cod_gain_exp + 1;
473 &st->sf0_exp_target_en,
474 &st->sf0_frac_target_en,
477 /* calculate optimum codebook gain and update
478 "unquantized" predictor */
479 MR475_update_unq_pred(
487 /* the real quantizer is not run here... */
491 /* predict codebook gain (using "unquantized" predictor) */
492 /* (note that code[] is unsharpened in MR475) */
503 /* calculate energy coefficients for quantization */
523 /* run real (4-dim) quantizer and update real gain predictor */
531 st->sf0_exp_target_en,
532 st->sf0_frac_target_en,
550 /*-------------------------------------------------------------------*
551 * predict codebook gain and quantize *
552 * (also compute normalized CB innovation energy for MR795) *
553 *-------------------------------------------------------------------*/
580 common_amr_tbls->qua_gain_code_ptr,
585 /* calculate energy coefficients for quantization */
643 /*------------------------------------------------------------------*
644 * update table of past quantized energies *
645 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
646 * st->past_qua_en(Q10) = 20 * Log10(qua_gain_code) / constant *
647 * = Log2(qua_gain_code) *
649 * constant = 20*Log10(2) *
650 *------------------------------------------------------------------*/