d_gain_c.cpp revision 4f1efc098cb5791c3e9f483f2af84aef70d2d0a0
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/****************************************************************************************
19Portions 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)
26Permission to distribute, modify and use this file under the standard license
27terms listed above has been obtained from the copyright holder.
28****************************************************************************************/
29/*
30------------------------------------------------------------------------------
31
32
33
34 Pathname: ./audio/gsm-amr/c/src/d_gain_c.c
35 Functions: d_gain_c
36
37     Date: 01/29/2002
38
39------------------------------------------------------------------------------
40 REVISION HISTORY
41
42 Description: Updated include files and intput/output section. Changed .tab
43              files to .c files.
44
45 Description:  Replaced OSCL mem type functions and eliminated include
46               files that now are chosen by OSCL definitions
47
48 Description:  Replaced "int" and/or "char" with OSCL defined types.
49
50 Description: Added #ifdef __cplusplus around extern'ed table.
51
52 Description:
53
54 ------------------------------------------------------------------------------
55 INPUT AND OUTPUT DEFINITIONS
56
57 Inputs:
58    pred_state  = pointer to sturcture type gc_predState. MA predictor state
59    mode        = AMR mode (MR795 or MR122) of type enum Mode
60    index       = received quantization index of type Word16
61    code[]      = pointer to innovation codevector of type Word16
62    pOverflow= pointer to value indicating existence of overflow (Flag)
63
64 Outputs:
65    pred_state  = pointer to sturcture type gc_predState. MA predictor state
66    gain_code   = pointer to decoded innovation gain of type Word16
67    pOverflow = 1 if there is an overflow else it is zero.
68
69 Returns:
70    None.
71
72 Global Variables Used:
73    None
74
75 Local Variables Needed:
76    None
77
78------------------------------------------------------------------------------
79 FUNCTION DESCRIPTION
80
81  Function    : d_gain_code
82  Purpose     : Decode the fixed codebook gain using the received index.
83
84------------------------------------------------------------------------------
85 REQUIREMENTS
86
87
88
89------------------------------------------------------------------------------
90 REFERENCES
91
92 d_gain_c.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
93
94------------------------------------------------------------------------------
95 PSEUDO-CODE
96
97
98
99------------------------------------------------------------------------------
100 RESOURCES USED
101   When the code is written for a specific target processor the
102     the resources used should be documented below.
103
104 STACK USAGE: [stack count for this module] + [variable to represent
105          stack usage for each subroutine called]
106
107     where: [stack usage variable] = stack usage for [subroutine
108         name] (see [filename].ext)
109
110 DATA MEMORY USED: x words
111
112 PROGRAM MEMORY USED: x words
113
114 CLOCK CYCLES: [cycle count equation for this module] + [variable
115           used to represent cycle count for each subroutine
116           called]
117
118     where: [cycle count variable] = cycle count for [subroutine
119        name] (see [filename].ext)
120
121------------------------------------------------------------------------------
122*/
123
124
125/*----------------------------------------------------------------------------
126; INCLUDES
127----------------------------------------------------------------------------*/
128#include "d_gain_c.h"
129#include "typedef.h"
130#include "mode.h"
131
132#include "oper_32b.h"
133#include "cnst.h"
134#include "log2.h"
135#include "pow2.h"
136#include "gc_pred.h"
137
138#include "basic_op.h"
139
140/*--------------------------------------------------------------------------*/
141#ifdef __cplusplus
142extern "C"
143{
144#endif
145
146    /*----------------------------------------------------------------------------
147    ; MACROS
148    ; Define module specific macros here
149    ----------------------------------------------------------------------------*/
150
151
152    /*----------------------------------------------------------------------------
153    ; DEFINES
154    ; Include all pre-processor statements here. Include conditional
155    ; compile variables also.
156    ----------------------------------------------------------------------------*/
157
158    /*----------------------------------------------------------------------------
159    ; LOCAL FUNCTION DEFINITIONS
160    ; Function Prototype declaration
161    ----------------------------------------------------------------------------*/
162
163
164    /*----------------------------------------------------------------------------
165    ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
166    ; Variable declaration - defined here and used outside this module
167    ----------------------------------------------------------------------------*/
168
169
170    /*----------------------------------------------------------------------------
171    ; EXTERNAL FUNCTION REFERENCES
172    ; Declare functions defined elsewhere and referenced in this module
173    ----------------------------------------------------------------------------*/
174
175    /*----------------------------------------------------------------------------
176    ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
177    ; Declare variables used in this module but defined elsewhere
178    ----------------------------------------------------------------------------*/
179    extern const Word16 qua_gain_code[];
180
181
182    /*--------------------------------------------------------------------------*/
183#ifdef __cplusplus
184}
185#endif
186
187/*----------------------------------------------------------------------------
188; FUNCTION CODE
189----------------------------------------------------------------------------*/
190void d_gain_code(
191    gc_predState *pred_state, /* i/o : MA predictor state               */
192    enum Mode mode,           /* i   : AMR mode (MR795 or MR122)        */
193    Word16 index,             /* i   : received quantization index      */
194    Word16 code[],            /* i   : innovation codevector            */
195    Word16 *gain_code,        /* o   : decoded innovation gain          */
196    Flag   *pOverflow
197)
198{
199    Word16 gcode0, exp, frac;
200    const Word16 *p;
201    Word16 qua_ener_MR122, qua_ener;
202    Word16 exp_inn_en;
203    Word16 frac_inn_en;
204    Word32 L_tmp;
205    Word16 tbl_tmp;
206    Word16 temp;
207    /*-------------- Decode codebook gain ---------------*/
208
209    /*-------------------------------------------------------------------*
210     *  predict codebook gain                                            *
211     *  ~~~~~~~~~~~~~~~~~~~~~                                            *
212     *  gc0     = Pow2(int(d)+frac(d))                                   *
213     *          = 2^exp + 2^frac                                         *
214     *                                                                   *
215     *-------------------------------------------------------------------*/
216
217    gc_pred(pred_state, mode, code, &exp, &frac,
218            &exp_inn_en, &frac_inn_en, pOverflow);
219
220    index &= 31;                    /* index < 32, to avoid buffer overflow */
221    tbl_tmp = index + (index << 1);
222
223    p = &qua_gain_code[tbl_tmp];
224
225    /* Different scalings between MR122 and the other modes */
226    temp = sub((Word16)mode, (Word16)MR122, pOverflow);
227    if (temp == 0)
228    {
229        gcode0 = (Word16)(Pow2(exp, frac, pOverflow));    /* predicted gain */
230        gcode0 = shl(gcode0, 4, pOverflow);
231        *gain_code = shl(mult(gcode0, *p++, pOverflow), 1, pOverflow);
232    }
233    else
234    {
235        gcode0 = (Word16)(Pow2(14, frac, pOverflow));
236        L_tmp = L_mult(*p++, gcode0, pOverflow);
237        L_tmp = L_shr(L_tmp, sub(9, exp, pOverflow), pOverflow);
238        *gain_code = extract_h(L_tmp);          /* Q1 */
239    }
240
241    /*-------------------------------------------------------------------*
242     *  update table of past quantized energies                          *
243     *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                          *
244     *-------------------------------------------------------------------*/
245    qua_ener_MR122 = *p++;
246    qua_ener = *p++;
247    gc_pred_update(pred_state, qua_ener_MR122, qua_ener);
248
249    return;
250}
251
252
253
254
255