First public contribution.
2 * Copyright (c) 1997-2002 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
20 #include "MMFPcm16ToImaAdPcmStereoCodec.h"
22 // __________________________________________________________________________
25 CMMFCodec* CMMFPcm16ImaAdPcmStereoCodec::NewL(TAny* aInitParams)
27 CMMFPcm16ImaAdPcmStereoCodec* self=new(ELeave) CMMFPcm16ImaAdPcmStereoCodec();
28 CleanupStack::PushL(self);
29 self->ConstructL(aInitParams);
30 CleanupStack::Pop(self);
31 return STATIC_CAST( CMMFCodec*, self );
34 CMMFPcm16ImaAdPcmStereoCodec::~CMMFPcm16ImaAdPcmStereoCodec()
38 CMMFPcm16ImaAdPcmStereoCodec::CMMFPcm16ImaAdPcmStereoCodec() : i16PcmToImaAdpcm(2)
42 void CMMFPcm16ImaAdPcmStereoCodec::ConstructL(TAny* /*aInitParams*/)
44 iTempSrcBufferPtr = iTempSrcBuffer;
45 iTempSrcBufferCount = 0;
48 /*************************************************************
49 CMMFPcm16ImaAdPcmStereoCodec::ProcessL
51 This function converts stereo PCM samples to IMA ADPCM samples
52 in blocks of KImaAdpcmBlockAlign (256) bytes. Any left
53 over bytes that do not fill a block are currently discarded.
54 996 source bytes are required to fill a 256 byte block.
55 **************************************************************/
56 TCodecProcessResult CMMFPcm16ImaAdPcmStereoCodec::ProcessL(const CMMFBuffer& aSrc, CMMFBuffer& aDst)
58 TCodecProcessResult result;
59 result.iStatus = TCodecProcessResult::EProcessIncomplete;
61 //convert from generic CMMFBuffer to CMMFDataBuffer
62 iSrc = STATIC_CAST(const CMMFDataBuffer*, &aSrc);
63 iDst = STATIC_CAST(CMMFDataBuffer*, &aDst);
65 const TUint srcLen = iSrc->Data().Length();
66 const TUint dstMaxLen = iDst->Data().MaxLength();
67 const TUint sourceRemain = srcLen - iSrc->Position();
69 if (dstMaxLen < KImaAdpcmBlockAlign)
70 User::Leave(KErrArgument);
72 //reset data if not a consecutive frame number
73 if ((iSrc->FrameNumber() != iLastFrameNumber) && (iSrc->FrameNumber() != (iLastFrameNumber+1)))
75 iTempSrcBufferPtr = iTempSrcBuffer;
76 iTempSrcBufferCount = 0;
78 iLastFrameNumber = iSrc->FrameNumber();
80 TUint dstRemain = (dstMaxLen - iDst->Position());
81 TUint srcToFillTempBuffer = 0;
83 //take account of src to be added to temporary buffer
84 if (iTempSrcBufferCount > 0)
86 srcToFillTempBuffer = KImaAdpcmStereoTempBufferSize - iTempSrcBufferCount;
88 if (srcToFillTempBuffer<sourceRemain) //enough source to fill temporary buffer
89 dstRemain -= KImaAdpcmBlockAlign;
90 else //not enough source to fill the temporary buffer
91 srcToFillTempBuffer = sourceRemain;
94 //calculate how much source is required to fill the destination buffer
95 TUint blocksRemaining = dstRemain/KImaAdpcmBlockAlign;
96 TUint maxUsableDst = blocksRemaining * KImaAdpcmBlockAlign;
97 TUint srcToUse = blocksRemaining * KImaAdpcmStereoSamplesPerBlock * 4;
99 srcToUse += srcToFillTempBuffer;
100 srcToUse = (srcToUse<sourceRemain ? srcToUse : sourceRemain);
102 //we need to cast away CONST even on the source, as the TClass needs a TUint8*
103 TUint8* pSrc = CONST_CAST(TUint8*,iSrc->Data().Ptr());
104 pSrc += iSrc->Position();
105 TUint8* pDst = CONST_CAST(TUint8*,iDst->Data().Ptr());
106 pDst += iDst->Position();
108 TUint dstBytesAdded = 0;
109 TUint srcLeft = srcToUse;
111 //convert remaining source from previous call to ProcessL
112 if (iTempSrcBufferCount > 0)
114 //Fill temp buffer from source buffer
115 while((iTempSrcBufferCount < KImaAdpcmStereoTempBufferSize) && (srcLeft))
117 *iTempSrcBufferPtr++ = *pSrc++;
118 iTempSrcBufferCount++;
122 if (iTempSrcBufferCount == KImaAdpcmStereoTempBufferSize)
125 iTempSrcBufferCount = 0;
126 iTempSrcBufferPtr = iTempSrcBuffer;
128 i16PcmToImaAdpcm.Convert(iTempSrcBufferPtr, pDst, KImaAdpcmStereoSamplesPerBlock);
130 pDst += KImaAdpcmBlockAlign;
131 dstBytesAdded += KImaAdpcmBlockAlign;
135 //convert full blocks
136 while (srcLeft >= (KImaAdpcmStereoSamplesPerBlock*4))
138 i16PcmToImaAdpcm.Convert(pSrc, pDst, KImaAdpcmStereoSamplesPerBlock);
140 pSrc += KImaAdpcmStereoSamplesPerBlock*4;
141 pDst += KImaAdpcmBlockAlign;
143 dstBytesAdded += KImaAdpcmBlockAlign;
144 srcLeft -= KImaAdpcmStereoSamplesPerBlock*4;
147 //save remaining source in iTempSrcBuffer
150 *iTempSrcBufferPtr++ = *pSrc++;
151 iTempSrcBufferCount++;
155 //if the source buffer is consumed
156 if ((srcLen == srcToUse + iSrc->Position()))
158 if (dstBytesAdded < maxUsableDst)
159 result.iStatus = TCodecProcessResult::EDstNotFilled;
161 result.iStatus = TCodecProcessResult::EProcessComplete;
164 result.iSrcBytesProcessed = srcToUse;
165 result.iDstBytesAdded = dstBytesAdded;
167 iDst->Data().SetLength(iDst->Position() + result.iDstBytesAdded);