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_ENCODER_WRAPPER_H__
30 #define __GSMAMR_ENCODER_WRAPPER_H__
32 //----------------------------------------------------------------------------
34 //----------------------------------------------------------------------------
35 #include "oscl_base.h"
37 class TInputAudioStream
40 // Pointer to buffer containing the audio samples.
41 // The application is required to allocate this buffer.
44 // Number of samples in bytes contained in iSampleBuffer
47 // Mode of operation (the same as bit rate)
48 // For example, "GSM_AMR_4_75" (for 4.75 kbps) for GSM-AMR
51 // Start time when samples were taken
54 // End time when samples were taken
59 class TOutputAudioStream
62 // Pointer to buffer containing encoded audio samples
63 uint8* iBitStreamBuffer;
65 // Number of sample frames encoded and are contained in the buffer
66 int32 iNumSampleFrames;
68 // Size in bytes of each encoded sample frame;
69 // This variable may point to an array if the sample frame sizes are
70 // variable. For example:
71 // iSampleFrameSize[0] = 23 (frame size of frame #1)
72 // iSampleFrameSize[1] = 12 (frame size of frame #2)
74 // iSampleFrameSize[iNumSampleFrames] = 10 (frame size of last frame)
75 int32* iSampleFrameSize;
77 // Start time of the encoded samples contained in the bit stream buffer
80 // Stop time of encoded samples contained in the bit stream buffer
84 class TEncodeProperties
87 // /////////////////////////////////////////////
88 // Input stream properties (uncompressed audio)
89 // /////////////////////////////////////////////
93 EINTERLEAVE_LR, // interleaved left-right
94 EGROUPED_LR, // non-interleaved left-right
95 ENUM_INTERLEAVE_MODES // number of modes supported
98 // DESCRIPTION: Number of bits per sample. For example, set it to "16"
100 // USAGE: The authoring application is required to fill this in.
101 // The CAEI uses the value for encoding.
102 int32 iInBitsPerSample;
104 // DESCRIPTION: Sampling rate of the input samples in Hz.
105 // For example, set it to "22050" Hz.
106 // USAGE: The authoring application is required to fill this in.
107 // If sampling rate is not known until CAEI is initialized,
108 // use '0'. The CAEI uses the value for encoding.
109 uint32 iInSamplingRate;
111 // DESCRIPTION: Clock rate or time scale to be used for the input timestamps
112 // (ticks per secs). For example, "22050" ticks/sec.
113 // USAGE: The authoring application is required to fill this in.
114 // If sampling rate is not known until CAEI is initialized,
115 // use '0'. The CAEI uses the value for encoding.
118 // DESCRIPTION: Number of input channels:1=Mono,2=Stereo.(Mono uses 1 channel;
119 // Stereo uses 2 channels).
120 // USAGE: The authoring application is required to fill this in.
121 // The CAEI uses the value for encoding.
122 uint8 iInNumChannels;
124 // DESCRIPTION: Whether to interleave or not the multi-channel input samples:
125 // EINTERLEAVE_LR = LRLRLRLRLR (interleaved left-right)
126 // EGROUPED_LR = LLLLLL...RRRRRR (non-interleaved left-right)
127 // USAGE: The authoring application is required to fill this in.
128 // The CAEI uses the value for encoding.
129 EInterleaveMode iInInterleaveMode;
131 // DESCRIPTION: Desired Sampling rate for a given bitrate combination:
132 // For example, set it to "16000" Hz if the encoding 16kbps
133 // mono/stereo or 24 kbps stereo
134 // USAGE: The authoring application is required to fill this in.
135 // The CAEI uses the value for encoding.
136 uint32 iDesiredSamplingRate;
139 // ////////////////////////////////////////////
140 // Output stream properties (compressed audio)
141 // ////////////////////////////////////////////
143 // DESCRIPTION: Mode of operation (the same as bit rate). For example,
144 // "GSM_AMR_4_75" (for 4.75 kbps).
145 // USAGE: The authoring application is required to fill this in.
146 // The CAEI uses the value to configure the codec library.
149 // DESCRIPTION: Bit order format:
150 // TRUE = MSB..................LSB
151 // d7 d6 d5 d4 d3 d2 d1 d0
152 // FALSE = MSB..................LSB
153 // d0 d1 d2 d3 d4 d5 d6 d7
154 // USAGE: The authoring application is required to fill this in.
155 // The CAEI will use the value to setup the codec library.
156 int32 iBitStreamFormat;
158 // DESCRIPTION: Audio object type for the output bitstream; only applies
160 // USAGE: The application is required to fill this in.
161 // The CADI will use the value to setup the codec library.
162 int32 iAudioObjectType;
164 // DESCRIPTION: Final sampling frequency used when encoding in Hz.
165 // For example, "44100" Hz.
166 // USAGE: If the input sampling rate is not appropriate (e.g.,
167 // the codec requires a different sampling frequency),
168 // the CAEI will fill this in with the final sampling
169 // rate. The CAEI will perform resampling if the
170 // input sampling frequency is not the same as the output
171 // sampling frequency.
172 uint32 iOutSamplingRate;
174 // DESCRIPTION: Number of output channels:1=Mono,2=Stereo. (Mono uses 1
175 // channel; Stereo uses 2 channels).
176 // USAGE: The CAEI will fill this in if it needs to convert
177 // the input samples to what is required by the codec.
178 uint8 iOutNumChannels;
180 // DESCRIPTION: Clock rate or time scale used for the timestamps (ticks per secs)
181 // For example, "8000" ticks/sec.
182 // USAGE: The CAEI will fill this in if the input data will be
184 uint32 iOutClockRate;
190 ** This class encodes audio samples using the GSM-AMR algorithm.
191 ** This codec operates on a 20-msec frame duration corresponding to 160
192 ** samples at the sampling frequency of 8000 samples/sec. The size of a frame
193 ** is 320 bytes. For each 20-ms frame, a bit-rate of 4.75, 5.15, 5.90, 6.70,
194 ** 7.40, 7.95, 10.2, or 12.2 kbits/sec can be produced.
198 ** // create a GSM-AMR encoder object
199 ** CGsmAmrEncoder* myAppEncoder = OSCL_NEW(CGsmAmrEncoder, ());
200 ** // set input parameters
201 ** TEncodeProperties myProps;
202 ** myProps.iInSamplingRate = 8000;
203 ** myProps.iInBitsPerSample = 16;
204 ** myProps.iOutBitRate = CGsmAmrEncoder::GSM_AMR_12_2;
205 ** myAppEncoder->InitializeEncoder(myProps, 2000);
207 ** // encode a sample block
208 ** myAppEncoder->Encode(myInput, myOutput);
210 // // done encoding so cleanup
211 ** myAppEncoder->CleanupEncoder();
212 ** OSCL_DELETE(myAppEncoder);
216 class CPvGsmAmrEncoder
219 //! Constructor -- creates a GSM-AMR encoder object
220 OSCL_IMPORT_REF CPvGsmAmrEncoder();
222 //! Destructor -- destroys the GSM-AMR encoder object
223 OSCL_IMPORT_REF ~CPvGsmAmrEncoder();
226 * This function initializes the GSM-AMR encoder.
227 * @param "aMaxOutputBufferSize" "the maximum buffer size for the output buffer when Encode() gets called"
228 * @param "aProps" "TEncodeProperties based pointer for the input encoding setting. if aProps=NULL, then
229 * default settings will be set"
230 * @return 0 for the correct operation, and -1 for the wrong operation
232 OSCL_IMPORT_REF int32 InitializeEncoder(int32 aMaxOutputBufferSize,
233 TEncodeProperties* aProps = NULL);
236 * This function initializes the GSM-AMR encoder.
237 * @param "aInStream" "TInputAudioStream based reference object that contains the input buffer and buffer size and timestamp info"
238 * @param "aOutStream" "TOutputAudioStream based reference object that contains the output buffer for compressed data
239 * @return 0 for the correct operation, and -1 for the wrong operation
241 OSCL_IMPORT_REF int32 Encode(TInputAudioStream& aInStream,
242 TOutputAudioStream& aOutStream);
245 * This function cleans up the encoder workspace when done encoding.
247 OSCL_IMPORT_REF int32 CleanupEncoder();
250 * This function reset the encoder workspace.
252 OSCL_IMPORT_REF int32 Reset();
257 // ** values should be the same as the Mode enum specified by AMR library
269 GSM_AMR_N_MODES /* number of (SPC) modes */
275 * This inline function checks whether the specified mode is valid or not.
276 * @param "aMode" "input the current mode to be used in encoding"
277 * @return true for the valid mode, and false for the wrong mode
279 inline bool IsModeValid(int32 aMode)
281 return((aMode < GSM_AMR_N_MODES) && (aMode >= 0));
286 // GSM AMR encoder state variables
290 // contains the current mode of GSM AMR
291 GSM_AMR_MODES iGsmAmrMode;
296 // number of samples per frame (granulity)
297 int32 iNumSamplesPerFrame;
298 // number of bytes per sample
299 int32 iBytesPerSample;
301 // maximum size allowed for output buffer
302 int32 iMaxOutputBufferSize;
305 int32 iBitStreamFormat;
311 GSMAMR_ENC_NO_ERROR = 0,
312 GSMAMR_ENC_NO_MEMORY_ERROR = -1,
313 GSMAMR_ENC_CODEC_INIT_FAILURE = -2,
314 GSMAMR_ENC_CODEC_NOT_INITIALIZED = -3,
315 GSMAMR_ENC_INVALID_PARAM = -4,
316 GSMAMR_ENC_INVALID_MODE = -5,
317 GSMAMR_ENC_CODEC_ENCODE_FAILURE = -6,
318 GSMAMR_ENC_MEMORY_OVERFLOW = -7
321 #endif // __GSMAMR_ENCODER_WRAPPER_H__