sl@0: /*
sl@0: * Copyright (c) 1997-2002 Nokia Corporation and/or its subsidiary(-ies).
sl@0: * All rights reserved.
sl@0: * This component and the accompanying materials are made available
sl@0: * under the terms of "Eclipse Public License v1.0"
sl@0: * which accompanies this distribution, and is available
sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: *
sl@0: * Initial Contributors:
sl@0: * Nokia Corporation - initial contribution.
sl@0: *
sl@0: * Contributors:
sl@0: *
sl@0: * Description:
sl@0: *
sl@0: */
sl@0: 
sl@0: 
sl@0: 
sl@0: #include "MMFAudioS16ToPcmS8Codec.h"
sl@0: 
sl@0: // __________________________________________________________________________
sl@0: // Implementation
sl@0: 
sl@0: CMMFCodec* CMMFAudioS16PcmS8Codec::NewL(TAny* aInitParams)
sl@0: 	{
sl@0: 	CMMFAudioS16PcmS8Codec* self=new(ELeave) CMMFAudioS16PcmS8Codec();
sl@0: 	CleanupStack::PushL(self);
sl@0: 	self->ConstructL(aInitParams);
sl@0: 	CleanupStack::Pop(self);
sl@0: 	return STATIC_CAST( CMMFCodec*, self );
sl@0: 	}
sl@0: 
sl@0: CMMFAudioS16PcmS8Codec::~CMMFAudioS16PcmS8Codec()
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: CMMFAudioS16PcmS8Codec::CMMFAudioS16PcmS8Codec()
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: void CMMFAudioS16PcmS8Codec::ConstructL(TAny* /*aInitParams*/)
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: 
sl@0: //this codec converts 2 bytes into 1
sl@0: TCodecProcessResult CMMFAudioS16PcmS8Codec::ProcessL(const CMMFBuffer& aSrc, CMMFBuffer& aDst)
sl@0: 	{
sl@0: 
sl@0: 	TCodecProcessResult result;
sl@0: 	result.iStatus = TCodecProcessResult::EProcessIncomplete;
sl@0: 
sl@0: 	//convert from generic CMMFBuffer to CMMFDataBuffer
sl@0: 	iSrc = STATIC_CAST(const CMMFDataBuffer*, &aSrc);
sl@0: 	iDst = STATIC_CAST(CMMFDataBuffer*, &aDst);
sl@0: 
sl@0: 	const TUint dstMaxLen = iDst->Data().MaxLength();
sl@0: 
sl@0: 	if (!dstMaxLen)
sl@0: 		User::Leave(KErrArgument);
sl@0: 
sl@0: 	//don't scribble Destination (pDst) by only consuming enough source to fill pDst
sl@0: 	TUint srcUse = (dstMaxLen - iDst->Position()) * 2;
sl@0: 	const TUint srcLen = iSrc->Data().Length();
sl@0: 	const TUint sourceRemain = srcLen - iSrc->Position();
sl@0: 
sl@0: 	//make sure we don't blow source by checking against remaining
sl@0: 	//and clipping to minimum remaining if necessary
sl@0: 	srcUse = (srcUse<sourceRemain ? srcUse : sourceRemain);
sl@0: 	
sl@0: 	//we need to cast away CONST even on the source, as the TClass needs a TUint8*
sl@0: 	TUint8* pSrc = CONST_CAST(TUint8*,iSrc->Data().Ptr());
sl@0: 	pSrc += iSrc->Position();
sl@0: 	TUint8* pDst = CONST_CAST(TUint8*,iDst->Data().Ptr());
sl@0: 	pDst += iDst->Position();
sl@0: 
sl@0: 	//compress TWO bytes (16-bit PCM word) into a to 1 byte sample
sl@0: 	iAudioS16ToS8Pcm.Convert(pSrc, pDst, srcUse / 2);
sl@0: 
sl@0: 	TUint dstBytesAdded = srcUse / 2;
sl@0: 
sl@0: 	if (dstBytesAdded + iDst->Position() < dstMaxLen)
sl@0: 		result.iStatus = TCodecProcessResult::EDstNotFilled;
sl@0: 
sl@0: 	else if (srcUse + iSrc->Position() >= srcLen)
sl@0: 		result.iStatus = TCodecProcessResult::EProcessComplete;
sl@0: 
sl@0: 	result.iSrcBytesProcessed = srcUse;
sl@0: 	result.iDstBytesAdded = dstBytesAdded;
sl@0: 
sl@0: 	iDst->Data().SetLength(iDst->Position() + result.iDstBytesAdded);
sl@0: 
sl@0: 	return result;
sl@0: 
sl@0: 	}