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 "MMFAudioS16ToPcmS8Codec.h"
22 // __________________________________________________________________________
25 CMMFCodec* CMMFAudioS16PcmS8Codec::NewL(TAny* aInitParams)
27 CMMFAudioS16PcmS8Codec* self=new(ELeave) CMMFAudioS16PcmS8Codec();
28 CleanupStack::PushL(self);
29 self->ConstructL(aInitParams);
30 CleanupStack::Pop(self);
31 return STATIC_CAST( CMMFCodec*, self );
34 CMMFAudioS16PcmS8Codec::~CMMFAudioS16PcmS8Codec()
38 CMMFAudioS16PcmS8Codec::CMMFAudioS16PcmS8Codec()
42 void CMMFAudioS16PcmS8Codec::ConstructL(TAny* /*aInitParams*/)
47 //this codec converts 2 bytes into 1
48 TCodecProcessResult CMMFAudioS16PcmS8Codec::ProcessL(const CMMFBuffer& aSrc, CMMFBuffer& aDst)
51 TCodecProcessResult result;
52 result.iStatus = TCodecProcessResult::EProcessIncomplete;
54 //convert from generic CMMFBuffer to CMMFDataBuffer
55 iSrc = STATIC_CAST(const CMMFDataBuffer*, &aSrc);
56 iDst = STATIC_CAST(CMMFDataBuffer*, &aDst);
58 const TUint dstMaxLen = iDst->Data().MaxLength();
61 User::Leave(KErrArgument);
63 //don't scribble Destination (pDst) by only consuming enough source to fill pDst
64 TUint srcUse = (dstMaxLen - iDst->Position()) * 2;
65 const TUint srcLen = iSrc->Data().Length();
66 const TUint sourceRemain = srcLen - iSrc->Position();
68 //make sure we don't blow source by checking against remaining
69 //and clipping to minimum remaining if necessary
70 srcUse = (srcUse<sourceRemain ? srcUse : sourceRemain);
72 //we need to cast away CONST even on the source, as the TClass needs a TUint8*
73 TUint8* pSrc = CONST_CAST(TUint8*,iSrc->Data().Ptr());
74 pSrc += iSrc->Position();
75 TUint8* pDst = CONST_CAST(TUint8*,iDst->Data().Ptr());
76 pDst += iDst->Position();
78 //compress TWO bytes (16-bit PCM word) into a to 1 byte sample
79 iAudioS16ToS8Pcm.Convert(pSrc, pDst, srcUse / 2);
81 TUint dstBytesAdded = srcUse / 2;
83 if (dstBytesAdded + iDst->Position() < dstMaxLen)
84 result.iStatus = TCodecProcessResult::EDstNotFilled;
86 else if (srcUse + iSrc->Position() >= srcLen)
87 result.iStatus = TCodecProcessResult::EProcessComplete;
89 result.iSrcBytesProcessed = srcUse;
90 result.iDstBytesAdded = dstBytesAdded;
92 iDst->Data().SetLength(iDst->Position() + result.iDstBytesAdded);