Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / gsmamr_encoder_wrapper.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 #ifndef GSMAMR_ENC_H_INCLUDED
30 #include "gsmamr_enc.h"
31 #endif
32
33 #ifndef GSMAMR_ENCODER_WRAPPER_H_INCLUDED
34 #include "gsmamr_encoder_wrapper.h"
35 #endif
36
37 #ifndef OSCL_DLL_H_INCLUDED
38 #include "oscl_dll.h"
39 #endif
40
41 // Define entry point for this DLL
42 OSCL_DLL_ENTRY_POINT_DEFAULT()
43
44 //----------------------------------------------------------------------------
45 // CONSTANTS
46 //----------------------------------------------------------------------------
47
48 // Number of samples per frame
49 #define KGAMR_NUM_SAMPLES_PER_FRAME   160
50
51 // Default mode
52 #define KDFLT_GAMR_MODE               MR475
53
54 // Default bits per sample for input audio
55 #define KDFLT_GAMR_BITS_PER_SAMPLE    16
56
57 // Default sampling rate for input audio (in Hz)
58 #define KDFLT_GAMR_SAMPLING_RATE      8000
59
60 // Default input clock rate for input audio (in ticks/sec)
61 #define KDFLT_GAMR_CLOCK_RATE         8000
62
63 // Default number of channels
64 #define KDFLT_GAMR_NUM_CHANNELS       1
65
66 // length of uncompressed audio frame in bytes
67 // Formula: (num_samples_per_frame * bits_per_sample) / num_bits_per_byte
68 #define PV_GSM_AMR_20_MSEC_SIZE       \
69         ((KGAMR_NUM_SAMPLES_PER_FRAME * KDFLT_GAMR_BITS_PER_SAMPLE) / 8)
70
71
72 ///////////////////////////////////////////////////////////////////////////////
73 OSCL_EXPORT_REF
74 CPvGsmAmrEncoder::CPvGsmAmrEncoder()
75 {
76     // initialize member data to default values
77     iEncState = NULL;
78     iSidState = NULL;
79     iGsmAmrMode = (GSM_AMR_MODES)KDFLT_GAMR_MODE;
80     iLastModeUsed = 0;
81     iBitStreamFormat = AMR_TX_WMF;
82     iNumSamplesPerFrame = KGAMR_NUM_SAMPLES_PER_FRAME;
83 }
84 ///////////////////////////////////////////////////////////////////////////////
85 OSCL_EXPORT_REF CPvGsmAmrEncoder::~CPvGsmAmrEncoder()
86 {
87 }
88
89 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
90 OSCL_EXPORT_REF int32 CPvGsmAmrEncoder::InitializeEncoder(int32 aMaxOutputBufferSize, TEncodeProperties* aProps)
91 {
92     if (aProps == NULL)
93     {
94         // use default parameters
95         TEncodeProperties dfltProps;
96         aProps = &dfltProps;
97         dfltProps.iInBitsPerSample = KDFLT_GAMR_BITS_PER_SAMPLE;
98         dfltProps.iInSamplingRate = KDFLT_GAMR_SAMPLING_RATE;
99         dfltProps.iInClockRate = dfltProps.iInSamplingRate;
100         dfltProps.iInNumChannels = KDFLT_GAMR_NUM_CHANNELS;
101         iGsmAmrMode = (GSM_AMR_MODES)KDFLT_GAMR_MODE;
102         iBitStreamFormat = AMR_TX_WMF;
103     }
104     else
105     {
106         // check first if input parameters are valid
107         if ((IsModeValid(aProps->iMode) == false) ||
108                 (aProps->iInBitsPerSample == 0) ||
109                 (aProps->iInClockRate == 0) ||
110                 (aProps->iInSamplingRate == 0) ||
111                 (aProps->iInNumChannels == 0))
112         {
113             return GSMAMR_ENC_INVALID_PARAM;
114         }
115         // set AMR mode (bits per second)
116         iGsmAmrMode = (GSM_AMR_MODES)aProps->iMode;
117         if (aProps->iBitStreamFormat == AMR_TX_WMF)
118         {
119             iBitStreamFormat = AMR_TX_WMF;
120         }
121         else if (aProps->iBitStreamFormat == AMR_TX_IF2)
122         {
123             iBitStreamFormat = AMR_TX_IF2;
124         }
125         else if (aProps->iBitStreamFormat == AMR_TX_IETF)
126         {
127             iBitStreamFormat = AMR_TX_IETF;
128         }
129         else
130         {
131             iBitStreamFormat = AMR_TX_ETS;
132         }
133     }
134
135     iBytesPerSample = aProps->iInBitsPerSample / 8;
136
137     // set maximum buffer size for encoded data
138     iMaxOutputBufferSize = aMaxOutputBufferSize;
139     // return output parameters that will be used
140     aProps->iOutSamplingRate = KDFLT_GAMR_SAMPLING_RATE;
141     aProps->iOutNumChannels = KDFLT_GAMR_NUM_CHANNELS;
142     aProps->iOutClockRate = aProps->iOutSamplingRate;
143
144     // initialize AMR encoder
145     int32 nResult = AMREncodeInit(&iEncState, &iSidState, false);
146     if (nResult < 0) return(GSMAMR_ENC_CODEC_INIT_FAILURE);
147
148     return GSMAMR_ENC_NO_ERROR;
149 }
150
151
152 ////////////////////////////////////////////////////////////////////////////
153 OSCL_EXPORT_REF int32 CPvGsmAmrEncoder::Encode(TInputAudioStream& aInStream,
154         TOutputAudioStream& aOutStream)
155 {
156     // check first if mode specified is invalid
157     if (IsModeValid(aInStream.iMode) == false)
158         return GSMAMR_ENC_INVALID_MODE;
159
160     // set AMR mode for this set of samples
161     iGsmAmrMode = (GSM_AMR_MODES)aInStream.iMode;
162
163     // get the maximum number of frames // BX
164     int32 bytesPerFrame = iNumSamplesPerFrame * iBytesPerSample;
165     int32 maxNumFrames = aInStream.iSampleLength / bytesPerFrame;
166     uint8 *pFrameIn  = aInStream.iSampleBuffer;
167     uint8 *pFrameOut = aOutStream.iBitStreamBuffer;
168     int32 i;
169
170     // encode samples
171     for (i = 0; i < maxNumFrames; i++)
172     {
173
174         // //////////////////////////////////////////
175         // encode this frame
176         // //////////////////////////////////////////
177         int32 * temp = & iLastModeUsed;
178         Word16 nStatus = AMREncode(iEncState, iSidState,    // BX, Word16 instead of int32 to avoid wierd case(IF2 format): the function returns 31, but nStatus ends up with a big wierd number
179                                    (Mode)iGsmAmrMode,
180                                    (Word16 *)pFrameIn,
181                                    (unsigned char *)pFrameOut,
182                                    (Frame_Type_3GPP*) temp,
183                                    iBitStreamFormat);
184
185         if (nStatus < 0)
186         {
187             // an error when encoding was received, so quit
188             return(GSMAMR_ENC_CODEC_ENCODE_FAILURE);
189         }
190
191         // save nStatus as this indicates the encoded frame size
192         int32 encFrameSize = (int32)nStatus;
193         aOutStream.iSampleFrameSize[i] = encFrameSize;
194         pFrameOut += encFrameSize;
195         pFrameIn  += bytesPerFrame;
196     }
197
198     // set other values to be returned
199     aOutStream.iNumSampleFrames = maxNumFrames;
200     return(GSMAMR_ENC_NO_ERROR);
201 }
202
203 ////////////////////////////////////////////////////////////////////////////
204 OSCL_EXPORT_REF int32 CPvGsmAmrEncoder::CleanupEncoder()
205 {
206     // call terminate function of GSM AMR encoder
207     AMREncodeExit(&iEncState, &iSidState);
208
209     iEncState = NULL;
210     iSidState = NULL;
211
212     return GSMAMR_ENC_NO_ERROR;
213 }
214
215 ////////////////////////////////////////////////////////////////////////////
216 OSCL_EXPORT_REF int32 CPvGsmAmrEncoder::Reset()
217 {
218     // reset GSM AMR encoder (state memory and SID sync function.)
219     Word16 nStatus = AMREncodeReset(&iEncState, &iSidState);
220
221     if (nStatus < 0)
222     {
223         return GSMAMR_ENC_CODEC_ENCODE_FAILURE;
224     }
225     return GSMAMR_ENC_NO_ERROR;
226 }
227
228