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 126 { 127 iBitStreamFormat = AMR_TX_ETS; 128 } 129 } 130 131 iBytesPerSample = aProps->iInBitsPerSample / 8; 132 133 // set maximum buffer size for encoded data 134 iMaxOutputBufferSize = aMaxOutputBufferSize; 135 // return output parameters that will be used 136 aProps->iOutSamplingRate = KDFLT_GAMR_SAMPLING_RATE; 137 aProps->iOutNumChannels = KDFLT_GAMR_NUM_CHANNELS; 138 aProps->iOutClockRate = aProps->iOutSamplingRate; 139 140 // initialize AMR encoder 141 int32 nResult = AMREncodeInit(&iEncState, &iSidState, false); 142 if (nResult < 0) return(GSMAMR_ENC_CODEC_INIT_FAILURE); 143 144 return GSMAMR_ENC_NO_ERROR; 145 } 146 147 148 //////////////////////////////////////////////////////////////////////////// 149 OSCL_EXPORT_REF int32 CPvGsmAmrEncoder::Encode(TInputAudioStream& aInStream, 150 TOutputAudioStream& aOutStream) 151 { 152 // check first if mode specified is invalid 153 if (IsModeValid(aInStream.iMode) == false) 154 return GSMAMR_ENC_INVALID_MODE; 155 156 // set AMR mode for this set of samples 157 iGsmAmrMode = (GSM_AMR_MODES)aInStream.iMode; 158 159 // get the maximum number of frames // BX 160 int32 bytesPerFrame = iNumSamplesPerFrame * iBytesPerSample; 161 int32 maxNumFrames = aInStream.iSampleLength / bytesPerFrame; 162 uint8 *pFrameIn = aInStream.iSampleBuffer; 163 uint8 *pFrameOut = aOutStream.iBitStreamBuffer; 164 int32 i; 165 166 // encode samples 167 for (i = 0; i < maxNumFrames; i++) 168 { 169 170 // ////////////////////////////////////////// 171 // encode this frame 172 // ////////////////////////////////////////// 173 int32 * temp = & iLastModeUsed; 174 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 175 (Mode)iGsmAmrMode, 176 (Word16 *)pFrameIn, 177 (unsigned char *)pFrameOut, 178 (Frame_Type_3GPP*) temp, 179 iBitStreamFormat); 180 181 if (nStatus < 0) 182 { 183 // an error when encoding was received, so quit 184 return(GSMAMR_ENC_CODEC_ENCODE_FAILURE); 185 } 186 187 // save nStatus as this indicates the encoded frame size 188 int32 encFrameSize = (int32)nStatus; 189 aOutStream.iSampleFrameSize[i] = encFrameSize; 190 pFrameOut += encFrameSize; 191 pFrameIn += bytesPerFrame; 192 } 193 194 // set other values to be returned 195 aOutStream.iNumSampleFrames = maxNumFrames; 196 return(GSMAMR_ENC_NO_ERROR); 197 } 198 199 //////////////////////////////////////////////////////////////////////////// 200 OSCL_EXPORT_REF int32 CPvGsmAmrEncoder::CleanupEncoder() 201 { 202 // call terminate function of GSM AMR encoder 203 AMREncodeExit(&iEncState, &iSidState); 204 205 iEncState = NULL; 206 iSidState = NULL; 207 208 return GSMAMR_ENC_NO_ERROR; 209 } 210 211 //////////////////////////////////////////////////////////////////////////// 212 OSCL_EXPORT_REF int32 CPvGsmAmrEncoder::Reset() 213 { 214 // reset GSM AMR encoder (state memory and SID sync function.) 215 Word16 nStatus = AMREncodeReset(&iEncState, &iSidState); 216 217 if (nStatus < 0) 218 { 219 return GSMAMR_ENC_CODEC_ENCODE_FAILURE; 220 } 221 return GSMAMR_ENC_NO_ERROR; 222 } 223 224 225