Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_wb / dec / src / decoder_amr_wb.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.173
22     ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
23     Available from http://www.3gpp.org
24
25 (C) 2007, 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 //////////////////////////////////////////////////////////////////////////////////
30 //                                                                              //
31 //  File: decoder_amr_wb.cpp                                                   //
32 //                                                                              //
33 //////////////////////////////////////////////////////////////////////////////////
34
35 #include "decoder_amr_wb.h"
36 #include "pvamrwbdecoder_api.h"
37 #include "pvamrwbdecoder.h"
38 #include "pvamrwbdecoder_cnst.h"
39 #include "dtx.h"
40
41
42 // Use default DLL entry point
43 #include "oscl_dll.h"
44 #include "oscl_error_codes.h"
45 #include "oscl_exception.h"
46 #include "oscl_mem.h"
47
48
49 #define KCAI_CODEC_INIT_FAILURE -1
50
51
52 OSCL_DLL_ENTRY_POINT_DEFAULT()
53
54 OSCL_EXPORT_REF CDecoder_AMR_WB *CDecoder_AMR_WB::NewL()
55 {
56     CDecoder_AMR_WB *dec = new CDecoder_AMR_WB;
57     if (dec == NULL)
58         OSCL_LEAVE(OsclErrNoMemory);
59     else
60         dec->ConstructL();
61     return dec;
62 }
63
64 OSCL_EXPORT_REF void CDecoder_AMR_WB::ConstructL()
65 {
66     st = NULL;
67     pt_st = NULL;
68     ScratchMem = NULL;
69     iInputBuf = NULL;
70     iOutputBuf = NULL;
71 }
72
73
74 /*
75 -----------------------------------------------------------------------------
76
77     CDecoder_AMR_WB
78
79     ~CDecoder_AMR_WB
80
81     Empty decoder destructor.
82
83     Parameters:     none
84
85     Return Values:  none
86
87 -----------------------------------------------------------------------------
88 */
89 OSCL_EXPORT_REF CDecoder_AMR_WB::~CDecoder_AMR_WB()
90 {
91     st = NULL;
92     ScratchMem = NULL;
93
94     if (pt_st != NULL)
95     {
96         OSCL_ARRAY_DELETE((uint8*)pt_st);
97         pt_st = NULL;
98     }
99
100     if (iInputBuf)
101     {
102         OSCL_ARRAY_DELETE(iInputBuf);
103         iInputBuf = NULL;
104     }
105
106     if (iOutputBuf)
107     {
108         OSCL_ARRAY_DELETE(iOutputBuf);
109         iOutputBuf = NULL;
110     }
111 }
112
113
114 /*
115 -----------------------------------------------------------------------------
116
117     CDecoder_AMR_WB
118
119     StartL
120
121     Start decoder object. Initialize codec status.
122
123     Parameters:     none
124
125     Return Values:  status
126
127 -----------------------------------------------------------------------------
128 */
129 OSCL_EXPORT_REF int32 CDecoder_AMR_WB::StartL(tPVAmrDecoderExternal * pExt,
130         bool aAllocateInputBuffer,
131         bool aAllocateOutputBuffer)
132 {
133
134     /*
135      *  Allocate Input bitstream buffer
136      */
137     if (aAllocateInputBuffer)
138     {
139         iInputBuf = OSCL_ARRAY_NEW(uint8, KAMRWB_NB_BYTES_MAX);
140         if (iInputBuf == NULL)
141         {
142             return KCAI_CODEC_INIT_FAILURE;
143         }
144     }
145     else
146     {
147         iInputBuf = NULL;
148     }
149     pExt->pInputBuffer = iInputBuf;
150
151     iInputSampleBuf = OSCL_ARRAY_NEW(int16, KAMRWB_NB_BITS_MAX);
152     if (iInputSampleBuf == NULL)
153     {
154         return KCAI_CODEC_INIT_FAILURE;
155     }
156     pExt->pInputSampleBuffer = iInputSampleBuf;
157
158     /*
159      *  Allocate Output PCM buffer
160      */
161     if (aAllocateOutputBuffer)
162     {
163         iOutputBuf = OSCL_ARRAY_NEW(int16, AMR_WB_PCM_FRAME);
164
165         if (iOutputBuf == NULL)
166         {
167             return KCAI_CODEC_INIT_FAILURE;
168         }
169     }
170     else
171     {
172         iOutputBuf = NULL;
173     }
174     pExt->pOutputBuffer = iOutputBuf;
175
176     pExt->samplingRate = 16000;
177     pExt->desiredChannels = 1;
178
179     pExt->reset_flag = 0;
180     pExt->reset_flag_old = 1;
181     pExt->mode_old = 0;
182     pExt->rx_state.prev_ft = RX_SPEECH_GOOD;
183     pExt->rx_state.prev_mode = 0;
184
185
186     int32 memreq = pvDecoder_AmrWbMemRequirements();
187
188     pt_st = OSCL_ARRAY_NEW(uint8, memreq);
189
190     if (pt_st == 0)
191     {
192         return(KCAI_CODEC_INIT_FAILURE);
193     }
194
195     pvDecoder_AmrWb_Init(&st, pt_st, &ScratchMem);
196
197     return 0;
198 }
199
200
201 /*
202 -----------------------------------------------------------------------------
203
204     CDecoder_AMR_WB
205
206     ExecuteL
207
208     Execute decoder object. Read one encoded speech frame from the input
209     stream,  decode it and write the decoded frame to output stream.
210
211     Parameters:
212
213     Return Values:  status
214
215
216 -----------------------------------------------------------------------------
217 */
218
219
220 OSCL_EXPORT_REF int32 CDecoder_AMR_WB::ExecuteL(tPVAmrDecoderExternal * pExt)
221 {
222
223     if (pExt->input_format == MIME_IETF)  /* MIME/storage file format */
224     {
225         mime_unsorting(pExt->pInputBuffer,
226                        pExt->pInputSampleBuffer,
227                        &pExt->frame_type,
228                        &pExt->mode,
229                        pExt->quality,
230                        &pExt->rx_state);
231     }
232
233
234     if ((pExt->frame_type == RX_NO_DATA) | (pExt->frame_type == RX_SPEECH_LOST))
235     {
236         pExt->mode = pExt->mode_old;
237         pExt->reset_flag = 0;
238     }
239     else
240     {
241         pExt->mode_old = pExt->mode;
242
243         /* if homed: check if this frame is another homing frame */
244         if (pExt->reset_flag_old == 1)
245         {
246             /* only check until end of first subframe */
247             pExt->reset_flag = pvDecoder_AmrWb_homing_frame_test_first(pExt->pInputSampleBuffer,
248                                pExt->mode);
249         }
250     }
251
252     /* produce encoder homing frame if homed & input=decoder homing frame */
253     if ((pExt->reset_flag != 0) && (pExt->reset_flag_old != 0))
254     {
255         /* set homing sequence ( no need to decode anything */
256
257         for (int16 i = 0; i < AMR_WB_PCM_FRAME; i++)
258         {
259             pExt->pOutputBuffer[i] = EHF_MASK;
260         }
261     }
262     else
263     {
264         pExt->status = pvDecoder_AmrWb(pExt->mode,
265                                        pExt->pInputSampleBuffer,
266                                        pExt->pOutputBuffer,
267                                        &pExt->frameLength,
268                                        st,
269                                        pExt->frame_type,
270                                        ScratchMem);
271     }
272
273     for (int16 i = 0; i < AMR_WB_PCM_FRAME; i++)   /* Delete the 2 LSBs (14-bit output) */
274     {
275         pExt->pOutputBuffer[i] &= 0xfffC;
276     }
277
278
279     /* if not homed: check whether current frame is a homing frame */
280     if (pExt->reset_flag_old == 0)
281     {
282         /* check whole frame */
283         pExt->reset_flag = pvDecoder_AmrWb_homing_frame_test(pExt->pInputSampleBuffer,
284                            pExt->mode);
285     }
286     /* reset decoder if current frame is a homing frame */
287     if (pExt->reset_flag != 0)
288     {
289         pvDecoder_AmrWb_Reset(st, 1);;
290     }
291     pExt->reset_flag_old = pExt->reset_flag;
292
293     return pExt->status;
294
295 }
296
297 /*
298 -----------------------------------------------------------------------------
299
300     CDecoder_AMR_WB
301
302     StopL
303
304     Stop decoder object.
305
306     Parameters:     none
307
308     Return Values:  none
309
310 -----------------------------------------------------------------------------
311 */
312 OSCL_EXPORT_REF void CDecoder_AMR_WB::StopL()
313 {
314 }
315
316 /*
317 -----------------------------------------------------------------------------
318
319     CDecoder_AMR_WB
320
321     ResetDecoderL
322
323     Stop decoder object. Reset decoder.
324
325     Parameters:     none
326
327     Return Values:  status
328
329 -----------------------------------------------------------------------------
330 */
331 OSCL_EXPORT_REF int32 CDecoder_AMR_WB::ResetDecoderL()
332 {
333     pvDecoder_AmrWb_Reset(st, 1);
334     return 0;
335 }
336
337
338 /*
339 -----------------------------------------------------------------------------
340
341     CDecoder_AMR_WB
342
343     TerminateDecoderL
344
345     Stop decoder object. close decoder.
346
347     Parameters:     none
348
349     Return Values:  none
350
351 -----------------------------------------------------------------------------
352 */
353 OSCL_EXPORT_REF void CDecoder_AMR_WB::TerminateDecoderL()
354 {
355     st = NULL;
356     ScratchMem = NULL;
357
358     if (pt_st != NULL)
359     {
360         OSCL_ARRAY_DELETE((uint8*)pt_st);
361         pt_st = NULL;
362     }
363
364     if (iInputBuf)
365     {
366         OSCL_ARRAY_DELETE(iInputBuf);
367         iInputBuf = NULL;
368     }
369
370     if (iOutputBuf)
371     {
372         OSCL_ARRAY_DELETE(iOutputBuf);
373         iOutputBuf = NULL;
374     }
375
376     if (iInputSampleBuf != NULL)
377     {
378         OSCL_ARRAY_DELETE(iInputSampleBuf);
379         iInputSampleBuf = NULL;
380     }
381
382 }
383