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 (AMR) speech codec
23 Available from http://www.3gpp.org
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"
33 #ifndef GSMAMR_ENCODER_WRAPPER_H_INCLUDED
34 #include "gsmamr_encoder_wrapper.h"
37 #ifndef OSCL_DLL_H_INCLUDED
41 // Define entry point for this DLL
42 OSCL_DLL_ENTRY_POINT_DEFAULT()
44 //----------------------------------------------------------------------------
46 //----------------------------------------------------------------------------
48 // Number of samples per frame
49 #define KGAMR_NUM_SAMPLES_PER_FRAME 160
52 #define KDFLT_GAMR_MODE MR475
54 // Default bits per sample for input audio
55 #define KDFLT_GAMR_BITS_PER_SAMPLE 16
57 // Default sampling rate for input audio (in Hz)
58 #define KDFLT_GAMR_SAMPLING_RATE 8000
60 // Default input clock rate for input audio (in ticks/sec)
61 #define KDFLT_GAMR_CLOCK_RATE 8000
63 // Default number of channels
64 #define KDFLT_GAMR_NUM_CHANNELS 1
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)
72 ///////////////////////////////////////////////////////////////////////////////
74 CPvGsmAmrEncoder::CPvGsmAmrEncoder()
76 // initialize member data to default values
79 iGsmAmrMode = (GSM_AMR_MODES)KDFLT_GAMR_MODE;
81 iBitStreamFormat = AMR_TX_WMF;
82 iNumSamplesPerFrame = KGAMR_NUM_SAMPLES_PER_FRAME;
84 ///////////////////////////////////////////////////////////////////////////////
85 OSCL_EXPORT_REF CPvGsmAmrEncoder::~CPvGsmAmrEncoder()
89 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
90 OSCL_EXPORT_REF int32 CPvGsmAmrEncoder::InitializeEncoder(int32 aMaxOutputBufferSize, TEncodeProperties* aProps)
94 // use default parameters
95 TEncodeProperties 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;
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))
113 return GSMAMR_ENC_INVALID_PARAM;
115 // set AMR mode (bits per second)
116 iGsmAmrMode = (GSM_AMR_MODES)aProps->iMode;
117 if (aProps->iBitStreamFormat == AMR_TX_WMF)
119 iBitStreamFormat = AMR_TX_WMF;
121 else if (aProps->iBitStreamFormat == AMR_TX_IF2)
123 iBitStreamFormat = AMR_TX_IF2;
125 else if (aProps->iBitStreamFormat == AMR_TX_IETF)
127 iBitStreamFormat = AMR_TX_IETF;
131 iBitStreamFormat = AMR_TX_ETS;
135 iBytesPerSample = aProps->iInBitsPerSample / 8;
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;
144 // initialize AMR encoder
145 int32 nResult = AMREncodeInit(&iEncState, &iSidState, false);
146 if (nResult < 0) return(GSMAMR_ENC_CODEC_INIT_FAILURE);
148 return GSMAMR_ENC_NO_ERROR;
152 ////////////////////////////////////////////////////////////////////////////
153 OSCL_EXPORT_REF int32 CPvGsmAmrEncoder::Encode(TInputAudioStream& aInStream,
154 TOutputAudioStream& aOutStream)
156 // check first if mode specified is invalid
157 if (IsModeValid(aInStream.iMode) == false)
158 return GSMAMR_ENC_INVALID_MODE;
160 // set AMR mode for this set of samples
161 iGsmAmrMode = (GSM_AMR_MODES)aInStream.iMode;
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;
171 for (i = 0; i < maxNumFrames; i++)
174 // //////////////////////////////////////////
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
181 (unsigned char *)pFrameOut,
182 (Frame_Type_3GPP*) temp,
187 // an error when encoding was received, so quit
188 return(GSMAMR_ENC_CODEC_ENCODE_FAILURE);
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;
198 // set other values to be returned
199 aOutStream.iNumSampleFrames = maxNumFrames;
200 return(GSMAMR_ENC_NO_ERROR);
203 ////////////////////////////////////////////////////////////////////////////
204 OSCL_EXPORT_REF int32 CPvGsmAmrEncoder::CleanupEncoder()
206 // call terminate function of GSM AMR encoder
207 AMREncodeExit(&iEncState, &iSidState);
212 return GSMAMR_ENC_NO_ERROR;
215 ////////////////////////////////////////////////////////////////////////////
216 OSCL_EXPORT_REF int32 CPvGsmAmrEncoder::Reset()
218 // reset GSM AMR encoder (state memory and SID sync function.)
219 Word16 nStatus = AMREncodeReset(&iEncState, &iSidState);
223 return GSMAMR_ENC_CODEC_ENCODE_FAILURE;
225 return GSMAMR_ENC_NO_ERROR;