Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / dec / src / decoder_gsm_amr.cpp
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 /****************************************************************************************
19 Portions 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)
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 ****************************************************************************************/
29 #include "decoder_gsm_amr.h"
30 #include "sp_dec.h"
31 #include "amrdecode.h"
32 #include "pvamrnbdecoder_api.h"
33
34 // Use default DLL entry point
35 #include "oscl_dll.h"
36 #include "oscl_error_codes.h"
37 #include "oscl_exception.h"
38 #include "oscl_mem.h"
39
40 #define KCAI_CODEC_INIT_FAILURE -1
41
42 OSCL_DLL_ENTRY_POINT_DEFAULT()
43
44 OSCL_EXPORT_REF CDecoder_AMR_NB *CDecoder_AMR_NB::NewL()
45 {
46     CDecoder_AMR_NB *dec = new CDecoder_AMR_NB;
47     if (dec == NULL)
48         OSCL_LEAVE(OsclErrNoMemory);
49     else
50         dec->ConstructL();
51     return dec;
52 }
53
54 OSCL_EXPORT_REF void CDecoder_AMR_NB::ConstructL()
55 {
56     iDecState = NULL;
57 }
58
59
60 /*
61 -----------------------------------------------------------------------------
62
63     CDecoder_AMR_NB
64
65     ~CDecoder_AMR_NB
66
67     Empty decoder destructor.
68
69     Parameters:     none
70
71     Return Values:  none
72
73 -----------------------------------------------------------------------------
74 */
75 OSCL_EXPORT_REF CDecoder_AMR_NB::~CDecoder_AMR_NB()
76 {
77     if (iDecState)
78         oscl_free(iDecState);
79     iDecState = NULL;
80
81     if (iInputBuf)
82     {
83         OSCL_ARRAY_DELETE(iInputBuf);
84         iInputBuf = NULL;
85     }
86
87     if (iOutputBuf)
88     {
89         OSCL_ARRAY_DELETE(iOutputBuf);
90         iOutputBuf = NULL;
91     }
92 }
93
94
95 /*
96 -----------------------------------------------------------------------------
97
98     CDecoder_AMR_NB
99
100     StartL
101
102     Start decoder object. Initialize codec status.
103
104     Parameters:     none
105
106     Return Values:  status
107
108 -----------------------------------------------------------------------------
109 */
110 OSCL_EXPORT_REF int32 CDecoder_AMR_NB::StartL(tPVAmrDecoderExternal * pExt,
111         bool aAllocateInputBuffer,
112         bool aAllocateOutputBuffer)
113 {
114
115     if (aAllocateInputBuffer)
116     {
117         iInputBuf = OSCL_ARRAY_NEW(int16, MAX_NUM_PACKED_INPUT_BYTES);
118         if (iInputBuf == NULL)
119         {
120             return KCAI_CODEC_INIT_FAILURE;
121         }
122     }
123     else
124     {
125         iInputBuf = NULL;
126     }
127     pExt->pInputBuffer = (uint8 *)iInputBuf;
128
129     if (aAllocateOutputBuffer)
130     {
131         iOutputBuf = OSCL_ARRAY_NEW(int16, L_FRAME);
132
133         if (iOutputBuf == NULL)
134         {
135             return KCAI_CODEC_INIT_FAILURE;
136         }
137     }
138     else
139     {
140         iOutputBuf = NULL;
141     }
142     pExt->pOutputBuffer = iOutputBuf;
143
144     pExt->samplingRate = 8000;
145     pExt->desiredChannels = 1;
146
147     pExt->reset_flag = 0;
148     pExt->reset_flag_old = 1;
149     pExt->mode_old = 0;
150
151     return GSMInitDecode(&iDecState, (int8*)"Decoder");
152 }
153
154
155 /*
156 -----------------------------------------------------------------------------
157
158     CDecoder_AMR_NB
159
160     ExecuteL
161
162     Execute decoder object. Read one encoded speech frame from the input
163     stream,  decode it and write the decoded frame to output stream.
164
165     Parameters:
166
167     Return Values:  status
168
169
170 -----------------------------------------------------------------------------
171 */
172
173 OSCL_EXPORT_REF int32 CDecoder_AMR_NB::ExecuteL(tPVAmrDecoderExternal * pExt)
174 {
175
176
177     if (pExt->input_format == WMF)
178         pExt->input_format = MIME_IETF;
179
180     return AMRDecode(iDecState,
181                      (enum Frame_Type_3GPP)pExt->mode,
182                      (uint8*) pExt->pInputBuffer,
183                      (int16*) pExt->pOutputBuffer,
184                      pExt->input_format);
185
186 }
187
188 /*
189 -----------------------------------------------------------------------------
190
191     CDecoder_AMR_NB
192
193     StopL
194
195     Stop decoder object. Flush out last frames, if necessary.
196
197     Parameters:     none
198
199     Return Values:  none
200
201 -----------------------------------------------------------------------------
202 */
203 OSCL_EXPORT_REF void CDecoder_AMR_NB::StopL()
204 {
205 }
206
207 /*
208 -----------------------------------------------------------------------------
209
210     CDecoder_AMR_NB
211
212     ResetDecoderL
213
214     Stop decoder object. Reset decoder.
215
216     Parameters:     none
217
218     Return Values:  status
219
220 -----------------------------------------------------------------------------
221 */
222 OSCL_EXPORT_REF int32 CDecoder_AMR_NB::ResetDecoderL()
223 {
224     return Speech_Decode_Frame_reset(iDecState);
225 }
226
227
228 /*
229 -----------------------------------------------------------------------------
230
231     CDecoder_AMR_NB
232
233     TerminateDecoderL
234
235     Stop decoder object. close decoder.
236
237     Parameters:     none
238
239     Return Values:  none
240
241 -----------------------------------------------------------------------------
242 */
243 OSCL_EXPORT_REF void CDecoder_AMR_NB::TerminateDecoderL()
244 {
245     GSMDecodeFrameExit(&iDecState);
246     iDecState = NULL;
247
248     if (iInputBuf)
249     {
250         OSCL_ARRAY_DELETE(iInputBuf);
251         iInputBuf = NULL;
252     }
253
254     if (iOutputBuf)
255     {
256         OSCL_ARRAY_DELETE(iOutputBuf);
257         iOutputBuf = NULL;
258     }
259 }
260
261
262