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 - Wideband (AMR-WB) speech codec
23 Available from http://www.3gpp.org
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 //////////////////////////////////////////////////////////////////////////////////
31 // File: decoder_amr_wb.cpp //
33 //////////////////////////////////////////////////////////////////////////////////
35 #include "decoder_amr_wb.h"
36 #include "pvamrwbdecoder_api.h"
37 #include "pvamrwbdecoder.h"
38 #include "pvamrwbdecoder_cnst.h"
42 // Use default DLL entry point
44 #include "oscl_error_codes.h"
45 #include "oscl_exception.h"
49 #define KCAI_CODEC_INIT_FAILURE -1
52 OSCL_DLL_ENTRY_POINT_DEFAULT()
54 OSCL_EXPORT_REF CDecoder_AMR_WB *CDecoder_AMR_WB::NewL()
56 CDecoder_AMR_WB *dec = new CDecoder_AMR_WB;
58 OSCL_LEAVE(OsclErrNoMemory);
64 OSCL_EXPORT_REF void CDecoder_AMR_WB::ConstructL()
75 -----------------------------------------------------------------------------
81 Empty decoder destructor.
87 -----------------------------------------------------------------------------
89 OSCL_EXPORT_REF CDecoder_AMR_WB::~CDecoder_AMR_WB()
96 OSCL_ARRAY_DELETE((uint8*)pt_st);
102 OSCL_ARRAY_DELETE(iInputBuf);
108 OSCL_ARRAY_DELETE(iOutputBuf);
115 -----------------------------------------------------------------------------
121 Start decoder object. Initialize codec status.
125 Return Values: status
127 -----------------------------------------------------------------------------
129 OSCL_EXPORT_REF int32 CDecoder_AMR_WB::StartL(tPVAmrDecoderExternal * pExt,
130 bool aAllocateInputBuffer,
131 bool aAllocateOutputBuffer)
135 * Allocate Input bitstream buffer
137 if (aAllocateInputBuffer)
139 iInputBuf = OSCL_ARRAY_NEW(uint8, KAMRWB_NB_BYTES_MAX);
140 if (iInputBuf == NULL)
142 return KCAI_CODEC_INIT_FAILURE;
149 pExt->pInputBuffer = iInputBuf;
151 iInputSampleBuf = OSCL_ARRAY_NEW(int16, KAMRWB_NB_BITS_MAX);
152 if (iInputSampleBuf == NULL)
154 return KCAI_CODEC_INIT_FAILURE;
156 pExt->pInputSampleBuffer = iInputSampleBuf;
159 * Allocate Output PCM buffer
161 if (aAllocateOutputBuffer)
163 iOutputBuf = OSCL_ARRAY_NEW(int16, AMR_WB_PCM_FRAME);
165 if (iOutputBuf == NULL)
167 return KCAI_CODEC_INIT_FAILURE;
174 pExt->pOutputBuffer = iOutputBuf;
176 pExt->samplingRate = 16000;
177 pExt->desiredChannels = 1;
179 pExt->reset_flag = 0;
180 pExt->reset_flag_old = 1;
182 pExt->rx_state.prev_ft = RX_SPEECH_GOOD;
183 pExt->rx_state.prev_mode = 0;
186 int32 memreq = pvDecoder_AmrWbMemRequirements();
188 pt_st = OSCL_ARRAY_NEW(uint8, memreq);
192 return(KCAI_CODEC_INIT_FAILURE);
195 pvDecoder_AmrWb_Init(&st, pt_st, &ScratchMem);
202 -----------------------------------------------------------------------------
208 Execute decoder object. Read one encoded speech frame from the input
209 stream, decode it and write the decoded frame to output stream.
213 Return Values: status
216 -----------------------------------------------------------------------------
220 OSCL_EXPORT_REF int32 CDecoder_AMR_WB::ExecuteL(tPVAmrDecoderExternal * pExt)
223 if (pExt->input_format == MIME_IETF) /* MIME/storage file format */
225 mime_unsorting(pExt->pInputBuffer,
226 pExt->pInputSampleBuffer,
234 if ((pExt->frame_type == RX_NO_DATA) | (pExt->frame_type == RX_SPEECH_LOST))
236 pExt->mode = pExt->mode_old;
237 pExt->reset_flag = 0;
241 pExt->mode_old = pExt->mode;
243 /* if homed: check if this frame is another homing frame */
244 if (pExt->reset_flag_old == 1)
246 /* only check until end of first subframe */
247 pExt->reset_flag = pvDecoder_AmrWb_homing_frame_test_first(pExt->pInputSampleBuffer,
252 /* produce encoder homing frame if homed & input=decoder homing frame */
253 if ((pExt->reset_flag != 0) && (pExt->reset_flag_old != 0))
255 /* set homing sequence ( no need to decode anything */
257 for (int16 i = 0; i < AMR_WB_PCM_FRAME; i++)
259 pExt->pOutputBuffer[i] = EHF_MASK;
264 pExt->status = pvDecoder_AmrWb(pExt->mode,
265 pExt->pInputSampleBuffer,
273 for (int16 i = 0; i < AMR_WB_PCM_FRAME; i++) /* Delete the 2 LSBs (14-bit output) */
275 pExt->pOutputBuffer[i] &= 0xfffC;
279 /* if not homed: check whether current frame is a homing frame */
280 if (pExt->reset_flag_old == 0)
282 /* check whole frame */
283 pExt->reset_flag = pvDecoder_AmrWb_homing_frame_test(pExt->pInputSampleBuffer,
286 /* reset decoder if current frame is a homing frame */
287 if (pExt->reset_flag != 0)
289 pvDecoder_AmrWb_Reset(st, 1);;
291 pExt->reset_flag_old = pExt->reset_flag;
298 -----------------------------------------------------------------------------
310 -----------------------------------------------------------------------------
312 OSCL_EXPORT_REF void CDecoder_AMR_WB::StopL()
317 -----------------------------------------------------------------------------
323 Stop decoder object. Reset decoder.
327 Return Values: status
329 -----------------------------------------------------------------------------
331 OSCL_EXPORT_REF int32 CDecoder_AMR_WB::ResetDecoderL()
333 pvDecoder_AmrWb_Reset(st, 1);
339 -----------------------------------------------------------------------------
345 Stop decoder object. close decoder.
351 -----------------------------------------------------------------------------
353 OSCL_EXPORT_REF void CDecoder_AMR_WB::TerminateDecoderL()
360 OSCL_ARRAY_DELETE((uint8*)pt_st);
366 OSCL_ARRAY_DELETE(iInputBuf);
372 OSCL_ARRAY_DELETE(iOutputBuf);
376 if (iInputSampleBuf != NULL)
378 OSCL_ARRAY_DELETE(iInputSampleBuf);
379 iInputSampleBuf = NULL;