sl@0
|
1 |
// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
//
|
sl@0
|
15 |
|
sl@0
|
16 |
|
sl@0
|
17 |
#include "MMFImaAdPcmToPcm16Codec.h"
|
sl@0
|
18 |
#include "MMFImaAudioCodecState.h"
|
sl@0
|
19 |
|
sl@0
|
20 |
#include <mmf/common/mmfaudio.h> // TMMFAudioConfig
|
sl@0
|
21 |
#include <mmf/plugin/mmfcodecimplementationuids.hrh> // KUidMmfCodecAudioSettings
|
sl@0
|
22 |
|
sl@0
|
23 |
const TUid KUidCodecAudioConfig = {KUidMmfCodecAudioSettings};
|
sl@0
|
24 |
|
sl@0
|
25 |
// __________________________________________________________________________
|
sl@0
|
26 |
// Implementation
|
sl@0
|
27 |
|
sl@0
|
28 |
CMMFCodec* CMMFImaAdPcmPcm16Codec::NewL(TAny* aInitParams)
|
sl@0
|
29 |
{
|
sl@0
|
30 |
CMMFImaAdPcmPcm16Codec* self=new(ELeave) CMMFImaAdPcmPcm16Codec();
|
sl@0
|
31 |
CleanupStack::PushL(self);
|
sl@0
|
32 |
self->ConstructL(aInitParams);
|
sl@0
|
33 |
CleanupStack::Pop(self);
|
sl@0
|
34 |
return STATIC_CAST( CMMFCodec*, self );
|
sl@0
|
35 |
}
|
sl@0
|
36 |
|
sl@0
|
37 |
CMMFImaAdPcmPcm16Codec::~CMMFImaAdPcmPcm16Codec()
|
sl@0
|
38 |
{
|
sl@0
|
39 |
}
|
sl@0
|
40 |
|
sl@0
|
41 |
CMMFImaAdPcmPcm16Codec::CMMFImaAdPcmPcm16Codec() : iImaAdpcmTo16Pcm(1)
|
sl@0
|
42 |
{
|
sl@0
|
43 |
}
|
sl@0
|
44 |
|
sl@0
|
45 |
void CMMFImaAdPcmPcm16Codec::ConstructL(TAny* /*aInitParams*/)
|
sl@0
|
46 |
{
|
sl@0
|
47 |
iTempSrcBufferPtr = iTempSrcBuffer;
|
sl@0
|
48 |
iTempSrcBufferCount = 0;
|
sl@0
|
49 |
|
sl@0
|
50 |
iChannels = 1;
|
sl@0
|
51 |
iSamplesRate = 0;
|
sl@0
|
52 |
iSamplesPerBlock = KImaAdpcmSamplesPerBlock;
|
sl@0
|
53 |
iBlockAlign = KImaAdpcmBlockAlign;
|
sl@0
|
54 |
}
|
sl@0
|
55 |
|
sl@0
|
56 |
void CMMFImaAdPcmPcm16Codec::ResetL()
|
sl@0
|
57 |
{
|
sl@0
|
58 |
//Reset the actual codec
|
sl@0
|
59 |
TMMFImaAdpcmCodecStateOld state;
|
sl@0
|
60 |
state.iIndex = 0;
|
sl@0
|
61 |
state.iPredicted = 0;
|
sl@0
|
62 |
iImaAdpcmTo16Pcm.SetState(state);
|
sl@0
|
63 |
iTempSrcBufferPtr = iTempSrcBuffer;
|
sl@0
|
64 |
iTempSrcBufferCount = 0;
|
sl@0
|
65 |
}
|
sl@0
|
66 |
|
sl@0
|
67 |
/**
|
sl@0
|
68 |
CMMFImaAdPcmPcm16Codec::ProcessL
|
sl@0
|
69 |
|
sl@0
|
70 |
This function converts IMA ADPCM samples to PCM 16 samples,
|
sl@0
|
71 |
it is for mono ADPCM only at the moment.
|
sl@0
|
72 |
*/
|
sl@0
|
73 |
TCodecProcessResult CMMFImaAdPcmPcm16Codec::ProcessL(const CMMFBuffer& aSrc, CMMFBuffer& aDst)
|
sl@0
|
74 |
{
|
sl@0
|
75 |
TCodecProcessResult result;
|
sl@0
|
76 |
result.iStatus = TCodecProcessResult::EProcessIncomplete;
|
sl@0
|
77 |
|
sl@0
|
78 |
//convert from generic CMMFBuffer to CMMFDataBuffer
|
sl@0
|
79 |
iSrc = STATIC_CAST(const CMMFDataBuffer*, &aSrc);
|
sl@0
|
80 |
iDst = STATIC_CAST(CMMFDataBuffer*, &aDst);
|
sl@0
|
81 |
|
sl@0
|
82 |
const TUint srcLen = iSrc->Data().Length();
|
sl@0
|
83 |
const TUint dstMaxLen = iDst->Data().MaxLength();
|
sl@0
|
84 |
const TUint sourceRemain = srcLen - iSrc->Position();
|
sl@0
|
85 |
|
sl@0
|
86 |
if (dstMaxLen < (iSamplesPerBlock * 2))
|
sl@0
|
87 |
User::Leave(KErrArgument);
|
sl@0
|
88 |
|
sl@0
|
89 |
if ((iSrc->FrameNumber() == 0) && (iDst->Position() == 0))
|
sl@0
|
90 |
{
|
sl@0
|
91 |
ResetL();
|
sl@0
|
92 |
}
|
sl@0
|
93 |
|
sl@0
|
94 |
//reset data if not a consecutive frame number
|
sl@0
|
95 |
if ((iSrc->FrameNumber() != iLastFrameNumber) && (iSrc->FrameNumber() != (iLastFrameNumber+1)))
|
sl@0
|
96 |
{
|
sl@0
|
97 |
iTempSrcBufferPtr = iTempSrcBuffer;
|
sl@0
|
98 |
iTempSrcBufferCount = 0;
|
sl@0
|
99 |
}
|
sl@0
|
100 |
iLastFrameNumber = iSrc->FrameNumber();
|
sl@0
|
101 |
|
sl@0
|
102 |
TUint dstRemain = (dstMaxLen - iDst->Position());
|
sl@0
|
103 |
TUint srcToFillTempBuffer = 0;
|
sl@0
|
104 |
|
sl@0
|
105 |
//take account of src to be added to temporary buffer
|
sl@0
|
106 |
if (iTempSrcBufferCount > 0)
|
sl@0
|
107 |
{
|
sl@0
|
108 |
srcToFillTempBuffer = iBlockAlign - iTempSrcBufferCount;
|
sl@0
|
109 |
|
sl@0
|
110 |
if (srcToFillTempBuffer < sourceRemain) //enough source to fill temporary buffer
|
sl@0
|
111 |
dstRemain -= (iSamplesPerBlock * 2);
|
sl@0
|
112 |
else //not enough source to fill the temporary buffer
|
sl@0
|
113 |
srcToFillTempBuffer = sourceRemain;
|
sl@0
|
114 |
}
|
sl@0
|
115 |
|
sl@0
|
116 |
//calculate how much source is required to fill the destination buffer
|
sl@0
|
117 |
TUint blocksRemaining = dstRemain / (iSamplesPerBlock * 2);
|
sl@0
|
118 |
TUint maxUsableDst = blocksRemaining * iSamplesPerBlock * 2;
|
sl@0
|
119 |
TUint srcToUse = blocksRemaining * iBlockAlign;
|
sl@0
|
120 |
|
sl@0
|
121 |
srcToUse += srcToFillTempBuffer;
|
sl@0
|
122 |
srcToUse = (srcToUse < sourceRemain) ? srcToUse : sourceRemain;
|
sl@0
|
123 |
|
sl@0
|
124 |
//we need to cast away CONST even on the source, as the TClass needs a TUint8*
|
sl@0
|
125 |
TUint8* pSrc = CONST_CAST(TUint8*,iSrc->Data().Ptr());
|
sl@0
|
126 |
pSrc += iSrc->Position();
|
sl@0
|
127 |
TUint8* pDst = CONST_CAST(TUint8*,iDst->Data().Ptr());
|
sl@0
|
128 |
pDst += iDst->Position();
|
sl@0
|
129 |
|
sl@0
|
130 |
TUint dstBytesAdded = 0;
|
sl@0
|
131 |
TUint srcLeft = srcToUse;
|
sl@0
|
132 |
|
sl@0
|
133 |
//convert remaining source from previous call to ProcessL
|
sl@0
|
134 |
if (iTempSrcBufferCount > 0)
|
sl@0
|
135 |
{
|
sl@0
|
136 |
//Fill temp buffer from source buffer
|
sl@0
|
137 |
while((iTempSrcBufferCount < iBlockAlign) && (srcLeft))
|
sl@0
|
138 |
{
|
sl@0
|
139 |
*iTempSrcBufferPtr++ = *pSrc++;
|
sl@0
|
140 |
iTempSrcBufferCount++;
|
sl@0
|
141 |
srcLeft --;
|
sl@0
|
142 |
}
|
sl@0
|
143 |
|
sl@0
|
144 |
if (iTempSrcBufferCount == iBlockAlign) //temp buffer full
|
sl@0
|
145 |
{
|
sl@0
|
146 |
//reset
|
sl@0
|
147 |
iTempSrcBufferCount = 0;
|
sl@0
|
148 |
iTempSrcBufferPtr = iTempSrcBuffer;
|
sl@0
|
149 |
|
sl@0
|
150 |
iImaAdpcmTo16Pcm.Convert(iTempSrcBufferPtr, pDst, iSamplesPerBlock);
|
sl@0
|
151 |
|
sl@0
|
152 |
pDst += (iSamplesPerBlock * 2);
|
sl@0
|
153 |
dstBytesAdded += (iSamplesPerBlock * 2);
|
sl@0
|
154 |
}
|
sl@0
|
155 |
}
|
sl@0
|
156 |
|
sl@0
|
157 |
//convert full blocks
|
sl@0
|
158 |
while (srcLeft >= iBlockAlign)
|
sl@0
|
159 |
{
|
sl@0
|
160 |
iImaAdpcmTo16Pcm.Convert(pSrc, pDst, iSamplesPerBlock);
|
sl@0
|
161 |
|
sl@0
|
162 |
pSrc += iBlockAlign;
|
sl@0
|
163 |
pDst += (iSamplesPerBlock * 2);
|
sl@0
|
164 |
|
sl@0
|
165 |
dstBytesAdded += (iSamplesPerBlock * 2);
|
sl@0
|
166 |
srcLeft -= iBlockAlign;
|
sl@0
|
167 |
}
|
sl@0
|
168 |
|
sl@0
|
169 |
while (srcLeft)
|
sl@0
|
170 |
{
|
sl@0
|
171 |
*iTempSrcBufferPtr++ = *pSrc++;
|
sl@0
|
172 |
iTempSrcBufferCount++;
|
sl@0
|
173 |
srcLeft--;
|
sl@0
|
174 |
}
|
sl@0
|
175 |
|
sl@0
|
176 |
//if the source buffer is consumed
|
sl@0
|
177 |
if ((srcLen == srcToUse + iSrc->Position()))
|
sl@0
|
178 |
{
|
sl@0
|
179 |
if (dstBytesAdded < maxUsableDst)
|
sl@0
|
180 |
result.iStatus = TCodecProcessResult::EDstNotFilled;
|
sl@0
|
181 |
else
|
sl@0
|
182 |
result.iStatus = TCodecProcessResult::EProcessComplete;
|
sl@0
|
183 |
}
|
sl@0
|
184 |
|
sl@0
|
185 |
result.iSrcBytesProcessed = srcToUse;
|
sl@0
|
186 |
result.iDstBytesAdded = dstBytesAdded;
|
sl@0
|
187 |
|
sl@0
|
188 |
iDst->Data().SetLength( iDst->Position() + result.iDstBytesAdded);
|
sl@0
|
189 |
|
sl@0
|
190 |
return result;
|
sl@0
|
191 |
}
|
sl@0
|
192 |
|
sl@0
|
193 |
/**
|
sl@0
|
194 |
Sets codec configuration.
|
sl@0
|
195 |
|
sl@0
|
196 |
@param aConfigType
|
sl@0
|
197 |
The UID of the codec to configure.
|
sl@0
|
198 |
@param aConfigData
|
sl@0
|
199 |
The configuration information.
|
sl@0
|
200 |
*/
|
sl@0
|
201 |
void CMMFImaAdPcmPcm16Codec::ConfigureL(TUid aConfigType, const TDesC8& aConfigData)
|
sl@0
|
202 |
{
|
sl@0
|
203 |
if (aConfigType != KUidCodecAudioConfig)
|
sl@0
|
204 |
{
|
sl@0
|
205 |
User::Leave(KErrArgument);
|
sl@0
|
206 |
}
|
sl@0
|
207 |
|
sl@0
|
208 |
const TMMFAudioConfig& audioConfig = static_cast<const TPckgBuf<TMMFAudioConfig>&>(aConfigData)();
|
sl@0
|
209 |
|
sl@0
|
210 |
iChannels = audioConfig.iChannels;
|
sl@0
|
211 |
iSamplesRate = audioConfig.iSampleRate;
|
sl@0
|
212 |
|
sl@0
|
213 |
switch (iSamplesRate * iChannels)
|
sl@0
|
214 |
{
|
sl@0
|
215 |
case 8000: // fall through, same as 11025
|
sl@0
|
216 |
case 11025:
|
sl@0
|
217 |
case 16000:
|
sl@0
|
218 |
iBlockAlign = 256;
|
sl@0
|
219 |
break;
|
sl@0
|
220 |
case 22050:
|
sl@0
|
221 |
iBlockAlign = 512;
|
sl@0
|
222 |
break;
|
sl@0
|
223 |
|
sl@0
|
224 |
case 44100:
|
sl@0
|
225 |
iBlockAlign = 1024;
|
sl@0
|
226 |
break;
|
sl@0
|
227 |
|
sl@0
|
228 |
case 88200:
|
sl@0
|
229 |
iBlockAlign = 2048;
|
sl@0
|
230 |
break;
|
sl@0
|
231 |
|
sl@0
|
232 |
default:
|
sl@0
|
233 |
User::Leave(KErrArgument);
|
sl@0
|
234 |
}
|
sl@0
|
235 |
|
sl@0
|
236 |
|
sl@0
|
237 |
// SamplesPerBlock = [(BlockAlign - 4 * Channels) * 8] / (BitsPerSample * Channels) + 1
|
sl@0
|
238 |
iSamplesPerBlock = (iBlockAlign - 4 * iChannels) * 8 / (KImaAdpcmBitsPerSample * iChannels) + 1;
|
sl@0
|
239 |
}
|
sl@0
|
240 |
|
sl@0
|
241 |
TInt CMMFImaAdPcmPcm16Codec::Extension_(TUint aExtensionId, TAny*& aExtPtr, TAny*)
|
sl@0
|
242 |
{
|
sl@0
|
243 |
if (aExtensionId == KUidCustomInterfaceDevSoundFileBlockLength.iUid)
|
sl@0
|
244 |
{
|
sl@0
|
245 |
aExtPtr = static_cast<MMMFDevSoundCustomInterfaceFileBlockLength*>(this);
|
sl@0
|
246 |
return KErrNone;
|
sl@0
|
247 |
}
|
sl@0
|
248 |
else
|
sl@0
|
249 |
{
|
sl@0
|
250 |
return CMMFCodec::Extension_(aExtensionId, aExtPtr, NULL);
|
sl@0
|
251 |
}
|
sl@0
|
252 |
}
|
sl@0
|
253 |
|
sl@0
|
254 |
void CMMFImaAdPcmPcm16Codec::SetFileBlockLength(TUint aBlockAlign)
|
sl@0
|
255 |
{
|
sl@0
|
256 |
iBlockAlign = aBlockAlign;
|
sl@0
|
257 |
iSamplesPerBlock = (iBlockAlign - 4 * iChannels) * 8 / (KImaAdpcmBitsPerSample * iChannels) + 1;
|
sl@0
|
258 |
|
sl@0
|
259 |
}
|
sl@0
|
260 |
|